Skip to content

Commit 96f828b

Browse files
committed
Merge bitcoin/bitcoin#22221: refactor: Pass block reference instead of pointer to PeerManagerImpl::BlockRequested
fa334b4 refactor: Pass block reference instead of pointer to PeerManagerImpl::BlockRequested (MarcoFalke) Pull request description: This allows to remove an assert and at the same time make it more obvious that the block is never nullptr. Also, add missing `{}` while touching the function. ACKs for top commit: jnewbery: Code review ACK fa334b4 mjdietzx: crACK fa334b4 theStack: Code review ACK fa334b4 Tree-SHA512: 9733d3e20e048fcb2ac7510eae3539ce8aaa7397bd944a265123f1ffd90e15637cdaad19dba16f76d83f3f0d1888f1b7014c191bb430e410a106c49ca61a725c
2 parents 1a369f0 + fa334b4 commit 96f828b

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/net_processing.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class PeerManagerImpl final : public PeerManager
476476
* Returns false, still setting pit, if the block was already in flight from the same peer
477477
* pit will only be valid as long as the same cs_main lock is being held
478478
*/
479-
bool BlockRequested(NodeId nodeid, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
479+
bool BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
480480

481481
bool TipMayBeStale() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
482482

@@ -793,10 +793,9 @@ void PeerManagerImpl::RemoveBlockRequest(const uint256& hash)
793793
mapBlocksInFlight.erase(it);
794794
}
795795

796-
bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit)
796+
bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit)
797797
{
798-
assert(pindex);
799-
const uint256& hash{pindex->GetBlockHash()};
798+
const uint256& hash{block.GetBlockHash()};
800799

801800
CNodeState *state = State(nodeid);
802801
assert(state != nullptr);
@@ -814,16 +813,17 @@ bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex* pindex, s
814813
RemoveBlockRequest(hash);
815814

816815
std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
817-
{pindex, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
816+
{&block, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
818817
state->nBlocksInFlight++;
819818
if (state->nBlocksInFlight == 1) {
820819
// We're starting a block download (batch) from this peer.
821820
state->m_downloading_since = GetTime<std::chrono::microseconds>();
822821
m_peers_downloading_from++;
823822
}
824823
itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
825-
if (pit)
824+
if (pit) {
826825
*pit = &itInFlight->second.second;
826+
}
827827
return true;
828828
}
829829

@@ -2091,7 +2091,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
20912091
}
20922092
uint32_t nFetchFlags = GetFetchFlags(pfrom);
20932093
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
2094-
BlockRequested(pfrom.GetId(), pindex);
2094+
BlockRequested(pfrom.GetId(), *pindex);
20952095
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
20962096
pindex->GetBlockHash().ToString(), pfrom.GetId());
20972097
}
@@ -3394,7 +3394,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33943394
if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
33953395
(fAlreadyInFlight && blockInFlightIt->second.first == pfrom.GetId())) {
33963396
std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
3397-
if (!BlockRequested(pfrom.GetId(), pindex, &queuedBlockIt)) {
3397+
if (!BlockRequested(pfrom.GetId(), *pindex, &queuedBlockIt)) {
33983398
if (!(*queuedBlockIt)->partialBlock)
33993399
(*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&m_mempool));
34003400
else {
@@ -4778,7 +4778,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
47784778
for (const CBlockIndex *pindex : vToDownload) {
47794779
uint32_t nFetchFlags = GetFetchFlags(*pto);
47804780
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
4781-
BlockRequested(pto->GetId(), pindex);
4781+
BlockRequested(pto->GetId(), *pindex);
47824782
LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
47834783
pindex->nHeight, pto->GetId());
47844784
}

0 commit comments

Comments
 (0)