Skip to content

Commit fa1b76a

Browse files
author
MacroFake
committed
Do not call global Params() when chainman is in scope
1 parent fa30234 commit fa1b76a

File tree

10 files changed

+28
-34
lines changed

10 files changed

+28
-34
lines changed

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,8 +1680,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16801680
chain_active_height = chainman.ActiveChain().Height();
16811681
if (tip_info) {
16821682
tip_info->block_height = chain_active_height;
1683-
tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : Params().GenesisBlock().GetBlockTime();
1684-
tip_info->verification_progress = GuessVerificationProgress(Params().TxData(), chainman.ActiveChain().Tip());
1683+
tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
1684+
tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), chainman.ActiveChain().Tip());
16851685
}
16861686
if (tip_info && chainman.m_best_header) {
16871687
tip_info->header_height = chainman.m_best_header->nHeight;

src/node/interfaces.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ class NodeImpl : public Node
228228
uint256 getBestBlockHash() override
229229
{
230230
const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip());
231-
return tip ? tip->GetBlockHash() : Params().GenesisBlock().GetHash();
231+
return tip ? tip->GetBlockHash() : chainman().GetParams().GenesisBlock().GetHash();
232232
}
233233
int64_t getLastBlockTime() override
234234
{
235235
LOCK(::cs_main);
236236
if (chainman().ActiveChain().Tip()) {
237237
return chainman().ActiveChain().Tip()->GetBlockTime();
238238
}
239-
return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
239+
return chainman().GetParams().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
240240
}
241241
double getVerificationProgress() override
242242
{
@@ -245,7 +245,7 @@ class NodeImpl : public Node
245245
LOCK(::cs_main);
246246
tip = chainman().ActiveChain().Tip();
247247
}
248-
return GuessVerificationProgress(Params().TxData(), tip);
248+
return GuessVerificationProgress(chainman().GetParams().TxData(), tip);
249249
}
250250
bool isInitialBlockDownload() override {
251251
return chainman().ActiveChainstate().IsInitialBlockDownload();
@@ -546,7 +546,7 @@ class ChainImpl : public Chain
546546
double guessVerificationProgress(const uint256& block_hash) override
547547
{
548548
LOCK(cs_main);
549-
return GuessVerificationProgress(Params().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
549+
return GuessVerificationProgress(chainman().GetParams().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
550550
}
551551
bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override
552552
{

src/rest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static bool rest_block(const std::any& context,
305305
if (chainman.m_blockman.IsBlockPruned(pblockindex))
306306
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
307307

308-
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
308+
if (!ReadBlockFromDisk(block, pblockindex, chainman.GetParams().GetConsensus()))
309309
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
310310
}
311311

src/rpc/blockchain.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,11 @@ static RPCHelpMan pruneblockchain()
777777

778778
unsigned int height = (unsigned int) heightParam;
779779
unsigned int chainHeight = (unsigned int) active_chain.Height();
780-
if (chainHeight < Params().PruneAfterHeight())
780+
if (chainHeight < chainman.GetParams().PruneAfterHeight()) {
781781
throw JSONRPCError(RPC_MISC_ERROR, "Blockchain is too short for pruning.");
782-
else if (height > chainHeight)
782+
} else if (height > chainHeight) {
783783
throw JSONRPCError(RPC_INVALID_PARAMETER, "Blockchain is shorter than the attempted prune height.");
784-
else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
784+
} else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
785785
LogPrint(BCLog::RPC, "Attempt to prune blocks close to the tip. Retaining the minimum number of blocks.\n");
786786
height = chainHeight - MIN_BLOCKS_TO_KEEP;
787787
}
@@ -1058,7 +1058,7 @@ static RPCHelpMan verifychain()
10581058

10591059
CChainState& active_chainstate = chainman.ActiveChainstate();
10601060
return CVerifyDB().VerifyDB(
1061-
active_chainstate, Params().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth);
1061+
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth);
10621062
},
10631063
};
10641064
}
@@ -1189,14 +1189,14 @@ RPCHelpMan getblockchaininfo()
11891189
const CBlockIndex& tip{*CHECK_NONFATAL(active_chainstate.m_chain.Tip())};
11901190
const int height{tip.nHeight};
11911191
UniValue obj(UniValue::VOBJ);
1192-
obj.pushKV("chain", Params().NetworkIDString());
1192+
obj.pushKV("chain", chainman.GetParams().NetworkIDString());
11931193
obj.pushKV("blocks", height);
11941194
obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1);
11951195
obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
11961196
obj.pushKV("difficulty", GetDifficulty(&tip));
11971197
obj.pushKV("time", tip.GetBlockTime());
11981198
obj.pushKV("mediantime", tip.GetMedianTimePast());
1199-
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), &tip));
1199+
obj.pushKV("verificationprogress", GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
12001200
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
12011201
obj.pushKV("chainwork", tip.nChainWork.GetHex());
12021202
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
@@ -1563,7 +1563,7 @@ static RPCHelpMan getchaintxstats()
15631563
{
15641564
ChainstateManager& chainman = EnsureAnyChainman(request.context);
15651565
const CBlockIndex* pindex;
1566-
int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPowTargetSpacing; // By default: 1 month
1566+
int blockcount = 30 * 24 * 60 * 60 / chainman.GetParams().GetConsensus().nPowTargetSpacing; // By default: 1 month
15671567

15681568
if (request.params[1].isNull()) {
15691569
LOCK(cs_main);
@@ -1879,7 +1879,7 @@ static RPCHelpMan getblockstats()
18791879
ret_all.pushKV("minfeerate", (minfeerate == MAX_MONEY) ? 0 : minfeerate);
18801880
ret_all.pushKV("mintxsize", mintxsize == MAX_BLOCK_SERIALIZED_SIZE ? 0 : mintxsize);
18811881
ret_all.pushKV("outs", outputs);
1882-
ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight, Params().GetConsensus()));
1882+
ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight, chainman.GetParams().GetConsensus()));
18831883
ret_all.pushKV("swtotal_size", swtotal_size);
18841884
ret_all.pushKV("swtotal_weight", swtotal_weight);
18851885
ret_all.pushKV("swtxs", swtxs);

src/rpc/mining.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
119119
block_hash.SetNull();
120120
block.hashMerkleRoot = BlockMerkleRoot(block);
121121

122-
CChainParams chainparams(Params());
123-
124-
while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus()) && !ShutdownRequested()) {
122+
while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainman.GetConsensus()) && !ShutdownRequested()) {
125123
++block.nNonce;
126124
--max_tries;
127125
}
@@ -349,7 +347,6 @@ static RPCHelpMan generateblock()
349347
}
350348
}
351349

352-
CChainParams chainparams(Params());
353350
CBlock block;
354351

355352
ChainstateManager& chainman = EnsureChainman(node);
@@ -374,7 +371,7 @@ static RPCHelpMan generateblock()
374371
LOCK(cs_main);
375372

376373
BlockValidationState state;
377-
if (!TestBlockValidity(state, chainparams, chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
374+
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
378375
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
379376
}
380377
}
@@ -429,7 +426,7 @@ static RPCHelpMan getmininginfo()
429426
obj.pushKV("difficulty", (double)GetDifficulty(active_chain.Tip()));
430427
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
431428
obj.pushKV("pooledtx", (uint64_t)mempool.size());
432-
obj.pushKV("chain", Params().NetworkIDString());
429+
obj.pushKV("chain", chainman.GetParams().NetworkIDString());
433430
obj.pushKV("warnings", GetWarnings(false).original);
434431
return obj;
435432
},
@@ -643,7 +640,7 @@ static RPCHelpMan getblocktemplate()
643640
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
644641
return "inconclusive-not-best-prevblk";
645642
BlockValidationState state;
646-
TestBlockValidity(state, Params(), active_chainstate, block, pindexPrev, false, true);
643+
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, false, true);
647644
return BIP22ValidationResult(state);
648645
}
649646

@@ -665,7 +662,7 @@ static RPCHelpMan getblocktemplate()
665662
if (strMode != "template")
666663
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
667664

668-
if (!Params().IsTestChain()) {
665+
if (!chainman.GetParams().IsTestChain()) {
669666
const CConnman& connman = EnsureConnman(node);
670667
if (connman.GetNodeCount(ConnectionDirection::Both) == 0) {
671668
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
@@ -726,7 +723,7 @@ static RPCHelpMan getblocktemplate()
726723
// TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
727724
}
728725

729-
const Consensus::Params& consensusParams = Params().GetConsensus();
726+
const Consensus::Params& consensusParams = chainman.GetParams().GetConsensus();
730727

731728
// GBT must be called with 'signet' set in the rules for signet chains
732729
if (consensusParams.signet_blocks && setClientRules.count("signet") != 1) {

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static RPCHelpMan getrawtransaction()
217217
uint256 hash = ParseHashV(request.params[0], "parameter 1");
218218
const CBlockIndex* blockindex = nullptr;
219219

220-
if (hash == Params().GenesisBlock().hashMerkleRoot) {
220+
if (hash == chainman.GetParams().GenesisBlock().hashMerkleRoot) {
221221
// Special exception for the genesis block coinbase transaction
222222
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
223223
}
@@ -245,7 +245,7 @@ static RPCHelpMan getrawtransaction()
245245
}
246246

247247
uint256 hash_block;
248-
const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, Params().GetConsensus(), hash_block);
248+
const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, chainman.GetConsensus(), hash_block);
249249
if (!tx) {
250250
std::string errmsg;
251251
if (blockindex) {

src/rpc/txoutproof.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static RPCHelpMan gettxoutproof()
8787
LOCK(cs_main);
8888

8989
if (pblockindex == nullptr) {
90-
const CTransactionRef tx = GetTransaction(/*block_index=*/nullptr, /*mempool=*/nullptr, *setTxids.begin(), Params().GetConsensus(), hashBlock);
90+
const CTransactionRef tx = GetTransaction(/*block_index=*/nullptr, /*mempool=*/nullptr, *setTxids.begin(), chainman.GetConsensus(), hashBlock);
9191
if (!tx || hashBlock.IsNull()) {
9292
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
9393
}
@@ -98,7 +98,7 @@ static RPCHelpMan gettxoutproof()
9898
}
9999

100100
CBlock block;
101-
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) {
101+
if (!ReadBlockFromDisk(block, pblockindex, chainman.GetConsensus())) {
102102
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
103103
}
104104

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
6565
const std::vector<CMutableTransaction>& txns,
6666
const CScript& scriptPubKey)
6767
{
68-
const CChainParams& chainparams = Params();
6968
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(scriptPubKey);
7069
CBlock& block = pblocktemplate->block;
7170
block.hashPrevBlock = prev->GetBlockHash();
@@ -83,7 +82,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
8382
block.hashMerkleRoot = BlockMerkleRoot(block);
8483
}
8584

86-
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
85+
while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
8786

8887
return block;
8988
}

src/test/denialofservice_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerM
133133
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
134134
{
135135
NodeId id{0};
136-
const CChainParams& chainparams = Params();
137136
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
138137
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr,
139138
*m_node.chainman, *m_node.mempool, false);
@@ -146,7 +145,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
146145

147146
const auto time_init{GetTime<std::chrono::seconds>()};
148147
SetMockTime(time_init);
149-
const auto time_later{time_init + 3 * std::chrono::seconds{chainparams.GetConsensus().nPowTargetSpacing} + 1s};
148+
const auto time_later{time_init + 3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s};
150149
connman->Init(options);
151150
std::vector<CNode *> vNodes;
152151

src/test/util/setup_common.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ CBlock TestChain100Setup::CreateBlock(
272272
const CScript& scriptPubKey,
273273
CChainState& chainstate)
274274
{
275-
const CChainParams& chainparams = Params();
276275
CTxMemPool empty_pool;
277276
CBlock block = BlockAssembler{chainstate, empty_pool}.CreateNewBlock(scriptPubKey)->block;
278277

@@ -282,7 +281,7 @@ CBlock TestChain100Setup::CreateBlock(
282281
}
283282
RegenerateCommitments(block, *Assert(m_node.chainman));
284283

285-
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
284+
while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
286285

287286
return block;
288287
}

0 commit comments

Comments
 (0)