Skip to content

Commit db33cde

Browse files
committed
index: Add chainstate member to BaseIndex
1 parent f4a47a1 commit db33cde

File tree

8 files changed

+32
-26
lines changed

8 files changed

+32
-26
lines changed

src/index/base.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,31 @@ bool BaseIndex::Init()
6363
if (locator.IsNull()) {
6464
m_best_block_index = nullptr;
6565
} else {
66-
m_best_block_index = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
66+
m_best_block_index = m_chainstate->m_blockman.FindForkInGlobalIndex(m_chainstate->m_chain, locator);
6767
}
68-
m_synced = m_best_block_index.load() == ::ChainActive().Tip();
68+
CChain& active_chain = m_chainstate->m_chain;
69+
m_synced = m_best_block_index.load() == active_chain.Tip();
6970
if (!m_synced) {
7071
bool prune_violation = false;
7172
if (!m_best_block_index) {
7273
// index is not built yet
7374
// make sure we have all block data back to the genesis
74-
const CBlockIndex* block = ::ChainActive().Tip();
75+
const CBlockIndex* block = active_chain.Tip();
7576
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
7677
block = block->pprev;
7778
}
78-
prune_violation = block != ::ChainActive().Genesis();
79+
prune_violation = block != active_chain.Genesis();
7980
}
8081
// in case the index has a best block set and is not fully synced
8182
// check if we have the required blocks to continue building the index
8283
else {
8384
const CBlockIndex* block_to_test = m_best_block_index.load();
84-
if (!ChainActive().Contains(block_to_test)) {
85+
if (!active_chain.Contains(block_to_test)) {
8586
// if the bestblock is not part of the mainchain, find the fork
8687
// and make sure we have all data down to the fork
87-
block_to_test = ::ChainActive().FindFork(block_to_test);
88+
block_to_test = active_chain.FindFork(block_to_test);
8889
}
89-
const CBlockIndex* block = ::ChainActive().Tip();
90+
const CBlockIndex* block = active_chain.Tip();
9091
prune_violation = true;
9192
// check backwards from the tip if we have all block data until we reach the indexes bestblock
9293
while (block_to_test && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
@@ -104,20 +105,20 @@ bool BaseIndex::Init()
104105
return true;
105106
}
106107

107-
static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
108+
static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
108109
{
109110
AssertLockHeld(cs_main);
110111

111112
if (!pindex_prev) {
112-
return ::ChainActive().Genesis();
113+
return chain.Genesis();
113114
}
114115

115-
const CBlockIndex* pindex = ::ChainActive().Next(pindex_prev);
116+
const CBlockIndex* pindex = chain.Next(pindex_prev);
116117
if (pindex) {
117118
return pindex;
118119
}
119120

120-
return ::ChainActive().Next(::ChainActive().FindFork(pindex_prev));
121+
return chain.Next(chain.FindFork(pindex_prev));
121122
}
122123

123124
void BaseIndex::ThreadSync()
@@ -140,7 +141,7 @@ void BaseIndex::ThreadSync()
140141

141142
{
142143
LOCK(cs_main);
143-
const CBlockIndex* pindex_next = NextSyncBlock(pindex);
144+
const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
144145
if (!pindex_next) {
145146
m_best_block_index = pindex;
146147
m_synced = true;
@@ -203,7 +204,7 @@ bool BaseIndex::Commit()
203204
bool BaseIndex::CommitInternal(CDBBatch& batch)
204205
{
205206
LOCK(cs_main);
206-
GetDB().WriteBestBlock(batch, ::ChainActive().GetLocator(m_best_block_index));
207+
GetDB().WriteBestBlock(batch, m_chainstate->m_chain.GetLocator(m_best_block_index));
207208
return true;
208209
}
209210

@@ -279,7 +280,7 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
279280
const CBlockIndex* locator_tip_index;
280281
{
281282
LOCK(cs_main);
282-
locator_tip_index = g_chainman.m_blockman.LookupBlockIndex(locator_tip_hash);
283+
locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
283284
}
284285

285286
if (!locator_tip_index) {
@@ -320,7 +321,7 @@ bool BaseIndex::BlockUntilSyncedToCurrentChain() const
320321
// Skip the queue-draining stuff if we know we're caught up with
321322
// ::ChainActive().Tip().
322323
LOCK(cs_main);
323-
const CBlockIndex* chain_tip = ::ChainActive().Tip();
324+
const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
324325
const CBlockIndex* best_block_index = m_best_block_index.load();
325326
if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
326327
return true;
@@ -337,8 +338,10 @@ void BaseIndex::Interrupt()
337338
m_interrupt();
338339
}
339340

340-
bool BaseIndex::Start()
341+
bool BaseIndex::Start(CChainState& active_chainstate)
341342
{
343+
assert(std::addressof(::ChainstateActive()) == std::addressof(active_chainstate));
344+
m_chainstate = &active_chainstate;
342345
// Need to register this ValidationInterface before running Init(), so that
343346
// callbacks are not missed if Init sets m_synced to true.
344347
RegisterValidationInterface(this);

src/index/base.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <validationinterface.h>
1313

1414
class CBlockIndex;
15+
class CChainState;
1516

1617
struct IndexSummary {
1718
std::string name;
@@ -75,8 +76,9 @@ class BaseIndex : public CValidationInterface
7576
/// to a chain reorganization), the index must halt until Commit succeeds or else it could end up
7677
/// getting corrupted.
7778
bool Commit();
78-
7979
protected:
80+
CChainState* m_chainstate{nullptr};
81+
8082
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
8183

8284
void ChainStateFlushed(const CBlockLocator& locator) override;
@@ -117,7 +119,7 @@ class BaseIndex : public CValidationInterface
117119

118120
/// Start initializes the sync state and registers the instance as a
119121
/// ValidationInterface so that it stays in sync with blockchain updates.
120-
[[nodiscard]] bool Start();
122+
[[nodiscard]] bool Start(CChainState& active_chainstate);
121123

122124
/// Stops the instance from staying in sync with blockchain updates.
123125
void Stop();

src/index/coinstatsindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n
266266

267267
{
268268
LOCK(cs_main);
269-
CBlockIndex* iter_tip{g_chainman.m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
269+
CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
270270
const auto& consensus_params{Params().GetConsensus()};
271271

272272
do {

src/index/txindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ bool TxIndex::Init()
204204
// Attempt to migrate txindex from the old database to the new one. Even if
205205
// chain_tip is null, the node could be reindexing and we still want to
206206
// delete txindex records in the old database.
207-
if (!m_db->MigrateData(*pblocktree, ::ChainActive().GetLocator())) {
207+
if (!m_db->MigrateData(*pblocktree, m_chainstate->m_chain.GetLocator())) {
208208
return false;
209209
}
210210

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,21 +1550,21 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15501550
// ********************************************************* Step 8: start indexers
15511551
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
15521552
g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
1553-
if (!g_txindex->Start()) {
1553+
if (!g_txindex->Start(::ChainstateActive())) {
15541554
return false;
15551555
}
15561556
}
15571557

15581558
for (const auto& filter_type : g_enabled_filter_types) {
15591559
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
1560-
if (!GetBlockFilterIndex(filter_type)->Start()) {
1560+
if (!GetBlockFilterIndex(filter_type)->Start(::ChainstateActive())) {
15611561
return false;
15621562
}
15631563
}
15641564

15651565
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
15661566
g_coin_stats_index = std::make_unique<CoinStatsIndex>(/* cache size */ 0, false, fReindex);
1567-
if (!g_coin_stats_index->Start()) {
1567+
if (!g_coin_stats_index->Start(::ChainstateActive())) {
15681568
return false;
15691569
}
15701570
}

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
131131
// BlockUntilSyncedToCurrentChain should return false before index is started.
132132
BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
133133

134-
BOOST_REQUIRE(filter_index.Start());
134+
BOOST_REQUIRE(filter_index.Start(::ChainstateActive()));
135135

136136
// Allow filter index to catch up with the block index.
137137
constexpr int64_t timeout_ms = 10 * 1000;

src/test/coinstatsindex_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
3232
// is started.
3333
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
3434

35-
BOOST_REQUIRE(coin_stats_index.Start());
35+
BOOST_REQUIRE(coin_stats_index.Start(::ChainstateActive()));
3636

3737
// Allow the CoinStatsIndex to catch up with the block index that is syncing
3838
// in a background thread.

src/test/txindex_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <script/standard.h>
88
#include <test/util/setup_common.h>
99
#include <util/time.h>
10+
#include <validation.h>
1011

1112
#include <boost/test/unit_test.hpp>
1213

@@ -27,7 +28,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
2728
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
2829
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
2930

30-
BOOST_REQUIRE(txindex.Start());
31+
BOOST_REQUIRE(txindex.Start(::ChainstateActive()));
3132

3233
// Allow tx index to catch up with the block index.
3334
constexpr int64_t timeout_ms = 10 * 1000;

0 commit comments

Comments
 (0)