Skip to content

Commit 8017547

Browse files
committed
Make CBlockIndex*es in net_processing const
1 parent 7dac1e5 commit 8017547

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

src/net_processing.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace {
101101
/** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
102102
struct QueuedBlock {
103103
uint256 hash;
104-
CBlockIndex* pindex; //!< Optional.
104+
const CBlockIndex* pindex; //!< Optional.
105105
bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
106106
std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
107107
};
@@ -156,13 +156,13 @@ struct CNodeState {
156156
//! List of asynchronously-determined block rejections to notify this peer about.
157157
std::vector<CBlockReject> rejects;
158158
//! The best known block we know this peer has announced.
159-
CBlockIndex *pindexBestKnownBlock;
159+
const CBlockIndex *pindexBestKnownBlock;
160160
//! The hash of the last unknown block this peer has announced.
161161
uint256 hashLastUnknownBlock;
162162
//! The last full block we both have.
163-
CBlockIndex *pindexLastCommonBlock;
163+
const CBlockIndex *pindexLastCommonBlock;
164164
//! The best header we have sent our peer.
165-
CBlockIndex *pindexBestHeaderSent;
165+
const CBlockIndex *pindexBestHeaderSent;
166166
//! Length of current-streak of unconnecting headers announcements
167167
int nUnconnectingHeaders;
168168
//! Whether we've started headers synchronization with this peer.
@@ -331,7 +331,7 @@ bool MarkBlockAsReceived(const uint256& hash) {
331331
// Requires cs_main.
332332
// returns false, still setting pit, if the block was already in flight from the same peer
333333
// pit will only be valid as long as the same cs_main lock is being held
334-
bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL, list<QueuedBlock>::iterator **pit = NULL) {
334+
bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, const CBlockIndex *pindex = NULL, list<QueuedBlock>::iterator **pit = NULL) {
335335
CNodeState *state = State(nodeid);
336336
assert(state != NULL);
337337

@@ -432,7 +432,7 @@ bool CanDirectFetch(const Consensus::Params &consensusParams)
432432
}
433433

434434
// Requires cs_main
435-
bool PeerHasHeader(CNodeState *state, CBlockIndex *pindex)
435+
bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
436436
{
437437
if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
438438
return true;
@@ -443,7 +443,7 @@ bool PeerHasHeader(CNodeState *state, CBlockIndex *pindex)
443443

444444
/** Find the last common ancestor two blocks have.
445445
* Both pa and pb must be non-NULL. */
446-
CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
446+
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
447447
if (pa->nHeight > pb->nHeight) {
448448
pa = pa->GetAncestor(pb->nHeight);
449449
} else if (pb->nHeight > pa->nHeight) {
@@ -462,7 +462,7 @@ CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
462462

463463
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
464464
* at most count entries. */
465-
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
465+
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
466466
if (count == 0)
467467
return;
468468

@@ -490,8 +490,8 @@ void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBl
490490
if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
491491
return;
492492

493-
std::vector<CBlockIndex*> vToFetch;
494-
CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
493+
std::vector<const CBlockIndex*> vToFetch;
494+
const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
495495
// Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
496496
// linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
497497
// download that next block if the window were 1 larger.
@@ -514,7 +514,7 @@ void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBl
514514
// are not yet downloaded and not in flight to vBlocks. In the mean time, update
515515
// pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
516516
// already part of our chain (and therefore don't need it even if pruned).
517-
BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
517+
BOOST_FOREACH(const CBlockIndex* pindex, vToFetch) {
518518
if (!pindex->IsValid(BLOCK_VALID_TREE)) {
519519
// We consider the chain that this peer is on invalid.
520520
return;
@@ -1049,7 +1049,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
10491049
}
10501050
}
10511051

1052-
uint32_t GetFetchFlags(CNode* pfrom, CBlockIndex* pprev, const Consensus::Params& chainparams) {
1052+
uint32_t GetFetchFlags(CNode* pfrom, const CBlockIndex* pprev, const Consensus::Params& chainparams) {
10531053
uint32_t nFetchFlags = 0;
10541054
if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
10551055
nFetchFlags |= MSG_WITNESS_FLAG;
@@ -1456,7 +1456,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
14561456
LOCK(cs_main);
14571457

14581458
// Find the last block the caller has in the main chain
1459-
CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
1459+
const CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
14601460

14611461
// Send the rest of the chain
14621462
if (pindex)
@@ -1552,7 +1552,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
15521552
}
15531553

15541554
CNodeState *nodestate = State(pfrom->GetId());
1555-
CBlockIndex* pindex = NULL;
1555+
const CBlockIndex* pindex = NULL;
15561556
if (locator.IsNull())
15571557
{
15581558
// If locator is null, return the hashStop block
@@ -1775,7 +1775,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
17751775
}
17761776
}
17771777

1778-
CBlockIndex *pindex = NULL;
1778+
const CBlockIndex *pindex = NULL;
17791779
CValidationState state;
17801780
if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
17811781
int nDoS;
@@ -2051,7 +2051,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
20512051
return true;
20522052
}
20532053

2054-
CBlockIndex *pindexLast = NULL;
2054+
const CBlockIndex *pindexLast = NULL;
20552055
{
20562056
LOCK(cs_main);
20572057
CNodeState *nodestate = State(pfrom->GetId());
@@ -2128,8 +2128,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
21282128
// If this set of headers is valid and ends in a block with at least as
21292129
// much work as our tip, download as much as possible.
21302130
if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
2131-
vector<CBlockIndex *> vToFetch;
2132-
CBlockIndex *pindexWalk = pindexLast;
2131+
vector<const CBlockIndex *> vToFetch;
2132+
const CBlockIndex *pindexWalk = pindexLast;
21332133
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
21342134
while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
21352135
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
@@ -2151,7 +2151,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
21512151
} else {
21522152
vector<CInv> vGetData;
21532153
// Download as much as possible, from earliest to latest.
2154-
BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vToFetch) {
2154+
BOOST_REVERSE_FOREACH(const CBlockIndex *pindex, vToFetch) {
21552155
if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
21562156
// Can't download any more from this peer
21572157
break;
@@ -2740,7 +2740,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
27402740
bool fRevertToInv = ((!state.fPreferHeaders &&
27412741
(!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
27422742
pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
2743-
CBlockIndex *pBestIndex = NULL; // last header queued for delivery
2743+
const CBlockIndex *pBestIndex = NULL; // last header queued for delivery
27442744
ProcessBlockAvailability(pto->id); // ensure pindexBestKnownBlock is up-to-date
27452745

27462746
if (!fRevertToInv) {
@@ -2751,7 +2751,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
27512751
BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {
27522752
BlockMap::iterator mi = mapBlockIndex.find(hash);
27532753
assert(mi != mapBlockIndex.end());
2754-
CBlockIndex *pindex = mi->second;
2754+
const CBlockIndex *pindex = mi->second;
27552755
if (chainActive[pindex->nHeight] != pindex) {
27562756
// Bail out if we reorged away from this block
27572757
fRevertToInv = true;
@@ -2828,7 +2828,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
28282828
const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
28292829
BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
28302830
assert(mi != mapBlockIndex.end());
2831-
CBlockIndex *pindex = mi->second;
2831+
const CBlockIndex *pindex = mi->second;
28322832

28332833
// Warn if we're announcing a block that is not on the main chain.
28342834
// This should be very rare and could be optimized out.
@@ -3013,10 +3013,10 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
30133013
//
30143014
vector<CInv> vGetData;
30153015
if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3016-
vector<CBlockIndex*> vToDownload;
3016+
vector<const CBlockIndex*> vToDownload;
30173017
NodeId staller = -1;
30183018
FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
3019-
BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
3019+
BOOST_FOREACH(const CBlockIndex *pindex, vToDownload) {
30203020
uint32_t nFetchFlags = GetFetchFlags(pto, pindex->pprev, consensusParams);
30213021
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
30223022
MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);

src/validation.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,12 +3030,14 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
30303030
}
30313031

30323032
// Exposed wrapper for AcceptBlockHeader
3033-
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
3033+
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex)
30343034
{
30353035
{
30363036
LOCK(cs_main);
30373037
for (const CBlockHeader& header : headers) {
3038-
if (!AcceptBlockHeader(header, state, chainparams, ppindex)) {
3038+
// cast away the ppindex-returns-const CBlockIndex - we're just assigning it to a CBlockIndex*
3039+
// that we own and is updated non-const anyway
3040+
if (!AcceptBlockHeader(header, state, chainparams, const_cast<CBlockIndex**>(ppindex))) {
30393041
return false;
30403042
}
30413043
}

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
243243
* @param[in] chainparams The params for the chain we want to connect to
244244
* @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
245245
*/
246-
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL);
246+
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex=NULL);
247247

248248
/** Check whether enough disk space is available for an incoming block */
249249
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);

0 commit comments

Comments
 (0)