101 "level 0 reads the blocks from disk",
102 "level 1 verifies block validity",
103 "level 2 verifies undo data",
104 "level 3 checks disconnection of tip blocks",
105 "level 4 tries to reconnect the blocks",
106 "each level includes the checks of the previous levels",
123 : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true),
124 checkMerkleRoot(true) {}
161std::optional<std::vector<int>> CalculatePrevHeights(
const CBlockIndex &tip,
163 const CTransaction &tx) {
164 std::vector<int> prev_heights;
165 prev_heights.resize(tx.vin.size());
166 for (
size_t i = 0; i < tx.vin.size(); ++i) {
167 const CTxIn &txin = tx.vin[i];
169 if (!coins.
GetCoin(txin.prevout, coin)) {
170 LogPrintf(
"ERROR: %s: Missing input %d in transaction \'%s\'\n",
171 __func__, i, tx.GetId().GetHex());
176 prev_heights[i] = tip.
nHeight + 1;
187 const CTransaction &tx) {
190 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
191 if (!prev_heights.has_value()) {
196 next_tip.
pprev = tip;
229 int64_t nMedianTimePast) {
230 return nMedianTimePast >=
gArgs.
GetIntArg(
"-replayprotectionactivationtime",
236 if (pindexPrev ==
nullptr) {
257 for (
const CTxIn &txin : tx.vin) {
258 const Coin &coin = view.AccessCoin(txin.prevout);
273 assert(txFrom->GetId() == txin.prevout.GetTxId());
274 assert(txFrom->vout.size() > txin.prevout.GetN());
277 const Coin &coinFromUTXOSet = coins_tip.AccessCoin(txin.prevout);
286 true, txdata, nSigChecksOut);
294 : m_pool(mempool), m_view(&m_dummy),
295 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
296 m_active_chainstate(active_chainstate) {}
302 const int64_t m_accept_time;
303 const bool m_bypass_limits;
311 std::vector<COutPoint> &m_coins_to_uncache;
312 const bool m_test_accept;
313 const unsigned int m_heightOverride;
319 const bool m_package_submission;
325 const bool m_package_feerates;
328 static ATMPArgs SingleAccept(
const Config &config, int64_t accept_time,
330 std::vector<COutPoint> &coins_to_uncache,
332 unsigned int heightOverride) {
350 PackageTestAccept(
const Config &config, int64_t accept_time,
351 std::vector<COutPoint> &coins_to_uncache) {
367 PackageChildWithParents(
const Config &config, int64_t accept_time,
368 std::vector<COutPoint> &coins_to_uncache) {
382 static ATMPArgs SingleInPackageAccept(
const ATMPArgs &package_args) {
384 package_args.m_config,
385 package_args.m_accept_time,
387 package_args.m_coins_to_uncache,
388 package_args.m_test_accept,
389 package_args.m_heightOverride,
401 ATMPArgs(
const Config &config, int64_t accept_time,
bool bypass_limits,
402 std::vector<COutPoint> &coins_to_uncache,
bool test_accept,
403 unsigned int height_override,
bool package_submission,
404 bool package_feerates)
405 : m_config{config}, m_accept_time{accept_time},
406 m_bypass_limits{bypass_limits},
407 m_coins_to_uncache{coins_to_uncache}, m_test_accept{test_accept},
408 m_heightOverride{height_override},
409 m_package_submission{package_submission},
410 m_package_feerates(package_feerates) {}
425 AcceptMultipleTransactions(
const std::vector<CTransactionRef> &txns,
442 AcceptSubPackage(
const std::vector<CTransactionRef> &subpackage,
460 const uint32_t next_block_script_verify_flags)
462 m_next_block_script_verify_flags(next_block_script_verify_flags) {
469 std::unique_ptr<CTxMemPoolEntry> m_entry;
507 const uint32_t m_next_block_script_verify_flags;
508 int m_sig_checks_standard;
516 bool PreChecks(ATMPArgs &args, Workspace &ws)
523 bool ConsensusScriptChecks(
const ATMPArgs &args, Workspace &ws)
529 bool Finalize(
const ATMPArgs &args, Workspace &ws)
537 bool SubmitPackage(
const ATMPArgs &args, std::vector<Workspace> &workspaces,
539 std::map<TxId, MempoolAcceptResult> &results)
543 bool CheckFeeRate(
size_t package_size,
size_t package_vsize,
549 const Amount mempoolRejectFee =
550 m_pool.GetMinFee().GetFee(package_vsize);
553 package_fee < mempoolRejectFee) {
554 return state.Invalid(
556 "mempool min fee not met",
557 strprintf(
"%d < %d", package_fee, mempoolRejectFee));
562 if (package_fee < m_pool.m_min_relay_feerate.GetFee(package_size)) {
563 return state.Invalid(
565 "min relay fee not met",
567 m_pool.m_min_relay_feerate.GetFee(package_size)));
582bool MemPoolAccept::PreChecks(ATMPArgs &args, Workspace &ws) {
586 const CTransaction &tx = *ws.m_ptx;
587 const TxId &txid = ws.m_ptx->GetId();
590 const int64_t nAcceptTime = args.m_accept_time;
591 const bool bypass_limits = args.m_bypass_limits;
592 std::vector<COutPoint> &coins_to_uncache = args.m_coins_to_uncache;
593 const unsigned int heightOverride = args.m_heightOverride;
605 if (m_pool.m_require_standard &&
607 m_pool.m_permit_bare_multisig,
608 m_pool.m_dust_relay_feerate, reason)) {
617 *
Assert(m_active_chainstate.m_chain.Tip()),
618 args.m_config.GetChainParams().GetConsensus(), tx, ctxState)) {
627 if (m_pool.exists(txid)) {
629 "txn-already-in-mempool");
633 for (
const CTxIn &txin : tx.vin) {
634 if (
const auto ptxConflicting = m_pool.GetConflictTx(txin.prevout)) {
635 if (m_pool.isAvalancheFinalized(ptxConflicting->GetId())) {
637 "finalized-tx-conflict");
642 "txn-mempool-conflict");
646 m_view.SetBackend(m_viewmempool);
650 for (
const CTxIn &txin : tx.vin) {
652 coins_to_uncache.push_back(txin.prevout);
659 if (!m_view.HaveCoin(txin.prevout)) {
661 for (
size_t out = 0; out < tx.vout.size(); out++) {
666 "txn-already-known");
673 "bad-txns-inputs-missingorspent");
678 if (!m_view.HaveInputs(tx)) {
680 "bad-txns-inputs-spent");
684 m_view.GetBestBlock();
689 m_view.SetBackend(m_dummy);
691 assert(m_active_chainstate.m_blockman.LookupBlockIndex(
692 m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
701 m_active_chainstate.m_chain.Tip(), m_view, tx)};
702 if (!lock_points.has_value() ||
712 m_active_chainstate.m_chain.Height() + 1,
719 if (m_pool.m_require_standard &&
722 "bad-txns-nonstandard-inputs");
726 ws.m_modified_fees = ws.m_base_fees;
727 m_pool.ApplyDelta(txid, ws.m_modified_fees);
729 unsigned int nSize = tx.GetTotalSize();
732 const uint32_t scriptVerifyFlags =
736 ws.m_precomputed_txdata, ws.m_sig_checks_standard)) {
741 ws.m_entry = std::make_unique<CTxMemPoolEntry>(
742 ptx, ws.m_base_fees, nAcceptTime,
743 heightOverride ? heightOverride : m_active_chainstate.m_chain.Height(),
744 ws.m_sig_checks_standard, lock_points.value());
746 ws.m_vsize = ws.m_entry->GetTxVirtualSize();
754 if (!bypass_limits &&
756 m_pool.m_min_relay_feerate.GetFee(ws.m_ptx->GetTotalSize())) {
763 m_pool.m_min_relay_feerate.GetFee(nSize)));
768 if (!bypass_limits && !args.m_package_feerates &&
769 !CheckFeeRate(nSize, ws.m_vsize, ws.m_modified_fees, state)) {
776bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs &args, Workspace &ws) {
779 const CTransaction &tx = *ws.m_ptx;
780 const TxId &txid = tx.GetId();
794 int nSigChecksConsensus;
796 tx, state, m_view, m_pool, ws.m_next_block_script_verify_flags,
797 ws.m_precomputed_txdata, nSigChecksConsensus,
798 m_active_chainstate.CoinsTip())) {
802 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against "
803 "latest-block but not STANDARD flags %s, %s\n",
808 if (ws.m_sig_checks_standard != nSigChecksConsensus) {
813 "%s: BUG! PLEASE REPORT THIS! SigChecks count differed between "
814 "standard and consensus flags in %s",
823 std::vector<Coin> spent_coins;
824 spent_coins.reserve(ptx->vin.size());
825 for (
const CTxIn &input : ptx->vin) {
827 const bool coinFound = coins_view.
GetCoin(input.prevout, coin);
829 spent_coins.push_back(std::move(coin));
834bool MemPoolAccept::Finalize(
const ATMPArgs &args, Workspace &ws) {
837 const TxId &txid = ws.m_ptx->GetId();
839 const bool bypass_limits = args.m_bypass_limits;
844 m_pool.addUnchecked(entry);
848 std::make_shared<
const std::vector<Coin>>(
849 getSpentCoins(ws.m_ptx, m_view)),
850 m_pool.GetAndIncrementSequence());
857 if (!args.m_package_submission && !bypass_limits) {
858 m_pool.LimitSize(m_active_chainstate.CoinsTip());
859 if (!m_pool.exists(txid)) {
869bool MemPoolAccept::SubmitPackage(
870 const ATMPArgs &args, std::vector<Workspace> &workspaces,
872 std::map<TxId, MempoolAcceptResult> &results) {
877 workspaces.cbegin(), workspaces.cend(),
878 [
this](
const auto &ws) { return !m_pool.exists(ws.m_ptx->GetId()); }));
880 bool all_submitted =
true;
887 for (Workspace &ws : workspaces) {
888 if (!ConsensusScriptChecks(args, ws)) {
889 results.emplace(ws.m_ptx->GetId(),
892 all_submitted =
false;
895 strprintf(
"BUG! PolicyScriptChecks succeeded but "
896 "ConsensusScriptChecks failed: %s",
897 ws.m_ptx->GetId().ToString()));
907 if (!Finalize(args, ws)) {
908 results.emplace(ws.m_ptx->GetId(),
911 all_submitted =
false;
913 strprintf(
"BUG! Adding to mempool failed: %s",
914 ws.m_ptx->GetId().ToString()));
920 m_pool.LimitSize(m_active_chainstate.CoinsTip());
922 std::vector<TxId> all_package_txids;
923 all_package_txids.reserve(workspaces.size());
924 std::transform(workspaces.cbegin(), workspaces.cend(),
925 std::back_inserter(all_package_txids),
926 [](
const auto &ws) { return ws.m_ptx->GetId(); });
930 for (Workspace &ws : workspaces) {
931 const auto effective_feerate =
932 args.m_package_feerates
933 ? ws.m_package_feerate
935 static_cast<uint32_t
>(ws.m_vsize)};
936 const auto effective_feerate_txids =
937 args.m_package_feerates ? all_package_txids
938 : std::vector<TxId>({ws.m_ptx->GetId()});
939 results.emplace(ws.m_ptx->GetId(),
942 effective_feerate_txids));
944 return all_submitted;
955 const CBlockIndex *tip = m_active_chainstate.m_chain.Tip();
960 const std::vector<TxId> single_txid{ws.m_ptx->GetId()};
965 if (!PreChecks(args, ws)) {
966 if (ws.m_state.GetResult() ==
971 ws.m_state,
CFeeRate(ws.m_modified_fees, ws.m_vsize),
977 if (!ConsensusScriptChecks(args, ws)) {
981 const TxId txid = ptx->GetId();
991 if (
auto it = m_pool.mapNextTx.lower_bound(COutPoint{txid, 0});
992 it != m_pool.mapNextTx.end() && it->first->GetTxId() == txid) {
993 LogPrintf(
"%s: BUG! PLEASE REPORT THIS! Attempt to add txid %s, but "
994 "its outputs are already spent in the "
998 "txn-child-before-parent");
1002 const CFeeRate effective_feerate{ws.m_modified_fees,
1003 static_cast<uint32_t
>(ws.m_vsize)};
1005 if (args.m_test_accept) {
1007 effective_feerate, single_txid);
1010 if (!Finalize(args, ws)) {
1014 Assume(ws.m_state.GetResult() ==
1017 ws.m_state,
CFeeRate(ws.m_modified_fees, ws.m_vsize), single_txid);
1021 effective_feerate, single_txid);
1025 const std::vector<CTransactionRef> &txns, ATMPArgs &args) {
1035 std::vector<Workspace> workspaces{};
1036 workspaces.reserve(txns.size());
1038 txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1039 [
this](
const auto &tx) {
1041 tx, GetNextBlockScriptFlags(m_active_chainstate.m_chain.Tip(),
1042 m_active_chainstate.m_chainman));
1044 std::map<TxId, MempoolAcceptResult> results;
1050 std::vector<TxId> valid_txids;
1051 for (Workspace &ws : workspaces) {
1052 if (!PreChecks(args, ws)) {
1054 "transaction failed");
1057 results.emplace(ws.m_ptx->GetId(),
1060 std::move(results));
1064 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1065 valid_txids.push_back(ws.m_ptx->GetId());
1080 const auto m_total_size = std::accumulate(
1081 workspaces.cbegin(), workspaces.cend(), int64_t{0},
1082 [](int64_t
sum,
auto &ws) { return sum + ws.m_ptx->GetTotalSize(); });
1083 const auto m_total_vsize =
1084 std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1085 [](int64_t
sum,
auto &ws) { return sum + ws.m_vsize; });
1086 const auto m_total_modified_fees = std::accumulate(
1087 workspaces.cbegin(), workspaces.cend(),
Amount::zero(),
1088 [](
Amount sum,
auto &ws) { return sum + ws.m_modified_fees; });
1089 const CFeeRate package_feerate(m_total_modified_fees, m_total_vsize);
1090 std::vector<TxId> all_package_txids;
1091 all_package_txids.reserve(workspaces.size());
1092 std::transform(workspaces.cbegin(), workspaces.cend(),
1093 std::back_inserter(all_package_txids),
1094 [](
const auto &ws) { return ws.m_ptx->GetId(); });
1096 if (args.m_package_feerates &&
1097 !CheckFeeRate(m_total_size, m_total_vsize, m_total_modified_fees,
1098 placeholder_state)) {
1100 "transaction failed");
1102 package_state, {{workspaces.back().m_ptx->GetId(),
1105 CFeeRate(m_total_modified_fees, m_total_vsize),
1106 all_package_txids)}});
1109 for (Workspace &ws : workspaces) {
1110 ws.m_package_feerate = package_feerate;
1111 const TxId &ws_txid = ws.m_ptx->GetId();
1112 if (args.m_test_accept &&
1113 std::find(valid_txids.begin(), valid_txids.end(), ws_txid) !=
1114 valid_txids.end()) {
1115 const auto effective_feerate =
1116 args.m_package_feerates
1117 ? ws.m_package_feerate
1119 static_cast<uint32_t
>(ws.m_vsize)};
1120 const auto effective_feerate_txids =
1121 args.m_package_feerates ? all_package_txids
1122 : std::vector<TxId>{ws.m_ptx->GetId()};
1126 results.emplace(ws_txid,
1128 ws.m_vsize, ws.m_base_fees, effective_feerate,
1129 effective_feerate_txids));
1133 if (args.m_test_accept) {
1137 if (!SubmitPackage(args, workspaces, package_state, results)) {
1146MemPoolAccept::AcceptSubPackage(
const std::vector<CTransactionRef> &subpackage,
1152 if (subpackage.size() > 1) {
1153 return AcceptMultipleTransactions(subpackage, args);
1155 const auto &tx = subpackage.front();
1156 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1157 const auto single_res = AcceptSingleTransaction(tx, single_args);
1159 if (single_res.m_result_type !=
1162 "transaction failed");
1165 {{tx->GetId(), single_res}});
1195 for (
const auto &outpoint : m_viewmempool.GetNonBaseCoins()) {
1199 m_view.Uncache(outpoint);
1202 m_viewmempool.Reset();
1218 if (!
CheckPackage(package, package_state_quit_early)) {
1227 "package-not-child-with-parents");
1232 assert(package.size() > 1);
1235 const auto &child = package.back();
1236 std::unordered_set<TxId, SaltedTxIdHasher> unconfirmed_parent_txids;
1238 package.cbegin(), package.cend() - 1,
1239 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1240 [](
const auto &tx) { return tx->GetId(); });
1249 const CCoinsViewCache &coins_tip_cache = m_active_chainstate.CoinsTip();
1250 for (
const auto &input : child->vin) {
1252 args.m_coins_to_uncache.push_back(input.prevout);
1259 m_view.
SetBackend(m_active_chainstate.CoinsTip());
1260 const auto package_or_confirmed = [
this, &unconfirmed_parent_txids](
1261 const auto &input) {
1262 return unconfirmed_parent_txids.count(input.prevout.GetTxId()) > 0 ||
1263 m_view.HaveCoin(input.prevout);
1265 if (!std::all_of(child->vin.cbegin(), child->vin.cend(),
1266 package_or_confirmed)) {
1267 package_state_quit_early.
Invalid(
1269 "package-not-child-with-unconfirmed-parents");
1275 m_view.SetBackend(m_dummy);
1281 std::map<TxId, MempoolAcceptResult> results_final;
1286 std::map<TxId, MempoolAcceptResult> individual_results_nonfinal;
1287 bool quit_early{
false};
1288 std::vector<CTransactionRef> txns_package_eval;
1289 for (
const auto &tx : package) {
1290 const auto &txid = tx->GetId();
1293 if (m_pool.exists(txid)) {
1307 auto iter = m_pool.GetIter(txid);
1308 assert(iter != std::nullopt);
1310 (*iter.value())->GetTxSize(),
1311 (*iter.value())->GetFee()));
1315 const auto single_package_res = AcceptSubPackage({tx}, args);
1316 const auto &single_res = single_package_res.m_tx_results.at(txid);
1317 if (single_res.m_result_type ==
1322 assert(m_pool.exists(txid));
1323 results_final.emplace(txid, single_res);
1324 }
else if (single_res.m_state.GetResult() !=
1326 single_res.m_state.GetResult() !=
1341 package_state_quit_early.
Invalid(
1343 individual_results_nonfinal.emplace(txid, single_res);
1345 individual_results_nonfinal.emplace(txid, single_res);
1346 txns_package_eval.push_back(tx);
1351 auto multi_submission_result =
1352 quit_early || txns_package_eval.empty()
1354 : AcceptSubPackage(txns_package_eval, args);
1356 multi_submission_result.m_state;
1361 m_pool.LimitSize(m_active_chainstate.CoinsTip());
1363 for (
const auto &tx : package) {
1364 const auto &txid = tx->GetId();
1365 if (multi_submission_result.m_tx_results.count(txid) > 0) {
1368 Assume(results_final.count(txid) == 0);
1372 const auto &txresult =
1373 multi_submission_result.m_tx_results.at(txid);
1374 if (txresult.m_result_type ==
1376 !m_pool.exists(txid)) {
1378 "transaction failed");
1382 results_final.emplace(
1385 results_final.emplace(txid, txresult);
1387 }
else if (
const auto final_it{results_final.find(txid)};
1388 final_it != results_final.end()) {
1391 Assume(final_it->second.m_result_type !=
1393 Assume(individual_results_nonfinal.count(txid) == 0);
1394 if (!m_pool.exists(tx->GetId())) {
1396 "transaction failed");
1401 results_final.erase(txid);
1402 results_final.emplace(
1405 }
else if (
const auto non_final_it{
1406 individual_results_nonfinal.find(txid)};
1407 non_final_it != individual_results_nonfinal.end()) {
1408 Assume(non_final_it->second.m_result_type ==
1411 results_final.emplace(txid, non_final_it->second);
1414 Assume(results_final.size() == package.size());
1416 std::move(results_final));
1422 int64_t accept_time,
bool bypass_limits,
1424 unsigned int heightOverride) {
1429 std::vector<COutPoint> coins_to_uncache;
1430 auto args = MemPoolAccept::ATMPArgs::SingleAccept(
1432 coins_to_uncache, test_accept, heightOverride);
1434 .AcceptSingleTransaction(tx, args);
1442 for (
const COutPoint &outpoint : coins_to_uncache) {
1459 assert(!package.empty());
1460 assert(std::all_of(package.cbegin(), package.cend(),
1461 [](
const auto &tx) { return tx != nullptr; }));
1465 std::vector<COutPoint> coins_to_uncache;
1469 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(
1470 config,
GetTime(), coins_to_uncache);
1471 return MemPoolAccept(pool, active_chainstate)
1472 .AcceptMultipleTransactions(package, args);
1474 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(
1475 config,
GetTime(), coins_to_uncache);
1476 return MemPoolAccept(pool, active_chainstate)
1477 .AcceptPackage(package, args);
1483 if (test_accept || result.m_state.IsInvalid()) {
1484 for (
const COutPoint &hashTx : coins_to_uncache) {
1497 if (halvings >= 64) {
1508 : m_dbview{
std::move(db_params),
std::move(options)},
1509 m_catcherview(&m_dbview) {}
1511void CoinsViews::InitCache() {
1513 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1518 std::optional<BlockHash> from_snapshot_blockhash)
1519 : m_mempool(mempool), m_blockman(blockman), m_chainman(chainman),
1520 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1526 if (!m_cached_snapshot_base) {
1527 m_cached_snapshot_base =
Assert(
1530 return m_cached_snapshot_base;
1534 bool should_wipe, std::string leveldb_name) {
1541 .cache_bytes = cache_size_bytes,
1542 .memory_only = in_memory,
1543 .wipe_data = should_wipe,
1549void Chainstate::InitCoinsCache(
size_t cache_size_bytes) {
1575 if (chain.Tip() ==
nullptr) {
1584 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
1611 std::string warning =
1612 std::string(
"'Warning: Large-work fork detected, forking after "
1619 LogPrintf(
"%s: Warning: Large fork found\n forking the "
1620 "chain at height %d (%s)\n lasting to height %d "
1621 "(%s).\nChain state database corruption likely.\n",
1628 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks "
1629 "longer than our best chain.\nChain state database "
1630 "corruption likely.\n",
1675 SetBlockFailureFlags(pindexNew);
1686 m_avalancheFinalizedBlockIndex = pindexNew->
pprev;
1689 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n",
1696 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n",
1708 pindex->nStatus = pindex->nStatus.withFailed();
1718 if (tx.IsCoinBase()) {
1722 txundo.
vprevout.reserve(tx.vin.size());
1723 for (
const CTxIn &txin : tx.vin) {
1737 const CScript &scriptSig =
ptxTo->vin[
nIn].scriptSig;
1759 bool sigCacheStore,
bool scriptCacheStore,
1763 std::vector<CScriptCheck> *pvChecks) {
1765 assert(!tx.IsCoinBase());
1768 pvChecks->reserve(tx.vin.size());
1778 (pBlockLimitSigChecks &&
1781 "too-many-sigchecks");
1786 int nSigChecksTotal = 0;
1788 for (
size_t i = 0; i < tx.vin.size(); i++) {
1789 const COutPoint &prevout = tx.vin[i].prevout;
1801 &txLimitSigChecks, pBlockLimitSigChecks);
1805 pvChecks->push_back(std::move(check));
1815 uint32_t mandatoryFlags =
1816 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS;
1817 if (
flags != mandatoryFlags) {
1824 sigCacheStore, txdata);
1828 strprintf(
"non-mandatory-script-verify-flag (%s)",
1844 strprintf(
"mandatory-script-verify-flag-failed (%s)",
1851 nSigChecksOut = nSigChecksTotal;
1853 if (scriptCacheStore && !pvChecks) {
1865 return state.
Error(strMessage);
1870 const COutPoint &out) {
1878 if (undo.GetHeight() == 0) {
1901 view.
AddCoin(out, std::move(undo), !fClean);
1916 error(
"DisconnectBlock(): failure reading undo data");
1920 return ApplyBlockUndo(std::move(blockUndo), block, pindex, view);
1928 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
1929 error(
"DisconnectBlock(): block and undo data inconsistent");
1934 for (
size_t i = 1; i < block.
vtx.size(); i++) {
1935 const CTransaction &tx = *(block.
vtx[i]);
1937 if (txundo.
vprevout.size() != tx.vin.size()) {
1938 error(
"DisconnectBlock(): transaction and undo data inconsistent");
1942 for (
size_t j = 0; j < tx.vin.size(); j++) {
1943 const COutPoint &out = tx.vin[j].prevout;
1955 for (
const auto &ptx : block.
vtx) {
1956 const CTransaction &tx = *ptx;
1957 const TxId &txid = tx.GetId();
1958 const bool is_coinbase = tx.IsCoinBase();
1962 for (
size_t o = 0; o < tx.vout.size(); o++) {
1963 if (tx.vout[o].scriptPubKey.IsUnspendable()) {
1967 COutPoint out(txid, o);
1969 bool is_spent = view.
SpendCoin(out, &coin);
1970 if (!is_spent || tx.vout[o] != coin.
GetTxOut() ||
2106 if (!
CheckBlock(block, state, consensusParams,
2113 return AbortNode(state,
"Corrupt block found indicating potential "
2114 "hardware failure; shutting down");
2116 return error(
"%s: Consensus::CheckBlock: %s", __func__,
2137 bool fScriptChecks =
true;
2146 BlockMap::const_iterator it{
2149 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2177 consensusParams) <= 60 * 60 * 24 * 7 * 2);
2199 bool fEnforceBIP30 = !((pindex->
nHeight == 91842 &&
2201 uint256S(
"0x00000000000a4d0a398161ffc163c503763"
2202 "b1f4360639393e0e4c8e300e0caec")) ||
2205 uint256S(
"0x00000000000743f190a18c5577a3c2d2a1f"
2206 "610ae9601ac046a38084ccb7cd721")));
2237 static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2274 (!pindexBIP34height ||
2280 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2281 for (
const auto &tx : block.
vtx) {
2282 for (
size_t o = 0; o < tx->vout.size(); o++) {
2283 if (view.
HaveCoin(COutPoint(tx->GetId(), o))) {
2284 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite "
2294 int nLockTimeFlags = 0;
2308 std::vector<int> prevheights;
2320 std::vector<TxSigCheckLimiter> nSigChecksTxLimiters;
2321 nSigChecksTxLimiters.resize(block.
vtx.size() - 1);
2324 blockundo.
vtxundo.resize(block.
vtx.size() - 1);
2331 for (
const auto &ptx : block.
vtx) {
2334 }
catch (
const std::logic_error &e) {
2343 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite transaction\n");
2352 for (
const auto &ptx : block.
vtx) {
2353 const CTransaction &tx = *ptx;
2354 const bool isCoinBase = tx.IsCoinBase();
2355 nInputs += tx.vin.size();
2369 return error(
"%s: Consensus::CheckTxInputs: %s, %s", __func__,
2370 tx.GetId().ToString(), state.
ToString());
2376 LogPrintf(
"ERROR: %s: accumulated fee in the block out of range.\n",
2379 "bad-txns-accumulated-fee-outofrange");
2390 prevheights.resize(tx.vin.size());
2391 for (
size_t j = 0; j < tx.vin.size(); j++) {
2395 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2396 LogPrintf(
"ERROR: %s: contains a non-BIP68-final transaction\n",
2399 "bad-txns-nonfinal");
2404 bool fCacheResults = fJustCheck;
2407 if (!fEnforceSigCheck) {
2414 std::vector<CScriptCheck> vChecks;
2416 if (fScriptChecks &&
2419 nSigChecksRet, nSigChecksTxLimiters[txIndex],
2420 &nSigChecksBlockLimiter, &vChecks)) {
2427 "ConnectBlock(): CheckInputScripts on %s failed with %s",
2428 tx.GetId().ToString(), state.
ToString());
2431 control.
Add(std::move(vChecks));
2445 " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) "
2446 "[%.2fs (%.2fms/blk)]\n",
2447 (
unsigned)block.
vtx.size(),
MILLI * (nTime3 - nTime2),
2448 MILLI * (nTime3 - nTime2) / block.
vtx.size(),
2449 nInputs <= 1 ? 0 :
MILLI * (nTime3 - nTime2) / (nInputs - 1),
2452 const Amount blockReward =
2454 if (block.
vtx[0]->GetValueOut() > blockReward) {
2455 LogPrintf(
"ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs "
2457 block.
vtx[0]->GetValueOut(), blockReward);
2466 if (!control.
Wait()) {
2468 "blk-bad-inputs",
"parallel script check failed");
2475 " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n",
2476 nInputs - 1,
MILLI * (nTime4 - nTime2),
2477 nInputs <= 1 ? 0 :
MILLI * (nTime4 - nTime2) / (nInputs - 1),
2484 if (!
m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2502 TRACE6(validation, block_connected, block_hash.data(), pindex->
nHeight,
2503 block.
vtx.size(), nInputs, nSigChecksRet,
2505 nTime5 - nTimeStart);
2518Chainstate::GetCoinsCacheSizeState(
size_t max_coins_cache_size_bytes,
2519 size_t max_mempool_size_bytes) {
2523 int64_t nTotalSpace =
2524 max_coins_cache_size_bytes +
2525 std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2528 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES =
2530 int64_t large_threshold = std::max(
2531 (9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2533 if (cacheSize > nTotalSpace) {
2534 LogPrintf(
"Cache size (%s) exceeds total space (%s)\n", cacheSize,
2537 }
else if (cacheSize > large_threshold) {
2547 std::set<int> setFilesToPrune;
2548 bool full_flush_completed =
false;
2555 bool fFlushForPrune =
false;
2556 bool fDoFullFlush =
false;
2568 std::optional<std::string> limiting_lock;
2570 for (
const auto &prune_lock :
m_blockman.m_prune_locks) {
2571 if (prune_lock.second.height_first ==
2572 std::numeric_limits<int>::max()) {
2577 const int lock_height{prune_lock.second.height_first -
2579 last_prune = std::max(1, std::min(last_prune, lock_height));
2580 if (last_prune == lock_height) {
2581 limiting_lock = prune_lock.first;
2585 if (limiting_lock) {
2587 limiting_lock.value(), last_prune);
2590 if (nManualPruneHeight > 0) {
2595 std::min(last_prune, nManualPruneHeight), *
this,
2604 if (!setFilesToPrune.empty()) {
2605 fFlushForPrune =
true;
2608 "prunedblockfiles",
true);
2613 const auto nNow = GetTime<std::chrono::microseconds>();
2638 fCacheCritical || fPeriodicFlush || fFlushForPrune;
2640 if (fDoFullFlush || fPeriodicWrite) {
2643 return AbortNode(state,
"Disk space is too low!",
2644 _(
"Disk space is too low!"));
2658 "%s: Failed to flush block file.\n",
2670 state,
"Failed to write to block index database");
2675 if (fFlushForPrune) {
2685 if (fDoFullFlush && !
CoinsTip().GetBestBlock().IsNull()) {
2687 strprintf(
"write coins cache to disk (%d coins, %.2fkB)",
2688 coins_count, coins_mem_usage / 1000),
2698 48 * 2 * 2 *
CoinsTip().GetCacheSize())) {
2699 return AbortNode(state,
"Disk space is too low!",
2700 _(
"Disk space is too low!"));
2706 return AbortNode(state,
"Failed to write to coin database");
2709 full_flush_completed =
true;
2714 GetTimeMicros() - nNow.count(), uint32_t(mode), coins_count,
2715 uint64_t(coins_mem_usage), fFlushForPrune);
2718 if (full_flush_completed) {
2723 }
catch (
const std::runtime_error &e) {
2724 return AbortNode(state, std::string(
"System error while flushing: ") +
2733 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
2742 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
2749 const std::string &func_name,
2750 const std::string &
prefix)
2753 LogPrintf(
"%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
2754 "date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
2760 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2761 coins_tip.GetCacheSize());
2764void Chainstate::UpdateTip(
const CBlockIndex *pindexNew) {
2766 const auto &coins_tip =
CoinsTip();
2775 constexpr int BACKGROUND_LOG_INTERVAL = 2000;
2776 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2778 "[background validation] ");
2794 UpdateTipLog(coins_tip, pindexNew, params, __func__,
"");
2821 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2824 return error(
"DisconnectTip(): Failed to read block");
2832 if (DisconnectBlock(block, pindexDelete, view) !=
2834 return error(
"DisconnectTip(): DisconnectBlock %s failed",
2838 bool flushed = view.
Flush();
2848 const int max_height_first{pindexDelete->
nHeight - 1};
2849 for (
auto &prune_lock :
m_blockman.m_prune_locks) {
2850 if (prune_lock.second.height_first <= max_height_first) {
2854 prune_lock.second.height_first = max_height_first;
2856 prune_lock.first, max_height_first);
2869 if (pindexDelete->
pprev !=
nullptr &&
2873 "Disconnecting mempool due to rewind of upgrade block\n");
2874 if (disconnectpool) {
2880 if (disconnectpool) {
2887 UpdateTip(pindexDelete->
pprev);
2907 const std::shared_ptr<const CBlock> &pblock,
2921 std::shared_ptr<const CBlock> pthisBlock;
2923 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2925 return AbortNode(state,
"Failed to read block");
2927 pthisBlock = pblockNew;
2929 pthisBlock = pblock;
2932 const CBlock &blockConnecting = *pthisBlock;
2943 bool rv =
ConnectBlock(blockConnecting, state, pindexNew, view,
2952 return error(
"%s: ConnectBlock %s failed, %s", __func__,
2973 const Amount blockReward =
2977 std::vector<std::unique_ptr<ParkingPolicy>> parkingPolicies;
2978 parkingPolicies.emplace_back(std::make_unique<MinerFundPolicy>(
2979 consensusParams, *pindexNew, blockConnecting, blockReward));
2995 parkingPolicies.emplace_back(
2996 std::make_unique<RTTPolicy>(consensusParams,
3001 parkingPolicies.emplace_back(
3002 std::make_unique<StakingRewardsPolicy>(
3003 *
avalanche, consensusParams, *pindexNew,
3004 blockConnecting, blockReward));
3007 parkingPolicies.emplace_back(
3008 std::make_unique<PreConsensusPolicy>(
3009 *pindexNew, blockConnecting,
m_mempool));
3014 if (std::find_if_not(parkingPolicies.begin(), parkingPolicies.end(),
3015 [&](
const auto &policy) {
3016 bool ret = (*policy)(blockPolicyState);
3019 "Park block because it "
3020 "violated a block policy: %s\n",
3021 blockPolicyState.ToString());
3024 }) != parkingPolicies.end()) {
3025 pindexNew->nStatus = pindexNew->nStatus.withParked();
3035 " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3038 bool flushed = view.
Flush();
3056 " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3062 disconnectpool.removeForBlock(blockConnecting.vtx, *m_mempool);
3067 if (pindexNew->
pprev !=
nullptr &&
3072 "Disconnecting mempool due to acceptance of upgrade block\n");
3073 disconnectpool.importMempool(*m_mempool);
3078 m_chain.SetTip(*pindexNew);
3079 UpdateTip(pindexNew);
3085 " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3095 if (
this != &m_chainman.ActiveChainstate()) {
3099 m_chainman.MaybeCompleteSnapshotValidation();
3111 std::vector<const CBlockIndex *> &blocksToReconcile,
bool fAutoUnpark) {
3118 std::set<CBlockIndex *, CBlockIndexWorkComparator>::reverse_iterator
3130 if (m_avalancheFinalizedBlockIndex &&
3132 LogPrintf(
"Park block %s because it forks prior to the "
3133 "avalanche finalized chaintip.\n",
3135 pindexNew->nStatus = pindexNew->nStatus.withParked();
3146 bool hasValidAncestor =
true;
3147 while (hasValidAncestor && pindexTest && pindexTest != pindexFork) {
3152 bool fParkedChain = pindexTest->nStatus.isOnParkedChain();
3153 if (fAutoUnpark && fParkedChain) {
3159 if (!pindexTip || !pindexFork) {
3173 pindexExtraPow = pindexExtraPow->
pprev;
3178 requiredWork += (deltaWork >> 1);
3189 LogPrintf(
"Unpark chain up to block %s as it has "
3190 "accumulated enough PoW.\n",
3192 fParkedChain =
false;
3201 bool fInvalidChain = pindexTest->nStatus.isInvalid();
3202 bool fMissingData = !pindexTest->nStatus.hasData();
3203 if (!(fInvalidChain || fParkedChain || fMissingData)) {
3206 pindexTest = pindexTest->
pprev;
3212 hasValidAncestor =
false;
3215 if (fInvalidChain && (
m_chainman.m_best_invalid ==
nullptr ||
3221 if (fParkedChain && (
m_chainman.m_best_parked ==
nullptr ||
3227 LogPrintf(
"Considered switching to better tip %s but that chain "
3228 "contains a%s%s%s block.\n",
3230 fInvalidChain ?
"n invalid" :
"",
3231 fParkedChain ?
" parked" :
"",
3232 fMissingData ?
" missing-data" :
"");
3236 while (pindexTest != pindexFailed) {
3237 if (fInvalidChain || fParkedChain) {
3238 pindexFailed->nStatus =
3239 pindexFailed->nStatus.withFailedParent(fInvalidChain)
3240 .withParkedParent(fParkedChain);
3241 }
else if (fMissingData) {
3247 std::make_pair(pindexFailed->
pprev, pindexFailed));
3250 pindexFailed = pindexFailed->
pprev;
3253 if (fInvalidChain || fParkedChain) {
3260 blocksToReconcile.push_back(pindexNew);
3263 if (hasValidAncestor) {
3296 const std::shared_ptr<const CBlock> &pblock,
bool &fInvalidFound,
3308 bool fBlocksDisconnected =
false;
3311 if (
m_mempool && !fBlocksDisconnected) {
3330 "Failed to disconnect block; see debug.log for details");
3334 fBlocksDisconnected =
true;
3338 std::vector<CBlockIndex *> vpindexToConnect;
3339 bool fContinue =
true;
3344 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3345 vpindexToConnect.clear();
3346 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3349 vpindexToConnect.push_back(pindexIter);
3350 pindexIter = pindexIter->
pprev;
3358 if (!
ConnectTip(state, blockPolicyState, pindexConnect,
3359 pindexConnect == pindexMostWork
3361 : std::shared_ptr<const CBlock>(),
3362 disconnectpool,
avalanche, chainstate_role)) {
3370 fInvalidFound =
true;
3391 if (!pindexOldTip ||
3403 if (fBlocksDisconnected || !disconnectpool.
isEmpty()) {
3409 "Updating mempool due to reorganization or "
3410 "rules upgrade/downgrade\n");
3418 if (fInvalidFound) {
3439 bool fNotify =
false;
3440 bool fInitialBlockDownload =
false;
3445 pindexHeader = chainman.m_best_header;
3447 if (pindexHeader != pindexHeaderOld) {
3449 fInitialBlockDownload = chainman.IsInitialBlockDownload();
3450 pindexHeaderOld = pindexHeader;
3456 chainman.GetNotifications().headerTip(
3472 std::shared_ptr<const CBlock> pblock,
3493 LogPrintf(
"m_disabled is set - this chainstate should not be in "
3494 "operation. Please report this as a bug. %s\n",
3502 bool exited_ibd{
false};
3512 std::vector<const CBlockIndex *> blocksToReconcile;
3513 bool blocks_connected =
false;
3515 const bool fAutoUnpark =
3530 if (pindexMostWork ==
nullptr) {
3536 if (pindexMostWork ==
nullptr ||
3541 bool fInvalidFound =
false;
3542 std::shared_ptr<const CBlock> nullBlockPtr;
3549 state, pindexMostWork,
3550 pblock && pblock->GetHash() ==
3554 fInvalidFound,
avalanche, chainstate_role)) {
3558 blocks_connected =
true;
3560 if (fInvalidFound ||
3561 (pindexMostWork && pindexMostWork->nStatus.isParked())) {
3563 pindexMostWork =
nullptr;
3588 if (blocks_connected) {
3592 if (was_in_ibd && !still_in_ibd) {
3601 pindexFork != pindexNewTip) {
3617 return m_avalancheFinalizedBlockIndex);
3618 for (
const CBlockIndex *pindex : blocksToReconcile) {
3624 if (blocks_connected) {
3626 while (pindexTest && pindexTest != pfinalized) {
3631 avalanche->computeStakingReward(pindexTest);
3632 pindexTest = pindexTest->
pprev;
3638 if (!blocks_connected) {
3642 if (nStopAtHeight && pindexNewTip &&
3643 pindexNewTip->
nHeight >= nStopAtHeight) {
3680 }
while (pindexNewTip != pindexMostWork);
3711 std::numeric_limits<int32_t>::min()) {
3733template <
typename Func>
struct Defer {
3735 Defer(
Func &&f) : func(
std::move(f)) {}
3736 ~Defer() { func(); }
3749 bool pindex_was_in_chain =
false;
3750 int disconnected = 0;
3766 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3770 for (
auto &entry :
m_blockman.m_block_index) {
3781 candidate_blocks_by_work.insert(
3782 std::make_pair(candidate->
nChainWork, candidate));
3794 constexpr int maxDisconnectPoolBlocks = 10;
3846 pindex_was_in_chain =
true;
3855 if (optDisconnectPool && disconnected > maxDisconnectPoolBlocks) {
3860 optDisconnectPool =
nullptr;
3876 invalid_walk_tip->nStatus =
3877 invalidate ? invalid_walk_tip->nStatus.withFailed()
3878 : invalid_walk_tip->nStatus.withParked();
3883 if (invalid_walk_tip == to_mark_failed_or_parked->
pprev &&
3884 (invalidate ? to_mark_failed_or_parked->nStatus.hasFailed()
3885 : to_mark_failed_or_parked->nStatus.isParked())) {
3889 to_mark_failed_or_parked->nStatus =
3891 ? to_mark_failed_or_parked->nStatus.withFailed(
false)
3893 : to_mark_failed_or_parked->nStatus.withParked(
false)
3894 .withParkedParent());
3900 auto candidate_it = candidate_blocks_by_work.lower_bound(
3902 while (candidate_it != candidate_blocks_by_work.end()) {
3904 invalid_walk_tip->
pprev)) {
3906 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3915 to_mark_failed_or_parked = invalid_walk_tip;
3931 to_mark_failed_or_parked->nStatus =
3932 invalidate ? to_mark_failed_or_parked->nStatus.withFailed()
3933 : to_mark_failed_or_parked->nStatus.withParked();
3946 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
3948 block_index.HaveNumChainTxs() &&
3961 if (pindex_was_in_chain) {
3964 *to_mark_failed_or_parked->
pprev);
3988template <
typename F>
3992 if (pindex->nStatus != newStatus &&
3995 pindex->nStatus = newStatus;
4011template <
typename F,
typename C,
typename AC>
4013 F f, C fChild, AC fAncestorWasChanged) {
4019 for (
auto pindexAncestor = pindex; pindexAncestor !=
nullptr;
4020 pindexAncestor = pindexAncestor->
pprev) {
4022 pindexDeepestChanged = pindexAncestor;
4028 pindexDeepestChanged) {
4030 pindexReset =
nullptr;
4034 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
4037 fAncestorWasChanged);
4041void Chainstate::SetBlockFailureFlags(
CBlockIndex *invalid_block) {
4044 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
4045 if (block_index.GetAncestor(invalid_block->
nHeight) == invalid_block &&
4046 !block_index.nStatus.isInvalid()) {
4047 block_index.nStatus = block_index.nStatus.withFailedParent();
4058 return status.withClearedFailureFlags();
4061 return status.withClearedFailureFlags();
4064 return status.withFailedParent(false);
4078 if (is_active_chainstate) {
4082 }
else if (!m_disabled) {
4088 if (snapshot_base->GetAncestor(pindex->
nHeight) == pindex) {
4100 return status.withClearedParkedFlags();
4103 return fClearChildren ? status.withClearedParkedFlags()
4104 : status.withParkedParent(false);
4107 return status.withParkedParent(false);
4129 "The block to mark finalized by avalanche is not on the "
4130 "active chain: %s\n",
4143 m_avalancheFinalizedBlockIndex = pindex;
4153 m_avalancheFinalizedBlockIndex =
nullptr;
4158 return pindex && m_avalancheFinalizedBlockIndex &&
4170 pindexNew->
nTx = block.
vtx.size();
4177 return block.nTx + (block.pprev ? block.pprev->nChainTx : 0);
4180 pindexNew->
nChainTx == prev_tx_sum(*pindexNew) ||
4181 pindexNew == GetSnapshotBaseBlock())) {
4182 LogPrintf(
"Internal bug detected: block %d has unexpected nChainTx %i "
4183 "that should be %i. Please report this issue here: %s\n",
4185 prev_tx_sum(*pindexNew), PACKAGE_BUGREPORT);
4189 pindexNew->nFile = pos.
nFile;
4190 pindexNew->nDataPos = pos.
nPos;
4191 pindexNew->nUndoPos = 0;
4192 pindexNew->nStatus = pindexNew->nStatus.withData();
4199 std::deque<CBlockIndex *> queue;
4200 queue.push_back(pindexNew);
4204 while (!queue.empty()) {
4212 pindex->
nChainTx == prev_tx_sum(*pindex))) {
4214 "Internal bug detected: block %d has unexpected nChainTx "
4215 "%i that should be %i. Please report this issue here: %s\n",
4219 pindex->
nChainTx = prev_tx_sum(*pindex);
4229 c->TryAddBlockIndexCandidate(pindex);
4232 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4233 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4235 while (range.first != range.second) {
4236 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4238 queue.push_back(it->second);
4243 }
else if (pindexNew->
pprev &&
4246 std::make_pair(pindexNew->
pprev, pindexNew));
4266 "high-hash",
"proof of work failed");
4283 "hashMerkleRoot mismatch");
4292 "bad-txns-duplicate",
4293 "duplicate transaction");
4325 if (block.
vtx.empty()) {
4327 "bad-cb-missing",
"first tx is not coinbase");
4336 "bad-blk-length",
"size limits failed");
4340 if (currentBlockSize > nMaxBlockSize) {
4342 "bad-blk-length",
"size limits failed");
4350 strprintf(
"Coinbase check failed (txid %s) %s",
4351 block.
vtx[0]->GetId().ToString(),
4357 for (
size_t i = 1; i < block.
vtx.size(); i++) {
4358 auto *tx = block.
vtx[i].get();
4363 strprintf(
"Transaction check failed (txid %s) %s",
4378 return std::all_of(headers.cbegin(), headers.cend(),
4379 [&](
const auto &header) {
4380 return CheckProofOfWork(
4381 header.GetHash(), header.nBits, consensusParams);
4389 "Block mutated: %s\n", state.
ToString());
4393 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase()) {
4401 return std::any_of(block.
vtx.begin(), block.
vtx.end(), [](
auto &tx) {
4402 return GetSerializeSize(tx, PROTOCOL_VERSION) == 64;
4436 const std::optional<CCheckpointData> &test_checkpoints = std::nullopt)
4439 assert(pindexPrev !=
nullptr);
4440 const int nHeight = pindexPrev->nHeight + 1;
4446 LogPrintf(
"bad bits after height: %d\n", pindexPrev->nHeight);
4448 "bad-diffbits",
"incorrect proof of work");
4452 if (chainman.m_options.checkpoints_enabled) {
4454 test_checkpoints ? test_checkpoints.value() : params.
Checkpoints();
4460 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4463 "checkpoint mismatch");
4471 blockman.GetLastCheckpoint(checkpoints);
4472 if (pcheckpoint && nHeight < pcheckpoint->
nHeight) {
4474 "ERROR: %s: forked chain older than last checkpoint "
4478 "bad-fork-prior-to-checkpoint");
4483 if (block.
GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
4485 "time-too-old",
"block's timestamp is too early");
4489 if (block.
Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4492 "block timestamp too far in the future");
4526 const int nBlockHeight = active_chain_tip.
nHeight + 1;
4551 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
4554 bool enforce_locktime_median_time_past{
false};
4557 assert(pindexPrev !=
nullptr);
4558 enforce_locktime_median_time_past =
true;
4561 const int64_t nMedianTimePast =
4564 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past
4569 const bool fIsMagneticAnomalyEnabled =
4576 const CTransaction *prevTx =
nullptr;
4577 for (
const auto &ptx : block.
vtx) {
4578 const CTransaction &tx = *ptx;
4579 if (fIsMagneticAnomalyEnabled) {
4580 if (prevTx && (tx.GetId() <= prevTx->GetId())) {
4581 if (tx.GetId() == prevTx->GetId()) {
4585 tx.GetId().ToString()));
4590 strprintf(
"Transaction order is invalid (%s < %s)",
4591 tx.GetId().ToString(),
4592 prevTx->GetId().ToString()));
4595 if (prevTx || !tx.IsCoinBase()) {
4613 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4615 block.
vtx[0]->vin[0].scriptSig.begin())) {
4618 "block height mismatch in coinbase");
4633 const std::optional<CCheckpointData> &test_checkpoints) {
4640 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4642 if (miSelf !=
m_blockman.m_block_index.end()) {
4649 if (pindex->nStatus.isInvalid()) {
4662 "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__,
4668 BlockMap::iterator mi{
4672 "header %s has prev block not found: %s\n",
4675 "prev-blk-not-found");
4680 if (pindexPrev->nStatus.isInvalid()) {
4682 "header %s has prev block invalid: %s\n", hash.
ToString(),
4692 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4723 if (pindexPrev->
GetAncestor(failedit->nHeight) == failedit) {
4724 assert(failedit->nStatus.hasFailed());
4726 while (invalid_walk != failedit) {
4727 invalid_walk->nStatus =
4728 invalid_walk->nStatus.withFailedParent();
4730 invalid_walk = invalid_walk->
pprev;
4733 "header %s has prev block invalid: %s\n",
4742 if (!min_pow_checked) {
4744 "%s: not adding new block header %s, missing anti-dos "
4745 "proof-of-work validation\n",
4748 "too-little-chainwork");
4764 const auto msg =
strprintf(
"Saw new header hash=%s height=%d",
4778 const std::vector<CBlockHeader> &headers,
bool min_pow_checked,
4780 const std::optional<CCheckpointData> &test_checkpoints) {
4788 header, state, &pindex, min_pow_checked, test_checkpoints);
4804 const int64_t blocks_left{
4805 (
GetTime() - last_accepted.GetBlockTime()) /
4807 const double progress{100.0 * last_accepted.nHeight /
4808 (last_accepted.nHeight + blocks_left)};
4809 LogPrintf(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4810 last_accepted.nHeight, progress);
4818 int64_t timestamp) {
4827 if (m_best_header->nChainWork >=
4833 auto now = Now<SteadyMilliseconds>();
4834 if (now < m_last_presync_update + 250ms) {
4837 m_last_presync_update = now;
4841 height, timestamp,
true);
4842 if (initial_download) {
4843 const int64_t blocks_left{(
GetTime() - timestamp) /
4845 const double progress{100.0 * height / (height + blocks_left)};
4846 LogPrintf(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4854 bool *fNewBlock,
bool min_pow_checked) {
4857 const CBlock &block = *pblock;
4864 bool accepted_header{
4868 if (!accepted_header) {
4876 bool fAlreadyHave = pindex->nStatus.hasData();
4888 int64_t chainTipTimeDiff =
4894 LogPrintf(
"Chain tip timestamp-to-received-time difference: hash=%s, "
4897 LogPrintf(
"New block timestamp-to-received-time difference: hash=%s, "
4902 bool fHasMoreOrSameWork =
4910 bool fTooFarAhead{pindex->
nHeight >
4923 if (pindex->
nTx != 0) {
4928 if (!fHasMoreOrSameWork) {
4952 pindex->nStatus = pindex->nStatus.withFailed();
4956 return error(
"%s: %s (block %s)", __func__, state.
ToString(),
4970 std::optional<int> snapshot_base_height = GetSnapshotBaseHeight();
4971 const bool is_background_block =
4973 pindex->
nHeight <= snapshot_base_height;
4975 if (!is_background_block && pindexFork &&
4977 LogPrintf(
"Park block %s as it would cause a deep reorg.\n",
4979 pindex->nStatus = pindex->nStatus.withParked();
4998 if (blockPos.IsNull()) {
5000 "%s: Failed to find position to write new block to disk",
5005 }
catch (
const std::runtime_error &e) {
5006 return AbortNode(state, std::string(
"System error: ") + e.what());
5024 const std::shared_ptr<const CBlock> &block,
bool force_processing,
5025 bool min_pow_checked,
bool *new_block,
5054 ret =
AcceptBlock(block, state, force_processing,
nullptr,
5055 new_block, min_pow_checked);
5060 return error(
"%s: AcceptBlock FAILED (%s)", __func__,
5070 return error(
"%s: ActivateBestChain failed (%s)", __func__,
5075 ? m_ibd_chainstate.get()
5078 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
5079 return error(
"%s: [background] ActivateBestChain failed (%s)", __func__,
5097 false, test_accept);
5113 indexDummy.
pprev = pindexPrev;
5120 adjusted_time_callback())) {
5121 return error(
"%s: Consensus::ContextualCheckBlockHeader: %s", __func__,
5126 return error(
"%s: Consensus::CheckBlock: %s", __func__,
5132 return error(
"%s: Consensus::ContextualCheckBlock: %s", __func__,
5136 if (!chainstate.
ConnectBlock(block, state, &indexDummy, viewNew,
5137 validationOptions,
nullptr,
true)) {
5147 int nManualPruneHeight) {
5150 nManualPruneHeight)) {
5151 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
5157 FopenFn mockable_fopen_function) {
5187 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5218 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
5222 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5223 LogPrintf(
"Verifying last %i blocks at level %i\n", nCheckDepth,
5229 int nGoodTransactions = 0;
5232 bool skipped_no_block_data{
false};
5233 bool skipped_l3_checks{
false};
5234 LogPrintf(
"Verification progress: 0%%\n");
5239 pindex = pindex->
pprev) {
5240 const int percentageDone = std::max(
5241 1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() -
5243 (
double)nCheckDepth *
5244 (nCheckLevel >= 4 ? 50 : 100))));
5245 if (reportDone < percentageDone / 10) {
5247 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
5248 reportDone = percentageDone / 10;
5257 !pindex->nStatus.hasData()) {
5260 LogPrintf(
"VerifyDB(): block verification stopping at height %d "
5261 "(no data). This could be due to pruning or use of an "
5262 "assumeutxo snapshot.\n",
5264 skipped_no_block_data =
true;
5273 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5279 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensusParams,
5282 "Verification error: found bad block at %d, hash=%s (%s)\n",
5289 if (nCheckLevel >= 2 && pindex) {
5293 LogPrintf(
"Verification error: found bad undo data at %d, "
5306 if (nCheckLevel >= 3) {
5310 chainstate.DisconnectBlock(block, pindex, coins);
5312 LogPrintf(
"Verification error: irrecoverable inconsistency "
5313 "in block data at %d, hash=%s\n",
5319 nGoodTransactions = 0;
5320 pindexFailure = pindex;
5322 nGoodTransactions += block.
vtx.size();
5325 skipped_l3_checks =
true;
5334 if (pindexFailure) {
5335 LogPrintf(
"Verification error: coin database inconsistencies found "
5336 "(last %i blocks, %i good transactions before that)\n",
5341 if (skipped_l3_checks) {
5342 LogPrintf(
"Skipped verification of level >=3 (insufficient database "
5343 "cache size). Consider increasing -dbcache.\n");
5350 if (nCheckLevel >= 4 && !skipped_l3_checks) {
5352 const int percentageDone = std::max(
5353 1, std::min(99, 100 -
int(
double(chainstate.
m_chain.
Height() -
5355 double(nCheckDepth) * 50)));
5356 if (reportDone < percentageDone / 10) {
5358 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
5359 reportDone = percentageDone / 10;
5366 LogPrintf(
"Verification error: ReadBlockFromDisk failed at %d, "
5371 if (!chainstate.
ConnectBlock(block, state, pindex, coins,
5373 LogPrintf(
"Verification error: found unconnectable block at "
5374 "%d, hash=%s (%s)\n",
5385 LogPrintf(
"Verification: No coin database inconsistencies in last %i "
5386 "blocks (%i transactions)\n",
5387 block_count, nGoodTransactions);
5389 if (skipped_l3_checks) {
5392 if (skipped_no_block_data) {
5408 return error(
"ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s",
5418 if (tx->IsCoinBase()) {
5422 for (
const CTxIn &txin : tx->vin) {
5436 std::vector<BlockHash> hashHeads =
db.GetHeadBlocks();
5437 if (hashHeads.empty()) {
5441 if (hashHeads.size() != 2) {
5442 return error(
"ReplayBlocks(): unknown inconsistent state");
5455 if (
m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5457 "ReplayBlocks(): reorganization to unknown block requested");
5460 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
5462 if (!hashHeads[1].IsNull()) {
5464 if (
m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5466 "ReplayBlocks(): reorganization from unknown block requested");
5469 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
5471 assert(pindexFork !=
nullptr);
5475 while (pindexOld != pindexFork) {
5480 return error(
"RollbackBlock(): ReadBlockFromDisk() failed at "
5491 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s",
5503 pindexOld = pindexOld->
pprev;
5507 int nForkHeight = pindexFork ? pindexFork->
nHeight : 0;
5514 _(
"Replaying blocks…"),
5515 (
int)((
nHeight - nForkHeight) * 100.0 /
5516 (pindexNew->
nHeight - nForkHeight)),
5531void Chainstate::ClearBlockIndexCandidates() {
5547 const uint64_t numHeaders{20};
5550 const fs::path filePathTmp = filePath +
".new";
5561 bool missingIndex{
false};
5562 for (uint64_t i = 0; i < numHeaders; i++) {
5564 LogPrintf(
"Missing block index, stopping the headers time "
5565 "dumping after %d blocks.\n",
5567 missingIndex =
true;
5574 index = index->
pprev;
5578 throw std::runtime_error(
strprintf(
"Failed to commit to file %s",
5584 fs::remove(filePathTmp);
5589 throw std::runtime_error(
strprintf(
"Rename failed from %s to %s",
5593 }
catch (
const std::exception &e) {
5594 LogPrintf(
"Failed to dump the headers time: %s.\n", e.what());
5598 LogPrintf(
"Successfully dumped the last %d headers time to %s.\n",
5614 LogPrintf(
"Failed to open header times from disk, skipping.\n");
5623 LogPrintf(
"Unsupported header times file version, skipping.\n");
5630 for (uint64_t i = 0; i < numBlocks; i++) {
5632 int64_t receiveTime;
5635 file >> receiveTime;
5639 LogPrintf(
"Missing index for block %s, stopping the headers "
5640 "time loading after %d blocks.\n",
5647 }
catch (
const std::exception &e) {
5648 LogPrintf(
"Failed to read the headers time file data on disk: %s.\n",
5666 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5668 std::vector<CBlockIndex *> vSortedByHeight{
5670 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5682 if (pindex == GetSnapshotBaseBlock() ||
5690 if (pindex->nStatus.isInvalid() &&
5692 pindex->
nChainWork > m_best_invalid->nChainWork)) {
5693 m_best_invalid = pindex;
5696 if (pindex->nStatus.isOnParkedChain() &&
5698 pindex->
nChainWork > m_best_parked->nChainWork)) {
5699 m_best_parked = pindex;
5703 (m_best_header ==
nullptr ||
5705 m_best_header = pindex;
5709 needs_init =
m_blockman.m_block_index.empty();
5719 LogPrintf(
"Initializing databases...\n");
5740 if (blockPos.IsNull()) {
5741 return error(
"%s: writing genesis block to disk failed", __func__);
5746 }
catch (
const std::runtime_error &e) {
5747 return error(
"%s: failed to write genesis block: %s", __func__,
5756 std::multimap<BlockHash, FlatFilePos> *blocks_with_unknown_parent,
5759 assert(!dbp == !blocks_with_unknown_parent);
5773 uint64_t nRewind = blkdat.
GetPos();
5774 while (!blkdat.
eof()) {
5784 unsigned int nSize = 0;
5789 nRewind = blkdat.
GetPos() + 1;
5791 if (memcmp(buf, params.
DiskMagic().data(),
5801 }
catch (
const std::exception &) {
5809 const uint64_t nBlockPos{blkdat.
GetPos()};
5811 dbp->
nPos = nBlockPos;
5813 blkdat.
SetLimit(nBlockPos + nSize);
5821 nRewind = nBlockPos + nSize;
5826 std::shared_ptr<CBlock> pblock{};
5835 "%s: Out of order block %s, parent %s not known\n",
5836 __func__, hash.ToString(),
5838 if (dbp && blocks_with_unknown_parent) {
5839 blocks_with_unknown_parent->emplace(
5848 if (!pindex || !pindex->nStatus.hasData()) {
5851 blkdat.
SetPos(nBlockPos);
5852 pblock = std::make_shared<CBlock>();
5854 nRewind = blkdat.
GetPos();
5857 if (
AcceptBlock(pblock, state,
true, dbp,
nullptr,
5865 pindex->
nHeight % 1000 == 0) {
5868 "Block Import: already had block %s at height %d\n",
5869 hash.ToString(), pindex->
nHeight);
5876 bool genesis_activation_failure =
false;
5877 for (
auto c :
GetAll()) {
5879 if (!c->ActivateBestChain(state,
nullptr,
avalanche)) {
5880 genesis_activation_failure =
true;
5884 if (genesis_activation_failure) {
5898 bool activation_failure =
false;
5899 for (
auto c :
GetAll()) {
5901 if (!c->ActivateBestChain(state, pblock,
avalanche)) {
5903 "failed to activate chain (%s)\n",
5905 activation_failure =
true;
5909 if (activation_failure) {
5916 if (!blocks_with_unknown_parent) {
5922 std::deque<BlockHash> queue;
5923 queue.push_back(hash);
5924 while (!queue.empty()) {
5927 auto range = blocks_with_unknown_parent->equal_range(head);
5928 while (range.first != range.second) {
5929 std::multimap<BlockHash, FlatFilePos>::iterator it =
5931 std::shared_ptr<CBlock> pblockrecursive =
5932 std::make_shared<CBlock>();
5937 "%s: Processing out of order child %s of %s\n",
5938 __func__, pblockrecursive->GetHash().ToString(),
5943 &it->second,
nullptr,
true)) {
5945 queue.push_back(pblockrecursive->GetHash());
5949 blocks_with_unknown_parent->erase(it);
5953 }
catch (
const std::exception &e) {
5974 "%s: unexpected data at file offset 0x%x - %s. "
5976 __func__, (nRewind - 1), e.what());
5979 }
catch (
const std::runtime_error &e) {
5980 AbortNode(std::string(
"System error: ") + e.what());
5983 LogPrintf(
"Loaded %i blocks from external file in %dms\n", nLoaded,
6004 std::multimap<CBlockIndex *, CBlockIndex *> forward;
6005 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
6006 forward.emplace(block_index.pprev, &block_index);
6011 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6012 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6013 rangeGenesis = forward.equal_range(
nullptr);
6015 rangeGenesis.first++;
6017 assert(rangeGenesis.first == rangeGenesis.second);
6039 CBlockIndex *pindexFirstNotTransactionsValid =
nullptr;
6045 CBlockIndex *pindexFirstNotScriptsValid =
nullptr;
6051 const CBlockIndex *snap_base{GetSnapshotBaseBlock()};
6052 CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{},
6053 *snap_first_nocv{}, *snap_first_nosv{};
6054 auto snap_update_firsts = [&] {
6055 if (pindex == snap_base) {
6056 std::swap(snap_first_missing, pindexFirstMissing);
6057 std::swap(snap_first_notx, pindexFirstNeverProcessed);
6058 std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
6059 std::swap(snap_first_nocv, pindexFirstNotChainValid);
6060 std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
6064 while (pindex !=
nullptr) {
6066 if (pindexFirstInvalid ==
nullptr && pindex->nStatus.hasFailed()) {
6067 pindexFirstInvalid = pindex;
6069 if (pindexFirstParked ==
nullptr && pindex->nStatus.isParked()) {
6070 pindexFirstParked = pindex;
6072 if (pindexFirstMissing ==
nullptr && !pindex->nStatus.hasData()) {
6073 pindexFirstMissing = pindex;
6075 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) {
6076 pindexFirstNeverProcessed = pindex;
6078 if (pindex->
pprev !=
nullptr && pindexFirstNotTreeValid ==
nullptr &&
6080 pindexFirstNotTreeValid = pindex;
6082 if (pindex->
pprev !=
nullptr) {
6083 if (pindexFirstNotTransactionsValid ==
nullptr &&
6085 pindexFirstNotTransactionsValid = pindex;
6087 if (pindexFirstNotChainValid ==
nullptr &&
6089 pindexFirstNotChainValid = pindex;
6091 if (pindexFirstNotScriptsValid ==
nullptr &&
6093 pindexFirstNotScriptsValid = pindex;
6098 if (pindex->
pprev ==
nullptr) {
6102 for (
auto c :
GetAll()) {
6103 if (c->m_chain.Genesis() !=
nullptr) {
6105 assert(pindex == c->m_chain.Genesis());
6120 assert(pindex->nStatus.hasData() == (pindex->
nTx > 0));
6121 assert(pindexFirstMissing == pindexFirstNeverProcessed);
6122 }
else if (pindex->nStatus.hasData()) {
6127 if (pindex->nStatus.hasUndo()) {
6128 assert(pindex->nStatus.hasData());
6130 if (snap_base && snap_base->GetAncestor(pindex->
nHeight) == pindex) {
6142 assert((pindexFirstNeverProcessed ==
nullptr || pindex == snap_base) ==
6144 assert((pindexFirstNotTransactionsValid ==
nullptr ||
6156 assert(pindexFirstNotTreeValid ==
nullptr);
6159 assert(pindexFirstNotTreeValid ==
nullptr);
6163 assert(pindexFirstNotChainValid ==
nullptr);
6167 assert(pindexFirstNotScriptsValid ==
nullptr);
6169 if (pindexFirstInvalid ==
nullptr) {
6172 assert(!pindex->nStatus.isInvalid());
6174 if (pindexFirstParked ==
nullptr) {
6178 assert(!pindex->nStatus.isOnParkedChain());
6181 if (!pindex->
pprev) {
6195 for (
auto c :
GetAll()) {
6196 if (c->m_chain.Tip() ==
nullptr) {
6213 (pindexFirstNeverProcessed ==
nullptr || pindex == snap_base)) {
6217 if (pindexFirstInvalid ==
nullptr) {
6224 GetSnapshotBaseBlock()->GetAncestor(pindex->
nHeight) ==
6238 if (pindexFirstMissing ==
nullptr) {
6239 assert(pindex->nStatus.isOnParkedChain() ||
6240 c->setBlockIndexCandidates.count(pindex));
6248 if (pindex == c->m_chain.Tip() ||
6249 pindex == c->SnapshotBase()) {
6250 assert(c->setBlockIndexCandidates.count(pindex));
6262 assert(c->setBlockIndexCandidates.count(pindex) == 0);
6266 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6267 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6270 bool foundInUnlinked =
false;
6271 while (rangeUnlinked.first != rangeUnlinked.second) {
6272 assert(rangeUnlinked.first->first == pindex->
pprev);
6273 if (rangeUnlinked.first->second == pindex) {
6274 foundInUnlinked =
true;
6277 rangeUnlinked.first++;
6279 if (pindex->
pprev && pindex->nStatus.hasData() &&
6280 pindexFirstNeverProcessed !=
nullptr &&
6281 pindexFirstInvalid ==
nullptr) {
6287 if (!pindex->nStatus.hasData()) {
6289 assert(!foundInUnlinked);
6291 if (pindexFirstMissing ==
nullptr) {
6294 assert(!foundInUnlinked);
6296 if (pindex->
pprev && pindex->nStatus.hasData() &&
6297 pindexFirstNeverProcessed ==
nullptr &&
6298 pindexFirstMissing !=
nullptr) {
6311 for (
auto c :
GetAll()) {
6314 c->setBlockIndexCandidates.count(pindex) == 0) {
6315 if (pindexFirstInvalid ==
nullptr) {
6317 snap_base->GetAncestor(pindex->
nHeight) == pindex) {
6329 snap_update_firsts();
6330 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6331 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6332 range = forward.equal_range(pindex);
6333 if (range.first != range.second) {
6335 pindex = range.first->second;
6343 snap_update_firsts();
6346 if (pindex == pindexFirstInvalid) {
6347 pindexFirstInvalid =
nullptr;
6349 if (pindex == pindexFirstParked) {
6350 pindexFirstParked =
nullptr;
6352 if (pindex == pindexFirstMissing) {
6353 pindexFirstMissing =
nullptr;
6355 if (pindex == pindexFirstNeverProcessed) {
6356 pindexFirstNeverProcessed =
nullptr;
6358 if (pindex == pindexFirstNotTreeValid) {
6359 pindexFirstNotTreeValid =
nullptr;
6361 if (pindex == pindexFirstNotTransactionsValid) {
6362 pindexFirstNotTransactionsValid =
nullptr;
6364 if (pindex == pindexFirstNotChainValid) {
6365 pindexFirstNotChainValid =
nullptr;
6367 if (pindex == pindexFirstNotScriptsValid) {
6368 pindexFirstNotScriptsValid =
nullptr;
6373 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6374 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6375 rangePar = forward.equal_range(pindexPar);
6376 while (rangePar.first->second != pindex) {
6379 assert(rangePar.first != rangePar.second);
6384 if (rangePar.first != rangePar.second) {
6386 pindex = rangePar.first->second;
6398 assert(nNodes == forward.size());
6404 return strprintf(
"Chainstate [%s] @ height %d (%s)",
6410bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size) {
6423 coinsdb_size * (1.0 / 1024 / 1024));
6425 coinstip_size * (1.0 / 1024 / 1024));
6430 if (coinstip_size > old_coinstip_size) {
6447 if (pindex ==
nullptr) {
6452 "Block %d has unset m_chain_tx_count. Unable to "
6453 "estimate verification progress.\n",
6458 int64_t nNow = time(
nullptr);
6473 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6475 return m_active_chainstate->m_from_snapshot_blockhash;
6477 return std::nullopt;
6482 std::vector<Chainstate *> out;
6485 {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6487 out.push_back(pchainstate);
6496 assert(!m_ibd_chainstate);
6497 assert(!m_active_chainstate);
6499 m_ibd_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman, *
this);
6500 m_active_chainstate = m_ibd_chainstate.get();
6501 return *m_active_chainstate;
6514 const bool existed{fs::remove(base_blockhash_path)};
6516 LogPrintf(
"[snapshot] snapshot chainstate dir being removed "
6520 }
catch (
const fs::filesystem_error &e) {
6521 LogPrintf(
"[snapshot] failed to remove file %s: %s\n",
6528 LogPrintf(
"Removing leveldb dir at %s\n", path_str);
6532 const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
6535 LogPrintf(
"error: leveldb DestroyDB call failed on %s\n", path_str);
6553 "Can't activate a snapshot-based chainstate more than once")};
6561 if (!
GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
6563 std::string heights_formatted =
6564 Join(available_heights,
", ",
6565 [&](
const auto &i) {
return ToString(i); });
6567 Untranslated(
"assumeutxo block hash in snapshot metadata not "
6568 "recognized (hash: %s). The following "
6569 "snapshot heights are available: %s."),
6570 base_blockhash.
ToString(), heights_formatted)};
6574 if (!snapshot_start_block) {
6576 Untranslated(
"The base block header (%s) must appear in the "
6577 "headers chain. Make sure all headers are "
6578 "syncing, and call loadtxoutset again."),
6582 if (snapshot_start_block->nStatus.isInvalid()) {
6585 "The base block header (%s) is part of an invalid chain"),
6589 if (!m_best_header ||
6590 m_best_header->GetAncestor(snapshot_start_block->nHeight) !=
6591 snapshot_start_block) {
6593 "A forked headers-chain with more work than the chain with the "
6594 "snapshot base block header exists. Please proceed to sync "
6595 "without AssumeUtxo.")};
6598 if (
Assert(m_active_chainstate->GetMempool())->size() > 0) {
6600 "Can't activate a snapshot when mempool not empty.")};
6604 int64_t current_coinsdb_cache_size{0};
6605 int64_t current_coinstip_cache_size{0};
6614 static constexpr double IBD_CACHE_PERC = 0.01;
6615 static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
6628 current_coinsdb_cache_size =
6630 current_coinstip_cache_size =
6636 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6637 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6640 auto snapshot_chainstate =
6647 snapshot_chainstate->InitCoinsDB(
6648 static_cast<size_t>(current_coinsdb_cache_size *
6649 SNAPSHOT_CACHE_PERC),
6650 in_memory,
false,
"chainstate");
6651 snapshot_chainstate->InitCoinsCache(
static_cast<size_t>(
6652 current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
6655 auto cleanup_bad_snapshot =
6657 this->MaybeRebalanceCaches();
6667 snapshot_chainstate.reset();
6672 "Failed to remove snapshot chainstate dir (%s). "
6673 "Manually remove it before restarting.\n",
6683 return cleanup_bad_snapshot(
Untranslated(
"population failed"));
6694 snapshot_chainstate->m_chain.Tip())) {
6695 return cleanup_bad_snapshot(
6696 Untranslated(
"work does not exceed active chainstate"));
6702 return cleanup_bad_snapshot(
6707 assert(!m_snapshot_chainstate);
6708 m_snapshot_chainstate.swap(snapshot_chainstate);
6709 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6714 Assert(m_active_chainstate->m_mempool->size() == 0);
6715 Assert(!m_snapshot_chainstate->m_mempool);
6716 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
6717 m_active_chainstate->m_mempool =
nullptr;
6718 m_active_chainstate = m_snapshot_chainstate.get();
6721 LogPrintf(
"[snapshot] successfully activated snapshot %s\n",
6724 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() /
6727 this->MaybeRebalanceCaches();
6728 return snapshot_start_block;
6732 bool snapshot_loaded) {
6735 snapshot_loaded ?
"saving snapshot chainstate"
6736 :
"flushing coins cache",
6738 BCLog::LogFlags::ALL);
6740 coins_cache.
Flush();
6744 const char *
what()
const throw()
override {
6745 return "ComputeUTXOStats interrupted by shutdown.";
6769 if (!snapshot_start_block) {
6772 LogPrintf(
"[snapshot] Did not find snapshot start blockheader %s\n",
6777 int base_height = snapshot_start_block->
nHeight;
6780 if (!maybe_au_data) {
6781 LogPrintf(
"[snapshot] assumeutxo height in snapshot metadata not "
6782 "recognized (%d) - refusing to load snapshot\n",
6794 LogPrintf(
"[snapshot] activation failed - work does not exceed active "
6802 LogPrintf(
"[snapshot] loading %d coins from snapshot %s\n", coins_left,
6804 int64_t coins_processed{0};
6806 while (coins_left > 0) {
6810 size_t coins_per_txid{0};
6813 if (coins_per_txid > coins_left) {
6814 LogPrintf(
"[snapshot] mismatch in coins count in snapshot "
6815 "metadata and actual snapshot data\n");
6819 for (
size_t i = 0; i < coins_per_txid; i++) {
6825 if (coin.
GetHeight() > uint32_t(base_height) ||
6827 std::numeric_limits<
decltype(outpoint.GetN())>::max()) {
6828 LogPrintf(
"[snapshot] bad snapshot data after "
6829 "deserializing %d coins\n",
6830 coins_count - coins_left);
6834 LogPrintf(
"[snapshot] bad snapshot data after "
6835 "deserializing %d coins - bad tx out value\n",
6836 coins_count - coins_left);
6845 if (coins_processed % 1000000 == 0) {
6846 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6848 static_cast<float>(coins_processed) * 100 /
6849 static_cast<float>(coins_count),
6857 if (coins_processed % 120000 == 0) {
6862 const auto snapshot_cache_state =
WITH_LOCK(
6864 return snapshot_chainstate.GetCoinsCacheSizeState());
6881 }
catch (
const std::ios_base::failure &) {
6882 LogPrintf(
"[snapshot] bad snapshot format or truncated snapshot "
6883 "after deserializing %d coins\n",
6896 bool out_of_coins{
false};
6900 }
catch (
const std::ios_base::failure &) {
6902 out_of_coins =
true;
6904 if (!out_of_coins) {
6905 LogPrintf(
"[snapshot] bad snapshot - coins left over after "
6906 "deserializing %d coins\n",
6911 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6925 std::optional<CCoinsStats> maybe_stats;
6934 if (!maybe_stats.has_value()) {
6935 LogPrintf(
"[snapshot] failed to generate coins stats\n");
6943 LogPrintf(
"[snapshot] bad snapshot content hash: expected %s, got %s\n",
6945 maybe_stats->hashSerialized.ToString());
6961 constexpr int AFTER_GENESIS_START{1};
6963 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height();
6965 index = snapshot_chainstate.
m_chain[i];
6976 assert(index == snapshot_start_block);
6980 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
7003 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
7004 !this->IsUsable(m_snapshot_chainstate.get()) ||
7005 !this->IsUsable(m_ibd_chainstate.get()) ||
7006 !m_ibd_chainstate->m_chain.Tip()) {
7012 const int snapshot_base_height = *
Assert(this->GetSnapshotBaseHeight());
7015 if (index_new.
nHeight < snapshot_base_height) {
7025 _(
"%s failed to validate the -assumeutxo snapshot state. "
7026 "This indicates a hardware problem, or a bug in the software, or "
7027 "a bad software modification that allowed an invalid snapshot to "
7028 "be loaded. As a result of this, the node will shut down and "
7029 "stop using any state that was built on the snapshot, resetting "
7030 "the chain height from %d to %d. On the next restart, the node "
7031 "will resume syncing from %d without using any snapshot data. "
7032 "Please report this incident to %s, including how you obtained "
7033 "the snapshot. The invalid snapshot chainstate will be left on "
7034 "disk in case it is helpful in diagnosing the issue that caused "
7036 PACKAGE_NAME, snapshot_tip_height, snapshot_base_height,
7037 snapshot_base_height, PACKAGE_BUGREPORT);
7040 LogPrintf(
"[snapshot] deleting snapshot, reverting to validated chain, "
7041 "and stopping node\n");
7043 m_active_chainstate = m_ibd_chainstate.get();
7044 m_snapshot_chainstate->m_disabled =
true;
7048 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
7049 if (!rename_result) {
7054 shutdown_fnc(user_error);
7059 "[snapshot] supposed base block %s does not match the "
7060 "snapshot base block %s (height %d). Snapshot is not valid.\n",
7062 snapshot_base_height);
7063 handle_invalid_snapshot();
7069 int curr_height = m_ibd_chainstate->m_chain.Height();
7071 assert(snapshot_base_height == curr_height);
7076 CCoinsViewDB &ibd_coins_db = m_ibd_chainstate->CoinsDB();
7077 m_ibd_chainstate->ForceFlushStateToDisk();
7079 const auto &maybe_au_data =
7081 if (!maybe_au_data) {
7082 LogPrintf(
"[snapshot] assumeutxo data not found for height "
7083 "(%d) - refusing to validate snapshot\n",
7085 handle_invalid_snapshot();
7090 std::optional<CCoinsStats> maybe_ibd_stats;
7092 "[snapshot] computing UTXO stats for background chainstate to validate "
7093 "snapshot - this could take a few minutes\n");
7102 if (!maybe_ibd_stats) {
7104 "[snapshot] failed to generate stats for validation coins db\n");
7108 handle_invalid_snapshot();
7111 const auto &ibd_stats = *maybe_ibd_stats;
7120 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
7121 ibd_stats.hashSerialized.ToString(),
7123 handle_invalid_snapshot();
7127 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
7130 m_ibd_chainstate->m_disabled =
true;
7131 this->MaybeRebalanceCaches();
7138 assert(m_active_chainstate);
7139 return *m_active_chainstate;
7144 return m_snapshot_chainstate &&
7145 m_active_chainstate == m_snapshot_chainstate.get();
7147void ChainstateManager::MaybeRebalanceCaches() {
7149 bool ibd_usable = this->
IsUsable(m_ibd_chainstate.get());
7150 bool snapshot_usable = this->
IsUsable(m_snapshot_chainstate.get());
7151 assert(ibd_usable || snapshot_usable);
7153 if (ibd_usable && !snapshot_usable) {
7154 LogPrintf(
"[snapshot] allocating all cache to the IBD chainstate\n");
7158 }
else if (snapshot_usable && !ibd_usable) {
7162 "[snapshot] allocating all cache to the snapshot chainstate\n");
7166 }
else if (ibd_usable && snapshot_usable) {
7175 m_snapshot_chainstate->ResizeCoinsCaches(
7178 m_snapshot_chainstate->ResizeCoinsCaches(
7186void ChainstateManager::ResetChainstates() {
7187 m_ibd_chainstate.reset();
7188 m_snapshot_chainstate.reset();
7189 m_active_chainstate =
nullptr;
7198 if (!opts.check_block_index.has_value()) {
7200 opts.config.GetChainParams().DefaultConsistencyChecks();
7203 if (!opts.minimum_chain_work.has_value()) {
7205 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
7207 if (!opts.assumed_valid_block.has_value()) {
7208 opts.assumed_valid_block =
7209 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
7211 Assert(opts.adjusted_time_callback);
7212 return std::move(opts);
7218 m_blockman{
std::move(blockman_options)} {}
7220bool ChainstateManager::DetectSnapshotChainstate(
CTxMemPool *mempool) {
7221 assert(!m_snapshot_chainstate);
7226 std::optional<BlockHash> base_blockhash =
7228 if (!base_blockhash) {
7231 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
7234 this->ActivateExistingSnapshot(*base_blockhash);
7239ChainstateManager::ActivateExistingSnapshot(
BlockHash base_blockhash) {
7240 assert(!m_snapshot_chainstate);
7241 m_snapshot_chainstate = std::make_unique<Chainstate>(
nullptr,
m_blockman,
7242 *
this, base_blockhash);
7243 LogPrintf(
"[snapshot] switching active chainstate to %s\n",
7244 m_snapshot_chainstate->ToString());
7247 Assert(m_active_chainstate->m_mempool->size() == 0);
7248 Assert(!m_snapshot_chainstate->m_mempool);
7249 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
7250 m_active_chainstate->m_mempool =
nullptr;
7251 m_active_chainstate = m_snapshot_chainstate.get();
7252 return *m_snapshot_chainstate;
7259 assert(
cs.m_from_snapshot_blockhash);
7260 auto storage_path_maybe =
cs.CoinsDB().StoragePath();
7262 assert(storage_path_maybe);
7263 return *storage_path_maybe;
7272 auto invalid_path = snapshot_datadir +
"_INVALID";
7275 LogPrintf(
"[snapshot] renaming snapshot datadir %s to %s\n", dbpath,
7282 fs::rename(snapshot_datadir, invalid_path);
7283 }
catch (
const fs::filesystem_error &e) {
7287 LogPrintf(
"%s: error renaming file '%s' -> '%s': %s\n", __func__,
7288 src_str, dest_str, e.what());
7290 "You should resolve this by manually "
7291 "moving or deleting the invalid "
7292 "snapshot directory %s, otherwise you "
7293 "will encounter the same error again "
7294 "on the next startup."),
7295 src_str, dest_str, src_str)};
7300bool ChainstateManager::DeleteSnapshotChainstate() {
7302 Assert(m_snapshot_chainstate);
7303 Assert(m_ibd_chainstate);
7308 LogPrintf(
"Deletion of %s failed. Please remove it manually to "
7309 "continue reindexing.\n",
7313 m_active_chainstate = m_ibd_chainstate.get();
7314 m_active_chainstate->m_mempool = m_snapshot_chainstate->m_mempool;
7315 m_snapshot_chainstate.reset();
7327const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock()
const {
7328 return m_active_chainstate ? m_active_chainstate->SnapshotBase() :
nullptr;
7331std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const {
7332 const CBlockIndex *base = this->GetSnapshotBaseBlock();
7333 return base ? std::make_optional(base->
nHeight) :
std::nullopt;
7336void ChainstateManager::RecalculateBestHeader() {
7339 for (
auto &entry :
m_blockman.m_block_index) {
7340 if (!(entry.second.nStatus.isInvalid()) &&
7341 m_best_header->nChainWork < entry.second.nChainWork) {
7342 m_best_header = &entry.second;
7347bool ChainstateManager::ValidatedSnapshotCleanup() {
7356 std::optional<fs::path> ibd_chainstate_path_maybe =
7357 get_storage_path(m_ibd_chainstate);
7358 std::optional<fs::path> snapshot_chainstate_path_maybe =
7359 get_storage_path(m_snapshot_chainstate);
7368 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
7369 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with "
7370 "in-memory chainstates. You are testing, right?\n");
7374 const auto &snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
7375 const auto &ibd_chainstate_path = *ibd_chainstate_path_maybe;
7383 this->ResetChainstates();
7388 LogPrintf(
"[snapshot] deleting background chainstate directory (now "
7389 "unnecessary) (%s)\n",
7392 fs::path tmp_old{ibd_chainstate_path +
"_todelete"};
7395 const fs::filesystem_error &err) {
7399 "Rename of '%s' -> '%s' failed. "
7400 "Cannot clean up the background chainstate leveldb directory.",
7405 fs::rename(ibd_chainstate_path, tmp_old);
7406 }
catch (
const fs::filesystem_error &e) {
7407 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
7411 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to "
7412 "default chainstate directory (%s)\n",
7417 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
7418 }
catch (
const fs::filesystem_error &e) {
7419 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
7426 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the "
7427 "directory is now unnecessary.\n",
7430 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
7436Chainstate &ChainstateManager::GetChainstateForIndexing() {
7440 return (this->
GetAll().size() > 1) ? *m_ibd_chainstate
7441 : *m_active_chainstate;
7445ChainstateManager::GetPruneRange(
const Chainstate &chainstate,
7446 int last_height_can_prune) {
7452 if (this->
GetAll().size() > 1 &&
7453 m_snapshot_chainstate.get() == &chainstate) {
7456 prune_start = *
Assert(GetSnapshotBaseHeight()) + 1;
7459 int max_prune = std::max<int>(0, chainstate.
m_chain.
Height() -
7469 int prune_end = std::min(last_height_can_prune, max_prune);
7471 return {prune_start, prune_end};
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)
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
Return the time it would take to redo the work difference between from and to, assuming the current h...
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
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.
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.
fs::path GetBlocksDirPath() const
Get blocks directory path.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Non-refcounted RAII wrapper for FILE*.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
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
bool m_checked_merkle_root
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
int64_t GetHeaderReceivedTime() const
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.
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
bool HaveNumChainTxs() const
Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot ...
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
int64_t GetReceivedTimeDiff() const
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
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
int64_t nTimeReceived
(memory only) block header metadata
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.
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
Prevent reading beyond a certain position.
uint64_t GetPos() const
return the current reading position
void FindByte(std::byte byte)
search for a given byte in the stream, and remain positioned on it
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
bool SetPos(uint64_t nPos)
rewind to a given reading position
bool eof() const
check whether we're at the end of the source file
An in-memory indexed chain of blocks.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
int Height() const
Return the maximal height in the chain.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const CBlock & GenesisBlock() const
std::vector< int > GetAvailableSnapshotHeights() const
const CMessageHeader::MessageMagic & DiskMagic() const
const ChainTxData & TxData() const
const Consensus::Params & GetConsensus() const
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
const CCheckpointData & Checkpoints() 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 TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, uint64_t mempool_sequence)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockChecked(const CBlock &, const BlockValidationState &)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
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
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.
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.
void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(
Initialize the in-memory coins cache (to be done after the health of the on-disk database is verified...
void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
Mutex m_chainstate_mutex
The ChainState Mutex.
bool ConnectTip(BlockValidationState &state, BlockPolicyValidationState &blockPolicyState, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, DisconnectedBlockTransactions &disconnectpool, const avalanche::Processor *const avalanche=nullptr, ChainstateRole chainstate_role=ChainstateRole::NORMAL) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
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?
CTxMemPool * GetMempool()
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
void SetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(voi ResetBlockFailureFlags)(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Set invalidity status to all descendants of a block.
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)
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, const avalanche::Processor *const avalanche=nullptr, ChainstateRole=ChainstateRole::NORMAL) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
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.
void LoadMempool(const fs::path &load_path, fsbridge::FopenFn mockable_fopen_function=fsbridge::fopen)
Load the persisted mempool from disk.
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
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.
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.
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
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 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.
CBlockIndex const * m_best_fork_tip
void TryAddBlockIndexCandidate(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) EXCLUSIVE_LOCKS_REQUIRED(bool FlushStateToDisk(BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
Resize the CoinsViews caches dynamically and flush state to disk.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate")
Return the current role of the chainstate.
CBlockIndex const * m_best_fork_base
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main
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.
void ClearAvalancheFinalizedBlock() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Clear avalanche finalization.
void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
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 ParkBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Park a block.
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(RecursiveMutex * MempoolMutex() const LOCK_RETURNED(m_mempool -> cs)
Dictates whether we need to flush the cache to disk or not.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
SnapshotCompletionResult MaybeCompleteSnapshotValidation(std::function< void(bilingual_str)> shutdown_fnc=[](bilingual_str msg) { AbortNode(msg.original, msg);}) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
std::atomic< int32_t > nBlockSequenceId
Every received block is assigned a unique and increasing identifier, so we know which one to give pri...
bool DetectSnapshotChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &ActivateExistingSnapshot(BlockHash base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(std::pair< int, int > GetPruneRange(const Chainstate &chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(boo DumpRecentHeadersTime)(const fs::path &filePath) const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
When starting up, search the datadir for a chainstate based on a UTXO snapshot that is in the process...
const Config & GetConfig() const
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
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...
kernel::Notifications & GetNotifications() const
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
bool ShouldCheckBlockIndex() const
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.
bool LoadRecentHeadersTime(const fs::path &filePath) EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Load the recent block headers reception time from a file.
void LoadExternalBlockFile(FILE *fileIn, FlatFilePos *dbp=nullptr, std::multimap< BlockHash, FlatFilePos > *blocks_with_unknown_parent=nullptr, avalanche::Processor *const avalanche=nullptr)
Import blocks from an external file.
std::optional< BlockHash > SnapshotBlockhash() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
bool IsUsable(const Chainstate *const pchainstate) const EXCLUSIVE_LOCKS_REQUIRED(
Return true if a chainstate is considered usable.
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
The state of a background sync (for net processing)
std::atomic< bool > m_cached_finished_ibd
Whether initial block download has ended and IsInitialBlockDownload should return false from now on.
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool IsSnapshotActive() const
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)
Sufficiently validate a block for disk storage (and store on disk).
std::function< void()> snapshot_download_completed
Function to restart active indexes; set dynamically to avoid a circular dependency on base/index....
const CChainParams & GetParams() const
bool ProcessNewBlockHeaders(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.
const Consensus::Params & GetConsensus() const
const arith_uint256 & MinimumChainWork() const
void CheckBlockIndex()
Make various assertions about the state of the block index.
util::Result< CBlockIndex * > ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(void ReportHeadersPresync(const arith_uint256 &work, int64_t height, int64_t timestamp)
Check to see if caches are out of balance and if so, call ResizeCoinsCaches() as needed.
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
const BlockHash & AssumedValidBlock() const
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...
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
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
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
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
bool CheckBlockDataAvailability(const CBlockIndex &upper_block LIFETIMEBOUND, const CBlockIndex &lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetFirstBlock(const CBlockIndex &upper_block LIFETIMEBOUND, std::function< bool(BlockStatus)> status_test, const CBlockIndex *lower_block=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned)
Check if all blocks in the [upper_block, lower_block] range have data available.
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, const FlatFilePos *dbp)
Store block on disk.
bool FlushChainstateBlockFile(int tip_height)
void FindFilesToPrune(std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool LoadingBlocks() const
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const std::optional< BlockHash > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex * AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove any pruned block & undo files that are still on disk.
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.
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain, ChainstateManager &chainman)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
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.
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_PACKAGE_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
@ 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 a finalized tx, i.e.
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_AVALANCHE_RECONSIDERABLE
fails some policy, but might be reconsidered by avalanche voting
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_CONSENSUS
invalid by consensus rules
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 RenameOver(fs::path src, fs::path dest)
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
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.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
bool error(const char *fmt, const Args &...args)
#define LogPrintLevel(category, level,...)
#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.
FILE * fopen(const fs::path &p, const char *mode)
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)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
std::atomic_bool fReindex
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.
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.
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
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.
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
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.
AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
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 shibusawaActivationTime
Unix time used for MTP activation of 15 Nov 2025 12:00:00 UTC upgrade.
int BIP34Height
Block height and hash at which BIP34 becomes active.
int nSubsidyHalvingInterval
BlockHash hashGenesisBlock
int64_t nPowTargetSpacing
bool fPowAllowMinDifficultyBlocks
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.
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.
bool store_recent_headers_time
If set, store and load the last few block headers reception time to speed up RTT bootstraping.
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)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
#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)
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Calculates the block height and previous block's median time past at which the transaction will be co...
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(
static bool NotifyHeaderTip(ChainstateManager &chainman) LOCKS_EXCLUDED(cs_main)
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
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...
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 CCheckQueue< CScriptCheck > scriptcheckqueue(128)
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
static int64_t nTimeVerify
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
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'.
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)
bool IsBlockMutated(const CBlock &block)
Check if a block has been mutated (with respect to its merkle root).
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 constexpr uint64_t HEADERS_TIME_VERSION
static int64_t nTimeCheck
static fs::path GetSnapshotCoinsDBPath(Chainstate &cs) EXCLUSIVE_LOCKS_REQUIRED(
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 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()