Skip to content

Commit a97791d

Browse files
author
MacroFake
committed
Merge bitcoin#25830: refactor: Replace m_params with chainman.GetParams()
5d3f98d refactor: Replace m_params with chainman.GetParams() (Aurèle Oulès) Pull request description: Fixes a TODO introduced in bitcoin#24595. Removes `m_params` from `CChainState` class and replaces it with `m_chainman.GetParams()`. ACKs for top commit: MarcoFalke: review ACK 5d3f98d 🌎 Tree-SHA512: de0fe31450d281cc7307c0d820495e86c93c7998e77a148db2c703da66cff1059e6560c041f1864913c42075aa24d259c2623d45e929ca0a8056ed330a9f9978
2 parents 003050d + 5d3f98d commit a97791d

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/validation.cpp

+38-31
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ MempoolAcceptResult AcceptToMemoryPool(Chainstate& active_chainstate, const CTra
14141414
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
14151415
{
14161416
AssertLockHeld(::cs_main);
1417-
const CChainParams& chainparams{active_chainstate.m_params};
1417+
const CChainParams& chainparams{active_chainstate.m_chainman.GetParams()};
14181418
assert(active_chainstate.GetMempool() != nullptr);
14191419
CTxMemPool& pool{*active_chainstate.GetMempool()};
14201420

@@ -1444,7 +1444,7 @@ PackageMempoolAcceptResult ProcessNewPackage(Chainstate& active_chainstate, CTxM
14441444
assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
14451445

14461446
std::vector<COutPoint> coins_to_uncache;
1447-
const CChainParams& chainparams = active_chainstate.m_params;
1447+
const CChainParams& chainparams = active_chainstate.m_chainman.GetParams();
14481448
const auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
14491449
AssertLockHeld(cs_main);
14501450
if (test_accept) {
@@ -1502,7 +1502,6 @@ Chainstate::Chainstate(
15021502
std::optional<uint256> from_snapshot_blockhash)
15031503
: m_mempool(mempool),
15041504
m_blockman(blockman),
1505-
m_params(chainman.GetParams()),
15061505
m_chainman(chainman),
15071506
m_from_snapshot_blockhash(from_snapshot_blockhash) {}
15081507

@@ -1997,6 +1996,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
19971996
assert(*pindex->phashBlock == block_hash);
19981997

19991998
const auto time_start{SteadyClock::now()};
1999+
const CChainParams& params{m_chainman.GetParams()};
20002000

20012001
// Check it again in case a previous version let a bad block in
20022002
// NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
@@ -2011,7 +2011,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20112011
// is enforced in ContextualCheckBlockHeader(); we wouldn't want to
20122012
// re-enforce that rule here (at least until we make it impossible for
20132013
// m_adjusted_time_callback() to go backward).
2014-
if (!CheckBlock(block, state, m_params.GetConsensus(), !fJustCheck, !fJustCheck)) {
2014+
if (!CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) {
20152015
if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
20162016
// We don't write down blocks to disk if they may have been
20172017
// corrupted, so this should be impossible unless we're having hardware
@@ -2029,7 +2029,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20292029

20302030
// Special case for the genesis block, skipping connection of its transactions
20312031
// (its coinbase is unspendable)
2032-
if (block_hash == m_params.GetConsensus().hashGenesisBlock) {
2032+
if (block_hash == params.GetConsensus().hashGenesisBlock) {
20332033
if (!fJustCheck)
20342034
view.SetBestBlock(pindex->GetBlockHash());
20352035
return true;
@@ -2061,7 +2061,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20612061
// artificially set the default assumed verified block further back.
20622062
// The test against nMinimumChainWork prevents the skipping when denied access to any chain at
20632063
// least as good as the expected chain.
2064-
fScriptChecks = (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, m_params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
2064+
fScriptChecks = (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
20652065
}
20662066
}
20672067
}
@@ -2142,9 +2142,9 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
21422142
// post BIP34 before approximately height 486,000,000. After block
21432143
// 1,983,702 testnet3 starts doing unnecessary BIP30 checking again.
21442144
assert(pindex->pprev);
2145-
CBlockIndex* pindexBIP34height = pindex->pprev->GetAncestor(m_params.GetConsensus().BIP34Height);
2145+
CBlockIndex* pindexBIP34height = pindex->pprev->GetAncestor(params.GetConsensus().BIP34Height);
21462146
//Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2147-
fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == m_params.GetConsensus().BIP34Hash));
2147+
fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == params.GetConsensus().BIP34Hash));
21482148

21492149
// TODO: Remove BIP30 checking from block height 1,983,702 on, once we have a
21502150
// consensus change that ensures coinbases at those heights cannot
@@ -2266,7 +2266,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22662266
Ticks<SecondsDouble>(time_connect),
22672267
Ticks<MillisecondsDouble>(time_connect) / num_blocks_total);
22682268

2269-
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, m_params.GetConsensus());
2269+
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, params.GetConsensus());
22702270
if (block.vtx[0]->GetValueOut() > blockReward) {
22712271
LogPrintf("ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)\n", block.vtx[0]->GetValueOut(), blockReward);
22722272
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-amount");
@@ -2287,7 +2287,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22872287
if (fJustCheck)
22882288
return true;
22892289

2290-
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, m_params)) {
2290+
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, params)) {
22912291
return false;
22922292
}
22932293

@@ -2406,7 +2406,7 @@ bool Chainstate::FlushStateToDisk(
24062406
} else {
24072407
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
24082408

2409-
m_blockman.FindFilesToPrune(setFilesToPrune, m_params.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
2409+
m_blockman.FindFilesToPrune(setFilesToPrune, m_chainman.GetParams().PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
24102410
m_blockman.m_check_for_pruning = false;
24112411
}
24122412
if (!setFilesToPrune.empty()) {
@@ -2560,13 +2560,15 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
25602560
AssertLockHeld(::cs_main);
25612561
const auto& coins_tip = this->CoinsTip();
25622562

2563+
const CChainParams& params{m_chainman.GetParams()};
2564+
25632565
// The remainder of the function isn't relevant if we are not acting on
25642566
// the active chainstate, so return if need be.
25652567
if (this != &m_chainman.ActiveChainstate()) {
25662568
// Only log every so often so that we don't bury log messages at the tip.
25672569
constexpr int BACKGROUND_LOG_INTERVAL = 2000;
25682570
if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2569-
UpdateTipLog(coins_tip, pindexNew, m_params, __func__, "[background validation] ", "");
2571+
UpdateTipLog(coins_tip, pindexNew, params, __func__, "[background validation] ", "");
25702572
}
25712573
return;
25722574
}
@@ -2587,7 +2589,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
25872589
const CBlockIndex* pindex = pindexNew;
25882590
for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
25892591
WarningBitsConditionChecker checker(m_chainman, bit);
2590-
ThresholdState state = checker.GetStateFor(pindex, m_params.GetConsensus(), warningcache.at(bit));
2592+
ThresholdState state = checker.GetStateFor(pindex, params.GetConsensus(), warningcache.at(bit));
25912593
if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) {
25922594
const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit);
25932595
if (state == ThresholdState::ACTIVE) {
@@ -2598,7 +2600,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
25982600
}
25992601
}
26002602
}
2601-
UpdateTipLog(coins_tip, pindexNew, m_params, __func__, "", warning_messages.original);
2603+
UpdateTipLog(coins_tip, pindexNew, params, __func__, "", warning_messages.original);
26022604
}
26032605

26042606
/** Disconnect m_chain's tip.
@@ -2622,7 +2624,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
26222624
// Read block from disk.
26232625
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
26242626
CBlock& block = *pblock;
2625-
if (!ReadBlockFromDisk(block, pindexDelete, m_params.GetConsensus())) {
2627+
if (!ReadBlockFromDisk(block, pindexDelete, m_chainman.GetConsensus())) {
26262628
return error("DisconnectTip(): Failed to read block");
26272629
}
26282630
// Apply the block atomically to the chain state.
@@ -2739,7 +2741,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27392741
std::shared_ptr<const CBlock> pthisBlock;
27402742
if (!pblock) {
27412743
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2742-
if (!ReadBlockFromDisk(*pblockNew, pindexNew, m_params.GetConsensus())) {
2744+
if (!ReadBlockFromDisk(*pblockNew, pindexNew, m_chainman.GetConsensus())) {
27432745
return AbortNode(state, "Failed to read block");
27442746
}
27452747
pthisBlock = pblockNew;
@@ -3847,7 +3849,9 @@ bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockV
38473849
if (pindex->nChainWork < nMinimumChainWork) return true;
38483850
}
38493851

3850-
if (!CheckBlock(block, state, m_params.GetConsensus()) ||
3852+
const CChainParams& params{m_chainman.GetParams()};
3853+
3854+
if (!CheckBlock(block, state, params.GetConsensus()) ||
38513855
!ContextualCheckBlock(block, state, m_chainman, pindex->pprev)) {
38523856
if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
38533857
pindex->nStatus |= BLOCK_FAILED_VALID;
@@ -3864,7 +3868,7 @@ bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockV
38643868
// Write block to history file
38653869
if (fNewBlock) *fNewBlock = true;
38663870
try {
3867-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, m_params, dbp)};
3871+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, params, dbp)};
38683872
if (blockPos.IsNull()) {
38693873
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
38703874
return false;
@@ -4008,7 +4012,7 @@ bool Chainstate::LoadChainTip()
40084012
tip->GetBlockHash().ToString(),
40094013
m_chain.Height(),
40104014
FormatISO8601DateTime(tip->GetBlockTime()),
4011-
GuessVerificationProgress(m_params.TxData(), tip));
4015+
GuessVerificationProgress(m_chainman.GetParams().TxData(), tip));
40124016
return true;
40134017
}
40144018

@@ -4144,7 +4148,7 @@ bool Chainstate::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& in
41444148
AssertLockHeld(cs_main);
41454149
// TODO: merge with ConnectBlock
41464150
CBlock block;
4147-
if (!ReadBlockFromDisk(block, pindex, m_params.GetConsensus())) {
4151+
if (!ReadBlockFromDisk(block, pindex, m_chainman.GetConsensus())) {
41484152
return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
41494153
}
41504154

@@ -4196,7 +4200,7 @@ bool Chainstate::ReplayBlocks()
41964200
while (pindexOld != pindexFork) {
41974201
if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
41984202
CBlock block;
4199-
if (!ReadBlockFromDisk(block, pindexOld, m_params.GetConsensus())) {
4203+
if (!ReadBlockFromDisk(block, pindexOld, m_chainman.GetConsensus())) {
42004204
return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
42014205
}
42024206
LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
@@ -4348,16 +4352,18 @@ bool Chainstate::LoadGenesisBlock()
43484352
{
43494353
LOCK(cs_main);
43504354

4355+
const CChainParams& params{m_chainman.GetParams()};
4356+
43514357
// Check whether we're already initialized by checking for genesis in
43524358
// m_blockman.m_block_index. Note that we can't use m_chain here, since it is
43534359
// set based on the coins db, not the block index db, which is the only
43544360
// thing loaded at this point.
4355-
if (m_blockman.m_block_index.count(m_params.GenesisBlock().GetHash()))
4361+
if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
43564362
return true;
43574363

43584364
try {
4359-
const CBlock& block = m_params.GenesisBlock();
4360-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, m_params, nullptr)};
4365+
const CBlock& block = params.GenesisBlock();
4366+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, params, nullptr)};
43614367
if (blockPos.IsNull()) {
43624368
return error("%s: writing genesis block to disk failed", __func__);
43634369
}
@@ -4381,6 +4387,7 @@ void Chainstate::LoadExternalBlockFile(
43814387
assert(!dbp == !blocks_with_unknown_parent);
43824388

43834389
const auto start{SteadyClock::now()};
4390+
const CChainParams& params{m_chainman.GetParams()};
43844391

43854392
int nLoaded = 0;
43864393
try {
@@ -4397,10 +4404,10 @@ void Chainstate::LoadExternalBlockFile(
43974404
try {
43984405
// locate a header
43994406
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4400-
blkdat.FindByte(m_params.MessageStart()[0]);
4407+
blkdat.FindByte(params.MessageStart()[0]);
44014408
nRewind = blkdat.GetPos() + 1;
44024409
blkdat >> buf;
4403-
if (memcmp(buf, m_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
4410+
if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
44044411
continue;
44054412
}
44064413
// read size
@@ -4426,7 +4433,7 @@ void Chainstate::LoadExternalBlockFile(
44264433
{
44274434
LOCK(cs_main);
44284435
// detect out of order blocks, and store them for later
4429-
if (hash != m_params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
4436+
if (hash != params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
44304437
LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
44314438
block.hashPrevBlock.ToString());
44324439
if (dbp && blocks_with_unknown_parent) {
@@ -4445,13 +4452,13 @@ void Chainstate::LoadExternalBlockFile(
44454452
if (state.IsError()) {
44464453
break;
44474454
}
4448-
} else if (hash != m_params.GetConsensus().hashGenesisBlock && pindex->nHeight % 1000 == 0) {
4455+
} else if (hash != params.GetConsensus().hashGenesisBlock && pindex->nHeight % 1000 == 0) {
44494456
LogPrint(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), pindex->nHeight);
44504457
}
44514458
}
44524459

44534460
// Activate the genesis block so normal node progress can continue
4454-
if (hash == m_params.GetConsensus().hashGenesisBlock) {
4461+
if (hash == params.GetConsensus().hashGenesisBlock) {
44554462
BlockValidationState state;
44564463
if (!ActivateBestChain(state, nullptr)) {
44574464
break;
@@ -4472,7 +4479,7 @@ void Chainstate::LoadExternalBlockFile(
44724479
while (range.first != range.second) {
44734480
std::multimap<uint256, FlatFilePos>::iterator it = range.first;
44744481
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
4475-
if (ReadBlockFromDisk(*pblockrecursive, it->second, m_params.GetConsensus())) {
4482+
if (ReadBlockFromDisk(*pblockrecursive, it->second, params.GetConsensus())) {
44764483
LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
44774484
head.ToString());
44784485
LOCK(cs_main);
@@ -4583,7 +4590,7 @@ void Chainstate::CheckBlockIndex()
45834590
// Begin: actual consistency checks.
45844591
if (pindex->pprev == nullptr) {
45854592
// Genesis block checks.
4586-
assert(pindex->GetBlockHash() == m_params.GetConsensus().hashGenesisBlock); // Genesis block's hash must match.
4593+
assert(pindex->GetBlockHash() == m_chainman.GetConsensus().hashGenesisBlock); // Genesis block's hash must match.
45874594
assert(pindex == m_chain.Genesis()); // The current active chain's genesis block must be this block.
45884595
}
45894596
if (!pindex->HaveTxsDownloaded()) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)

src/validation.h

-4
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,6 @@ class Chainstate
472472
//! Chainstate instances.
473473
node::BlockManager& m_blockman;
474474

475-
/** Chain parameters for this chainstate */
476-
/* TODO: replace with m_chainman.GetParams() */
477-
const CChainParams& m_params;
478-
479475
//! The chainstate manager that owns this chainstate. The reference is
480476
//! necessary so that this instance can check whether it is the active
481477
//! chainstate within deeply nested method calls.

0 commit comments

Comments
 (0)