@@ -169,6 +169,14 @@ void EraseOrphansFor(NodeId peer);
169
169
170
170
// Internal stuff
171
171
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
+
172
180
/* *
173
181
* Data structure for an individual peer. This struct is not protected by
174
182
* cs_main since it does not contain validation-critical data.
@@ -409,19 +417,33 @@ class PeerManagerImpl final : public PeerManager
409
417
*/
410
418
Mutex m_recent_confirmed_transactions_mutex;
411
419
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 };
412
443
};
413
444
} // namespace
414
445
415
446
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
-
425
447
/* * Stack of nodes which we have set to announce using compact blocks */
426
448
std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY (cs_main);
427
449
@@ -431,9 +453,6 @@ namespace {
431
453
/* * Number of peers from which we're downloading blocks. */
432
454
int nPeersWithValidatedDownloads GUARDED_BY (cs_main) = 0;
433
455
434
- /* * When our tip was last updated. */
435
- std::atomic<int64_t > g_last_tip_update (0 );
436
-
437
456
/* * Relay map (txid or wtxid -> CTransactionRef) */
438
457
typedef std::map<uint256, CTransactionRef> MapRelay;
439
458
MapRelay mapRelay GUARDED_BY (cs_main);
@@ -612,9 +631,8 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
612
631
nPreferredDownload += state->fPreferredDownload ;
613
632
}
614
633
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
+ {
618
636
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find (hash);
619
637
if (itInFlight != mapBlocksInFlight.end ()) {
620
638
CNodeState *state = State (itInFlight->second .first );
@@ -637,9 +655,8 @@ static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs
637
655
return false ;
638
656
}
639
657
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
+ {
643
660
CNodeState *state = State (nodeid);
644
661
assert (state != nullptr );
645
662
@@ -752,13 +769,13 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman& connma
752
769
}
753
770
}
754
771
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)
756
773
{
757
774
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 ();
760
777
}
761
- return g_last_tip_update < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
778
+ return m_last_tip_update < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
762
779
}
763
780
764
781
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
775
792
return false ;
776
793
}
777
794
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)
781
796
{
782
797
if (count == 0 )
783
798
return ;
@@ -1396,7 +1411,7 @@ void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock
1396
1411
LogPrint (BCLog::MEMPOOL, " Erased %d orphan tx included or conflicted by block\n " , nErased);
1397
1412
}
1398
1413
1399
- g_last_tip_update = GetTime ();
1414
+ m_last_tip_update = GetTime ();
1400
1415
}
1401
1416
{
1402
1417
LOCK (m_recent_confirmed_transactions_mutex);
@@ -4251,7 +4266,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
4251
4266
// Check whether our tip is stale, and if so, allow using an extra
4252
4267
// outbound peer
4253
4268
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 );
4255
4270
m_connman.SetTryNewOutboundPeer (true );
4256
4271
} else if (m_connman.GetTryNewOutboundPeer ()) {
4257
4272
m_connman.SetTryNewOutboundPeer (false );
0 commit comments