@@ -77,10 +77,6 @@ bool CBlockIndexWorkComparator::operator()(const CBlockIndex *pa, const CBlockIn
7777 return false ;
7878}
7979
80- namespace {
81- BlockManager g_blockman;
82- } // anon namespace
83-
8480ChainstateManager g_chainman;
8581
8682CChainState& ChainstateActive ()
@@ -153,8 +149,8 @@ namespace {
153149CBlockIndex* LookupBlockIndex (const uint256& hash)
154150{
155151 AssertLockHeld (cs_main);
156- BlockMap::const_iterator it = g_blockman. m_block_index .find (hash);
157- return it == g_blockman. m_block_index .end () ? nullptr : it->second ;
152+ BlockMap::const_iterator it = g_chainman. BlockIndex () .find (hash);
153+ return it == g_chainman. BlockIndex () .end () ? nullptr : it->second ;
158154}
159155
160156CBlockIndex* FindForkInGlobalIndex (const CChain& chain, const CBlockLocator& locator)
@@ -1246,8 +1242,8 @@ void CoinsViews::InitCache()
12461242
12471243// NOTE: for now m_blockman is set to a global, but this will be changed
12481244// in a future commit.
1249- CChainState::CChainState (uint256 from_snapshot_blockhash) :
1250- m_blockman(g_blockman ),
1245+ CChainState::CChainState (BlockManager& blockman, uint256 from_snapshot_blockhash) :
1246+ m_blockman(blockman ),
12511247 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
12521248
12531249
@@ -1302,7 +1298,7 @@ static CBlockIndex *pindexBestForkTip = nullptr, *pindexBestForkBase = nullptr;
13021298
13031299BlockMap& BlockIndex ()
13041300{
1305- return g_blockman .m_block_index ;
1301+ return g_chainman. m_blockman .m_block_index ;
13061302}
13071303
13081304static void AlertNotify (const std::string& strMessage)
@@ -3435,7 +3431,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
34353431 if (fCheckpointsEnabled ) {
34363432 // Don't accept any forks from the main chain prior to last checkpoint.
34373433 // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
3438- // g_blockman.m_block_index .
3434+ // BlockIndex() .
34393435 CBlockIndex* pcheckpoint = GetLastCheckpoint (params.Checkpoints ());
34403436 if (pcheckpoint && nHeight < pcheckpoint->nHeight ) {
34413437 LogPrintf (" ERROR: %s: forked chain older than last checkpoint (height %d)\n " , __func__, nHeight);
@@ -3643,7 +3639,8 @@ bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, BlockValid
36433639 LOCK (cs_main);
36443640 for (const CBlockHeader& header : headers) {
36453641 CBlockIndex *pindex = nullptr ; // Use a temp pindex instead of ppindex to avoid a const_cast
3646- bool accepted = g_blockman.AcceptBlockHeader (header, state, chainparams, &pindex);
3642+ bool accepted = g_chainman.m_blockman .AcceptBlockHeader (
3643+ header, state, chainparams, &pindex);
36473644 ::ChainstateActive ().CheckBlockIndex(chainparams.GetConsensus());
36483645
36493646 if (!accepted) {
@@ -3845,7 +3842,7 @@ void PruneOneBlockFile(const int fileNumber)
38453842{
38463843 LOCK (cs_LastBlockFile);
38473844
3848- for (const auto & entry : g_blockman. m_block_index ) {
3845+ for (const auto & entry : g_chainman. BlockIndex () ) {
38493846 CBlockIndex* pindex = entry.second ;
38503847 if (pindex->nFile == fileNumber) {
38513848 pindex->nStatus &= ~BLOCK_HAVE_DATA;
@@ -3859,12 +3856,12 @@ void PruneOneBlockFile(const int fileNumber)
38593856 // to be downloaded again in order to consider its chain, at which
38603857 // point it would be considered as a candidate for
38613858 // m_blocks_unlinked or setBlockIndexCandidates.
3862- auto range = g_blockman .m_blocks_unlinked .equal_range (pindex->pprev );
3859+ auto range = g_chainman. m_blockman .m_blocks_unlinked .equal_range (pindex->pprev );
38633860 while (range.first != range.second ) {
38643861 std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first ;
38653862 range.first ++;
38663863 if (_it->second == pindex) {
3867- g_blockman .m_blocks_unlinked .erase (_it);
3864+ g_chainman. m_blockman .m_blocks_unlinked .erase (_it);
38683865 }
38693866 }
38703867 }
@@ -4101,9 +4098,11 @@ void BlockManager::Unload() {
41014098
41024099bool static LoadBlockIndexDB (const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
41034100{
4104- if (!g_blockman.LoadBlockIndex (
4105- chainparams.GetConsensus (), *pblocktree, ::ChainstateActive ().setBlockIndexCandidates ))
4101+ if (!g_chainman.m_blockman .LoadBlockIndex (
4102+ chainparams.GetConsensus (), *pblocktree,
4103+ ::ChainstateActive ().setBlockIndexCandidates)) {
41064104 return false ;
4105+ }
41074106
41084107 // Load block file info
41094108 pblocktree->ReadLastBlockFile (nLastBlockFile);
@@ -4125,7 +4124,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams) EXCLUSIVE_LOCKS_RE
41254124 // Check presence of blk files
41264125 LogPrintf (" Checking all blk files are present...\n " );
41274126 std::set<int > setBlkDataFiles;
4128- for (const std::pair<const uint256, CBlockIndex*>& item : g_blockman. m_block_index )
4127+ for (const std::pair<const uint256, CBlockIndex*>& item : g_chainman. BlockIndex () )
41294128 {
41304129 CBlockIndex* pindex = item.second ;
41314130 if (pindex->nStatus & BLOCK_HAVE_DATA) {
@@ -4529,7 +4528,6 @@ void UnloadBlockIndex()
45294528{
45304529 LOCK (cs_main);
45314530 g_chainman.Unload ();
4532- g_blockman.Unload ();
45334531 pindexBestInvalid = nullptr ;
45344532 pindexBestHeader = nullptr ;
45354533 mempool.clear ();
@@ -4551,7 +4549,7 @@ bool LoadBlockIndex(const CChainParams& chainparams)
45514549 if (!fReindex ) {
45524550 bool ret = LoadBlockIndexDB (chainparams);
45534551 if (!ret) return false ;
4554- needs_init = g_blockman .m_block_index .empty ();
4552+ needs_init = g_chainman. m_blockman .m_block_index .empty ();
45554553 }
45564554
45574555 if (needs_init) {
@@ -5097,10 +5095,10 @@ class CMainCleanup
50975095 CMainCleanup () {}
50985096 ~CMainCleanup () {
50995097 // block headers
5100- BlockMap::iterator it1 = g_blockman. m_block_index .begin ();
5101- for (; it1 != g_blockman. m_block_index .end (); it1++)
5098+ BlockMap::iterator it1 = g_chainman. BlockIndex () .begin ();
5099+ for (; it1 != g_chainman. BlockIndex () .end (); it1++)
51025100 delete (*it1).second ;
5103- g_blockman. m_block_index .clear ();
5101+ g_chainman. BlockIndex () .clear ();
51045102 }
51055103};
51065104static CMainCleanup instance_of_cmaincleanup;
@@ -5126,7 +5124,7 @@ CChainState& ChainstateManager::InitializeChainstate(
51265124 std::unique_ptr<CChainState>& to_modify = (
51275125 snapshot_blockhash.IsNull () ? m_ibd_chainstate : m_snapshot_chainstate);
51285126
5129- to_modify.reset (new CChainState (snapshot_blockhash));
5127+ to_modify.reset (new CChainState (m_blockman, snapshot_blockhash));
51305128
51315129 if (activate) {
51325130 LogPrintf (" Switching active chainstate to %s\n " , snapshot_blockhash.ToString ());
@@ -5161,6 +5159,8 @@ void ChainstateManager::Unload()
51615159 chainstate->m_chain .SetTip (nullptr );
51625160 chainstate->UnloadBlockIndex ();
51635161 }
5162+
5163+ m_blockman.Unload ();
51645164}
51655165
51665166void ChainstateManager::Reset ()
0 commit comments