Skip to content

Commit

Permalink
fix: init g_txindex only once, downgrade from assert to exception
Browse files Browse the repository at this point in the history
`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`
  • Loading branch information
kwvg committed Jul 18, 2024
1 parent 09239a1 commit a376e93
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ TestChainSetup::TestChainSetup(int num_blocks, const std::vector<const char*>& e
// Generate a num_blocks length chain:
this->mineBlocks(num_blocks);

// Initialize transaction index *after* chain has been constructed
g_txindex = std::make_unique<TxIndex>(1 << 20, true);
if (!g_txindex->Start(m_node.chainman->ActiveChainstate())) {
throw std::runtime_error("TxIndex::Start() failed.");
}
IndexWaitSynced(*g_txindex);

CCheckpointData checkpoints{
{
/* TestChainDATSetup */
Expand Down Expand Up @@ -361,11 +368,10 @@ void TestChainSetup::mineBlocks(int num_blocks)
m_coinbase_txns.push_back(b.vtx[0]);
}

g_txindex = std::make_unique<TxIndex>(1 << 20, true);
assert(g_txindex->Start(m_node.chainman->ActiveChainstate()));

// Allow tx index to catch up with the block index.
IndexWaitSynced(*g_txindex);
if (g_txindex) {
IndexWaitSynced(*g_txindex);
}
}

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

0 comments on commit a376e93

Please sign in to comment.