Skip to content

Commit 8f60436

Browse files
committed
Merge #16194: refactor: share blockmetadata with BlockManager
682a1d0 refactoring: remove mapBlockIndex global (James O'Beirne) 55d525a refactoring: make pindexBestInvalid internal to validation.cpp (James O'Beirne) 4ed55df refactoring: add block_index_candidates arg to LoadBlockIndex (James O'Beirne) 613c46f refactoring: move block metadata structures into BlockManager (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/2019-04-proposal/proposal --- Under an assumeutxo model, we have multiple CChainState instances in use at once in order to support background validation. Currently, each CChainState instance has its own mapBlockIndex, a collection of linked block headers, in addition to a few other data structures that are related to maintenance of the block tree but not necessarily to any given chainstate. In order to avoid duplicating this data across chainstates, this change moves chainstate-agnostic block metadata (and related behavior) into a class, `BlockManager`. Chainstates are parameterized with a reference to a blockmanager instance and in practice they share the same instance. Most of this change is conceptually move-only, though the diff is somewhat muddled. The first commit can be reviewed slightly more easily with `--color-moved=dimmed_zebra`. Admittedly, that commit is pretty unwieldy; I tried to split it up after the fact with `git add --patch`, but that was difficult because of git's inability to split hunks past a certain point. Some of the moves also ended up being obscured when done over separate commits. ACKs for top commit: MarcoFalke: ACK 682a1d0 ryanofsky: utACK 682a1d0, only changes since last review were rebase and fixing conflict on a moved line ariard: utACK 682a1d0. Most of the changes are move-only, with main problem being to avoid creating circular dependencies between `BlockManager` and `CChainState`. Tested, comments are mostly nits, feel free to ignore them Tree-SHA512: 738d8d06539ba53acf4bd2d48ae000473e645bbc4e63d798d55d247a4d5a4f781b73538ed590f6407be9ab402ea9d395570ea20bff0a4b9ce747bcc1600c5108
2 parents 8f9725c + 682a1d0 commit 8f60436

File tree

6 files changed

+221
-155
lines changed

6 files changed

+221
-155
lines changed

src/init.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ void SetupServerArgs()
492492
"and level 4 tries to reconnect the blocks, "
493493
"each level includes the checks of the previous levels "
494494
"(0-4, default: %u)", DEFAULT_CHECKLEVEL), true, OptionsCategory::DEBUG_TEST);
495-
gArgs.AddArg("-checkblockindex", strprintf("Do a full consistency check for mapBlockIndex, setBlockIndexCandidates, ::ChainActive() and mapBlocksUnlinked occasionally. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), true, OptionsCategory::DEBUG_TEST);
495+
gArgs.AddArg("-checkblockindex", strprintf("Do a full consistency check for the block tree, setBlockIndexCandidates, ::ChainActive() and mapBlocksUnlinked occasionally. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), true, OptionsCategory::DEBUG_TEST);
496496
gArgs.AddArg("-checkmempool=<n>", strprintf("Run checks every <n> transactions (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), regtestChainParams->DefaultConsistencyChecks()), true, OptionsCategory::DEBUG_TEST);
497497
gArgs.AddArg("-checkpoints", strprintf("Disable expensive verification for known chain history (default: %u)", DEFAULT_CHECKPOINTS_ENABLED), true, OptionsCategory::DEBUG_TEST);
498498
gArgs.AddArg("-deprecatedrpc=<method>", "Allows deprecated RPC method(s) to be used", true, OptionsCategory::DEBUG_TEST);
@@ -1517,7 +1517,8 @@ bool AppInitMain(InitInterfaces& interfaces)
15171517

15181518
// If the loaded chain has a wrong genesis, bail out immediately
15191519
// (we're likely using a testnet datadir, or the other way around).
1520-
if (!mapBlockIndex.empty() && !LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
1520+
if (!::BlockIndex().empty() &&
1521+
!LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
15211522
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
15221523
}
15231524

@@ -1538,7 +1539,7 @@ bool AppInitMain(InitInterfaces& interfaces)
15381539
}
15391540

15401541
// At this point we're either in reindex or we've loaded a useful
1541-
// block tree into mapBlockIndex!
1542+
// block tree into BlockIndex()!
15421543

15431544
pcoinsdbview.reset(new CCoinsViewDB(nCoinDBCache, false, fReset || fReindexChainState));
15441545
pcoinscatcher.reset(new CCoinsViewErrorCatcher(pcoinsdbview.get()));
@@ -1577,7 +1578,7 @@ bool AppInitMain(InitInterfaces& interfaces)
15771578
if (!fReset) {
15781579
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
15791580
// It both disconnects blocks based on ::ChainActive(), and drops block data in
1580-
// mapBlockIndex based on lack of available witness data.
1581+
// BlockIndex() based on lack of available witness data.
15811582
uiInterface.InitMessage(_("Rewinding blocks..."));
15821583
if (!RewindBlockIndex(chainparams)) {
15831584
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain");
@@ -1749,7 +1750,7 @@ bool AppInitMain(InitInterfaces& interfaces)
17491750
//// debug print
17501751
{
17511752
LOCK(cs_main);
1752-
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
1753+
LogPrintf("block tree size = %u\n", ::BlockIndex().size());
17531754
chain_active_height = ::ChainActive().Height();
17541755
}
17551756
LogPrintf("nBestHeight = %d\n", chain_active_height);

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,15 +1411,15 @@ static UniValue getchaintips(const JSONRPCRequest& request)
14111411
/*
14121412
* Idea: the set of chain tips is ::ChainActive().tip, plus orphan blocks which do not have another orphan building off of them.
14131413
* Algorithm:
1414-
* - Make one pass through mapBlockIndex, picking out the orphan blocks, and also storing a set of the orphan block's pprev pointers.
1414+
* - 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.
14151415
* - Iterate through the orphan blocks. If the block isn't pointed to by another orphan, it is a chain tip.
14161416
* - add ::ChainActive().Tip()
14171417
*/
14181418
std::set<const CBlockIndex*, CompareBlocksByHeight> setTips;
14191419
std::set<const CBlockIndex*> setOrphans;
14201420
std::set<const CBlockIndex*> setPrevs;
14211421

1422-
for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
1422+
for (const std::pair<const uint256, CBlockIndex*>& item : ::BlockIndex())
14231423
{
14241424
if (!::ChainActive().Contains(item.second)) {
14251425
setOrphans.insert(item.second);

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
250250

251251
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
252252

253-
// Load mapBlockIndex
253+
// Load m_block_index
254254
while (pcursor->Valid()) {
255255
boost::this_thread::interruption_point();
256256
if (ShutdownRequested()) return false;

0 commit comments

Comments
 (0)