Skip to content

Commit a55904a

Browse files
committed
Merge bitcoin/bitcoin#21866: [Bundle 7/7] validation: Farewell, global Chainstate!
6f99488 validation: Farewell, global Chainstate! (Carl Dong) 972c516 qt/test: Reset chainman in ~ChainstateManager instead (Carl Dong) 6c3b5dc scripted-diff: tree-wide: Remove all review-only assertions (Carl Dong) 3e82abb tree-wide: Remove stray review-only assertion (Carl Dong) f323248 qt/test: Use existing chainman in ::TestGUI (can be scripted-diff) (Carl Dong) 6c15de1 scripted-diff: wallet/test: Use existing chainman (Carl Dong) ee0ab1e fuzz: Initialize a TestingSetup for test_one_input (Carl Dong) 0d61634 scripted-diff: test: Use existing chainman in unit tests (Carl Dong) e197076 test: Pass in CoinsTip to ValidateCheckInputsForAllFlags (Carl Dong) 4d99b61 test/miner_tests: Pass in chain tip to CreateBlockIndex (Carl Dong) f0dd5e6 test/util: Use existing chainman in ::PrepareBlock (Carl Dong) 464c313 init: Use existing chainman (Carl Dong) Pull request description: Based on: #21767 à la Mr. Sandman ``` Mr. Chainman, bring me a tip (bung, bung, bung, bung) Make it the most work that I've ever seen (bung, bung, bung, bung) Rewind old tip till we're at the fork point (bung, bung, bung, bung) Then tell it that it's time to call Con-nectTip Chainman, I'm so alone (bung, bung, bung, bung) No local objects to call my own (bung, bung, bung, bung) Please make sure I have a ref Mr. Chainman, bring me a tip! ``` This is the last bundle in the #20158 series. Thanks everyone for their diligent review. I would like to call attention to bitcoin/bitcoin#21766, where a few leftover improvements were collated. - Remove globals: - `ChainstateManager g_chainman` - `CChainState& ChainstateActive()` - `CChain& ChainActive()` - Remove all review-only assertions. ACKs for top commit: jamesob: reACK bitcoin/bitcoin@6f99488 based on the contents of ariard: Code Review ACK 6f99488. jnewbery: utACK 6f99488 achow101: Code Review ACK 6f99488 ryanofsky: Code review ACK 6f99488. Tree-SHA512: 4052ea79360cf0efd81ad0ee3f982e1d93aab1837dcec75f875a56ceda085de078bb3099a2137935d7cc2222004ad88da94b605ef5efef35cb6bc733725debe6
2 parents a8c8dbc + 6f99488 commit a55904a

35 files changed

+173
-291
lines changed

src/bench/duplicate_inputs.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ static void DuplicateInputs(benchmark::Bench& bench)
2525
CMutableTransaction naughtyTx{};
2626

2727
LOCK(cs_main);
28-
assert(std::addressof(::ChainActive()) == std::addressof(testing_setup->m_node.chainman->ActiveChain()));
2928
CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip();
3029
assert(pindexPrev != nullptr);
3130
block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());

src/index/base.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ void BaseIndex::Interrupt()
340340

341341
bool BaseIndex::Start(CChainState& active_chainstate)
342342
{
343-
assert(std::addressof(::ChainstateActive()) == std::addressof(active_chainstate));
344343
m_chainstate = &active_chainstate;
345344
// Need to register this ValidationInterface before running Init(), so that
346345
// callbacks are not missed if Init sets m_synced to true.

src/init.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void Shutdown(NodeContext& node)
283283
init::UnsetGlobals();
284284
node.mempool.reset();
285285
node.fee_estimator.reset();
286-
node.chainman = nullptr;
286+
node.chainman.reset();
287287
node.scheduler.reset();
288288

289289
try {
@@ -1175,8 +1175,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11751175
node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get(), check_ratio);
11761176

11771177
assert(!node.chainman);
1178-
node.chainman = &g_chainman;
1179-
ChainstateManager& chainman = *Assert(node.chainman);
1178+
node.chainman = std::make_unique<ChainstateManager>();
1179+
ChainstateManager& chainman = *node.chainman;
11801180

11811181
assert(!node.peerman);
11821182
node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(),
@@ -1381,7 +1381,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13811381
// If the loaded chain has a wrong genesis, bail out immediately
13821382
// (we're likely using a testnet datadir, or the other way around).
13831383
if (!chainman.BlockIndex().empty() &&
1384-
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
1384+
!chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
13851385
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
13861386
}
13871387

@@ -1396,7 +1396,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13961396
// If we're not mid-reindex (based on disk + args), add a genesis block on disk
13971397
// (otherwise we use the one already on disk).
13981398
// This is called again in ThreadImport after the reindex completes.
1399-
if (!fReindex && !::ChainstateActive().LoadGenesisBlock(chainparams)) {
1399+
if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock(chainparams)) {
14001400
strLoadError = _("Error initializing block database");
14011401
break;
14021402
}
@@ -1545,21 +1545,21 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15451545
// ********************************************************* Step 8: start indexers
15461546
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
15471547
g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
1548-
if (!g_txindex->Start(::ChainstateActive())) {
1548+
if (!g_txindex->Start(chainman.ActiveChainstate())) {
15491549
return false;
15501550
}
15511551
}
15521552

15531553
for (const auto& filter_type : g_enabled_filter_types) {
15541554
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
1555-
if (!GetBlockFilterIndex(filter_type)->Start(::ChainstateActive())) {
1555+
if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
15561556
return false;
15571557
}
15581558
}
15591559

15601560
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
15611561
g_coin_stats_index = std::make_unique<CoinStatsIndex>(/* cache size */ 0, false, fReindex);
1562-
if (!g_coin_stats_index->Start(::ChainstateActive())) {
1562+
if (!g_coin_stats_index->Start(chainman.ActiveChainstate())) {
15631563
return false;
15641564
}
15651565
}
@@ -1607,7 +1607,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16071607
// Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
16081608
// No locking, as this happens before any background thread is started.
16091609
boost::signals2::connection block_notify_genesis_wait_connection;
1610-
if (::ChainActive().Tip() == nullptr) {
1610+
if (chainman.ActiveChain().Tip() == nullptr) {
16111611
block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(std::bind(BlockNotifyGenesisWait, std::placeholders::_2));
16121612
} else {
16131613
fHaveGenesis = true;

src/miner.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,7 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
4545
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
4646
block.vtx.at(0) = MakeTransactionRef(tx);
4747

48-
CBlockIndex* prev_block;
49-
{
50-
// TODO: Temporary scope to check correctness of refactored code.
51-
// Should be removed manually after merge of
52-
// https://github.com/bitcoin/bitcoin/pull/20158
53-
LOCK(::cs_main);
54-
assert(std::addressof(g_chainman.m_blockman) == std::addressof(chainman.m_blockman));
55-
prev_block = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
56-
}
48+
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
5749
GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus());
5850

5951
block.hashMerkleRoot = BlockMerkleRoot(block);
@@ -124,7 +116,6 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
124116
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
125117

126118
LOCK2(cs_main, m_mempool.cs);
127-
assert(std::addressof(*::ChainActive().Tip()) == std::addressof(*m_chainstate.m_chain.Tip()));
128119
CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
129120
assert(pindexPrev != nullptr);
130121
nHeight = pindexPrev->nHeight + 1;
@@ -184,7 +175,6 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
184175
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
185176

186177
BlockValidationState state;
187-
assert(std::addressof(::ChainstateActive()) == std::addressof(m_chainstate));
188178
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, false, false)) {
189179
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
190180
}

src/net_processing.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,6 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& conn
13611361
m_stale_tip_check_time(0),
13621362
m_ignore_incoming_txs(ignore_incoming_txs)
13631363
{
1364-
assert(std::addressof(g_chainman) == std::addressof(m_chainman));
13651364
// Initialize global variables that cannot be constructed at startup.
13661365
recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
13671366

src/node/blockstorage.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight,
248248
// when the undo file is keeping up with the block file, we want to flush it explicitly
249249
// when it is lagging behind (more blocks arrive than are being connected), we let the
250250
// undo block write case handle it
251-
assert(std::addressof(::ChainActive()) == std::addressof(active_chain));
252251
finalize_undo = (vinfoBlockFile[nFile].nHeightLast == (unsigned int)active_chain.Tip()->nHeight);
253252
nFile++;
254253
if (vinfoBlockFile.size() <= nFile) {

src/node/coin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ void FindCoins(const NodeContext& node, std::map<COutPoint, Coin>& coins)
1313
assert(node.mempool);
1414
assert(node.chainman);
1515
LOCK2(cs_main, node.mempool->cs);
16-
assert(std::addressof(::ChainstateActive()) == std::addressof(node.chainman->ActiveChainstate()));
1716
CCoinsViewCache& chain_view = node.chainman->ActiveChainstate().CoinsTip();
1817
CCoinsViewMemPool mempool_view(&chain_view, *node.mempool);
1918
for (auto& coin : coins) {

src/node/coinstats.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
9797
if (!pindex) {
9898
{
9999
LOCK(cs_main);
100-
assert(std::addressof(g_chainman.m_blockman) == std::addressof(blockman));
101100
pindex = blockman.LookupBlockIndex(view->GetBestBlock());
102101
}
103102
}

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <policy/fees.h>
1313
#include <scheduler.h>
1414
#include <txmempool.h>
15+
#include <validation.h>
1516

1617
NodeContext::NodeContext() {}
1718
NodeContext::~NodeContext() {}

src/node/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct NodeContext {
4444
std::unique_ptr<CTxMemPool> mempool;
4545
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
4646
std::unique_ptr<PeerManager> peerman;
47-
ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
47+
std::unique_ptr<ChainstateManager> chainman;
4848
std::unique_ptr<BanMan> banman;
4949
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
5050
std::unique_ptr<interfaces::Chain> chain;

0 commit comments

Comments
 (0)