mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
Refactor ProcessNewBlock to reduce code duplication
This commit is contained in:
parent
8e69370b15
commit
9a0653553a
1 changed files with 17 additions and 24 deletions
41
src/net_processing.cpp
Normal file → Executable file
41
src/net_processing.cpp
Normal file → Executable file
|
@ -451,6 +451,8 @@ private:
|
||||||
|
|
||||||
void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(::cs_main);
|
void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(::cs_main);
|
||||||
|
|
||||||
|
void ProcessBlock(CNode& pfrom, const std::shared_ptr<const CBlock>& pblock, bool fForceProcessing);
|
||||||
|
|
||||||
/** Relay map (txid or wtxid -> CTransactionRef) */
|
/** Relay map (txid or wtxid -> CTransactionRef) */
|
||||||
typedef std::map<uint256, CTransactionRef> MapRelay;
|
typedef std::map<uint256, CTransactionRef> MapRelay;
|
||||||
MapRelay mapRelay GUARDED_BY(cs_main);
|
MapRelay mapRelay GUARDED_BY(cs_main);
|
||||||
|
@ -2309,6 +2311,18 @@ void PeerManagerImpl::ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv)
|
||||||
m_connman.PushMessage(&peer, std::move(msg));
|
m_connman.PushMessage(&peer, std::move(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PeerManagerImpl::ProcessBlock(CNode& pfrom, const std::shared_ptr<const CBlock>& pblock, bool fForceProcessing)
|
||||||
|
{
|
||||||
|
bool fNewBlock = false;
|
||||||
|
m_chainman.ProcessNewBlock(m_chainparams, pblock, fForceProcessing, &fNewBlock);
|
||||||
|
if (fNewBlock) {
|
||||||
|
pfrom.nLastBlockTime = GetTime();
|
||||||
|
} else {
|
||||||
|
LOCK(cs_main);
|
||||||
|
mapBlockSource.erase(pblock->GetHash());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
|
||||||
const std::chrono::microseconds time_received,
|
const std::chrono::microseconds time_received,
|
||||||
const std::atomic<bool>& interruptMsgProc)
|
const std::atomic<bool>& interruptMsgProc)
|
||||||
|
@ -3390,7 +3404,6 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
|
mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
|
||||||
}
|
}
|
||||||
bool fNewBlock = false;
|
|
||||||
// Setting fForceProcessing to true means that we bypass some of
|
// Setting fForceProcessing to true means that we bypass some of
|
||||||
// our anti-DoS protections in AcceptBlock, which filters
|
// our anti-DoS protections in AcceptBlock, which filters
|
||||||
// unrequested blocks that might be trying to waste our resources
|
// unrequested blocks that might be trying to waste our resources
|
||||||
|
@ -3400,13 +3413,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||||
// we have a chain with at least nMinimumChainWork), and we ignore
|
// we have a chain with at least nMinimumChainWork), and we ignore
|
||||||
// compact blocks with less work than our tip, it is safe to treat
|
// compact blocks with less work than our tip, it is safe to treat
|
||||||
// reconstructed compact blocks as having been requested.
|
// reconstructed compact blocks as having been requested.
|
||||||
m_chainman.ProcessNewBlock(m_chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
|
ProcessBlock(pfrom, pblock, /*fForceProcessing=*/true);
|
||||||
if (fNewBlock) {
|
|
||||||
pfrom.nLastBlockTime = GetTime();
|
|
||||||
} else {
|
|
||||||
LOCK(cs_main);
|
|
||||||
mapBlockSource.erase(pblock->GetHash());
|
|
||||||
}
|
|
||||||
LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
|
LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
|
||||||
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
|
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
|
||||||
// Clear download state for this block, which is in
|
// Clear download state for this block, which is in
|
||||||
|
@ -3483,20 +3490,13 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||||
}
|
}
|
||||||
} // Don't hold cs_main when we call into ProcessNewBlock
|
} // Don't hold cs_main when we call into ProcessNewBlock
|
||||||
if (fBlockRead) {
|
if (fBlockRead) {
|
||||||
bool fNewBlock = false;
|
|
||||||
// Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
|
// Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
|
||||||
// even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
|
// even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
|
||||||
// This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
|
// This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
|
||||||
// disk-space attacks), but this should be safe due to the
|
// disk-space attacks), but this should be safe due to the
|
||||||
// protections in the compact block handler -- see related comment
|
// protections in the compact block handler -- see related comment
|
||||||
// in compact block optimistic reconstruction handling.
|
// in compact block optimistic reconstruction handling.
|
||||||
m_chainman.ProcessNewBlock(m_chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
|
ProcessBlock(pfrom, pblock, /*fForceProcessing=*/true);
|
||||||
if (fNewBlock) {
|
|
||||||
pfrom.nLastBlockTime = GetTime();
|
|
||||||
} else {
|
|
||||||
LOCK(cs_main);
|
|
||||||
mapBlockSource.erase(pblock->GetHash());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -3551,14 +3551,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||||
// cs_main in ProcessNewBlock is fine.
|
// cs_main in ProcessNewBlock is fine.
|
||||||
mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
|
mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
|
||||||
}
|
}
|
||||||
bool fNewBlock = false;
|
ProcessBlock(pfrom, pblock, forceProcessing);
|
||||||
m_chainman.ProcessNewBlock(m_chainparams, pblock, forceProcessing, &fNewBlock);
|
|
||||||
if (fNewBlock) {
|
|
||||||
pfrom.nLastBlockTime = GetTime();
|
|
||||||
} else {
|
|
||||||
LOCK(cs_main);
|
|
||||||
mapBlockSource.erase(pblock->GetHash());
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue