90 #define MICRO 0.000001
98 "level 0 reads the blocks from disk",
99 "level 1 verifies block validity",
100 "level 2 verifies undo data",
101 "level 3 checks disconnection of tip blocks",
102 "level 4 tries to reconnect the blocks",
103 "each level includes the checks of the previous levels",
118 : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true),
119 checkMerkleRoot(true) {}
156 std::optional<std::vector<int>> CalculatePrevHeights(
const CBlockIndex &tip,
159 std::vector<int> prev_heights;
160 prev_heights.resize(tx.
vin.size());
161 for (
size_t i = 0; i < tx.
vin.size(); ++i) {
165 LogPrintf(
"ERROR: %s: Missing input %d in transaction \'%s\'\n",
171 prev_heights[i] = tip.
nHeight + 1;
185 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
186 if (!prev_heights.has_value()) {
191 next_tip.
pprev = tip;
224 int64_t nMedianTimePast) {
225 return nMedianTimePast >=
gArgs.
GetIntArg(
"-replayprotectionactivationtime",
231 if (pindexPrev ==
nullptr) {
272 const Coin &coinFromUTXOSet = coins_tip.AccessCoin(txin.
prevout);
281 true, txdata, nSigChecksOut);
286 class MemPoolAccept {
289 : m_pool(mempool), m_view(&m_dummy),
290 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
291 m_active_chainstate(active_chainstate) {}
297 const int64_t m_accept_time;
298 const bool m_bypass_limits;
306 std::vector<COutPoint> &m_coins_to_uncache;
307 const bool m_test_accept;
308 const unsigned int m_heightOverride;
314 const bool m_package_submission;
320 const bool m_package_feerates;
323 static ATMPArgs SingleAccept(
const Config &config, int64_t accept_time,
325 std::vector<COutPoint> &coins_to_uncache,
327 unsigned int heightOverride) {
345 PackageTestAccept(
const Config &config, int64_t accept_time,
346 std::vector<COutPoint> &coins_to_uncache) {
362 PackageChildWithParents(
const Config &config, int64_t accept_time,
363 std::vector<COutPoint> &coins_to_uncache) {
377 static ATMPArgs SingleInPackageAccept(
const ATMPArgs &package_args) {
379 package_args.m_config,
380 package_args.m_accept_time,
382 package_args.m_coins_to_uncache,
383 package_args.m_test_accept,
384 package_args.m_heightOverride,
396 ATMPArgs(
const Config &config, int64_t accept_time,
bool bypass_limits,
397 std::vector<COutPoint> &coins_to_uncache,
bool test_accept,
398 unsigned int height_override,
bool package_submission,
399 bool package_feerates)
400 : m_config{config}, m_accept_time{accept_time},
401 m_bypass_limits{bypass_limits},
402 m_coins_to_uncache{coins_to_uncache}, m_test_accept{test_accept},
403 m_heightOverride{height_override},
404 m_package_submission{package_submission},
405 m_package_feerates(package_feerates) {}
420 AcceptMultipleTransactions(
const std::vector<CTransactionRef> &txns,
437 AcceptSubPackage(
const std::vector<CTransactionRef> &subpackage,
455 const uint32_t next_block_script_verify_flags)
457 m_next_block_script_verify_flags(next_block_script_verify_flags) {
464 std::unique_ptr<CTxMemPoolEntry> m_entry;
502 const uint32_t m_next_block_script_verify_flags;
503 int m_sig_checks_standard;
511 bool PreChecks(ATMPArgs &args, Workspace &ws)
518 bool ConsensusScriptChecks(
const ATMPArgs &args, Workspace &ws)
524 bool Finalize(
const ATMPArgs &args, Workspace &ws)
532 bool SubmitPackage(
const ATMPArgs &args, std::vector<Workspace> &workspaces,
534 std::map<TxId, MempoolAcceptResult> &results)
538 bool CheckFeeRate(
size_t package_size,
size_t package_vsize,
544 const Amount mempoolRejectFee =
545 m_pool.GetMinFee().GetFee(package_vsize);
548 package_fee < mempoolRejectFee) {
549 return state.Invalid(
551 "mempool min fee not met",
552 strprintf(
"%d < %d", package_fee, mempoolRejectFee));
557 if (package_fee < m_pool.m_min_relay_feerate.GetFee(package_size)) {
558 return state.Invalid(
561 m_pool.m_min_relay_feerate.GetFee(package_size)));
576 bool MemPoolAccept::PreChecks(ATMPArgs &args, Workspace &ws) {
581 const TxId &txid = ws.m_ptx->GetId();
584 const int64_t nAcceptTime = args.m_accept_time;
585 const bool bypass_limits = args.m_bypass_limits;
586 std::vector<COutPoint> &coins_to_uncache = args.m_coins_to_uncache;
587 const unsigned int heightOverride = args.m_heightOverride;
599 if (m_pool.m_require_standard &&
601 m_pool.m_permit_bare_multisig,
602 m_pool.m_dust_relay_feerate, reason)) {
611 *
Assert(m_active_chainstate.m_chain.Tip()),
612 args.m_config.GetChainParams().GetConsensus(), tx, ctxState)) {
621 if (m_pool.exists(txid)) {
623 "txn-already-in-mempool");
629 if (ptxConflicting) {
632 "txn-mempool-conflict");
636 m_view.SetBackend(m_viewmempool);
642 coins_to_uncache.push_back(txin.
prevout);
649 if (!m_view.HaveCoin(txin.
prevout)) {
651 for (
size_t out = 0; out < tx.
vout.size(); out++) {
656 "txn-already-known");
663 "bad-txns-inputs-missingorspent");
668 if (!m_view.HaveInputs(tx)) {
670 "bad-txns-inputs-spent");
674 m_view.GetBestBlock();
679 m_view.SetBackend(m_dummy);
681 assert(m_active_chainstate.m_blockman.LookupBlockIndex(
682 m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
691 m_active_chainstate.m_chain.Tip(), m_view, tx)};
692 if (!lock_points.has_value() ||
702 m_active_chainstate.m_chain.Height() + 1,
709 if (m_pool.m_require_standard &&
712 "bad-txns-nonstandard-inputs");
716 ws.m_modified_fees = ws.m_base_fees;
717 m_pool.ApplyDelta(txid, ws.m_modified_fees);
722 const uint32_t scriptVerifyFlags =
726 ws.m_precomputed_txdata, ws.m_sig_checks_standard)) {
731 ws.m_entry = std::make_unique<CTxMemPoolEntry>(
732 ptx, ws.m_base_fees, nAcceptTime,
733 heightOverride ? heightOverride : m_active_chainstate.m_chain.Height(),
734 ws.m_sig_checks_standard, lock_points.value());
736 ws.m_vsize = ws.m_entry->GetTxVirtualSize();
744 if (!bypass_limits &&
746 m_pool.m_min_relay_feerate.GetFee(ws.m_ptx->GetTotalSize())) {
753 m_pool.m_min_relay_feerate.GetFee(nSize)));
758 if (!bypass_limits && !args.m_package_feerates &&
759 !CheckFeeRate(nSize, ws.m_vsize, ws.m_modified_fees, state)) {
766 bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs &args, Workspace &ws) {
784 int nSigChecksConsensus;
786 tx, state, m_view, m_pool, ws.m_next_block_script_verify_flags,
787 ws.m_precomputed_txdata, nSigChecksConsensus,
788 m_active_chainstate.CoinsTip())) {
792 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against "
793 "latest-block but not STANDARD flags %s, %s\n",
798 if (ws.m_sig_checks_standard != nSigChecksConsensus) {
803 "%s: BUG! PLEASE REPORT THIS! SigChecks count differed between "
804 "standard and consensus flags in %s",
810 bool MemPoolAccept::Finalize(
const ATMPArgs &args, Workspace &ws) {
813 const TxId &txid = ws.m_ptx->GetId();
815 const bool bypass_limits = args.m_bypass_limits;
820 m_pool.addUnchecked(entry);
827 if (!args.m_package_submission && !bypass_limits) {
828 m_pool.LimitSize(m_active_chainstate.CoinsTip());
829 if (!m_pool.exists(txid)) {
842 std::vector<Coin> spent_coins;
843 spent_coins.reserve(ptx->vin.size());
844 for (
const CTxIn &input : ptx->vin) {
848 spent_coins.push_back(std::move(coin));
853 bool MemPoolAccept::SubmitPackage(
854 const ATMPArgs &args, std::vector<Workspace> &workspaces,
856 std::map<TxId, MempoolAcceptResult> &results) {
861 workspaces.cbegin(), workspaces.cend(),
862 [
this](
const auto &ws) { return !m_pool.exists(ws.m_ptx->GetId()); }));
864 bool all_submitted =
true;
871 for (Workspace &ws : workspaces) {
872 if (!ConsensusScriptChecks(args, ws)) {
873 results.emplace(ws.m_ptx->GetId(),
876 all_submitted =
false;
879 strprintf(
"BUG! PolicyScriptChecks succeeded but "
880 "ConsensusScriptChecks failed: %s",
881 ws.m_ptx->GetId().ToString()));
891 if (!Finalize(args, ws)) {
892 results.emplace(ws.m_ptx->GetId(),
895 all_submitted =
false;
897 strprintf(
"BUG! Adding to mempool failed: %s",
898 ws.m_ptx->GetId().ToString()));
904 m_pool.LimitSize(m_active_chainstate.CoinsTip());
906 std::vector<TxId> all_package_txids;
907 all_package_txids.reserve(workspaces.size());
908 std::transform(workspaces.cbegin(), workspaces.cend(),
909 std::back_inserter(all_package_txids),
910 [](
const auto &ws) { return ws.m_ptx->GetId(); });
914 for (Workspace &ws : workspaces) {
915 const auto effective_feerate =
916 args.m_package_feerates
917 ? ws.m_package_feerate
919 static_cast<uint32_t
>(ws.m_vsize)};
920 const auto effective_feerate_txids =
921 args.m_package_feerates ? all_package_txids
922 : std::vector<TxId>({ws.m_ptx->GetId()});
923 results.emplace(ws.m_ptx->GetId(),
926 effective_feerate_txids));
929 std::make_shared<
const std::vector<Coin>>(
930 getSpentCoins(ws.m_ptx, m_view)),
931 m_pool.GetAndIncrementSequence());
933 return all_submitted;
944 const CBlockIndex *tip = m_active_chainstate.m_chain.Tip();
949 const std::vector<TxId> single_txid{ws.m_ptx->GetId()};
954 if (!PreChecks(args, ws)) {
959 ws.m_state,
CFeeRate(ws.m_modified_fees, ws.m_vsize),
965 if (!ConsensusScriptChecks(args, ws)) {
969 const TxId txid = ptx->GetId();
979 if (
auto it = m_pool.mapNextTx.lower_bound(
COutPoint{txid, 0});
980 it != m_pool.mapNextTx.end() && it->first->GetTxId() == txid) {
981 LogPrintf(
"%s: BUG! PLEASE REPORT THIS! Attempt to add txid %s, but "
982 "its outputs are already spent in the "
986 "txn-child-before-parent");
990 const CFeeRate effective_feerate{ws.m_modified_fees,
991 static_cast<uint32_t
>(ws.m_vsize)};
993 if (args.m_test_accept) {
995 effective_feerate, single_txid);
998 if (!Finalize(args, ws)) {
1004 ws.m_state,
CFeeRate(ws.m_modified_fees, ws.m_vsize), single_txid);
1009 std::make_shared<
const std::vector<Coin>>(getSpentCoins(ptx, m_view)),
1010 m_pool.GetAndIncrementSequence());
1013 effective_feerate, single_txid);
1017 const std::vector<CTransactionRef> &txns, ATMPArgs &args) {
1027 std::vector<Workspace> workspaces{};
1028 workspaces.reserve(txns.size());
1030 txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1031 [
this](
const auto &tx) {
1033 tx, GetNextBlockScriptFlags(m_active_chainstate.m_chain.Tip(),
1034 m_active_chainstate.m_chainman));
1036 std::map<TxId, MempoolAcceptResult> results;
1042 std::vector<TxId> valid_txids;
1043 for (Workspace &ws : workspaces) {
1044 if (!PreChecks(args, ws)) {
1046 "transaction failed");
1049 results.emplace(ws.m_ptx->GetId(),
1052 std::move(results));
1056 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1057 valid_txids.push_back(ws.m_ptx->GetId());
1072 const auto m_total_size = std::accumulate(
1073 workspaces.cbegin(), workspaces.cend(), int64_t{0},
1074 [](int64_t
sum,
auto &ws) { return sum + ws.m_ptx->GetTotalSize(); });
1075 const auto m_total_vsize =
1076 std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1077 [](int64_t
sum,
auto &ws) { return sum + ws.m_vsize; });
1078 const auto m_total_modified_fees = std::accumulate(
1079 workspaces.cbegin(), workspaces.cend(),
Amount::zero(),
1080 [](
Amount sum,
auto &ws) { return sum + ws.m_modified_fees; });
1081 const CFeeRate package_feerate(m_total_modified_fees, m_total_vsize);
1082 std::vector<TxId> all_package_txids;
1083 all_package_txids.reserve(workspaces.size());
1084 std::transform(workspaces.cbegin(), workspaces.cend(),
1085 std::back_inserter(all_package_txids),
1086 [](
const auto &ws) { return ws.m_ptx->GetId(); });
1088 if (args.m_package_feerates &&
1089 !CheckFeeRate(m_total_size, m_total_vsize, m_total_modified_fees,
1090 placeholder_state)) {
1092 "transaction failed");
1094 package_state, {{workspaces.back().m_ptx->GetId(),
1097 CFeeRate(m_total_modified_fees, m_total_vsize),
1098 all_package_txids)}});
1101 for (Workspace &ws : workspaces) {
1102 ws.m_package_feerate = package_feerate;
1103 const TxId &ws_txid = ws.m_ptx->GetId();
1104 if (args.m_test_accept &&
1105 std::find(valid_txids.begin(), valid_txids.end(), ws_txid) !=
1106 valid_txids.end()) {
1107 const auto effective_feerate =
1108 args.m_package_feerates
1109 ? ws.m_package_feerate
1111 static_cast<uint32_t
>(ws.m_vsize)};
1112 const auto effective_feerate_txids =
1113 args.m_package_feerates ? all_package_txids
1114 : std::vector<TxId>{ws.m_ptx->GetId()};
1118 results.emplace(ws_txid,
1120 ws.m_vsize, ws.m_base_fees, effective_feerate,
1121 effective_feerate_txids));
1125 if (args.m_test_accept) {
1129 if (!SubmitPackage(args, workspaces, package_state, results)) {
1138 MemPoolAccept::AcceptSubPackage(
const std::vector<CTransactionRef> &subpackage,
1144 if (subpackage.size() > 1) {
1145 return AcceptMultipleTransactions(subpackage, args);
1147 const auto &tx = subpackage.front();
1148 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1149 const auto single_res = AcceptSingleTransaction(tx, single_args);
1151 if (single_res.m_result_type !=
1154 "transaction failed");
1157 {{tx->
GetId(), single_res}});
1187 for (
const auto &outpoint : m_viewmempool.GetNonBaseCoins()) {
1191 m_view.Uncache(outpoint);
1194 m_viewmempool.Reset();
1210 if (!
CheckPackage(package, package_state_quit_early)) {
1219 "package-not-child-with-parents");
1224 assert(package.size() > 1);
1227 const auto &child = package.back();
1228 std::unordered_set<TxId, SaltedTxIdHasher> unconfirmed_parent_txids;
1230 package.cbegin(), package.cend() - 1,
1231 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1232 [](
const auto &tx) { return tx->GetId(); });
1241 const CCoinsViewCache &coins_tip_cache = m_active_chainstate.CoinsTip();
1242 for (
const auto &input : child->vin) {
1244 args.m_coins_to_uncache.push_back(input.
prevout);
1251 m_view.
SetBackend(m_active_chainstate.CoinsTip());
1252 const auto package_or_confirmed = [
this, &unconfirmed_parent_txids](
1253 const auto &input) {
1254 return unconfirmed_parent_txids.count(input.
prevout.
GetTxId()) > 0 ||
1255 m_view.HaveCoin(input.
prevout);
1257 if (!std::all_of(child->vin.cbegin(), child->vin.cend(),
1258 package_or_confirmed)) {
1259 package_state_quit_early.
Invalid(
1261 "package-not-child-with-unconfirmed-parents");
1267 m_view.SetBackend(m_dummy);
1273 std::map<TxId, MempoolAcceptResult> results_final;
1278 std::map<TxId, MempoolAcceptResult> individual_results_nonfinal;
1279 bool quit_early{
false};
1280 std::vector<CTransactionRef> txns_package_eval;
1281 for (
const auto &tx : package) {
1282 const auto &txid = tx->
GetId();
1285 if (m_pool.exists(txid)) {
1299 auto iter = m_pool.GetIter(txid);
1300 assert(iter != std::nullopt);
1302 (*iter.value())->GetTxSize(),
1303 (*iter.value())->GetFee()));
1307 const auto single_package_res = AcceptSubPackage({tx}, args);
1308 const auto &single_res = single_package_res.m_tx_results.at(txid);
1309 if (single_res.m_result_type ==
1314 assert(m_pool.exists(txid));
1315 results_final.emplace(txid, single_res);
1316 }
else if (single_res.m_state.GetResult() !=
1318 single_res.m_state.GetResult() !=
1333 package_state_quit_early.
Invalid(
1335 individual_results_nonfinal.emplace(txid, single_res);
1337 individual_results_nonfinal.emplace(txid, single_res);
1338 txns_package_eval.push_back(tx);
1343 auto multi_submission_result =
1344 quit_early || txns_package_eval.empty()
1346 : AcceptSubPackage(txns_package_eval, args);
1348 multi_submission_result.m_state;
1353 m_pool.LimitSize(m_active_chainstate.CoinsTip());
1355 for (
const auto &tx : package) {
1356 const auto &txid = tx->
GetId();
1357 if (multi_submission_result.m_tx_results.count(txid) > 0) {
1360 Assume(results_final.count(txid) == 0);
1364 const auto &txresult =
1365 multi_submission_result.m_tx_results.at(txid);
1366 if (txresult.m_result_type ==
1368 !m_pool.exists(txid)) {
1370 "transaction failed");
1374 results_final.emplace(
1377 results_final.emplace(txid, txresult);
1379 }
else if (
const auto final_it{results_final.find(txid)};
1380 final_it != results_final.end()) {
1383 Assume(final_it->second.m_result_type !=
1385 Assume(individual_results_nonfinal.count(txid) == 0);
1386 if (!m_pool.exists(tx->
GetId())) {
1388 "transaction failed");
1393 results_final.erase(txid);
1394 results_final.emplace(
1397 }
else if (
const auto non_final_it{
1398 individual_results_nonfinal.find(txid)};
1399 non_final_it != individual_results_nonfinal.end()) {
1400 Assume(non_final_it->second.m_result_type ==
1403 results_final.emplace(txid, non_final_it->second);
1406 Assume(results_final.size() == package.size());
1408 std::move(results_final));
1414 int64_t accept_time,
bool bypass_limits,
1416 unsigned int heightOverride) {
1421 std::vector<COutPoint> coins_to_uncache;
1422 auto args = MemPoolAccept::ATMPArgs::SingleAccept(
1424 coins_to_uncache, test_accept, heightOverride);
1426 .AcceptSingleTransaction(tx, args);
1434 for (
const COutPoint &outpoint : coins_to_uncache) {
1451 assert(!package.empty());
1452 assert(std::all_of(package.cbegin(), package.cend(),
1453 [](
const auto &tx) { return tx != nullptr; }));
1457 std::vector<COutPoint> coins_to_uncache;
1461 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(
1462 config,
GetTime(), coins_to_uncache);
1463 return MemPoolAccept(pool, active_chainstate)
1464 .AcceptMultipleTransactions(package, args);
1466 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(
1467 config,
GetTime(), coins_to_uncache);
1468 return MemPoolAccept(pool, active_chainstate)
1469 .AcceptPackage(package, args);
1475 if (test_accept || result.m_state.IsInvalid()) {
1476 for (
const COutPoint &hashTx : coins_to_uncache) {
1489 if (halvings >= 64) {
1500 : m_dbview{
std::move(db_params),
std::move(options)},
1501 m_catcherview(&m_dbview) {}
1503 void CoinsViews::InitCache() {
1505 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1510 std::optional<BlockHash> from_snapshot_blockhash)
1511 : m_mempool(mempool), m_blockman(blockman), m_chainman(chainman),
1512 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1515 bool should_wipe, std::string leveldb_name) {
1522 .cache_bytes = cache_size_bytes,
1523 .memory_only = in_memory,
1524 .wipe_data = should_wipe,
1530 void Chainstate::InitCoinsCache(
size_t cache_size_bytes) {
1565 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
1592 std::string warning =
1593 std::string(
"'Warning: Large-work fork detected, forking after "
1600 LogPrintf(
"%s: Warning: Large fork found\n forking the "
1601 "chain at height %d (%s)\n lasting to height %d "
1602 "(%s).\nChain state database corruption likely.\n",
1609 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks "
1610 "longer than our best chain.\nChain state database "
1611 "corruption likely.\n",
1666 m_avalancheFinalizedBlockIndex = pindexNew->
pprev;
1669 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n",
1676 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n",
1688 pindex->nStatus = pindex->nStatus.withFailed();
1739 bool sigCacheStore,
bool scriptCacheStore,
1743 std::vector<CScriptCheck> *pvChecks) {
1748 pvChecks->reserve(tx.
vin.size());
1758 (pBlockLimitSigChecks &&
1761 "too-many-sigchecks");
1766 int nSigChecksTotal = 0;
1768 for (
size_t i = 0; i < tx.
vin.size(); i++) {
1781 &txLimitSigChecks, pBlockLimitSigChecks);
1785 pvChecks->push_back(std::move(check));
1795 uint32_t mandatoryFlags =
1797 if (
flags != mandatoryFlags) {
1804 sigCacheStore, txdata);
1808 strprintf(
"non-mandatory-script-verify-flag (%s)",
1824 strprintf(
"mandatory-script-verify-flag-failed (%s)",
1831 nSigChecksOut = nSigChecksTotal;
1833 if (scriptCacheStore && !pvChecks) {
1845 return state.
Error(strMessage);
1858 if (undo.GetHeight() == 0) {
1881 view.
AddCoin(out, std::move(undo), !fClean);
1896 error(
"DisconnectBlock(): failure reading undo data");
1900 return ApplyBlockUndo(std::move(blockUndo), block, pindex, view);
1908 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
1909 error(
"DisconnectBlock(): block and undo data inconsistent");
1914 for (
size_t i = 1; i < block.
vtx.size(); i++) {
1918 error(
"DisconnectBlock(): transaction and undo data inconsistent");
1922 for (
size_t j = 0; j < tx.
vin.size(); j++) {
1935 for (
const auto &ptx : block.
vtx) {
1942 for (
size_t o = 0; o < tx.
vout.size(); o++) {
1943 if (tx.
vout[o].scriptPubKey.IsUnspendable()) {
1949 bool is_spent = view.
SpendCoin(out, &coin);
2086 if (!
CheckBlock(block, state, consensusParams,
2093 return AbortNode(state,
"Corrupt block found indicating potential "
2094 "hardware failure; shutting down");
2096 return error(
"%s: Consensus::CheckBlock: %s", __func__,
2117 bool fScriptChecks =
true;
2126 BlockMap::const_iterator it{
2129 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2157 consensusParams) <= 60 * 60 * 24 * 7 * 2);
2179 bool fEnforceBIP30 = !((pindex->
nHeight == 91842 &&
2181 uint256S(
"0x00000000000a4d0a398161ffc163c503763"
2182 "b1f4360639393e0e4c8e300e0caec")) ||
2185 uint256S(
"0x00000000000743f190a18c5577a3c2d2a1f"
2186 "610ae9601ac046a38084ccb7cd721")));
2217 static constexpr
int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2254 (!pindexBIP34height ||
2260 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2261 for (
const auto &tx : block.
vtx) {
2262 for (
size_t o = 0; o < tx->
vout.size(); o++) {
2264 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite "
2274 int nLockTimeFlags = 0;
2288 std::vector<int> prevheights;
2300 std::vector<TxSigCheckLimiter> nSigChecksTxLimiters;
2301 nSigChecksTxLimiters.resize(block.
vtx.size() - 1);
2304 blockundo.
vtxundo.resize(block.
vtx.size() - 1);
2311 for (
const auto &ptx : block.
vtx) {
2314 }
catch (
const std::logic_error &e) {
2323 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite transaction\n");
2332 for (
const auto &ptx : block.
vtx) {
2335 nInputs += tx.
vin.size();
2349 return error(
"%s: Consensus::CheckTxInputs: %s, %s", __func__,
2356 LogPrintf(
"ERROR: %s: accumulated fee in the block out of range.\n",
2359 "bad-txns-accumulated-fee-outofrange");
2370 prevheights.resize(tx.
vin.size());
2371 for (
size_t j = 0; j < tx.
vin.size(); j++) {
2375 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2376 LogPrintf(
"ERROR: %s: contains a non-BIP68-final transaction\n",
2379 "bad-txns-nonfinal");
2384 bool fCacheResults = fJustCheck;
2387 if (!fEnforceSigCheck) {
2394 std::vector<CScriptCheck> vChecks;
2396 if (fScriptChecks &&
2399 nSigChecksRet, nSigChecksTxLimiters[txIndex],
2400 &nSigChecksBlockLimiter, &vChecks)) {
2407 "ConnectBlock(): CheckInputScripts on %s failed with %s",
2411 control.
Add(std::move(vChecks));
2425 " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) "
2426 "[%.2fs (%.2fms/blk)]\n",
2427 (
unsigned)block.
vtx.size(),
MILLI * (nTime3 - nTime2),
2428 MILLI * (nTime3 - nTime2) / block.
vtx.size(),
2429 nInputs <= 1 ? 0 :
MILLI * (nTime3 - nTime2) / (nInputs - 1),
2432 const Amount blockReward =
2434 if (block.
vtx[0]->GetValueOut() > blockReward) {
2435 LogPrintf(
"ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs "
2437 block.
vtx[0]->GetValueOut(), blockReward);
2446 if (!control.
Wait()) {
2448 "blk-bad-inputs",
"parallel script check failed");
2455 " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n",
2456 nInputs - 1,
MILLI * (nTime4 - nTime2),
2457 nInputs <= 1 ? 0 :
MILLI * (nTime4 - nTime2) / (nInputs - 1),
2464 if (!
m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2482 TRACE6(validation, block_connected, block_hash.data(), pindex->
nHeight,
2483 block.
vtx.size(), nInputs, nSigChecksRet,
2485 nTime5 - nTimeStart);
2498 Chainstate::GetCoinsCacheSizeState(
size_t max_coins_cache_size_bytes,
2499 size_t max_mempool_size_bytes) {
2503 int64_t nTotalSpace =
2504 max_coins_cache_size_bytes +
2505 std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2508 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES =
2510 int64_t large_threshold = std::max(
2511 (9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2513 if (cacheSize > nTotalSpace) {
2514 LogPrintf(
"Cache size (%s) exceeds total space (%s)\n", cacheSize,
2517 }
else if (cacheSize > large_threshold) {
2526 assert(this->CanFlushToDisk());
2527 std::set<int> setFilesToPrune;
2528 bool full_flush_completed =
false;
2535 bool fFlushForPrune =
false;
2536 bool fDoFullFlush =
false;
2548 std::optional<std::string> limiting_lock;
2550 for (
const auto &prune_lock :
m_blockman.m_prune_locks) {
2551 if (prune_lock.second.height_first ==
2552 std::numeric_limits<int>::max()) {
2557 const int lock_height{prune_lock.second.height_first -
2559 last_prune = std::max(1, std::min(last_prune, lock_height));
2560 if (last_prune == lock_height) {
2561 limiting_lock = prune_lock.first;
2565 if (limiting_lock) {
2567 limiting_lock.value(), last_prune);
2570 if (nManualPruneHeight > 0) {
2575 std::min(last_prune, nManualPruneHeight),
2586 if (!setFilesToPrune.empty()) {
2587 fFlushForPrune =
true;
2590 "prunedblockfiles",
true);
2595 const auto nNow = GetTime<std::chrono::microseconds>();
2620 fCacheCritical || fPeriodicFlush || fFlushForPrune;
2622 if (fDoFullFlush || fPeriodicWrite) {
2625 return AbortNode(state,
"Disk space is too low!",
2626 _(
"Disk space is too low!"));
2645 state,
"Failed to write to block index database");
2650 if (fFlushForPrune) {
2660 if (fDoFullFlush && !
CoinsTip().GetBestBlock().IsNull()) {
2662 strprintf(
"write coins cache to disk (%d coins, %.2fkB)",
2663 coins_count, coins_mem_usage / 1000),
2673 48 * 2 * 2 *
CoinsTip().GetCacheSize())) {
2674 return AbortNode(state,
"Disk space is too low!",
2675 _(
"Disk space is too low!"));
2681 return AbortNode(state,
"Failed to write to coin database");
2684 full_flush_completed =
true;
2689 GetTimeMicros() - nNow.count(), uint32_t(mode), coins_count,
2690 uint64_t(coins_mem_usage), fFlushForPrune);
2693 if (full_flush_completed) {
2697 }
catch (
const std::runtime_error &e) {
2698 return AbortNode(state, std::string(
"System error while flushing: ") +
2707 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
2716 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
2723 const std::string &func_name,
2724 const std::string &
prefix)
2727 LogPrintf(
"%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
2728 "date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
2734 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2735 coins_tip.GetCacheSize());
2738 void Chainstate::UpdateTip(
const CBlockIndex *pindexNew) {
2740 const auto &coins_tip =
CoinsTip();
2746 if (
this != &
m_chainman.ActiveChainstate()) {
2749 constexpr
int BACKGROUND_LOG_INTERVAL = 2000;
2750 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2752 "[background validation] ");
2768 UpdateTipLog(coins_tip, pindexNew, params, __func__,
"");
2795 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2798 return error(
"DisconnectTip(): Failed to read block");
2806 if (DisconnectBlock(block, pindexDelete, view) !=
2808 return error(
"DisconnectTip(): DisconnectBlock %s failed",
2812 bool flushed = view.
Flush();
2822 const int max_height_first{pindexDelete->
nHeight - 1};
2823 for (
auto &prune_lock :
m_blockman.m_prune_locks) {
2824 if (prune_lock.second.height_first <= max_height_first) {
2828 prune_lock.second.height_first = max_height_first;
2830 prune_lock.first, max_height_first);
2843 if (pindexDelete->
pprev !=
nullptr &&
2847 "Disconnecting mempool due to rewind of upgrade block\n");
2848 if (disconnectpool) {
2854 if (disconnectpool) {
2861 UpdateTip(pindexDelete->
pprev);
2895 std::shared_ptr<const CBlock> pblock) {
2925 const std::shared_ptr<const CBlock> &pblock,
2939 std::shared_ptr<const CBlock> pthisBlock;
2941 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2943 return AbortNode(state,
"Failed to read block");
2945 pthisBlock = pblockNew;
2947 pthisBlock = pblock;
2950 const CBlock &blockConnecting = *pthisBlock;
2961 bool rv =
ConnectBlock(blockConnecting, state, pindexNew, view,
2970 return error(
"%s: ConnectBlock %s failed, %s", __func__,
2991 const Amount blockReward =
2995 std::vector<std::unique_ptr<ParkingPolicy>> parkingPolicies;
2996 parkingPolicies.emplace_back(std::make_unique<MinerFundPolicy>(
2997 consensusParams, *pindexNew, blockConnecting, blockReward));
3000 parkingPolicies.emplace_back(
3001 std::make_unique<StakingRewardsPolicy>(
3002 *
avalanche, consensusParams, *pindexNew,
3003 blockConnecting, blockReward));
3006 parkingPolicies.emplace_back(
3007 std::make_unique<PreConsensusPolicy>(
3008 *pindexNew, blockConnecting,
m_mempool));
3013 if (std::find_if_not(parkingPolicies.begin(), parkingPolicies.end(),
3014 [&](
const auto &policy) {
3015 bool ret = (*policy)(blockPolicyState);
3018 "Park block because it "
3019 "violated a block policy: %s\n",
3020 blockPolicyState.ToString());
3023 }) != parkingPolicies.end()) {
3024 pindexNew->nStatus = pindexNew->nStatus.withParked();
3034 " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3037 bool flushed = view.
Flush();
3055 " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3061 disconnectpool.removeForBlock(blockConnecting.vtx, *m_mempool);
3066 if (pindexNew->
pprev !=
nullptr &&
3071 "Disconnecting mempool due to acceptance of upgrade block\n");
3072 disconnectpool.importMempool(*m_mempool);
3077 m_chain.SetTip(*pindexNew);
3078 UpdateTip(pindexNew);
3084 " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3094 if (
this != &m_chainman.ActiveChainstate()) {
3098 m_chainman.MaybeCompleteSnapshotValidation();
3101 connectTrace.BlockConnected(pindexNew, std::move(pthisBlock));
3110 std::vector<const CBlockIndex *> &blocksToReconcile,
bool fAutoUnpark) {
3117 std::set<CBlockIndex *, CBlockIndexWorkComparator>::reverse_iterator
3129 if (m_avalancheFinalizedBlockIndex &&
3131 LogPrintf(
"Park block %s because it forks prior to the "
3132 "avalanche finalized chaintip.\n",
3134 pindexNew->nStatus = pindexNew->nStatus.withParked();
3145 bool hasValidAncestor =
true;
3146 while (hasValidAncestor && pindexTest && pindexTest != pindexFork) {
3151 bool fParkedChain = pindexTest->nStatus.isOnParkedChain();
3152 if (fAutoUnpark && fParkedChain) {
3158 if (!pindexTip || !pindexFork) {
3172 pindexExtraPow = pindexExtraPow->
pprev;
3177 requiredWork += (deltaWork >> 1);
3188 LogPrintf(
"Unpark chain up to block %s as it has "
3189 "accumulated enough PoW.\n",
3191 fParkedChain =
false;
3200 bool fInvalidChain = pindexTest->nStatus.isInvalid();
3201 bool fMissingData = !pindexTest->nStatus.hasData();
3202 if (!(fInvalidChain || fParkedChain || fMissingData)) {
3205 pindexTest = pindexTest->
pprev;
3211 hasValidAncestor =
false;
3214 if (fInvalidChain && (
m_chainman.m_best_invalid ==
nullptr ||
3220 if (fParkedChain && (
m_chainman.m_best_parked ==
nullptr ||
3226 LogPrintf(
"Considered switching to better tip %s but that chain "
3227 "contains a%s%s%s block.\n",
3229 fInvalidChain ?
"n invalid" :
"",
3230 fParkedChain ?
" parked" :
"",
3231 fMissingData ?
" missing-data" :
"");
3235 while (pindexTest != pindexFailed) {
3236 if (fInvalidChain || fParkedChain) {
3237 pindexFailed->nStatus =
3238 pindexFailed->nStatus.withFailedParent(fInvalidChain)
3239 .withParkedParent(fParkedChain);
3240 }
else if (fMissingData) {
3246 std::make_pair(pindexFailed->
pprev, pindexFailed));
3249 pindexFailed = pindexFailed->
pprev;
3252 if (fInvalidChain || fParkedChain) {
3259 blocksToReconcile.push_back(pindexNew);
3262 if (hasValidAncestor) {
3295 const std::shared_ptr<const CBlock> &pblock,
bool &fInvalidFound,
3306 bool fBlocksDisconnected =
false;
3309 if (!fBlocksDisconnected) {
3328 "Failed to disconnect block; see debug.log for details");
3332 fBlocksDisconnected =
true;
3336 std::vector<CBlockIndex *> vpindexToConnect;
3337 bool fContinue =
true;
3342 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3343 vpindexToConnect.clear();
3344 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3347 vpindexToConnect.push_back(pindexIter);
3348 pindexIter = pindexIter->
pprev;
3356 if (!
ConnectTip(state, blockPolicyState, pindexConnect,
3357 pindexConnect == pindexMostWork
3359 : std::shared_ptr<const CBlock>(),
3360 connectTrace, disconnectpool,
avalanche)) {
3368 fInvalidFound =
true;
3389 if (!pindexOldTip ||
3401 if (fBlocksDisconnected || !disconnectpool.
isEmpty()) {
3407 "Updating mempool due to reorganization or "
3408 "rules upgrade/downgrade\n");
3416 if (fInvalidFound) {
3436 bool fNotify =
false;
3437 bool fInitialBlockDownload =
false;
3442 pindexHeader = chainstate.m_chainman.m_best_header;
3444 if (pindexHeader != pindexHeaderOld) {
3446 fInitialBlockDownload = chainstate.IsInitialBlockDownload();
3447 pindexHeaderOld = pindexHeader;
3453 chainstate.m_chainman.GetNotifications().headerTip(
3469 std::shared_ptr<const CBlock> pblock,
3471 bool skip_checkblockindex) {
3491 LogPrintf(
"m_disabled is set - this chainstate should not be in "
3492 "operation. Please report this as a bug. %s\n",
3509 std::vector<const CBlockIndex *> blocksToReconcile;
3510 bool blocks_connected =
false;
3512 const bool fAutoUnpark =
3519 LOCK(MempoolMutex());
3529 if (pindexMostWork ==
nullptr) {
3535 if (pindexMostWork ==
nullptr ||
3540 bool fInvalidFound =
false;
3541 std::shared_ptr<const CBlock> nullBlockPtr;
3543 state, pindexMostWork,
3544 pblock && pblock->GetHash() ==
3548 fInvalidFound, connectTrace,
avalanche)) {
3552 blocks_connected =
true;
3554 if (fInvalidFound ||
3555 (pindexMostWork && pindexMostWork->nStatus.isParked())) {
3557 pindexMostWork =
nullptr;
3563 assert(trace.pblock && trace.pindex);
3585 if (!skip_checkblockindex) {
3589 if (blocks_connected) {
3596 if (pindexFork != pindexNewTip) {
3611 for (
const CBlockIndex *pindex : blocksToReconcile) {
3613 avalanche->computeStakingReward(pindex);
3617 if (!blocks_connected) {
3621 if (nStopAtHeight && pindexNewTip &&
3622 pindexNewTip->
nHeight >= nStopAtHeight) {
3640 }
while (pindexNewTip != pindexMostWork);
3692 template <
typename Func>
struct Defer {
3694 Defer(
Func &&f) : func(
std::move(f)) {}
3695 ~Defer() { func(); }
3708 bool pindex_was_in_chain =
false;
3709 int disconnected = 0;
3725 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3729 for (
auto &entry :
m_blockman.m_block_index) {
3740 candidate_blocks_by_work.insert(
3741 std::make_pair(candidate->
nChainWork, candidate));
3751 LOCK(MempoolMutex());
3753 constexpr
int maxDisconnectPoolBlocks = 10;
3805 pindex_was_in_chain =
true;
3814 if (optDisconnectPool && disconnected > maxDisconnectPoolBlocks) {
3819 optDisconnectPool =
nullptr;
3835 invalid_walk_tip->nStatus =
3836 invalidate ? invalid_walk_tip->nStatus.withFailed()
3837 : invalid_walk_tip->nStatus.withParked();
3842 if (invalid_walk_tip == to_mark_failed_or_parked->
pprev &&
3843 (invalidate ? to_mark_failed_or_parked->nStatus.hasFailed()
3844 : to_mark_failed_or_parked->nStatus.isParked())) {
3848 to_mark_failed_or_parked->nStatus =
3850 ? to_mark_failed_or_parked->nStatus.withFailed(
false)
3852 : to_mark_failed_or_parked->nStatus.withParked(
false)
3853 .withParkedParent());
3859 auto candidate_it = candidate_blocks_by_work.lower_bound(
3861 while (candidate_it != candidate_blocks_by_work.end()) {
3863 invalid_walk_tip->
pprev)) {
3865 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3874 to_mark_failed_or_parked = invalid_walk_tip;
3890 to_mark_failed_or_parked->nStatus =
3891 invalidate ? to_mark_failed_or_parked->nStatus.withFailed()
3892 : to_mark_failed_or_parked->nStatus.withParked();
3905 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
3907 block_index.HaveTxsDownloaded() &&
3920 if (pindex_was_in_chain) {
3923 *to_mark_failed_or_parked->
pprev);
3947 template <
typename F>
3951 if (pindex->nStatus != newStatus &&
3954 pindex->nStatus = newStatus;
3970 template <
typename F,
typename C,
typename AC>
3972 F f, C fChild, AC fAncestorWasChanged) {
3978 for (
auto pindexAncestor = pindex; pindexAncestor !=
nullptr;
3979 pindexAncestor = pindexAncestor->
pprev) {
3981 pindexDeepestChanged = pindexAncestor;
3987 pindexDeepestChanged) {
3989 pindexReset =
nullptr;
3993 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
3996 fAncestorWasChanged);
4006 return status.withClearedFailureFlags();
4009 return status.withClearedFailureFlags();
4012 return status.withFailedParent(false);
4022 return status.withClearedParkedFlags();
4025 return fClearChildren ? status.withClearedParkedFlags()
4026 : status.withParkedParent(false);
4029 return status.withParkedParent(false);
4049 "The block to mark finalized by avalanche is not on the "
4050 "active chain: %s\n",
4063 m_avalancheFinalizedBlockIndex = pindex;
4073 m_avalancheFinalizedBlockIndex =
nullptr;
4078 return pindex && m_avalancheFinalizedBlockIndex &&
4090 pindexNew->
nTx = block.
vtx.size();
4092 pindexNew->nFile = pos.
nFile;
4093 pindexNew->nDataPos = pos.
nPos;
4094 pindexNew->nUndoPos = 0;
4095 pindexNew->nStatus = pindexNew->nStatus.withData();
4102 std::deque<CBlockIndex *> queue;
4103 queue.push_back(pindexNew);
4107 while (!queue.empty()) {
4125 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4126 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4128 while (range.first != range.second) {
4129 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4131 queue.push_back(it->second);
4136 }
else if (pindexNew->
pprev &&
4139 std::make_pair(pindexNew->
pprev, pindexNew));
4159 "high-hash",
"proof of work failed");
4185 "bad-txnmrklroot",
"hashMerkleRoot mismatch");
4193 "bad-txns-duplicate",
"duplicate transaction");
4202 if (block.
vtx.empty()) {
4204 "bad-cb-missing",
"first tx is not coinbase");
4213 "bad-blk-length",
"size limits failed");
4217 if (currentBlockSize > nMaxBlockSize) {
4219 "bad-blk-length",
"size limits failed");
4227 strprintf(
"Coinbase check failed (txid %s) %s",
4228 block.
vtx[0]->GetId().ToString(),
4234 for (
size_t i = 1; i < block.
vtx.size(); i++) {
4235 auto *tx = block.
vtx[i].get();
4240 strprintf(
"Transaction check failed (txid %s) %s",
4255 return std::all_of(headers.cbegin(), headers.cend(),
4256 [&](
const auto &header) {
4257 return CheckProofOfWork(
4258 header.GetHash(), header.nBits, consensusParams);
4285 const std::optional<CCheckpointData> &test_checkpoints = std::nullopt)
4288 assert(pindexPrev !=
nullptr);
4289 const int nHeight = pindexPrev->nHeight + 1;
4295 LogPrintf(
"bad bits after height: %d\n", pindexPrev->nHeight);
4297 "bad-diffbits",
"incorrect proof of work");
4301 if (chainman.m_options.checkpoints_enabled) {
4303 test_checkpoints ? test_checkpoints.value() : params.
Checkpoints();
4309 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4312 "checkpoint mismatch");
4320 blockman.GetLastCheckpoint(checkpoints);
4321 if (pcheckpoint && nHeight < pcheckpoint->
nHeight) {
4323 "ERROR: %s: forked chain older than last checkpoint "
4327 "bad-fork-prior-to-checkpoint");
4332 if (block.
GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
4334 "time-too-old",
"block's timestamp is too early");
4338 if (block.
Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4341 "block timestamp too far in the future");
4375 const int nBlockHeight = active_chain_tip.
nHeight + 1;
4400 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
4403 bool enforce_locktime_median_time_past{
false};
4406 assert(pindexPrev !=
nullptr);
4407 enforce_locktime_median_time_past =
true;
4410 const int64_t nMedianTimePast =
4413 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past
4418 const bool fIsMagneticAnomalyEnabled =
4426 for (
const auto &ptx : block.
vtx) {
4428 if (fIsMagneticAnomalyEnabled) {
4429 if (prevTx && (tx.
GetId() <= prevTx->
GetId())) {
4439 strprintf(
"Transaction order is invalid (%s < %s)",
4462 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4464 block.
vtx[0]->vin[0].scriptSig.begin())) {
4467 "block height mismatch in coinbase");
4482 const std::optional<CCheckpointData> &test_checkpoints) {
4489 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4491 if (miSelf !=
m_blockman.m_block_index.end()) {
4498 if (pindex->nStatus.isInvalid()) {
4511 "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__,
4517 BlockMap::iterator mi{
4521 "header %s has prev block not found: %s\n",
4524 "prev-blk-not-found");
4529 if (pindexPrev->nStatus.isInvalid()) {
4531 "header %s has prev block invalid: %s\n", hash.
ToString(),
4541 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4572 if (pindexPrev->
GetAncestor(failedit->nHeight) == failedit) {
4573 assert(failedit->nStatus.hasFailed());
4575 while (invalid_walk != failedit) {
4576 invalid_walk->nStatus =
4577 invalid_walk->nStatus.withFailedParent();
4579 invalid_walk = invalid_walk->
pprev;
4582 "header %s has prev block invalid: %s\n",
4591 if (!min_pow_checked) {
4593 "%s: not adding new block header %s, missing anti-dos "
4594 "proof-of-work validation\n",
4597 "too-little-chainwork");
4610 const std::vector<CBlockHeader> &headers,
bool min_pow_checked,
4612 const std::optional<CCheckpointData> &test_checkpoints) {
4620 header, state, &pindex, min_pow_checked, test_checkpoints);
4621 ActiveChainstate().CheckBlockIndex();
4634 if (ActiveChainstate().IsInitialBlockDownload() && ppindex &&
4637 const int64_t blocks_left{
4638 (
GetTime() - last_accepted.GetBlockTime()) /
4640 const double progress{100.0 * last_accepted.nHeight /
4641 (last_accepted.nHeight + blocks_left)};
4642 LogPrintf(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4643 last_accepted.nHeight, progress);
4649 void ChainstateManager::ReportHeadersPresync(
const arith_uint256 &work,
4651 int64_t timestamp) {
4653 const auto &chainstate = ActiveChainstate();
4661 if (m_best_header->nChainWork >=
4667 auto now = Now<SteadyMilliseconds>();
4668 if (now < m_last_presync_update + 250ms) {
4671 m_last_presync_update = now;
4673 bool initial_download = chainstate.IsInitialBlockDownload();
4675 height, timestamp,
true);
4676 if (initial_download) {
4677 const int64_t blocks_left{(
GetTime() - timestamp) /
4679 const double progress{100.0 * height / (height + blocks_left)};
4680 LogPrintf(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4700 bool min_pow_checked) {
4703 const CBlock &block = *pblock;
4710 bool accepted_header{
4714 if (!accepted_header) {
4721 bool fAlreadyHave = pindex->nStatus.hasData();
4733 int64_t chainTipTimeDiff =
4739 LogPrintf(
"Chain tip timestamp-to-received-time difference: hash=%s, "
4742 LogPrintf(
"New block timestamp-to-received-time difference: hash=%s, "
4747 bool fHasMoreOrSameWork =
4756 bool fTooFarAhead{pindex->
nHeight >
4769 if (pindex->
nTx != 0) {
4774 if (!fHasMoreOrSameWork) {
4795 if (!
CheckBlock(block, state, consensusParams,
4800 pindex->nStatus = pindex->nStatus.withFailed();
4804 return error(
"%s: %s (block %s)", __func__, state.
ToString(),
4817 LogPrintf(
"Park block %s as it would cause a deep reorg.\n",
4819 pindex->nStatus = pindex->nStatus.withParked();
4838 if (blockPos.IsNull()) {
4840 "%s: Failed to find position to write new block to disk",
4845 }
catch (
const std::runtime_error &e) {
4846 return AbortNode(state, std::string(
"System error: ") + e.what());
4857 const std::shared_ptr<const CBlock> &block,
bool force_processing,
4858 bool min_pow_checked,
bool *new_block,
4887 ret = ActiveChainstate().AcceptBlock(block, state, force_processing,
4894 return error(
"%s: AcceptBlock FAILED (%s)", __func__,
4903 if (!ActiveChainstate().ActivateBestChain(state, block,
avalanche)) {
4904 return error(
"%s: ActivateBestChain failed (%s)", __func__,
4915 Chainstate &active_chainstate = ActiveChainstate();
4922 false, test_accept);
4938 indexDummy.
pprev = pindexPrev;
4945 adjusted_time_callback())) {
4946 return error(
"%s: Consensus::ContextualCheckBlockHeader: %s", __func__,
4951 return error(
"%s: Consensus::CheckBlock: %s", __func__,
4957 return error(
"%s: Consensus::ContextualCheckBlock: %s", __func__,
4961 if (!chainstate.
ConnectBlock(block, state, &indexDummy, viewNew,
4962 validationOptions,
nullptr,
true)) {
4972 int nManualPruneHeight) {
4975 nManualPruneHeight)) {
4976 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
4982 FopenFn mockable_fopen_function) {
5012 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5043 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
5047 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5048 LogPrintf(
"Verifying last %i blocks at level %i\n", nCheckDepth,
5054 int nGoodTransactions = 0;
5057 bool skipped_no_block_data{
false};
5058 bool skipped_l3_checks{
false};
5059 LogPrintf(
"Verification progress: 0%%\n");
5064 pindex = pindex->
pprev) {
5065 const int percentageDone = std::max(
5066 1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() -
5068 (
double)nCheckDepth *
5069 (nCheckLevel >= 4 ? 50 : 100))));
5070 if (reportDone < percentageDone / 10) {
5072 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
5073 reportDone = percentageDone / 10;
5082 !pindex->nStatus.hasData()) {
5085 LogPrintf(
"VerifyDB(): block verification stopping at height %d "
5086 "(no data). This could be due to pruning or use of an "
5087 "assumeutxo snapshot.\n",
5089 skipped_no_block_data =
true;
5098 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5104 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensusParams,
5107 "Verification error: found bad block at %d, hash=%s (%s)\n",
5114 if (nCheckLevel >= 2 && pindex) {
5118 LogPrintf(
"Verification error: found bad undo data at %d, "
5131 if (nCheckLevel >= 3) {
5135 chainstate.DisconnectBlock(block, pindex, coins);
5137 LogPrintf(
"Verification error: irrecoverable inconsistency "
5138 "in block data at %d, hash=%s\n",
5144 nGoodTransactions = 0;
5145 pindexFailure = pindex;
5147 nGoodTransactions += block.
vtx.size();
5150 skipped_l3_checks =
true;
5159 if (pindexFailure) {
5160 LogPrintf(
"Verification error: coin database inconsistencies found "
5161 "(last %i blocks, %i good transactions before that)\n",
5166 if (skipped_l3_checks) {
5167 LogPrintf(
"Skipped verification of level >=3 (insufficient database "
5168 "cache size). Consider increasing -dbcache.\n");
5175 if (nCheckLevel >= 4 && !skipped_l3_checks) {
5177 const int percentageDone = std::max(
5178 1, std::min(99, 100 -
int(
double(chainstate.
m_chain.
Height() -
5180 double(nCheckDepth) * 50)));
5181 if (reportDone < percentageDone / 10) {
5183 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
5184 reportDone = percentageDone / 10;
5191 LogPrintf(
"Verification error: ReadBlockFromDisk failed at %d, "
5196 if (!chainstate.
ConnectBlock(block, state, pindex, coins,
5198 LogPrintf(
"Verification error: found unconnectable block at "
5199 "%d, hash=%s (%s)\n",
5210 LogPrintf(
"Verification: No coin database inconsistencies in last %i "
5211 "blocks (%i transactions)\n",
5212 block_count, nGoodTransactions);
5214 if (skipped_l3_checks) {
5217 if (skipped_no_block_data) {
5233 return error(
"ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s",
5243 if (tx->IsCoinBase()) {
5247 for (
const CTxIn &txin : tx->vin) {
5261 std::vector<BlockHash> hashHeads =
db.GetHeadBlocks();
5262 if (hashHeads.empty()) {
5266 if (hashHeads.size() != 2) {
5267 return error(
"ReplayBlocks(): unknown inconsistent state");
5280 if (
m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5282 "ReplayBlocks(): reorganization to unknown block requested");
5285 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
5287 if (!hashHeads[1].IsNull()) {
5289 if (
m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5291 "ReplayBlocks(): reorganization from unknown block requested");
5294 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
5296 assert(pindexFork !=
nullptr);
5300 while (pindexOld != pindexFork) {
5305 return error(
"RollbackBlock(): ReadBlockFromDisk() failed at "
5316 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s",
5328 pindexOld = pindexOld->
pprev;
5332 int nForkHeight = pindexFork ? pindexFork->
nHeight : 0;
5339 _(
"Replaying blocks…"),
5340 (
int)((
nHeight - nForkHeight) * 100.0 /
5341 (pindexNew->
nHeight - nForkHeight)),
5374 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5376 std::vector<CBlockIndex *> vSortedByHeight{
5378 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5382 int first_assumed_valid_height = std::numeric_limits<int>::max();
5384 for (
const CBlockIndex *block : vSortedByHeight) {
5385 if (block->IsAssumedValid()) {
5386 auto chainstates =
GetAll();
5393 auto any_chain = [&](
auto fnc) {
5394 return std::any_of(chainstates.cbegin(), chainstates.cend(),
5397 assert(any_chain([](
auto chainstate) {
5400 assert(any_chain([](
auto chainstate) {
5404 first_assumed_valid_height = block->nHeight;
5405 LogPrintf(
"Saw first assumedvalid block at height %d (%s)\n",
5406 first_assumed_valid_height, block->
ToString());
5445 pindex->
nHeight < first_assumed_valid_height) {
5451 if (pindex->nStatus.isInvalid() &&
5453 pindex->
nChainWork > m_best_invalid->nChainWork)) {
5454 m_best_invalid = pindex;
5457 if (pindex->nStatus.isOnParkedChain() &&
5459 pindex->
nChainWork > m_best_parked->nChainWork)) {
5460 m_best_parked = pindex;
5464 (m_best_header ==
nullptr ||
5466 m_best_header = pindex;
5470 needs_init =
m_blockman.m_block_index.empty();
5480 LogPrintf(
"Initializing databases...\n");
5502 if (blockPos.IsNull()) {
5503 return error(
"%s: writing genesis block to disk failed", __func__);
5508 }
catch (
const std::runtime_error &e) {
5509 return error(
"%s: failed to write genesis block: %s", __func__,
5518 std::multimap<BlockHash, FlatFilePos> *blocks_with_unknown_parent,
5523 assert(!dbp == !blocks_with_unknown_parent);
5537 uint64_t nRewind = blkdat.GetPos();
5538 while (!blkdat.eof()) {
5543 blkdat.SetPos(nRewind);
5548 unsigned int nSize = 0;
5552 blkdat.FindByte(std::byte(params.
DiskMagic()[0]));
5553 nRewind = blkdat.GetPos() + 1;
5555 if (memcmp(buf, params.
DiskMagic().data(),
5565 }
catch (
const std::exception &) {
5573 const uint64_t nBlockPos{blkdat.GetPos()};
5575 dbp->
nPos = nBlockPos;
5577 blkdat.SetLimit(nBlockPos + nSize);
5585 nRewind = nBlockPos + nSize;
5586 blkdat.SkipTo(nRewind);
5590 std::shared_ptr<CBlock> pblock{};
5599 "%s: Out of order block %s, parent %s not known\n",
5600 __func__, hash.ToString(),
5602 if (dbp && blocks_with_unknown_parent) {
5603 blocks_with_unknown_parent->emplace(
5612 if (!pindex || !pindex->nStatus.hasData()) {
5615 blkdat.SetPos(nBlockPos);
5616 pblock = std::make_shared<CBlock>();
5618 nRewind = blkdat.GetPos();
5621 if (
AcceptBlock(pblock, state,
true, dbp,
nullptr,
5629 pindex->
nHeight % 1000 == 0) {
5632 "Block Import: already had block %s at height %d\n",
5633 hash.ToString(), pindex->
nHeight);
5658 "failed to activate chain (%s)\n",
5666 if (!blocks_with_unknown_parent) {
5672 std::deque<BlockHash> queue;
5673 queue.push_back(hash);
5674 while (!queue.empty()) {
5677 auto range = blocks_with_unknown_parent->equal_range(head);
5678 while (range.first != range.second) {
5679 std::multimap<BlockHash, FlatFilePos>::iterator it =
5681 std::shared_ptr<CBlock> pblockrecursive =
5682 std::make_shared<CBlock>();
5687 "%s: Processing out of order child %s of %s\n",
5688 __func__, pblockrecursive->GetHash().ToString(),
5693 &it->second,
nullptr,
true)) {
5695 queue.push_back(pblockrecursive->GetHash());
5699 blocks_with_unknown_parent->erase(it);
5703 }
catch (
const std::exception &e) {
5724 "%s: unexpected data at file offset 0x%x - %s. "
5726 __func__, (nRewind - 1), e.what());
5729 }
catch (
const std::runtime_error &e) {
5730 AbortNode(std::string(
"System error: ") + e.what());
5733 LogPrintf(
"Loaded %i blocks from external file in %dms\n", nLoaded,
5754 std::multimap<CBlockIndex *, CBlockIndex *> forward;
5755 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
5756 forward.emplace(block_index.pprev, &block_index);
5761 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
5762 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
5763 rangeGenesis = forward.equal_range(
nullptr);
5765 rangeGenesis.first++;
5767 assert(rangeGenesis.first == rangeGenesis.second);
5787 CBlockIndex *pindexFirstNotTransactionsValid =
nullptr;
5793 CBlockIndex *pindexFirstNotScriptsValid =
nullptr;
5794 while (pindex !=
nullptr) {
5796 if (pindexFirstInvalid ==
nullptr && pindex->nStatus.hasFailed()) {
5797 pindexFirstInvalid = pindex;
5799 if (pindexFirstParked ==
nullptr && pindex->nStatus.isParked()) {
5800 pindexFirstParked = pindex;
5804 if (pindexFirstMissing ==
nullptr && !pindex->nStatus.hasData() &&
5806 pindexFirstMissing = pindex;
5808 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) {
5809 pindexFirstNeverProcessed = pindex;
5811 if (pindex->
pprev !=
nullptr && pindexFirstNotTreeValid ==
nullptr &&
5813 pindexFirstNotTreeValid = pindex;
5816 if (pindexFirstNotTransactionsValid ==
nullptr &&
5818 pindexFirstNotTransactionsValid = pindex;
5820 if (pindexFirstNotChainValid ==
nullptr &&
5822 pindexFirstNotChainValid = pindex;
5824 if (pindexFirstNotScriptsValid ==
nullptr &&
5826 pindexFirstNotScriptsValid = pindex;
5831 if (pindex->
pprev ==
nullptr) {
5852 assert(pindex->nStatus.hasData() == (pindex->
nTx > 0));
5853 assert(pindexFirstMissing == pindexFirstNeverProcessed);
5854 }
else if (pindex->nStatus.hasData()) {
5859 if (pindex->nStatus.hasUndo()) {
5860 assert(pindex->nStatus.hasData());
5871 assert((pindex->nStatus.getValidity() >=
5879 assert((pindexFirstNeverProcessed ==
nullptr) ==
5881 assert((pindexFirstNotTransactionsValid ==
nullptr) ==
5893 assert(pindexFirstNotTreeValid ==
nullptr);
5896 assert(pindexFirstNotTreeValid ==
nullptr);
5900 assert(pindexFirstNotChainValid ==
nullptr);
5904 assert(pindexFirstNotScriptsValid ==
nullptr);
5906 if (pindexFirstInvalid ==
nullptr) {
5909 assert(!pindex->nStatus.isInvalid());
5911 if (pindexFirstParked ==
nullptr) {
5915 assert(!pindex->nStatus.isOnParkedChain());
5918 pindexFirstNeverProcessed ==
nullptr) {
5919 if (pindexFirstInvalid ==
nullptr) {
5924 if (
this == &
m_chainman.ActiveChainstate()) {
5928 if (pindexFirstMissing ==
nullptr) {
5929 assert(pindex->nStatus.isOnParkedChain() ||
5950 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
5951 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
5954 bool foundInUnlinked =
false;
5955 while (rangeUnlinked.first != rangeUnlinked.second) {
5956 assert(rangeUnlinked.first->first == pindex->
pprev);
5957 if (rangeUnlinked.first->second == pindex) {
5958 foundInUnlinked =
true;
5961 rangeUnlinked.first++;
5963 if (pindex->
pprev && pindex->nStatus.hasData() &&
5964 pindexFirstNeverProcessed !=
nullptr &&
5965 pindexFirstInvalid ==
nullptr) {
5971 if (!pindex->nStatus.hasData()) {
5973 assert(!foundInUnlinked);
5975 if (pindexFirstMissing ==
nullptr) {
5978 assert(!foundInUnlinked);
5980 if (pindex->
pprev && pindex->nStatus.hasData() &&
5981 pindexFirstNeverProcessed ==
nullptr &&
5982 pindexFirstMissing !=
nullptr) {
5998 if (pindexFirstInvalid ==
nullptr) {
6008 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6009 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6010 range = forward.equal_range(pindex);
6011 if (range.first != range.second) {
6013 pindex = range.first->second;
6023 if (pindex == pindexFirstInvalid) {
6024 pindexFirstInvalid =
nullptr;
6026 if (pindex == pindexFirstParked) {
6027 pindexFirstParked =
nullptr;
6029 if (pindex == pindexFirstMissing) {
6030 pindexFirstMissing =
nullptr;
6032 if (pindex == pindexFirstNeverProcessed) {
6033 pindexFirstNeverProcessed =
nullptr;
6035 if (pindex == pindexFirstNotTreeValid) {
6036 pindexFirstNotTreeValid =
nullptr;
6038 if (pindex == pindexFirstNotTransactionsValid) {
6039 pindexFirstNotTransactionsValid =
nullptr;
6041 if (pindex == pindexFirstNotChainValid) {
6042 pindexFirstNotChainValid =
nullptr;
6044 if (pindex == pindexFirstNotScriptsValid) {
6045 pindexFirstNotScriptsValid =
nullptr;
6050 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6051 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6052 rangePar = forward.equal_range(pindexPar);
6053 while (rangePar.first->second != pindex) {
6056 assert(rangePar.first != rangePar.second);
6061 if (rangePar.first != rangePar.second) {
6063 pindex = rangePar.first->second;
6075 assert(nNodes == forward.size());
6081 return strprintf(
"Chainstate [%s] @ height %d (%s)",
6087 bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size) {
6100 coinsdb_size * (1.0 / 1024 / 1024));
6102 coinstip_size * (1.0 / 1024 / 1024));
6107 if (coinstip_size > old_coinstip_size) {
6124 if (pindex ==
nullptr) {
6128 int64_t nNow = time(
nullptr);
6143 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6145 return m_active_chainstate->m_from_snapshot_blockhash;
6147 return std::nullopt;
6152 std::vector<Chainstate *> out;
6155 {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6156 if (this->IsUsable(pchainstate)) {
6157 out.push_back(pchainstate);
6166 assert(!m_ibd_chainstate);
6167 assert(!m_active_chainstate);
6169 m_ibd_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman, *
this);
6170 m_active_chainstate = m_ibd_chainstate.get();
6171 return *m_active_chainstate;
6177 const auto assumeutxo_found = valid_assumeutxos_map.find(height);
6179 if (assumeutxo_found != valid_assumeutxos_map.end()) {
6180 return &assumeutxo_found->second;
6194 const bool existed{fs::remove(base_blockhash_path)};
6196 LogPrintf(
"[snapshot] snapshot chainstate dir being removed "
6200 }
catch (
const fs::filesystem_error &e) {
6201 LogPrintf(
"[snapshot] failed to remove file %s: %s\n",
6208 LogPrintf(
"Removing leveldb dir at %s\n", path_str);
6212 const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
6215 LogPrintf(
"error: leveldb DestroyDB call failed on %s\n", path_str);
6233 LogPrintf(
"[snapshot] can't activate a snapshot-based chainstate more "
6238 int64_t current_coinsdb_cache_size{0};
6239 int64_t current_coinstip_cache_size{0};
6248 static constexpr
double IBD_CACHE_PERC = 0.01;
6249 static constexpr
double SNAPSHOT_CACHE_PERC = 0.99;
6262 current_coinsdb_cache_size =
6263 this->ActiveChainstate().m_coinsdb_cache_size_bytes;
6264 current_coinstip_cache_size =
6265 this->ActiveChainstate().m_coinstip_cache_size_bytes;
6269 this->ActiveChainstate().ResizeCoinsCaches(
6270 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6271 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6274 auto snapshot_chainstate =
6281 snapshot_chainstate->InitCoinsDB(
6282 static_cast<size_t>(current_coinsdb_cache_size *
6283 SNAPSHOT_CACHE_PERC),
6284 in_memory,
false,
"chainstate");
6285 snapshot_chainstate->InitCoinsCache(
static_cast<size_t>(
6286 current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
6290 coins_file, metadata);
6297 snapshot_ok =
false;
6302 this->MaybeRebalanceCaches();
6311 snapshot_chainstate.reset();
6316 strprintf(
"Failed to remove snapshot chainstate dir (%s). "
6317 "Manually remove it before restarting.\n",
6326 assert(!m_snapshot_chainstate);
6327 m_snapshot_chainstate.swap(snapshot_chainstate);
6328 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6331 m_active_chainstate = m_snapshot_chainstate.get();
6333 LogPrintf(
"[snapshot] successfully activated snapshot %s\n",
6336 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() /
6339 this->MaybeRebalanceCaches();
6345 bool snapshot_loaded) {
6348 snapshot_loaded ?
"saving snapshot chainstate"
6349 :
"flushing coins cache",
6351 BCLog::LogFlags::ALL);
6353 coins_cache.
Flush();
6357 const char *
what()
const throw()
override {
6358 return "ComputeUTXOStats interrupted by shutdown.";
6382 if (!snapshot_start_block) {
6385 LogPrintf(
"[snapshot] Did not find snapshot start blockheader %s\n",
6390 int base_height = snapshot_start_block->
nHeight;
6393 if (!maybe_au_data) {
6394 LogPrintf(
"[snapshot] assumeutxo height in snapshot metadata not "
6395 "recognized (%d) - refusing to load snapshot\n",
6407 LogPrintf(
"[snapshot] loading coins from snapshot %s\n",
6409 int64_t coins_processed{0};
6411 while (coins_left > 0) {
6413 coins_file >> outpoint;
6415 }
catch (
const std::ios_base::failure &) {
6416 LogPrintf(
"[snapshot] bad snapshot format or truncated snapshot "
6417 "after deserializing %d coins\n",
6418 coins_count - coins_left);
6421 if (coin.
GetHeight() > uint32_t(base_height) ||
6424 std::numeric_limits<decltype(outpoint.
GetN())>::max()) {
6426 "[snapshot] bad snapshot data after deserializing %d coins\n",
6427 coins_count - coins_left);
6436 if (coins_processed % 1000000 == 0) {
6437 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6439 static_cast<float>(coins_processed) * 100 /
6440 static_cast<float>(coins_count),
6448 if (coins_processed % 120000 == 0) {
6453 const auto snapshot_cache_state =
WITH_LOCK(
6454 ::
cs_main,
return snapshot_chainstate.GetCoinsCacheSizeState());
6477 bool out_of_coins{
false};
6479 coins_file >> outpoint;
6480 }
catch (
const std::ios_base::failure &) {
6482 out_of_coins =
true;
6484 if (!out_of_coins) {
6485 LogPrintf(
"[snapshot] bad snapshot - coins left over after "
6486 "deserializing %d coins\n",
6491 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6505 std::optional<CCoinsStats> maybe_stats;
6514 if (!maybe_stats.has_value()) {
6515 LogPrintf(
"[snapshot] failed to generate coins stats\n");
6523 LogPrintf(
"[snapshot] bad snapshot content hash: expected %s, got %s\n",
6525 maybe_stats->hashSerialized.ToString());
6542 constexpr
int AFTER_GENESIS_START{1};
6544 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height();
6546 index = snapshot_chainstate.
m_chain[i];
6561 index->nStatus = index->nStatus.withAssumedValid();
6576 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
6599 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
6600 !this->IsUsable(m_snapshot_chainstate.get()) ||
6601 !this->IsUsable(m_ibd_chainstate.get()) ||
6602 !m_ibd_chainstate->m_chain.Tip()) {
6608 const int snapshot_base_height = *
Assert(this->GetSnapshotBaseHeight());
6611 if (index_new.
nHeight < snapshot_base_height) {
6621 _(
"%s failed to validate the -assumeutxo snapshot state. "
6622 "This indicates a hardware problem, or a bug in the software, or "
6623 "a bad software modification that allowed an invalid snapshot to "
6624 "be loaded. As a result of this, the node will shut down and "
6625 "stop using any state that was built on the snapshot, resetting "
6626 "the chain height from %d to %d. On the next restart, the node "
6627 "will resume syncing from %d without using any snapshot data. "
6628 "Please report this incident to %s, including how you obtained "
6629 "the snapshot. The invalid snapshot chainstate will be left on "
6630 "disk in case it is helpful in diagnosing the issue that caused "
6632 PACKAGE_NAME, snapshot_tip_height, snapshot_base_height,
6633 snapshot_base_height, PACKAGE_BUGREPORT);
6636 LogPrintf(
"[snapshot] deleting snapshot, reverting to validated chain, "
6637 "and stopping node\n");
6639 m_active_chainstate = m_ibd_chainstate.get();
6640 m_snapshot_chainstate->m_disabled =
true;
6641 assert(!this->IsUsable(m_snapshot_chainstate.get()));
6642 assert(this->IsUsable(m_ibd_chainstate.get()));
6644 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
6645 if (!rename_result) {
6650 shutdown_fnc(user_error);
6655 "[snapshot] supposed base block %s does not match the "
6656 "snapshot base block %s (height %d). Snapshot is not valid.\n",
6658 snapshot_base_height);
6659 handle_invalid_snapshot();
6665 int curr_height = m_ibd_chainstate->m_chain.Height();
6667 assert(snapshot_base_height == curr_height);
6669 assert(this->IsUsable(m_snapshot_chainstate.get()));
6672 CCoinsViewDB &ibd_coins_db = m_ibd_chainstate->CoinsDB();
6673 m_ibd_chainstate->ForceFlushStateToDisk();
6676 if (!maybe_au_data) {
6677 LogPrintf(
"[snapshot] assumeutxo data not found for height "
6678 "(%d) - refusing to validate snapshot\n",
6680 handle_invalid_snapshot();
6685 std::optional<CCoinsStats> maybe_ibd_stats;
6687 "[snapshot] computing UTXO stats for background chainstate to validate "
6688 "snapshot - this could take a few minutes\n");
6697 if (!maybe_ibd_stats) {
6699 "[snapshot] failed to generate stats for validation coins db\n");
6703 handle_invalid_snapshot();
6706 const auto &ibd_stats = *maybe_ibd_stats;
6715 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
6716 ibd_stats.hashSerialized.ToString(),
6718 handle_invalid_snapshot();
6722 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
6725 m_ibd_chainstate->m_disabled =
true;
6726 this->MaybeRebalanceCaches();
6731 Chainstate &ChainstateManager::ActiveChainstate()
const {
6733 assert(m_active_chainstate);
6734 return *m_active_chainstate;
6739 return m_snapshot_chainstate &&
6740 m_active_chainstate == m_snapshot_chainstate.get();
6742 void ChainstateManager::MaybeRebalanceCaches() {
6744 bool ibd_usable = this->IsUsable(m_ibd_chainstate.get());
6745 bool snapshot_usable = this->IsUsable(m_snapshot_chainstate.get());
6746 assert(ibd_usable || snapshot_usable);
6748 if (ibd_usable && !snapshot_usable) {
6749 LogPrintf(
"[snapshot] allocating all cache to the IBD chainstate\n");
6753 }
else if (snapshot_usable && !ibd_usable) {
6757 "[snapshot] allocating all cache to the snapshot chainstate\n");
6761 }
else if (ibd_usable && snapshot_usable) {
6767 if (m_snapshot_chainstate->IsInitialBlockDownload()) {
6770 m_snapshot_chainstate->ResizeCoinsCaches(
6773 m_snapshot_chainstate->ResizeCoinsCaches(
6781 void ChainstateManager::ResetChainstates() {
6782 m_ibd_chainstate.reset();
6783 m_snapshot_chainstate.reset();
6784 m_active_chainstate =
nullptr;
6793 if (!opts.check_block_index.has_value()) {
6795 opts.config.GetChainParams().DefaultConsistencyChecks();
6798 if (!opts.minimum_chain_work.has_value()) {
6800 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
6802 if (!opts.assumed_valid_block.has_value()) {
6803 opts.assumed_valid_block =
6804 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
6806 Assert(opts.adjusted_time_callback);
6807 return std::move(opts);
6812 : m_options{
Flatten(
std::move(options))}, m_blockman{
std::move(
6813 blockman_options)} {}
6815 bool ChainstateManager::DetectSnapshotChainstate(
CTxMemPool *mempool) {
6816 assert(!m_snapshot_chainstate);
6821 std::optional<BlockHash> base_blockhash =
6823 if (!base_blockhash) {
6826 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
6829 this->ActivateExistingSnapshot(mempool, *base_blockhash);
6834 ChainstateManager::ActivateExistingSnapshot(
CTxMemPool *mempool,
6836 assert(!m_snapshot_chainstate);
6837 m_snapshot_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman,
6838 *
this, base_blockhash);
6839 LogPrintf(
"[snapshot] switching active chainstate to %s\n",
6840 m_snapshot_chainstate->ToString());
6841 m_active_chainstate = m_snapshot_chainstate.get();
6842 return *m_snapshot_chainstate;
6851 assert(storage_path_maybe);
6852 fs::path snapshot_datadir = *storage_path_maybe;
6857 auto invalid_path = snapshot_datadir +
"_INVALID";
6860 LogPrintf(
"[snapshot] renaming snapshot datadir %s to %s\n", dbpath,
6867 fs::rename(snapshot_datadir, invalid_path);
6868 }
catch (
const fs::filesystem_error &e) {
6872 LogPrintf(
"%s: error renaming file '%s' -> '%s': %s\n", __func__,
6873 src_str, dest_str, e.what());
6875 "You should resolve this by manually "
6876 "moving or deleting the invalid "
6877 "snapshot directory %s, otherwise you "
6878 "will encounter the same error again "
6879 "on the next startup."),
6880 src_str, dest_str, src_str)};
6885 const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock()
const {
6887 if (!blockhash_op) {
6893 std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const {
6894 const CBlockIndex *base = this->GetSnapshotBaseBlock();
6895 return base ? std::make_optional(base->
nHeight) :
std::nullopt;
6898 bool ChainstateManager::ValidatedSnapshotCleanup() {
6901 ::
cs_main) -> std::optional<fs::path> {
6907 std::optional<fs::path> ibd_chainstate_path_maybe =
6908 get_storage_path(m_ibd_chainstate);
6909 std::optional<fs::path> snapshot_chainstate_path_maybe =
6910 get_storage_path(m_snapshot_chainstate);
6919 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
6920 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with "
6921 "in-memory chainstates. You are testing, right?\n");
6925 const auto &snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
6926 const auto &ibd_chainstate_path = *ibd_chainstate_path_maybe;
6934 this->ResetChainstates();
6939 LogPrintf(
"[snapshot] deleting background chainstate directory (now "
6940 "unnecessary) (%s)\n",
6943 fs::path tmp_old{ibd_chainstate_path +
"_todelete"};
6946 const fs::filesystem_error &err) {
6950 "Rename of '%s' -> '%s' failed. "
6951 "Cannot clean up the background chainstate leveldb directory.",
6956 fs::rename(ibd_chainstate_path, tmp_old);
6957 }
catch (
const fs::filesystem_error &e) {
6958 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
6962 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to "
6963 "default chainstate directory (%s)\n",
6968 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
6969 }
catch (
const fs::filesystem_error &e) {
6970 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
6977 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the "
6978 "directory is now unnecessary.\n",
6981 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
bool IsDAAEnabled(const Consensus::Params ¶ms, int nHeight)
bool IsUAHFenabled(const Consensus::Params ¶ms, int nHeight)
static bool IsPhononEnabled(const Consensus::Params ¶ms, int32_t nHeight)
static bool IsGravitonEnabled(const Consensus::Params ¶ms, int32_t nHeight)
bool IsMagneticAnomalyEnabled(const Consensus::Params ¶ms, int32_t nHeight)
Check if Nov 15, 2018 HF has activated using block height.
bool MoneyRange(const Amount nValue)
static constexpr Amount SATOSHI
static constexpr Amount COIN
arith_uint256 UintToArith256(const uint256 &a)
@ CHAIN
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends,...
@ TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
@ SCRIPTS
Scripts & signatures ok.
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
arith_uint256 GetBlockProof(const CBlockIndex &block)
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
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...
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
const fs::path & GetBlocksDirPath() const
Get blocks directory path.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Non-refcounted RAII wrapper for FILE*.
std::string ToString() const
uint64_t getExcessiveBlockSize() const
BlockValidationOptions withCheckPoW(bool _checkPoW=true) const
BlockValidationOptions withCheckMerkleRoot(bool _checkMerkleRoot=true) const
BlockValidationOptions(const Config &config)
bool shouldValidatePoW() const
bool shouldValidateMerkleRoot() const
std::string ToString() const
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
std::string ToString() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
const BlockHash * phashBlock
pointer to the hash of the block, if any.
bool HaveTxsDownloaded() const
Check whether this block's and all previous blocks' transactions have been downloaded (and stored to ...
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
int64_t GetReceivedTimeDiff() const
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
bool UpdateChainStats()
Update chain tx stats.
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
unsigned int nTx
Number of transactions in this block.
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
int32_t nVersion
block header
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
BlockHash GetBlockHash() const
unsigned int nSize
Size of this block.
int nHeight
height of the entry in the chain. The genesis block has height 0
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
int Height() const
Return the maximal height in the chain.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const ChainTxData & TxData() const
const CCheckpointData & Checkpoints() const
const MapAssumeutxo & Assumeutxo() const
Get allowed assumeutxo configuration.
const CMessageHeader::MessageMagic & DiskMagic() const
const Consensus::Params & GetConsensus() const
uint64_t PruneAfterHeight() const
const CBlock & GenesisBlock() const
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
void Add(std::vector< T > &&vChecks)
Queue for verifications that have to be performed.
void SetBackend(CCoinsView &viewIn)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
void SetBestBlock(const BlockHash &hashBlock)
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
bool Flush()
Push the modifications applied to this cache to its base and wipe local state.
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
CCoinsView backed by the coin database (chainstate/)
std::optional< fs::path > StoragePath()
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Abstract view on the open txout dataset.
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
CCoinsView that brings transactions from a mempool into view.
Fee rate in satoshis per kilobyte: Amount / kB.
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockChecked(const CBlock &, const BlockValidationState &)
void BlockFinalized(const CBlockIndex *)
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin >>, uint64_t mempool_sequence)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void ChainStateFlushed(const CBlockLocator &)
An outpoint - a combination of a transaction hash and an index n into its vout.
const TxId & GetTxId() const
void insert(Span< const uint8_t > vKey)
bool contains(Span< const uint8_t > vKey) const
Closure representing one script verification.
ScriptError GetScriptError() const
ScriptExecutionMetrics GetScriptExecutionMetrics() const
TxSigCheckLimiter * pTxLimitSigChecks
ScriptExecutionMetrics metrics
PrecomputedTransactionData txdata
const CTransaction * ptxTo
CheckInputsLimiter * pBlockLimitSigChecks
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
const std::vector< CTxIn > vin
An input of a transaction.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
void AddTransactionsUpdated(unsigned int n)
const int64_t m_max_size_bytes
size_t DynamicMemoryUsage() const
void SetLoadTried(bool load_tried)
Set whether or not we've made an attempt to load the mempool (regardless of whether the attempt was s...
Restore the UTXO in a Coin at a given COutPoint.
std::vector< Coin > vprevout
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::Notifications & m_notifications
Chainstate stores and provides an API to update our local knowledge of the current best chain.
std::set< CBlockIndex *, CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors...
bool IsBlockAvalancheFinalized(const CBlockIndex *pindex) const EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Checks if a block is finalized by avalanche voting.
const std::optional< BlockHash > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
CTxMemPool * GetMempool()
void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mutex m_chainstate_mutex
The ChainState Mutex.
void UnloadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void UpdateFlags(CBlockIndex *pindex, CBlockIndex *&pindexReset, F f, C fChild, AC fAncestorWasChanged) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CChain m_chain
The current chain of blockheaders we consult and build on.
bool HasCoinsViews() const
Does this chainstate have a UTXO set attached?
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, ConnectTrace &connectTrace, const avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
void UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< BlockHash > from_snapshot_blockhash=std::nullopt)
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Mutex cs_avalancheFinalizedBlockIndex
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(std::chrono::microsecond m_last_write)
Check warning conditions and do some notifications on new chain tip set.
void UnparkBlockAndChildren(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block and its descendants.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
bool reliesOnAssumedValid()
Return true if this chainstate relies on blocks that are assumed-valid.
void LoadMempool(const fs::path &load_path, fsbridge::FopenFn mockable_fopen_function=fsbridge::fopen)
Load the persisted mempool from disk.
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
std::chrono::microseconds m_last_flush
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
bool UnwindBlock(BlockValidationState &state, CBlockIndex *pindex, bool invalidate) EXCLUSIVE_LOCKS_REQUIRED(m_chainstate_mutex
bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as invalid.
bool ConnectTip(BlockValidationState &state, BlockPolicyValidationState &blockPolicyState, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, ConnectTrace &connectTrace, DisconnectedBlockTransactions &disconnectpool, const avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
CRollingBloomFilter m_filterParkingPoliciesApplied
Filter to prevent parking a block due to block policies more than once.
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
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).
void ResetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove invalidity status from a block and its descendants.
void CheckBlockIndex()
Make various assertions about the state of the block index.
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo ConnectBlock)(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, BlockValidationOptions options, Amount *blockFees=nullptr, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of this block (with given index) on the UTXO set represented by coins.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
CBlockIndex const * m_best_fork_tip
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
bool FlushStateToDisk(BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
Update the on-disk chain state.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
CBlockIndex const * m_best_fork_base
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main
std::atomic< bool > m_cached_finished_ibd
Whether this chainstate is undergoing initial block download.
void UnparkBlock(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block.
bool PreciousBlock(BlockValidationState &state, CBlockIndex *pindex, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as precious and reorganize.
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr, bool skip_checkblockindex=false) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
void ClearAvalancheFinalizedBlock() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Clear avalanche finalization.
const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
std::atomic< int32_t > nBlockSequenceId
Every received block is assigned a unique and increasing identifier, so we know which one to give pri...
CBlockIndex * FindMostWorkChain(std::vector< const CBlockIndex * > &blocksToReconcile, bool fAutoUnpark) 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...
bool UpdateFlagsForBlock(CBlockIndex *pindexBase, CBlockIndex *pindex, F f) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool AcceptBlock(const std::shared_ptr< const CBlock > &pblock, BlockValidationState &state, bool fRequested, const FlatFilePos *dbp, bool *fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Store a block on disk.
bool ParkBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Park a block.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
const CChainParams & GetParams() const
const arith_uint256 & MinimumChainWork() const
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
bool AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't desce...
bool ShouldCheckBlockIndex() const
const Config & GetConfig() const
const BlockHash & AssumedValidBlock() const
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &block, bool force_processing, bool min_pow_checked, bool *new_block, avalanche::Processor *const avalanche=nullptr) LOCKS_EXCLUDED(cs_main)
Process an incoming block.
std::optional< BlockHash > SnapshotBlockhash() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
bool IsSnapshotActive() const
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &block, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex=nullptr, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) LOCKS_EXCLUDED(cs_main)
Process incoming block headers.
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
ChainstateManager(Options options, node::BlockManager::Options blockman_options)
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
std::set< CBlockIndex * > m_failed_blocks
In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to conn...
kernel::Notifications & GetNotifications() const
const Consensus::Params & GetConsensus() const
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
uint32_t GetHeight() const
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
virtual const CChainParams & GetChainParams() const =0
Used to track blocks whose transactions were applied to the UTXO state as a part of a single Activate...
std::vector< PerBlockConnectTrace > blocksConnected
std::vector< PerBlockConnectTrace > & GetBlocksConnected()
void BlockConnected(CBlockIndex *pindex, std::shared_ptr< const CBlock > pblock)
void updateMempoolForReorg(Chainstate &active_chainstate, bool fAddToMempool, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transac...
void addForBlock(const std::vector< CTransactionRef > &vtx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
void importMempool(CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
Different type to mark Mutex at global scope.
static RCUPtr acquire(T *&ptrIn)
Acquire ownership of some pointer.
The script cache is a map using a key/value element, that caches the success of executing a specific ...
static TxSigCheckLimiter getDisabled()
std::string GetRejectReason() const
std::string GetDebugMessage() const
bool Error(const std::string &reject_reason)
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
std::string ToString() const
256-bit unsigned big integer.
std::string ToString() const
std::string GetHex() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
A base class defining functions for notifying about certain kernel events.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
virtual void warning(const std::string &warning)
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
virtual void blockTip(SynchronizationState state, CBlockIndex &index)
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
RecursiveMutex cs_LastBlockFile
void FindFilesToPrune(std::set< int > &setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex &start_block) EXCLUSIVE_LOCKS_REQUIRED(bool m_have_pruned
Find the first block that is not pruned.
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const FlatFilePos *dbp)
Store block on disk.
bool LoadingBlocks() const
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
bool IsPruneMode() const
Whether running in -prune mode.
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.
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
const Coin & AccessByTxid(const CCoinsViewCache &view, const TxId &txid)
Utility function to find any unspent output with a given txid.
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_CHILD_BEFORE_PARENT
This tx outputs are already spent in the mempool.
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/etc limits
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_DUPLICATE
Tx already in mempool or in the chain.
@ TX_INPUTS_NOT_STANDARD
inputs failed policy rules
@ TX_CONFLICT
Tx conflicts with another mempool tx, i.e.
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_CONSENSUS
invalid by consensus rules
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
uint64_t GetMaxBlockSigChecksCount(uint64_t maxBlockSize)
Compute the maximum number of sigchecks that can be contained in a block given the MAXIMUM block size...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep)
Determine if a deployment is active for the next block.
bool DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep)
Determine if a deployment is active for this block.
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror)
Execute an unlocking and locking script together.
std::map< int, const AssumeutxoData > MapAssumeutxo
static void LoadExternalBlockFile(benchmark::Bench &bench)
The LoadExternalBlockFile() function is used during -reindex and -loadblock.
bool error(const char *fmt, const Args &...args)
#define LogPrint(category,...)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
bool CheckBlock(const CCheckpointData &data, int nHeight, const BlockHash &hash)
Returns true if block passes checkpoint checks.
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, Amount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts).
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
std::string get_filesystem_error_message(const fs::filesystem_error &e)
std::function< FILE *(const fs::path &, const char *)> FopenFn
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
bool LoadMempool(CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, FopenFn mockable_fopen_function)
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::optional< fs::path > FindSnapshotChainstateDir()
Return a path to the snapshot-based chainstate dir, if one exists.
std::unordered_map< BlockHash, CBlockIndex, BlockHasher > BlockMap
std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir)
static constexpr unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
std::atomic_bool fReindex
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< BlockHash > constexpr ReadSnapshotBaseBlockhash(const fs::path &chaindir) EXCLUSIVE_LOCKS_REQUIRED(std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
bilingual_str ErrorString(const Result< T > &result)
std::shared_ptr< Chain::Notifications > m_notifications
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
bool CheckPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
@ PCKG_MEMPOOL_ERROR
Mempool logic error.
@ PCKG_TX
At least one tx is invalid.
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, uint32_t flags)
Check transaction inputs to mitigate two potential denial-of-service attacks:
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
static constexpr uint32_t STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
static constexpr uint32_t STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
static constexpr uint32_t STANDARD_NOT_MANDATORY_VERIFY_FLAGS
For convenience, standard but not mandatory verify flags.
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
reverse_range< T > reverse_iterate(T &x)
std::string ScriptErrorString(const ScriptError serror)
@ SIGCHECKS_LIMIT_EXCEEDED
@ SCRIPT_VERIFY_SIGPUSHONLY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_ENABLE_REPLAY_PROTECTION
@ SCRIPT_ENABLE_SCHNORR_MULTISIG
@ SCRIPT_VERIFY_STRICTENC
@ SCRIPT_ENFORCE_SIGCHECKS
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_MINIMALDATA
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
@ SCRIPT_ENABLE_SIGHASH_FORKID
void AddKeyInScriptCache(ScriptCacheKey key, int nSigChecks)
Add an entry in the cache.
bool IsKeyInScriptCache(ScriptCacheKey key, bool erase, int &nSigChecksOut)
Check if a given key is in the cache, and if so, return its values.
size_t GetSerializeSize(const T &t, int nVersion=0)
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
void StartShutdown()
Request shutdown of the application.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
static constexpr Amount zero() noexcept
Holds configuration for use during UTXO snapshot load and validation.
const AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
const unsigned int nChainTx
Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
A BlockHash is a unqiue identifier for a block.
bool isValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< BlockHash > vHave
Holds various statistics on transactions within a chain.
User-controlled performance and debug options.
Parameters that influence chain consensus.
int BIP34Height
Block height and hash at which BIP34 becomes active.
int nSubsidyHalvingInterval
BlockHash hashGenesisBlock
int64_t nPowTargetSpacing
int augustoActivationTime
Unix time used for MTP activation of 15 Nov 2024 12:00:00 UTC upgrade.
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
@ VALID
Fully validated, valid.
static MempoolAcceptResult Failure(TxValidationState state)
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
static MempoolAcceptResult Success(int64_t vsize, Amount fees, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
Constructor for success case.
static MempoolAcceptResult MempoolTx(int64_t vsize, Amount fees)
Constructor for already-in-mempool case.
std::chrono::time_point< NodeClock > time_point
Validation result for package mempool acceptance.
std::shared_ptr< const CBlock > pblock
Precompute sighash midstate to avoid quadratic hashing.
const char * what() const override
A TxId is the identifier of a transaction.
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
const std::function< NodeClock::time_point()> adjusted_time_callback
std::optional< bool > check_block_index
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
CoinsViewOptions coins_view
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define LOCKS_EXCLUDED(...)
#define NO_THREAD_SAFETY_ANALYSIS
int64_t GetTimeMicros()
Returns the system time (not mockable)
int64_t GetTimeMillis()
Returns the system time (not mockable)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
#define TRACE6(context, event, a, b, c, d, e, f)
#define TRACE5(context, event, a, b, c, d, e)
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
bool CheckRegularTransaction(const CTransaction &tx, TxValidationState &state)
Context-independent validity checks for coinbase and non-coinbase transactions.
bool CheckCoinbase(const CTransaction &tx, TxValidationState &state)
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...
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
bool ContextualCheckTransaction(const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state, int nHeight, int64_t nMedianTimePast)
Context dependent validity checks for non coinbase transactions.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
uint256 uint256S(const char *str)
uint256 from const char *.
static bool DeleteCoinsDBFromDisk(const fs::path &db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
void StartScriptCheckWorkerThreads(int threads_num)
Run instances of script checking worker threads.
static int64_t nTimeConnectTotal
GlobalMutex g_best_block_mutex
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
std::condition_variable g_best_block_cv
arith_uint256 CalculateHeadersWork(const std::vector< CBlockHeader > &headers)
Return the sum of the work on a given set of headers.
DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view)
Undo a block from the block and the undoblock data.
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index require cs_main if pindex h...
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept, unsigned int heightOverride)
Try to add a transaction to the mempool.
static int64_t nBlocksTotal
static int64_t nTimePostConnect
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params ¶ms, BlockValidationOptions validationOptions)
Return true if the provided block header is valid.
static SynchronizationState GetSynchronizationState(bool init)
static void SnapshotUTXOHashBreakpoint()
static int64_t nTimeFlush
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 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 uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex, const ChainstateManager &chainman)
const CBlockIndex * g_best_block
Used to notify getblocktemplate RPC of new tips.
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL
Time to wait between flushing chainstate to disk.
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Validate (and maybe submit) a package to the mempool.
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
static int64_t nTimeVerify
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams &chainparams)
Return the expected assumeutxo value for a given height, if one exists.
void StopScriptCheckWorkerThreads()
Stop all of the script checking worker threads.
static void LimitValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
return CheckInputScripts(tx, state, view, flags, true, true, txdata, nSigChecksOut)
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, const uint32_t flags, PrecomputedTransactionData &txdata, int &nSigChecksOut, CCoinsViewCache &coins_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Checks to avoid mempool polluting consensus critical paths since cached signature and script validity...
void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Mark all the coins corresponding to a given transaction inputs as spent.
static int64_t nTimeTotal
static int64_t nTimeConnect
static bool NotifyHeaderTip(Chainstate &chainstate) LOCKS_EXCLUDED(cs_main)
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params ¶ms, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
bool ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state)
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
DisconnectResult UndoCoinSpend(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static int64_t nTimeIndex
bool TestBlockValidity(BlockValidationState &state, const CChainParams ¶ms, Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev, const std::function< NodeClock::time_point()> &adjusted_time_callback, BlockValidationOptions validationOptions)
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
Calculate LockPoints required to check if transaction will be BIP68 final in the next block to be cre...
bool AbortNode(BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage)
void UpdateCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Apply the effects of this transaction on the UTXO set represented by view.
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, ChainstateManager &chainman, const CBlockIndex *pindexPrev, NodeClock::time_point now, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
static int64_t nTimeForks
static int64_t nTimeCheck
static void UpdateTipLog(const CCoinsViewCache &coins_tip, const CBlockIndex *tip, const CChainParams ¶ms, const std::string &func_name, const std::string &prefix) EXCLUSIVE_LOCKS_REQUIRED(
static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL
Time to wait between writing blocks/block index to disk.
static int64_t nTimeChainState
static CCheckQueue< CScriptCheck > scriptcheckqueue(128)
static int64_t nTimeReadFromDisk
static bool IsReplayProtectionEnabled(const Consensus::Params ¶ms, int64_t nMedianTimePast)
#define MIN_TRANSACTION_SIZE
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
@ BASE_BLOCKHASH_MISMATCH
SynchronizationState
Current sync state passed to tip changed callbacks.
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.
static const int DEFAULT_STOPATHEIGHT
Default for -stopatheight.
CMainSignals & GetMainSignals()
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static const int PROTOCOL_VERSION
network protocol versioning
void SetfLargeWorkInvalidChainFound(bool flag)
void SetfLargeWorkForkFound(bool flag)
bool GetfLargeWorkForkFound()