Skip to content

Commit 20f2636

Browse files
committed
fix: init g_txindex only once, downgrade from assert to exception
`g_txindex` should be initialized in `TestChainSetup`'s constructor but in bitcoin#19806 (dash#5236), when portions of the constructor were split into `mineBlocks()`, `g_txindex`'s init was left behind in the latter instead of the former. This meant that every `mineBlocks()` call would re-create a `TxIndex` instance, which is not intended behaviour. Also, a runtime exception is more appropriate and closer to the usage of `BOOST_REQUIRE` in other index `Start()` calls than the harsher `assert`
1 parent f16025f commit 20f2636

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/test/util/setup_common.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@ TestChainSetup::TestChainSetup(int num_blocks, const std::vector<const char*>& e
349349
hash != checkpoints.mapCheckpoints.end() &&
350350
m_node.chainman->ActiveChain().Tip()->GetBlockHash() == hash->second);
351351
}
352+
353+
g_txindex = std::make_unique<TxIndex>(1 << 20, true);
354+
if (!g_txindex->Start(m_node.chainman->ActiveChainstate())) {
355+
throw std::runtime_error("TxIndex::Start() failed.");
356+
}
357+
358+
// Allow tx index to catch up with the block index.
359+
IndexWaitSynced(*g_txindex);
352360
}
353361

354362
void TestChainSetup::mineBlocks(int num_blocks)
@@ -361,10 +369,6 @@ void TestChainSetup::mineBlocks(int num_blocks)
361369
m_coinbase_txns.push_back(b.vtx[0]);
362370
}
363371

364-
g_txindex = std::make_unique<TxIndex>(1 << 20, true);
365-
assert(g_txindex->Start(m_node.chainman->ActiveChainstate()));
366-
367-
// Allow tx index to catch up with the block index.
368372
IndexWaitSynced(*g_txindex);
369373
}
370374

0 commit comments

Comments
 (0)