Skip to content

Commit a376e93

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 09239a1 commit a376e93

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/test/util/setup_common.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ TestChainSetup::TestChainSetup(int num_blocks, const std::vector<const char*>& e
323323
// Generate a num_blocks length chain:
324324
this->mineBlocks(num_blocks);
325325

326+
// Initialize transaction index *after* chain has been constructed
327+
g_txindex = std::make_unique<TxIndex>(1 << 20, true);
328+
if (!g_txindex->Start(m_node.chainman->ActiveChainstate())) {
329+
throw std::runtime_error("TxIndex::Start() failed.");
330+
}
331+
IndexWaitSynced(*g_txindex);
332+
326333
CCheckpointData checkpoints{
327334
{
328335
/* TestChainDATSetup */
@@ -361,11 +368,10 @@ void TestChainSetup::mineBlocks(int num_blocks)
361368
m_coinbase_txns.push_back(b.vtx[0]);
362369
}
363370

364-
g_txindex = std::make_unique<TxIndex>(1 << 20, true);
365-
assert(g_txindex->Start(m_node.chainman->ActiveChainstate()));
366-
367371
// Allow tx index to catch up with the block index.
368-
IndexWaitSynced(*g_txindex);
372+
if (g_txindex) {
373+
IndexWaitSynced(*g_txindex);
374+
}
369375
}
370376

371377
CBlock TestChainSetup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)

0 commit comments

Comments
 (0)