Skip to content

Commit

Permalink
validation: change UpdateTip for multiple chainstates
Browse files Browse the repository at this point in the history
Summary:
Only perform certain behavior (namely that related to servicing
the getblocktemplate RPC call) for the active chainstate when
calling UpdateTip.

Co-authored-by: Jon Atack <jon@atack.com>

This is a backport of [[bitcoin/bitcoin#21526 | core#21526]] [3/12]
bitcoin/bitcoin@b217020
Depends on D12277

Notes:
 - the main difference from the source material is that we do not have the code handling BIP9 deployments in `UpdateTip`. As a result, there is no need for the `warning_messages` arguments in `UpdateTipLog`
 - the  `const` qualifier for the  `CblockIndex *pindexNew` argument in `UpdateTip` was missed in D1972

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12278
  • Loading branch information
jamesob authored and PiRK committed Oct 19, 2022
1 parent 7db9e19 commit a1093fa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
45 changes: 34 additions & 11 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,39 @@ void CChainState::PruneAndFlush() {
}
}

void CChainState::UpdateTip(CBlockIndex *pindexNew) {
static void UpdateTipLog(const CCoinsViewCache &coins_tip,
const CBlockIndex *tip, const CChainParams &params,
const std::string &func_name,
const std::string &prefix)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
AssertLockHeld(::cs_main);
LogPrintf("%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
"date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
prefix, func_name, tip->GetBlockHash().ToString(), tip->nHeight,
tip->nVersion, log(tip->nChainWork.getdouble()) / log(2.0),
tip->GetChainTxCount(),
FormatISO8601DateTime(tip->GetBlockTime()),
GuessVerificationProgress(params.TxData(), tip),
coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
coins_tip.GetCacheSize());
}

void CChainState::UpdateTip(const CBlockIndex *pindexNew) {
const auto &coins_tip = CoinsTip();

// 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, m_params, __func__,
"[background validation] ");
}
return;
}

// New best block
if (m_mempool) {
m_mempool->AddTransactionsUpdated(1);
Expand All @@ -2129,16 +2161,7 @@ void CChainState::UpdateTip(CBlockIndex *pindexNew) {
g_best_block_cv.notify_all();
}

LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
"date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
__func__, pindexNew->GetBlockHash().ToString(),
pindexNew->nHeight, pindexNew->nVersion,
log(pindexNew->nChainWork.getdouble()) / log(2.0),
pindexNew->GetChainTxCount(),
FormatISO8601DateTime(pindexNew->GetBlockTime()),
GuessVerificationProgress(m_params.TxData(), pindexNew),
CoinsTip().DynamicMemoryUsage() * (1.0 / (1 << 20)),
CoinsTip().GetCacheSize());
UpdateTipLog(coins_tip, pindexNew, m_params, __func__, "");
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,8 @@ class CChainState {
/**
* Check warning conditions and do some notifications on new chain tip set.
*/
void UpdateTip(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
void UpdateTip(const CBlockIndex *pindexNew)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

friend ChainstateManager;
};
Expand Down

0 comments on commit a1093fa

Please sign in to comment.