Skip to content

Commit facb4d0

Browse files
author
MarcoFalke
committed
refactor: Move GuessVerificationProgress into ChainstateManager
1 parent d73f37d commit facb4d0

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18271827
if (tip_info) {
18281828
tip_info->block_height = chain_active_height;
18291829
tip_info->block_time = best_block_time;
1830-
tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), &tip);
1830+
tip_info->verification_progress = chainman.GuessVerificationProgress(&tip);
18311831
}
18321832
if (tip_info && chainman.m_best_header) {
18331833
tip_info->header_height = chainman.m_best_header->nHeight;

src/node/interfaces.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class NodeImpl : public Node
324324
}
325325
double getVerificationProgress() override
326326
{
327-
return GuessVerificationProgress(chainman().GetParams().TxData(), WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()));
327+
return chainman().GuessVerificationProgress(WITH_LOCK(chainman().GetMutex(), return chainman().ActiveChain().Tip()));
328328
}
329329
bool isInitialBlockDownload() override
330330
{
@@ -406,9 +406,9 @@ class NodeImpl : public Node
406406
}
407407
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
408408
{
409-
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
409+
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn, this](SynchronizationState sync_state, const CBlockIndex* block) {
410410
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
411-
GuessVerificationProgress(Params().TxData(), block));
411+
chainman().GuessVerificationProgress(block));
412412
}));
413413
}
414414
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
@@ -639,8 +639,8 @@ class ChainImpl : public Chain
639639
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
640640
double guessVerificationProgress(const uint256& block_hash) override
641641
{
642-
LOCK(::cs_main);
643-
return GuessVerificationProgress(chainman().GetParams().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
642+
LOCK(chainman().GetMutex());
643+
return chainman().GuessVerificationProgress(chainman().m_blockman.LookupBlockIndex(block_hash));
644644
}
645645
bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override
646646
{

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ RPCHelpMan getblockchaininfo()
13361336
obj.pushKV("difficulty", GetDifficulty(tip));
13371337
obj.pushKV("time", tip.GetBlockTime());
13381338
obj.pushKV("mediantime", tip.GetMedianTimePast());
1339-
obj.pushKV("verificationprogress", GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
1339+
obj.pushKV("verificationprogress", chainman.GuessVerificationProgress(&tip));
13401340
obj.pushKV("initialblockdownload", chainman.IsInitialBlockDownload());
13411341
obj.pushKV("chainwork", tip.nChainWork.GetHex());
13421342
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
@@ -3338,7 +3338,7 @@ return RPCHelpMan{
33383338
data.pushKV("blocks", (int)chain.Height());
33393339
data.pushKV("bestblockhash", tip->GetBlockHash().GetHex());
33403340
data.pushKV("difficulty", GetDifficulty(*tip));
3341-
data.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), tip));
3341+
data.pushKV("verificationprogress", chainman.GuessVerificationProgress(tip));
33423342
data.pushKV("coins_db_cache_bytes", cs.m_coinsdb_cache_size_bytes);
33433343
data.pushKV("coins_tip_cache_bytes", cs.m_coinstip_cache_size_bytes);
33443344
if (cs.m_from_snapshot_blockhash) {

src/validation.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,9 +2983,9 @@ void Chainstate::PruneAndFlush()
29832983
}
29842984

29852985
static void UpdateTipLog(
2986+
const ChainstateManager& chainman,
29862987
const CCoinsViewCache& coins_tip,
29872988
const CBlockIndex* tip,
2988-
const CChainParams& params,
29892989
const std::string& func_name,
29902990
const std::string& prefix,
29912991
const std::string& warning_messages) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
@@ -2997,7 +2997,7 @@ static void UpdateTipLog(
29972997
tip->GetBlockHash().ToString(), tip->nHeight, tip->nVersion,
29982998
log(tip->nChainWork.getdouble()) / log(2.0), tip->m_chain_tx_count,
29992999
FormatISO8601DateTime(tip->GetBlockTime()),
3000-
GuessVerificationProgress(params.TxData(), tip),
3000+
chainman.GuessVerificationProgress(tip),
30013001
coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
30023002
coins_tip.GetCacheSize(),
30033003
!warning_messages.empty() ? strprintf(" warning='%s'", warning_messages) : "");
@@ -3008,15 +3008,13 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
30083008
AssertLockHeld(::cs_main);
30093009
const auto& coins_tip = this->CoinsTip();
30103010

3011-
const CChainParams& params{m_chainman.GetParams()};
3012-
30133011
// The remainder of the function isn't relevant if we are not acting on
30143012
// the active chainstate, so return if need be.
30153013
if (this != &m_chainman.ActiveChainstate()) {
30163014
// Only log every so often so that we don't bury log messages at the tip.
30173015
constexpr int BACKGROUND_LOG_INTERVAL = 2000;
30183016
if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
3019-
UpdateTipLog(coins_tip, pindexNew, params, __func__, "[background validation] ", "");
3017+
UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "[background validation] ", "");
30203018
}
30213019
return;
30223020
}
@@ -3031,7 +3029,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
30313029
const CBlockIndex* pindex = pindexNew;
30323030
for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
30333031
WarningBitsConditionChecker checker(m_chainman, bit);
3034-
ThresholdState state = checker.GetStateFor(pindex, params.GetConsensus(), m_chainman.m_warningcache.at(bit));
3032+
ThresholdState state = checker.GetStateFor(pindex, m_chainman.GetConsensus(), m_chainman.m_warningcache.at(bit));
30353033
if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) {
30363034
const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit);
30373035
if (state == ThresholdState::ACTIVE) {
@@ -3042,7 +3040,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
30423040
}
30433041
}
30443042
}
3045-
UpdateTipLog(coins_tip, pindexNew, params, __func__, "",
3043+
UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "",
30463044
util::Join(warning_messages, Untranslated(", ")).original);
30473045
}
30483046

@@ -4720,7 +4718,7 @@ bool Chainstate::LoadChainTip()
47204718
tip->GetBlockHash().ToString(),
47214719
m_chain.Height(),
47224720
FormatISO8601DateTime(tip->GetBlockTime()),
4723-
GuessVerificationProgress(m_chainman.GetParams().TxData(), tip));
4721+
m_chainman.GuessVerificationProgress(tip));
47244722

47254723
// Ensure KernelNotifications m_tip_block is set even if no new block arrives.
47264724
if (this->GetRole() != ChainstateRole::BACKGROUND) {
@@ -5611,9 +5609,12 @@ bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
56115609

56125610
//! Guess how far we are in the verification process at the given block index
56135611
//! require cs_main if pindex has not been validated yet (because m_chain_tx_count might be unset)
5614-
double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pindex) {
5615-
if (pindex == nullptr)
5612+
double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) const
5613+
{
5614+
const ChainTxData& data{GetParams().TxData()};
5615+
if (pindex == nullptr) {
56165616
return 0.0;
5617+
}
56175618

56185619
if (!Assume(pindex->m_chain_tx_count > 0)) {
56195620
LogWarning("Internal bug detected: block %d has unset m_chain_tx_count (%s %s). Please report this issue here: %s\n",

src/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
9595

9696
bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const bilingual_str& message);
9797

98-
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
99-
double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex* pindex);
100-
10198
/** Prune block files up to a given height */
10299
void PruneBlockFilesManual(Chainstate& active_chainstate, int nManualPruneHeight);
103100

@@ -1151,6 +1148,9 @@ class ChainstateManager
11511148
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
11521149
bool IsInitialBlockDownload() const;
11531150

1151+
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
1152+
double GuessVerificationProgress(const CBlockIndex* pindex) const;
1153+
11541154
/**
11551155
* Import blocks from an external file
11561156
*

0 commit comments

Comments
 (0)