From 6d877a19f95c4d4220ebc505dc4706d24d8ff5c9 Mon Sep 17 00:00:00 2001 From: Pol Espinasa Date: Mon, 28 Oct 2024 17:36:26 +0100 Subject: [PATCH 1/3] rpc, logging: move estimate progress from based on time to based on best header chain --- src/validation.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index 64588e802d7..3d984c43617 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4732,7 +4732,6 @@ bool Chainstate::LoadChainTip() // Ignoring return value for now. (void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), *pindex); } - return true; } @@ -5614,11 +5613,12 @@ bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) return ret; } -//! Guess how far we are in the verification process at the given block index +//! Guess how far we are in the verification process at the given block index and the best headers chain //! require cs_main if pindex has not been validated yet (because m_chain_tx_count might be unset) double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) const { const ChainTxData& data{GetParams().TxData()}; + LOCK(::cs_main); if (pindex == nullptr) { return 0.0; } @@ -5629,16 +5629,22 @@ double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) c return 0.0; } - int64_t nNow = time(nullptr); + int64_t end_of_chain_timestamp = pindex->GetBlockTime(); + + if (m_best_header && m_best_header->nChainWork > pindex->nChainWork) { + int64_t header_age = time(nullptr) - m_best_header->GetBlockTime(); + if (header_age < 24 * 60 * 60) { + end_of_chain_timestamp = std::max(end_of_chain_timestamp, m_best_header->GetBlockTime()); + } + } double fTxTotal; if (pindex->m_chain_tx_count <= data.tx_count) { - fTxTotal = data.tx_count + (nNow - data.nTime) * data.dTxRate; + fTxTotal = data.tx_count + (end_of_chain_timestamp - data.nTime) * data.dTxRate; } else { - fTxTotal = pindex->m_chain_tx_count + (nNow - pindex->GetBlockTime()) * data.dTxRate; + fTxTotal = pindex->m_chain_tx_count + (end_of_chain_timestamp - pindex->GetBlockTime()) * data.dTxRate; } - return std::min(pindex->m_chain_tx_count / fTxTotal, 1.0); } From ef15351b7605a1b93d0c0f77607802553219f258 Mon Sep 17 00:00:00 2001 From: Pol Espinasa Date: Mon, 3 Feb 2025 23:06:13 +0100 Subject: [PATCH 2/3] cleanup and locking improvement --- src/node/interfaces.cpp | 5 +++-- src/validation.cpp | 6 +----- src/validation.h | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 7ae2ff64536..f1c380405e2 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -324,7 +324,8 @@ public: } double getVerificationProgress() override { - return chainman().GuessVerificationProgress(WITH_LOCK(chainman().GetMutex(), return chainman().ActiveChain().Tip())); + LOCK(chainman().GetMutex()); + return chainman().GuessVerificationProgress(chainman().ActiveChain().Tip()); } bool isInitialBlockDownload() override { @@ -408,7 +409,7 @@ public: { return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn, this](SynchronizationState sync_state, const CBlockIndex* block) { fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()}, - chainman().GuessVerificationProgress(block)); + WITH_LOCK(chainman().GetMutex(), return chainman().GuessVerificationProgress(block))); })); } std::unique_ptr handleNotifyHeaderTip(NotifyHeaderTipFn fn) override diff --git a/src/validation.cpp b/src/validation.cpp index 3d984c43617..8f96f21d007 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -5613,12 +5613,8 @@ bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) return ret; } -//! Guess how far we are in the verification process at the given block index and the best headers chain -//! require cs_main if pindex has not been validated yet (because m_chain_tx_count might be unset) double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) const { - const ChainTxData& data{GetParams().TxData()}; - LOCK(::cs_main); if (pindex == nullptr) { return 0.0; } @@ -5639,7 +5635,7 @@ double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) c } double fTxTotal; - + const ChainTxData& data{GetParams().TxData()}; if (pindex->m_chain_tx_count <= data.tx_count) { fTxTotal = data.tx_count + (end_of_chain_timestamp - data.nTime) * data.dTxRate; } else { diff --git a/src/validation.h b/src/validation.h index 9e4fdbe6809..2223dffc249 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1149,7 +1149,7 @@ public: bool IsInitialBlockDownload() const; /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */ - double GuessVerificationProgress(const CBlockIndex* pindex) const; + double GuessVerificationProgress(const CBlockIndex* pindex) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main); /** * Import blocks from an external file From 317ddca8e68637f0ad774e46f69b3a5d29218cf6 Mon Sep 17 00:00:00 2001 From: Pol Espinasa Date: Mon, 3 Feb 2025 23:32:12 +0100 Subject: [PATCH 3/3] use buid-in time functionality --- src/validation.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index 8f96f21d007..bee19f917ce 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -5625,10 +5625,9 @@ double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) c return 0.0; } - int64_t end_of_chain_timestamp = pindex->GetBlockTime(); - + int64_t end_of_chain_timestamp = TicksSinceEpoch(NodeClock::time_point{std::chrono::seconds{pindex->GetBlockTime()}}); if (m_best_header && m_best_header->nChainWork > pindex->nChainWork) { - int64_t header_age = time(nullptr) - m_best_header->GetBlockTime(); + int64_t header_age = TicksSinceEpoch(Now()) - m_best_header->GetBlockTime(); if (header_age < 24 * 60 * 60) { end_of_chain_timestamp = std::max(end_of_chain_timestamp, m_best_header->GetBlockTime()); }