@@ -63,30 +63,31 @@ bool BaseIndex::Init()
63
63
if (locator.IsNull ()) {
64
64
m_best_block_index = nullptr ;
65
65
} 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);
67
67
}
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 ();
69
70
if (!m_synced) {
70
71
bool prune_violation = false ;
71
72
if (!m_best_block_index) {
72
73
// index is not built yet
73
74
// make sure we have all block data back to the genesis
74
- const CBlockIndex* block = :: ChainActive () .Tip ();
75
+ const CBlockIndex* block = active_chain .Tip ();
75
76
while (block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
76
77
block = block->pprev ;
77
78
}
78
- prune_violation = block != :: ChainActive () .Genesis ();
79
+ prune_violation = block != active_chain .Genesis ();
79
80
}
80
81
// in case the index has a best block set and is not fully synced
81
82
// check if we have the required blocks to continue building the index
82
83
else {
83
84
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)) {
85
86
// if the bestblock is not part of the mainchain, find the fork
86
87
// 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);
88
89
}
89
- const CBlockIndex* block = :: ChainActive () .Tip ();
90
+ const CBlockIndex* block = active_chain .Tip ();
90
91
prune_violation = true ;
91
92
// check backwards from the tip if we have all block data until we reach the indexes bestblock
92
93
while (block_to_test && block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
@@ -104,20 +105,20 @@ bool BaseIndex::Init()
104
105
return true ;
105
106
}
106
107
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)
108
109
{
109
110
AssertLockHeld (cs_main);
110
111
111
112
if (!pindex_prev) {
112
- return :: ChainActive () .Genesis ();
113
+ return chain .Genesis ();
113
114
}
114
115
115
- const CBlockIndex* pindex = :: ChainActive () .Next (pindex_prev);
116
+ const CBlockIndex* pindex = chain .Next (pindex_prev);
116
117
if (pindex) {
117
118
return pindex;
118
119
}
119
120
120
- return :: ChainActive () .Next (:: ChainActive () .FindFork (pindex_prev));
121
+ return chain .Next (chain .FindFork (pindex_prev));
121
122
}
122
123
123
124
void BaseIndex::ThreadSync ()
@@ -140,7 +141,7 @@ void BaseIndex::ThreadSync()
140
141
141
142
{
142
143
LOCK (cs_main);
143
- const CBlockIndex* pindex_next = NextSyncBlock (pindex);
144
+ const CBlockIndex* pindex_next = NextSyncBlock (pindex, m_chainstate-> m_chain );
144
145
if (!pindex_next) {
145
146
m_best_block_index = pindex;
146
147
m_synced = true ;
@@ -203,7 +204,7 @@ bool BaseIndex::Commit()
203
204
bool BaseIndex::CommitInternal (CDBBatch& batch)
204
205
{
205
206
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));
207
208
return true ;
208
209
}
209
210
@@ -279,7 +280,7 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
279
280
const CBlockIndex* locator_tip_index;
280
281
{
281
282
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);
283
284
}
284
285
285
286
if (!locator_tip_index) {
@@ -320,7 +321,7 @@ bool BaseIndex::BlockUntilSyncedToCurrentChain() const
320
321
// Skip the queue-draining stuff if we know we're caught up with
321
322
// ::ChainActive().Tip().
322
323
LOCK (cs_main);
323
- const CBlockIndex* chain_tip = :: ChainActive () .Tip ();
324
+ const CBlockIndex* chain_tip = m_chainstate-> m_chain .Tip ();
324
325
const CBlockIndex* best_block_index = m_best_block_index.load ();
325
326
if (best_block_index->GetAncestor (chain_tip->nHeight ) == chain_tip) {
326
327
return true ;
@@ -337,8 +338,10 @@ void BaseIndex::Interrupt()
337
338
m_interrupt ();
338
339
}
339
340
340
- bool BaseIndex::Start ()
341
+ bool BaseIndex::Start (CChainState& active_chainstate )
341
342
{
343
+ assert (std::addressof (::ChainstateActive ()) == std::addressof (active_chainstate));
344
+ m_chainstate = &active_chainstate;
342
345
// Need to register this ValidationInterface before running Init(), so that
343
346
// callbacks are not missed if Init sets m_synced to true.
344
347
RegisterValidationInterface (this );
0 commit comments