refactor: more const annotations for uses of CBlockIndex*

This commit is contained in:
Anthony Towns 2022-03-02 15:42:57 +10:00 committed by Carl Dong
parent 430acb7d2a
commit 5be9ee3c54
10 changed files with 26 additions and 25 deletions

View File

@ -277,7 +277,7 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n
{ {
LOCK(cs_main); LOCK(cs_main);
CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())}; const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
const auto& consensus_params{Params().GetConsensus()}; const auto& consensus_params{Params().GetConsensus()};
do { do {

View File

@ -408,13 +408,13 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
return true; return true;
} }
CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data) const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
{ {
const MapCheckpoints& checkpoints = data.mapCheckpoints; const MapCheckpoints& checkpoints = data.mapCheckpoints;
for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) { for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) {
const uint256& hash = i.second; const uint256& hash = i.second;
CBlockIndex* pindex = LookupBlockIndex(hash); const CBlockIndex* pindex = LookupBlockIndex(hash);
if (pindex) { if (pindex) {
return pindex; return pindex;
} }

View File

@ -163,7 +163,7 @@ public:
uint64_t CalculateCurrentUsage(); uint64_t CalculateCurrentUsage();
//! Returns last CBlockIndex* that is a checkpoint //! Returns last CBlockIndex* that is a checkpoint
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main); const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
~BlockManager() ~BlockManager()
{ {

View File

@ -490,7 +490,7 @@ public:
{ {
LOCK(cs_main); LOCK(cs_main);
const CChainState& active = Assert(m_node.chainman)->ActiveChainstate(); const CChainState& active = Assert(m_node.chainman)->ActiveChainstate();
if (CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) { if (const CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
return fork->nHeight; return fork->nHeight;
} }
return std::nullopt; return std::nullopt;
@ -557,7 +557,7 @@ public:
// used to limit the range, and passing min_height that's too low or // used to limit the range, and passing min_height that's too low or
// max_height that's too high will not crash or change the result. // max_height that's too high will not crash or change the result.
LOCK(::cs_main); LOCK(::cs_main);
if (CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) { if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height); if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) { for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
// Check pprev to not segfault if min_height is too low // Check pprev to not segfault if min_height is too low

View File

@ -50,7 +50,7 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block)); tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
block.vtx.at(0) = MakeTransactionRef(tx); block.vtx.at(0) = MakeTransactionRef(tx);
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)); const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus()); GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus());
block.hashMerkleRoot = BlockMerkleRoot(block); block.hashMerkleRoot = BlockMerkleRoot(block);

View File

@ -283,8 +283,8 @@ static bool rest_block(const std::any& context,
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
CBlock block; CBlock block;
CBlockIndex* pblockindex = nullptr; const CBlockIndex* pblockindex = nullptr;
CBlockIndex* tip = nullptr; const CBlockIndex* tip = nullptr;
{ {
ChainstateManager* maybe_chainman = GetChainman(context, req); ChainstateManager* maybe_chainman = GetChainman(context, req);
if (!maybe_chainman) return false; if (!maybe_chainman) return false;

View File

@ -110,7 +110,8 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
return blockindex == tip ? 1 : -1; return blockindex == tip ? 1 : -1;
} }
CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainman) { static const CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainman)
{
LOCK(::cs_main); LOCK(::cs_main);
CChain& active_chain = chainman.ActiveChain(); CChain& active_chain = chainman.ActiveChain();
@ -127,7 +128,7 @@ CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainma
return active_chain[height]; return active_chain[height];
} else { } else {
const uint256 hash{ParseHashV(param, "hash_or_height")}; const uint256 hash{ParseHashV(param, "hash_or_height")};
CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash); const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
if (!pindex) { if (!pindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
@ -854,7 +855,7 @@ static RPCHelpMan getblockhash()
if (nHeight < 0 || nHeight > active_chain.Height()) if (nHeight < 0 || nHeight > active_chain.Height())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
CBlockIndex* pblockindex = active_chain[nHeight]; const CBlockIndex* pblockindex = active_chain[nHeight];
return pblockindex->GetBlockHash().GetHex(); return pblockindex->GetBlockHash().GetHex();
}, },
}; };
@ -1132,7 +1133,7 @@ static RPCHelpMan pruneblockchain()
// too low to be a block time (corresponds to timestamp from Sep 2001). // too low to be a block time (corresponds to timestamp from Sep 2001).
if (heightParam > 1000000000) { if (heightParam > 1000000000) {
// Add a 2 hour buffer to include blocks which might have had old timestamps // Add a 2 hour buffer to include blocks which might have had old timestamps
CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0); const CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0);
if (!pindex) { if (!pindex) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Could not find block with at least the specified timestamp."); throw JSONRPCError(RPC_INVALID_PARAMETER, "Could not find block with at least the specified timestamp.");
} }
@ -1226,7 +1227,7 @@ static RPCHelpMan gettxoutsetinfo()
{ {
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
CBlockIndex* pindex{nullptr}; const CBlockIndex* pindex{nullptr};
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())}; const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
CCoinsStats stats{hash_type}; CCoinsStats stats{hash_type};
stats.index_requested = request.params[2].isNull() || request.params[2].get_bool(); stats.index_requested = request.params[2].isNull() || request.params[2].get_bool();
@ -2177,7 +2178,7 @@ static RPCHelpMan getblockstats()
{ {
ChainstateManager& chainman = EnsureAnyChainman(request.context); ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(cs_main); LOCK(cs_main);
CBlockIndex* pindex{ParseHashOrHeight(request.params[0], chainman)}; const CBlockIndex* pindex{ParseHashOrHeight(request.params[0], chainman)};
CHECK_NONFATAL(pindex != nullptr); CHECK_NONFATAL(pindex != nullptr);
std::set<std::string> stats; std::set<std::string> stats;
@ -2572,7 +2573,7 @@ static RPCHelpMan scantxoutset()
g_should_abort_scan = false; g_should_abort_scan = false;
int64_t count = 0; int64_t count = 0;
std::unique_ptr<CCoinsViewCursor> pcursor; std::unique_ptr<CCoinsViewCursor> pcursor;
CBlockIndex* tip; const CBlockIndex* tip;
NodeContext& node = EnsureAnyNodeContext(request.context); NodeContext& node = EnsureAnyNodeContext(request.context);
{ {
ChainstateManager& chainman = EnsureChainman(node); ChainstateManager& chainman = EnsureChainman(node);
@ -2760,7 +2761,7 @@ UniValue CreateUTXOSnapshot(
{ {
std::unique_ptr<CCoinsViewCursor> pcursor; std::unique_ptr<CCoinsViewCursor> pcursor;
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED}; CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
CBlockIndex* tip; const CBlockIndex* tip;
{ {
// We need to lock cs_main to ensure that the coinsdb isn't written to // We need to lock cs_main to ensure that the coinsdb isn't written to

View File

@ -67,7 +67,7 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
LOCK(cs_main); LOCK(cs_main);
entry.pushKV("blockhash", hashBlock.GetHex()); entry.pushKV("blockhash", hashBlock.GetHex());
CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock); const CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
if (pindex) { if (pindex) {
if (active_chainstate.m_chain.Contains(pindex)) { if (active_chainstate.m_chain.Contains(pindex)) {
entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight); entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
@ -207,7 +207,7 @@ static RPCHelpMan getrawtransaction()
bool in_active_chain = true; bool in_active_chain = true;
uint256 hash = ParseHashV(request.params[0], "parameter 1"); uint256 hash = ParseHashV(request.params[0], "parameter 1");
CBlockIndex* blockindex = nullptr; const CBlockIndex* blockindex = nullptr;
if (hash == Params().GenesisBlock().hashMerkleRoot) { if (hash == Params().GenesisBlock().hashMerkleRoot) {
// Special exception for the genesis block coinbase transaction // Special exception for the genesis block coinbase transaction
@ -302,7 +302,7 @@ static RPCHelpMan gettxoutproof()
} }
} }
CBlockIndex* pblockindex = nullptr; const CBlockIndex* pblockindex = nullptr;
uint256 hashBlock; uint256 hashBlock;
ChainstateManager& chainman = EnsureAnyChainman(request.context); ChainstateManager& chainman = EnsureAnyChainman(request.context);
if (!request.params[1].isNull()) { if (!request.params[1].isNull()) {

View File

@ -152,14 +152,14 @@ arith_uint256 nMinimumChainWork;
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE); CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const const CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
// Find the latest block common to locator and chain - we expect that // Find the latest block common to locator and chain - we expect that
// locator.vHave is sorted descending by height. // locator.vHave is sorted descending by height.
for (const uint256& hash : locator.vHave) { for (const uint256& hash : locator.vHave) {
CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)}; const CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
if (pindex) { if (pindex) {
if (m_chain.Contains(pindex)) { if (m_chain.Contains(pindex)) {
return pindex; return pindex;
@ -3373,7 +3373,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
// Don't accept any forks from the main chain prior to last checkpoint. // Don't accept any forks from the main chain prior to last checkpoint.
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
// BlockIndex(). // BlockIndex().
CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(params.Checkpoints()); const CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(params.Checkpoints());
if (pcheckpoint && nHeight < pcheckpoint->nHeight) { if (pcheckpoint && nHeight < pcheckpoint->nHeight) {
LogPrintf("ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__, nHeight); LogPrintf("ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__, nHeight);
return state.Invalid(BlockValidationResult::BLOCK_CHECKPOINT, "bad-fork-prior-to-checkpoint"); return state.Invalid(BlockValidationResult::BLOCK_CHECKPOINT, "bad-fork-prior-to-checkpoint");
@ -4186,7 +4186,7 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
} }
// process in case the block isn't known yet // process in case the block isn't known yet
CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash); const CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) { if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
BlockValidationState state; BlockValidationState state;
if (AcceptBlock(pblock, state, nullptr, true, dbp, nullptr)) { if (AcceptBlock(pblock, state, nullptr, true, dbp, nullptr)) {

View File

@ -693,7 +693,7 @@ public:
bool IsInitialBlockDownload() const; bool IsInitialBlockDownload() const;
/** Find the last common block of this chain and a locator. */ /** Find the last common block of this chain and a locator. */
CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main); const CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** /**
* Make various assertions about the state of the block index. * Make various assertions about the state of the block index.