diff --git a/src/init.cpp b/src/init.cpp index 753450fddf..fc22480999 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1983,8 +1983,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) if (tip_info) { tip_info->block_height = chain_active_height; tip_info->block_time = best_block_time; - tip_info->verification_progress = GuessVerificationProgress(&tip, chainman.GetParams().GetConsensus().nPowTargetSpacing); - + tip_info->verification_progress = chainman.GuessVerificationProgress(&tip); } if (tip_info && chainman.m_best_header) { tip_info->header_height = chainman.m_best_header->nHeight; diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 1e627231d0..b5b4fba3c3 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -324,7 +324,7 @@ public: } double getVerificationProgress() override { - return GuessVerificationProgress(WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()), chainman().GetParams().GetConsensus().nPowTargetSpacing); + return chainman().GuessVerificationProgress(WITH_LOCK(chainman().GetMutex(), return chainman().ActiveChain().Tip())); } bool isInitialBlockDownload() override { @@ -406,9 +406,9 @@ public: } std::unique_ptr handleNotifyBlockTip(NotifyBlockTipFn fn) override { - return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) { + return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn, this](SynchronizationState sync_state, const CBlockIndex* block) { fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()}, - GuessVerificationProgress(block, Params().GetConsensus().nPowTargetSpacing)); + chainman().GuessVerificationProgress(block)); })); } std::unique_ptr handleNotifyHeaderTip(NotifyHeaderTipFn fn) override @@ -639,8 +639,8 @@ public: void findCoins(std::map& coins) override { return FindCoins(m_node, coins); } double guessVerificationProgress(const uint256& block_hash) override { - LOCK(::cs_main); - return GuessVerificationProgress(chainman().m_blockman.LookupBlockIndex(block_hash), chainman().GetParams().GetConsensus().nPowTargetSpacing); + LOCK(chainman().GetMutex()); + return chainman().GuessVerificationProgress(chainman().m_blockman.LookupBlockIndex(block_hash)); } bool hasBlocks(const uint256& block_hash, int min_height, std::optional max_height) override { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 0bedacdf7f..68a4629496 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1508,7 +1508,7 @@ RPCHelpMan getblockchaininfo() } obj.pushKV("time", tip.GetBlockTime()); obj.pushKV("mediantime", tip.GetMedianTimePast()); - obj.pushKV("verificationprogress", GuessVerificationProgress(&tip, chainparams.GetConsensus().nPowTargetSpacing)); + obj.pushKV("verificationprogress", chainman.GuessVerificationProgress(&tip)); obj.pushKV("initialblockdownload", chainman.IsInitialBlockDownload()); if (!g_signed_blocks) { obj.pushKV("chainwork", tip.nChainWork.GetHex()); @@ -3625,7 +3625,7 @@ return RPCHelpMan{ if (tip->nBits > 0) { data.pushKV("difficulty", (double)GetDifficulty(*tip)); } - data.pushKV("verificationprogress", GuessVerificationProgress(tip, Params().GetConsensus().nPowTargetSpacing)); + data.pushKV("verificationprogress", chainman.GuessVerificationProgress(tip)); data.pushKV("coins_db_cache_bytes", cs.m_coinsdb_cache_size_bytes); data.pushKV("coins_tip_cache_bytes", cs.m_coinstip_cache_size_bytes); if (cs.m_from_snapshot_blockhash) { diff --git a/src/validation.cpp b/src/validation.cpp index 16cee540c1..da40e43146 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3419,9 +3419,9 @@ void Chainstate::PruneAndFlush() } static void UpdateTipLog( + const ChainstateManager& chainman, const CCoinsViewCache& coins_tip, const CBlockIndex* tip, - const CChainParams& params, const std::string& func_name, const std::string& prefix, const std::string& warning_messages) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) @@ -3433,7 +3433,7 @@ static void UpdateTipLog( tip->GetBlockHash().ToString(), tip->nHeight, tip->nVersion, log(tip->nChainWork.getdouble()) / log(2.0), tip->m_chain_tx_count, FormatISO8601DateTime(tip->GetBlockTime()), - GuessVerificationProgress(tip, params.GetConsensus().nPowTargetSpacing), + chainman.GuessVerificationProgress(tip), coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)), coins_tip.GetCacheSize(), !warning_messages.empty() ? strprintf(" warning='%s'", warning_messages) : ""); @@ -3456,15 +3456,13 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew) AssertLockHeld(::cs_main); const auto& coins_tip = this->CoinsTip(); - const CChainParams& params{m_chainman.GetParams()}; - // The remainder of the function isn't relevant if we are not acting on // the active chainstate, so return if need be. if (this != &m_chainman.ActiveChainstate()) { // Only log every so often so that we don't bury log messages at the tip. constexpr int BACKGROUND_LOG_INTERVAL = 2000; if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) { - UpdateTipLog(coins_tip, pindexNew, params, __func__, "[background validation] ", ""); + UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "[background validation] ", ""); } return; } @@ -3479,7 +3477,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew) const CBlockIndex* pindex = pindexNew; for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) { WarningBitsConditionChecker checker(m_chainman, bit); - ThresholdState state = checker.GetStateFor(pindex, params.GetConsensus(), m_chainman.m_warningcache.at(bit)); + ThresholdState state = checker.GetStateFor(pindex, m_chainman.GetConsensus(), m_chainman.m_warningcache.at(bit)); if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) { const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit); if (state == ThresholdState::ACTIVE) { @@ -3490,7 +3488,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew) } } } - UpdateTipLog(coins_tip, pindexNew, params, __func__, "", + UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "", util::Join(warning_messages, Untranslated(", ")).original); ForceUntrimHeader(pindexNew); @@ -5354,7 +5352,7 @@ bool Chainstate::LoadChainTip() tip->GetBlockHash().ToString(), m_chain.Height(), FormatISO8601DateTime(tip->GetBlockTime()), - GuessVerificationProgress(tip, m_chainman.GetConsensus().nPowTargetSpacing)); + m_chainman.GuessVerificationProgress(tip)); // Ensure KernelNotifications m_tip_block is set even if no new block arrives. if (this->GetRole() != ChainstateRole::BACKGROUND) { @@ -6243,7 +6241,6 @@ 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. //! Since we have signed fixed-interval blocks, estimating progress is a very easy. //! We can extrapolate the last block time to the current time to estimate how many more blocks @@ -6262,6 +6259,13 @@ double GuessVerificationProgress(const CBlockIndex* pindex, int64_t blockInterva return std::min(1.0, progress); } +//! Guess how far we are in the verification process at the given block index +//! 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 +{ + return ::GuessVerificationProgress(pindex, GetConsensus().nPowTargetSpacing); +} + std::optional ChainstateManager::SnapshotBlockhash() const { LOCK(::cs_main); diff --git a/src/validation.h b/src/validation.h index 669463029e..96ce6e5dd1 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1162,6 +1162,9 @@ public: /** Check whether we are doing an initial block download (synchronizing from disk or network) */ bool IsInitialBlockDownload() const; + /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */ + double GuessVerificationProgress(const CBlockIndex* pindex) const; + /** * Import blocks from an external file *