@@ -513,7 +513,11 @@ class PeerManagerImpl final : public PeerManager
513
513
bool IgnoresIncomingTxs () override { return m_opts.ignore_incoming_txs ; }
514
514
void SendPings () override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
515
515
void RelayTransaction (const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
516
- void SetBestHeight (int height) override { m_best_height = height; };
516
+ void SetBestBlock (int height, std::chrono::seconds time) override
517
+ {
518
+ m_best_height = height;
519
+ m_best_block_time = time;
520
+ };
517
521
void UnitTestMisbehaving (NodeId peer_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving (*Assert (GetPeerRef (peer_id)), howmuch, " " ); };
518
522
void ProcessMessage (CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
519
523
const std::chrono::microseconds time_received, const std::atomic<bool >& interruptMsgProc) override
@@ -721,6 +725,8 @@ class PeerManagerImpl final : public PeerManager
721
725
722
726
/* * The height of the best chain */
723
727
std::atomic<int > m_best_height{-1 };
728
+ /* * The time of the best chain tip block */
729
+ std::atomic<std::chrono::seconds> m_best_block_time{0s};
724
730
725
731
/* * Next time to check for stale tip */
726
732
std::chrono::seconds m_stale_tip_check_time GUARDED_BY (cs_main){0s};
@@ -992,6 +998,12 @@ class PeerManagerImpl final : public PeerManager
992
998
void UpdateBlockAvailability (NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
993
999
bool CanDirectFetch () EXCLUSIVE_LOCKS_REQUIRED(cs_main);
994
1000
1001
+ /* *
1002
+ * Estimates the distance, in blocks, between the best-known block and the network chain tip.
1003
+ * Utilizes the best-block time and the chainparams blocks spacing to approximate it.
1004
+ */
1005
+ int64_t ApproximateBestBlockDepth () const ;
1006
+
995
1007
/* *
996
1008
* To prevent fingerprinting attacks, only send blocks/headers outside of
997
1009
* the active chain if they are no more than a month older (both in time,
@@ -1311,6 +1323,11 @@ bool PeerManagerImpl::TipMayBeStale()
1311
1323
return m_last_tip_update.load () < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3 } && mapBlocksInFlight.empty ();
1312
1324
}
1313
1325
1326
+ int64_t PeerManagerImpl::ApproximateBestBlockDepth () const
1327
+ {
1328
+ return (GetTime<std::chrono::seconds>() - m_best_block_time.load ()).count () / m_chainparams.GetConsensus ().nPowTargetSpacing ;
1329
+ }
1330
+
1314
1331
bool PeerManagerImpl::CanDirectFetch ()
1315
1332
{
1316
1333
return m_chainman.ActiveChain ().Tip ()->Time () > GetAdjustedTime () - m_chainparams.GetConsensus ().PowTargetSpacing () * 20 ;
@@ -2047,7 +2064,7 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
2047
2064
*/
2048
2065
void PeerManagerImpl::UpdatedBlockTip (const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload )
2049
2066
{
2050
- SetBestHeight (pindexNew->nHeight );
2067
+ SetBestBlock (pindexNew->nHeight , std::chrono::seconds{pindexNew-> GetBlockTime ()} );
2051
2068
SetServiceFlagsIBDCache (!fInitialDownload );
2052
2069
2053
2070
// Don't relay inventory during initial block download.
0 commit comments