Skip to content

Commit 0226408

Browse files
committed
have ChainstateManager inject BlockManager into chainstates
1 parent 4813167 commit 0226408

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ static UniValue getchaintips(const JSONRPCRequest& request)
13441344
/*
13451345
* Idea: the set of chain tips is ::ChainActive().tip, plus orphan blocks which do not have another orphan building off of them.
13461346
* Algorithm:
1347-
* - Make one pass through g_blockman.m_block_index, picking out the orphan blocks, and also storing a set of the orphan block's pprev pointers.
1347+
* - Make one pass through BlockIndex(), picking out the orphan blocks, and also storing a set of the orphan block's pprev pointers.
13481348
* - Iterate through the orphan blocks. If the block isn't pointed to by another orphan, it is a chain tip.
13491349
* - add ::ChainActive().Tip()
13501350
*/

src/validation.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
8480
ChainstateManager g_chainman;
8581

8682
CChainState& ChainstateActive()
@@ -153,8 +149,8 @@ namespace {
153149
CBlockIndex* 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

160156
CBlockIndex* 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

13031299
BlockMap& BlockIndex()
13041300
{
1305-
return g_blockman.m_block_index;
1301+
return g_chainman.m_blockman.m_block_index;
13061302
}
13071303

13081304
static 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

41024099
bool 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
};
51065104
static 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

51665166
void ChainstateManager::Reset()

src/validation.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,7 @@ class CChainState {
582582
std::unique_ptr<CoinsViews> m_coins_views;
583583

584584
public:
585-
CChainState(BlockManager& blockman) : m_blockman(blockman) {}
586-
CChainState(uint256 from_snapshot_blockhash = uint256());
585+
CChainState(BlockManager& blockman, uint256 from_snapshot_blockhash = uint256());
587586

588587
/**
589588
* Initialize the CoinsViews UTXO set database management data structures. The in-memory
@@ -818,6 +817,10 @@ class ChainstateManager
818817
friend CChain& ChainActive();
819818

820819
public:
820+
//! A single BlockManager instance is shared across each constructed
821+
//! chainstate to avoid duplicating block metadata.
822+
BlockManager m_blockman GUARDED_BY(::cs_main);
823+
821824
//! Instantiate a new chainstate and assign it based upon whether it is
822825
//! from a snapshot.
823826
//!
@@ -835,6 +838,11 @@ class ChainstateManager
835838
int ActiveHeight() const { return ActiveChain().Height(); }
836839
CBlockIndex* ActiveTip() const { return ActiveChain().Tip(); }
837840

841+
BlockMap& BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
842+
{
843+
return m_blockman.m_block_index;
844+
}
845+
838846
bool IsSnapshotActive() const;
839847

840848
uint256 SnapshotBlockhash() const { return m_snapshot_blockhash; }

0 commit comments

Comments
 (0)