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.
This commit is contained in:
3 files changed
+25
-6
No files matched your search
+4
-2
@@ -1754,13 +1754,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
// ********************************************************* Step 12: start node
|
// ********************************************************* Step 12: start node
|
||||||
|
|
||||||
//// debug print
|
//// debug print
|
||||||
|
int64_t best_block_time{};
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
|
LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
|
||||||
chain_active_height = chainman.ActiveChain().Height();
|
chain_active_height = chainman.ActiveChain().Height();
|
||||||
|
best_block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
|
||||||
if (tip_info) {
|
if (tip_info) {
|
||||||
tip_info->block_height = chain_active_height;
|
tip_info->block_height = chain_active_height;
|
||||||
tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
|
tip_info->block_time = best_block_time;
|
||||||
tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), chainman.ActiveChain().Tip());
|
tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), chainman.ActiveChain().Tip());
|
||||||
}
|
}
|
||||||
if (tip_info && chainman.m_best_header) {
|
if (tip_info && chainman.m_best_header) {
|
||||||
@@ -1769,7 +1771,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
LogPrintf("nBestHeight = %d\n", chain_active_height);
|
LogPrintf("nBestHeight = %d\n", chain_active_height);
|
||||||
if (node.peerman) node.peerman->SetBestHeight(chain_active_height);
|
if (node.peerman) node.peerman->SetBestBlock(chain_active_height, std::chrono::seconds{best_block_time});
|
||||||
|
|
||||||
// Map ports with UPnP or NAT-PMP.
|
// Map ports with UPnP or NAT-PMP.
|
||||||
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
|
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
|
||||||
|
|||||||
+19
-2
@@ -513,7 +513,11 @@ public:
|
|||||||
bool IgnoresIncomingTxs() override { return m_opts.ignore_incoming_txs; }
|
bool IgnoresIncomingTxs() override { return m_opts.ignore_incoming_txs; }
|
||||||
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
|
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
|
||||||
void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
|
void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
|
||||||
void SetBestHeight(int height) override { m_best_height = height; };
|
void SetBestBlock(int height, std::chrono::seconds time) override
|
||||||
|
{
|
||||||
|
m_best_height = height;
|
||||||
|
m_best_block_time = time;
|
||||||
|
};
|
||||||
void UnitTestMisbehaving(NodeId peer_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving(*Assert(GetPeerRef(peer_id)), howmuch, ""); };
|
void UnitTestMisbehaving(NodeId peer_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving(*Assert(GetPeerRef(peer_id)), howmuch, ""); };
|
||||||
void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
|
void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
|
||||||
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
|
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
|
||||||
@@ -721,6 +725,8 @@ private:
|
|||||||
|
|
||||||
/** The height of the best chain */
|
/** The height of the best chain */
|
||||||
std::atomic<int> m_best_height{-1};
|
std::atomic<int> m_best_height{-1};
|
||||||
|
/** The time of the best chain tip block */
|
||||||
|
std::atomic<std::chrono::seconds> m_best_block_time{0s};
|
||||||
|
|
||||||
/** Next time to check for stale tip */
|
/** Next time to check for stale tip */
|
||||||
std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
|
std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
|
||||||
@@ -992,6 +998,12 @@ private:
|
|||||||
void UpdateBlockAvailability(NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
void UpdateBlockAvailability(NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
bool CanDirectFetch() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
bool CanDirectFetch() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimates the distance, in blocks, between the best-known block and the network chain tip.
|
||||||
|
* Utilizes the best-block time and the chainparams blocks spacing to approximate it.
|
||||||
|
*/
|
||||||
|
int64_t ApproximateBestBlockDepth() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To prevent fingerprinting attacks, only send blocks/headers outside of
|
* To prevent fingerprinting attacks, only send blocks/headers outside of
|
||||||
* the active chain if they are no more than a month older (both in time,
|
* the active chain if they are no more than a month older (both in time,
|
||||||
@@ -1311,6 +1323,11 @@ bool PeerManagerImpl::TipMayBeStale()
|
|||||||
return m_last_tip_update.load() < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3} && mapBlocksInFlight.empty();
|
return m_last_tip_update.load() < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3} && mapBlocksInFlight.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t PeerManagerImpl::ApproximateBestBlockDepth() const
|
||||||
|
{
|
||||||
|
return (GetTime<std::chrono::seconds>() - m_best_block_time.load()).count() / m_chainparams.GetConsensus().nPowTargetSpacing;
|
||||||
|
}
|
||||||
|
|
||||||
bool PeerManagerImpl::CanDirectFetch()
|
bool PeerManagerImpl::CanDirectFetch()
|
||||||
{
|
{
|
||||||
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
|
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
|
|||||||
*/
|
*/
|
||||||
void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
|
void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
|
||||||
{
|
{
|
||||||
SetBestHeight(pindexNew->nHeight);
|
SetBestBlock(pindexNew->nHeight, std::chrono::seconds{pindexNew->GetBlockTime()});
|
||||||
SetServiceFlagsIBDCache(!fInitialDownload);
|
SetServiceFlagsIBDCache(!fInitialDownload);
|
||||||
|
|
||||||
// Don't relay inventory during initial block download.
|
// Don't relay inventory during initial block download.
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ public:
|
|||||||
/** Send ping message to all peers */
|
/** Send ping message to all peers */
|
||||||
virtual void SendPings() = 0;
|
virtual void SendPings() = 0;
|
||||||
|
|
||||||
/** Set the best height */
|
/** Set the height of the best block and its time (seconds since epoch). */
|
||||||
virtual void SetBestHeight(int height) = 0;
|
virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
|
||||||
|
|
||||||
/* Public for unit testing. */
|
/* Public for unit testing. */
|
||||||
virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch) = 0;
|
virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch) = 0;
|
||||||
|
|||||||
Reference in new issue
Block a user