Skip to content

Commit 5d3f98d

Browse files
committed
refactor: Replace m_params with chainman.GetParams()
Fixes a TODO introduced in #24595.
1 parent 57c1927 commit 5d3f98d

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/validation.cpp

Lines changed: 38 additions & 31 deletions
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

@@ -1985,6 +1984,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
19851984
assert(*pindex->phashBlock == block_hash);
19861985

19871986
const auto time_start{SteadyClock::now()};
1987+
const CChainParams& params{m_chainman.GetParams()};
19881988

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

20182018
// Special case for the genesis block, skipping connection of its transactions
20192019
// (its coinbase is unspendable)
2020-
if (block_hash == m_params.GetConsensus().hashGenesisBlock) {
2020+
if (block_hash == params.GetConsensus().hashGenesisBlock) {
20212021
if (!fJustCheck)
20222022
view.SetBestBlock(pindex->GetBlockHash());
20232023
return true;
@@ -2049,7 +2049,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20492049
// artificially set the default assumed verified block further back.
20502050
// The test against nMinimumChainWork prevents the skipping when denied access to any chain at
20512051
// least as good as the expected chain.
2052-
fScriptChecks = (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, m_params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
2052+
fScriptChecks = (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
20532053
}
20542054
}
20552055
}
@@ -2130,9 +2130,9 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
21302130
// post BIP34 before approximately height 486,000,000. After block
21312131
// 1,983,702 testnet3 starts doing unnecessary BIP30 checking again.
21322132
assert(pindex->pprev);
2133-
CBlockIndex* pindexBIP34height = pindex->pprev->GetAncestor(m_params.GetConsensus().BIP34Height);
2133+
CBlockIndex* pindexBIP34height = pindex->pprev->GetAncestor(params.GetConsensus().BIP34Height);
21342134
//Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2135-
fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == m_params.GetConsensus().BIP34Hash));
2135+
fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == params.GetConsensus().BIP34Hash));
21362136

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

2257-
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, m_params.GetConsensus());
2257+
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, params.GetConsensus());
22582258
if (block.vtx[0]->GetValueOut() > blockReward) {
22592259
LogPrintf("ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)\n", block.vtx[0]->GetValueOut(), blockReward);
22602260
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-amount");
@@ -2275,7 +2275,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22752275
if (fJustCheck)
22762276
return true;
22772277

2278-
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, m_params)) {
2278+
if (!m_blockman.WriteUndoDataForBlock(blockundo, state, pindex, params)) {
22792279
return false;
22802280
}
22812281

@@ -2394,7 +2394,7 @@ bool Chainstate::FlushStateToDisk(
23942394
} else {
23952395
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
23962396

2397-
m_blockman.FindFilesToPrune(setFilesToPrune, m_params.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
2397+
m_blockman.FindFilesToPrune(setFilesToPrune, m_chainman.GetParams().PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
23982398
m_blockman.m_check_for_pruning = false;
23992399
}
24002400
if (!setFilesToPrune.empty()) {
@@ -2548,13 +2548,15 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
25482548
AssertLockHeld(::cs_main);
25492549
const auto& coins_tip = this->CoinsTip();
25502550

2551+
const CChainParams& params{m_chainman.GetParams()};
2552+
25512553
// The remainder of the function isn't relevant if we are not acting on
25522554
// the active chainstate, so return if need be.
25532555
if (this != &m_chainman.ActiveChainstate()) {
25542556
// Only log every so often so that we don't bury log messages at the tip.
25552557
constexpr int BACKGROUND_LOG_INTERVAL = 2000;
25562558
if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2557-
UpdateTipLog(coins_tip, pindexNew, m_params, __func__, "[background validation] ", "");
2559+
UpdateTipLog(coins_tip, pindexNew, params, __func__, "[background validation] ", "");
25582560
}
25592561
return;
25602562
}
@@ -2575,7 +2577,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
25752577
const CBlockIndex* pindex = pindexNew;
25762578
for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
25772579
WarningBitsConditionChecker checker(m_chainman, bit);
2578-
ThresholdState state = checker.GetStateFor(pindex, m_params.GetConsensus(), warningcache.at(bit));
2580+
ThresholdState state = checker.GetStateFor(pindex, params.GetConsensus(), warningcache.at(bit));
25792581
if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) {
25802582
const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit);
25812583
if (state == ThresholdState::ACTIVE) {
@@ -2586,7 +2588,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
25862588
}
25872589
}
25882590
}
2589-
UpdateTipLog(coins_tip, pindexNew, m_params, __func__, "", warning_messages.original);
2591+
UpdateTipLog(coins_tip, pindexNew, params, __func__, "", warning_messages.original);
25902592
}
25912593

25922594
/** Disconnect m_chain's tip.
@@ -2610,7 +2612,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
26102612
// Read block from disk.
26112613
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
26122614
CBlock& block = *pblock;
2613-
if (!ReadBlockFromDisk(block, pindexDelete, m_params.GetConsensus())) {
2615+
if (!ReadBlockFromDisk(block, pindexDelete, m_chainman.GetConsensus())) {
26142616
return error("DisconnectTip(): Failed to read block");
26152617
}
26162618
// Apply the block atomically to the chain state.
@@ -2727,7 +2729,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27272729
std::shared_ptr<const CBlock> pthisBlock;
27282730
if (!pblock) {
27292731
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2730-
if (!ReadBlockFromDisk(*pblockNew, pindexNew, m_params.GetConsensus())) {
2732+
if (!ReadBlockFromDisk(*pblockNew, pindexNew, m_chainman.GetConsensus())) {
27312733
return AbortNode(state, "Failed to read block");
27322734
}
27332735
pthisBlock = pblockNew;
@@ -3835,7 +3837,9 @@ bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockV
38353837
if (pindex->nChainWork < nMinimumChainWork) return true;
38363838
}
38373839

3838-
if (!CheckBlock(block, state, m_params.GetConsensus()) ||
3840+
const CChainParams& params{m_chainman.GetParams()};
3841+
3842+
if (!CheckBlock(block, state, params.GetConsensus()) ||
38393843
!ContextualCheckBlock(block, state, m_chainman, pindex->pprev)) {
38403844
if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
38413845
pindex->nStatus |= BLOCK_FAILED_VALID;
@@ -3852,7 +3856,7 @@ bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockV
38523856
// Write block to history file
38533857
if (fNewBlock) *fNewBlock = true;
38543858
try {
3855-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, m_params, dbp)};
3859+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, params, dbp)};
38563860
if (blockPos.IsNull()) {
38573861
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
38583862
return false;
@@ -3996,7 +4000,7 @@ bool Chainstate::LoadChainTip()
39964000
tip->GetBlockHash().ToString(),
39974001
m_chain.Height(),
39984002
FormatISO8601DateTime(tip->GetBlockTime()),
3999-
GuessVerificationProgress(m_params.TxData(), tip));
4003+
GuessVerificationProgress(m_chainman.GetParams().TxData(), tip));
40004004
return true;
40014005
}
40024006

@@ -4132,7 +4136,7 @@ bool Chainstate::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& in
41324136
AssertLockHeld(cs_main);
41334137
// TODO: merge with ConnectBlock
41344138
CBlock block;
4135-
if (!ReadBlockFromDisk(block, pindex, m_params.GetConsensus())) {
4139+
if (!ReadBlockFromDisk(block, pindex, m_chainman.GetConsensus())) {
41364140
return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
41374141
}
41384142

@@ -4184,7 +4188,7 @@ bool Chainstate::ReplayBlocks()
41844188
while (pindexOld != pindexFork) {
41854189
if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
41864190
CBlock block;
4187-
if (!ReadBlockFromDisk(block, pindexOld, m_params.GetConsensus())) {
4191+
if (!ReadBlockFromDisk(block, pindexOld, m_chainman.GetConsensus())) {
41884192
return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
41894193
}
41904194
LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
@@ -4336,16 +4340,18 @@ bool Chainstate::LoadGenesisBlock()
43364340
{
43374341
LOCK(cs_main);
43384342

4343+
const CChainParams& params{m_chainman.GetParams()};
4344+
43394345
// Check whether we're already initialized by checking for genesis in
43404346
// m_blockman.m_block_index. Note that we can't use m_chain here, since it is
43414347
// set based on the coins db, not the block index db, which is the only
43424348
// thing loaded at this point.
4343-
if (m_blockman.m_block_index.count(m_params.GenesisBlock().GetHash()))
4349+
if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
43444350
return true;
43454351

43464352
try {
4347-
const CBlock& block = m_params.GenesisBlock();
4348-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, m_params, nullptr)};
4353+
const CBlock& block = params.GenesisBlock();
4354+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, params, nullptr)};
43494355
if (blockPos.IsNull()) {
43504356
return error("%s: writing genesis block to disk failed", __func__);
43514357
}
@@ -4369,6 +4375,7 @@ void Chainstate::LoadExternalBlockFile(
43694375
assert(!dbp == !blocks_with_unknown_parent);
43704376

43714377
const auto start{SteadyClock::now()};
4378+
const CChainParams& params{m_chainman.GetParams()};
43724379

43734380
int nLoaded = 0;
43744381
try {
@@ -4385,10 +4392,10 @@ void Chainstate::LoadExternalBlockFile(
43854392
try {
43864393
// locate a header
43874394
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4388-
blkdat.FindByte(m_params.MessageStart()[0]);
4395+
blkdat.FindByte(params.MessageStart()[0]);
43894396
nRewind = blkdat.GetPos() + 1;
43904397
blkdat >> buf;
4391-
if (memcmp(buf, m_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
4398+
if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
43924399
continue;
43934400
}
43944401
// read size
@@ -4414,7 +4421,7 @@ void Chainstate::LoadExternalBlockFile(
44144421
{
44154422
LOCK(cs_main);
44164423
// detect out of order blocks, and store them for later
4417-
if (hash != m_params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
4424+
if (hash != params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
44184425
LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
44194426
block.hashPrevBlock.ToString());
44204427
if (dbp && blocks_with_unknown_parent) {
@@ -4433,13 +4440,13 @@ void Chainstate::LoadExternalBlockFile(
44334440
if (state.IsError()) {
44344441
break;
44354442
}
4436-
} else if (hash != m_params.GetConsensus().hashGenesisBlock && pindex->nHeight % 1000 == 0) {
4443+
} else if (hash != params.GetConsensus().hashGenesisBlock && pindex->nHeight % 1000 == 0) {
44374444
LogPrint(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), pindex->nHeight);
44384445
}
44394446
}
44404447

44414448
// Activate the genesis block so normal node progress can continue
4442-
if (hash == m_params.GetConsensus().hashGenesisBlock) {
4449+
if (hash == params.GetConsensus().hashGenesisBlock) {
44434450
BlockValidationState state;
44444451
if (!ActivateBestChain(state, nullptr)) {
44454452
break;
@@ -4460,7 +4467,7 @@ void Chainstate::LoadExternalBlockFile(
44604467
while (range.first != range.second) {
44614468
std::multimap<uint256, FlatFilePos>::iterator it = range.first;
44624469
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
4463-
if (ReadBlockFromDisk(*pblockrecursive, it->second, m_params.GetConsensus())) {
4470+
if (ReadBlockFromDisk(*pblockrecursive, it->second, params.GetConsensus())) {
44644471
LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
44654472
head.ToString());
44664473
LOCK(cs_main);
@@ -4560,7 +4567,7 @@ void Chainstate::CheckBlockIndex()
45604567
// Begin: actual consistency checks.
45614568
if (pindex->pprev == nullptr) {
45624569
// Genesis block checks.
4563-
assert(pindex->GetBlockHash() == m_params.GetConsensus().hashGenesisBlock); // Genesis block's hash must match.
4570+
assert(pindex->GetBlockHash() == m_chainman.GetConsensus().hashGenesisBlock); // Genesis block's hash must match.
45644571
assert(pindex == m_chain.Genesis()); // The current active chain's genesis block must be this block.
45654572
}
45664573
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

Lines changed: 0 additions & 4 deletions
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)