If you're taking a pointer, you should check that it's non-null before dereferencing it. Even better, pass by reference to communicate that this must be called with a valid CBlockIndex:
<details>
<summary>Diff</summary>
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index a822960dc2..f552aa36b5 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -246,7 +246,7 @@ public:
/** Implement PeerManager */
void CheckForStaleTipAndEvictPeers() override;
- bool FetchBlock(NodeId id, const CBlockIndex* index) override;
+ bool FetchBlock(NodeId id, const CBlockIndex& index) override;
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override;
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
void SendPings() override;
@@ -1253,7 +1253,7 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
}
-bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex* index)
+bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& index)
{
PeerRef peer = GetPeerRef(id);
if (peer == nullptr) return false;
@@ -1263,12 +1263,12 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex* index)
if (state == nullptr) return false;
if (!state->fHaveWitness) return false;
const int node_sync_height = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
- if (index->nHeight > node_sync_height) {
+ if (index.nHeight > node_sync_height) {
return false;
}
- std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, index->GetBlockHash())};
+ std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, index.GetBlockHash())};
// Mark block as in-flight unless it already is
- if (!MarkBlockAsInFlight(id, index->GetBlockHash(), index)) return false;
+ if (!MarkBlockAsInFlight(id, index.GetBlockHash(), &index)) return false;
bool success = m_connman.ForNode(id, [this, invs](CNode* pnode) {
const CNetMsgMaker msgMaker(pnode->GetCommonVersion());
this->m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::GETDATA, invs));
@@ -1276,12 +1276,12 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex* index)
});
if (success) {
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
- index->GetBlockHash().ToString(), id);
+ index.GetBlockHash().ToString(), id);
} else {
// Remove block from in-flight
- MarkBlockAsReceived(index->GetBlockHash());
+ MarkBlockAsReceived(index.GetBlockHash());
LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n",
- index->GetBlockHash().ToString(), id);
+ index.GetBlockHash().ToString(), id);
}
return success;
}
diff --git a/src/net_processing.h b/src/net_processing.h
index 577c925562..659504397f 100644
--- a/src/net_processing.h
+++ b/src/net_processing.h
@@ -43,7 +43,7 @@ public:
virtual ~PeerManager() { }
/** Attempt to manually fetch block from a given node. */
- virtual bool FetchBlock(const NodeId nodeid, const CBlockIndex* pindex) = 0;
+ virtual bool FetchBlock(const NodeId nodeid, const CBlockIndex& pindex) = 0;
/** Get statistics from node state */
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index aa6b216ab1..cb3f2f3abc 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -746,7 +746,7 @@ static RPCHelpMan getblockfrompeer()
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
- if (!peerman.FetchBlock(nodeid, pblockindex)) {
+ if (!peerman.FetchBlock(nodeid, *pblockindex)) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer");
}
return UniValue::VOBJ;
</details>