6 #include <bitcoin-build-config.h> 62 #include <util/time.h> 101 "level 0 reads the blocks from disk",
102 "level 1 verifies block validity",
103 "level 2 verifies undo data",
104 "level 3 checks disconnection of tip blocks",
105 "level 4 tries to reconnect the blocks",
106 "each level includes the checks of the previous levels",
144 std::vector<CScriptCheck>* pvChecks =
nullptr)
157 const int nBlockHeight = active_chain_tip.nHeight + 1;
164 const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
166 return IsFinalTx(tx, nBlockHeight, nBlockTime);
180 std::optional<std::vector<int>> CalculatePrevHeights(
185 std::vector<int> prev_heights;
186 prev_heights.resize(tx.
vin.size());
187 for (
size_t i = 0; i < tx.
vin.size(); ++i) {
188 if (
auto coin{coins.
GetCoin(tx.
vin[i].prevout)}) {
193 LogInfo(
"ERROR: %s: Missing input %d in transaction \'%s\'\n", __func__, i, tx.
GetHash().
GetHex());
208 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
209 if (!prev_heights.has_value())
return std::nullopt;
212 next_tip.
pprev = tip;
230 int max_input_height{0};
231 for (
const int height : prev_heights.value()) {
233 if (height != next_tip.
nHeight) {
234 max_input_height = std::max(max_input_height, height);
269 int expired = pool.Expire(GetTime<std::chrono::seconds>() - pool.m_opts.expiry);
274 std::vector<COutPoint> vNoSpendsRemaining;
275 pool.TrimToSize(pool.m_opts.max_size_bytes, &vNoSpendsRemaining);
276 for (
const COutPoint& removed : vNoSpendsRemaining)
277 coins_cache.Uncache(removed);
283 if (active_chainstate.m_chainman.IsInitialBlockDownload()) {
288 if (active_chainstate.m_chain.Height() < active_chainstate.m_chainman.m_best_header->nHeight - 1) {
302 std::vector<Txid> vHashUpdate;
309 const auto queuedTx = disconnectpool.
take();
310 auto it = queuedTx.rbegin();
311 while (it != queuedTx.rend()) {
313 if (!fAddToMempool || (*it)->IsCoinBase() ||
321 vHashUpdate.push_back((*it)->GetHash());
362 it->UpdateLockPoints(*new_lock_points);
369 if (it->GetSpendsCoinbase()) {
375 if (coin.IsCoinBase() && mempool_spend_height - coin.nHeight <
COINBASE_MATURITY) {
411 if (coin.
IsSpent())
return false;
423 const Coin& coinFromUTXOSet = coins_tip.AccessCoin(txin.
prevout);
441 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
442 m_active_chainstate(active_chainstate)
450 const int64_t m_accept_time;
451 const bool m_bypass_limits;
459 std::vector<COutPoint>& m_coins_to_uncache;
461 const bool m_test_accept;
465 const bool m_allow_replacement;
467 const bool m_allow_sibling_eviction;
470 const bool m_package_submission;
474 const bool m_package_feerates;
479 const std::optional<CFeeRate> m_client_maxfeerate;
482 static ATMPArgs SingleAccept(
const CChainParams& chainparams, int64_t accept_time,
483 bool bypass_limits, std::vector<COutPoint>& coins_to_uncache,
485 return ATMPArgs{ chainparams,
499 static ATMPArgs PackageTestAccept(
const CChainParams& chainparams, int64_t accept_time,
500 std::vector<COutPoint>& coins_to_uncache) {
501 return ATMPArgs{ chainparams,
515 static ATMPArgs PackageChildWithParents(
const CChainParams& chainparams, int64_t accept_time,
516 std::vector<COutPoint>& coins_to_uncache,
const std::optional<CFeeRate>& client_maxfeerate) {
517 return ATMPArgs{ chainparams,
531 static ATMPArgs SingleInPackageAccept(
const ATMPArgs& package_args) {
532 return ATMPArgs{ package_args.m_chainparams,
533 package_args.m_accept_time,
535 package_args.m_coins_to_uncache,
536 package_args.m_test_accept,
541 package_args.m_client_maxfeerate,
551 std::vector<COutPoint>& coins_to_uncache,
553 bool allow_replacement,
554 bool allow_sibling_eviction,
555 bool package_submission,
556 bool package_feerates,
557 std::optional<CFeeRate> client_maxfeerate)
558 : m_chainparams{chainparams},
559 m_accept_time{accept_time},
560 m_bypass_limits{bypass_limits},
561 m_coins_to_uncache{coins_to_uncache},
562 m_test_accept{test_accept},
563 m_allow_replacement{allow_replacement},
564 m_allow_sibling_eviction{allow_sibling_eviction},
565 m_package_submission{package_submission},
566 m_package_feerates{package_feerates},
567 m_client_maxfeerate{client_maxfeerate}
571 if (m_package_feerates) {
572 Assume(m_package_submission);
573 Assume(!m_allow_sibling_eviction);
575 if (m_allow_sibling_eviction)
Assume(m_allow_replacement);
586 ClearSubPackageState();
599 ClearSubPackageState();
627 explicit Workspace(
const CTransactionRef& ptx) : m_ptx(ptx), m_hash(ptx->GetHash()) {}
630 std::set<Txid> m_conflicts;
635 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> m_parents;
640 bool m_sibling_eviction{
false};
673 bool PackageRBFChecks(
const std::vector<CTransactionRef>& txns,
674 std::vector<Workspace>& workspaces,
695 std::map<Wtxid, MempoolAcceptResult>& results)
703 CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
704 if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
708 if (package_fee < m_pool.m_opts.min_relay_feerate.GetFee(package_size)) {
710 strprintf(
"%d < %d", package_fee, m_pool.m_opts.min_relay_feerate.GetFee(package_size)));
717 return m_active_chainstate.m_chainman.m_validation_cache;
749 struct SubPackageState {
751 CAmount m_total_modified_fees{0};
753 int64_t m_total_vsize{0};
760 std::list<CTransactionRef> m_replaced_transactions;
762 std::unique_ptr<CTxMemPool::ChangeSet> m_changeset;
767 size_t m_conflicting_size{0};
770 struct SubPackageState m_subpackage;
775 m_subpackage = SubPackageState{};
778 CleanupTemporaryCoins();
782 bool MemPoolAccept::PreChecks(ATMPArgs&
args, Workspace& ws)
788 const Txid& hash = ws.m_hash;
791 const int64_t nAcceptTime =
args.m_accept_time;
792 const bool bypass_limits =
args.m_bypass_limits;
793 std::vector<COutPoint>& coins_to_uncache =
args.m_coins_to_uncache;
808 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)) {
826 }
else if (m_pool.exists(tx.
GetHash())) {
836 if (ptxConflicting) {
837 if (!
args.m_allow_replacement) {
841 ws.m_conflicts.insert(ptxConflicting->
GetHash());
845 m_view.SetBackend(m_viewmempool);
851 coins_to_uncache.push_back(txin.
prevout);
857 if (!m_view.HaveCoin(txin.
prevout)) {
872 m_view.GetBestBlock();
877 m_view.SetBackend(m_dummy);
879 assert(m_active_chainstate.m_blockman.LookupBlockIndex(m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
886 const std::optional<LockPoints> lock_points{
CalculateLockPointsAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx)};
909 bool fSpendsCoinbase =
false;
911 const Coin &coin = m_view.AccessCoin(txin.
prevout);
913 fSpendsCoinbase =
true;
920 const uint64_t entry_sequence = bypass_limits ? 0 : m_pool.GetSequence();
921 if (!m_subpackage.m_changeset) {
922 m_subpackage.m_changeset = m_pool.GetChangeSet();
924 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());
927 ws.m_modified_fees = ws.m_tx_handle->GetModifiedFee();
929 ws.m_vsize = ws.m_tx_handle->GetTxSize();
932 if (m_pool.m_opts.require_standard) {
933 if (!
PreCheckEphemeralTx(*ptx, m_pool.m_opts.dust_relay_feerate, ws.m_base_fees, ws.m_modified_fees, state)) {
945 if (!bypass_limits && !
args.m_package_feerates && !
CheckFeeRate(ws.m_vsize, ws.m_modified_fees, state))
return false;
947 ws.m_iters_conflicting = m_pool.GetIterSet(ws.m_conflicts);
949 ws.m_parents = m_pool.GetParents(*ws.m_tx_handle);
951 if (!
args.m_bypass_limits) {
953 if (
const auto err{
SingleTRUCChecks(m_pool, ws.m_ptx, ws.m_parents, ws.m_conflicts, ws.m_vsize)}) {
955 if (
args.m_allow_sibling_eviction && err->second !=
nullptr) {
960 ws.m_conflicts.insert(err->second->GetHash());
964 ws.m_iters_conflicting.insert(m_pool.GetIter(err->second->GetHash()).value());
965 ws.m_sibling_eviction =
true;
977 m_subpackage.m_rbf |= !ws.m_conflicts.empty();
981 bool MemPoolAccept::ReplacementChecks(Workspace& ws)
987 const Txid& hash = ws.m_hash;
990 CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
997 strprintf(
"too many potential replacements%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1003 m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1004 m_subpackage.m_conflicting_size += it->GetTxSize();
1007 if (
const auto err_string{
PaysForRBF(m_subpackage.m_conflicting_fees, ws.m_modified_fees, ws.m_vsize,
1008 m_pool.m_opts.incremental_relay_feerate, hash)}) {
1011 strprintf(
"insufficient fee%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1015 for (
auto it : all_conflicts) {
1016 m_subpackage.m_changeset->StageRemoval(it);
1020 if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
1034 bool MemPoolAccept::PackageRBFChecks(
const std::vector<CTransactionRef>& txns,
1035 std::vector<Workspace>& workspaces,
1036 const int64_t total_vsize,
1042 assert(std::all_of(txns.cbegin(), txns.cend(), [
this](
const auto& tx)
1043 {
return !m_pool.exists(tx->
GetHash());}));
1045 assert(txns.size() == workspaces.size());
1060 for (
const auto& ws : workspaces) {
1061 if (!ws.m_parents.empty()) {
1068 for (Workspace& ws : workspaces) {
1070 direct_conflict_iters.merge(ws.m_iters_conflicting);
1073 const auto& parent_ws = workspaces[0];
1074 const auto& child_ws = workspaces[1];
1082 "package RBF failed: too many potential replacements", *err_string);
1086 m_subpackage.m_changeset->StageRemoval(it);
1087 m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1088 m_subpackage.m_conflicting_size += it->GetTxSize();
1092 const Txid& child_hash = child_ws.m_ptx->GetHash();
1093 if (
const auto err_string{
PaysForRBF(m_subpackage.m_conflicting_fees,
1094 m_subpackage.m_total_modified_fees,
1095 m_subpackage.m_total_vsize,
1096 m_pool.m_opts.incremental_relay_feerate, child_hash)}) {
1098 "package RBF failed: insufficient anti-DoS fees", *err_string);
1103 const CFeeRate parent_feerate(parent_ws.m_modified_fees, parent_ws.m_vsize);
1104 const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1105 if (package_feerate <= parent_feerate) {
1107 "package RBF failed: package feerate is less than or equal to parent feerate",
1108 strprintf(
"package feerate %s <= parent feerate is %s", package_feerate.ToString(), parent_feerate.ToString()));
1112 if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
1120 "package RBF failed: " + err_tup.value().second,
"");
1123 LogDebug(
BCLog::TXPACKAGES,
"package RBF checks passed: parent %s (wtxid=%s), child %s (wtxid=%s), package hash (%s)\n",
1124 txns.front()->GetHash().ToString(), txns.front()->GetWitnessHash().ToString(),
1125 txns.back()->GetHash().ToString(), txns.back()->GetWitnessHash().ToString(),
1132 bool MemPoolAccept::PolicyScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1143 if (!
CheckInputScripts(tx, state, m_view, scriptVerifyFlags,
true,
false, ws.m_precomputed_txdata, GetValidationCache())) {
1155 bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1160 const Txid& hash = ws.m_hash;
1180 ws.m_precomputed_txdata, m_active_chainstate.CoinsTip(), GetValidationCache())) {
1181 LogError(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s", hash.
ToString(), state.
ToString());
1188 void MemPoolAccept::FinalizeSubpackage(
const ATMPArgs&
args)
1193 if (!m_subpackage.m_changeset->GetRemovals().empty())
Assume(
args.m_allow_replacement);
1197 std::string log_string =
strprintf(
"replacing mempool tx %s (wtxid=%s, fees=%s, vsize=%s). ",
1198 it->GetTx().GetHash().ToString(),
1199 it->GetTx().GetWitnessHash().ToString(),
1202 FeeFrac feerate{m_subpackage.m_total_modified_fees, int32_t(m_subpackage.m_total_vsize)};
1204 const bool replaced_with_tx{m_subpackage.m_changeset->GetTxCount() == 1};
1205 if (replaced_with_tx) {
1206 const CTransaction& tx = m_subpackage.m_changeset->GetAddedTxn(0);
1208 log_string +=
strprintf(
"New tx %s (wtxid=%s, fees=%s, vsize=%s)",
1214 tx_or_package_hash =
GetPackageHash(m_subpackage.m_changeset->GetAddedTxns());
1215 log_string +=
strprintf(
"New package %s with %lu txs, fees=%s, vsize=%s",
1216 tx_or_package_hash.ToString(),
1217 m_subpackage.m_changeset->GetTxCount(),
1224 it->GetTx().GetHash().data(),
1227 std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).
count(),
1228 tx_or_package_hash.data(),
1233 m_subpackage.m_replaced_transactions.push_back(it->GetSharedTx());
1235 m_subpackage.m_changeset->Apply();
1236 m_subpackage.m_changeset.reset();
1239 bool MemPoolAccept::SubmitPackage(
const ATMPArgs&
args, std::vector<Workspace>& workspaces,
1241 std::map<Wtxid, MempoolAcceptResult>& results)
1247 assert(std::all_of(workspaces.cbegin(), workspaces.cend(), [
this](
const auto& ws) {
return !m_pool.exists(ws.m_ptx->GetHash()); }));
1249 bool all_submitted =
true;
1250 FinalizeSubpackage(
args);
1255 for (Workspace& ws : workspaces) {
1256 if (!ConsensusScriptChecks(
args, ws)) {
1260 all_submitted =
false;
1262 strprintf(
"BUG! PolicyScriptChecks succeeded but ConsensusScriptChecks failed: %s",
1263 ws.m_ptx->GetHash().ToString()));
1265 if (!m_subpackage.m_changeset) m_subpackage.m_changeset = m_pool.GetChangeSet();
1266 m_subpackage.m_changeset->StageRemoval(m_pool.GetIter(ws.m_ptx->GetHash()).value());
1269 if (!all_submitted) {
1270 Assume(m_subpackage.m_changeset);
1274 m_subpackage.m_changeset->Apply();
1275 m_subpackage.m_changeset.reset();
1279 std::vector<Wtxid> all_package_wtxids;
1280 all_package_wtxids.reserve(workspaces.size());
1281 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1282 [](
const auto& ws) {
return ws.m_ptx->GetWitnessHash(); });
1284 if (!m_subpackage.m_replaced_transactions.empty()) {
1285 LogDebug(
BCLog::MEMPOOL,
"replaced %u mempool transactions with %u new one(s) for %s additional fees, %d delta bytes\n",
1286 m_subpackage.m_replaced_transactions.size(), workspaces.size(),
1287 m_subpackage.m_total_modified_fees - m_subpackage.m_conflicting_fees,
1288 m_subpackage.m_total_vsize -
static_cast<int>(m_subpackage.m_conflicting_size));
1292 for (Workspace& ws : workspaces) {
1293 auto iter = m_pool.GetIter(ws.m_ptx->GetHash());
1294 Assume(iter.has_value());
1295 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1296 CFeeRate{ws.m_modified_fees,
static_cast<int32_t
>(ws.m_vsize)};
1297 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1298 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1299 results.emplace(ws.m_ptx->GetWitnessHash(),
1301 ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
1302 if (!m_pool.m_opts.signals)
continue;
1305 ws.m_vsize, (*iter)->GetHeight(),
1306 args.m_bypass_limits,
args.m_package_submission,
1308 m_pool.HasNoInputsOf(tx));
1309 m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1311 return all_submitted;
1320 const std::vector<Wtxid> single_wtxid{ws.m_ptx->GetWitnessHash()};
1322 if (!PreChecks(
args, ws)) {
1330 if (m_subpackage.m_rbf && !ReplacementChecks(ws)) {
1339 if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
1346 if (ws.m_conflicts.size()) {
1347 auto ancestors = m_subpackage.m_changeset->CalculateMemPoolAncestors(ws.m_tx_handle);
1361 m_subpackage.m_total_vsize = ws.m_vsize;
1362 m_subpackage.m_total_modified_fees = ws.m_modified_fees;
1365 if (
args.m_client_maxfeerate &&
CFeeRate(ws.m_modified_fees, ws.m_vsize) >
args.m_client_maxfeerate.value()) {
1370 if (!
args.m_bypass_limits && m_pool.m_opts.require_standard) {
1372 if (!
CheckEphemeralSpends({ptx}, m_pool.m_opts.dust_relay_feerate, m_pool, ws.m_state, dummy_wtxid)) {
1383 const CFeeRate effective_feerate{ws.m_modified_fees,
static_cast<int32_t
>(ws.m_vsize)};
1385 if (
args.m_test_accept) {
1387 ws.m_base_fees, effective_feerate, single_wtxid);
1390 FinalizeSubpackage(
args);
1393 if (!
args.m_package_submission && !
args.m_bypass_limits) {
1397 CleanupTemporaryCoins();
1399 if (!m_pool.exists(ws.m_hash)) {
1406 if (m_pool.m_opts.signals) {
1408 auto iter = m_pool.GetIter(tx.
GetHash());
1409 Assume(iter.has_value());
1411 ws.m_vsize, (*iter)->GetHeight(),
1412 args.m_bypass_limits,
args.m_package_submission,
1414 m_pool.HasNoInputsOf(tx));
1415 m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1418 if (!m_subpackage.m_replaced_transactions.empty()) {
1419 LogDebug(
BCLog::MEMPOOL,
"replaced %u mempool transactions with 1 new transaction for %s additional fees, %d delta bytes\n",
1420 m_subpackage.m_replaced_transactions.size(),
1421 ws.m_modified_fees - m_subpackage.m_conflicting_fees,
1422 ws.m_vsize -
static_cast<int>(m_subpackage.m_conflicting_size));
1426 effective_feerate, single_wtxid);
1438 std::vector<Workspace> workspaces{};
1439 workspaces.reserve(txns.size());
1440 std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1441 [](
const auto& tx) {
return Workspace(tx); });
1442 std::map<Wtxid, MempoolAcceptResult> results;
1445 for (Workspace& ws : workspaces) {
1446 if (!PreChecks(
args, ws)) {
1455 if (
args.m_client_maxfeerate &&
CFeeRate(ws.m_modified_fees, ws.m_vsize) >
args.m_client_maxfeerate.value()) {
1473 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1478 for (Workspace& ws : workspaces) {
1479 if (
auto err{
PackageTRUCChecks(m_pool, ws.m_ptx, ws.m_vsize, txns, ws.m_parents)}) {
1494 m_subpackage.m_total_vsize = std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1495 [](int64_t
sum,
auto& ws) {
return sum + ws.m_vsize; });
1496 m_subpackage.m_total_modified_fees = std::accumulate(workspaces.cbegin(), workspaces.cend(),
CAmount{0},
1497 [](
CAmount sum,
auto& ws) {
return sum + ws.m_modified_fees; });
1498 const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1499 std::vector<Wtxid> all_package_wtxids;
1500 all_package_wtxids.reserve(workspaces.size());
1501 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1502 [](
const auto& ws) {
return ws.m_ptx->GetWitnessHash(); });
1504 if (
args.m_package_feerates &&
1505 !
CheckFeeRate(m_subpackage.m_total_vsize, m_subpackage.m_total_modified_fees, placeholder_state)) {
1512 if (m_subpackage.m_rbf && !PackageRBFChecks(txns, workspaces, m_subpackage.m_total_vsize, package_state)) {
1517 if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
1523 if (m_pool.m_opts.require_standard) {
1526 if (!
CheckEphemeralSpends(txns, m_pool.m_opts.dust_relay_feerate, m_pool, child_state, child_wtxid)) {
1533 for (Workspace& ws : workspaces) {
1534 ws.m_package_feerate = package_feerate;
1535 if (!PolicyScriptChecks(
args, ws)) {
1541 if (
args.m_test_accept) {
1542 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1543 CFeeRate{ws.m_modified_fees,
static_cast<int32_t
>(ws.m_vsize)};
1544 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1545 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1546 results.emplace(ws.m_ptx->GetWitnessHash(),
1548 ws.m_vsize, ws.m_base_fees, effective_feerate,
1549 effective_feerate_wtxids));
1555 if (!SubmitPackage(
args, workspaces, package_state, results)) {
1563 void MemPoolAccept::CleanupTemporaryCoins()
1584 for (
const auto& outpoint : m_viewmempool.GetNonBaseCoins()) {
1587 m_view.Uncache(outpoint);
1590 m_viewmempool.Reset();
1598 if (subpackage.size() > 1) {
1599 return AcceptMultipleTransactionsInternal(subpackage,
args);
1601 const auto& tx = subpackage.front();
1602 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(
args);
1603 const auto single_res = AcceptSingleTransactionInternal(tx, single_args);
1614 ClearSubPackageState();
1621 Assert(!package.empty());
1647 std::map<Wtxid, MempoolAcceptResult> results_final;
1651 std::map<Wtxid, MempoolAcceptResult> individual_results_nonfinal;
1653 bool quit_early{
false};
1654 std::vector<CTransactionRef> txns_package_eval;
1655 for (
const auto& tx : package) {
1657 const auto& txid = tx->
GetHash();
1661 if (m_pool.exists(wtxid)) {
1671 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1673 }
else if (m_pool.exists(txid)) {
1681 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1687 const auto single_package_res = AcceptSubPackage({tx},
args);
1688 const auto& single_res = single_package_res.m_tx_results.at(wtxid);
1692 assert(m_pool.exists(wtxid));
1693 results_final.emplace(wtxid, single_res);
1694 }
else if (package.size() == 1 ||
1708 individual_results_nonfinal.emplace(wtxid, single_res);
1710 individual_results_nonfinal.emplace(wtxid, single_res);
1711 txns_package_eval.push_back(tx);
1716 auto multi_submission_result = quit_early || txns_package_eval.empty() ?
PackageMempoolAcceptResult(package_state_quit_early, {}) :
1717 AcceptSubPackage(txns_package_eval,
args);
1723 ClearSubPackageState();
1730 for (
const auto& tx : package) {
1732 if (multi_submission_result.m_tx_results.contains(wtxid)) {
1734 Assume(!results_final.contains(wtxid));
1737 const auto& txresult = multi_submission_result.m_tx_results.at(wtxid);
1744 results_final.emplace(wtxid, txresult);
1746 }
else if (
const auto it{results_final.find(wtxid)}; it != results_final.end()) {
1750 Assume(!individual_results_nonfinal.contains(wtxid));
1752 if (!m_pool.exists(tx->
GetHash())) {
1757 results_final.erase(wtxid);
1760 }
else if (
const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
1763 results_final.emplace(wtxid, it->second);
1766 Assume(results_final.size() == package.size());
1773 int64_t accept_time,
bool bypass_limits,
bool test_accept)
1780 std::vector<COutPoint> coins_to_uncache;
1782 auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept);
1791 for (
const COutPoint& hashTx : coins_to_uncache)
1794 tx->GetHash().data(),
1795 result.m_state.GetRejectReason().c_str()
1805 const Package& package,
bool test_accept,
const std::optional<CFeeRate>& client_maxfeerate)
1808 assert(!package.empty());
1809 assert(std::all_of(package.cbegin(), package.cend(), [](
const auto& tx){
return tx !=
nullptr;}));
1811 std::vector<COutPoint> coins_to_uncache;
1816 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(chainparams,
GetTime(), coins_to_uncache);
1817 return MemPoolAccept(pool, active_chainstate).AcceptMultipleTransactionsAndCleanup(package,
args);
1819 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(chainparams,
GetTime(), coins_to_uncache, client_maxfeerate);
1820 return MemPoolAccept(pool, active_chainstate).AcceptPackage(package,
args);
1825 if (test_accept ||
result.m_state.IsInvalid()) {
1826 for (
const COutPoint& hashTx : coins_to_uncache) {
1845 nSubsidy >>= halvings;
1850 : m_dbview{std::move(db_params), std::move(options)},
1851 m_catcherview(&m_dbview) {}
1853 void CoinsViews::InitCache()
1856 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1857 m_connect_block_view = std::make_unique<CoinsViewOverlay>(&*m_cacheview);
1864 std::optional<uint256> from_snapshot_blockhash)
1865 : m_mempool(mempool),
1866 m_blockman(blockman),
1867 m_chainman(chainman),
1869 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1880 const CBlockIndex* Chainstate::SnapshotBase()
const 1884 return m_cached_snapshot_base;
1887 const CBlockIndex* Chainstate::TargetBlock()
const 1889 if (!m_target_blockhash)
return nullptr;
1891 return m_cached_target_block;
1894 void Chainstate::SetTargetBlock(
CBlockIndex* block)
1899 m_target_blockhash.reset();
1901 m_cached_target_block = block;
1904 void Chainstate::SetTargetBlockHash(
uint256 block_hash)
1906 m_target_blockhash = block_hash;
1907 m_cached_target_block =
nullptr;
1910 void Chainstate::InitCoinsDB(
1911 size_t cache_size_bytes,
1918 .cache_bytes = cache_size_bytes,
1919 .memory_only = in_memory,
1920 .wipe_data = should_wipe,
1928 void Chainstate::InitCoinsCache(
size_t cache_size_bytes)
1946 if (this->GetRole().historical) {
1951 LogWarning(
"Found invalid chain more than 6 blocks longer than our best chain. This could be due to database corruption or consensus incompatibility with peers.");
1954 _(
"Warning: Found invalid chain more than 6 blocks longer than our best chain. This could be due to database corruption or consensus incompatibility with peers."));
1967 SetBlockFailureFlags(pindexNew);
1972 LogInfo(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n", __func__,
1977 LogInfo(
"%s: current best=%s height=%d log2_work=%f date=%s\n", __func__,
2015 if (
VerifyScript(scriptSig,
m_tx_out.
scriptPubKey, witness,
m_flags,
CachingTransactionSignatureChecker(
ptxTo,
nIn,
m_tx_out.
nValue,
cacheStore, *
m_signature_cache, *
txdata), &error)) {
2016 return std::nullopt;
2019 return std::make_pair(error, std::move(debug_str));
2024 : m_signature_cache{signature_cache_bytes}
2031 m_script_execution_cache_hasher.Write(
nonce.begin(), 32);
2032 m_script_execution_cache_hasher.Write(
nonce.begin(), 32);
2034 const auto [num_elems, approx_size_bytes] = m_script_execution_cache.setup_bytes(script_execution_cache_bytes);
2035 LogInfo(
"Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements",
2036 approx_size_bytes >> 20, script_execution_cache_bytes >> 20, num_elems);
2062 std::vector<CScriptCheck>* pvChecks)
2067 pvChecks->reserve(tx.
vin.size());
2084 std::vector<CTxOut> spent_outputs;
2085 spent_outputs.reserve(tx.
vin.size());
2087 for (
const auto& txin : tx.
vin) {
2091 spent_outputs.emplace_back(coin.
out);
2093 txdata.
Init(tx, std::move(spent_outputs));
2097 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
2108 pvChecks->emplace_back(std::move(
check));
2124 if (cacheFullScriptStore && !pvChecks) {
2152 if (undo.nHeight == 0) {
2158 undo.nHeight = alternate.
nHeight;
2183 LogError(
"DisconnectBlock(): failure reading undo data\n");
2187 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
2188 LogError(
"DisconnectBlock(): block and undo data inconsistent\n");
2198 bool fEnforceBIP30 = !((pindex->
nHeight==91722 && pindex->
GetBlockHash() ==
uint256{
"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
2199 (pindex->
nHeight==91812 && pindex->
GetBlockHash() ==
uint256{
"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"}));
2202 for (
int i = block.
vtx.size() - 1; i >= 0; i--) {
2206 bool is_bip30_exception = (is_coinbase && !fEnforceBIP30);
2210 for (
size_t o = 0; o < tx.
vout.size(); o++) {
2211 if (!tx.
vout[o].scriptPubKey.IsUnspendable()) {
2216 if (!is_bip30_exception) {
2227 LogError(
"DisconnectBlock(): transaction and undo data inconsistent\n");
2230 for (
unsigned int j = tx.
vin.size(); j > 0;) {
2301 const auto time_start{SteadyClock::now()};
2317 if (!
CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) {
2329 uint256 hashPrevBlock = pindex->
pprev ==
nullptr ?
uint256() : pindex->pprev->GetBlockHash();
2336 if (block_hash == params.GetConsensus().hashGenesisBlock) {
2342 const char* script_check_reason;
2344 script_check_reason =
"assumevalid=0 (always verify)";
2346 constexpr int64_t TWO_WEEKS_IN_SECONDS{60 * 60 * 24 * 7 * 2};
2354 script_check_reason =
"assumevalid hash not in headers";
2355 }
else if (it->second.GetAncestor(pindex->
nHeight) != pindex) {
2356 script_check_reason = (pindex->
nHeight > it->second.nHeight) ?
"block height above assumevalid height" :
"block not in assumevalid chain";
2358 script_check_reason =
"block not in best header chain";
2360 script_check_reason =
"best header chainwork below minimumchainwork";
2362 script_check_reason =
"block too recent relative to best header";
2378 script_check_reason =
nullptr;
2382 const auto time_1{SteadyClock::now()};
2383 m_chainman.time_check += time_1 - time_start;
2385 Ticks<MillisecondsDouble>(time_1 - time_start),
2427 static constexpr
int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2459 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->
GetBlockHash() == params.GetConsensus().BIP34Hash));
2464 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2465 for (
const auto& tx : block.
vtx) {
2466 for (
size_t o = 0; o < tx->
vout.size(); o++) {
2469 "tried to overwrite transaction");
2476 int nLockTimeFlags = 0;
2484 const auto time_2{SteadyClock::now()};
2487 Ticks<MillisecondsDouble>(time_2 - time_1),
2491 const bool fScriptChecks{!!script_check_reason};
2493 if (script_check_reason != m_last_script_check_reason_logged && role.validated && !role.historical) {
2494 if (fScriptChecks) {
2495 LogInfo(
"Enabling script verification at block #%d (%s): %s.",
2496 pindex->
nHeight, block_hash.ToString(), script_check_reason);
2498 LogInfo(
"Disabling script verification at block #%d (%s).",
2499 pindex->
nHeight, block_hash.ToString());
2501 m_last_script_check_reason_logged = script_check_reason;
2511 std::optional<CCheckQueueControl<CScriptCheck>> control;
2514 std::vector<PrecomputedTransactionData> txsdata(block.
vtx.size());
2516 std::vector<int> prevheights;
2519 int64_t nSigOpsCost = 0;
2520 blockundo.
vtxundo.reserve(block.
vtx.size() - 1);
2521 for (
unsigned int i = 0; i < block.
vtx.size(); i++)
2526 nInputs += tx.
vin.size();
2542 "accumulated fee in the block out of range");
2549 prevheights.resize(tx.
vin.size());
2550 for (
size_t j = 0; j < tx.
vin.size(); j++) {
2554 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2573 bool fCacheResults = fJustCheck;
2579 std::vector<CScriptCheck> vChecks;
2581 if (tx_ok) control->Add(std::move(vChecks));
2595 blockundo.
vtxundo.emplace_back();
2599 const auto time_3{SteadyClock::now()};
2601 LogDebug(
BCLog::BENCH,
" - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (
unsigned)block.
vtx.size(),
2602 Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.
vtx.size(),
2603 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2604 Ticks<SecondsDouble>(
m_chainman.time_connect),
2608 if (block.
vtx[0]->GetValueOut() > blockReward && state.
IsValid()) {
2610 strprintf(
"coinbase pays too much (actual=%d vs limit=%d)", block.
vtx[0]->GetValueOut(), blockReward));
2613 auto parallel_result = control->Complete();
2614 if (parallel_result.has_value() && state.
IsValid()) {
2622 const auto time_4{SteadyClock::now()};
2624 LogDebug(
BCLog::BENCH,
" - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
2625 Ticks<MillisecondsDouble>(time_4 - time_2),
2626 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2627 Ticks<SecondsDouble>(
m_chainman.time_verify),
2634 if (!
m_blockman.WriteBlockUndo(blockundo, state, *pindex)) {
2638 const auto time_5{SteadyClock::now()};
2641 Ticks<MillisecondsDouble>(time_5 - time_4),
2653 const auto time_6{SteadyClock::now()};
2656 Ticks<MillisecondsDouble>(time_6 - time_5),
2666 Ticks<std::chrono::nanoseconds>(time_5 - time_start)
2675 return this->GetCoinsCacheSizeState(
2681 size_t max_coins_cache_size_bytes,
2682 size_t max_mempool_size_bytes)
2687 int64_t nTotalSpace =
2688 max_coins_cache_size_bytes + std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2690 if (cacheSize > nTotalSpace) {
2691 LogInfo(
"Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2699 bool Chainstate::FlushStateToDisk(
2702 int nManualPruneHeight)
2705 assert(this->CanFlushToDisk());
2706 std::set<int> setFilesToPrune;
2707 bool full_flush_completed =
false;
2714 bool fFlushForPrune =
false;
2722 std::optional<std::string> limiting_lock;
2724 for (
const auto& prune_lock :
m_blockman.m_prune_locks) {
2725 if (prune_lock.second.height_first == std::numeric_limits<int>::max())
continue;
2728 last_prune = std::max(1, std::min(last_prune, lock_height));
2729 if (last_prune == lock_height) {
2730 limiting_lock = prune_lock.first;
2734 if (limiting_lock) {
2735 LogDebug(
BCLog::PRUNE,
"%s limited pruning to height %d\n", limiting_lock.value(), last_prune);
2738 if (nManualPruneHeight > 0) {
2743 std::min(last_prune, nManualPruneHeight),
2751 if (!setFilesToPrune.empty()) {
2752 fFlushForPrune =
true;
2754 m_blockman.m_block_tree_db->WriteFlag(
"prunedblockfiles",
true);
2771 LogDebug(
BCLog::COINDB,
"Writing chainstate to disk: flush mode=%s, prune=%d, large=%d, critical=%d, periodic=%d",
2772 FlushStateModeNames[
size_t(mode)], fFlushForPrune, fCacheLarge, fCacheCritical, fPeriodicWrite);
2785 LogWarning(
"%s: Failed to flush block file.\n", __func__);
2796 if (fFlushForPrune) {
2802 if (!
CoinsTip().GetBestBlock().IsNull()) {
2813 full_flush_completed =
true;
2815 int64_t{Ticks<std::chrono::microseconds>(
NodeClock::now() - nNow)},
2817 (uint64_t)coins_count,
2818 (uint64_t)coins_mem_usage,
2819 (
bool)fFlushForPrune);
2823 if (should_write ||
m_next_write == NodeClock::time_point::max()) {
2832 }
catch (
const std::runtime_error& e) {
2859 const std::string& func_name,
2860 const std::string&
prefix,
2872 chainman.GuessVerificationProgress(tip),
2873 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2874 coins_tip.GetCacheSize(),
2875 !warning_messages.empty() ?
strprintf(
" warning='%s'", warning_messages) :
"");
2878 void Chainstate::UpdateTip(
const CBlockIndex* pindexNew)
2881 const auto& coins_tip = this->
CoinsTip();
2887 constexpr
int BACKGROUND_LOG_INTERVAL = 2000;
2888 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2899 std::vector<bilingual_str> warning_messages;
2902 for (
auto [bit, active] : bits) {
2907 warning_messages.push_back(warning);
2934 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2937 LogError(
"DisconnectTip(): Failed to read block\n");
2941 const auto time_start{SteadyClock::now()};
2945 if (DisconnectBlock(block, pindexDelete, view) !=
DISCONNECT_OK) {
2952 Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
2956 const int max_height_first{pindexDelete->
nHeight - 1};
2957 for (
auto& prune_lock :
m_blockman.m_prune_locks) {
2958 if (prune_lock.second.height_first <= max_height_first)
continue;
2960 prune_lock.second.height_first = max_height_first;
2961 LogDebug(
BCLog::PRUNE,
"%s prune lock moved back to %d\n", prune_lock.first, max_height_first);
2981 UpdateTip(pindexDelete->
pprev);
3039 std::shared_ptr<const CBlock> block_to_connect,
3048 const auto time_1{SteadyClock::now()};
3049 if (!block_to_connect) {
3050 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3054 block_to_connect = std::move(pblockNew);
3059 const auto time_2{SteadyClock::now()};
3060 SteadyClock::time_point time_3;
3064 Ticks<MillisecondsDouble>(time_2 - time_1));
3068 bool rv =
ConnectBlock(*block_to_connect, state, pindexNew, view);
3078 time_3 = SteadyClock::now();
3079 m_chainman.time_connect_total += time_3 - time_2;
3082 Ticks<MillisecondsDouble>(time_3 - time_2),
3083 Ticks<SecondsDouble>(
m_chainman.time_connect_total),
3087 const auto time_4{SteadyClock::now()};
3090 Ticks<MillisecondsDouble>(time_4 - time_3),
3097 const auto time_5{SteadyClock::now()};
3098 m_chainman.time_chainstate += time_5 - time_4;
3100 Ticks<MillisecondsDouble>(time_5 - time_4),
3101 Ticks<SecondsDouble>(
m_chainman.time_chainstate),
3111 UpdateTip(pindexNew);
3113 const auto time_6{SteadyClock::now()};
3114 m_chainman.time_post_connect += time_6 - time_5;
3117 Ticks<MillisecondsDouble>(time_6 - time_5),
3118 Ticks<SecondsDouble>(
m_chainman.time_post_connect),
3121 Ticks<MillisecondsDouble>(time_6 - time_1),
3135 m_chainman.MaybeValidateSnapshot(*
this, current_cs);
3137 connectTrace.
BlockConnected(pindexNew, std::move(block_to_connect));
3162 bool fInvalidAncestor =
false;
3172 if (fFailedChain || fMissingData) {
3179 while (pindexTest != pindexFailed) {
3183 }
else if (fMissingData) {
3188 std::make_pair(pindexFailed->
pprev, pindexFailed));
3191 pindexFailed = pindexFailed->
pprev;
3194 fInvalidAncestor =
true;
3197 pindexTest = pindexTest->
pprev;
3199 if (!fInvalidAncestor)
3231 bool fBlocksDisconnected =
false;
3245 fBlocksDisconnected =
true;
3249 std::vector<CBlockIndex*> vpindexToConnect;
3250 bool fContinue =
true;
3255 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3256 vpindexToConnect.clear();
3257 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3260 vpindexToConnect.push_back(pindexIter);
3261 pindexIter = pindexIter->
pprev;
3266 for (
CBlockIndex* pindexConnect : vpindexToConnect | std::views::reverse) {
3267 if (!
ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
3274 fInvalidFound =
true;
3295 if (fBlocksDisconnected) {
3320 LogInfo(
"Leaving InitialBlockDownload (latching to false)");
3326 bool fNotify =
false;
3327 bool fInitialBlockDownload =
false;
3331 pindexHeader = m_best_header;
3333 if (pindexHeader != m_last_notified_header) {
3336 m_last_notified_header = pindexHeader;
3349 if (signals.CallbacksPending() > 10) {
3350 signals.SyncWithValidationInterfaceQueue();
3354 bool Chainstate::ActivateBestChain(
BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3379 bool exited_ibd{
false};
3393 LOCK(MempoolMutex());
3396 bool blocks_connected =
false;
3402 if (pindexMostWork ==
nullptr) {
3407 if (pindexMostWork ==
nullptr || pindexMostWork ==
m_chain.
Tip()) {
3411 bool fInvalidFound =
false;
3412 std::shared_ptr<const CBlock> nullBlockPtr;
3417 if (!
ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->
GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) {
3421 blocks_connected =
true;
3423 if (fInvalidFound) {
3425 pindexMostWork =
nullptr;
3430 assert(trace.pblock && trace.pindex);
3441 if (!blocks_connected)
return true;
3446 if (was_in_ibd && !still_in_ibd) {
3479 bool reached_target;
3496 if (reached_target) {
3515 }
while (pindexNewTip != pindexMostWork);
3550 return ActivateBestChain(state, std::shared_ptr<const CBlock>());
3560 if (pindex->
nHeight == 0)
return false;
3574 std::multimap<const arith_uint256, CBlockIndex*> highpow_outofchain_headers;
3578 for (
auto& entry :
m_blockman.m_block_index) {
3588 highpow_outofchain_headers.insert({candidate->
nChainWork, candidate});
3594 bool pindex_was_in_chain =
false;
3595 int disconnected = 0;
3607 LOCK(MempoolMutex());
3609 pindex_was_in_chain =
true;
3622 if (!
ret)
return false;
3624 assert(disconnected_tip->pprev == new_tip);
3639 auto candidate_it = highpow_outofchain_headers.lower_bound(new_tip->nChainWork);
3641 const bool best_header_needs_update{
m_chainman.m_best_header->GetAncestor(disconnected_tip->nHeight) == disconnected_tip};
3642 if (best_header_needs_update) {
3647 while (candidate_it != highpow_outofchain_headers.end()) {
3649 if (candidate->
GetAncestor(disconnected_tip->nHeight) == disconnected_tip) {
3655 candidate_it = highpow_outofchain_headers.erase(candidate_it);
3665 if (best_header_needs_update &&
3673 to_mark_failed = disconnected_tip;
3699 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3709 if (pindex_was_in_chain) {
3719 *to_mark_failed->
pprev,
3731 void Chainstate::SetBlockFailureFlags(
CBlockIndex* invalid_block)
3735 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3736 if (invalid_block != &block_index && block_index.GetAncestor(invalid_block->
nHeight) == invalid_block) {
3749 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3756 if (&block_index ==
m_chainman.m_best_invalid) {
3783 if (!target_block) {
3790 if (target_block->GetAncestor(pindex->
nHeight) == pindex) {
3800 pindexNew->
nTx = block.
vtx.size();
3806 auto prev_tx_sum = [](
CBlockIndex& block) {
return block.nTx + (block.pprev ? block.pprev->m_chain_tx_count : 0); };
3809 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",
3813 pindexNew->nFile = pos.
nFile;
3814 pindexNew->nDataPos = pos.
nPos;
3815 pindexNew->nUndoPos = 0;
3825 std::deque<CBlockIndex*> queue;
3826 queue.push_back(pindexNew);
3829 while (!queue.empty()) {
3837 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",
3842 for (
const auto& c : m_chainstates) {
3843 c->TryAddBlockIndexCandidate(pindex);
3845 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range =
m_blockman.
m_blocks_unlinked.equal_range(pindex);
3846 while (range.first != range.second) {
3847 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3848 queue.push_back(it->second);
3879 "hashMerkleRoot mismatch");
3888 "bad-txns-duplicate",
3889 "duplicate transaction");
3904 if (expect_witness_commitment) {
3909 assert(!block.
vtx.empty() && !block.
vtx[0]->vin.empty());
3910 const auto& witness_stack{block.
vtx[0]->vin[0].scriptWitness.stack};
3912 if (witness_stack.size() != 1 || witness_stack[0].size() != 32) {
3915 "bad-witness-nonce-size",
3916 strprintf(
"%s : invalid witness reserved value size", __func__));
3925 if (memcmp(hash_witness.
begin(), &block.
vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
3928 "bad-witness-merkle-match",
3929 strprintf(
"%s : witness merkle commitment mismatch", __func__));
3938 for (
const auto& tx : block.
vtx) {
3942 "unexpected-witness",
3943 strprintf(
"%s : unexpected witness data found", __func__));
3983 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase())
3985 for (
unsigned int i = 1; i < block.
vtx.size(); i++)
3986 if (block.
vtx[i]->IsCoinBase())
3991 for (
const auto& tx : block.
vtx) {
4003 unsigned int nSigOps = 0;
4004 for (
const auto& tx : block.
vtx)
4011 if (fCheckPOW && fCheckMerkleRoot)
4017 void ChainstateManager::UpdateUncommittedBlockStructures(
CBlock& block,
const CBlockIndex* pindexPrev)
const 4020 static const std::vector<unsigned char>
nonce(32, 0x00);
4023 tx.
vin[0].scriptWitness.stack.resize(1);
4024 tx.
vin[0].scriptWitness.stack[0] =
nonce;
4032 std::vector<unsigned char>
ret(32, 0x00);
4040 out.scriptPubKey[1] = 0x24;
4041 out.scriptPubKey[2] = 0xaa;
4042 out.scriptPubKey[3] = 0x21;
4043 out.scriptPubKey[4] = 0xa9;
4044 out.scriptPubKey[5] = 0xed;
4047 tx.vout.push_back(
out);
4050 UpdateUncommittedBlockStructures(block, pindexPrev);
4055 return std::ranges::all_of(headers,
4056 [&](
const auto& header) {
return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams); });
4067 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase()) {
4074 return std::any_of(block.
vtx.begin(), block.
vtx.end(),
4115 assert(pindexPrev !=
nullptr);
4163 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
4166 bool enforce_locktime_median_time_past{
false};
4168 assert(pindexPrev !=
nullptr);
4169 enforce_locktime_median_time_past =
true;
4172 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
4177 for (
const auto& tx : block.
vtx) {
4187 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4188 !std::equal(
expect.begin(),
expect.end(), block.
vtx[0]->vin[0].scriptSig.begin())) {
4224 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4226 if (miSelf !=
m_blockman.m_block_index.end()) {
4251 pindexPrev = &((*mi).second);
4261 if (!min_pow_checked) {
4296 blocks_left = std::max<int64_t>(0, blocks_left);
4297 const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};
4298 LogInfo(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
4316 if (now < m_last_presync_update + std::chrono::milliseconds{250})
return;
4317 m_last_presync_update = now;
4321 if (initial_download) {
4323 blocks_left = std::max<int64_t>(0, blocks_left);
4324 const double progress{100.0 * height / (height + blocks_left)};
4325 LogInfo(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
4332 const CBlock& block = *pblock;
4334 if (fNewBlock) *fNewBlock =
false;
4338 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
4340 bool accepted_header{
AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4343 if (!accepted_header)
4367 if (fAlreadyHave)
return true;
4369 if (pindex->
nTx != 0)
return true;
4370 if (!fHasMoreOrSameWork)
return true;
4371 if (fTooFarAhead)
return true;
4382 if (!
CheckBlock(block, state, params.GetConsensus()) ||
4398 if (fNewBlock) *fNewBlock =
true;
4406 if (blockPos.IsNull()) {
4407 state.
Error(
strprintf(
"%s: Failed to find position to write new block to disk", __func__));
4412 }
catch (
const std::runtime_error& e) {
4436 if (new_block) *new_block =
false;
4451 ret =
AcceptBlock(block, state, &pindex, force_processing,
nullptr, new_block, min_pow_checked);
4466 LogError(
"%s: ActivateBestChain failed (%s)\n", __func__, state.
ToString());
4472 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
4473 LogError(
"%s: [background] ActivateBestChain failed (%s)\n", __func__, bg_state.
ToString());
4498 const bool check_pow,
4499 const bool check_merkle_root)
4510 state.Invalid({},
"inconclusive-not-best-prevblk");
4551 index_dummy.pprev = tip;
4553 index_dummy.phashBlock = &block_hash;
4557 if(!chainstate.
ConnectBlock(block, state, &index_dummy, view_dummy,
true)) {
4572 if (!active_chainstate.FlushStateToDisk(
4585 if (tip && tip->GetBlockHash() == coins_cache.
GetBestBlock()) {
4601 assert(
cs->setBlockIndexCandidates.empty());
4609 target = target->pprev;
4612 LogInfo(
"Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f",
4613 tip->GetBlockHash().ToString(),
4619 if (!this->GetRole().historical) {
4647 int nCheckLevel,
int nCheckDepth)
4656 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
4659 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4660 LogInfo(
"Verifying last %i blocks at level %i", nCheckDepth, nCheckLevel);
4664 int nGoodTransactions = 0;
4667 bool skipped_no_block_data{
false};
4668 bool skipped_l3_checks{
false};
4669 LogInfo(
"Verification progress: 0%%");
4674 const int percentageDone = std::max(1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4675 if (reportDone < percentageDone / 10) {
4677 LogInfo(
"Verification progress: %d%%", percentageDone);
4678 reportDone = percentageDone / 10;
4687 LogInfo(
"Block verification stopping at height %d (no data). This could be due to pruning or use of an assumeutxo snapshot.", pindex->
nHeight);
4688 skipped_no_block_data =
true;
4698 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensus_params)) {
4699 LogError(
"Verification error: found bad block at %d, hash=%s (%s)",
4704 if (nCheckLevel >= 2 && pindex) {
4716 if (nCheckLevel >= 3) {
4725 nGoodTransactions = 0;
4726 pindexFailure = pindex;
4728 nGoodTransactions += block.
vtx.size();
4731 skipped_l3_checks =
true;
4736 if (pindexFailure) {
4737 LogError(
"Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)", chainstate.
m_chain.
Height() - pindexFailure->
nHeight + 1, nGoodTransactions);
4740 if (skipped_l3_checks) {
4741 LogWarning(
"Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.");
4748 if (nCheckLevel >= 4 && !skipped_l3_checks) {
4750 const int percentageDone = std::max(1, std::min(99, 100 - (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (double)nCheckDepth * 50)));
4751 if (reportDone < percentageDone / 10) {
4753 LogInfo(
"Verification progress: %d%%", percentageDone);
4754 reportDone = percentageDone / 10;
4763 if (!chainstate.
ConnectBlock(block, state, pindex, coins)) {
4771 LogInfo(
"Verification: No coin database inconsistencies in last %i blocks (%i transactions)", block_count, nGoodTransactions);
4773 if (skipped_l3_checks) {
4776 if (skipped_no_block_data) {
4794 if (!tx->IsCoinBase()) {
4795 for (
const CTxIn &txin : tx->vin) {
4813 if (hashHeads.empty())
return true;
4814 if (hashHeads.size() != 2) {
4815 LogError(
"ReplayBlocks(): unknown inconsistent state\n");
4826 if (!
m_blockman.m_block_index.contains(hashHeads[0])) {
4827 LogError(
"ReplayBlocks(): reorganization to unknown block requested\n");
4830 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
4832 if (!hashHeads[1].IsNull()) {
4833 if (!
m_blockman.m_block_index.contains(hashHeads[1])) {
4834 LogError(
"ReplayBlocks(): reorganization from unknown block requested\n");
4837 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
4839 assert(pindexFork !=
nullptr);
4843 const int nForkHeight{pindexFork ? pindexFork->
nHeight : 0};
4844 if (pindexOld != pindexFork) {
4846 while (pindexOld != pindexFork) {
4853 if (pindexOld->
nHeight % 10
'000 == 0) { 4854 LogInfo("Rolling back %s (%i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight); 4856 DisconnectResult res = DisconnectBlock(block, pindexOld, cache); 4857 if (res == DISCONNECT_FAILED) { 4858 LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString()); 4861 // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was 4862 // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations 4863 // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations, 4864 // the result is still a version of the UTXO set with the effects of that block undone. 4866 pindexOld = pindexOld->pprev; 4868 LogInfo("Rolled back to %s", pindexFork->GetBlockHash().ToString()); 4871 // Roll forward from the forking point to the new tip. 4872 if (nForkHeight < pindexNew->nHeight) { 4873 LogInfo("Rolling forward to %s (%i to %i)", pindexNew->GetBlockHash().ToString(), nForkHeight, pindexNew->nHeight); 4874 for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) { 4875 const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))}; 4877 if (nHeight % 10'000 == 0) {
4904 block = block->pprev;
4910 void Chainstate::ClearBlockIndexCandidates()
4916 void Chainstate::PopulateBlockIndexCandidates()
4924 if (pindex == SnapshotBase() || pindex == TargetBlock() ||
4938 if (!
ret)
return false;
4940 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
4942 std::vector<CBlockIndex*> vSortedByHeight{
m_blockman.GetAllBlockIndices()};
4943 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4947 if (m_interrupt)
return false;
4949 m_best_invalid = pindex;
4952 m_best_header = pindex;
4968 if (
m_blockman.m_block_index.contains(params.GenesisBlock().GetHash()))
4972 const CBlock& block = params.GenesisBlock();
4974 if (blockPos.IsNull()) {
4975 LogError(
"%s: writing genesis block to disk failed\n", __func__);
4980 }
catch (
const std::runtime_error& e) {
4981 LogError(
"%s: failed to write genesis block: %s\n", __func__, e.what());
4991 std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
4994 assert(!dbp == !blocks_with_unknown_parent);
4996 const auto start{SteadyClock::now()};
5004 uint64_t nRewind = blkdat.GetPos();
5005 while (!blkdat.eof()) {
5006 if (m_interrupt)
return;
5008 blkdat.SetPos(nRewind);
5011 unsigned int nSize = 0;
5015 blkdat.FindByte(std::byte(params.MessageStart()[0]));
5016 nRewind = blkdat.GetPos() + 1;
5018 if (buf != params.MessageStart()) {
5025 }
catch (
const std::exception&) {
5032 const uint64_t nBlockPos{blkdat.GetPos()};
5034 dbp->
nPos = nBlockPos;
5035 blkdat.SetLimit(nBlockPos + nSize);
5038 const uint256 hash{header.GetHash()};
5041 nRewind = nBlockPos + nSize;
5042 blkdat.SkipTo(nRewind);
5044 std::shared_ptr<CBlock> pblock{};
5051 header.hashPrevBlock.ToString());
5052 if (dbp && blocks_with_unknown_parent) {
5053 blocks_with_unknown_parent->emplace(header.hashPrevBlock, *dbp);
5062 blkdat.SetPos(nBlockPos);
5063 pblock = std::make_shared<CBlock>();
5065 nRewind = blkdat.GetPos();
5068 if (AcceptBlock(pblock, state,
nullptr,
true, dbp,
nullptr,
true)) {
5074 }
else if (hash != params.GetConsensus().hashGenesisBlock && pindex->
nHeight % 1000 == 0) {
5086 if (hash == params.GetConsensus().hashGenesisBlock &&
WITH_LOCK(::
cs_main,
return ActiveHeight()) == -1) {
5088 if (!ActiveChainstate().ActivateBestChain(state,
nullptr)) {
5109 if (!blocks_with_unknown_parent)
continue;
5112 std::deque<uint256> queue;
5113 queue.push_back(hash);
5114 while (!queue.empty()) {
5117 auto range = blocks_with_unknown_parent->equal_range(head);
5118 while (range.first != range.second) {
5119 std::multimap<uint256, FlatFilePos>::iterator it = range.first;
5120 std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5122 const auto& block_hash{pblockrecursive->GetHash()};
5126 if (AcceptBlock(pblockrecursive, dummy,
nullptr,
true, &it->second,
nullptr,
true)) {
5128 queue.push_back(block_hash);
5132 blocks_with_unknown_parent->erase(it);
5136 }
catch (
const std::exception& e) {
5148 LogDebug(
BCLog::REINDEX,
"%s: unexpected data at file offset 0x%x - %s. continuing\n", __func__, (nRewind - 1), e.what());
5151 }
catch (
const std::runtime_error& e) {
5152 GetNotifications().fatalError(
strprintf(
_(
"System error while loading external block file: %s"), e.what()));
5154 LogInfo(
"Loaded %i blocks from external file in %dms", nLoaded, Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
5160 if (!*
Assert(m_options.check_block_index))
return false;
5161 if (
FastRandomContext().randrange(*m_options.check_block_index) >= 1)
return false;
5167 if (!ShouldCheckBlockIndex()) {
5176 if (ActiveChain().Height() < 0) {
5189 best_hdr_chain.
SetTip(*m_best_header);
5191 std::multimap<const CBlockIndex*, const CBlockIndex*> forward;
5192 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
5194 if (!best_hdr_chain.
Contains(&block_index)) {
5196 assert(block_index.pprev);
5197 forward.emplace(block_index.pprev, &block_index);
5211 const CBlockIndex* pindexFirstNeverProcessed =
nullptr;
5212 const CBlockIndex* pindexFirstNotTreeValid =
nullptr;
5213 const CBlockIndex* pindexFirstNotTransactionsValid =
nullptr;
5214 const CBlockIndex* pindexFirstNotChainValid =
nullptr;
5215 const CBlockIndex* pindexFirstNotScriptsValid =
nullptr;
5221 const CBlockIndex* snap_base{CurrentChainstate().SnapshotBase()};
5222 const CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{}, *snap_first_nocv{}, *snap_first_nosv{};
5223 auto snap_update_firsts = [&] {
5224 if (pindex == snap_base) {
5225 std::swap(snap_first_missing, pindexFirstMissing);
5226 std::swap(snap_first_notx, pindexFirstNeverProcessed);
5227 std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
5228 std::swap(snap_first_nocv, pindexFirstNotChainValid);
5229 std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
5233 while (pindex !=
nullptr) {
5235 if (pindexFirstInvalid ==
nullptr && pindex->nStatus &
BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
5236 if (pindexFirstMissing ==
nullptr && !(pindex->nStatus &
BLOCK_HAVE_DATA)) {
5237 pindexFirstMissing = pindex;
5239 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) pindexFirstNeverProcessed = pindex;
5240 if (pindex->pprev !=
nullptr && pindexFirstNotTreeValid ==
nullptr && (pindex->nStatus &
BLOCK_VALID_MASK) <
BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
5242 if (pindex->pprev !=
nullptr) {
5243 if (pindexFirstNotTransactionsValid ==
nullptr &&
5245 pindexFirstNotTransactionsValid = pindex;
5248 if (pindexFirstNotChainValid ==
nullptr &&
5250 pindexFirstNotChainValid = pindex;
5253 if (pindexFirstNotScriptsValid ==
nullptr &&
5255 pindexFirstNotScriptsValid = pindex;
5260 if (pindex->pprev ==
nullptr) {
5262 assert(pindex->GetBlockHash() == GetConsensus().hashGenesisBlock);
5263 for (
const auto& c : m_chainstates) {
5264 if (c->m_chain.Genesis() !=
nullptr) {
5265 assert(pindex == c->m_chain.Genesis());
5277 assert(pindexFirstMissing == pindexFirstNeverProcessed);
5283 if (snap_base && snap_base->GetAncestor(pindex->nHeight) == pindex) {
5292 assert((pindexFirstNeverProcessed ==
nullptr || pindex == snap_base) == pindex->HaveNumChainTxs());
5293 assert((pindexFirstNotTransactionsValid ==
nullptr || pindex == snap_base) == pindex->HaveNumChainTxs());
5295 assert(pindex->pprev ==
nullptr || pindex->nChainWork >= pindex->pprev->nChainWork);
5297 assert(pindexFirstNotTreeValid ==
nullptr);
5301 if (pindexFirstInvalid ==
nullptr) {
5308 if (!pindex->pprev) {
5310 assert(pindex->m_chain_tx_count == pindex->nTx);
5311 }
else if (pindex->pprev->m_chain_tx_count > 0 && pindex->nTx > 0) {
5313 assert(pindex->m_chain_tx_count == pindex->nTx + pindex->pprev->m_chain_tx_count);
5317 assert((pindex->m_chain_tx_count != 0) == (pindex == snap_base));
5323 for (
const auto& c : m_chainstates) {
5324 if (c->m_chain.Tip() ==
nullptr)
continue;
5338 if (!
CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && (pindexFirstNeverProcessed ==
nullptr || pindex == snap_base)) {
5342 if (pindexFirstInvalid ==
nullptr) {
5361 if (pindexFirstMissing ==
nullptr || pindex == c->m_chain.Tip() || pindex == c->SnapshotBase()) {
5367 if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
5368 assert(c->setBlockIndexCandidates.contains(pindex));
5376 assert(!c->setBlockIndexCandidates.contains(pindex));
5381 bool foundInUnlinked =
false;
5382 while (rangeUnlinked.first != rangeUnlinked.second) {
5383 assert(rangeUnlinked.first->first == pindex->pprev);
5384 if (rangeUnlinked.first->second == pindex) {
5385 foundInUnlinked =
true;
5388 rangeUnlinked.first++;
5390 if (pindex->pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed !=
nullptr && pindexFirstInvalid ==
nullptr) {
5395 if (pindexFirstMissing ==
nullptr)
assert(!foundInUnlinked);
5396 if (pindex->pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed ==
nullptr && pindexFirstMissing !=
nullptr) {
5407 for (
const auto& c : m_chainstates) {
5409 if (pindexFirstInvalid ==
nullptr) {
5410 if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
5422 snap_update_firsts();
5423 auto range{forward.equal_range(pindex)};
5424 if (range.first != range.second) {
5426 pindex = range.first->second;
5429 }
else if (best_hdr_chain.
Contains(pindex)) {
5432 pindex = best_hdr_chain[
nHeight];
5440 snap_update_firsts();
5442 if (pindex == pindexFirstInvalid) pindexFirstInvalid =
nullptr;
5443 if (pindex == pindexFirstMissing) pindexFirstMissing =
nullptr;
5444 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed =
nullptr;
5445 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid =
nullptr;
5446 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid =
nullptr;
5447 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid =
nullptr;
5448 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid =
nullptr;
5452 auto rangePar{forward.equal_range(pindexPar)};
5453 while (rangePar.first->second != pindex) {
5454 assert(rangePar.first != rangePar.second);
5459 if (rangePar.first != rangePar.second) {
5461 pindex = rangePar.first->second;
5463 }
else if (pindexPar == best_hdr_chain[
nHeight - 1]) {
5465 pindex = best_hdr_chain[
nHeight];
5467 assert((pindex ==
nullptr) == (pindexPar == best_hdr_chain.
Tip()));
5479 assert(nNodes == forward.size() + best_hdr_chain.
Height() + 1);
5486 return strprintf(
"Chainstate [%s] @ height %d (%s)",
5491 bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size)
5504 LogInfo(
"[%s] resized coinsdb cache to %.1f MiB",
5505 this->
ToString(), coinsdb_size * (1.0 / 1024 / 1024));
5506 LogInfo(
"[%s] resized coinstip cache to %.1f MiB",
5507 this->
ToString(), coinstip_size * (1.0 / 1024 / 1024));
5512 if (coinstip_size > old_coinstip_size) {
5526 if (pindex ==
nullptr) {
5535 const int64_t nNow{TicksSinceEpoch<std::chrono::seconds>(
NodeClock::now())};
5536 const auto block_time{
5537 (
Assume(m_best_header) && std::abs(nNow - pindex->
GetBlockTime()) <= Ticks<std::chrono::seconds>(2h) &&
5544 nNow - (m_best_header->nHeight - pindex->
nHeight) * GetConsensus().nPowTargetSpacing :
5551 fTxTotal =
data.tx_count + (nNow -
data.nTime) *
data.dTxRate;
5562 assert(m_chainstates.empty());
5563 m_chainstates.emplace_back(std::make_unique<Chainstate>(mempool,
m_blockman, *
this));
5564 return *m_chainstates.back();
5576 bool existed = fs::remove(base_blockhash_path);
5578 LogWarning(
"[snapshot] snapshot chainstate dir being removed lacks %s file",
5581 }
catch (
const fs::filesystem_error& e) {
5582 LogWarning(
"[snapshot] failed to remove file %s: %s\n",
5588 LogInfo(
"Removing leveldb dir at %s\n", path_str);
5592 const bool destroyed =
DestroyDB(path_str);
5595 LogError(
"leveldb DestroyDB call failed on %s", path_str);
5622 if (!GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
5623 auto available_heights = GetParams().GetAvailableSnapshotHeights();
5624 std::string heights_formatted =
util::Join(available_heights,
", ", [&](
const auto& i) {
return util::ToString(i); });
5625 return util::Error{
Untranslated(
strprintf(
"assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s",
5627 heights_formatted))};
5631 if (!snapshot_start_block) {
5632 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",
5637 if (start_block_invalid) {
5641 if (!m_best_header || m_best_header->GetAncestor(snapshot_start_block->nHeight) != snapshot_start_block) {
5642 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.")};
5645 auto mempool{CurrentChainstate().GetMempool()};
5646 if (mempool && mempool->
size() > 0) {
5651 int64_t current_coinsdb_cache_size{0};
5652 int64_t current_coinstip_cache_size{0};
5660 static constexpr
double IBD_CACHE_PERC = 0.01;
5661 static constexpr
double SNAPSHOT_CACHE_PERC = 0.99;
5673 current_coinsdb_cache_size = this->ActiveChainstate().m_coinsdb_cache_size_bytes;
5674 current_coinstip_cache_size = this->ActiveChainstate().m_coinstip_cache_size_bytes;
5678 this->ActiveChainstate().ResizeCoinsCaches(
5679 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
5680 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
5684 return std::make_unique<Chainstate>(
5685 nullptr,
m_blockman, *
this, base_blockhash));
5689 snapshot_chainstate->InitCoinsDB(
5690 static_cast<size_t>(current_coinsdb_cache_size * SNAPSHOT_CACHE_PERC),
5692 snapshot_chainstate->InitCoinsCache(
5693 static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
5697 this->MaybeRebalanceCaches();
5705 snapshot_chainstate.reset();
5708 GetNotifications().fatalError(
strprintf(
_(
"Failed to remove snapshot chainstate dir (%s). " 5709 "Manually remove it before restarting.\n"), fs::PathToString(*snapshot_datadir)));
5715 if (
auto res{this->PopulateAndValidateSnapshot(*snapshot_chainstate, coins_file, metadata)}; !res) {
5726 return cleanup_bad_snapshot(
Untranslated(
"work does not exceed active chainstate"));
5732 return cleanup_bad_snapshot(
Untranslated(
"could not write base blockhash"));
5736 Chainstate& chainstate{AddChainstate(std::move(snapshot_chainstate))};
5739 chainstate.PopulateBlockIndexCandidates();
5741 LogInfo(
"[snapshot] successfully activated snapshot %s", base_blockhash.
ToString());
5742 LogInfo(
"[snapshot] (%.2f MB)",
5743 chainstate.CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
5745 this->MaybeRebalanceCaches();
5746 return snapshot_start_block;
5753 snapshot_loaded ?
"saving snapshot chainstate" :
"flushing coins cache",
5757 coins_cache.
Flush();
5762 const char*
what() const noexcept
override 5764 return "ComputeUTXOStats interrupted.";
5786 if (!snapshot_start_block) {
5790 base_blockhash.ToString()))};
5793 int base_height = snapshot_start_block->
nHeight;
5794 const auto& maybe_au_data = GetParams().AssumeutxoForHeight(base_height);
5796 if (!maybe_au_data) {
5798 "(%d) - refusing to load snapshot", base_height))};
5813 LogInfo(
"[snapshot] loading %d coins from snapshot %s", coins_left, base_blockhash.ToString());
5814 int64_t coins_processed{0};
5816 while (coins_left > 0) {
5820 size_t coins_per_txid{0};
5823 if (coins_per_txid > coins_left) {
5827 for (
size_t i = 0; i < coins_per_txid; i++) {
5831 outpoint.
hash = txid;
5833 if (coin.nHeight > base_height ||
5834 outpoint.
n >= std::numeric_limits<decltype(outpoint.
n)>::max()
5837 coins_count - coins_left))};
5841 coins_count - coins_left))};
5848 if (coins_processed % 1000000 == 0) {
5849 LogInfo(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)",
5851 static_cast<float>(coins_processed) * 100 / static_cast<float>(coins_count),
5859 if (coins_processed % 120000 == 0) {
5865 return snapshot_chainstate.GetCoinsCacheSizeState());
5878 }
catch (
const std::ios_base::failure&) {
5891 bool out_of_coins{
false};
5893 std::byte left_over_byte;
5894 coins_file >> left_over_byte;
5895 }
catch (
const std::ios_base::failure&) {
5897 out_of_coins =
true;
5899 if (!out_of_coins) {
5904 LogInfo(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s",
5907 base_blockhash.ToString());
5918 std::optional<CCoinsStats> maybe_stats;
5926 if (!maybe_stats.has_value()) {
5947 constexpr
int AFTER_GENESIS_START{1};
5949 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height(); ++i) {
5950 index = snapshot_chainstate.
m_chain[i];
5967 assert(index == snapshot_start_block);
5970 LogInfo(
"[snapshot] validated snapshot (%.2f MB)",
5997 !validated_cs.m_target_blockhash ||
6008 "%s failed to validate the -assumeutxo snapshot state. " 6009 "This indicates a hardware problem, or a bug in the software, or a " 6010 "bad software modification that allowed an invalid snapshot to be " 6011 "loaded. As a result of this, the node will shut down and stop using any " 6012 "state that was built on the snapshot, resetting the chain height " 6013 "from %d to %d. On the next " 6014 "restart, the node will resume syncing from %d " 6015 "without using any snapshot data. " 6016 "Please report this incident to %s, including how you obtained the snapshot. " 6017 "The invalid snapshot chainstate will be left on disk in case it is " 6018 "helpful in diagnosing the issue that caused this error."),
6024 LogError(
"[snapshot] deleting snapshot, reverting to validated chain, and stopping node\n");
6027 validated_cs.SetTargetBlock(
nullptr);
6031 auto rename_result = unvalidated_cs.InvalidateCoinsDBOnDisk();
6032 if (!rename_result) {
6036 GetNotifications().fatalError(user_error);
6042 const auto& maybe_au_data = m_options.chainparams.AssumeutxoForHeight(validated_cs.
m_chain.
Height());
6043 if (!maybe_au_data) {
6044 LogWarning(
"[snapshot] assumeutxo data not found for height " 6045 "(%d) - refusing to validate snapshot", validated_cs.
m_chain.
Height());
6046 handle_invalid_snapshot();
6051 std::optional<CCoinsStats> validated_cs_stats;
6052 LogInfo(
"[snapshot] computing UTXO stats for background chainstate to validate " 6053 "snapshot - this could take a few minutes");
6056 CoinStatsHashType::HASH_SERIALIZED,
6057 &validated_coins_db,
6065 if (!validated_cs_stats) {
6066 LogWarning(
"[snapshot] failed to generate stats for validation coins db");
6070 handle_invalid_snapshot();
6081 LogWarning(
"[snapshot] hash mismatch: actual=%s, expected=%s",
6082 validated_cs_stats->hashSerialized.ToString(),
6084 handle_invalid_snapshot();
6088 LogInfo(
"[snapshot] snapshot beginning at %s has been fully validated",
6092 validated_cs.m_target_utxohash =
AssumeutxoHash{validated_cs_stats->hashSerialized};
6093 this->MaybeRebalanceCaches();
6101 return CurrentChainstate();
6104 void ChainstateManager::MaybeRebalanceCaches()
6108 Chainstate* historical_cs{HistoricalChainstate()};
6109 if (!historical_cs && !current_cs.m_from_snapshot_blockhash) {
6112 current_cs.ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache);
6113 }
else if (!historical_cs) {
6115 LogInfo(
"[snapshot] allocating all cache to the snapshot chainstate");
6117 current_cs.ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache);
6122 if (IsInitialBlockDownload()) {
6123 historical_cs->ResizeCoinsCaches(
6124 m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05);
6125 current_cs.ResizeCoinsCaches(
6126 m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95);
6128 current_cs.ResizeCoinsCaches(
6129 m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05);
6130 historical_cs->ResizeCoinsCaches(
6131 m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95);
6136 void ChainstateManager::ResetChainstates()
6138 m_chainstates.clear();
6148 if (!opts.check_block_index.has_value()) opts.
check_block_index = opts.chainparams.DefaultConsistencyChecks();
6149 if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work =
UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork);
6150 if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
6151 return std::move(opts);
6156 m_interrupt{interrupt},
6157 m_options{
Flatten(std::move(options))},
6158 m_blockman{interrupt, std::move(blockman_options)},
6170 Chainstate* ChainstateManager::LoadAssumeutxoChainstate()
6178 if (!base_blockhash) {
6181 LogInfo(
"[snapshot] detected active snapshot chainstate (%s) - loading",
6182 fs::PathToString(*path));
6184 auto snapshot_chainstate{std::make_unique<Chainstate>(
nullptr,
m_blockman, *
this, base_blockhash)};
6185 LogInfo(
"[snapshot] switching active chainstate to %s", snapshot_chainstate->ToString());
6186 return &this->AddChainstate(std::move(snapshot_chainstate));
6189 Chainstate& ChainstateManager::AddChainstate(std::unique_ptr<Chainstate> chainstate)
6194 assert(!prev_chainstate.m_target_blockhash);
6195 prev_chainstate.m_target_blockhash = chainstate->m_from_snapshot_blockhash;
6196 m_chainstates.push_back(std::move(chainstate));
6198 assert(&curr_chainstate == m_chainstates.back().get());
6202 assert(!prev_chainstate.m_mempool || prev_chainstate.m_mempool->size() == 0);
6203 assert(!curr_chainstate.m_mempool);
6204 std::swap(curr_chainstate.m_mempool, prev_chainstate.m_mempool);
6205 return curr_chainstate;
6210 return (block_index.
nHeight==91842 && block_index.
GetBlockHash() ==
uint256{
"00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec"}) ||
6211 (block_index.
nHeight==91880 && block_index.
GetBlockHash() ==
uint256{
"00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721"});
6216 return (block_height==91722 && block_hash ==
uint256{
"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
6217 (block_height==91812 && block_hash ==
uint256{
"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"});
6229 const fs::path invalid_path{db_path +
"_INVALID"};
6232 LogInfo(
"[snapshot] renaming snapshot datadir %s to %s", db_path_str, invalid_path_str);
6238 fs::rename(db_path, invalid_path);
6239 }
catch (
const fs::filesystem_error& e) {
6240 LogError(
"While invalidating the coins db: Error renaming file '%s' -> '%s': %s",
6241 db_path_str, invalid_path_str, e.what());
6243 "Rename of '%s' -> '%s' failed. " 6244 "You should resolve this by manually moving or deleting the invalid " 6245 "snapshot directory %s, otherwise you will encounter the same error again " 6246 "on the next startup."),
6247 db_path_str, invalid_path_str, db_path_str)};
6252 bool ChainstateManager::DeleteChainstate(
Chainstate& chainstate)
6258 LogError(
"Deletion of %s failed. Please remove it manually to continue reindexing.",
6259 fs::PathToString(db_path));
6264 assert(prev_chainstate->m_mempool->size() == 0);
6265 assert(!curr_chainstate.m_mempool);
6266 std::swap(curr_chainstate.m_mempool, prev_chainstate->m_mempool);
6275 void ChainstateManager::RecalculateBestHeader()
6279 for (
auto& entry :
m_blockman.m_block_index) {
6280 if (!(entry.second.nStatus &
BLOCK_FAILED_VALID) && m_best_header->nChainWork < entry.second.nChainWork) {
6281 m_best_header = &entry.second;
6286 std::optional<int> ChainstateManager::BlocksAheadOfTip()
const 6292 if (best_header && tip && best_header->nChainWork > tip->
nChainWork &&
6293 best_header->GetAncestor(tip->
nHeight) == tip) {
6296 return std::nullopt;
6299 bool ChainstateManager::ValidatedSnapshotCleanup(
Chainstate& validated_cs,
Chainstate& unvalidated_cs)
6309 const fs::path delete_path{validated_path +
"_todelete"};
6317 this->ResetChainstates();
6318 assert(this->m_chainstates.size() == 0);
6320 LogInfo(
"[snapshot] deleting background chainstate directory (now unnecessary) (%s)",
6321 fs::PathToString(validated_path));
6323 auto rename_failed_abort = [
this](
6326 const fs::filesystem_error& err) {
6327 LogError(
"[snapshot] Error renaming path (%s) -> (%s): %s\n",
6328 fs::PathToString(p_old), fs::PathToString(p_new), err.what());
6330 "Rename of '%s' -> '%s' failed. " 6331 "Cannot clean up the background chainstate leveldb directory."),
6332 fs::PathToString(p_old), fs::PathToString(p_new)));
6336 fs::rename(validated_path, delete_path);
6337 }
catch (
const fs::filesystem_error& e) {
6338 rename_failed_abort(validated_path, delete_path, e);
6342 LogInfo(
"[snapshot] moving snapshot chainstate (%s) to " 6343 "default chainstate directory (%s)",
6344 fs::PathToString(assumed_valid_path), fs::PathToString(validated_path));
6347 fs::rename(assumed_valid_path, validated_path);
6348 }
catch (
const fs::filesystem_error& e) {
6349 rename_failed_abort(assumed_valid_path, validated_path, e);
6356 LogWarning(
"Deletion of %s failed. Please remove it manually, as the " 6357 "directory is now unnecessary.",
6358 fs::PathToString(delete_path));
6360 LogInfo(
"[snapshot] deleted background chainstate directory (%s)",
6361 fs::PathToString(validated_path));
6366 std::pair<int, int> Chainstate::GetPruneRange(
int last_height_can_prune)
const 6377 prune_start =
Assert(SnapshotBase())->nHeight + 1;
6380 int max_prune = std::max<int>(
6389 int prune_end = std::min(last_height_can_prune, max_prune);
6391 return {prune_start, prune_end};
6394 std::optional<std::pair<const CBlockIndex*, const CBlockIndex*>> ChainstateManager::GetHistoricalBlockRange()
const 6397 if (!chainstate)
return {};
6398 return std::make_pair(chainstate->
m_chain.
Tip(), chainstate->TargetBlock());
6407 std::vector<Chainstate*> chainstates;
6410 chainstates.reserve(m_chainstates.size());
6411 for (
const auto& chainstate : m_chainstates) {
6412 if (chainstate && chainstate->m_assumeutxo !=
Assumeutxo::INVALID && !chainstate->m_target_utxohash) {
6413 chainstates.push_back(chainstate.get());
6419 if (!chainstate->ActivateBestChain(state,
nullptr)) {
std::shared_ptr< const CTransaction > CTransactionRef
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
static constexpr auto DATABASE_WRITE_INTERVAL_MAX
CoinsViewOptions coins_view
bool ConnectTip(BlockValidationState &state, CBlockIndex *pindexNew, std::shared_ptr< const CBlock > block_to_connect, ConnectTrace &connectTrace, DisconnectedBlockTransactions &disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
std::string GetHex() const
CSHA256 & Write(const unsigned char *data, size_t len)
bool SpendsNonAnchorWitnessProg(const CTransaction &tx, const CCoinsViewCache &prevouts)
Check whether this transaction spends any witness program but P2A, including not-yet-defined ones...
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
bool IsSpent() const
Either this coin never existed (see e.g.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from. ...
is a home for simple enum and struct type definitions that can be used internally by functions in the...
const uint256 & AssumedValidBlock() const
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
virtual void warningUnset(Warning id)
fs::path path
Location in the filesystem where leveldb data will be stored.
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
std::vector< Coin > vprevout
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
static time_point now() noexcept
Return current system time or mocked time, if set.
static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE
Maximum age of our tip for us to be considered current for fee estimation.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
script_verify_flags m_flags
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
The assumeutxo snapshot failed validation.
invalid by consensus rules
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...
static bool CheckWitnessMalleation(const CBlock &block, bool expect_witness_commitment, BlockValidationState &state)
CheckWitnessMalleation performs checks for block malleation with regard to its witnesses.
bool HaveNumChainTxs() const
Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot ...
SignatureCache m_signature_cache
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool CheckInputScripts(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, script_verify_flags 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.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted...
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
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.
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void UpdateBlockInfo(const CBlock &block, unsigned int nHeight, const FlatFilePos &pos)
Update blockfile info while processing a block during reindex.
bool Error(const std::string &reject_reason)
SynchronizationState
Current sync state passed to tip changed callbacks.
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 const int WITNESS_SCALE_FACTOR
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 IsPruneMode() const
Whether running in -prune mode.
std::string GetDebugMessage() const
std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries that have as much work as our current tip or more, and transaction data needed to be validated (with BLOCK_VALID_TRANSACTIONS for each block and its parents back to the genesis block or an assumeutxo snapshot block).
constexpr int64_t count_seconds(std::chrono::seconds t)
Convenience class for initializing and passing the script execution cache and signature cache...
int64_t GetBlockTime() const
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
enum ScriptError_t ScriptError
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::optional< std::string > EntriesAndTxidsDisjoint(const CTxMemPool::setEntries &ancestors, const std::set< Txid > &direct_conflicts, const Txid &txid)
Check the intersection between two sets of transactions (a set of mempool entries and a set of txids)...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
uint256 GetPackageHash(const std::vector< CTransactionRef > &transactions)
Get the hash of the concatenated wtxids of transactions, with wtxids treated as a little-endian numbe...
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
ValidationSignals * signals
The package itself is invalid (e.g. too many transactions).
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances...
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...
We don't have the previous block the checked one is built on.
RecursiveMutex cs_LastBlockFile
The cache is at >= 90% capacity.
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
std::optional< std::string > PackageTRUCChecks(const CTxMemPool &pool, const CTransactionRef &ptx, int64_t vsize, const Package &package, const std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &mempool_parents)
Must be called for every transaction that is submitted within a package, even if not TRUC...
An in-memory indexed chain of blocks.
bool ShouldCheckBlockIndex() const
size_t DynamicMemoryUsage() const
bool DestroyDB(const std::string &path_str)
invalid proof of work or time too old
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
VerifyDBResult VerifyDB(Chainstate &chainstate, const Consensus::Params &consensus_params, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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...
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
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...
Information about chainstate that notifications are sent from.
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
transaction was missing some of its inputs
int64_t GetTime()
DEPRECATED, see GetTime.
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(util::Result< CBlockIndex * ActivateSnapshot)(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Instantiate a new chainstate.
All parent headers found, difficulty matches, timestamp >= median previous.
bool MoneyRange(const CAmount &nValue)
int Height() const
Return the maximal height in the chain.
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
std::unordered_map< uint256, CBlockIndex, BlockHasher > BlockMap
CTxOut out
unspent transaction output
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache &versionbitscache)
Determine if a deployment is active for the next block.
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...
stage after last reached validness failed
unsigned long size() const
void GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev) const
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
constexpr const std::byte * begin() const
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx)
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
unsigned int fCoinBase
whether containing transaction was a coinbase
static constexpr int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
The coins cache is in immediate need of a flush.
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule) ...
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
static constexpr int32_t SEQ_ID_BEST_CHAIN_FROM_DISK
Init values for CBlockIndex nSequenceId when loaded from disk.
bool IsInterrupted(const T &result)
std::vector< CTxOut > m_spent_outputs
std::set< txiter, CompareIteratorByHash > setEntries
bool CheckBlockDataAvailability(const CBlockIndex &upper_block, const CBlockIndex &lower_block, BlockStatus block_status=BLOCK_HAVE_DATA) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex &GetFirstBlock(const CBlockIndex &upper_block LIFETIMEBOUND, uint32_t status_mask, const CBlockIndex *lower_block LIFETIMEBOUND=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned)
Check if all blocks in the [upper_block, lower_block] range have data available as defined by the sta...
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
static bool IsCurrentForFeeEstimation(Chainstate &active_chainstate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
PerBlockConnectTrace()=default
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
User-controlled performance and debug options.
violated mempool's fee/size/descendant/RBF/etc limits
the block header may be on a too-little-work chain
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.
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
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...
inputs (covered by txid) failed policy rules
undo data available in rev*.dat
memcpy(result.begin(), stream.data(), stream.size())
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
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...
const ResultType m_result_type
Result type.
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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...
TRACEPOINT_SEMAPHORE(validation, block_connected)
transaction spends a coinbase too early, or violates locktime/sequence locks
void LoadExternalBlockFile(AutoFile &file_in, FlatFilePos *dbp=nullptr, std::multimap< uint256, FlatFilePos > *blocks_with_unknown_parent=nullptr)
Import blocks from an external file.
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, script_verify_flags flags)
Compute total signature operation cost of a transaction.
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction. ...
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 IsInitialBlockDownload() const noexcept
Check whether we are doing an initial block download (synchronizing from disk or network) ...
CChain m_chain
The current chain of blockheaders we consult and build on.
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
consteval auto _(util::TranslatedLiteral str)
util::Result< void > PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
static const int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule) ...
bool DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache &versionbitscache)
Determine if a deployment is active for this block.
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
const char * what() const noexcept override
Non-refcounted RAII wrapper for FILE*.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
void ForceFlushStateToDisk(bool wipe_cache=true)
Flush all changes to disk.
static void LimitMempoolSize(CTxMemPool &pool, CCoinsViewCache &coins_cache) EXCLUSIVE_LOCKS_REQUIRED(
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'...
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
const std::vector< CTxIn > vin
const util::SignalInterrupt & m_interrupt
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
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...
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, script_verify_flags 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...
CVerifyDB(kernel::Notifications ¬ifications)
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.
arith_uint256 UintToArith256(const uint256 &a)
VersionBitsCache m_versionbitscache
Track versionbit status.
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
constexpr unsigned char * begin()
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
bool validated
Whether this is a notification from a chainstate that's been fully validated starting from the genesi...
std::function< void()> snapshot_download_completed
Function to restart active indexes; set dynamically to avoid a circular dependency on base/index...
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
int nSubsidyHalvingInterval
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.
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.
void UpdateIBDStatus() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update and possibly latch the IBD status.
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...
int64_t CAmount
Amount in satoshis (Can be negative)
void SetBestBlock(const uint256 &hashBlock)
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned...
void ChainStateFlushed(const kernel::ChainstateRole &, const CBlockLocator &)
void BlockChecked(const std::shared_ptr< const CBlock > &, const BlockValidationState &)
std::array< uint8_t, 4 > MessageStartChars
uint256 GetBlockHash() const
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs.
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 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.
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
unsigned int GetCacheSize() const
Size of the cache (in number of transaction outputs)
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0...
std::function< FILE *(const fs::path &, const char *)> FopenFn
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).
arith_uint256 GetBlockProof(const CBlockIndex &block)
Compute how much work a block index entry corresponds to.
AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
bool IsBIP30Repeat(const CBlockIndex &block_index)
Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) ...
static bool exists(const path &p)
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(
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
void 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.
static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt &interrupt)
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
std::optional< std::string > PaysForRBF(CAmount original_fees, CAmount replacement_fees, size_t replacement_vsize, CFeeRate relay_fee, const Txid &txid)
The replacement transaction must pay more fees than the original transactions.
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
constexpr std::array FlushStateModeNames
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty...
void ReportHeadersPresync(int64_t height, int64_t timestamp)
This is used by net_processing to report pre-synchronization progress of headers, as headers are not ...
Blocks after an assumeutxo snapshot have been validated but the snapshot itself has not been validate...
Chainstate stores and provides an API to update our local knowledge of the current best chain...
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
std::optional< std::pair< std::string, CTransactionRef > > SingleTRUCChecks(const CTxMemPool &pool, const CTransactionRef &ptx, const std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &mempool_parents, const std::set< Txid > &direct_conflicts, int64_t vsize)
Must be called for every transaction, even if not TRUC.
static void LimitValidationInterfaceQueue(ValidationSignals &signals) LOCKS_EXCLUDED(cs_main)
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
constexpr int64_t LargeCoinsCacheThreshold(int64_t total_space) noexcept
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
Abstract view on the open txout dataset.
std::map< uint256, script_verify_flags > script_flag_exceptions
Hashes of blocks that.
this block was cached as being invalid and we didn't store the reason why
Validation result for package mempool acceptance.
std::unique_ptr< Chainstate > RemoveChainstate(Chainstate &chainstate) EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Remove a chainstate.
An input of a transaction.
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.
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block. ...
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
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...
Removed for reorganization.
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).
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
void Flush(bool reallocate_cache=true)
Push the modifications applied to this cache to its base and wipe local state.
std::string ToString() const
ValidationCache(size_t script_execution_cache_bytes, size_t signature_cache_bytes)
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
void ActiveTipChange(const CBlockIndex &, bool)
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex...
void Finalize(std::span< unsigned char > output)
double GuessVerificationProgress(const CBlockIndex *pindex) const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< uint256 > from_snapshot_blockhash=std::nullopt)
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
virtual InterruptResult blockTip(SynchronizationState state, const CBlockIndex &index, double verification_progress)
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.
kernel::Notifications & GetNotifications() const
const kernel::BlockManagerOpts m_opts
std::optional< std::pair< ScriptError, std::string > > operator()()
std::string ScriptErrorString(const ScriptError serror)
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...
bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Whether the chain state needs to be redownloaded due to lack of witness data.
ChainstateManager(const util::SignalInterrupt &interrupt, Options options, node::BlockManager::Options blockman_options)
Holds various statistics on transactions within a chain.
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...
void Finalize(unsigned char hash[OUTPUT_SIZE])
bool LoadingBlocks() const
const std::vector< CTxOut > vout
uint64_t GetSerializeSize(const T &t)
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Remove any entries that are in this block.
bool signet_blocks
If true, witness commitments contain a payload equal to a Bitcoin Script solution to the signet chall...
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
std::string ToString() const
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp)
Test whether the LockPoints height and time are still valid on the current chain. ...
const Consensus::Params & GetConsensus() const
arith_uint256 CalculateClaimedHeadersWork(std::span< const CBlockHeader > headers)
Return the sum of the claimed work on a given set of headers.
ResetGuard CreateResetGuard() noexcept
Create a scoped guard that will call Reset() on this cache when it goes out of scope.
std::shared_ptr< const CBlock > pblock
SnapshotCompletionResult MaybeValidateSnapshot(Chainstate &validated_cs, Chainstate &unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(Chainstate & CurrentChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Try to validate an assumeutxo snapshot by using a validated historical chainstate targeted at the sna...
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, ConnectTrace &connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Return the [start, end] (inclusive) of block heights we can prune.
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
CTxMemPool::txiter TxHandle
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
Every block in the chain has been validated.
std::atomic_bool m_blockfiles_indexed
Whether all blockfiles have been added to the block tree database.
static constexpr script_verify_flags STANDARD_NOT_MANDATORY_VERIFY_FLAGS
For convenience, standard but not mandatory verify flags.
static constexpr script_verify_flags STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
An output of a transaction.
virtual void warningSet(Warning id, const bilingual_str &message)
std::string ToString() const
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
std::vector< uint256 > vHave
At least one tx is invalid.
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
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...
Parameters that influence chain consensus.
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.
An outpoint - a combination of a transaction hash and an index n into its vout.
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
A base class defining functions for notifying about certain kernel events.
void TryAddBlockIndexCandidate(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Add a block to the candidate set if it has as much work as the current tip.
void AddTransactionsUpdated(unsigned int n)
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
constexpr bool IsNull() const
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...
fails some policy, but might be acceptable if submitted in a (different) package
DisconnectedBlockTransactions.
Validation result for a transaction evaluated by MemPoolAccept (single or package).
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool CheckEphemeralSpends(const Package &package, CFeeRate dust_relay_rate, const CTxMemPool &tx_pool, TxValidationState &out_child_state, Wtxid &out_child_wtxid)
Called for each transaction(package) if any dust is in the package.
static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define LogPrintLevel_(category, level, should_ratelimit,...)
#define Assume(val)
Assume is the identity function.
static MempoolAcceptResult MempoolTxDifferentWitness(const Wtxid &other_wtxid)
256-bit unsigned big integer.
int64_t GetMedianTimePast() const
block data in blk*.dat was received with a witness-enforcing client
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
std::string FormatFullVersion()
void BlockConnected(CBlockIndex *pindex, std::shared_ptr< const CBlock > pblock)
bool exists(const Txid &txid) const
std::chrono::seconds PowTargetSpacing() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
Closure representing one script verification Note that this stores references to the spending transac...
ValidationCache m_validation_cache
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 CTransactionRef MakeTransactionRef(Tx &&txIn)
void insert(Element e)
insert loops at most depth_limit times trying to insert a hash at various locations in the table via ...
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW=true)
int64_t DifficultyAdjustmentInterval() const
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
script_verify_flags GetBlockScriptFlags(const CBlockIndex &block_index, const ChainstateManager &chainman)
void InvalidateBlock(ChainstateManager &chainman, const uint256 block_hash)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Transaction is missing a witness.
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
uint256 BlockWitnessMerkleRoot(const CBlock &block)
int32_t nVersion
block header
const CChainParams & GetParams() const
#define LogDebug(category,...)
bool IsTipRecent(const arith_uint256 &min_chain_work, std::chrono::seconds max_tip_age) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this chain's tip exists, has enough work, and is recent.
invalid by consensus rules (excluding any below reasons)
CSHA256 ScriptExecutionCacheHasher() const
Return a copy of the pre-initialized hasher.
static time_point now() noexcept
Return current system time or mocked time, if set.
const CTransaction * ptxTo
const arith_uint256 & MinimumChainWork() const
bool enforce_BIP94
Enforce BIP94 timewarp attack mitigation.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::vector< CTransactionRef > vtx
fs::path StoragePath() const
Return path to chainstate leveldb directory.
kernel::Notifications & m_notifications
void UpdateTransactionsFromBlock(const std::vector< Txid > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
the block's data didn't match the data committed to by the PoW
Data structure storing a fee and size, ordered by increasing fee/size.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain)
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, const ChainstateManager &chainman, const CBlockIndex *pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
Assumeutxo
Chainstate assumeutxo validity.
#define LOCKS_EXCLUDED(...)
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range) noexcept
Return the time point advanced by a uniform random duration.
static SynchronizationState GetSynchronizationState(bool init, bool blockfiles_indexed)
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
The block chain is a tree shaped structure starting with the genesis block at the root...
Undo information for a CBlock.
Serialized script, used inside transaction inputs and outputs.
Undo information for a CTransaction.
std::vector< PerBlockConnectTrace > blocksConnected
CCoinsView backed by the coin database (chainstate/)
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...
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point, std::unique_ptr< CCoinsViewCursor > pcursor)
Calculate statistics about the unspent transaction output set.
const fs::path blocks_dir
uint64_t m_chain_tx_count
Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex().
static constexpr int32_t SEQ_ID_INIT_FROM_DISK
#define TRACEPOINT(context,...)
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
A block this one builds on is invalid.
int worker_threads_num
Number of script check worker threads. Zero means no parallel verification.
std::string ToString() const
#define NONFATAL_UNREACHABLE()
NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code.
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.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
uint256 GetRandHash() noexcept
Generate a random uint256.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool RaiseValidity(enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
static std::string PathToString(const path &path)
Convert path object to a byte string.
Chainstate *LoadAssumeutxoChainstate() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &AddChainstate(std::unique_ptr< Chainstate > chainstate) EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(bool DeleteChainstate(Chainstate &chainstate) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup(Chainstate &validated_cs, Chainstate &unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(std::optional< std::pair< const CBlockIndex *, const CBlockIndex * > > GetHistoricalBlockRange() const EXCLUSIVE_LOCKS_REQUIRED(util::Result< void > ActivateBestChains() LOCKS_EXCLUDED(void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > BlocksAheadOfTip() const LOCKS_EXCLUDED(CCheckQueue< CScriptCheck > & GetCheckQueue()
When starting up, search the datadir for a chainstate based on a UTXO snapshot that is in the process...
Application-specific storage settings.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac...
bool m_checked_witness_commitment
Holds configuration for use during UTXO snapshot load and validation.
static constexpr auto DATABASE_WRITE_INTERVAL_MIN
Time window to wait between writing blocks/block index and chainstate to disk.
#define AssertLockNotHeld(cs)
bool HasValidProofOfWork(std::span< const CBlockHeader > headers, const Consensus::Params &consensusParams)
Check that the proof of work on each blockheader matches the value in nBits.
std::optional< fs::path > FindAssumeutxoChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
const CBlockIndex *SnapshotBase() const EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *TargetBlock() const EXCLUSIVE_LOCKS_REQUIRED(void SetTargetBlock(CBlockIndex *block) EXCLUSIVE_LOCKS_REQUIRED(void SetTargetBlockHash(uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(boo ReachedTarget)() const EXCLUSIVE_LOCKS_REQUIRED(
The base of the snapshot this chainstate was created from.
bilingual_str ErrorString(const Result< T > &result)
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).
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
bool FlushChainstateBlockFile(int tip_height)
this node does not have a mempool so can't validate the transaction
A mutable version of CTransaction.
unsigned char * UCharCast(char *c)
bool IsValid(enum BlockStatus nUpTo) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
block timestamp was > 2 hours in the future (or our clock is bad)
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
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 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 const unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits) ...
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
const uint256 & ToUint256() const LIFETIMEBOUND
bool IsWellFormedPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
Mutex m_chainstate_mutex
The ChainState Mutex A lock that must be held when modifying this ChainState - held in ActivateBestCh...
#define STR_INTERNAL_BUG(msg)
static MempoolAcceptResult Failure(TxValidationState state)
size_t script_execution_cache_bytes
bool FatalError(Notifications ¬ifications, BlockValidationState &state, const bilingual_str &message)
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current time before the block ...
bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex())
std::list< CTransactionRef > take()
Clear all data structures and return the list of transactions.
bool IsBIP30Unspendable(const uint256 &block_hash, int block_height)
Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30) ...
The basic transaction that is broadcasted on the network and contained in blocks. ...
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
int nHeight
height of the entry in the chain. The genesis block has height 0
BlockValidationState TestBlockValidity(Chainstate &chainstate, const CBlock &block, const bool check_pow, const bool check_merkle_root)
Verify a block, including transactions.
std::optional< std::pair< DiagramCheckError, std::string > > ImprovesFeerateDiagram(CTxMemPool::ChangeSet &changeset)
The replacement transaction must improve the feerate diagram of the mempool.
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
CCoinsView that adds a memory cache for transactions to another CCoinsView.
std::vector< PerBlockConnectTrace > & GetBlocksConnected()
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
std::string GetRejectReason() const
full block available in blk*.dat
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
CuckooCache::cache< uint256, SignatureCacheHasher > m_script_execution_cache
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found.
A hasher class for SHA-256.
void BlockConnected(const kernel::ChainstateRole &, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Chainstate * HistoricalChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Return historical chainstate targeting a specific block, if any.
std::vector< CTxUndo > vtxundo
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...
static int64_t GetBlockWeight(const CBlock &block)
std::optional< int32_t > check_block_index
static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
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.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
std::vector< CTransactionRef > AddTransactionsFromBlock(const std::vector< CTransactionRef > &vtx)
Add transactions from the block, iterating through vtx in reverse order.
CCoinsView that brings transactions from a mempool into view.
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
otherwise didn't meet our local policy rules
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
SignatureCache * m_signature_cache
unsigned int nTx
Number of transactions in this block.
CTxMemPool * GetMempool()
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
std::string ToString(const T &t)
Locale-independent version of std::to_string.
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
#define Assert(val)
Identity function.
std::shared_ptr< Chain::Notifications > m_notifications
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.
const Txid & GetHash() const LIFETIMEBOUND
bool m_checked_merkle_root
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...
static MempoolAcceptResult Success(std::list< CTransactionRef > &&replaced_txns, int64_t vsize, CAmount fees, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static constexpr TransactionSerParams TX_WITH_WITNESS
static constexpr TransactionSerParams TX_NO_WITNESS
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
Used to track blocks whose transactions were applied to the UTXO state as a part of a single Activate...
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
static constexpr int64_t MAX_TIMEWARP
Maximum number of seconds that the timestamp of the first block of a difficulty adjustment period is ...
std::atomic_bool m_cached_is_ibd
Whether initial block download (IBD) is ongoing.
void CheckBlockIndex() const
Make various assertions about the state of the block index.
CHash256 & Write(std::span< const unsigned char > input)
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
PrecomputedTransactionData * txdata
New diagram wasn't strictly superior.