Skip to content

Commit a490f0a

Browse files
committed
net_processing: move MarkBlockAs*, TipMayBeStale, FindNextBlocksToDL to PeerManagerImpl
Allows converting mapBlocksInFlight and g_last_tip_update from globals to member variables.
1 parent 052d9bc commit a490f0a

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

src/net_processing.cpp

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ void EraseOrphansFor(NodeId peer);
169169

170170
// Internal stuff
171171
namespace {
172+
/** Blocks that are in flight, and that are in the queue to be downloaded. */
173+
struct QueuedBlock {
174+
uint256 hash;
175+
const CBlockIndex* pindex; //!< Optional.
176+
bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
177+
std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
178+
};
179+
172180
/**
173181
* Data structure for an individual peer. This struct is not protected by
174182
* cs_main since it does not contain validation-critical data.
@@ -409,19 +417,33 @@ class PeerManagerImpl final : public PeerManager
409417
*/
410418
Mutex m_recent_confirmed_transactions_mutex;
411419
std::unique_ptr<CRollingBloomFilter> m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex);
420+
421+
/* Returns a bool indicating whether we requested this block.
422+
* Also used if a block was /not/ received and timed out or started with another peer
423+
*/
424+
bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
425+
426+
/* Mark a block as in flight
427+
* Returns false, still setting pit, if the block was already in flight from the same peer
428+
* pit will only be valid as long as the same cs_main lock is being held
429+
*/
430+
bool MarkBlockAsInFlight(CTxMemPool& mempool, NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
431+
432+
bool TipMayBeStale(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
433+
434+
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
435+
* at most count entries.
436+
*/
437+
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
438+
439+
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
440+
441+
/** When our tip was last updated. */
442+
std::atomic<int64_t> m_last_tip_update{0};
412443
};
413444
} // namespace
414445

415446
namespace {
416-
/** Blocks that are in flight, and that are in the queue to be downloaded. */
417-
struct QueuedBlock {
418-
uint256 hash;
419-
const CBlockIndex* pindex; //!< Optional.
420-
bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
421-
std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
422-
};
423-
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
424-
425447
/** Stack of nodes which we have set to announce using compact blocks */
426448
std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
427449

@@ -431,9 +453,6 @@ namespace {
431453
/** Number of peers from which we're downloading blocks. */
432454
int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0;
433455

434-
/** When our tip was last updated. */
435-
std::atomic<int64_t> g_last_tip_update(0);
436-
437456
/** Relay map (txid or wtxid -> CTransactionRef) */
438457
typedef std::map<uint256, CTransactionRef> MapRelay;
439458
MapRelay mapRelay GUARDED_BY(cs_main);
@@ -612,9 +631,8 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
612631
nPreferredDownload += state->fPreferredDownload;
613632
}
614633

615-
// Returns a bool indicating whether we requested this block.
616-
// Also used if a block was /not/ received and timed out or started with another peer
617-
static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
634+
bool PeerManagerImpl::MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
635+
{
618636
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
619637
if (itInFlight != mapBlocksInFlight.end()) {
620638
CNodeState *state = State(itInFlight->second.first);
@@ -637,9 +655,8 @@ static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs
637655
return false;
638656
}
639657

640-
// returns false, still setting pit, if the block was already in flight from the same peer
641-
// pit will only be valid as long as the same cs_main lock is being held
642-
static bool MarkBlockAsInFlight(CTxMemPool& mempool, NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
658+
bool PeerManagerImpl::MarkBlockAsInFlight(CTxMemPool& mempool, NodeId nodeid, const uint256& hash, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
659+
{
643660
CNodeState *state = State(nodeid);
644661
assert(state != nullptr);
645662

@@ -752,13 +769,13 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman& connma
752769
}
753770
}
754771

755-
static bool TipMayBeStale(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
772+
bool PeerManagerImpl::TipMayBeStale(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
756773
{
757774
AssertLockHeld(cs_main);
758-
if (g_last_tip_update == 0) {
759-
g_last_tip_update = GetTime();
775+
if (m_last_tip_update == 0) {
776+
m_last_tip_update = GetTime();
760777
}
761-
return g_last_tip_update < GetTime() - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty();
778+
return m_last_tip_update < GetTime() - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty();
762779
}
763780

764781
static bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -775,9 +792,7 @@ static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIV
775792
return false;
776793
}
777794

778-
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
779-
* at most count entries. */
780-
static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
795+
void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
781796
{
782797
if (count == 0)
783798
return;
@@ -1396,7 +1411,7 @@ void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock
13961411
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
13971412
}
13981413

1399-
g_last_tip_update = GetTime();
1414+
m_last_tip_update = GetTime();
14001415
}
14011416
{
14021417
LOCK(m_recent_confirmed_transactions_mutex);
@@ -4251,7 +4266,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
42514266
// Check whether our tip is stale, and if so, allow using an extra
42524267
// outbound peer
42534268
if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale(m_chainparams.GetConsensus())) {
4254-
LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n", time_in_seconds - g_last_tip_update);
4269+
LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n", time_in_seconds - m_last_tip_update);
42554270
m_connman.SetTryNewOutboundPeer(true);
42564271
} else if (m_connman.GetTryNewOutboundPeer()) {
42574272
m_connman.SetTryNewOutboundPeer(false);

0 commit comments

Comments
 (0)