Skip to content

Commit 17b51cd

Browse files
author
MarcoFalke
committed
Merge #21713: Refactor ProcessNewBlock to reduce code duplication
9a06535 Refactor ProcessNewBlock to reduce code duplication (R E Broadley) Pull request description: There are probably a few issues with this code (maybe there's even a reason this code is duplicated as it currently is), so apologies in advance that I'm still a little (maybe very) bad with C++ ACKs for top commit: MarcoFalke: ACK 9a06535 💻 promag: Code review ACK 9a06535. theStack: Code-review ACK 9a06535 🌴 Tree-SHA512: f8634ffad4b2370204d1a0945db4e27248b9e579d9912784da432b8ee3303cae424fa9f7500000dcfb31e6d29d04a8f7d322d17a6fe3d4adaddd10c539458a8c
2 parents ed133fe + 9a06535 commit 17b51cd

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/net_processing.cpp

100644100755
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ class PeerManagerImpl final : public PeerManager
451451

452452
void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(::cs_main);
453453

454+
void ProcessBlock(CNode& pfrom, const std::shared_ptr<const CBlock>& pblock, bool fForceProcessing);
455+
454456
/** Relay map (txid or wtxid -> CTransactionRef) */
455457
typedef std::map<uint256, CTransactionRef> MapRelay;
456458
MapRelay mapRelay GUARDED_BY(cs_main);
@@ -2309,6 +2311,18 @@ void PeerManagerImpl::ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv)
23092311
m_connman.PushMessage(&peer, std::move(msg));
23102312
}
23112313

2314+
void PeerManagerImpl::ProcessBlock(CNode& pfrom, const std::shared_ptr<const CBlock>& pblock, bool fForceProcessing)
2315+
{
2316+
bool fNewBlock = false;
2317+
m_chainman.ProcessNewBlock(m_chainparams, pblock, fForceProcessing, &fNewBlock);
2318+
if (fNewBlock) {
2319+
pfrom.nLastBlockTime = GetTime();
2320+
} else {
2321+
LOCK(cs_main);
2322+
mapBlockSource.erase(pblock->GetHash());
2323+
}
2324+
}
2325+
23122326
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
23132327
const std::chrono::microseconds time_received,
23142328
const std::atomic<bool>& interruptMsgProc)
@@ -3390,7 +3404,6 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33903404
LOCK(cs_main);
33913405
mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
33923406
}
3393-
bool fNewBlock = false;
33943407
// Setting fForceProcessing to true means that we bypass some of
33953408
// our anti-DoS protections in AcceptBlock, which filters
33963409
// unrequested blocks that might be trying to waste our resources
@@ -3400,13 +3413,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
34003413
// we have a chain with at least nMinimumChainWork), and we ignore
34013414
// compact blocks with less work than our tip, it is safe to treat
34023415
// reconstructed compact blocks as having been requested.
3403-
m_chainman.ProcessNewBlock(m_chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
3404-
if (fNewBlock) {
3405-
pfrom.nLastBlockTime = GetTime();
3406-
} else {
3407-
LOCK(cs_main);
3408-
mapBlockSource.erase(pblock->GetHash());
3409-
}
3416+
ProcessBlock(pfrom, pblock, /*fForceProcessing=*/true);
34103417
LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
34113418
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
34123419
// Clear download state for this block, which is in
@@ -3483,20 +3490,13 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
34833490
}
34843491
} // Don't hold cs_main when we call into ProcessNewBlock
34853492
if (fBlockRead) {
3486-
bool fNewBlock = false;
34873493
// Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
34883494
// even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
34893495
// This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
34903496
// disk-space attacks), but this should be safe due to the
34913497
// protections in the compact block handler -- see related comment
34923498
// in compact block optimistic reconstruction handling.
3493-
m_chainman.ProcessNewBlock(m_chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
3494-
if (fNewBlock) {
3495-
pfrom.nLastBlockTime = GetTime();
3496-
} else {
3497-
LOCK(cs_main);
3498-
mapBlockSource.erase(pblock->GetHash());
3499-
}
3499+
ProcessBlock(pfrom, pblock, /*fForceProcessing=*/true);
35003500
}
35013501
return;
35023502
}
@@ -3551,14 +3551,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
35513551
// cs_main in ProcessNewBlock is fine.
35523552
mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
35533553
}
3554-
bool fNewBlock = false;
3555-
m_chainman.ProcessNewBlock(m_chainparams, pblock, forceProcessing, &fNewBlock);
3556-
if (fNewBlock) {
3557-
pfrom.nLastBlockTime = GetTime();
3558-
} else {
3559-
LOCK(cs_main);
3560-
mapBlockSource.erase(pblock->GetHash());
3561-
}
3554+
ProcessBlock(pfrom, pblock, forceProcessing);
35623555
return;
35633556
}
35643557

0 commit comments

Comments
 (0)