Skip to content

Commit 4d66886

Browse files
committed
refactoring: introduce ChainstateActive()
To be used once we move global functions (e.g. FlushStateToDisk()) into CChainState methods. Thanks to Marco Falke for suggestions
1 parent d7c97ed commit 4d66886

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/validation.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ bool CBlockIndexWorkComparator::operator()(const CBlockIndex *pa, const CBlockIn
8080

8181
CChainState g_chainstate;
8282

83+
CChainState& ChainstateActive() { return g_chainstate; }
84+
8385
CChain& ChainActive() { return g_chainstate.m_chain; }
8486

8587
/**
@@ -94,7 +96,7 @@ CChain& ChainActive() { return g_chainstate.m_chain; }
9496
*/
9597
RecursiveMutex cs_main;
9698

97-
BlockMap& mapBlockIndex = g_chainstate.mapBlockIndex;
99+
BlockMap& mapBlockIndex = ::ChainstateActive().mapBlockIndex;
98100
CBlockIndex *pindexBestHeader = nullptr;
99101
Mutex g_best_block_mutex;
100102
std::condition_variable g_best_block_cv;
@@ -125,12 +127,12 @@ CScript COINBASE_FLAGS;
125127

126128
// Internal stuff
127129
namespace {
128-
CBlockIndex *&pindexBestInvalid = g_chainstate.pindexBestInvalid;
130+
CBlockIndex *&pindexBestInvalid = ::ChainstateActive().pindexBestInvalid;
129131

130132
/** All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
131133
* Pruned nodes may have entries where B is missing data.
132134
*/
133-
std::multimap<CBlockIndex*, CBlockIndex*>& mapBlocksUnlinked = g_chainstate.mapBlocksUnlinked;
135+
std::multimap<CBlockIndex*, CBlockIndex*>& mapBlocksUnlinked = ::ChainstateActive().mapBlocksUnlinked;
134136

135137
CCriticalSection cs_LastBlockFile;
136138
std::vector<CBlockFileInfo> vinfoBlockFile;
@@ -2627,7 +2629,7 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
26272629
}
26282630

26292631
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
2630-
return g_chainstate.ActivateBestChain(state, chainparams, std::move(pblock));
2632+
return ::ChainstateActive().ActivateBestChain(state, chainparams, std::move(pblock));
26312633
}
26322634

26332635
bool CChainState::PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
@@ -2659,7 +2661,7 @@ bool CChainState::PreciousBlock(CValidationState& state, const CChainParams& par
26592661
return ActivateBestChain(state, params, std::shared_ptr<const CBlock>());
26602662
}
26612663
bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex) {
2662-
return g_chainstate.PreciousBlock(state, params, pindex);
2664+
return ::ChainstateActive().PreciousBlock(state, params, pindex);
26632665
}
26642666

26652667
bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
@@ -2748,7 +2750,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
27482750
}
27492751

27502752
bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex) {
2751-
return g_chainstate.InvalidateBlock(state, chainparams, pindex);
2753+
return ::ChainstateActive().InvalidateBlock(state, chainparams, pindex);
27522754
}
27532755

27542756
void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
@@ -2786,7 +2788,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
27862788
}
27872789

27882790
void ResetBlockFailureFlags(CBlockIndex *pindex) {
2789-
return g_chainstate.ResetBlockFailureFlags(pindex);
2791+
return ::ChainstateActive().ResetBlockFailureFlags(pindex);
27902792
}
27912793

27922794
CBlockIndex* CChainState::AddToBlockIndex(const CBlockHeader& block)
@@ -3324,7 +3326,7 @@ bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidatio
33243326
LOCK(cs_main);
33253327
for (const CBlockHeader& header : headers) {
33263328
CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
3327-
if (!g_chainstate.AcceptBlockHeader(header, state, chainparams, &pindex)) {
3329+
if (!::ChainstateActive().AcceptBlockHeader(header, state, chainparams, &pindex)) {
33283330
if (first_invalid) *first_invalid = header;
33293331
return false;
33303332
}
@@ -3455,7 +3457,7 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
34553457
bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus());
34563458
if (ret) {
34573459
// Store to disk
3458-
ret = g_chainstate.AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, nullptr, fNewBlock);
3460+
ret = ::ChainstateActive().AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, nullptr, fNewBlock);
34593461
}
34603462
if (!ret) {
34613463
GetMainSignals().BlockChecked(*pblock, state);
@@ -3466,7 +3468,7 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
34663468
NotifyHeaderTip();
34673469

34683470
CValidationState state; // Only used to report errors, not invalidity - ignore it
3469-
if (!g_chainstate.ActivateBestChain(state, chainparams, pblock))
3471+
if (!::ChainstateActive().ActivateBestChain(state, chainparams, pblock))
34703472
return error("%s: ActivateBestChain failed (%s)", __func__, FormatStateMessage(state));
34713473

34723474
return true;
@@ -3490,7 +3492,7 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
34903492
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
34913493
if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
34923494
return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
3493-
if (!g_chainstate.ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
3495+
if (!::ChainstateActive().ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
34943496
return false;
34953497
assert(state.IsValid());
34963498

@@ -3757,7 +3759,7 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
37573759

37583760
bool static LoadBlockIndexDB(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
37593761
{
3760-
if (!g_chainstate.LoadBlockIndex(chainparams.GetConsensus(), *pblocktree))
3762+
if (!::ChainstateActive().LoadBlockIndex(chainparams.GetConsensus(), *pblocktree))
37613763
return false;
37623764

37633765
// Load block file info
@@ -3832,7 +3834,7 @@ bool LoadChainTip(const CChainParams& chainparams)
38323834
}
38333835
::ChainActive().SetTip(pindex);
38343836

3835-
g_chainstate.PruneBlockIndexCandidates();
3837+
::ChainstateActive().PruneBlockIndexCandidates();
38363838

38373839
LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
38383840
::ChainActive().Tip()->GetBlockHash().ToString(), ::ChainActive().Height(),
@@ -3905,7 +3907,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
39053907
// check level 3: check for inconsistencies during memory-only disconnect of tip blocks
39063908
if (nCheckLevel >= 3 && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
39073909
assert(coins.GetBestBlock() == pindex->GetBlockHash());
3908-
DisconnectResult res = g_chainstate.DisconnectBlock(block, pindex, coins);
3910+
DisconnectResult res = ::ChainstateActive().DisconnectBlock(block, pindex, coins);
39093911
if (res == DISCONNECT_FAILED) {
39103912
return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
39113913
}
@@ -3940,7 +3942,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
39403942
CBlock block;
39413943
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
39423944
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3943-
if (!g_chainstate.ConnectBlock(block, state, pindex, coins, chainparams))
3945+
if (!::ChainstateActive().ConnectBlock(block, state, pindex, coins, chainparams))
39443946
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
39453947
}
39463948
}
@@ -4039,7 +4041,7 @@ bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
40394041
}
40404042

40414043
bool ReplayBlocks(const CChainParams& params, CCoinsView* view) {
4042-
return g_chainstate.ReplayBlocks(params, view);
4044+
return ::ChainstateActive().ReplayBlocks(params, view);
40434045
}
40444046

40454047
//! Helper for CChainState::RewindBlockIndex
@@ -4172,7 +4174,7 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
41724174
}
41734175

41744176
bool RewindBlockIndex(const CChainParams& params) {
4175-
if (!g_chainstate.RewindBlockIndex(params)) {
4177+
if (!::ChainstateActive().RewindBlockIndex(params)) {
41764178
return false;
41774179
}
41784180

@@ -4222,7 +4224,7 @@ void UnloadBlockIndex()
42224224
mapBlockIndex.clear();
42234225
fHavePruned = false;
42244226

4225-
g_chainstate.UnloadBlockIndex();
4227+
::ChainstateActive().UnloadBlockIndex();
42264228
}
42274229

42284230
bool LoadBlockIndex(const CChainParams& chainparams)
@@ -4274,7 +4276,7 @@ bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
42744276

42754277
bool LoadGenesisBlock(const CChainParams& chainparams)
42764278
{
4277-
return g_chainstate.LoadGenesisBlock(chainparams);
4279+
return ::ChainstateActive().LoadGenesisBlock(chainparams);
42784280
}
42794281

42804282
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFilePos *dbp)
@@ -4339,7 +4341,7 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
43394341
CBlockIndex* pindex = LookupBlockIndex(hash);
43404342
if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
43414343
CValidationState state;
4342-
if (g_chainstate.AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr)) {
4344+
if (::ChainstateActive().AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr)) {
43434345
nLoaded++;
43444346
}
43454347
if (state.IsError()) {
@@ -4376,7 +4378,7 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
43764378
head.ToString());
43774379
LOCK(cs_main);
43784380
CValidationState dummy;
4379-
if (g_chainstate.AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
4381+
if (::ChainstateActive().AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
43804382
{
43814383
nLoaded++;
43824384
queue.push_back(pblockrecursive->GetHash());

src/validation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,9 @@ bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, C
575575
/** Remove invalidity status from a block and its descendants. */
576576
void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
577577

578+
/** @returns the most-work valid chainstate. */
579+
CChainState& ChainstateActive();
580+
578581
/** @returns the most-work chain. */
579582
CChain& ChainActive();
580583

0 commit comments

Comments
 (0)