6#include <bitcoin-build-config.h>
102 "level 0 reads the blocks from disk",
103 "level 1 verifies block validity",
104 "level 2 verifies undo data",
105 "level 3 checks disconnection of tip blocks",
106 "level 4 tries to reconnect the blocks",
107 "each level includes the checks of the previous levels",
145 std::vector<CScriptCheck>* pvChecks =
nullptr)
158 const int nBlockHeight = active_chain_tip.nHeight + 1;
165 const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
167 return IsFinalTx(tx, nBlockHeight, nBlockTime);
181std::optional<std::vector<int>> CalculatePrevHeights(
186 std::vector<int> prev_heights;
187 prev_heights.resize(tx.
vin.size());
188 for (
size_t i = 0; i < tx.
vin.size(); ++i) {
189 if (
auto coin{coins.
GetCoin(tx.
vin[i].prevout)}) {
209 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
210 if (!prev_heights.has_value())
return std::nullopt;
213 next_tip.
pprev = tip;
231 int max_input_height{0};
232 for (
const int height : prev_heights.value()) {
234 if (height != next_tip.
nHeight) {
235 max_input_height = std::max(max_input_height, height);
273 int expired = pool.Expire(GetTime<std::chrono::seconds>() - pool.m_opts.expiry);
278 std::vector<COutPoint> vNoSpendsRemaining;
279 pool.TrimToSize(pool.m_opts.max_size_bytes, &vNoSpendsRemaining);
280 for (
const COutPoint& removed : vNoSpendsRemaining)
281 coins_cache.Uncache(removed);
287 if (active_chainstate.m_chainman.IsInitialBlockDownload()) {
292 if (active_chainstate.m_chain.Height() < active_chainstate.m_chainman.m_best_header->nHeight - 1) {
306 std::vector<uint256> vHashUpdate;
313 const auto queuedTx = disconnectpool.
take();
314 auto it = queuedTx.rbegin();
315 while (it != queuedTx.rend()) {
317 if (!fAddToMempool || (*it)->IsCoinBase() ||
319 true,
false).m_result_type !=
325 vHashUpdate.push_back((*it)->GetHash());
366 it->UpdateLockPoints(*new_lock_points);
373 if (it->GetSpendsCoinbase()) {
379 if (coin.IsCoinBase() && mempool_spend_height - coin.nHeight <
COINBASE_MATURITY) {
415 if (coin.
IsSpent())
return false;
445 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
446 m_active_chainstate(active_chainstate)
454 const int64_t m_accept_time;
455 const bool m_bypass_limits;
463 std::vector<COutPoint>& m_coins_to_uncache;
465 const bool m_test_accept;
469 const bool m_allow_replacement;
471 const bool m_allow_sibling_eviction;
476 const bool m_package_submission;
480 const bool m_package_feerates;
485 const std::optional<CFeeRate> m_client_maxfeerate;
488 const bool m_allow_carveouts;
491 static ATMPArgs SingleAccept(
const CChainParams& chainparams, int64_t accept_time,
492 bool bypass_limits, std::vector<COutPoint>& coins_to_uncache,
494 return ATMPArgs{ chainparams,
509 static ATMPArgs PackageTestAccept(
const CChainParams& chainparams, int64_t accept_time,
510 std::vector<COutPoint>& coins_to_uncache) {
511 return ATMPArgs{ chainparams,
526 static ATMPArgs PackageChildWithParents(
const CChainParams& chainparams, int64_t accept_time,
527 std::vector<COutPoint>& coins_to_uncache,
const std::optional<CFeeRate>& client_maxfeerate) {
528 return ATMPArgs{ chainparams,
543 static ATMPArgs SingleInPackageAccept(
const ATMPArgs& package_args) {
544 return ATMPArgs{ package_args.m_chainparams,
545 package_args.m_accept_time,
547 package_args.m_coins_to_uncache,
548 package_args.m_test_accept,
553 package_args.m_client_maxfeerate,
564 std::vector<COutPoint>& coins_to_uncache,
566 bool allow_replacement,
567 bool allow_sibling_eviction,
568 bool package_submission,
569 bool package_feerates,
570 std::optional<CFeeRate> client_maxfeerate,
571 bool allow_carveouts)
572 : m_chainparams{chainparams},
573 m_accept_time{accept_time},
574 m_bypass_limits{bypass_limits},
575 m_coins_to_uncache{coins_to_uncache},
576 m_test_accept{test_accept},
577 m_allow_replacement{allow_replacement},
578 m_allow_sibling_eviction{allow_sibling_eviction},
579 m_package_submission{package_submission},
580 m_package_feerates{package_feerates},
581 m_client_maxfeerate{client_maxfeerate},
582 m_allow_carveouts{allow_carveouts}
586 if (m_package_feerates) {
587 Assume(m_package_submission);
588 Assume(!m_allow_carveouts);
589 Assume(!m_allow_sibling_eviction);
591 if (m_allow_sibling_eviction)
Assume(m_allow_replacement);
631 explicit Workspace(
const CTransactionRef& ptx) : m_ptx(ptx), m_hash(ptx->GetHash()) {}
634 std::set<Txid> m_conflicts;
644 bool m_sibling_eviction{
false};
679 bool PackageMempoolChecks(
const std::vector<CTransactionRef>& txns,
680 std::vector<Workspace>& workspaces,
701 std::map<Wtxid, MempoolAcceptResult>& results)
709 CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
710 if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
714 if (package_fee < m_pool.m_opts.min_relay_feerate.GetFee(package_size)) {
716 strprintf(
"%d < %d", package_fee, m_pool.m_opts.min_relay_feerate.GetFee(package_size)));
723 return m_active_chainstate.m_chainman.m_validation_cache;
736 struct SubPackageState {
738 CAmount m_total_modified_fees{0};
740 int64_t m_total_vsize{0};
747 std::list<CTransactionRef> m_replaced_transactions;
749 std::unique_ptr<CTxMemPool::ChangeSet> m_changeset;
754 size_t m_conflicting_size{0};
757 struct SubPackageState m_subpackage;
762 m_subpackage = SubPackageState{};
765 CleanupTemporaryCoins();
769bool MemPoolAccept::PreChecks(ATMPArgs&
args, Workspace& ws)
775 const Txid& hash = ws.m_hash;
778 const int64_t nAcceptTime =
args.m_accept_time;
779 const bool bypass_limits =
args.m_bypass_limits;
780 std::vector<COutPoint>& coins_to_uncache =
args.m_coins_to_uncache;
795 if (m_pool.m_opts.require_standard && !
IsStandardTx(tx, m_pool.m_opts.max_datacarrier_bytes, m_pool.m_opts.permit_bare_multisig, m_pool.m_opts.dust_relay_feerate, reason)) {
823 if (ptxConflicting) {
824 if (!
args.m_allow_replacement) {
828 ws.m_conflicts.insert(ptxConflicting->
GetHash());
832 m_view.SetBackend(m_viewmempool);
838 coins_to_uncache.push_back(txin.
prevout);
844 if (!m_view.HaveCoin(txin.
prevout)) {
859 m_view.GetBestBlock();
864 m_view.SetBackend(m_dummy);
866 assert(m_active_chainstate.m_blockman.LookupBlockIndex(m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
873 const std::optional<LockPoints> lock_points{
CalculateLockPointsAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx)};
896 bool fSpendsCoinbase =
false;
898 const Coin &coin = m_view.AccessCoin(txin.
prevout);
900 fSpendsCoinbase =
true;
907 const uint64_t entry_sequence = bypass_limits ? 0 : m_pool.GetSequence();
908 if (!m_subpackage.m_changeset) {
909 m_subpackage.m_changeset = m_pool.GetChangeSet();
911 ws.m_tx_handle = m_subpackage.m_changeset->StageAddition(ptx, ws.m_base_fees, nAcceptTime, m_active_chainstate.m_chain.Height(), entry_sequence, fSpendsCoinbase, nSigOpsCost, lock_points.value());
914 ws.m_modified_fees = ws.m_tx_handle->GetModifiedFee();
916 ws.m_vsize = ws.m_tx_handle->GetTxSize();
919 if (m_pool.m_opts.require_standard) {
920 if (!
PreCheckEphemeralTx(*ptx, m_pool.m_opts.dust_relay_feerate, ws.m_base_fees, ws.m_modified_fees, state)) {
935 if (!bypass_limits && ws.m_ptx->version !=
TRUC_VERSION && ws.m_modified_fees < m_pool.m_opts.min_relay_feerate.GetFee(ws.m_vsize)) {
939 strprintf(
"%d < %d", ws.m_modified_fees, m_pool.m_opts.min_relay_feerate.GetFee(ws.m_vsize)));
944 if (!bypass_limits && !
args.m_package_feerates && !
CheckFeeRate(ws.m_vsize, ws.m_modified_fees, state))
return false;
946 ws.m_iters_conflicting = m_pool.GetIterSet(ws.m_conflicts);
953 if (ws.m_conflicts.size() == 1 &&
args.m_allow_carveouts) {
981 assert(ws.m_iters_conflicting.size() == 1);
988 if (
auto ancestors{m_subpackage.m_changeset->CalculateMemPoolAncestors(ws.m_tx_handle, maybe_rbf_limits)}) {
989 ws.m_ancestors = std::move(*ancestors);
995 if (!
args.m_allow_carveouts) {
1020 if (
auto ancestors_retry{m_subpackage.m_changeset->CalculateMemPoolAncestors(ws.m_tx_handle, cpfp_carve_out_limits)}) {
1021 ws.m_ancestors = std::move(*ancestors_retry);
1030 if (
const auto err{
SingleTRUCChecks(ws.m_ptx, ws.m_ancestors, ws.m_conflicts, ws.m_vsize)}) {
1032 if (
args.m_allow_sibling_eviction && err->second !=
nullptr) {
1038 ws.m_conflicts.insert(err->second->GetHash());
1042 ws.m_iters_conflicting.insert(m_pool.GetIter(err->second->GetHash()).value());
1043 ws.m_sibling_eviction =
true;
1064 m_subpackage.m_rbf |= !ws.m_conflicts.empty();
1068bool MemPoolAccept::ReplacementChecks(Workspace& ws)
1074 const uint256& hash = ws.m_hash;
1077 CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
1091 strprintf(
"insufficient fee%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1099 strprintf(
"too many potential replacements%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1104 Assume(!ws.m_sibling_eviction);
1106 strprintf(
"replacement-adds-unconfirmed%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1112 m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1113 m_subpackage.m_conflicting_size += it->GetTxSize();
1115 if (
const auto err_string{
PaysForRBF(m_subpackage.m_conflicting_fees, ws.m_modified_fees, ws.m_vsize,
1116 m_pool.m_opts.incremental_relay_feerate, hash)}) {
1119 strprintf(
"insufficient fee%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1123 for (
auto it : all_conflicts) {
1124 m_subpackage.m_changeset->StageRemoval(it);
1129bool MemPoolAccept::PackageMempoolChecks(
const std::vector<CTransactionRef>& txns,
1130 std::vector<Workspace>& workspaces,
1131 const int64_t total_vsize,
1138 assert(std::all_of(txns.cbegin(), txns.cend(), [
this](
const auto& tx)
1139 { return !m_pool.exists(GenTxid::Txid(tx->GetHash()));}));
1141 assert(txns.size() == workspaces.size());
1143 auto result = m_pool.CheckPackageLimits(txns, total_vsize);
1150 if (!m_subpackage.m_rbf)
return true;
1161 for (
const auto& ws : workspaces) {
1162 if (!ws.m_ancestors.empty()) {
1169 for (Workspace& ws : workspaces) {
1171 direct_conflict_iters.merge(ws.m_iters_conflicting);
1174 const auto& parent_ws = workspaces[0];
1175 const auto& child_ws = workspaces[1];
1183 "package RBF failed: too many potential replacements", *err_string);
1188 m_subpackage.m_changeset->StageRemoval(it);
1189 m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1190 m_subpackage.m_conflicting_size += it->GetTxSize();
1194 const Txid& child_hash = child_ws.m_ptx->GetHash();
1195 if (
const auto err_string{
PaysForRBF(m_subpackage.m_conflicting_fees,
1196 m_subpackage.m_total_modified_fees,
1197 m_subpackage.m_total_vsize,
1198 m_pool.m_opts.incremental_relay_feerate, child_hash)}) {
1200 "package RBF failed: insufficient anti-DoS fees", *err_string);
1205 const CFeeRate parent_feerate(parent_ws.m_modified_fees, parent_ws.m_vsize);
1206 const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1207 if (package_feerate <= parent_feerate) {
1209 "package RBF failed: package feerate is less than or equal to parent feerate",
1210 strprintf(
"package feerate %s <= parent feerate is %s", package_feerate.ToString(), parent_feerate.ToString()));
1217 "package RBF failed: " + err_tup.value().second,
"");
1220 LogDebug(
BCLog::TXPACKAGES,
"package RBF checks passed: parent %s (wtxid=%s), child %s (wtxid=%s), package hash (%s)\n",
1221 txns.front()->GetHash().ToString(), txns.front()->GetWitnessHash().ToString(),
1222 txns.back()->GetHash().ToString(), txns.back()->GetWitnessHash().ToString(),
1229bool MemPoolAccept::PolicyScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1240 if (!
CheckInputScripts(tx, state, m_view, scriptVerifyFlags,
true,
false, ws.m_precomputed_txdata, GetValidationCache())) {
1257bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1262 const uint256& hash = ws.m_hash;
1280 unsigned int currentBlockScriptVerifyFlags{
GetBlockScriptFlags(*m_active_chainstate.m_chain.Tip(), m_active_chainstate.m_chainman)};
1282 ws.m_precomputed_txdata, m_active_chainstate.CoinsTip(), GetValidationCache())) {
1283 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s\n", hash.
ToString(), state.
ToString());
1290void MemPoolAccept::FinalizeSubpackage(
const ATMPArgs&
args)
1295 if (!m_subpackage.m_changeset->GetRemovals().empty())
Assume(
args.m_allow_replacement);
1299 std::string log_string =
strprintf(
"replacing mempool tx %s (wtxid=%s, fees=%s, vsize=%s). ",
1300 it->GetTx().GetHash().ToString(),
1301 it->GetTx().GetWitnessHash().ToString(),
1304 FeeFrac feerate{m_subpackage.m_total_modified_fees, int32_t(m_subpackage.m_total_vsize)};
1306 const bool replaced_with_tx{m_subpackage.m_changeset->GetTxCount() == 1};
1307 if (replaced_with_tx) {
1308 const CTransaction& tx = m_subpackage.m_changeset->GetAddedTxn(0);
1309 tx_or_package_hash = tx.
GetHash();
1310 log_string +=
strprintf(
"New tx %s (wtxid=%s, fees=%s, vsize=%s)",
1316 tx_or_package_hash =
GetPackageHash(m_subpackage.m_changeset->GetAddedTxns());
1317 log_string +=
strprintf(
"New package %s with %lu txs, fees=%s, vsize=%s",
1318 tx_or_package_hash.ToString(),
1319 m_subpackage.m_changeset->GetTxCount(),
1326 it->GetTx().GetHash().data(),
1329 std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count(),
1330 tx_or_package_hash.data(),
1335 m_subpackage.m_replaced_transactions.push_back(it->GetSharedTx());
1337 m_subpackage.m_changeset->Apply();
1338 m_subpackage.m_changeset.reset();
1341bool MemPoolAccept::SubmitPackage(
const ATMPArgs&
args, std::vector<Workspace>& workspaces,
1343 std::map<Wtxid, MempoolAcceptResult>& results)
1349 assert(std::all_of(workspaces.cbegin(), workspaces.cend(), [
this](
const auto& ws){
1350 return !m_pool.exists(GenTxid::Txid(ws.m_ptx->GetHash())); }));
1352 bool all_submitted =
true;
1353 FinalizeSubpackage(
args);
1358 for (Workspace& ws : workspaces) {
1359 if (!ConsensusScriptChecks(
args, ws)) {
1363 all_submitted =
false;
1365 strprintf(
"BUG! PolicyScriptChecks succeeded but ConsensusScriptChecks failed: %s",
1366 ws.m_ptx->GetHash().ToString()));
1368 if (!m_subpackage.m_changeset) m_subpackage.m_changeset = m_pool.GetChangeSet();
1369 m_subpackage.m_changeset->StageRemoval(m_pool.GetIter(ws.m_ptx->GetHash()).value());
1372 if (!all_submitted) {
1373 Assume(m_subpackage.m_changeset);
1377 m_subpackage.m_changeset->Apply();
1378 m_subpackage.m_changeset.reset();
1382 std::vector<Wtxid> all_package_wtxids;
1383 all_package_wtxids.reserve(workspaces.size());
1384 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1385 [](
const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1387 if (!m_subpackage.m_replaced_transactions.empty()) {
1388 LogDebug(
BCLog::MEMPOOL,
"replaced %u mempool transactions with %u new one(s) for %s additional fees, %d delta bytes\n",
1389 m_subpackage.m_replaced_transactions.size(), workspaces.size(),
1390 m_subpackage.m_total_modified_fees - m_subpackage.m_conflicting_fees,
1391 m_subpackage.m_total_vsize -
static_cast<int>(m_subpackage.m_conflicting_size));
1395 for (Workspace& ws : workspaces) {
1396 auto iter = m_pool.GetIter(ws.m_ptx->GetHash());
1397 Assume(iter.has_value());
1398 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1399 CFeeRate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1400 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1401 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1402 results.emplace(ws.m_ptx->GetWitnessHash(),
1404 ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
1405 if (!m_pool.m_opts.signals)
continue;
1408 ws.m_vsize, (*iter)->GetHeight(),
1409 args.m_bypass_limits,
args.m_package_submission,
1411 m_pool.HasNoInputsOf(tx));
1412 m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1414 return all_submitted;
1423 const std::vector<Wtxid> single_wtxid{ws.m_ptx->GetWitnessHash()};
1425 if (!PreChecks(
args, ws)) {
1433 m_subpackage.m_total_vsize = ws.m_vsize;
1434 m_subpackage.m_total_modified_fees = ws.m_modified_fees;
1437 if (
args.m_client_maxfeerate &&
CFeeRate(ws.m_modified_fees, ws.m_vsize) >
args.m_client_maxfeerate.value()) {
1442 if (m_pool.m_opts.require_standard) {
1444 if (!
CheckEphemeralSpends({ptx}, m_pool.m_opts.dust_relay_feerate, m_pool, ws.m_state, dummy_wtxid)) {
1449 if (m_subpackage.m_rbf && !ReplacementChecks(ws)) {
1463 const CFeeRate effective_feerate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1465 if (
args.m_test_accept) {
1467 ws.m_base_fees, effective_feerate, single_wtxid);
1470 FinalizeSubpackage(
args);
1473 if (!
args.m_package_submission && !
args.m_bypass_limits) {
1482 if (m_pool.m_opts.signals) {
1484 auto iter = m_pool.GetIter(tx.
GetHash());
1485 Assume(iter.has_value());
1487 ws.m_vsize, (*iter)->GetHeight(),
1488 args.m_bypass_limits,
args.m_package_submission,
1490 m_pool.HasNoInputsOf(tx));
1491 m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1494 if (!m_subpackage.m_replaced_transactions.empty()) {
1495 LogDebug(
BCLog::MEMPOOL,
"replaced %u mempool transactions with 1 new transaction for %s additional fees, %d delta bytes\n",
1496 m_subpackage.m_replaced_transactions.size(),
1497 ws.m_modified_fees - m_subpackage.m_conflicting_fees,
1498 ws.m_vsize -
static_cast<int>(m_subpackage.m_conflicting_size));
1502 effective_feerate, single_wtxid);
1513 std::vector<Workspace> workspaces{};
1514 workspaces.reserve(txns.size());
1515 std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1516 [](
const auto& tx) { return Workspace(tx); });
1517 std::map<Wtxid, MempoolAcceptResult> results;
1522 for (Workspace& ws : workspaces) {
1523 if (!PreChecks(
args, ws)) {
1532 if (
args.m_client_maxfeerate &&
CFeeRate(ws.m_modified_fees, ws.m_vsize) >
args.m_client_maxfeerate.value()) {
1550 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1555 for (Workspace& ws : workspaces) {
1571 m_subpackage.m_total_vsize = std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1572 [](int64_t
sum,
auto& ws) { return sum + ws.m_vsize; });
1573 m_subpackage.m_total_modified_fees = std::accumulate(workspaces.cbegin(), workspaces.cend(),
CAmount{0},
1574 [](
CAmount sum,
auto& ws) { return sum + ws.m_modified_fees; });
1575 const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1576 std::vector<Wtxid> all_package_wtxids;
1577 all_package_wtxids.reserve(workspaces.size());
1578 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1579 [](
const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1581 if (
args.m_package_feerates &&
1582 !
CheckFeeRate(m_subpackage.m_total_vsize, m_subpackage.m_total_modified_fees, placeholder_state)) {
1590 if (txns.size() > 1 && !PackageMempoolChecks(txns, workspaces, m_subpackage.m_total_vsize, package_state)) {
1595 if (m_pool.m_opts.require_standard) {
1598 if (!
CheckEphemeralSpends(txns, m_pool.m_opts.dust_relay_feerate, m_pool, child_state, child_wtxid)) {
1605 for (Workspace& ws : workspaces) {
1606 ws.m_package_feerate = package_feerate;
1607 if (!PolicyScriptChecks(
args, ws)) {
1613 if (
args.m_test_accept) {
1614 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1615 CFeeRate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1616 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1617 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1618 results.emplace(ws.m_ptx->GetWitnessHash(),
1620 ws.m_vsize, ws.m_base_fees, effective_feerate,
1621 effective_feerate_wtxids));
1627 if (!SubmitPackage(
args, workspaces, package_state, results)) {
1635void MemPoolAccept::CleanupTemporaryCoins()
1655 for (
const auto& outpoint : m_viewmempool.GetNonBaseCoins()) {
1658 m_view.Uncache(outpoint);
1661 m_viewmempool.Reset();
1669 if (subpackage.size() > 1) {
1670 return AcceptMultipleTransactions(subpackage,
args);
1672 const auto& tx = subpackage.front();
1673 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(
args);
1674 const auto single_res = AcceptSingleTransaction(tx, single_args);
1685 ClearSubPackageState();
1692 Assert(!package.empty());
1708 if (package.size() > 1) {
1717 assert(package.size() > 1);
1720 const auto& child = package.back();
1721 std::unordered_set<uint256, SaltedTxidHasher> unconfirmed_parent_txids;
1722 std::transform(package.cbegin(), package.cend() - 1,
1723 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1724 [](
const auto& tx) { return tx->GetHash(); });
1731 const CCoinsViewCache& coins_tip_cache = m_active_chainstate.CoinsTip();
1732 for (
const auto& input : child->vin) {
1734 args.m_coins_to_uncache.push_back(input.prevout);
1740 m_view.SetBackend(m_active_chainstate.CoinsTip());
1741 const auto package_or_confirmed = [
this, &unconfirmed_parent_txids](
const auto& input) {
1742 return unconfirmed_parent_txids.count(input.prevout.hash) > 0 || m_view.HaveCoin(input.prevout);
1744 if (!std::all_of(child->vin.cbegin(), child->vin.cend(), package_or_confirmed)) {
1750 m_view.SetBackend(m_dummy);
1756 std::map<Wtxid, MempoolAcceptResult> results_final;
1760 std::map<Wtxid, MempoolAcceptResult> individual_results_nonfinal;
1762 bool quit_early{
false};
1763 std::vector<CTransactionRef> txns_package_eval;
1764 for (
const auto& tx : package) {
1766 const auto& txid = tx->
GetHash();
1780 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1790 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1796 const auto single_package_res = AcceptSubPackage({tx},
args);
1797 const auto& single_res = single_package_res.m_tx_results.at(wtxid);
1802 results_final.emplace(wtxid, single_res);
1803 }
else if (package.size() == 1 ||
1817 individual_results_nonfinal.emplace(wtxid, single_res);
1819 individual_results_nonfinal.emplace(wtxid, single_res);
1820 txns_package_eval.push_back(tx);
1825 auto multi_submission_result = quit_early || txns_package_eval.empty() ?
PackageMempoolAcceptResult(package_state_quit_early, {}) :
1826 AcceptSubPackage(txns_package_eval,
args);
1832 ClearSubPackageState();
1838 for (
const auto& tx : package) {
1840 if (multi_submission_result.m_tx_results.count(wtxid) > 0) {
1842 Assume(results_final.count(wtxid) == 0);
1845 const auto& txresult = multi_submission_result.m_tx_results.at(wtxid);
1852 results_final.emplace(wtxid, txresult);
1854 }
else if (
const auto it{results_final.find(wtxid)}; it != results_final.end()) {
1858 Assume(individual_results_nonfinal.count(wtxid) == 0);
1865 results_final.erase(wtxid);
1868 }
else if (
const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
1871 results_final.emplace(wtxid, it->second);
1874 Assume(results_final.size() == package.size());
1881 int64_t accept_time,
bool bypass_limits,
bool test_accept)
1888 std::vector<COutPoint> coins_to_uncache;
1889 auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept);
1897 for (
const COutPoint& hashTx : coins_to_uncache)
1900 tx->GetHash().data(),
1911 const Package& package,
bool test_accept,
const std::optional<CFeeRate>& client_maxfeerate)
1914 assert(!package.empty());
1915 assert(std::all_of(package.cbegin(), package.cend(), [](
const auto& tx){return tx != nullptr;}));
1917 std::vector<COutPoint> coins_to_uncache;
1922 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(chainparams,
GetTime(), coins_to_uncache);
1923 return MemPoolAccept(pool, active_chainstate).AcceptMultipleTransactions(package,
args);
1925 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(chainparams,
GetTime(), coins_to_uncache, client_maxfeerate);
1926 return MemPoolAccept(pool, active_chainstate).AcceptPackage(package,
args);
1931 if (test_accept || result.m_state.IsInvalid()) {
1932 for (
const COutPoint& hashTx : coins_to_uncache) {
1951 nSubsidy >>= halvings;
1956 : m_dbview{
std::move(db_params),
std::move(options)},
1957 m_catcherview(&m_dbview) {}
1959void CoinsViews::InitCache()
1962 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1969 std::optional<uint256> from_snapshot_blockhash)
1970 : m_mempool(mempool),
1971 m_blockman(blockman),
1972 m_chainman(chainman),
1973 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1979 return m_cached_snapshot_base;
1983 size_t cache_size_bytes,
1986 fs::path leveldb_name)
1995 .cache_bytes = cache_size_bytes,
1996 .memory_only = in_memory,
1997 .wipe_data = should_wipe,
2005void Chainstate::InitCoinsCache(
size_t cache_size_bytes)
2031 if (chain.Tip() ==
nullptr) {
2040 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
2057 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
2060 _(
"Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade."));
2073 SetBlockFailureFlags(pindexNew);
2078 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n", __func__,
2083 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n", __func__,
2122 if (
VerifyScript(scriptSig,
m_tx_out.
scriptPubKey, witness,
nFlags,
CachingTransactionSignatureChecker(
ptxTo,
nIn,
m_tx_out.
nValue,
cacheStore, *
m_signature_cache, *
txdata), &error)) {
2123 return std::nullopt;
2126 return std::make_pair(error, std::move(debug_str));
2131 : m_signature_cache{signature_cache_bytes}
2142 LogPrintf(
"Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements\n",
2143 approx_size_bytes >> 20, script_execution_cache_bytes >> 20, num_elems);
2169 std::vector<CScriptCheck>* pvChecks)
2174 pvChecks->reserve(tx.
vin.size());
2191 std::vector<CTxOut> spent_outputs;
2192 spent_outputs.reserve(tx.
vin.size());
2194 for (
const auto& txin : tx.
vin) {
2198 spent_outputs.emplace_back(coin.
out);
2200 txdata.
Init(tx, std::move(spent_outputs));
2204 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
2215 pvChecks->emplace_back(std::move(check));
2216 }
else if (
auto result = check(); result.has_value()) {
2228 auto mandatory_result = check2();
2229 if (!mandatory_result.has_value()) {
2238 result = mandatory_result;
2248 if (cacheFullScriptStore && !pvChecks) {
2276 if (undo.nHeight == 0) {
2282 undo.nHeight = alternate.
nHeight;
2307 LogError(
"DisconnectBlock(): failure reading undo data\n");
2311 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
2312 LogError(
"DisconnectBlock(): block and undo data inconsistent\n");
2322 bool fEnforceBIP30 = !((pindex->
nHeight==91722 && pindex->
GetBlockHash() ==
uint256{
"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
2323 (pindex->
nHeight==91812 && pindex->
GetBlockHash() ==
uint256{
"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"}));
2326 for (
int i = block.
vtx.size() - 1; i >= 0; i--) {
2330 bool is_bip30_exception = (is_coinbase && !fEnforceBIP30);
2334 for (
size_t o = 0; o < tx.
vout.size(); o++) {
2335 if (!tx.
vout[o].scriptPubKey.IsUnspendable()) {
2340 if (!is_bip30_exception) {
2351 LogError(
"DisconnectBlock(): transaction and undo data inconsistent\n");
2354 for (
unsigned int j = tx.
vin.size(); j > 0;) {
2425 const auto time_start{SteadyClock::now()};
2441 if (!
CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) {
2453 uint256 hashPrevBlock = pindex->
pprev ==
nullptr ?
uint256() : pindex->pprev->GetBlockHash();
2460 if (block_hash == params.GetConsensus().hashGenesisBlock) {
2466 bool fScriptChecks =
true;
2475 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2497 const auto time_1{SteadyClock::now()};
2498 m_chainman.time_check += time_1 - time_start;
2500 Ticks<MillisecondsDouble>(time_1 - time_start),
2542 static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2574 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->
GetBlockHash() == params.GetConsensus().BIP34Hash));
2579 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2580 for (
const auto& tx : block.
vtx) {
2581 for (
size_t o = 0; o < tx->
vout.size(); o++) {
2584 "tried to overwrite transaction");
2591 int nLockTimeFlags = 0;
2599 const auto time_2{SteadyClock::now()};
2602 Ticks<MillisecondsDouble>(time_2 - time_1),
2613 std::optional<CCheckQueueControl<CScriptCheck>> control;
2616 std::vector<PrecomputedTransactionData> txsdata(block.
vtx.size());
2618 std::vector<int> prevheights;
2621 int64_t nSigOpsCost = 0;
2622 blockundo.
vtxundo.reserve(block.
vtx.size() - 1);
2623 for (
unsigned int i = 0; i < block.
vtx.size(); i++)
2628 nInputs += tx.
vin.size();
2644 "accumulated fee in the block out of range");
2651 prevheights.resize(tx.
vin.size());
2652 for (
size_t j = 0; j < tx.
vin.size(); j++) {
2656 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2675 bool fCacheResults = fJustCheck;
2681 std::vector<CScriptCheck> vChecks;
2683 if (tx_ok) control->Add(std::move(vChecks));
2697 blockundo.
vtxundo.emplace_back();
2701 const auto time_3{SteadyClock::now()};
2703 LogDebug(
BCLog::BENCH,
" - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (
unsigned)block.
vtx.size(),
2704 Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.
vtx.size(),
2705 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2706 Ticks<SecondsDouble>(
m_chainman.time_connect),
2710 if (block.
vtx[0]->GetValueOut() > blockReward && state.
IsValid()) {
2712 strprintf(
"coinbase pays too much (actual=%d vs limit=%d)", block.
vtx[0]->GetValueOut(), blockReward));
2715 auto parallel_result = control->Complete();
2716 if (parallel_result.has_value() && state.
IsValid()) {
2724 const auto time_4{SteadyClock::now()};
2726 LogDebug(
BCLog::BENCH,
" - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
2727 Ticks<MillisecondsDouble>(time_4 - time_2),
2728 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2729 Ticks<SecondsDouble>(
m_chainman.time_verify),
2736 if (!
m_blockman.WriteBlockUndo(blockundo, state, *pindex)) {
2740 const auto time_5{SteadyClock::now()};
2743 Ticks<MillisecondsDouble>(time_5 - time_4),
2755 const auto time_6{SteadyClock::now()};
2758 Ticks<MillisecondsDouble>(time_6 - time_5),
2768 Ticks<std::chrono::nanoseconds>(time_5 - time_start)
2777 return this->GetCoinsCacheSizeState(
2783 size_t max_coins_cache_size_bytes,
2784 size_t max_mempool_size_bytes)
2789 int64_t nTotalSpace =
2790 max_coins_cache_size_bytes + std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2793 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024;
2794 int64_t large_threshold =
2795 std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2797 if (cacheSize > nTotalSpace) {
2798 LogPrintf(
"Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2800 }
else if (cacheSize > large_threshold) {
2809 int nManualPruneHeight)
2813 std::set<int> setFilesToPrune;
2814 bool full_flush_completed =
false;
2821 bool fFlushForPrune =
false;
2829 std::optional<std::string> limiting_lock;
2831 for (
const auto& prune_lock :
m_blockman.m_prune_locks) {
2832 if (prune_lock.second.height_first == std::numeric_limits<int>::max())
continue;
2835 last_prune = std::max(1, std::min(last_prune, lock_height));
2836 if (last_prune == lock_height) {
2837 limiting_lock = prune_lock.first;
2841 if (limiting_lock) {
2842 LogDebug(
BCLog::PRUNE,
"%s limited pruning to height %d\n", limiting_lock.value(), last_prune);
2845 if (nManualPruneHeight > 0) {
2850 std::min(last_prune, nManualPruneHeight),
2858 if (!setFilesToPrune.empty()) {
2859 fFlushForPrune =
true;
2861 m_blockman.m_block_tree_db->WriteFlag(
"prunedblockfiles",
true);
2874 bool should_write = (mode ==
FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicWrite || fFlushForPrune;
2901 if (fFlushForPrune) {
2907 if (!
CoinsTip().GetBestBlock().IsNull()) {
2908 if (coins_mem_usage >=
WARN_FLUSH_COINS_SIZE)
LogWarning(
"Flushing large (%d GiB) UTXO set to disk, it may take several minutes", coins_mem_usage >> 30);
2925 full_flush_completed =
true;
2927 int64_t{Ticks<std::chrono::microseconds>(
NodeClock::now() - nNow)},
2929 (uint64_t)coins_count,
2930 (uint64_t)coins_mem_usage,
2931 (
bool)fFlushForPrune);
2935 if (should_write ||
m_next_write == NodeClock::time_point::max()) {
2944 }
catch (
const std::runtime_error& e) {
2971 const std::string& func_name,
2972 const std::string&
prefix,
2977 LogPrintf(
"%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)%s\n",
2982 chainman.GuessVerificationProgress(tip),
2985 !warning_messages.empty() ?
strprintf(
" warning='%s'", warning_messages) :
"");
2988void Chainstate::UpdateTip(
const CBlockIndex* pindexNew)
2991 const auto& coins_tip = this->
CoinsTip();
2997 constexpr int BACKGROUND_LOG_INTERVAL = 2000;
2998 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
3009 std::vector<bilingual_str> warning_messages;
3012 for (
auto [bit, active] : bits) {
3017 warning_messages.push_back(warning);
3044 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3047 LogError(
"DisconnectTip(): Failed to read block\n");
3051 const auto time_start{SteadyClock::now()};
3055 if (DisconnectBlock(block, pindexDelete, view) !=
DISCONNECT_OK) {
3059 bool flushed = view.
Flush();
3063 Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
3067 const int max_height_first{pindexDelete->
nHeight - 1};
3068 for (
auto& prune_lock :
m_blockman.m_prune_locks) {
3069 if (prune_lock.second.height_first <= max_height_first)
continue;
3071 prune_lock.second.height_first = max_height_first;
3072 LogDebug(
BCLog::PRUNE,
"%s prune lock moved back to %d\n", prune_lock.first, max_height_first);
3091 UpdateTip(pindexDelete->
pprev);
3153 const auto time_1{SteadyClock::now()};
3154 std::shared_ptr<const CBlock> pthisBlock;
3156 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3160 pthisBlock = pblockNew;
3163 pthisBlock = pblock;
3165 const CBlock& blockConnecting = *pthisBlock;
3167 const auto time_2{SteadyClock::now()};
3168 SteadyClock::time_point time_3;
3172 Ticks<MillisecondsDouble>(time_2 - time_1));
3175 bool rv =
ConnectBlock(blockConnecting, state, pindexNew, view);
3185 time_3 = SteadyClock::now();
3186 m_chainman.time_connect_total += time_3 - time_2;
3189 Ticks<MillisecondsDouble>(time_3 - time_2),
3190 Ticks<SecondsDouble>(
m_chainman.time_connect_total),
3192 bool flushed = view.
Flush();
3195 const auto time_4{SteadyClock::now()};
3198 Ticks<MillisecondsDouble>(time_4 - time_3),
3205 const auto time_5{SteadyClock::now()};
3206 m_chainman.time_chainstate += time_5 - time_4;
3208 Ticks<MillisecondsDouble>(time_5 - time_4),
3209 Ticks<SecondsDouble>(
m_chainman.time_chainstate),
3218 UpdateTip(pindexNew);
3220 const auto time_6{SteadyClock::now()};
3221 m_chainman.time_post_connect += time_6 - time_5;
3224 Ticks<MillisecondsDouble>(time_6 - time_5),
3225 Ticks<SecondsDouble>(
m_chainman.time_post_connect),
3228 Ticks<MillisecondsDouble>(time_6 - time_1),
3237 m_chainman.MaybeCompleteSnapshotValidation();
3265 bool fInvalidAncestor =
false;
3275 if (fFailedChain || fMissingData) {
3282 while (pindexTest != pindexFailed) {
3286 }
else if (fMissingData) {
3291 std::make_pair(pindexFailed->
pprev, pindexFailed));
3294 pindexFailed = pindexFailed->
pprev;
3297 fInvalidAncestor =
true;
3300 pindexTest = pindexTest->
pprev;
3302 if (!fInvalidAncestor)
3334 bool fBlocksDisconnected =
false;
3348 fBlocksDisconnected =
true;
3352 std::vector<CBlockIndex*> vpindexToConnect;
3353 bool fContinue =
true;
3358 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3359 vpindexToConnect.clear();
3360 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3363 vpindexToConnect.push_back(pindexIter);
3364 pindexIter = pindexIter->
pprev;
3369 for (
CBlockIndex* pindexConnect : vpindexToConnect | std::views::reverse) {
3370 if (!
ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
3377 fInvalidFound =
true;
3398 if (fBlocksDisconnected) {
3419 bool fNotify =
false;
3420 bool fInitialBlockDownload =
false;
3424 pindexHeader = m_best_header;
3426 if (pindexHeader != m_last_notified_header) {
3429 m_last_notified_header = pindexHeader;
3442 if (signals.CallbacksPending() > 10) {
3443 signals.SyncWithValidationInterfaceQueue();
3447bool Chainstate::ActivateBestChain(
BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3466 LogPrintf(
"m_disabled is set - this chainstate should not be in operation. "
3467 "Please report this as a bug. %s\n", CLIENT_BUGREPORT);
3473 bool exited_ibd{
false};
3490 bool blocks_connected =
false;
3496 if (pindexMostWork ==
nullptr) {
3501 if (pindexMostWork ==
nullptr || pindexMostWork ==
m_chain.
Tip()) {
3505 bool fInvalidFound =
false;
3506 std::shared_ptr<const CBlock> nullBlockPtr;
3511 if (!
ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->
GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) {
3515 blocks_connected =
true;
3517 if (fInvalidFound) {
3519 pindexMostWork =
nullptr;
3524 assert(trace.pblock && trace.pindex);
3539 if (!blocks_connected)
return true;
3544 if (was_in_ibd && !still_in_ibd) {
3600 }
while (pindexNewTip != pindexMostWork);
3640 return ActivateBestChain(state, std::shared_ptr<const CBlock>());
3650 if (pindex->
nHeight == 0)
return false;
3653 bool pindex_was_in_chain =
false;
3654 int disconnected = 0;
3668 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3672 for (
auto& entry :
m_blockman.m_block_index) {
3683 candidate_blocks_by_work.insert(std::make_pair(candidate->
nChainWork, candidate));
3700 pindex_was_in_chain =
true;
3713 if (!
ret)
return false;
3733 auto candidate_it = candidate_blocks_by_work.lower_bound(invalid_walk_tip->
pprev->
nChainWork);
3734 while (candidate_it != candidate_blocks_by_work.end()) {
3737 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3745 to_mark_failed = invalid_walk_tip;
3772 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3782 if (pindex_was_in_chain) {
3801void Chainstate::SetBlockFailureFlags(
CBlockIndex* invalid_block)
3805 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3806 if (invalid_block != &block_index && block_index.GetAncestor(invalid_block->
nHeight) == invalid_block) {
3807 block_index.nStatus = (block_index.nStatus & ~BLOCK_FAILED_VALID) |
BLOCK_FAILED_CHILD;
3819 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3820 if (!block_index.IsValid() && block_index.GetAncestor(
nHeight) == pindex) {
3821 block_index.nStatus &= ~BLOCK_FAILED_MASK;
3826 if (&block_index ==
m_chainman.m_best_invalid) {
3835 while (pindex !=
nullptr) {
3837 pindex->nStatus &= ~BLOCK_FAILED_MASK;
3841 pindex = pindex->
pprev;
3855 if (is_active_chainstate) {
3859 }
else if (!m_disabled) {
3864 if (snapshot_base->GetAncestor(pindex->
nHeight) == pindex) {
3874 pindexNew->
nTx = block.
vtx.size();
3880 auto prev_tx_sum = [](
CBlockIndex& block) {
return block.nTx + (block.pprev ? block.pprev->m_chain_tx_count : 0); };
3882 pindexNew == GetSnapshotBaseBlock())) {
3883 LogWarning(
"Internal bug detected: block %d has unexpected m_chain_tx_count %i that should be %i (%s %s). Please report this issue here: %s\n",
3887 pindexNew->nFile = pos.
nFile;
3888 pindexNew->nDataPos = pos.
nPos;
3889 pindexNew->nUndoPos = 0;
3899 std::deque<CBlockIndex*> queue;
3900 queue.push_back(pindexNew);
3903 while (!queue.empty()) {
3911 LogWarning(
"Internal bug detected: block %d has unexpected m_chain_tx_count %i that should be %i (%s %s). Please report this issue here: %s\n",
3917 c->TryAddBlockIndexCandidate(pindex);
3919 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range =
m_blockman.
m_blocks_unlinked.equal_range(pindex);
3920 while (range.first != range.second) {
3921 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3922 queue.push_back(it->second);
3953 "hashMerkleRoot mismatch");
3962 "bad-txns-duplicate",
3963 "duplicate transaction");
3978 if (expect_witness_commitment) {
3983 assert(!block.
vtx.empty() && !block.
vtx[0]->vin.empty());
3984 const auto& witness_stack{block.
vtx[0]->vin[0].scriptWitness.stack};
3986 if (witness_stack.size() != 1 || witness_stack[0].size() != 32) {
3989 "bad-witness-nonce-size",
3990 strprintf(
"%s : invalid witness reserved value size", __func__));
3999 if (memcmp(hash_witness.
begin(), &block.
vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
4002 "bad-witness-merkle-match",
4003 strprintf(
"%s : witness merkle commitment mismatch", __func__));
4012 for (
const auto& tx : block.
vtx) {
4016 "unexpected-witness",
4017 strprintf(
"%s : unexpected witness data found", __func__));
4057 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase())
4059 for (
unsigned int i = 1; i < block.
vtx.size(); i++)
4060 if (block.
vtx[i]->IsCoinBase())
4065 for (
const auto& tx : block.
vtx) {
4077 unsigned int nSigOps = 0;
4078 for (
const auto& tx : block.
vtx)
4085 if (fCheckPOW && fCheckMerkleRoot)
4094 static const std::vector<unsigned char>
nonce(32, 0x00);
4097 tx.
vin[0].scriptWitness.stack.resize(1);
4098 tx.
vin[0].scriptWitness.stack[0] =
nonce;
4105 std::vector<unsigned char> commitment;
4107 std::vector<unsigned char>
ret(32, 0x00);
4115 out.scriptPubKey[1] = 0x24;
4116 out.scriptPubKey[2] = 0xaa;
4117 out.scriptPubKey[3] = 0x21;
4118 out.scriptPubKey[4] = 0xa9;
4119 out.scriptPubKey[5] = 0xed;
4120 memcpy(&
out.scriptPubKey[6], witnessroot.
begin(), 32);
4121 commitment = std::vector<unsigned char>(
out.scriptPubKey.begin(),
out.scriptPubKey.end());
4132 return std::all_of(headers.cbegin(), headers.cend(),
4133 [&](
const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
4144 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase()) {
4151 return std::any_of(block.
vtx.begin(), block.
vtx.end(),
4152 [](
auto& tx) { return GetSerializeSize(TX_NO_WITNESS(tx)) == 64; });
4193 assert(pindexPrev !=
nullptr);
4194 const int nHeight = pindexPrev->nHeight + 1;
4202 if (block.
GetBlockTime() <= pindexPrev->GetMedianTimePast())
4241 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
4244 bool enforce_locktime_median_time_past{
false};
4246 assert(pindexPrev !=
nullptr);
4247 enforce_locktime_median_time_past =
true;
4250 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
4255 for (
const auto& tx : block.
vtx) {
4265 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4266 !std::equal(
expect.begin(),
expect.end(), block.
vtx[0]->vin[0].scriptSig.begin())) {
4302 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4304 if (miSelf !=
m_blockman.m_block_index.end()) {
4328 pindexPrev = &((*mi).second);
4363 if (pindexPrev->
GetAncestor(failedit->nHeight) == failedit) {
4366 while (invalid_walk != failedit) {
4369 invalid_walk = invalid_walk->
pprev;
4377 if (!min_pow_checked) {
4412 blocks_left = std::max<int64_t>(0, blocks_left);
4413 const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};
4414 LogInfo(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
4432 if (now < m_last_presync_update + std::chrono::milliseconds{250})
return;
4433 m_last_presync_update = now;
4437 if (initial_download) {
4439 blocks_left = std::max<int64_t>(0, blocks_left);
4440 const double progress{100.0 * height / (height + blocks_left)};
4441 LogInfo(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
4448 const CBlock& block = *pblock;
4450 if (fNewBlock) *fNewBlock =
false;
4454 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
4456 bool accepted_header{
AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4459 if (!accepted_header)
4483 if (fAlreadyHave)
return true;
4485 if (pindex->
nTx != 0)
return true;
4486 if (!fHasMoreOrSameWork)
return true;
4487 if (fTooFarAhead)
return true;
4498 if (!
CheckBlock(block, state, params.GetConsensus()) ||
4515 if (fNewBlock) *fNewBlock =
true;
4523 if (blockPos.IsNull()) {
4524 state.
Error(
strprintf(
"%s: Failed to find position to write new block to disk", __func__));
4529 }
catch (
const std::runtime_error& e) {
4553 if (new_block) *new_block =
false;
4568 ret =
AcceptBlock(block, state, &pindex, force_processing,
nullptr, new_block, min_pow_checked);
4583 LogError(
"%s: ActivateBestChain failed (%s)\n", __func__, state.
ToString());
4589 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
4590 LogError(
"%s: [background] ActivateBestChain failed (%s)\n", __func__, bg_state.
ToString());
4617 bool fCheckMerkleRoot)
4624 indexDummy.
pprev = pindexPrev;
4630 LogError(
"%s: Consensus::ContextualCheckBlockHeader: %s\n", __func__, state.
ToString());
4638 LogError(
"%s: Consensus::ContextualCheckBlock: %s\n", __func__, state.
ToString());
4641 if (!chainstate.
ConnectBlock(block, state, &indexDummy, viewNew,
true)) {
4679 LogPrintf(
"Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
4709 int nCheckLevel,
int nCheckDepth)
4718 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
4721 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4722 LogPrintf(
"Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4726 int nGoodTransactions = 0;
4729 bool skipped_no_block_data{
false};
4730 bool skipped_l3_checks{
false};
4731 LogPrintf(
"Verification progress: 0%%\n");
4736 const int percentageDone = std::max(1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (
double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4737 if (reportDone < percentageDone / 10) {
4739 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
4740 reportDone = percentageDone / 10;
4749 LogPrintf(
"VerifyDB(): block verification stopping at height %d (no data). This could be due to pruning or use of an assumeutxo snapshot.\n", pindex->
nHeight);
4750 skipped_no_block_data =
true;
4760 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensus_params)) {
4761 LogPrintf(
"Verification error: found bad block at %d, hash=%s (%s)\n",
4766 if (nCheckLevel >= 2 && pindex) {
4778 if (nCheckLevel >= 3) {
4787 nGoodTransactions = 0;
4788 pindexFailure = pindex;
4790 nGoodTransactions += block.
vtx.size();
4793 skipped_l3_checks =
true;
4798 if (pindexFailure) {
4799 LogPrintf(
"Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.
m_chain.
Height() - pindexFailure->
nHeight + 1, nGoodTransactions);
4802 if (skipped_l3_checks) {
4803 LogPrintf(
"Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.\n");
4810 if (nCheckLevel >= 4 && !skipped_l3_checks) {
4812 const int percentageDone = std::max(1, std::min(99, 100 - (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (
double)nCheckDepth * 50)));
4813 if (reportDone < percentageDone / 10) {
4815 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
4816 reportDone = percentageDone / 10;
4825 if (!chainstate.
ConnectBlock(block, state, pindex, coins)) {
4833 LogPrintf(
"Verification: No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
4835 if (skipped_l3_checks) {
4838 if (skipped_no_block_data) {
4856 if (!tx->IsCoinBase()) {
4857 for (
const CTxIn &txin : tx->vin) {
4875 if (hashHeads.empty())
return true;
4876 if (hashHeads.size() != 2) {
4877 LogError(
"ReplayBlocks(): unknown inconsistent state\n");
4888 if (
m_blockman.m_block_index.count(hashHeads[0]) == 0) {
4889 LogError(
"ReplayBlocks(): reorganization to unknown block requested\n");
4892 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
4894 if (!hashHeads[1].IsNull()) {
4895 if (
m_blockman.m_block_index.count(hashHeads[1]) == 0) {
4896 LogError(
"ReplayBlocks(): reorganization from unknown block requested\n");
4899 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
4901 assert(pindexFork !=
nullptr);
4905 while (pindexOld != pindexFork) {
4923 pindexOld = pindexOld->
pprev;
4927 int nForkHeight = pindexFork ? pindexFork->
nHeight : 0;
4954 block = block->pprev;
4960void Chainstate::ClearBlockIndexCandidates()
4972 if (!
ret)
return false;
4974 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
4976 std::vector<CBlockIndex*> vSortedByHeight{
m_blockman.GetAllBlockIndices()};
4977 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4987 if (pindex == GetSnapshotBaseBlock() ||
4992 chainstate->TryAddBlockIndexCandidate(pindex);
4996 m_best_invalid = pindex;
4999 m_best_header = pindex;
5015 if (
m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
5019 const CBlock& block = params.GenesisBlock();
5021 if (blockPos.IsNull()) {
5022 LogError(
"%s: writing genesis block to disk failed\n", __func__);
5027 }
catch (
const std::runtime_error& e) {
5028 LogError(
"%s: failed to write genesis block: %s\n", __func__, e.what());
5038 std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
5041 assert(!dbp == !blocks_with_unknown_parent);
5043 const auto start{SteadyClock::now()};
5051 uint64_t nRewind = blkdat.GetPos();
5052 while (!blkdat.eof()) {
5055 blkdat.SetPos(nRewind);
5058 unsigned int nSize = 0;
5062 blkdat.FindByte(std::byte(params.MessageStart()[0]));
5063 nRewind = blkdat.GetPos() + 1;
5065 if (buf != params.MessageStart()) {
5072 }
catch (
const std::exception&) {
5079 const uint64_t nBlockPos{blkdat.GetPos()};
5081 dbp->
nPos = nBlockPos;
5082 blkdat.SetLimit(nBlockPos + nSize);
5088 nRewind = nBlockPos + nSize;
5089 blkdat.SkipTo(nRewind);
5091 std::shared_ptr<CBlock> pblock{};
5099 if (dbp && blocks_with_unknown_parent) {
5100 blocks_with_unknown_parent->emplace(header.
hashPrevBlock, *dbp);
5109 blkdat.SetPos(nBlockPos);
5110 pblock = std::make_shared<CBlock>();
5112 nRewind = blkdat.GetPos();
5115 if (
AcceptBlock(pblock, state,
nullptr,
true, dbp,
nullptr,
true)) {
5121 }
else if (hash != params.GetConsensus().hashGenesisBlock && pindex->
nHeight % 1000 == 0) {
5148 bool activation_failure =
false;
5149 for (
auto c :
GetAll()) {
5151 if (!c->ActivateBestChain(state, pblock)) {
5153 activation_failure =
true;
5157 if (activation_failure) {
5164 if (!blocks_with_unknown_parent)
continue;
5167 std::deque<uint256> queue;
5168 queue.push_back(hash);
5169 while (!queue.empty()) {
5172 auto range = blocks_with_unknown_parent->equal_range(head);
5173 while (range.first != range.second) {
5174 std::multimap<uint256, FlatFilePos>::iterator it = range.first;
5175 std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5177 LogDebug(
BCLog::REINDEX,
"%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
5181 if (
AcceptBlock(pblockrecursive, dummy,
nullptr,
true, &it->second,
nullptr,
true)) {
5183 queue.push_back(pblockrecursive->GetHash());
5187 blocks_with_unknown_parent->erase(it);
5191 }
catch (
const std::exception& e) {
5203 LogDebug(
BCLog::REINDEX,
"%s: unexpected data at file offset 0x%x - %s. continuing\n", __func__, (nRewind - 1), e.what());
5206 }
catch (
const std::runtime_error& e) {
5209 LogPrintf(
"Loaded %i blocks from external file in %dms\n", nLoaded, Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
5243 best_hdr_chain.
SetTip(*m_best_header);
5245 std::multimap<CBlockIndex*,CBlockIndex*> forward;
5246 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
5248 if (!best_hdr_chain.
Contains(&block_index)) {
5250 assert(block_index.pprev);
5251 forward.emplace(block_index.pprev, &block_index);
5267 CBlockIndex* pindexFirstNotTransactionsValid =
nullptr;
5269 CBlockIndex* pindexFirstNotScriptsValid =
nullptr;
5275 const CBlockIndex* snap_base{GetSnapshotBaseBlock()};
5276 CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{}, *snap_first_nocv{}, *snap_first_nosv{};
5277 auto snap_update_firsts = [&] {
5278 if (pindex == snap_base) {
5279 std::swap(snap_first_missing, pindexFirstMissing);
5280 std::swap(snap_first_notx, pindexFirstNeverProcessed);
5281 std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
5282 std::swap(snap_first_nocv, pindexFirstNotChainValid);
5283 std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
5287 while (pindex !=
nullptr) {
5289 if (pindexFirstInvalid ==
nullptr && pindex->nStatus &
BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
5290 if (pindexFirstMissing ==
nullptr && !(pindex->nStatus &
BLOCK_HAVE_DATA)) {
5291 pindexFirstMissing = pindex;
5293 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) pindexFirstNeverProcessed = pindex;
5296 if (pindex->
pprev !=
nullptr) {
5297 if (pindexFirstNotTransactionsValid ==
nullptr &&
5299 pindexFirstNotTransactionsValid = pindex;
5302 if (pindexFirstNotChainValid ==
nullptr &&
5304 pindexFirstNotChainValid = pindex;
5307 if (pindexFirstNotScriptsValid ==
nullptr &&
5309 pindexFirstNotScriptsValid = pindex;
5314 if (pindex->
pprev ==
nullptr) {
5317 for (
auto c :
GetAll()) {
5318 if (c->m_chain.Genesis() !=
nullptr) {
5319 assert(pindex == c->m_chain.Genesis());
5329 assert(pindexFirstMissing == pindexFirstNeverProcessed);
5335 if (snap_base && snap_base->GetAncestor(pindex->
nHeight) == pindex) {
5345 assert((pindexFirstNotTransactionsValid ==
nullptr || pindex == snap_base) == pindex->
HaveNumChainTxs());
5349 assert(pindexFirstNotTreeValid ==
nullptr);
5353 if (pindexFirstInvalid ==
nullptr) {
5358 if (!pindex->
pprev) {
5371 for (
auto c :
GetAll()) {
5372 if (c->m_chain.Tip() ==
nullptr)
continue;
5386 if (!
CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && (pindexFirstNeverProcessed ==
nullptr || pindex == snap_base)) {
5390 if (pindexFirstInvalid ==
nullptr) {
5409 if (pindexFirstMissing ==
nullptr || pindex == c->m_chain.Tip() || pindex == c->SnapshotBase()) {
5416 assert(c->setBlockIndexCandidates.count(pindex));
5424 assert(c->setBlockIndexCandidates.count(pindex) == 0);
5428 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked =
m_blockman.
m_blocks_unlinked.equal_range(pindex->
pprev);
5429 bool foundInUnlinked =
false;
5430 while (rangeUnlinked.first != rangeUnlinked.second) {
5431 assert(rangeUnlinked.first->first == pindex->
pprev);
5432 if (rangeUnlinked.first->second == pindex) {
5433 foundInUnlinked =
true;
5436 rangeUnlinked.first++;
5438 if (pindex->
pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed !=
nullptr && pindexFirstInvalid ==
nullptr) {
5443 if (pindexFirstMissing ==
nullptr)
assert(!foundInUnlinked);
5444 if (pindex->
pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed ==
nullptr && pindexFirstMissing !=
nullptr) {
5455 for (
auto c :
GetAll()) {
5458 if (pindexFirstInvalid ==
nullptr) {
5459 if (is_active || snap_base->GetAncestor(pindex->
nHeight) == pindex) {
5471 snap_update_firsts();
5472 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
5473 if (range.first != range.second) {
5475 pindex = range.first->second;
5478 }
else if (best_hdr_chain.
Contains(pindex)) {
5481 pindex = best_hdr_chain[
nHeight];
5489 snap_update_firsts();
5491 if (pindex == pindexFirstInvalid) pindexFirstInvalid =
nullptr;
5492 if (pindex == pindexFirstMissing) pindexFirstMissing =
nullptr;
5493 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed =
nullptr;
5494 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid =
nullptr;
5495 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid =
nullptr;
5496 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid =
nullptr;
5497 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid =
nullptr;
5501 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
5502 while (rangePar.first->second != pindex) {
5503 assert(rangePar.first != rangePar.second);
5508 if (rangePar.first != rangePar.second) {
5510 pindex = rangePar.first->second;
5512 }
else if (pindexPar == best_hdr_chain[
nHeight - 1]) {
5514 pindex = best_hdr_chain[
nHeight];
5516 assert((pindex ==
nullptr) == (pindexPar == best_hdr_chain.
Tip()));
5528 assert(nNodes == forward.size() + best_hdr_chain.
Height() + 1);
5535 return strprintf(
"Chainstate [%s] @ height %d (%s)",
5540bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size)
5553 LogPrintf(
"[%s] resized coinsdb cache to %.1f MiB\n",
5554 this->
ToString(), coinsdb_size * (1.0 / 1024 / 1024));
5555 LogPrintf(
"[%s] resized coinstip cache to %.1f MiB\n",
5556 this->
ToString(), coinstip_size * (1.0 / 1024 / 1024));
5561 if (coinstip_size > old_coinstip_size) {
5576 if (pindex ==
nullptr) {
5585 int64_t nNow = time(
nullptr);
5590 fTxTotal =
data.tx_count + (nNow -
data.nTime) *
data.dTxRate;
5601 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
5603 return m_active_chainstate->m_from_snapshot_blockhash;
5605 return std::nullopt;
5611 std::vector<Chainstate*>
out;
5613 for (
Chainstate*
cs : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
5623 assert(!m_ibd_chainstate);
5624 assert(!m_active_chainstate);
5626 m_ibd_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman, *
this);
5627 m_active_chainstate = m_ibd_chainstate.get();
5628 return *m_active_chainstate;
5640 bool existed = fs::remove(base_blockhash_path);
5642 LogPrintf(
"[snapshot] snapshot chainstate dir being removed lacks %s file\n",
5645 }
catch (
const fs::filesystem_error& e) {
5646 LogWarning(
"[snapshot] failed to remove file %s: %s\n",
5652 LogPrintf(
"Removing leveldb dir at %s\n", path_str);
5656 const bool destroyed =
DestroyDB(path_str);
5659 LogPrintf(
"error: leveldb DestroyDB call failed on %s\n", path_str);
5687 if (!
GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
5689 std::string heights_formatted =
util::Join(available_heights,
", ", [&](
const auto& i) {
return util::ToString(i); });
5690 return util::Error{
Untranslated(
strprintf(
"assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s",
5692 heights_formatted))};
5696 if (!snapshot_start_block) {
5697 return util::Error{
Untranslated(
strprintf(
"The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again",
5702 if (start_block_invalid) {
5706 if (!m_best_header || m_best_header->GetAncestor(snapshot_start_block->nHeight) != snapshot_start_block) {
5707 return util::Error{
Untranslated(
"A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo.")};
5710 auto mempool{m_active_chainstate->GetMempool()};
5711 if (mempool && mempool->
size() > 0) {
5716 int64_t current_coinsdb_cache_size{0};
5717 int64_t current_coinstip_cache_size{0};
5725 static constexpr double IBD_CACHE_PERC = 0.01;
5726 static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
5738 current_coinsdb_cache_size = this->
ActiveChainstate().m_coinsdb_cache_size_bytes;
5739 current_coinstip_cache_size = this->
ActiveChainstate().m_coinstip_cache_size_bytes;
5744 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
5745 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
5749 return std::make_unique<Chainstate>(
5750 nullptr,
m_blockman, *
this, base_blockhash));
5754 snapshot_chainstate->InitCoinsDB(
5755 static_cast<size_t>(current_coinsdb_cache_size * SNAPSHOT_CACHE_PERC),
5756 in_memory,
false,
"chainstate");
5757 snapshot_chainstate->InitCoinsCache(
5758 static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
5762 this->MaybeRebalanceCaches();
5770 snapshot_chainstate.reset();
5774 "Manually remove it before restarting.\n"),
fs::PathToString(*snapshot_datadir)));
5791 return cleanup_bad_snapshot(
Untranslated(
"work does not exceed active chainstate"));
5797 return cleanup_bad_snapshot(
Untranslated(
"could not write base blockhash"));
5801 assert(!m_snapshot_chainstate);
5802 m_snapshot_chainstate.swap(snapshot_chainstate);
5803 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
5808 Assert(m_active_chainstate->m_mempool->size() == 0);
5809 Assert(!m_snapshot_chainstate->m_mempool);
5810 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5811 m_active_chainstate->m_mempool =
nullptr;
5812 m_active_chainstate = m_snapshot_chainstate.get();
5815 LogPrintf(
"[snapshot] successfully activated snapshot %s\n", base_blockhash.
ToString());
5817 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
5819 this->MaybeRebalanceCaches();
5820 return snapshot_start_block;
5827 snapshot_loaded ?
"saving snapshot chainstate" :
"flushing coins cache",
5831 coins_cache.
Flush();
5836 const char*
what() const noexcept
override
5838 return "ComputeUTXOStats interrupted.";
5860 if (!snapshot_start_block) {
5867 int base_height = snapshot_start_block->
nHeight;
5870 if (!maybe_au_data) {
5872 "(%d) - refusing to load snapshot", base_height))};
5887 LogPrintf(
"[snapshot] loading %d coins from snapshot %s\n", coins_left, base_blockhash.
ToString());
5888 int64_t coins_processed{0};
5890 while (coins_left > 0) {
5894 size_t coins_per_txid{0};
5897 if (coins_per_txid > coins_left) {
5901 for (
size_t i = 0; i < coins_per_txid; i++) {
5905 outpoint.
hash = txid;
5907 if (coin.
nHeight > base_height ||
5908 outpoint.
n >= std::numeric_limits<
decltype(outpoint.
n)>::max()
5911 coins_count - coins_left))};
5915 coins_count - coins_left))};
5922 if (coins_processed % 1000000 == 0) {
5923 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
5925 static_cast<float>(coins_processed) * 100 /
static_cast<float>(coins_count),
5933 if (coins_processed % 120000 == 0) {
5939 return snapshot_chainstate.GetCoinsCacheSizeState());
5952 }
catch (
const std::ios_base::failure&) {
5965 bool out_of_coins{
false};
5967 std::byte left_over_byte;
5968 coins_file >> left_over_byte;
5969 }
catch (
const std::ios_base::failure&) {
5971 out_of_coins =
true;
5973 if (!out_of_coins) {
5978 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
5992 std::optional<CCoinsStats> maybe_stats;
6000 if (!maybe_stats.has_value()) {
6021 constexpr int AFTER_GENESIS_START{1};
6023 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height(); ++i) {
6024 index = snapshot_chainstate.
m_chain[i];
6041 assert(index == snapshot_start_block);
6045 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
6067 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
6068 !this->IsUsable(m_snapshot_chainstate.get()) ||
6069 !this->IsUsable(m_ibd_chainstate.get()) ||
6070 !m_ibd_chainstate->m_chain.Tip()) {
6076 const int snapshot_base_height = *
Assert(this->GetSnapshotBaseHeight());
6079 if (index_new.
nHeight < snapshot_base_height) {
6089 "%s failed to validate the -assumeutxo snapshot state. "
6090 "This indicates a hardware problem, or a bug in the software, or a "
6091 "bad software modification that allowed an invalid snapshot to be "
6092 "loaded. As a result of this, the node will shut down and stop using any "
6093 "state that was built on the snapshot, resetting the chain height "
6094 "from %d to %d. On the next "
6095 "restart, the node will resume syncing from %d "
6096 "without using any snapshot data. "
6097 "Please report this incident to %s, including how you obtained the snapshot. "
6098 "The invalid snapshot chainstate will be left on disk in case it is "
6099 "helpful in diagnosing the issue that caused this error."),
6100 CLIENT_NAME, snapshot_tip_height, snapshot_base_height, snapshot_base_height, CLIENT_BUGREPORT
6104 LogError(
"[snapshot] deleting snapshot, reverting to validated chain, and stopping node\n");
6106 m_active_chainstate = m_ibd_chainstate.get();
6107 m_snapshot_chainstate->m_disabled =
true;
6111 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
6112 if (!rename_result) {
6120 LogPrintf(
"[snapshot] supposed base block %s does not match the "
6121 "snapshot base block %s (height %d). Snapshot is not valid.\n",
6122 index_new.
ToString(), snapshot_blockhash.
ToString(), snapshot_base_height);
6123 handle_invalid_snapshot();
6129 int curr_height = m_ibd_chainstate->m_chain.Height();
6131 assert(snapshot_base_height == curr_height);
6136 CCoinsViewDB& ibd_coins_db = m_ibd_chainstate->CoinsDB();
6137 m_ibd_chainstate->ForceFlushStateToDisk();
6140 if (!maybe_au_data) {
6141 LogPrintf(
"[snapshot] assumeutxo data not found for height "
6142 "(%d) - refusing to validate snapshot\n", curr_height);
6143 handle_invalid_snapshot();
6148 std::optional<CCoinsStats> maybe_ibd_stats;
6149 LogPrintf(
"[snapshot] computing UTXO stats for background chainstate to validate "
6150 "snapshot - this could take a few minutes\n");
6153 CoinStatsHashType::HASH_SERIALIZED,
6162 if (!maybe_ibd_stats) {
6163 LogPrintf(
"[snapshot] failed to generate stats for validation coins db\n");
6167 handle_invalid_snapshot();
6170 const auto& ibd_stats = *maybe_ibd_stats;
6179 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
6180 ibd_stats.hashSerialized.ToString(),
6182 handle_invalid_snapshot();
6186 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
6189 m_ibd_chainstate->m_disabled =
true;
6190 this->MaybeRebalanceCaches();
6198 assert(m_active_chainstate);
6199 return *m_active_chainstate;
6205 return m_snapshot_chainstate && m_active_chainstate == m_snapshot_chainstate.get();
6208void ChainstateManager::MaybeRebalanceCaches()
6211 bool ibd_usable = this->
IsUsable(m_ibd_chainstate.get());
6212 bool snapshot_usable = this->
IsUsable(m_snapshot_chainstate.get());
6213 assert(ibd_usable || snapshot_usable);
6215 if (ibd_usable && !snapshot_usable) {
6220 else if (snapshot_usable && !ibd_usable) {
6222 LogPrintf(
"[snapshot] allocating all cache to the snapshot chainstate\n");
6226 else if (ibd_usable && snapshot_usable) {
6231 m_ibd_chainstate->ResizeCoinsCaches(
6233 m_snapshot_chainstate->ResizeCoinsCaches(
6236 m_snapshot_chainstate->ResizeCoinsCaches(
6238 m_ibd_chainstate->ResizeCoinsCaches(
6244void ChainstateManager::ResetChainstates()
6246 m_ibd_chainstate.reset();
6247 m_snapshot_chainstate.reset();
6248 m_active_chainstate =
nullptr;
6258 if (!opts.check_block_index.has_value()) opts.
check_block_index = opts.chainparams.DefaultConsistencyChecks();
6259 if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work =
UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork);
6260 if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
6261 return std::move(opts);
6266 m_interrupt{interrupt},
6268 m_blockman{interrupt,
std::move(blockman_options)},
6269 m_validation_cache{m_options.script_execution_cache_bytes, m_options.signature_cache_bytes}
6280bool ChainstateManager::DetectSnapshotChainstate()
6282 assert(!m_snapshot_chainstate);
6288 if (!base_blockhash) {
6291 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
6292 fs::PathToString(*path));
6294 this->ActivateExistingSnapshot(*base_blockhash);
6300 assert(!m_snapshot_chainstate);
6301 m_snapshot_chainstate =
6302 std::make_unique<Chainstate>(
nullptr,
m_blockman, *
this, base_blockhash);
6303 LogPrintf(
"[snapshot] switching active chainstate to %s\n", m_snapshot_chainstate->ToString());
6306 Assert(m_active_chainstate->m_mempool->size() == 0);
6307 Assert(!m_snapshot_chainstate->m_mempool);
6308 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
6309 m_active_chainstate->m_mempool =
nullptr;
6310 m_active_chainstate = m_snapshot_chainstate.get();
6311 return *m_snapshot_chainstate;
6316 return (block_index.
nHeight==91842 && block_index.
GetBlockHash() ==
uint256{
"00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec"}) ||
6317 (block_index.
nHeight==91880 && block_index.
GetBlockHash() ==
uint256{
"00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721"});
6322 return (block_index.
nHeight==91722 && block_index.
GetBlockHash() ==
uint256{
"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
6323 (block_index.
nHeight==91812 && block_index.
GetBlockHash() ==
uint256{
"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"});
6330 assert(
cs.m_from_snapshot_blockhash);
6331 auto storage_path_maybe =
cs.CoinsDB().StoragePath();
6333 assert(storage_path_maybe);
6334 return *storage_path_maybe;
6344 auto invalid_path = snapshot_datadir +
"_INVALID";
6347 LogPrintf(
"[snapshot] renaming snapshot datadir %s to %s\n", dbpath, target);
6353 fs::rename(snapshot_datadir, invalid_path);
6354 }
catch (
const fs::filesystem_error& e) {
6358 LogPrintf(
"%s: error renaming file '%s' -> '%s': %s\n",
6359 __func__, src_str, dest_str, e.what());
6361 "Rename of '%s' -> '%s' failed. "
6362 "You should resolve this by manually moving or deleting the invalid "
6363 "snapshot directory %s, otherwise you will encounter the same error again "
6364 "on the next startup."),
6365 src_str, dest_str, src_str)};
6370bool ChainstateManager::DeleteSnapshotChainstate()
6373 Assert(m_snapshot_chainstate);
6374 Assert(m_ibd_chainstate);
6378 LogPrintf(
"Deletion of %s failed. Please remove it manually to continue reindexing.\n",
6379 fs::PathToString(snapshot_datadir));
6382 m_active_chainstate = m_ibd_chainstate.get();
6383 m_active_chainstate->m_mempool = m_snapshot_chainstate->m_mempool;
6384 m_snapshot_chainstate.reset();
6398const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock()
const
6400 return m_active_chainstate ? m_active_chainstate->SnapshotBase() :
nullptr;
6403std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const
6405 const CBlockIndex* base = this->GetSnapshotBaseBlock();
6406 return base ? std::make_optional(base->
nHeight) :
std::nullopt;
6409void ChainstateManager::RecalculateBestHeader()
6413 for (
auto& entry :
m_blockman.m_block_index) {
6414 if (!(entry.second.nStatus &
BLOCK_FAILED_MASK) && m_best_header->nChainWork < entry.second.nChainWork) {
6415 m_best_header = &entry.second;
6420bool ChainstateManager::ValidatedSnapshotCleanup()
6424 if (!(chainstate && chainstate->HasCoinsViews())) {
6427 return chainstate->CoinsDB().StoragePath();
6429 std::optional<fs::path> ibd_chainstate_path_maybe = get_storage_path(m_ibd_chainstate);
6430 std::optional<fs::path> snapshot_chainstate_path_maybe = get_storage_path(m_snapshot_chainstate);
6439 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
6440 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with "
6441 "in-memory chainstates. You are testing, right?\n");
6445 const auto& snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
6446 const auto& ibd_chainstate_path = *ibd_chainstate_path_maybe;
6454 this->ResetChainstates();
6459 LogPrintf(
"[snapshot] deleting background chainstate directory (now unnecessary) (%s)\n",
6460 fs::PathToString(ibd_chainstate_path));
6462 fs::path tmp_old{ibd_chainstate_path +
"_todelete"};
6464 auto rename_failed_abort = [
this](
6467 const fs::filesystem_error& err) {
6468 LogError(
"[snapshot] Error renaming path (%s) -> (%s): %s\n",
6469 fs::PathToString(p_old), fs::PathToString(p_new), err.what());
6471 "Rename of '%s' -> '%s' failed. "
6472 "Cannot clean up the background chainstate leveldb directory."),
6473 fs::PathToString(p_old), fs::PathToString(p_new)));
6477 fs::rename(ibd_chainstate_path, tmp_old);
6478 }
catch (
const fs::filesystem_error& e) {
6479 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
6483 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to "
6484 "default chainstate directory (%s)\n",
6485 fs::PathToString(snapshot_chainstate_path), fs::PathToString(ibd_chainstate_path));
6488 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
6489 }
catch (
const fs::filesystem_error& e) {
6490 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
6497 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the "
6498 "directory is now unnecessary.\n",
6499 fs::PathToString(tmp_old));
6501 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
6502 fs::PathToString(ibd_chainstate_path));
6507Chainstate& ChainstateManager::GetChainstateForIndexing()
6512 return (this->
GetAll().size() > 1) ? *m_ibd_chainstate : *m_active_chainstate;
6515std::pair<int, int> ChainstateManager::GetPruneRange(
const Chainstate& chainstate,
int last_height_can_prune)
6522 if (this->
GetAll().size() > 1 && m_snapshot_chainstate.get() == &chainstate) {
6525 prune_start = *
Assert(GetSnapshotBaseHeight()) + 1;
6528 int max_prune = std::max<int>(
6537 int prune_end = std::min(last_height_can_prune, max_prune);
6539 return {prune_start, prune_end};
bool MoneyRange(const CAmount &nValue)
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
arith_uint256 UintToArith256(const uint256 &a)
void InvalidateBlock(ChainstateManager &chainman, const uint256 block_hash)
arith_uint256 GetBlockProof(const CBlockIndex &block)
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
Return the time it would take to redo the work difference between from and to, assuming the current h...
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
@ BLOCK_VALID_CHAIN
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends,...
@ BLOCK_VALID_MASK
All validity bits.
@ BLOCK_VALID_TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
@ BLOCK_VALID_SCRIPTS
Scripts & signatures ok.
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous.
@ BLOCK_HAVE_UNDO
undo data available in rev*.dat
@ BLOCK_HAVE_DATA
full block available in blk*.dat
@ BLOCK_FAILED_CHILD
descends from failed block
@ BLOCK_FAILED_VALID
stage after last reached validness failed
@ BLOCK_OPT_WITNESS
block data in blk*.dat was received with a witness-enforcing client
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
Non-refcounted RAII wrapper for FILE*.
std::string ToString() const
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
bool m_checked_merkle_root
std::vector< CTransactionRef > vtx
bool m_checked_witness_commitment
The block chain is a tree shaped structure starting with the genesis block at the root,...
std::string ToString() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block.
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
bool HaveNumChainTxs() const
Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot ...
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
uint256 GetBlockHash() const
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
bool RaiseValidity(enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
unsigned int nTx
Number of transactions in this block.
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
int32_t nVersion
block header
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
int nHeight
height of the entry in the chain. The genesis block has height 0
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
An in-memory indexed chain of blocks.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
int Height() const
Return the maximal height in the chain.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip in of this chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
std::vector< int > GetAvailableSnapshotHeights() const
const ChainTxData & TxData() const
const Consensus::Params & GetConsensus() const
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
CCoinsView that adds a memory cache for transactions to another CCoinsView.
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SetBestBlock(const uint256 &hashBlock)
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
bool Flush()
Push the modifications applied to this cache to its base and wipe local state.
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
bool Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
CCoinsView backed by the coin database (chainstate/)
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Abstract view on the open txout dataset.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
CCoinsView that brings transactions from a mempool into view.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
void Finalize(std::span< unsigned char > output)
CHash256 & Write(std::span< const unsigned char > input)
An outpoint - a combination of a transaction hash and an index n into its vout.
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
Closure representing one script verification Note that this stores references to the spending transac...
SignatureCache * m_signature_cache
PrecomputedTransactionData * txdata
std::optional< std::pair< ScriptError, std::string > > operator()()
const CTransaction * ptxTo
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
const Txid & GetHash() const LIFETIMEBOUND
const std::vector< CTxIn > vin
An input of a transaction.
CTxMemPool::txiter TxHandle
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
void AddTransactionsUpdated(unsigned int n)
CTransactionRef get(const uint256 &hash) const
size_t DynamicMemoryUsage() const
void removeForReorg(CChain &chain, std::function< bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs
After reorg, filter the entries that would no longer be valid in the next block, and update the entri...
std::set< txiter, CompareIteratorByHash > setEntries
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
bool exists(const GenTxid >xid) const
unsigned long size() const
An output of a transaction.
Undo information for a CTransaction.
std::vector< Coin > vprevout
VerifyDBResult VerifyDB(Chainstate &chainstate, const Consensus::Params &consensus_params, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::Notifications & m_notifications
CVerifyDB(kernel::Notifications ¬ifications)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(
Initialize the in-memory coins cache (to be done after the health of the on-disk database is verified...
Mutex m_chainstate_mutex
The ChainState Mutex A lock that must be held when modifying this ChainState - held in ActivateBestCh...
CChain m_chain
The current chain of blockheaders we consult and build on.
CTxMemPool * GetMempool()
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
bool ConnectTip(BlockValidationState &state, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, ConnectTrace &connectTrace, DisconnectedBlockTransactions &disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(NodeClock::time_poin m_next_write)
Check warning conditions and do some notifications on new chain tip set.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
bool PreciousBlock(BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(void SetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(voi ResetBlockFailureFlags)(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as precious and reorganize.
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) LOCKS_EXCLUDED(DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo ConnectBlock)(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the best known block, and make it the tip of the block chain.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
CBlockIndex * FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Return the tip of the chain with the most work in it, that isn't known to be invalid (it's however fa...
ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate")
Return the current role of the chainstate.
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
void TryAddBlockIndexCandidate(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) EXCLUSIVE_LOCKS_REQUIRED(bool FlushStateToDisk(BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
Resize the CoinsViews caches dynamically and flush state to disk.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
void MaybeUpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transac...
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, ConnectTrace &connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< uint256 > from_snapshot_blockhash=std::nullopt)
bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Whether the chain state needs to be redownloaded due to lack of witness data.
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(RecursiveMutex * MempoolMutex() const LOCK_RETURNED(m_mempool -> cs)
Dictates whether we need to flush the cache to disk or not.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
util::Result< void > PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
const uint256 & AssumedValidBlock() const
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
ValidationCache m_validation_cache
double GuessVerificationProgress(const CBlockIndex *pindex) const
Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).
size_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev) const
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
kernel::Notifications & GetNotifications() const
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
bool ShouldCheckBlockIndex() const
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &block, bool force_processing, bool min_pow_checked, bool *new_block) LOCKS_EXCLUDED(cs_main)
Process an incoming block.
bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
The state of a background sync (for net processing)
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
std::atomic< bool > m_cached_finished_ibd
Whether initial block download has ended and IsInitialBlockDownload should return false from now on.
const util::SignalInterrupt & m_interrupt
void LoadExternalBlockFile(AutoFile &file_in, FlatFilePos *dbp=nullptr, std::multimap< uint256, FlatFilePos > *blocks_with_unknown_parent=nullptr)
Import blocks from an external file.
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
VersionBitsCache m_versionbitscache
Track versionbit status.
bool IsSnapshotActive() const
std::function< void()> snapshot_download_completed
Function to restart active indexes; set dynamically to avoid a circular dependency on base/index....
const CChainParams & GetParams() const
bool ProcessNewBlockHeaders(std::span< const CBlockHeader > headers, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex=nullptr) LOCKS_EXCLUDED(cs_main)
Process incoming block headers.
const Consensus::Params & GetConsensus() const
ChainstateManager(const util::SignalInterrupt &interrupt, Options options, node::BlockManager::Options blockman_options)
const arith_uint256 & MinimumChainWork() const
void CheckBlockIndex()
Make various assertions about the state of the block index.
util::Result< CBlockIndex * > ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
std::optional< uint256 > SnapshotBlockhash() const
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't desce...
void ReportHeadersPresync(const arith_uint256 &work, int64_t height, int64_t timestamp)
This is used by net_processing to report pre-synchronization progress of headers, as headers are not ...
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex())
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(void UpdateUncommittedBlockStructures(CBlock &block, const CBlockIndex *pindexPrev) const
Check to see if caches are out of balance and if so, call ResizeCoinsCaches() as needed.
bool DetectSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &ActivateExistingSnapshot(uint256 base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(std::pair< int, int > GetPruneRange(const Chainstate &chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(CCheckQueue< CScriptCheck > & GetCheckQueue()
When starting up, search the datadir for a chainstate based on a UTXO snapshot that is in the process...
std::set< CBlockIndex * > m_failed_blocks
In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to conn...
bool IsUsable(const Chainstate *const cs) const EXCLUSIVE_LOCKS_REQUIRED(
Return true if a chainstate is considered usable.
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
bool AcceptBlock(const std::shared_ptr< const CBlock > &pblock, BlockValidationState &state, CBlockIndex **ppindex, bool fRequested, const FlatFilePos *dbp, bool *fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Sufficiently validate a block for disk storage (and store on disk).
CTxOut out
unspent transaction output
bool IsSpent() const
Either this coin never existed (see e.g.
uint32_t nHeight
at which height this containing transaction was included in the active block chain
unsigned int fCoinBase
whether containing transaction was a coinbase
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
Used to track blocks whose transactions were applied to the UTXO state as a part of a single Activate...
std::vector< PerBlockConnectTrace > & GetBlocksConnected()
std::vector< PerBlockConnectTrace > blocksConnected
void BlockConnected(CBlockIndex *pindex, std::shared_ptr< const CBlock > pblock)
std::pair< uint32_t, size_t > setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
void insert(Element e)
insert loops at most depth_limit times trying to insert a hash at various locations in the table via ...
bool contains(const Element &e, const bool erase) const
contains iterates through the hash locations for a given element and checks to see if it is present.
DisconnectedBlockTransactions.
std::list< CTransactionRef > take()
Clear all data structures and return the list of transactions.
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Remove any entries that are in this block.
std::vector< CTransactionRef > AddTransactionsFromBlock(const std::vector< CTransactionRef > &vtx)
Add transactions from the block, iterating through vtx in reverse order.
static GenTxid Wtxid(const uint256 &hash)
static GenTxid Txid(const uint256 &hash)
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range) noexcept
Return the time point advanced by a uniform random duration.
Convenience class for initializing and passing the script execution cache and signature cache.
ValidationCache(size_t script_execution_cache_bytes, size_t signature_cache_bytes)
CuckooCache::cache< uint256, SignatureCacheHasher > m_script_execution_cache
CSHA256 ScriptExecutionCacheHasher() const
Return a copy of the pre-initialized hasher.
CSHA256 m_script_execution_cache_hasher
Pre-initialized hasher to avoid having to recreate it for every hash calculation.
SignatureCache m_signature_cache
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
void ActiveTipChange(const CBlockIndex &, bool)
void BlockChecked(const CBlock &, const BlockValidationState &)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
std::string GetRejectReason() const
std::string GetDebugMessage() const
bool Error(const std::string &reject_reason)
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
std::string ToString() const
std::vector< std::pair< int, bool > > CheckUnknownActivations(const CBlockIndex *pindex, const CChainParams &chainparams) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check for unknown activations Returns a vector containing the bit number used for signalling and a bo...
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
256-bit unsigned big integer.
constexpr bool IsNull() const
std::string ToString() const
constexpr unsigned char * begin()
A base class defining functions for notifying about certain kernel events.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
virtual void fatalError(const bilingual_str &message)
The fatal error notification is sent to notify the user when an error occurs in kernel code that can'...
virtual void warningSet(Warning id, const bilingual_str &message)
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex &index)
virtual void warningUnset(Warning id)
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
const kernel::BlockManagerOpts m_opts
RecursiveMutex cs_LastBlockFile
bool FlushChainstateBlockFile(int tip_height)
void FindFilesToPrune(std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
Prune block and undo files (blk???.dat and rev???.dat) so that the disk space used is less than a use...
void UpdateBlockInfo(const CBlock &block, unsigned int nHeight, const FlatFilePos &pos)
Update blockfile info while processing a block during reindex.
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
std::atomic_bool m_blockfiles_indexed
Whether all blockfiles have been added to the block tree database.
std::vector< CBlockIndex * > GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(std::multimap< CBlockIndex *, CBlockIndex * > m_blocks_unlinked
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool LoadingBlocks() const
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const std::optional< uint256 > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex * AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove any pruned block & undo files that are still on disk.
bool CheckBlockDataAvailability(const CBlockIndex &upper_block LIFETIMEBOUND, const CBlockIndex &lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetFirstBlock(const CBlockIndex &upper_block LIFETIMEBOUND, uint32_t status_mask, const CBlockIndex *lower_block=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned)
Check if all blocks in the [upper_block, lower_block] range have data available.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
bool IsPruneMode() const
Whether running in -prune mode.
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain, ChainstateManager &chainman)
bool WriteBlockUndo(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos WriteBlock(const CBlock &block, int nHeight)
Store block on disk and update block file statistics.
bool ReadBlock(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
std::string ToString() const
constexpr const std::byte * begin() const
std::string GetHex() const
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
std::string FormatFullVersion()
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction.
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
static int64_t GetBlockWeight(const CBlock &block)
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/RBF/etc limits
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_INPUTS_NOT_STANDARD
inputs (covered by txid) failed policy rules
@ TX_WITNESS_STRIPPED
Transaction is missing a witness.
@ TX_CONFLICT
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_WITNESS_MUTATED
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_CONSENSUS
invalid by consensus rules
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
static constexpr int64_t MAX_TIMEWARP
Maximum number of seconds that the timestamp of the first block of a difficulty adjustment period is ...
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits)
static const int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule)
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
static const int WITNESS_SCALE_FACTOR
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool DestroyDB(const std::string &path_str)
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, VersionBitsCache &versionbitscache)
Determine if a deployment is active for the next block.
bool DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, VersionBitsCache &versionbitscache)
Determine if a deployment is active for this block.
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
bool CheckEphemeralSpends(const Package &package, CFeeRate dust_relay_rate, const CTxMemPool &tx_pool, TxValidationState &out_child_state, Wtxid &out_child_wtxid)
Must be called for each transaction(package) if any dust is in the package.
bool PreCheckEphemeralTx(const CTransaction &tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState &state)
These utility functions ensure that ephemeral dust is safely created and spent without unduly risking...
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SCRIPT_VERIFY_NULLDUMMY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
#define LogPrintLevel(category, level,...)
#define LogDebug(category,...)
@ REORG
Removed for reorganization.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
std::array< uint8_t, 4 > MessageStartChars
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, CAmount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts) This does not m...
std::function< FILE *(const fs::path &, const char *)> FopenFn
bool IsInterrupted(const T &result)
@ UNKNOWN_NEW_RULES_ACTIVATED
@ LARGE_WORK_INVALID_CHAIN
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
std::unordered_map< uint256, CBlockIndex, BlockHasher > BlockMap
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
bilingual_str ErrorString(const Result< T > &result)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static feebumper::Result CheckFeeRate(const CWallet &wallet, const CMutableTransaction &mtx, const CFeeRate &newFeerate, const int64_t maxTxSize, CAmount old_fee, std::vector< bilingual_str > &errors)
Check if the user provided a valid feeRate.
std::shared_ptr< Chain::Notifications > m_notifications
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
bool IsWellFormedPackage(const Package &txns, PackageValidationState &state, bool require_sorted)
Context-free package policy checks:
uint256 GetPackageHash(const std::vector< CTransactionRef > &transactions)
Get the hash of the concatenated wtxids of transactions, with wtxids treated as a little-endian numbe...
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
@ PCKG_MEMPOOL_ERROR
Mempool logic error.
@ PCKG_TX
At least one tx is invalid.
std::optional< std::string > HasNoNewUnconfirmed(const CTransaction &tx, const CTxMemPool &pool, const CTxMemPool::setEntries &iters_conflicting)
The replacement transaction may only include an unconfirmed input if that input was included in one o...
std::optional< std::pair< DiagramCheckError, std::string > > ImprovesFeerateDiagram(CTxMemPool::ChangeSet &changeset)
The replacement transaction must improve the feerate diagram of the mempool.
std::optional< std::string > PaysForRBF(CAmount original_fees, CAmount replacement_fees, size_t replacement_vsize, CFeeRate relay_fee, const uint256 &txid)
The replacement transaction must pay more fees than the original transactions.
std::optional< std::string > EntriesAndTxidsDisjoint(const CTxMemPool::setEntries &ancestors, const std::set< Txid > &direct_conflicts, const uint256 &txid)
Check the intersection between two sets of transactions (a set of mempool entries and a set of txids)...
std::optional< std::string > PaysMoreThanConflicts(const CTxMemPool::setEntries &iters_conflicting, CFeeRate replacement_feerate, const uint256 &txid)
Check that the feerate of the replacement transaction(s) is higher than the feerate of each of the tr...
std::optional< std::string > GetEntriesForConflicts(const CTransaction &tx, CTxMemPool &pool, const CTxMemPool::setEntries &iters_conflicting, CTxMemPool::setEntries &all_conflicts)
Get all descendants of iters_conflicting.
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs.
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
static constexpr unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT
An extra transaction can be added to a package, as long as it only has one ancestor and is no larger ...
static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST
The maximum number of sigops we're willing to relay/mine in a single tx.
static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE
The minimum non-witness size for transactions we're willing to relay/mine: one larger than 64
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS
For convenience, standard but not mandatory verify flags.
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static constexpr TransactionSerParams TX_NO_WITNESS
static constexpr TransactionSerParams TX_WITH_WITNESS
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
Generate a random uint256.
std::string ScriptErrorString(const ScriptError serror)
enum ScriptError_t ScriptError
@ SCRIPT_ERR_UNKNOWN_ERROR
size_t GetSerializeSize(const T &t)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
unsigned char * UCharCast(char *c)
Holds configuration for use during UTXO snapshot load and validation.
AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
uint64_t m_chain_tx_count
Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex().
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< uint256 > vHave
A mutable version of CTransaction.
std::vector< CTxOut > vout
Holds various statistics on transactions within a chain.
User-controlled performance and debug options.
Parameters that influence chain consensus.
bool enforce_BIP94
Enforce BIP94 timewarp attack mitigation.
std::map< uint256, uint32_t > script_flag_exceptions
Hashes of blocks that.
int64_t DifficultyAdjustmentInterval() const
bool signet_blocks
If true, witness commitments contain a payload equal to a Bitcoin Script solution to the signet chall...
int nSubsidyHalvingInterval
std::chrono::seconds PowTargetSpacing() const
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
Data structure storing a fee and size, ordered by increasing fee/size.
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
const TxValidationState m_state
Contains information about why the transaction failed.
@ INVALID
‍Fully validated, valid.
static MempoolAcceptResult Failure(TxValidationState state)
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static MempoolAcceptResult MempoolTxDifferentWitness(const Wtxid &other_wtxid)
static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees)
static MempoolAcceptResult Success(std::list< CTransactionRef > &&replaced_txns, int64_t vsize, CAmount fees, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static time_point now() noexcept
Return current system time or mocked time, if set.
static time_point now() noexcept
Return current system time or mocked time, if set.
Validation result for package mempool acceptance.
std::shared_ptr< const CBlock > pblock
PerBlockConnectTrace()=default
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
std::vector< CTxOut > m_spent_outputs
const char * what() const noexcept override
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
const fs::path blocks_dir
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
ValidationSignals * signals
std::optional< int32_t > check_block_index
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
const CChainParams & chainparams
CoinsViewOptions coins_view
Options struct containing limit options for a CTxMemPool.
int64_t descendant_count
The maximum allowed number of transactions in a package including the entry and its descendants.
int64_t descendant_size_vbytes
The maximum allowed size in virtual bytes of an entry and its descendants within a package.
int64_t ancestor_count
The maximum allowed number of transactions in a package including the entry and its ancestors.
int64_t ancestor_size_vbytes
The maximum allowed size in virtual bytes of an entry and its ancestors within a package.
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define LOCKS_EXCLUDED(...)
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
#define TRACEPOINT(context,...)
consteval auto _(util::TranslatedLiteral str)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::optional< std::string > PackageTRUCChecks(const CTransactionRef &ptx, int64_t vsize, const Package &package, const CTxMemPool::setEntries &mempool_ancestors)
Must be called for every transaction that is submitted within a package, even if not TRUC.
std::optional< std::pair< std::string, CTransactionRef > > SingleTRUCChecks(const CTransactionRef &ptx, const CTxMemPool::setEntries &mempool_ancestors, const std::set< Txid > &direct_conflicts, int64_t vsize)
Must be called for every transaction, even if not TRUC.
static constexpr decltype(CTransaction::version) TRUC_VERSION
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Calculates the block height and previous block's median time past at which the transaction will be co...
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, uint32_t flags)
Compute total signature operation cost of a transaction.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp)
Test whether the LockPoints height and time are still valid on the current chain.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0....
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t count_seconds(std::chrono::seconds t)
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept, const std::optional< CFeeRate > &client_maxfeerate)
Validate (and maybe submit) a package to the mempool.
bool CheckInputScripts(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData &txdata, ValidationCache &validation_cache, std::vector< CScriptCheck > *pvChecks=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Check whether all of this transaction's input scripts succeed.
static void LimitMempoolSize(CTxMemPool &pool, CCoinsViewCache &coins_cache) EXCLUSIVE_LOCKS_REQUIRED(
bool IsBlockMutated(const CBlock &block, bool check_witness_root)
Check if a block has been mutated (with respect to its merkle root and witness commitments).
static unsigned int GetBlockScriptFlags(const CBlockIndex &block_index, const ChainstateManager &chainman)
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
static constexpr size_t WARN_FLUSH_COINS_SIZE
Size threshold for warning about slow UTXO set flush to disk.
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept)
Try to add a transaction to the mempool.
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static bool ContextualCheckBlock(const CBlock &block, BlockValidationState &state, const ChainstateManager &chainman, const CBlockIndex *pindexPrev)
NOTE: This function is not currently invoked by ConnectBlock(), so we should consider upgrade issues ...
bool FatalError(Notifications ¬ifications, BlockValidationState &state, const bilingual_str &message)
bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, unsigned int flags, PrecomputedTransactionData &txdata, CCoinsViewCache &coins_tip, ValidationCache &validation_cache) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Checks to avoid mempool polluting consensus critical paths since cached signature and script validity...
static void UpdateTipLog(const ChainstateManager &chainman, const CCoinsViewCache &coins_tip, const CBlockIndex *tip, const std::string &func_name, const std::string &prefix, const std::string &warning_messages) EXCLUSIVE_LOCKS_REQUIRED(
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
static constexpr auto DATABASE_WRITE_INTERVAL_MAX
static bool CheckWitnessMalleation(const CBlock &block, bool expect_witness_commitment, BlockValidationState &state)
CheckWitnessMalleation performs checks for block malleation with regard to its witnesses.
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
arith_uint256 CalculateClaimedHeadersWork(std::span< const CBlockHeader > headers)
Return the sum of the claimed work on a given set of headers.
bool IsBIP30Unspendable(const CBlockIndex &block_index)
Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30)
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE
Maximum age of our tip for us to be considered current for fee estimation.
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
bool TestBlockValidity(BlockValidationState &state, const CChainParams &chainparams, Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
Check a block is completely valid from start to finish (only works on top of our current best block)
static bool IsCurrentForFeeEstimation(Chainstate &active_chainstate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
static constexpr auto DATABASE_WRITE_INTERVAL_MIN
Time window to wait between writing blocks/block index and chainstate to disk.
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW=true)
bool IsBIP30Repeat(const CBlockIndex &block_index)
Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30)
static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt &interrupt)
static fs::path GetSnapshotCoinsDBPath(Chainstate &cs) EXCLUSIVE_LOCKS_REQUIRED(
static SynchronizationState GetSynchronizationState(bool init, bool blockfiles_indexed)
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, const ChainstateManager &chainman, const CBlockIndex *pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
TRACEPOINT_SEMAPHORE(validation, block_connected)
static void LimitValidationInterfaceQueue(ValidationSignals &signals) LOCKS_EXCLUDED(cs_main)
static constexpr int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
@ BASE_BLOCKHASH_MISMATCH
SynchronizationState
Current sync state passed to tip changed callbacks.
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.