102 "level 0 reads the blocks from disk",
103 "level 1 verifies block validity",
104 "level 2 verifies undo data",
105 "level 3 checks disconnection of tip blocks",
106 "level 4 tries to reconnect the blocks",
107 "each level includes the checks of the previous levels",
124 : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true),
125 checkMerkleRoot(true) {}
162std::optional<std::vector<int>> CalculatePrevHeights(
const CBlockIndex &tip,
164 const CTransaction &tx) {
165 std::vector<int> prev_heights;
166 prev_heights.resize(tx.vin.size());
167 for (
size_t i = 0; i < tx.vin.size(); ++i) {
168 const CTxIn &txin = tx.vin[i];
170 if (!coins.
GetCoin(txin.prevout, coin)) {
171 LogPrintf(
"ERROR: %s: Missing input %d in transaction \'%s\'\n",
172 __func__, i, tx.GetId().GetHex());
177 prev_heights[i] = tip.
nHeight + 1;
188 const CTransaction &tx) {
191 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
192 if (!prev_heights.has_value()) {
197 next_tip.
pprev = tip;
232 const std::optional<int64_t> activation_time) {
233 if (pindexPrev ==
nullptr) {
256 for (
const CTxIn &txin : tx.vin) {
272 assert(txFrom->GetId() == txin.prevout.GetTxId());
273 assert(txFrom->vout.size() > txin.prevout.GetN());
286 validation_cache, 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) {
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)) {
565 "min relay fee not met",
567 m_pool.m_min_relay_feerate.GetFee(package_size)));
574 return m_active_chainstate.m_chainman.m_validation_cache;
586bool MemPoolAccept::PreChecks(ATMPArgs &args, Workspace &ws) {
590 const CTransaction &tx = *ws.m_ptx;
591 const TxId &txid = ws.m_ptx->GetId();
594 const int64_t nAcceptTime = args.m_accept_time;
595 const bool bypass_limits = args.m_bypass_limits;
596 std::vector<COutPoint> &coins_to_uncache = args.m_coins_to_uncache;
597 const unsigned int heightOverride = args.m_heightOverride;
609 if (m_pool.m_require_standard &&
611 m_pool.m_permit_bare_multisig,
612 m_pool.m_dust_relay_feerate, reason)) {
621 *
Assert(m_active_chainstate.m_chain.Tip()),
622 args.m_config.GetChainParams().GetConsensus(), tx, ctxState)) {
631 if (m_pool.exists(txid)) {
633 "txn-already-in-mempool");
637 for (
const CTxIn &txin : tx.vin) {
638 if (
const auto ptxConflicting = m_pool.GetConflictTx(txin.prevout)) {
639 if (m_pool.isAvalancheFinalizedPreConsensus(
640 ptxConflicting->GetId())) {
642 "finalized-tx-conflict");
647 "txn-mempool-conflict");
651 m_view.SetBackend(m_viewmempool);
655 for (
const CTxIn &txin : tx.vin) {
657 coins_to_uncache.push_back(txin.prevout);
664 if (!m_view.HaveCoin(txin.prevout)) {
666 for (
size_t out = 0; out < tx.vout.size(); out++) {
671 "txn-already-known");
678 "bad-txns-inputs-missingorspent");
683 if (!m_view.HaveInputs(tx)) {
685 "bad-txns-inputs-spent");
689 m_view.GetBestBlock();
694 m_view.SetBackend(m_dummy);
696 assert(m_active_chainstate.m_blockman.LookupBlockIndex(
697 m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
706 m_active_chainstate.m_chain.Tip(), m_view, tx)};
707 if (!lock_points.has_value() ||
717 m_active_chainstate.m_chain.Height() + 1,
724 if (m_pool.m_require_standard &&
727 "bad-txns-nonstandard-inputs");
731 ws.m_modified_fees = ws.m_base_fees;
732 m_pool.ApplyDelta(txid, ws.m_modified_fees);
734 unsigned int nSize = tx.GetTotalSize();
737 const uint32_t scriptVerifyFlags =
741 ws.m_precomputed_txdata, GetValidationCache(),
742 ws.m_sig_checks_standard)) {
747 ws.m_entry = std::make_unique<CTxMemPoolEntry>(
748 ptx, ws.m_base_fees, nAcceptTime,
749 heightOverride ? heightOverride : m_active_chainstate.m_chain.Height(),
750 ws.m_sig_checks_standard, lock_points.value());
752 ws.m_vsize = ws.m_entry->GetTxVirtualSize();
760 if (!bypass_limits &&
762 m_pool.m_min_relay_feerate.GetFee(ws.m_ptx->GetTotalSize())) {
769 m_pool.m_min_relay_feerate.GetFee(nSize)));
774 if (!bypass_limits && !args.m_package_feerates &&
775 !CheckFeeRate(nSize, ws.m_vsize, ws.m_modified_fees, state)) {
782bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs &args, Workspace &ws) {
785 const CTransaction &tx = *ws.m_ptx;
786 const TxId &txid = tx.GetId();
800 int nSigChecksConsensus;
802 tx, state, m_view, m_pool, ws.m_next_block_script_verify_flags,
803 ws.m_precomputed_txdata, GetValidationCache(), nSigChecksConsensus,
804 m_active_chainstate.CoinsTip())) {
808 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against "
809 "latest-block but not STANDARD flags %s, %s\n",
814 if (ws.m_sig_checks_standard != nSigChecksConsensus) {
819 "%s: BUG! PLEASE REPORT THIS! SigChecks count differed between "
820 "standard and consensus flags in %s\n",
827bool MemPoolAccept::Finalize(
const ATMPArgs &args, Workspace &ws) {
830 const TxId &txid = ws.m_ptx->GetId();
832 const bool bypass_limits = args.m_bypass_limits;
837 m_pool.addUnchecked(entry);
841 std::make_shared<
const std::vector<Coin>>(
843 m_pool.GetAndIncrementSequence());
850 if (!args.m_package_submission && !bypass_limits) {
851 m_pool.LimitSize(m_active_chainstate.CoinsTip());
852 if (!m_pool.exists(txid)) {
862bool MemPoolAccept::SubmitPackage(
863 const ATMPArgs &args, std::vector<Workspace> &workspaces,
865 std::map<TxId, MempoolAcceptResult> &results) {
870 workspaces.cbegin(), workspaces.cend(),
871 [
this](
const auto &ws) { return !m_pool.exists(ws.m_ptx->GetId()); }));
873 bool all_submitted =
true;
880 for (Workspace &ws : workspaces) {
881 if (!ConsensusScriptChecks(args, ws)) {
882 results.emplace(ws.m_ptx->GetId(),
885 all_submitted =
false;
888 strprintf(
"BUG! PolicyScriptChecks succeeded but "
889 "ConsensusScriptChecks failed: %s",
890 ws.m_ptx->GetId().ToString()));
900 if (!Finalize(args, ws)) {
901 results.emplace(ws.m_ptx->GetId(),
904 all_submitted =
false;
906 strprintf(
"BUG! Adding to mempool failed: %s",
907 ws.m_ptx->GetId().ToString()));
913 m_pool.LimitSize(m_active_chainstate.CoinsTip());
915 std::vector<TxId> all_package_txids;
916 all_package_txids.reserve(workspaces.size());
917 std::transform(workspaces.cbegin(), workspaces.cend(),
918 std::back_inserter(all_package_txids),
919 [](
const auto &ws) { return ws.m_ptx->GetId(); });
923 for (Workspace &ws : workspaces) {
924 const auto effective_feerate =
925 args.m_package_feerates
926 ? ws.m_package_feerate
928 static_cast<uint32_t
>(ws.m_vsize)};
929 const auto effective_feerate_txids =
930 args.m_package_feerates ? all_package_txids
931 : std::vector<TxId>({ws.m_ptx->GetId()});
932 results.emplace(ws.m_ptx->GetId(),
935 effective_feerate_txids));
937 return all_submitted;
948 const CBlockIndex *tip = m_active_chainstate.m_chain.Tip();
953 const std::vector<TxId> single_txid{ws.m_ptx->GetId()};
958 if (!PreChecks(args, ws)) {
959 if (ws.m_state.GetResult() ==
964 ws.m_state,
CFeeRate(ws.m_modified_fees, ws.m_vsize),
970 if (!ConsensusScriptChecks(args, ws)) {
974 const TxId txid = ptx->GetId();
984 if (
auto it = m_pool.mapNextTx.lower_bound(COutPoint{txid, 0});
985 it != m_pool.mapNextTx.end() && it->first->GetTxId() == txid) {
986 LogPrintf(
"%s: BUG! PLEASE REPORT THIS! Attempt to add txid %s, but "
987 "its outputs are already spent in the "
991 "txn-child-before-parent");
995 const CFeeRate effective_feerate{ws.m_modified_fees,
996 static_cast<uint32_t
>(ws.m_vsize)};
998 if (args.m_test_accept) {
1000 effective_feerate, single_txid);
1003 if (!Finalize(args, ws)) {
1007 Assume(ws.m_state.GetResult() ==
1010 ws.m_state,
CFeeRate(ws.m_modified_fees, ws.m_vsize), single_txid);
1014 effective_feerate, single_txid);
1018 const std::vector<CTransactionRef> &txns, ATMPArgs &args) {
1028 std::vector<Workspace> workspaces{};
1029 workspaces.reserve(txns.size());
1031 txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1032 [
this](
const auto &tx) {
1034 tx, GetNextBlockScriptFlags(m_active_chainstate.m_chain.Tip(),
1035 m_active_chainstate.m_chainman));
1037 std::map<TxId, MempoolAcceptResult> results;
1043 std::vector<TxId> valid_txids;
1044 for (Workspace &ws : workspaces) {
1045 if (!PreChecks(args, ws)) {
1047 "transaction failed");
1050 results.emplace(ws.m_ptx->GetId(),
1053 std::move(results));
1057 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1058 valid_txids.push_back(ws.m_ptx->GetId());
1073 const auto m_total_size = std::accumulate(
1074 workspaces.cbegin(), workspaces.cend(), int64_t{0},
1075 [](int64_t
sum,
auto &ws) { return sum + ws.m_ptx->GetTotalSize(); });
1076 const auto m_total_vsize =
1077 std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1078 [](int64_t
sum,
auto &ws) { return sum + ws.m_vsize; });
1079 const auto m_total_modified_fees = std::accumulate(
1080 workspaces.cbegin(), workspaces.cend(),
Amount::zero(),
1081 [](
Amount sum,
auto &ws) { return sum + ws.m_modified_fees; });
1082 const CFeeRate package_feerate(m_total_modified_fees, m_total_vsize);
1083 std::vector<TxId> all_package_txids;
1084 all_package_txids.reserve(workspaces.size());
1085 std::transform(workspaces.cbegin(), workspaces.cend(),
1086 std::back_inserter(all_package_txids),
1087 [](
const auto &ws) { return ws.m_ptx->GetId(); });
1089 if (args.m_package_feerates &&
1090 !CheckFeeRate(m_total_size, m_total_vsize, m_total_modified_fees,
1091 placeholder_state)) {
1093 "transaction failed");
1095 package_state, {{workspaces.back().m_ptx->GetId(),
1098 CFeeRate(m_total_modified_fees, m_total_vsize),
1099 all_package_txids)}});
1102 for (Workspace &ws : workspaces) {
1103 ws.m_package_feerate = package_feerate;
1104 const TxId &ws_txid = ws.m_ptx->GetId();
1105 if (args.m_test_accept &&
1106 std::find(valid_txids.begin(), valid_txids.end(), ws_txid) !=
1107 valid_txids.end()) {
1108 const auto effective_feerate =
1109 args.m_package_feerates
1110 ? ws.m_package_feerate
1112 static_cast<uint32_t
>(ws.m_vsize)};
1113 const auto effective_feerate_txids =
1114 args.m_package_feerates ? all_package_txids
1115 : std::vector<TxId>{ws.m_ptx->GetId()};
1119 results.emplace(ws_txid,
1121 ws.m_vsize, ws.m_base_fees, effective_feerate,
1122 effective_feerate_txids));
1126 if (args.m_test_accept) {
1130 if (!SubmitPackage(args, workspaces, package_state, results)) {
1139MemPoolAccept::AcceptSubPackage(
const std::vector<CTransactionRef> &subpackage,
1145 if (subpackage.size() > 1) {
1146 return AcceptMultipleTransactions(subpackage, args);
1148 const auto &tx = subpackage.front();
1149 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1150 const auto single_res = AcceptSingleTransaction(tx, single_args);
1152 if (single_res.m_result_type !=
1155 "transaction failed");
1158 {{tx->GetId(), single_res}});
1188 for (
const auto &outpoint : m_viewmempool.GetNonBaseCoins()) {
1192 m_view.Uncache(outpoint);
1195 m_viewmempool.Reset();
1211 if (!
CheckPackage(package, package_state_quit_early)) {
1220 "package-not-child-with-parents");
1225 assert(package.size() > 1);
1228 const auto &child = package.back();
1229 std::unordered_set<TxId, SaltedTxIdHasher> unconfirmed_parent_txids;
1231 package.cbegin(), package.cend() - 1,
1232 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1233 [](
const auto &tx) { return tx->GetId(); });
1242 const CCoinsViewCache &coins_tip_cache = m_active_chainstate.CoinsTip();
1243 for (
const auto &input : child->vin) {
1245 args.m_coins_to_uncache.push_back(input.prevout);
1252 m_view.
SetBackend(m_active_chainstate.CoinsTip());
1253 const auto package_or_confirmed = [
this, &unconfirmed_parent_txids](
1254 const auto &input) {
1255 return unconfirmed_parent_txids.count(input.prevout.GetTxId()) > 0 ||
1256 m_view.HaveCoin(input.prevout);
1258 if (!std::all_of(child->vin.cbegin(), child->vin.cend(),
1259 package_or_confirmed)) {
1260 package_state_quit_early.
Invalid(
1262 "package-not-child-with-unconfirmed-parents");
1268 m_view.SetBackend(m_dummy);
1274 std::map<TxId, MempoolAcceptResult> results_final;
1279 std::map<TxId, MempoolAcceptResult> individual_results_nonfinal;
1280 bool quit_early{
false};
1281 std::vector<CTransactionRef> txns_package_eval;
1282 for (
const auto &tx : package) {
1283 const auto &txid = tx->GetId();
1286 if (m_pool.exists(txid)) {
1300 auto iter = m_pool.GetIter(txid);
1301 assert(iter != std::nullopt);
1303 (*iter.value())->GetTxSize(),
1304 (*iter.value())->GetFee()));
1308 const auto single_package_res = AcceptSubPackage({tx}, args);
1309 const auto &single_res = single_package_res.m_tx_results.at(txid);
1310 if (single_res.m_result_type ==
1315 assert(m_pool.exists(txid));
1316 results_final.emplace(txid, single_res);
1317 }
else if (single_res.m_state.GetResult() !=
1319 single_res.m_state.GetResult() !=
1334 package_state_quit_early.
Invalid(
1336 individual_results_nonfinal.emplace(txid, single_res);
1338 individual_results_nonfinal.emplace(txid, single_res);
1339 txns_package_eval.push_back(tx);
1344 auto multi_submission_result =
1345 quit_early || txns_package_eval.empty()
1347 : AcceptSubPackage(txns_package_eval, args);
1349 multi_submission_result.m_state;
1354 m_pool.LimitSize(m_active_chainstate.CoinsTip());
1356 for (
const auto &tx : package) {
1357 const auto &txid = tx->GetId();
1358 if (multi_submission_result.m_tx_results.count(txid) > 0) {
1361 Assume(results_final.count(txid) == 0);
1365 const auto &txresult =
1366 multi_submission_result.m_tx_results.at(txid);
1367 if (txresult.m_result_type ==
1369 !m_pool.exists(txid)) {
1371 "transaction failed");
1375 results_final.emplace(
1378 results_final.emplace(txid, txresult);
1380 }
else if (
const auto final_it{results_final.find(txid)};
1381 final_it != results_final.end()) {
1384 Assume(final_it->second.m_result_type !=
1386 Assume(individual_results_nonfinal.count(txid) == 0);
1387 if (!m_pool.exists(tx->GetId())) {
1389 "transaction failed");
1394 results_final.erase(txid);
1395 results_final.emplace(
1398 }
else if (
const auto non_final_it{
1399 individual_results_nonfinal.find(txid)};
1400 non_final_it != individual_results_nonfinal.end()) {
1401 Assume(non_final_it->second.m_result_type ==
1404 results_final.emplace(txid, non_final_it->second);
1407 Assume(results_final.size() == package.size());
1409 std::move(results_final));
1415 int64_t accept_time,
bool bypass_limits,
1417 unsigned int heightOverride) {
1422 std::vector<COutPoint> coins_to_uncache;
1423 auto args = MemPoolAccept::ATMPArgs::SingleAccept(
1425 coins_to_uncache, test_accept, heightOverride);
1427 .AcceptSingleTransaction(tx, args);
1435 for (
const COutPoint &outpoint : coins_to_uncache) {
1452 assert(!package.empty());
1453 assert(std::all_of(package.cbegin(), package.cend(),
1454 [](
const auto &tx) { return tx != nullptr; }));
1458 std::vector<COutPoint> coins_to_uncache;
1462 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(
1463 config,
GetTime(), coins_to_uncache);
1464 return MemPoolAccept(pool, active_chainstate)
1465 .AcceptMultipleTransactions(package, args);
1467 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(
1468 config,
GetTime(), coins_to_uncache);
1469 return MemPoolAccept(pool, active_chainstate)
1470 .AcceptPackage(package, args);
1476 if (test_accept || result.m_state.IsInvalid()) {
1477 for (
const COutPoint &hashTx : coins_to_uncache) {
1490 if (halvings >= 64) {
1501 : m_dbview{
std::move(db_params),
std::move(options)},
1502 m_catcherview(&m_dbview) {}
1504void CoinsViews::InitCache() {
1506 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1511 std::optional<BlockHash> from_snapshot_blockhash)
1512 : m_mempool(mempool), m_blockman(blockman), m_chainman(chainman),
1513 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1519 if (!m_cached_snapshot_base) {
1520 m_cached_snapshot_base =
Assert(
1523 return m_cached_snapshot_base;
1527 bool should_wipe, std::string leveldb_name) {
1534 .cache_bytes = cache_size_bytes,
1535 .memory_only = in_memory,
1536 .wipe_data = should_wipe,
1542void Chainstate::InitCoinsCache(
size_t cache_size_bytes) {
1568 if (chain.Tip() ==
nullptr) {
1577 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
1604 std::string warning =
1605 std::string(
"'Warning: Large-work fork detected, forking after "
1612 LogPrintf(
"%s: Warning: Large fork found\n forking the "
1613 "chain at height %d (%s)\n lasting to height %d "
1614 "(%s).\nChain state database corruption likely.\n",
1621 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks "
1622 "longer than our best chain.\nChain state database "
1623 "corruption likely.\n",
1668 SetBlockFailureFlags(pindexNew);
1679 m_avalancheFinalizedBlockIndex = pindexNew->
pprev;
1682 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n",
1689 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n",
1701 pindex->nStatus = pindex->nStatus.withFailed();
1711 if (tx.IsCoinBase()) {
1715 txundo.
vprevout.reserve(tx.vin.size());
1716 for (
const CTxIn &txin : tx.vin) {
1731 std::vector<Coin> spent_coins;
1732 spent_coins.reserve(ptx->vin.size());
1733 for (
const CTxIn &input : ptx->vin) {
1735 const bool coinFound = coins_view.
GetCoin(input.prevout, coin);
1737 spent_coins.push_back(std::move(coin));
1743 const CScript &scriptSig =
ptxTo->vin[
nIn].scriptSig;
1745 auto debug_str =
strprintf(
"input %i of %s, spending %s:%i",
nIn,
1746 ptxTo->GetId().ToString(),
1747 ptxTo->vin[
nIn].prevout.GetTxId().ToString(),
1754 return std::make_pair(error, std::move(debug_str));
1764 std::move(debug_str));
1766 return std::nullopt;
1770 const size_t signature_cache_bytes)
1771 : m_signature_cache{signature_cache_bytes} {
1780 const auto [num_elems, approx_size_bytes] =
1782 LogPrintf(
"Using %zu MiB out of %zu MiB requested for script execution "
1783 "cache, able to store %zu elements\n",
1784 approx_size_bytes >> 20, script_execution_cache_bytes >> 20,
1790 bool sigCacheStore,
bool scriptCacheStore,
1795 std::vector<CScriptCheck> *pvChecks) {
1797 assert(!tx.IsCoinBase());
1800 pvChecks->reserve(tx.vin.size());
1811 elem, !scriptCacheStore);
1813 if (found_in_cache) {
1815 (pBlockLimitSigChecks &&
1818 "too-many-sigchecks");
1823 int nSigChecksTotal = 0;
1825 for (
size_t i = 0; i < tx.vin.size(); i++) {
1826 const COutPoint &prevout = tx.vin[i].prevout;
1839 sigCacheStore, txdata, &txLimitSigChecks, pBlockLimitSigChecks);
1843 pvChecks->push_back(std::move(check));
1847 if (
auto result = check(); result.has_value()) {
1852 uint32_t mandatoryFlags =
1853 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS;
1854 if (
flags != mandatoryFlags) {
1862 mandatoryFlags, sigCacheStore, txdata);
1863 auto mandatory_result = check2();
1864 if (!mandatory_result.has_value()) {
1867 strprintf(
"non-mandatory-script-verify-flag (%s)",
1878 result = mandatory_result;
1890 strprintf(
"mandatory-script-verify-flag-failed (%s)",
1898 nSigChecksOut = nSigChecksTotal;
1900 if (scriptCacheStore && !pvChecks) {
1911 const std::string &strMessage,
1913 notifications.
fatalError(strMessage, userMessage);
1914 return state.
Error(strMessage);
1919 const COutPoint &out) {
1927 if (undo.GetHeight() == 0) {
1950 view.
AddCoin(out, std::move(undo), !fClean);
1965 LogError(
"DisconnectBlock(): failure reading undo data\n");
1969 return ApplyBlockUndo(std::move(blockUndo), block, pindex, view);
1977 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
1978 LogError(
"DisconnectBlock(): block and undo data inconsistent\n");
1983 for (
size_t i = 1; i < block.
vtx.size(); i++) {
1984 const CTransaction &tx = *(block.
vtx[i]);
1986 if (txundo.
vprevout.size() != tx.vin.size()) {
1988 "DisconnectBlock(): transaction and undo data inconsistent\n");
1992 for (
size_t j = 0; j < tx.vin.size(); j++) {
1993 const COutPoint &out = tx.vin[j].prevout;
2005 for (
const auto &ptx : block.
vtx) {
2006 const CTransaction &tx = *ptx;
2007 const TxId &txid = tx.GetId();
2008 const bool is_coinbase = tx.IsCoinBase();
2012 for (
size_t o = 0; o < tx.vout.size(); o++) {
2013 if (tx.vout[o].scriptPubKey.IsUnspendable()) {
2017 COutPoint out(txid, o);
2019 bool is_spent = view.
SpendCoin(out, &coin);
2020 if (!is_spent || tx.vout[o] != coin.
GetTxOut() ||
2109 consensusparams, pindex,
2141 const auto time_start{SteadyClock::now()};
2158 if (!
CheckBlock(block, state, consensusParams,
2166 "Corrupt block found indicating potential "
2167 "hardware failure; shutting down");
2190 bool fScriptChecks =
true;
2199 BlockMap::const_iterator it{
2202 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2230 consensusParams) <= 60 * 60 * 24 * 7 * 2);
2235 const auto time_1{SteadyClock::now()};
2238 Ticks<MillisecondsDouble>(time_1 - time_start),
2253 bool fEnforceBIP30 = !((pindex->
nHeight == 91842 &&
2255 uint256S(
"0x00000000000a4d0a398161ffc163c503763"
2256 "b1f4360639393e0e4c8e300e0caec")) ||
2259 uint256S(
"0x00000000000743f190a18c5577a3c2d2a1f"
2260 "610ae9601ac046a38084ccb7cd721")));
2291 static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2328 (!pindexBIP34height ||
2334 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2335 for (
const auto &tx : block.
vtx) {
2336 for (
size_t o = 0; o < tx->vout.size(); o++) {
2337 if (view.
HaveCoin(COutPoint(tx->GetId(), o))) {
2340 "tried to overwrite transaction");
2347 int nLockTimeFlags = 0;
2355 const auto time_2{SteadyClock::now()};
2358 Ticks<MillisecondsDouble>(time_2 - time_1),
2362 std::vector<int> prevheights;
2374 std::vector<TxSigCheckLimiter> nSigChecksTxLimiters;
2375 nSigChecksTxLimiters.resize(block.
vtx.size() - 1);
2378 blockundo.
vtxundo.resize(block.
vtx.size() - 1);
2385 for (
const auto &ptx : block.
vtx) {
2388 }
catch (
const std::logic_error &e) {
2398 "tx-duplicate",
"tried to overwrite transaction");
2405 for (
const auto &ptx : block.
vtx) {
2406 const CTransaction &tx = *ptx;
2407 const bool isCoinBase = tx.IsCoinBase();
2408 nInputs += tx.vin.size();
2421 tx.GetId().ToString());
2429 "bad-txns-accumulated-fee-outofrange",
2430 "accumulated fee in the block out of range");
2442 prevheights.resize(tx.vin.size());
2443 for (
size_t j = 0; j < tx.vin.size(); j++) {
2447 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2449 "bad-txns-nonfinal",
2450 "contains a non-BIP68-final transaction " +
2451 tx.GetHash().ToString());
2457 bool fCacheResults = fJustCheck;
2460 if (!fEnforceSigCheck) {
2467 std::vector<CScriptCheck> vChecks;
2469 if (fScriptChecks &&
2473 nSigChecksTxLimiters[txIndex],
2474 &nSigChecksBlockLimiter, &vChecks)) {
2483 control.
Add(std::move(vChecks));
2493 const auto time_3{SteadyClock::now()};
2496 " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) "
2497 "[%.2fs (%.2fms/blk)]\n",
2498 (
unsigned)block.
vtx.size(),
2499 Ticks<MillisecondsDouble>(time_3 - time_2),
2500 Ticks<MillisecondsDouble>(time_3 - time_2) / block.
vtx.size(),
2503 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2507 const Amount blockReward =
2509 if (block.
vtx[0]->GetValueOut() > blockReward && state.
IsValid()) {
2512 strprintf(
"coinbase pays too much (actual=%d vs limit=%d)",
2513 block.
vtx[0]->GetValueOut(), blockReward));
2520 auto parallel_result = control.
Complete();
2521 if (parallel_result.has_value() && state.
IsValid()) {
2523 strprintf(
"mandatory-script-verify-flag-failed (%s)",
2525 parallel_result->second);
2531 const auto time_4{SteadyClock::now()};
2535 " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n",
2536 nInputs - 1, Ticks<MillisecondsDouble>(time_4 - time_2),
2539 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2547 if (!
m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2559 const auto time_5{SteadyClock::now()};
2562 Ticks<MillisecondsDouble>(time_5 - time_4),
2566 TRACE6(validation, block_connected, block_hash.data(), pindex->
nHeight,
2567 block.
vtx.size(), nInputs, nSigChecksRet,
2569 time_5 - time_start);
2582Chainstate::GetCoinsCacheSizeState(
size_t max_coins_cache_size_bytes,
2583 size_t max_mempool_size_bytes) {
2587 int64_t nTotalSpace =
2588 max_coins_cache_size_bytes +
2589 std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2592 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES =
2594 int64_t large_threshold = std::max(
2595 (9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2597 if (cacheSize > nTotalSpace) {
2598 LogPrintf(
"Cache size (%s) exceeds total space (%s)\n", cacheSize,
2601 }
else if (cacheSize > large_threshold) {
2611 std::set<int> setFilesToPrune;
2612 bool full_flush_completed =
false;
2619 bool fFlushForPrune =
false;
2631 std::optional<std::string> limiting_lock;
2633 for (
const auto &prune_lock :
m_blockman.m_prune_locks) {
2634 if (prune_lock.second.height_first ==
2635 std::numeric_limits<int>::max()) {
2640 const int lock_height{prune_lock.second.height_first -
2642 last_prune = std::max(1, std::min(last_prune, lock_height));
2643 if (last_prune == lock_height) {
2644 limiting_lock = prune_lock.first;
2648 if (limiting_lock) {
2650 limiting_lock.value(), last_prune);
2653 if (nManualPruneHeight > 0) {
2658 std::min(last_prune, nManualPruneHeight), *
this,
2667 if (!setFilesToPrune.empty()) {
2668 fFlushForPrune =
true;
2671 "prunedblockfiles",
true);
2687 bool fPeriodicWrite =
2691 fCacheLarge || fCacheCritical ||
2692 fPeriodicWrite || fFlushForPrune;
2698 "Disk space is too low!",
2699 _(
"Disk space is too low!"));
2713 "%s: Failed to flush block file.\n",
2726 "Failed to write to block index database");
2731 if (fFlushForPrune) {
2738 if (!
CoinsTip().GetBestBlock().IsNull()) {
2740 LogWarning(
"Flushing large (%d GiB) UTXO set to disk, "
2741 "it may take several minutes\n",
2742 coins_mem_usage >> 30);
2746 "write coins cache to disk (%d coins, %.2fKiB)",
2747 coins_count, coins_mem_usage >> 10),
2760 "Disk space is too low!",
2761 _(
"Disk space is too low!"));
2767 fCacheLarge || fCacheCritical};
2771 "Failed to write to coin database");
2773 full_flush_completed =
true;
2775 int64_t{Ticks<std::chrono::microseconds>(
2776 SteadyClock::now() - nNow)},
2777 uint32_t(mode), coins_count,
2778 uint64_t(coins_mem_usage), fFlushForPrune);
2782 if (should_write ||
m_next_write == NodeClock::time_point::max()) {
2790 if (full_flush_completed) {
2795 }
catch (
const std::runtime_error &e) {
2797 std::string(
"System error while flushing: ") +
2806 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
2815 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
2822 const std::string &func_name,
2823 const std::string &
prefix)
2832 "%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
2833 "date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
2842void Chainstate::UpdateTip(
const CBlockIndex *pindexNew) {
2844 const auto &coins_tip =
CoinsTip();
2853 constexpr int BACKGROUND_LOG_INTERVAL = 2000;
2854 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2856 "[background validation] ");
2872 UpdateTipLog(coins_tip, pindexNew, params, __func__,
"");
2899 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2902 LogError(
"DisconnectTip(): Failed to read block\n");
2907 const auto time_start{SteadyClock::now()};
2911 if (DisconnectBlock(block, pindexDelete, view) !=
2913 LogError(
"DisconnectTip(): DisconnectBlock %s failed\n",
2918 bool flushed = view.
Flush();
2922 Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
2927 const int max_height_first{pindexDelete->
nHeight - 1};
2928 for (
auto &prune_lock :
m_blockman.m_prune_locks) {
2929 if (prune_lock.second.height_first <= max_height_first) {
2933 prune_lock.second.height_first = max_height_first;
2935 prune_lock.first, max_height_first);
2948 if (pindexDelete->
pprev !=
nullptr &&
2952 "Disconnecting mempool due to rewind of upgrade block\n");
2953 if (disconnectpool) {
2959 if (disconnectpool) {
2966 UpdateTip(pindexDelete->
pprev);
2986 const std::shared_ptr<const CBlock> &pblock,
2999 const auto time_1{SteadyClock::now()};
3000 std::shared_ptr<const CBlock> pthisBlock;
3002 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3005 "Failed to read block");
3007 pthisBlock = pblockNew;
3009 pthisBlock = pblock;
3012 const CBlock &blockConnecting = *pthisBlock;
3015 const auto time_2{SteadyClock::now()};
3017 SteadyClock::time_point time_3;
3019 " - Load block from disk: %.2fms [%.2fs (%.2fms/blk)]\n",
3020 Ticks<MillisecondsDouble>(time_2 - time_1),
3027 bool rv =
ConnectBlock(blockConnecting, state, pindexNew, view,
3036 LogError(
"%s: ConnectBlock %s failed, %s\n", __func__,
3057 const Amount blockReward =
3061 std::vector<std::unique_ptr<ParkingPolicy>> parkingPolicies;
3062 parkingPolicies.emplace_back(std::make_unique<MinerFundPolicy>(
3063 consensusParams, *pindexNew, blockConnecting, blockReward));
3079 parkingPolicies.emplace_back(
3080 std::make_unique<RTTPolicy>(consensusParams,
3085 parkingPolicies.emplace_back(
3086 std::make_unique<StakingRewardsPolicy>(
3087 *
avalanche, consensusParams, *pindexNew,
3088 blockConnecting, blockReward));
3091 parkingPolicies.emplace_back(
3092 std::make_unique<PreConsensusPolicy>(
3093 *
avalanche, *pindexNew, blockConnecting,
3099 if (std::find_if_not(parkingPolicies.begin(), parkingPolicies.end(),
3100 [&](
const auto &policy) {
3101 bool ret = (*policy)(blockPolicyState);
3104 "Park block because it "
3105 "violated a block policy: %s\n",
3106 blockPolicyState.ToString());
3109 }) != parkingPolicies.end()) {
3110 pindexNew->nStatus = pindexNew->nStatus.withParked();
3116 time_3 = SteadyClock::now();
3120 BCLog::BENCH,
" - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3121 Ticks<MillisecondsDouble>(time_3 - time_2),
3124 bool flushed = view.
Flush();
3128 const auto time_4{SteadyClock::now()};
3131 Ticks<MillisecondsDouble>(time_4 - time_3),
3138 const auto time_5{SteadyClock::now()};
3141 " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3142 Ticks<MillisecondsDouble>(time_5 - time_4),
3147 disconnectpool.removeForBlock(blockConnecting.vtx, *m_mempool);
3152 if (pindexNew->
pprev !=
nullptr &&
3157 "Disconnecting mempool due to acceptance of upgrade block\n");
3158 disconnectpool.importMempool(*m_mempool);
3163 m_chain.SetTip(*pindexNew);
3164 UpdateTip(pindexNew);
3166 const auto time_6{SteadyClock::now()};
3170 " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3171 Ticks<MillisecondsDouble>(time_6 - time_5),
3175 Ticks<MillisecondsDouble>(time_6 - time_1),
3182 if (
this != &m_chainman.ActiveChainstate()) {
3186 m_chainman.MaybeCompleteSnapshotValidation();
3198 std::vector<const CBlockIndex *> &blocksToReconcile,
bool fAutoUnpark) {
3205 std::set<CBlockIndex *, CBlockIndexWorkComparator>::reverse_iterator
3217 if (m_avalancheFinalizedBlockIndex &&
3219 LogPrintf(
"Park block %s because it forks prior to the "
3220 "avalanche finalized chaintip.\n",
3222 pindexNew->nStatus = pindexNew->nStatus.withParked();
3233 bool hasValidAncestor =
true;
3234 while (hasValidAncestor && pindexTest && pindexTest != pindexFork) {
3239 bool fParkedChain = pindexTest->nStatus.isOnParkedChain();
3240 if (fAutoUnpark && fParkedChain) {
3246 if (!pindexTip || !pindexFork) {
3260 pindexExtraPow = pindexExtraPow->
pprev;
3265 requiredWork += (deltaWork >> 1);
3276 LogPrintf(
"Unpark chain up to block %s as it has "
3277 "accumulated enough PoW.\n",
3279 fParkedChain =
false;
3288 bool fInvalidChain = pindexTest->nStatus.isInvalid();
3289 bool fMissingData = !pindexTest->nStatus.hasData();
3290 if (!(fInvalidChain || fParkedChain || fMissingData)) {
3293 pindexTest = pindexTest->
pprev;
3299 hasValidAncestor =
false;
3302 if (fInvalidChain && (
m_chainman.m_best_invalid ==
nullptr ||
3308 if (fParkedChain && (
m_chainman.m_best_parked ==
nullptr ||
3314 LogPrintf(
"Considered switching to better tip %s but that chain "
3315 "contains a%s%s%s block.\n",
3317 fInvalidChain ?
"n invalid" :
"",
3318 fParkedChain ?
" parked" :
"",
3319 fMissingData ?
" missing-data" :
"");
3323 while (pindexTest != pindexFailed) {
3324 if (fInvalidChain || fParkedChain) {
3325 pindexFailed->nStatus =
3326 pindexFailed->nStatus.withFailedParent(fInvalidChain)
3327 .withParkedParent(fParkedChain);
3328 }
else if (fMissingData) {
3334 std::make_pair(pindexFailed->
pprev, pindexFailed));
3337 pindexFailed = pindexFailed->
pprev;
3340 if (fInvalidChain || fParkedChain) {
3347 blocksToReconcile.push_back(pindexNew);
3350 if (hasValidAncestor) {
3383 const std::shared_ptr<const CBlock> &pblock,
bool &fInvalidFound,
3395 bool fBlocksDisconnected =
false;
3398 if (
m_mempool && !fBlocksDisconnected) {
3417 "Failed to disconnect block; see debug.log for details");
3421 fBlocksDisconnected =
true;
3425 std::vector<CBlockIndex *> vpindexToConnect;
3426 bool fContinue =
true;
3431 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3432 vpindexToConnect.clear();
3433 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3436 vpindexToConnect.push_back(pindexIter);
3437 pindexIter = pindexIter->
pprev;
3445 if (!
ConnectTip(state, blockPolicyState, pindexConnect,
3446 pindexConnect == pindexMostWork
3448 : std::shared_ptr<const CBlock>(),
3449 disconnectpool,
avalanche, chainstate_role)) {
3457 fInvalidFound =
true;
3478 if (!pindexOldTip ||
3490 if (fBlocksDisconnected || !disconnectpool.
isEmpty()) {
3496 "Updating mempool due to reorganization or "
3497 "rules upgrade/downgrade\n");
3505 if (fInvalidFound) {
3526 bool fNotify =
false;
3527 bool fInitialBlockDownload =
false;
3532 pindexHeader = chainman.m_best_header;
3534 if (pindexHeader != pindexHeaderOld) {
3536 fInitialBlockDownload = chainman.IsInitialBlockDownload();
3537 pindexHeaderOld = pindexHeader;
3543 chainman.GetNotifications().headerTip(
3559 std::shared_ptr<const CBlock> pblock,
3580 LogPrintf(
"m_disabled is set - this chainstate should not be in "
3581 "operation. Please report this as a bug. %s\n",
3588 bool exited_ibd{
false};
3598 std::vector<const CBlockIndex *> blocksToReconcile;
3599 bool blocks_connected =
false;
3613 if (pindexMostWork ==
nullptr) {
3620 if (pindexMostWork ==
nullptr ||
3625 bool fInvalidFound =
false;
3626 std::shared_ptr<const CBlock> nullBlockPtr;
3633 state, pindexMostWork,
3634 pblock && pblock->GetHash() ==
3638 fInvalidFound,
avalanche, chainstate_role)) {
3642 blocks_connected =
true;
3644 if (fInvalidFound ||
3645 (pindexMostWork && pindexMostWork->nStatus.isParked())) {
3647 pindexMostWork =
nullptr;
3672 if (blocks_connected) {
3676 if (was_in_ibd && !still_in_ibd) {
3685 pindexFork != pindexNewTip) {
3701 return m_avalancheFinalizedBlockIndex);
3702 for (
const CBlockIndex *pindex : blocksToReconcile) {
3708 if (blocks_connected) {
3710 while (pindexTest && pindexTest != pfinalized) {
3715 avalanche->computeStakingReward(pindexTest);
3716 pindexTest = pindexTest->
pprev;
3722 if (!blocks_connected) {
3764 }
while (pindexNewTip != pindexMostWork);
3795 std::numeric_limits<int32_t>::min()) {
3817template <
typename Func>
struct Defer {
3819 Defer(
Func &&f) : func(
std::move(f)) {}
3820 ~Defer() { func(); }
3833 bool pindex_was_in_chain =
false;
3834 int disconnected = 0;
3850 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3854 for (
auto &entry :
m_blockman.m_block_index) {
3865 candidate_blocks_by_work.insert(
3866 std::make_pair(candidate->
nChainWork, candidate));
3878 constexpr int maxDisconnectPoolBlocks = 10;
3930 pindex_was_in_chain =
true;
3939 if (optDisconnectPool && disconnected > maxDisconnectPoolBlocks) {
3944 optDisconnectPool =
nullptr;
3960 invalid_walk_tip->nStatus =
3961 invalidate ? invalid_walk_tip->nStatus.withFailed()
3962 : invalid_walk_tip->nStatus.withParked();
3967 if (invalid_walk_tip == to_mark_failed_or_parked->
pprev &&
3968 (invalidate ? to_mark_failed_or_parked->nStatus.hasFailed()
3969 : to_mark_failed_or_parked->nStatus.isParked())) {
3973 to_mark_failed_or_parked->nStatus =
3975 ? to_mark_failed_or_parked->nStatus.withFailed(
false)
3977 : to_mark_failed_or_parked->nStatus.withParked(
false)
3978 .withParkedParent());
3984 auto candidate_it = candidate_blocks_by_work.lower_bound(
3986 while (candidate_it != candidate_blocks_by_work.end()) {
3988 invalid_walk_tip->
pprev)) {
3990 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3999 to_mark_failed_or_parked = invalid_walk_tip;
4015 to_mark_failed_or_parked->nStatus =
4016 invalidate ? to_mark_failed_or_parked->nStatus.withFailed()
4017 : to_mark_failed_or_parked->nStatus.withParked();
4030 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
4032 block_index.HaveNumChainTxs() &&
4045 if (pindex_was_in_chain) {
4048 *to_mark_failed_or_parked->
pprev);
4072template <
typename F>
4076 if (pindex->nStatus != newStatus &&
4079 pindex->nStatus = newStatus;
4095template <
typename F,
typename C,
typename AC>
4097 F f, C fChild, AC fAncestorWasChanged) {
4103 for (
auto pindexAncestor = pindex; pindexAncestor !=
nullptr;
4104 pindexAncestor = pindexAncestor->
pprev) {
4106 pindexDeepestChanged = pindexAncestor;
4112 pindexDeepestChanged) {
4114 pindexReset =
nullptr;
4118 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
4121 fAncestorWasChanged);
4125void Chainstate::SetBlockFailureFlags(
CBlockIndex *invalid_block) {
4128 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
4129 if (block_index.GetAncestor(invalid_block->
nHeight) == invalid_block &&
4130 !block_index.nStatus.isInvalid()) {
4131 block_index.nStatus = block_index.nStatus.withFailedParent();
4142 return status.withClearedFailureFlags();
4145 return status.withClearedFailureFlags();
4148 return status.withFailedParent(false);
4162 if (is_active_chainstate) {
4166 }
else if (!m_disabled) {
4172 if (snapshot_base->GetAncestor(pindex->
nHeight) == pindex) {
4184 return status.withClearedParkedFlags();
4187 return fClearChildren ? status.withClearedParkedFlags()
4188 : status.withParkedParent(false);
4191 return status.withParkedParent(false);
4203bool Chainstate::AvalancheFinalizeBlock(
CBlockIndex *pindex,
4214 "The block to mark finalized by avalanche is not on the "
4215 "active chain: %s\n",
4226 m_avalancheFinalizedBlockIndex = pindex;
4236 m_avalancheFinalizedBlockIndex =
nullptr;
4241 return pindex && m_avalancheFinalizedBlockIndex &&
4253 pindexNew->
nTx = block.
vtx.size();
4260 return block.nTx + (block.pprev ? block.pprev->nChainTx : 0);
4263 pindexNew->
nChainTx == prev_tx_sum(*pindexNew) ||
4264 pindexNew == GetSnapshotBaseBlock())) {
4265 LogPrintf(
"Internal bug detected: block %d has unexpected nChainTx %i "
4266 "that should be %i. Please report this issue here: %s\n",
4268 prev_tx_sum(*pindexNew), PACKAGE_BUGREPORT);
4272 pindexNew->nFile = pos.
nFile;
4273 pindexNew->nDataPos = pos.
nPos;
4274 pindexNew->nUndoPos = 0;
4275 pindexNew->nStatus = pindexNew->nStatus.withData();
4282 std::deque<CBlockIndex *> queue;
4283 queue.push_back(pindexNew);
4287 while (!queue.empty()) {
4295 pindex->
nChainTx == prev_tx_sum(*pindex))) {
4297 "Internal bug detected: block %d has unexpected nChainTx "
4298 "%i that should be %i. Please report this issue here: %s\n",
4302 pindex->
nChainTx = prev_tx_sum(*pindex);
4312 c->TryAddBlockIndexCandidate(pindex);
4315 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4316 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4318 while (range.first != range.second) {
4319 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4321 queue.push_back(it->second);
4326 }
else if (pindexNew->
pprev &&
4329 std::make_pair(pindexNew->
pprev, pindexNew));
4349 "high-hash",
"proof of work failed");
4366 "hashMerkleRoot mismatch");
4375 "bad-txns-duplicate",
4376 "duplicate transaction");
4408 if (block.
vtx.empty()) {
4410 "bad-cb-missing",
"first tx is not coinbase");
4419 "bad-blk-length",
"size limits failed");
4423 if (currentBlockSize > nMaxBlockSize) {
4425 "bad-blk-length",
"size limits failed");
4433 strprintf(
"Coinbase check failed (txid %s) %s",
4434 block.
vtx[0]->GetId().ToString(),
4440 for (
size_t i = 1; i < block.
vtx.size(); i++) {
4441 auto *tx = block.
vtx[i].get();
4446 strprintf(
"Transaction check failed (txid %s) %s",
4461 return std::all_of(headers.cbegin(), headers.cend(),
4462 [&](
const auto &header) {
4463 return CheckProofOfWork(
4464 header.GetHash(), header.nBits, consensusParams);
4472 "Block mutated: %s\n", state.
ToString());
4476 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase()) {
4484 return std::any_of(block.
vtx.begin(), block.
vtx.end(), [](
auto &tx) {
4485 return GetSerializeSize(tx, PROTOCOL_VERSION) == 64;
4519 const std::optional<CCheckpointData> &test_checkpoints = std::nullopt)
4522 assert(pindexPrev !=
nullptr);
4523 const int nHeight = pindexPrev->nHeight + 1;
4529 LogPrintf(
"bad bits after height: %d\n", pindexPrev->nHeight);
4531 "bad-diffbits",
"incorrect proof of work");
4535 if (chainman.m_options.checkpoints_enabled) {
4537 test_checkpoints ? test_checkpoints.value() : params.
Checkpoints();
4543 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4546 "checkpoint mismatch");
4554 blockman.GetLastCheckpoint(checkpoints);
4555 if (pcheckpoint && nHeight < pcheckpoint->
nHeight) {
4557 "ERROR: %s: forked chain older than last checkpoint "
4561 "bad-fork-prior-to-checkpoint");
4566 if (block.
GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
4568 "time-too-old",
"block's timestamp is too early");
4572 if (block.
Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4575 "block timestamp too far in the future");
4608 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
4611 bool enforce_locktime_median_time_past{
false};
4614 assert(pindexPrev !=
nullptr);
4615 enforce_locktime_median_time_past =
true;
4618 const int64_t nMedianTimePast =
4621 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past
4626 const bool fIsMagneticAnomalyEnabled =
4633 const CTransaction *prevTx =
nullptr;
4634 for (
const auto &ptx : block.
vtx) {
4635 const CTransaction &tx = *ptx;
4636 if (fIsMagneticAnomalyEnabled) {
4637 if (prevTx && (tx.GetId() <= prevTx->GetId())) {
4638 if (tx.GetId() == prevTx->GetId()) {
4642 tx.GetId().ToString()));
4647 strprintf(
"Transaction order is invalid (%s < %s)",
4648 tx.GetId().ToString(),
4649 prevTx->GetId().ToString()));
4652 if (prevTx || !tx.IsCoinBase()) {
4670 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4672 block.
vtx[0]->vin[0].scriptSig.begin())) {
4675 "block height mismatch in coinbase");
4690 const std::optional<CCheckpointData> &test_checkpoints) {
4697 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4699 if (miSelf !=
m_blockman.m_block_index.end()) {
4706 if (pindex->nStatus.isInvalid()) {
4719 "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__,
4725 BlockMap::iterator mi{
4729 "header %s has prev block not found: %s\n",
4732 "prev-blk-not-found");
4737 if (pindexPrev->nStatus.isInvalid()) {
4739 "header %s has prev block invalid: %s\n", hash.
ToString(),
4749 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4780 if (pindexPrev->
GetAncestor(failedit->nHeight) == failedit) {
4781 assert(failedit->nStatus.hasFailed());
4783 while (invalid_walk != failedit) {
4784 invalid_walk->nStatus =
4785 invalid_walk->nStatus.withFailedParent();
4787 invalid_walk = invalid_walk->
pprev;
4790 "header %s has prev block invalid: %s\n",
4799 if (!min_pow_checked) {
4801 "%s: not adding new block header %s, missing anti-dos "
4802 "proof-of-work validation\n",
4805 "too-little-chainwork");
4821 const auto msg =
strprintf(
"Saw new header hash=%s height=%d",
4835 const std::vector<CBlockHeader> &headers,
bool min_pow_checked,
4837 const std::optional<CCheckpointData> &test_checkpoints) {
4845 header, state, &pindex, min_pow_checked, test_checkpoints);
4861 const int64_t blocks_left{
4862 (
GetTime() - last_accepted.GetBlockTime()) /
4864 const double progress{100.0 * last_accepted.nHeight /
4865 (last_accepted.nHeight + blocks_left)};
4866 LogPrintf(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4867 last_accepted.nHeight, progress);
4875 int64_t timestamp) {
4884 if (m_best_header->nChainWork >=
4890 auto now = Now<SteadyMilliseconds>();
4891 if (now < m_last_presync_update + 250ms) {
4894 m_last_presync_update = now;
4898 height, timestamp,
true);
4899 if (initial_download) {
4900 const int64_t blocks_left{(
GetTime() - timestamp) /
4902 const double progress{100.0 * height / (height + blocks_left)};
4903 LogPrintf(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4911 bool *fNewBlock,
bool min_pow_checked) {
4914 const CBlock &block = *pblock;
4921 bool accepted_header{
4925 if (!accepted_header) {
4933 bool fAlreadyHave = pindex->nStatus.hasData();
4945 int64_t chainTipTimeDiff =
4951 LogPrintf(
"Chain tip timestamp-to-received-time difference: hash=%s, "
4954 LogPrintf(
"New block timestamp-to-received-time difference: hash=%s, "
4959 bool fHasMoreOrSameWork =
4967 bool fTooFarAhead{pindex->
nHeight >
4980 if (pindex->
nTx != 0) {
4985 if (!fHasMoreOrSameWork) {
5009 pindex->nStatus = pindex->nStatus.withFailed();
5028 std::optional<int> snapshot_base_height = GetSnapshotBaseHeight();
5029 const bool is_background_block =
5031 pindex->
nHeight <= snapshot_base_height;
5033 if (!is_background_block && pindexFork &&
5035 LogPrintf(
"Park block %s as it would cause a deep reorg.\n",
5037 pindex->nStatus = pindex->nStatus.withParked();
5060 if (blockPos.IsNull()) {
5062 "%s: Failed to find position to write new block to disk",
5068 }
catch (
const std::runtime_error &e) {
5070 std::string(
"System error: ") + e.what());
5088 const std::shared_ptr<const CBlock> &block,
bool force_processing,
5089 bool min_pow_checked,
bool *new_block,
5118 ret =
AcceptBlock(block, state, force_processing,
nullptr,
5119 new_block, min_pow_checked);
5124 LogError(
"%s: AcceptBlock FAILED (%s)\n", __func__,
5135 LogError(
"%s: ActivateBestChain failed (%s)\n", __func__,
5141 ? m_ibd_chainstate.get()
5144 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
5145 LogError(
"%s: [background] ActivateBestChain failed (%s)\n", __func__,
5164 false, test_accept);
5180 indexDummy.
pprev = pindexPrev;
5187 adjusted_time_callback())) {
5188 LogError(
"%s: Consensus::ContextualCheckBlockHeader: %s\n", __func__,
5200 LogError(
"%s: Consensus::ContextualCheckBlock: %s\n", __func__,
5205 if (!chainstate.
ConnectBlock(block, state, &indexDummy, viewNew,
5206 validationOptions,
nullptr,
true)) {
5216 int nManualPruneHeight) {
5219 nManualPruneHeight)) {
5220 LogPrintf(
"%s: failed to flush state (%s)\n", __func__,
5226 FopenFn mockable_fopen_function) {
5256 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5287 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
5291 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5292 LogPrintf(
"Verifying last %i blocks at level %i\n", nCheckDepth,
5298 int nGoodTransactions = 0;
5301 bool skipped_no_block_data{
false};
5302 bool skipped_l3_checks{
false};
5303 LogPrintf(
"Verification progress: 0%%\n");
5308 pindex = pindex->
pprev) {
5309 const int percentageDone = std::max(
5310 1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() -
5312 (
double)nCheckDepth *
5313 (nCheckLevel >= 4 ? 50 : 100))));
5314 if (reportDone < percentageDone / 10) {
5316 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
5317 reportDone = percentageDone / 10;
5326 !pindex->nStatus.hasData()) {
5329 LogPrintf(
"VerifyDB(): block verification stopping at height %d "
5330 "(no data). This could be due to pruning or use of an "
5331 "assumeutxo snapshot.\n",
5333 skipped_no_block_data =
true;
5342 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5348 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensusParams,
5351 "Verification error: found bad block at %d, hash=%s (%s)\n",
5358 if (nCheckLevel >= 2 && pindex) {
5362 LogPrintf(
"Verification error: found bad undo data at %d, "
5375 if (nCheckLevel >= 3) {
5379 chainstate.DisconnectBlock(block, pindex, coins);
5381 LogPrintf(
"Verification error: irrecoverable inconsistency "
5382 "in block data at %d, hash=%s\n",
5388 nGoodTransactions = 0;
5389 pindexFailure = pindex;
5391 nGoodTransactions += block.
vtx.size();
5394 skipped_l3_checks =
true;
5403 if (pindexFailure) {
5404 LogPrintf(
"Verification error: coin database inconsistencies found "
5405 "(last %i blocks, %i good transactions before that)\n",
5410 if (skipped_l3_checks) {
5411 LogPrintf(
"Skipped verification of level >=3 (insufficient database "
5412 "cache size). Consider increasing -dbcache.\n");
5419 if (nCheckLevel >= 4 && !skipped_l3_checks) {
5421 const int percentageDone = std::max(
5422 1, std::min(99, 100 -
int(
double(chainstate.
m_chain.
Height() -
5424 double(nCheckDepth) * 50)));
5425 if (reportDone < percentageDone / 10) {
5427 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
5428 reportDone = percentageDone / 10;
5435 LogPrintf(
"Verification error: ReadBlockFromDisk failed at %d, "
5440 if (!chainstate.
ConnectBlock(block, state, pindex, coins,
5442 LogPrintf(
"Verification error: found unconnectable block at "
5443 "%d, hash=%s (%s)\n",
5454 LogPrintf(
"Verification: No coin database inconsistencies in last %i "
5455 "blocks (%i transactions)\n",
5456 block_count, nGoodTransactions);
5458 if (skipped_l3_checks) {
5461 if (skipped_no_block_data) {
5477 LogError(
"ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s\n",
5488 if (tx->IsCoinBase()) {
5492 for (
const CTxIn &txin : tx->vin) {
5506 std::vector<BlockHash> hashHeads =
db.GetHeadBlocks();
5507 if (hashHeads.empty()) {
5511 if (hashHeads.size() != 2) {
5512 LogError(
"ReplayBlocks(): unknown inconsistent state\n");
5526 if (
m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5527 LogError(
"ReplayBlocks(): reorganization to unknown block requested\n");
5531 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
5533 if (!hashHeads[1].IsNull()) {
5535 if (
m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5536 LogError(
"ReplayBlocks(): reorganization from unknown block "
5541 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
5543 assert(pindexFork !=
nullptr);
5547 while (pindexOld != pindexFork) {
5552 LogError(
"RollbackBlock(): ReadBlockFromDisk() failed at "
5564 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n",
5577 pindexOld = pindexOld->
pprev;
5581 int nForkHeight = pindexFork ? pindexFork->
nHeight : 0;
5588 _(
"Replaying blocks…"),
5589 (
int)((
nHeight - nForkHeight) * 100.0 /
5590 (pindexNew->
nHeight - nForkHeight)),
5605void Chainstate::ClearBlockIndexCandidates() {
5621 const uint64_t numHeaders{20};
5624 const fs::path filePathTmp = filePath +
".new";
5635 bool missingIndex{
false};
5636 for (uint64_t i = 0; i < numHeaders; i++) {
5638 LogPrintf(
"Missing block index, stopping the headers time "
5639 "dumping after %d blocks.\n",
5641 missingIndex =
true;
5648 index = index->
pprev;
5652 throw std::runtime_error(
strprintf(
"Failed to commit to file %s",
5658 fs::remove(filePathTmp);
5663 throw std::runtime_error(
strprintf(
"Rename failed from %s to %s",
5667 }
catch (
const std::exception &e) {
5668 LogPrintf(
"Failed to dump the headers time: %s.\n", e.what());
5672 LogPrintf(
"Successfully dumped the last %d headers time to %s.\n",
5688 LogPrintf(
"Failed to open header times from disk, skipping.\n");
5697 LogPrintf(
"Unsupported header times file version, skipping.\n");
5704 for (uint64_t i = 0; i < numBlocks; i++) {
5706 int64_t receiveTime;
5709 file >> receiveTime;
5713 LogPrintf(
"Missing index for block %s, stopping the headers "
5714 "time loading after %d blocks.\n",
5721 }
catch (
const std::exception &e) {
5722 LogPrintf(
"Failed to read the headers time file data on disk: %s.\n",
5740 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5742 std::vector<CBlockIndex *> vSortedByHeight{
5744 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5756 if (pindex == GetSnapshotBaseBlock() ||
5764 if (pindex->nStatus.isInvalid() &&
5766 pindex->
nChainWork > m_best_invalid->nChainWork)) {
5767 m_best_invalid = pindex;
5770 if (pindex->nStatus.isOnParkedChain() &&
5772 pindex->
nChainWork > m_best_parked->nChainWork)) {
5773 m_best_parked = pindex;
5777 (m_best_header ==
nullptr ||
5779 m_best_header = pindex;
5783 needs_init =
m_blockman.m_block_index.empty();
5793 LogPrintf(
"Initializing databases...\n");
5814 if (blockPos.IsNull()) {
5815 LogError(
"%s: writing genesis block to disk failed\n", __func__);
5821 }
catch (
const std::runtime_error &e) {
5822 LogError(
"%s: failed to write genesis block: %s\n", __func__, e.what());
5831 std::multimap<BlockHash, FlatFilePos> *blocks_with_unknown_parent,
5834 assert(!dbp == !blocks_with_unknown_parent);
5848 uint64_t nRewind = blkdat.
GetPos();
5849 while (!blkdat.
eof()) {
5859 unsigned int nSize = 0;
5864 nRewind = blkdat.
GetPos() + 1;
5866 if (memcmp(buf, params.
DiskMagic().data(),
5876 }
catch (
const std::exception &) {
5884 const uint64_t nBlockPos{blkdat.
GetPos()};
5886 dbp->
nPos = nBlockPos;
5888 blkdat.
SetLimit(nBlockPos + nSize);
5896 nRewind = nBlockPos + nSize;
5901 std::shared_ptr<CBlock> pblock{};
5910 "%s: Out of order block %s, parent %s not known\n",
5911 __func__, hash.ToString(),
5913 if (dbp && blocks_with_unknown_parent) {
5914 blocks_with_unknown_parent->emplace(
5923 if (!pindex || !pindex->nStatus.hasData()) {
5926 blkdat.
SetPos(nBlockPos);
5927 pblock = std::make_shared<CBlock>();
5929 nRewind = blkdat.
GetPos();
5932 if (
AcceptBlock(pblock, state,
true, dbp,
nullptr,
5940 pindex->
nHeight % 1000 == 0) {
5943 "Block Import: already had block %s at height %d\n",
5944 hash.ToString(), pindex->
nHeight);
5951 bool genesis_activation_failure =
false;
5952 for (
auto c :
GetAll()) {
5954 if (!c->ActivateBestChain(state,
nullptr,
avalanche)) {
5955 genesis_activation_failure =
true;
5959 if (genesis_activation_failure) {
5973 bool activation_failure =
false;
5974 for (
auto c :
GetAll()) {
5976 if (!c->ActivateBestChain(state, pblock,
avalanche)) {
5978 "failed to activate chain (%s)\n",
5980 activation_failure =
true;
5984 if (activation_failure) {
5991 if (!blocks_with_unknown_parent) {
5997 std::deque<BlockHash> queue;
5998 queue.push_back(hash);
5999 while (!queue.empty()) {
6002 auto range = blocks_with_unknown_parent->equal_range(head);
6003 while (range.first != range.second) {
6004 std::multimap<BlockHash, FlatFilePos>::iterator it =
6006 std::shared_ptr<CBlock> pblockrecursive =
6007 std::make_shared<CBlock>();
6012 "%s: Processing out of order child %s of %s\n",
6013 __func__, pblockrecursive->GetHash().ToString(),
6018 &it->second,
nullptr,
true)) {
6020 queue.push_back(pblockrecursive->GetHash());
6024 blocks_with_unknown_parent->erase(it);
6028 }
catch (
const std::exception &e) {
6049 "%s: unexpected data at file offset 0x%x - %s. "
6051 __func__, (nRewind - 1), e.what());
6054 }
catch (
const std::runtime_error &e) {
6058 LogPrintf(
"Loaded %i blocks from external file in %dms\n", nLoaded,
6079 std::multimap<CBlockIndex *, CBlockIndex *> forward;
6080 for (
auto &[
_, block_index] :
m_blockman.m_block_index) {
6081 forward.emplace(block_index.pprev, &block_index);
6086 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6087 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6088 rangeGenesis = forward.equal_range(
nullptr);
6090 rangeGenesis.first++;
6092 assert(rangeGenesis.first == rangeGenesis.second);
6114 CBlockIndex *pindexFirstNotTransactionsValid =
nullptr;
6120 CBlockIndex *pindexFirstNotScriptsValid =
nullptr;
6126 const CBlockIndex *snap_base{GetSnapshotBaseBlock()};
6127 CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{},
6128 *snap_first_nocv{}, *snap_first_nosv{};
6129 auto snap_update_firsts = [&] {
6130 if (pindex == snap_base) {
6131 std::swap(snap_first_missing, pindexFirstMissing);
6132 std::swap(snap_first_notx, pindexFirstNeverProcessed);
6133 std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
6134 std::swap(snap_first_nocv, pindexFirstNotChainValid);
6135 std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
6139 while (pindex !=
nullptr) {
6141 if (pindexFirstInvalid ==
nullptr && pindex->nStatus.hasFailed()) {
6142 pindexFirstInvalid = pindex;
6144 if (pindexFirstParked ==
nullptr && pindex->nStatus.isParked()) {
6145 pindexFirstParked = pindex;
6147 if (pindexFirstMissing ==
nullptr && !pindex->nStatus.hasData()) {
6148 pindexFirstMissing = pindex;
6150 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) {
6151 pindexFirstNeverProcessed = pindex;
6153 if (pindex->
pprev !=
nullptr && pindexFirstNotTreeValid ==
nullptr &&
6155 pindexFirstNotTreeValid = pindex;
6157 if (pindex->
pprev !=
nullptr) {
6158 if (pindexFirstNotTransactionsValid ==
nullptr &&
6160 pindexFirstNotTransactionsValid = pindex;
6162 if (pindexFirstNotChainValid ==
nullptr &&
6164 pindexFirstNotChainValid = pindex;
6166 if (pindexFirstNotScriptsValid ==
nullptr &&
6168 pindexFirstNotScriptsValid = pindex;
6173 if (pindex->
pprev ==
nullptr) {
6177 for (
auto c :
GetAll()) {
6178 if (c->m_chain.Genesis() !=
nullptr) {
6180 assert(pindex == c->m_chain.Genesis());
6195 assert(pindex->nStatus.hasData() == (pindex->
nTx > 0));
6196 assert(pindexFirstMissing == pindexFirstNeverProcessed);
6197 }
else if (pindex->nStatus.hasData()) {
6202 if (pindex->nStatus.hasUndo()) {
6203 assert(pindex->nStatus.hasData());
6205 if (snap_base && snap_base->GetAncestor(pindex->
nHeight) == pindex) {
6217 assert((pindexFirstNeverProcessed ==
nullptr || pindex == snap_base) ==
6219 assert((pindexFirstNotTransactionsValid ==
nullptr ||
6231 assert(pindexFirstNotTreeValid ==
nullptr);
6234 assert(pindexFirstNotTreeValid ==
nullptr);
6238 assert(pindexFirstNotChainValid ==
nullptr);
6242 assert(pindexFirstNotScriptsValid ==
nullptr);
6244 if (pindexFirstInvalid ==
nullptr) {
6247 assert(!pindex->nStatus.isInvalid());
6249 if (pindexFirstParked ==
nullptr) {
6253 assert(!pindex->nStatus.isOnParkedChain());
6256 if (!pindex->
pprev) {
6270 for (
auto c :
GetAll()) {
6271 if (c->m_chain.Tip() ==
nullptr) {
6288 (pindexFirstNeverProcessed ==
nullptr || pindex == snap_base)) {
6292 if (pindexFirstInvalid ==
nullptr) {
6299 GetSnapshotBaseBlock()->GetAncestor(pindex->
nHeight) ==
6313 if (pindexFirstMissing ==
nullptr) {
6314 assert(pindex->nStatus.isOnParkedChain() ||
6315 c->setBlockIndexCandidates.count(pindex));
6323 if (pindex == c->m_chain.Tip() ||
6324 pindex == c->SnapshotBase()) {
6325 assert(c->setBlockIndexCandidates.count(pindex));
6337 assert(c->setBlockIndexCandidates.count(pindex) == 0);
6341 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6342 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6345 bool foundInUnlinked =
false;
6346 while (rangeUnlinked.first != rangeUnlinked.second) {
6347 assert(rangeUnlinked.first->first == pindex->
pprev);
6348 if (rangeUnlinked.first->second == pindex) {
6349 foundInUnlinked =
true;
6352 rangeUnlinked.first++;
6354 if (pindex->
pprev && pindex->nStatus.hasData() &&
6355 pindexFirstNeverProcessed !=
nullptr &&
6356 pindexFirstInvalid ==
nullptr) {
6362 if (!pindex->nStatus.hasData()) {
6364 assert(!foundInUnlinked);
6366 if (pindexFirstMissing ==
nullptr) {
6369 assert(!foundInUnlinked);
6371 if (pindex->
pprev && pindex->nStatus.hasData() &&
6372 pindexFirstNeverProcessed ==
nullptr &&
6373 pindexFirstMissing !=
nullptr) {
6386 for (
auto c :
GetAll()) {
6389 c->setBlockIndexCandidates.count(pindex) == 0) {
6390 if (pindexFirstInvalid ==
nullptr) {
6392 snap_base->GetAncestor(pindex->
nHeight) == pindex) {
6404 snap_update_firsts();
6405 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6406 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6407 range = forward.equal_range(pindex);
6408 if (range.first != range.second) {
6410 pindex = range.first->second;
6418 snap_update_firsts();
6421 if (pindex == pindexFirstInvalid) {
6422 pindexFirstInvalid =
nullptr;
6424 if (pindex == pindexFirstParked) {
6425 pindexFirstParked =
nullptr;
6427 if (pindex == pindexFirstMissing) {
6428 pindexFirstMissing =
nullptr;
6430 if (pindex == pindexFirstNeverProcessed) {
6431 pindexFirstNeverProcessed =
nullptr;
6433 if (pindex == pindexFirstNotTreeValid) {
6434 pindexFirstNotTreeValid =
nullptr;
6436 if (pindex == pindexFirstNotTransactionsValid) {
6437 pindexFirstNotTransactionsValid =
nullptr;
6439 if (pindex == pindexFirstNotChainValid) {
6440 pindexFirstNotChainValid =
nullptr;
6442 if (pindex == pindexFirstNotScriptsValid) {
6443 pindexFirstNotScriptsValid =
nullptr;
6448 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6449 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6450 rangePar = forward.equal_range(pindexPar);
6451 while (rangePar.first->second != pindex) {
6454 assert(rangePar.first != rangePar.second);
6459 if (rangePar.first != rangePar.second) {
6461 pindex = rangePar.first->second;
6473 assert(nNodes == forward.size());
6479 return strprintf(
"Chainstate [%s] @ height %d (%s)",
6485bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size) {
6498 coinsdb_size * (1.0 / 1024 / 1024));
6500 coinstip_size * (1.0 / 1024 / 1024));
6505 if (coinstip_size > old_coinstip_size) {
6522 if (pindex ==
nullptr) {
6527 "Block %d has unset m_chain_tx_count. Unable to "
6528 "estimate verification progress.\n",
6533 int64_t nNow = time(
nullptr);
6548 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6550 return m_active_chainstate->m_from_snapshot_blockhash;
6552 return std::nullopt;
6557 std::vector<Chainstate *> out;
6560 {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6562 out.push_back(pchainstate);
6571 assert(!m_ibd_chainstate);
6572 assert(!m_active_chainstate);
6574 m_ibd_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman, *
this);
6575 m_active_chainstate = m_ibd_chainstate.get();
6576 return *m_active_chainstate;
6589 const bool existed{fs::remove(base_blockhash_path)};
6591 LogPrintf(
"[snapshot] snapshot chainstate dir being removed "
6595 }
catch (
const fs::filesystem_error &e) {
6596 LogPrintf(
"[snapshot] failed to remove file %s: %s\n",
6603 LogPrintf(
"Removing leveldb dir at %s\n", path_str);
6607 const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
6610 LogPrintf(
"error: leveldb DestroyDB call failed on %s\n", path_str);
6628 "Can't activate a snapshot-based chainstate more than once")};
6636 if (!
GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
6638 std::string heights_formatted =
6639 Join(available_heights,
", ",
6640 [&](
const auto &i) {
return ToString(i); });
6642 Untranslated(
"assumeutxo block hash in snapshot metadata not "
6643 "recognized (hash: %s). The following "
6644 "snapshot heights are available: %s."),
6645 base_blockhash.
ToString(), heights_formatted)};
6649 if (!snapshot_start_block) {
6651 Untranslated(
"The base block header (%s) must appear in the "
6652 "headers chain. Make sure all headers are "
6653 "syncing, and call loadtxoutset again."),
6657 if (snapshot_start_block->nStatus.isInvalid()) {
6660 "The base block header (%s) is part of an invalid chain"),
6664 if (!m_best_header ||
6665 m_best_header->GetAncestor(snapshot_start_block->nHeight) !=
6666 snapshot_start_block) {
6668 "A forked headers-chain with more work than the chain with the "
6669 "snapshot base block header exists. Please proceed to sync "
6670 "without AssumeUtxo.")};
6673 if (
Assert(m_active_chainstate->GetMempool())->size() > 0) {
6675 "Can't activate a snapshot when mempool not empty.")};
6679 int64_t current_coinsdb_cache_size{0};
6680 int64_t current_coinstip_cache_size{0};
6689 static constexpr double IBD_CACHE_PERC = 0.01;
6690 static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
6703 current_coinsdb_cache_size =
6705 current_coinstip_cache_size =
6711 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6712 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6715 auto snapshot_chainstate =
6722 snapshot_chainstate->InitCoinsDB(
6723 static_cast<size_t>(current_coinsdb_cache_size *
6724 SNAPSHOT_CACHE_PERC),
6725 in_memory,
false,
"chainstate");
6726 snapshot_chainstate->InitCoinsCache(
static_cast<size_t>(
6727 current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
6730 auto cleanup_bad_snapshot =
6732 this->MaybeRebalanceCaches();
6737 if (
auto snapshot_datadir =
6743 snapshot_chainstate.reset();
6748 "Failed to remove snapshot chainstate dir (%s). "
6749 "Manually remove it before restarting.\n",
6759 return cleanup_bad_snapshot(
Untranslated(
"population failed"));
6770 snapshot_chainstate->m_chain.Tip())) {
6771 return cleanup_bad_snapshot(
6772 Untranslated(
"work does not exceed active chainstate"));
6778 return cleanup_bad_snapshot(
6783 assert(!m_snapshot_chainstate);
6784 m_snapshot_chainstate.swap(snapshot_chainstate);
6785 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6790 Assert(m_active_chainstate->m_mempool->size() == 0);
6791 Assert(!m_snapshot_chainstate->m_mempool);
6792 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
6793 m_active_chainstate->m_mempool =
nullptr;
6794 m_active_chainstate = m_snapshot_chainstate.get();
6797 LogPrintf(
"[snapshot] successfully activated snapshot %s\n",
6800 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() /
6803 this->MaybeRebalanceCaches();
6804 return snapshot_start_block;
6808 bool snapshot_loaded) {
6811 snapshot_loaded ?
"saving snapshot chainstate"
6812 :
"flushing coins cache",
6814 BCLog::LogFlags::ALL);
6816 coins_cache.
Flush();
6820 const char *
what()
const throw()
override {
6821 return "ComputeUTXOStats interrupted by shutdown.";
6845 if (!snapshot_start_block) {
6848 LogPrintf(
"[snapshot] Did not find snapshot start blockheader %s\n",
6853 int base_height = snapshot_start_block->
nHeight;
6856 if (!maybe_au_data) {
6857 LogPrintf(
"[snapshot] assumeutxo height in snapshot metadata not "
6858 "recognized (%d) - refusing to load snapshot\n",
6870 LogPrintf(
"[snapshot] activation failed - work does not exceed active "
6878 LogPrintf(
"[snapshot] loading %d coins from snapshot %s\n", coins_left,
6880 int64_t coins_processed{0};
6882 while (coins_left > 0) {
6886 size_t coins_per_txid{0};
6889 if (coins_per_txid > coins_left) {
6890 LogPrintf(
"[snapshot] mismatch in coins count in snapshot "
6891 "metadata and actual snapshot data\n");
6895 for (
size_t i = 0; i < coins_per_txid; i++) {
6901 if (coin.
GetHeight() > uint32_t(base_height) ||
6903 std::numeric_limits<
decltype(outpoint.GetN())>::max()) {
6904 LogPrintf(
"[snapshot] bad snapshot data after "
6905 "deserializing %d coins\n",
6906 coins_count - coins_left);
6910 LogPrintf(
"[snapshot] bad snapshot data after "
6911 "deserializing %d coins - bad tx out value\n",
6912 coins_count - coins_left);
6921 if (coins_processed % 1000000 == 0) {
6922 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6924 static_cast<float>(coins_processed) * 100 /
6925 static_cast<float>(coins_count),
6933 if (coins_processed % 120000 == 0) {
6938 const auto snapshot_cache_state =
WITH_LOCK(
6940 return snapshot_chainstate.GetCoinsCacheSizeState());
6957 }
catch (
const std::ios_base::failure &) {
6958 LogPrintf(
"[snapshot] bad snapshot format or truncated snapshot "
6959 "after deserializing %d coins\n",
6972 bool out_of_coins{
false};
6976 }
catch (
const std::ios_base::failure &) {
6978 out_of_coins =
true;
6980 if (!out_of_coins) {
6981 LogPrintf(
"[snapshot] bad snapshot - coins left over after "
6982 "deserializing %d coins\n",
6987 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
7001 std::optional<CCoinsStats> maybe_stats;
7005 CoinStatsHashType::HASH_SERIALIZED, snapshot_coinsdb,
m_blockman,
7012 if (!maybe_stats.has_value()) {
7013 LogPrintf(
"[snapshot] failed to generate coins stats\n");
7021 LogPrintf(
"[snapshot] bad snapshot content hash: expected %s, got %s\n",
7023 maybe_stats->hashSerialized.ToString());
7039 constexpr int AFTER_GENESIS_START{1};
7041 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height();
7043 index = snapshot_chainstate.
m_chain[i];
7054 assert(index == snapshot_start_block);
7058 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
7080 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
7081 !this->IsUsable(m_snapshot_chainstate.get()) ||
7082 !this->IsUsable(m_ibd_chainstate.get()) ||
7083 !m_ibd_chainstate->m_chain.Tip()) {
7089 const int snapshot_base_height = *
Assert(this->GetSnapshotBaseHeight());
7092 if (index_new.
nHeight < snapshot_base_height) {
7102 _(
"%s failed to validate the -assumeutxo snapshot state. "
7103 "This indicates a hardware problem, or a bug in the software, or "
7104 "a bad software modification that allowed an invalid snapshot to "
7105 "be loaded. As a result of this, the node will shut down and "
7106 "stop using any state that was built on the snapshot, resetting "
7107 "the chain height from %d to %d. On the next restart, the node "
7108 "will resume syncing from %d without using any snapshot data. "
7109 "Please report this incident to %s, including how you obtained "
7110 "the snapshot. The invalid snapshot chainstate will be left on "
7111 "disk in case it is helpful in diagnosing the issue that caused "
7113 PACKAGE_NAME, snapshot_tip_height, snapshot_base_height,
7114 snapshot_base_height, PACKAGE_BUGREPORT);
7117 LogPrintf(
"[snapshot] deleting snapshot, reverting to validated chain, "
7118 "and stopping node\n");
7120 m_active_chainstate = m_ibd_chainstate.get();
7121 m_snapshot_chainstate->m_disabled =
true;
7125 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
7126 if (!rename_result) {
7136 "[snapshot] supposed base block %s does not match the "
7137 "snapshot base block %s (height %d). Snapshot is not valid.\n",
7139 snapshot_base_height);
7140 handle_invalid_snapshot();
7146 int curr_height = m_ibd_chainstate->m_chain.Height();
7148 assert(snapshot_base_height == curr_height);
7153 CCoinsViewDB &ibd_coins_db = m_ibd_chainstate->CoinsDB();
7154 m_ibd_chainstate->ForceFlushStateToDisk();
7156 const auto &maybe_au_data =
7158 if (!maybe_au_data) {
7159 LogPrintf(
"[snapshot] assumeutxo data not found for height "
7160 "(%d) - refusing to validate snapshot\n",
7162 handle_invalid_snapshot();
7167 std::optional<CCoinsStats> maybe_ibd_stats;
7169 "[snapshot] computing UTXO stats for background chainstate to validate "
7170 "snapshot - this could take a few minutes\n");
7181 if (!maybe_ibd_stats) {
7183 "[snapshot] failed to generate stats for validation coins db\n");
7187 handle_invalid_snapshot();
7190 const auto &ibd_stats = *maybe_ibd_stats;
7199 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
7200 ibd_stats.hashSerialized.ToString(),
7202 handle_invalid_snapshot();
7206 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
7209 m_ibd_chainstate->m_disabled =
true;
7210 this->MaybeRebalanceCaches();
7217 assert(m_active_chainstate);
7218 return *m_active_chainstate;
7223 LOCK(active_chainstate.cs_avalancheFinalizedBlockIndex);
7224 return active_chainstate.m_avalancheFinalizedBlockIndex;
7229 return m_snapshot_chainstate &&
7230 m_active_chainstate == m_snapshot_chainstate.get();
7232void ChainstateManager::MaybeRebalanceCaches() {
7234 bool ibd_usable = this->
IsUsable(m_ibd_chainstate.get());
7235 bool snapshot_usable = this->
IsUsable(m_snapshot_chainstate.get());
7236 assert(ibd_usable || snapshot_usable);
7238 if (ibd_usable && !snapshot_usable) {
7239 LogPrintf(
"[snapshot] allocating all cache to the IBD chainstate\n");
7243 }
else if (snapshot_usable && !ibd_usable) {
7247 "[snapshot] allocating all cache to the snapshot chainstate\n");
7251 }
else if (ibd_usable && snapshot_usable) {
7260 m_snapshot_chainstate->ResizeCoinsCaches(
7263 m_snapshot_chainstate->ResizeCoinsCaches(
7271void ChainstateManager::ResetChainstates() {
7272 m_ibd_chainstate.reset();
7273 m_snapshot_chainstate.reset();
7274 m_active_chainstate =
nullptr;
7283 if (!opts.check_block_index.has_value()) {
7285 opts.config.GetChainParams().DefaultConsistencyChecks();
7288 if (!opts.minimum_chain_work.has_value()) {
7290 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
7292 if (!opts.assumed_valid_block.has_value()) {
7293 opts.assumed_valid_block =
7294 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
7296 Assert(opts.adjusted_time_callback);
7297 return std::move(opts);
7303 : m_interrupt{interrupt}, m_options{
Flatten(
std::move(options))},
7304 m_blockman{interrupt,
std::move(blockman_options)},
7305 m_validation_cache{m_options.script_execution_cache_bytes,
7306 m_options.signature_cache_bytes} {}
7308bool ChainstateManager::DetectSnapshotChainstate(
CTxMemPool *mempool) {
7309 assert(!m_snapshot_chainstate);
7310 std::optional<fs::path> path =
7315 std::optional<BlockHash> base_blockhash =
7317 if (!base_blockhash) {
7320 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
7323 this->ActivateExistingSnapshot(*base_blockhash);
7328ChainstateManager::ActivateExistingSnapshot(
BlockHash base_blockhash) {
7329 assert(!m_snapshot_chainstate);
7330 m_snapshot_chainstate = std::make_unique<Chainstate>(
nullptr,
m_blockman,
7331 *
this, base_blockhash);
7332 LogPrintf(
"[snapshot] switching active chainstate to %s\n",
7333 m_snapshot_chainstate->ToString());
7336 Assert(m_active_chainstate->m_mempool->size() == 0);
7337 Assert(!m_snapshot_chainstate->m_mempool);
7338 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
7339 m_active_chainstate->m_mempool =
nullptr;
7340 m_active_chainstate = m_snapshot_chainstate.get();
7341 return *m_snapshot_chainstate;
7348 assert(
cs.m_from_snapshot_blockhash);
7349 auto storage_path_maybe =
cs.CoinsDB().StoragePath();
7351 assert(storage_path_maybe);
7352 return *storage_path_maybe;
7361 auto invalid_path = snapshot_datadir +
"_INVALID";
7364 LogPrintf(
"[snapshot] renaming snapshot datadir %s to %s\n", dbpath,
7371 fs::rename(snapshot_datadir, invalid_path);
7372 }
catch (
const fs::filesystem_error &e) {
7376 LogPrintf(
"%s: error renaming file '%s' -> '%s': %s\n", __func__,
7377 src_str, dest_str, e.what());
7379 "You should resolve this by manually "
7380 "moving or deleting the invalid "
7381 "snapshot directory %s, otherwise you "
7382 "will encounter the same error again "
7383 "on the next startup."),
7384 src_str, dest_str, src_str)};
7389bool ChainstateManager::DeleteSnapshotChainstate() {
7391 Assert(m_snapshot_chainstate);
7392 Assert(m_ibd_chainstate);
7397 LogPrintf(
"Deletion of %s failed. Please remove it manually to "
7398 "continue reindexing.\n",
7402 m_active_chainstate = m_ibd_chainstate.get();
7403 m_active_chainstate->m_mempool = m_snapshot_chainstate->m_mempool;
7404 m_snapshot_chainstate.reset();
7416const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock()
const {
7417 return m_active_chainstate ? m_active_chainstate->SnapshotBase() :
nullptr;
7420std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const {
7421 const CBlockIndex *base = this->GetSnapshotBaseBlock();
7422 return base ? std::make_optional(base->
nHeight) :
std::nullopt;
7425void ChainstateManager::RecalculateBestHeader() {
7428 for (
auto &entry :
m_blockman.m_block_index) {
7429 if (!(entry.second.nStatus.isInvalid()) &&
7430 m_best_header->nChainWork < entry.second.nChainWork) {
7431 m_best_header = &entry.second;
7436bool ChainstateManager::ValidatedSnapshotCleanup() {
7445 std::optional<fs::path> ibd_chainstate_path_maybe =
7446 get_storage_path(m_ibd_chainstate);
7447 std::optional<fs::path> snapshot_chainstate_path_maybe =
7448 get_storage_path(m_snapshot_chainstate);
7457 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
7458 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with "
7459 "in-memory chainstates. You are testing, right?\n");
7463 const auto &snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
7464 const auto &ibd_chainstate_path = *ibd_chainstate_path_maybe;
7472 this->ResetChainstates();
7477 LogPrintf(
"[snapshot] deleting background chainstate directory (now "
7478 "unnecessary) (%s)\n",
7481 fs::path tmp_old{ibd_chainstate_path +
"_todelete"};
7484 const fs::filesystem_error &err) {
7488 "Rename of '%s' -> '%s' failed. "
7489 "Cannot clean up the background chainstate leveldb directory.",
7494 fs::rename(ibd_chainstate_path, tmp_old);
7495 }
catch (
const fs::filesystem_error &e) {
7496 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
7500 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to "
7501 "default chainstate directory (%s)\n",
7506 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
7507 }
catch (
const fs::filesystem_error &e) {
7508 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
7515 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the "
7516 "directory is now unnecessary.\n",
7519 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
7525Chainstate &ChainstateManager::GetChainstateForIndexing() {
7529 return (this->
GetAll().size() > 1) ? *m_ibd_chainstate
7530 : *m_active_chainstate;
7534ChainstateManager::GetPruneRange(
const Chainstate &chainstate,
7535 int last_height_can_prune) {
7541 if (this->
GetAll().size() > 1 &&
7542 m_snapshot_chainstate.get() == &chainstate) {
7545 prune_start = *
Assert(GetSnapshotBaseHeight()) + 1;
7548 int max_prune = std::max<int>(0, chainstate.
m_chain.
Height() -
7558 int prune_end = std::min(last_height_can_prune, max_prune);
7560 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.
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...
std::optional< R > Complete()
void Add(std::vector< T > &&vChecks)
The verifications are represented by a type T, which must provide an operator(), returning an std::op...
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)
bool Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
CCoinsView backed by the coin database (chainstate/)
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 BlockFinalized(const CBlockIndex *pindex)
void insert(Span< const uint8_t > vKey)
bool contains(Span< const uint8_t > vKey) const
CSHA256 & Write(const uint8_t *data, size_t len)
Closure representing one script verification.
SignatureCache * m_signature_cache
ScriptExecutionMetrics GetScriptExecutionMetrics() const
TxSigCheckLimiter * pTxLimitSigChecks
ScriptExecutionMetrics metrics
std::optional< std::pair< ScriptError, std::string > > operator()()
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
CTransactionRef get(const TxId &txid) const
void clear(bool include_finalized_txs=false)
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?
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(NodeClock::time_poin m_next_write)
Check warning conditions and do some notifications on new chain tip set.
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 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(
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.
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)
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(voi ClearAvalancheFinalizedBlock)() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
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 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() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
ValidationCache m_validation_cache
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
size_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.
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)
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
std::atomic< bool > m_cached_finished_ibd
Whether initial block download has ended and IsInitialBlockDownload should return false from now on.
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
const util::SignalInterrupt & m_interrupt
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
ChainstateManager(const util::SignalInterrupt &interrupt, Options options, node::BlockManager::Options blockman_options)
const arith_uint256 & MinimumChainWork() const
void CheckBlockIndex()
Make various assertions about the state of the block index.
const CBlockIndex * GetAvalancheFinalizedTip() const
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
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
std::pair< uint32_t, size_t > setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
bool get(Element &e, const bool erase) const
get is almost identical to contains(), with the difference that it obtains the found element (for Ele...
void insert(Element e, bool replace=false)
insert loops at most depth_limit times trying to insert a hash at various locations in the table via ...
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)
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range) noexcept
Return the time point advanced by a uniform random duration.
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()
Convenience class for initializing and passing the script execution cache and signature cache.
CuckooCache::cache< ScriptCacheElement, ScriptCacheHasher > m_script_execution_cache
ValidationCache(size_t script_execution_cache_bytes, size_t signature_cache_bytes)
CSHA256 ScriptExecutionCacheHasher() const
Return a copy of the pre-initialized hasher.
CSHA256 m_script_execution_cache_hasher
Pre-initialized hasher to avoid having to recreate it for every hash calculation.
SignatureCache m_signature_cache
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 fatalError(const std::string &debug_message, const bilingual_str &user_message={})
The fatal error notification is sent to notify the user when an error occurs in kernel code that can'...
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...
const kernel::BlockManagerOpts m_opts
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 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...
void UpdateBlockInfo(const CBlock &block, unsigned int nHeight, const FlatFilePos &pos)
Update blockfile info while processing a block during reindex.
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 WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight)
Store block on disk and update block file statistics.
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.
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
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.
#define LogPrintLevel(category, level,...)
#define LogPrintLevel_(category, level, should_ratelimit,...)
#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)
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::unordered_map< BlockHash, CBlockIndex, BlockHasher > BlockMap
std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir)
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::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
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
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)
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 BIP34Height
Block height and hash at which BIP34 becomes active.
int nSubsidyHalvingInterval
int obolenskyActivationTime
Unix time used for MTP activation of 15 May 2026 12:00:00 UTC upgrade.
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.
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::time_point< NodeClock > time_point
Validation result for package mempool acceptance.
Precompute sighash midstate to avoid quadratic hashing.
In future if many more values are added, it should be considered to expand the element size to 64 byt...
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...
const fs::path blocks_dir
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
std::optional< int64_t > replay_protection_activation_time
If set, this overwrites the timestamp at which replay protection activates.
#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 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 ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state)
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
bool ContextualCheckTransaction(const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state, int nHeight, int64_t nMedianTimePast)
Context dependent validity checks for non coinbase transactions.
bool ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
This is a variant of ContextualCheckTransaction which computes the contextual check for a transaction...
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 num_blocks_total
bool FatalError(Notifications ¬ifications, BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage)
GlobalMutex g_best_block_mutex
static SteadyClock::duration time_connect_total
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...
return CheckInputScripts(tx, state, view, flags, true, true, txdata, validation_cache, nSigChecksOut)
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.
static constexpr size_t WARN_FLUSH_COINS_SIZE
Size threshold for warning about slow UTXO set flush to disk.
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 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 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 SteadyClock::duration time_post_connect
static SteadyClock::duration time_chainstate
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.
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Validate (and maybe submit) a package to the mempool.
static SteadyClock::duration time_forks
static CCheckQueue< CScriptCheck > scriptcheckqueue(128)
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
static constexpr auto DATABASE_WRITE_INTERVAL_MAX
static SteadyClock::duration time_verify
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static SteadyClock::duration time_check
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
static SteadyClock::duration time_index
void StopScriptCheckWorkerThreads()
Stop all of the script checking worker threads.
static void LimitValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Mark all the coins corresponding to a given transaction inputs as spent.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params ¶ms, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
static SteadyClock::duration time_connect
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.
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)
Check a block is completely valid from start to finish (only works on top of our current best block)
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).
std::vector< Coin > GetSpentCoins(const CTransactionRef &ptx, const CCoinsViewCache &coins_view)
Get the coins spent by ptx from the coins_view.
static constexpr auto DATABASE_WRITE_INTERVAL_MIN
Time window to wait between writing blocks/block index and chainstate to disk.
static SteadyClock::duration time_total
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, const uint32_t flags, PrecomputedTransactionData &txdata, ValidationCache &validation_cache, 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 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 SteadyClock::duration time_flush
static SteadyClock::duration time_read_from_disk_total
static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt &interrupt)
static constexpr uint64_t HEADERS_TIME_VERSION
static fs::path GetSnapshotCoinsDBPath(Chainstate &cs) EXCLUSIVE_LOCKS_REQUIRED(
static bool IsReplayProtectionEnabled(const Consensus::Params ¶ms, const CBlockIndex *pindexPrev, const std::optional< int64_t > activation_time)
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(
#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.
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()