Skip to content

Commit 97df4e3

Browse files
committed
net: store best block tip time inside PeerManager
And implement 'ApproximateBestBlockDepth()' to estimate the distance, in blocks, between the best-known block and the network chain tip. Utilizing the best-block time and the chainparams blocks spacing to approximate it.
1 parent dd39194 commit 97df4e3

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/init.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,13 +1754,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17541754
// ********************************************************* Step 12: start node
17551755

17561756
//// debug print
1757+
int64_t best_block_time{};
17571758
{
17581759
LOCK(cs_main);
17591760
LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
17601761
chain_active_height = chainman.ActiveChain().Height();
1762+
best_block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
17611763
if (tip_info) {
17621764
tip_info->block_height = chain_active_height;
1763-
tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
1765+
tip_info->block_time = best_block_time;
17641766
tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), chainman.ActiveChain().Tip());
17651767
}
17661768
if (tip_info && chainman.m_best_header) {
@@ -1769,7 +1771,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17691771
}
17701772
}
17711773
LogPrintf("nBestHeight = %d\n", chain_active_height);
1772-
if (node.peerman) node.peerman->SetBestHeight(chain_active_height);
1774+
if (node.peerman) node.peerman->SetBestBlock(chain_active_height, std::chrono::seconds{best_block_time});
17731775

17741776
// Map ports with UPnP or NAT-PMP.
17751777
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));

src/net_processing.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,11 @@ class PeerManagerImpl final : public PeerManager
513513
bool IgnoresIncomingTxs() override { return m_opts.ignore_incoming_txs; }
514514
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
515515
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+
};
517521
void UnitTestMisbehaving(NodeId peer_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving(*Assert(GetPeerRef(peer_id)), howmuch, ""); };
518522
void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
519523
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
@@ -721,6 +725,8 @@ class PeerManagerImpl final : public PeerManager
721725

722726
/** The height of the best chain */
723727
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};
724730

725731
/** Next time to check for stale tip */
726732
std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
@@ -992,6 +998,12 @@ class PeerManagerImpl final : public PeerManager
992998
void UpdateBlockAvailability(NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
993999
bool CanDirectFetch() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
9941000

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+
9951007
/**
9961008
* To prevent fingerprinting attacks, only send blocks/headers outside of
9971009
* the active chain if they are no more than a month older (both in time,
@@ -1311,6 +1323,11 @@ bool PeerManagerImpl::TipMayBeStale()
13111323
return m_last_tip_update.load() < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3} && mapBlocksInFlight.empty();
13121324
}
13131325

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+
13141331
bool PeerManagerImpl::CanDirectFetch()
13151332
{
13161333
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
20472064
*/
20482065
void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
20492066
{
2050-
SetBestHeight(pindexNew->nHeight);
2067+
SetBestBlock(pindexNew->nHeight, std::chrono::seconds{pindexNew->GetBlockTime()});
20512068
SetServiceFlagsIBDCache(!fInitialDownload);
20522069

20532070
// Don't relay inventory during initial block download.

src/net_processing.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
9292
/** Send ping message to all peers */
9393
virtual void SendPings() = 0;
9494

95-
/** Set the best height */
96-
virtual void SetBestHeight(int height) = 0;
95+
/** Set the height of the best block and its time (seconds since epoch). */
96+
virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
9797

9898
/* Public for unit testing. */
9999
virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch) = 0;

0 commit comments

Comments
 (0)