Skip to content

Commit 5be9ee3

Browse files
ajtownsdongcarl
authored andcommitted
refactor: more const annotations for uses of CBlockIndex*
1 parent 430acb7 commit 5be9ee3

File tree

10 files changed

+26
-25
lines changed

10 files changed

+26
-25
lines changed

src/index/coinstatsindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n
277277

278278
{
279279
LOCK(cs_main);
280-
CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
280+
const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
281281
const auto& consensus_params{Params().GetConsensus()};
282282

283283
do {

src/node/blockstorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
408408
return true;
409409
}
410410

411-
CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
411+
const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
412412
{
413413
const MapCheckpoints& checkpoints = data.mapCheckpoints;
414414

415415
for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) {
416416
const uint256& hash = i.second;
417-
CBlockIndex* pindex = LookupBlockIndex(hash);
417+
const CBlockIndex* pindex = LookupBlockIndex(hash);
418418
if (pindex) {
419419
return pindex;
420420
}

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class BlockManager
163163
uint64_t CalculateCurrentUsage();
164164

165165
//! Returns last CBlockIndex* that is a checkpoint
166-
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
166+
const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
167167

168168
~BlockManager()
169169
{

src/node/interfaces.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class ChainImpl : public Chain
490490
{
491491
LOCK(cs_main);
492492
const CChainState& active = Assert(m_node.chainman)->ActiveChainstate();
493-
if (CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
493+
if (const CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
494494
return fork->nHeight;
495495
}
496496
return std::nullopt;
@@ -557,7 +557,7 @@ class ChainImpl : public Chain
557557
// used to limit the range, and passing min_height that's too low or
558558
// max_height that's too high will not crash or change the result.
559559
LOCK(::cs_main);
560-
if (CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
560+
if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
561561
if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
562562
for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
563563
// Check pprev to not segfault if min_height is too low

src/node/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
5050
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
5151
block.vtx.at(0) = MakeTransactionRef(tx);
5252

53-
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
53+
const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
5454
GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus());
5555

5656
block.hashMerkleRoot = BlockMerkleRoot(block);

src/rest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ static bool rest_block(const std::any& context,
283283
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
284284

285285
CBlock block;
286-
CBlockIndex* pblockindex = nullptr;
287-
CBlockIndex* tip = nullptr;
286+
const CBlockIndex* pblockindex = nullptr;
287+
const CBlockIndex* tip = nullptr;
288288
{
289289
ChainstateManager* maybe_chainman = GetChainman(context, req);
290290
if (!maybe_chainman) return false;

src/rpc/blockchain.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
110110
return blockindex == tip ? 1 : -1;
111111
}
112112

113-
CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainman) {
113+
static const CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainman)
114+
{
114115
LOCK(::cs_main);
115116
CChain& active_chain = chainman.ActiveChain();
116117

@@ -127,7 +128,7 @@ CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainma
127128
return active_chain[height];
128129
} else {
129130
const uint256 hash{ParseHashV(param, "hash_or_height")};
130-
CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
131+
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
131132

132133
if (!pindex) {
133134
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
@@ -854,7 +855,7 @@ static RPCHelpMan getblockhash()
854855
if (nHeight < 0 || nHeight > active_chain.Height())
855856
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
856857

857-
CBlockIndex* pblockindex = active_chain[nHeight];
858+
const CBlockIndex* pblockindex = active_chain[nHeight];
858859
return pblockindex->GetBlockHash().GetHex();
859860
},
860861
};
@@ -1132,7 +1133,7 @@ static RPCHelpMan pruneblockchain()
11321133
// too low to be a block time (corresponds to timestamp from Sep 2001).
11331134
if (heightParam > 1000000000) {
11341135
// Add a 2 hour buffer to include blocks which might have had old timestamps
1135-
CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0);
1136+
const CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0);
11361137
if (!pindex) {
11371138
throw JSONRPCError(RPC_INVALID_PARAMETER, "Could not find block with at least the specified timestamp.");
11381139
}
@@ -1226,7 +1227,7 @@ static RPCHelpMan gettxoutsetinfo()
12261227
{
12271228
UniValue ret(UniValue::VOBJ);
12281229

1229-
CBlockIndex* pindex{nullptr};
1230+
const CBlockIndex* pindex{nullptr};
12301231
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
12311232
CCoinsStats stats{hash_type};
12321233
stats.index_requested = request.params[2].isNull() || request.params[2].get_bool();
@@ -2177,7 +2178,7 @@ static RPCHelpMan getblockstats()
21772178
{
21782179
ChainstateManager& chainman = EnsureAnyChainman(request.context);
21792180
LOCK(cs_main);
2180-
CBlockIndex* pindex{ParseHashOrHeight(request.params[0], chainman)};
2181+
const CBlockIndex* pindex{ParseHashOrHeight(request.params[0], chainman)};
21812182
CHECK_NONFATAL(pindex != nullptr);
21822183

21832184
std::set<std::string> stats;
@@ -2572,7 +2573,7 @@ static RPCHelpMan scantxoutset()
25722573
g_should_abort_scan = false;
25732574
int64_t count = 0;
25742575
std::unique_ptr<CCoinsViewCursor> pcursor;
2575-
CBlockIndex* tip;
2576+
const CBlockIndex* tip;
25762577
NodeContext& node = EnsureAnyNodeContext(request.context);
25772578
{
25782579
ChainstateManager& chainman = EnsureChainman(node);
@@ -2760,7 +2761,7 @@ UniValue CreateUTXOSnapshot(
27602761
{
27612762
std::unique_ptr<CCoinsViewCursor> pcursor;
27622763
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
2763-
CBlockIndex* tip;
2764+
const CBlockIndex* tip;
27642765

27652766
{
27662767
// We need to lock cs_main to ensure that the coinsdb isn't written to

src/rpc/rawtransaction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
6767
LOCK(cs_main);
6868

6969
entry.pushKV("blockhash", hashBlock.GetHex());
70-
CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
70+
const CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
7171
if (pindex) {
7272
if (active_chainstate.m_chain.Contains(pindex)) {
7373
entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
@@ -207,7 +207,7 @@ static RPCHelpMan getrawtransaction()
207207

208208
bool in_active_chain = true;
209209
uint256 hash = ParseHashV(request.params[0], "parameter 1");
210-
CBlockIndex* blockindex = nullptr;
210+
const CBlockIndex* blockindex = nullptr;
211211

212212
if (hash == Params().GenesisBlock().hashMerkleRoot) {
213213
// Special exception for the genesis block coinbase transaction
@@ -302,7 +302,7 @@ static RPCHelpMan gettxoutproof()
302302
}
303303
}
304304

305-
CBlockIndex* pblockindex = nullptr;
305+
const CBlockIndex* pblockindex = nullptr;
306306
uint256 hashBlock;
307307
ChainstateManager& chainman = EnsureAnyChainman(request.context);
308308
if (!request.params[1].isNull()) {

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ arith_uint256 nMinimumChainWork;
152152

153153
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
154154

155-
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
155+
const CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
156156
{
157157
AssertLockHeld(cs_main);
158158

159159
// Find the latest block common to locator and chain - we expect that
160160
// locator.vHave is sorted descending by height.
161161
for (const uint256& hash : locator.vHave) {
162-
CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
162+
const CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
163163
if (pindex) {
164164
if (m_chain.Contains(pindex)) {
165165
return pindex;
@@ -3373,7 +3373,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
33733373
// Don't accept any forks from the main chain prior to last checkpoint.
33743374
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
33753375
// BlockIndex().
3376-
CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(params.Checkpoints());
3376+
const CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(params.Checkpoints());
33773377
if (pcheckpoint && nHeight < pcheckpoint->nHeight) {
33783378
LogPrintf("ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__, nHeight);
33793379
return state.Invalid(BlockValidationResult::BLOCK_CHECKPOINT, "bad-fork-prior-to-checkpoint");
@@ -4186,7 +4186,7 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
41864186
}
41874187

41884188
// process in case the block isn't known yet
4189-
CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
4189+
const CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
41904190
if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
41914191
BlockValidationState state;
41924192
if (AcceptBlock(pblock, state, nullptr, true, dbp, nullptr)) {

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ class CChainState
693693
bool IsInitialBlockDownload() const;
694694

695695
/** Find the last common block of this chain and a locator. */
696-
CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
696+
const CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
697697

698698
/**
699699
* Make various assertions about the state of the block index.

0 commit comments

Comments
 (0)