6 #if defined(HAVE_CONFIG_H) 98 "level 0 reads the blocks from disk",
99 "level 1 verifies block validity",
100 "level 2 verifies undo data",
101 "level 3 checks disconnection of tip blocks",
102 "level 4 tries to reconnect the blocks",
103 "each level includes the checks of the previous levels",
139 std::vector<CScriptCheck>* pvChecks =
nullptr)
152 const int nBlockHeight = active_chain_tip.nHeight + 1;
159 const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
161 return IsFinalTx(tx, nBlockHeight, nBlockTime);
175 std::optional<std::vector<int>> CalculatePrevHeights(
180 std::vector<int> prev_heights;
181 prev_heights.resize(tx.
vin.size());
182 for (
size_t i = 0; i < tx.
vin.size(); ++i) {
191 prev_heights[i] = tip.
nHeight + 1;
193 prev_heights[i] = coin.
nHeight;
207 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
208 if (!prev_heights.has_value())
return std::nullopt;
211 next_tip.
pprev = tip;
229 int max_input_height{0};
230 for (
const int height : prev_heights.value()) {
232 if (height != next_tip.
nHeight) {
233 max_input_height = std::max(max_input_height, height);
271 int expired = pool.Expire(GetTime<std::chrono::seconds>() - pool.m_expiry);
276 std::vector<COutPoint> vNoSpendsRemaining;
277 pool.TrimToSize(pool.m_max_size_bytes, &vNoSpendsRemaining);
278 for (
const COutPoint& removed : vNoSpendsRemaining)
279 coins_cache.Uncache(removed);
285 if (active_chainstate.m_chainman.IsInitialBlockDownload()) {
290 if (active_chainstate.m_chain.Height() < active_chainstate.m_chainman.m_best_header->nHeight - 1) {
304 std::vector<uint256> vHashUpdate;
311 const auto queuedTx = disconnectpool.
take();
312 auto it = queuedTx.rbegin();
313 while (it != queuedTx.rend()) {
315 if (!fAddToMempool || (*it)->IsCoinBase() ||
323 vHashUpdate.push_back((*it)->GetHash());
364 it->UpdateLockPoints(*new_lock_points);
371 if (it->GetSpendsCoinbase()) {
412 if (coin.
IsSpent())
return false;
424 const Coin& coinFromUTXOSet = coins_tip.AccessCoin(txin.
prevout);
442 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
443 m_active_chainstate(active_chainstate)
451 const int64_t m_accept_time;
452 const bool m_bypass_limits;
460 std::vector<COutPoint>& m_coins_to_uncache;
461 const bool m_test_accept;
465 const bool m_allow_replacement;
470 const bool m_package_submission;
474 const bool m_package_feerates;
477 static ATMPArgs SingleAccept(
const CChainParams& chainparams, int64_t accept_time,
478 bool bypass_limits, std::vector<COutPoint>& coins_to_uncache,
480 return ATMPArgs{ chainparams,
492 static ATMPArgs PackageTestAccept(
const CChainParams& chainparams, int64_t accept_time,
493 std::vector<COutPoint>& coins_to_uncache) {
494 return ATMPArgs{ chainparams,
506 static ATMPArgs PackageChildWithParents(
const CChainParams& chainparams, int64_t accept_time,
507 std::vector<COutPoint>& coins_to_uncache) {
508 return ATMPArgs{ chainparams,
520 static ATMPArgs SingleInPackageAccept(
const ATMPArgs& package_args) {
521 return ATMPArgs{ package_args.m_chainparams,
522 package_args.m_accept_time,
524 package_args.m_coins_to_uncache,
525 package_args.m_test_accept,
538 std::vector<COutPoint>& coins_to_uncache,
540 bool allow_replacement,
541 bool package_submission,
542 bool package_feerates)
543 : m_chainparams{chainparams},
544 m_accept_time{accept_time},
545 m_bypass_limits{bypass_limits},
546 m_coins_to_uncache{coins_to_uncache},
547 m_test_accept{test_accept},
548 m_allow_replacement{allow_replacement},
549 m_package_submission{package_submission},
550 m_package_feerates{package_feerates}
591 explicit Workspace(
const CTransactionRef& ptx) : m_ptx(ptx), m_hash(ptx->GetHash()) {}
593 std::set<Txid> m_conflicts;
603 std::unique_ptr<CTxMemPoolEntry> m_entry;
607 std::list<CTransactionRef> m_replaced_transactions;
619 size_t m_conflicting_size{0};
646 bool PackageMempoolChecks(
const std::vector<CTransactionRef>& txns,
669 std::map<uint256, MempoolAcceptResult>& results)
677 CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
678 if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
682 if (package_fee < m_pool.m_min_relay_feerate.GetFee(package_size)) {
684 strprintf(
"%d < %d", package_fee, m_pool.m_min_relay_feerate.GetFee(package_size)));
701 bool MemPoolAccept::PreChecks(ATMPArgs&
args, Workspace& ws)
707 const Txid& hash = ws.m_hash;
710 const int64_t nAcceptTime =
args.m_accept_time;
711 const bool bypass_limits =
args.m_bypass_limits;
712 std::vector<COutPoint>& coins_to_uncache =
args.m_coins_to_uncache;
716 std::unique_ptr<CTxMemPoolEntry>& entry = ws.m_entry;
728 if (m_pool.m_require_standard && !
IsStandardTx(tx, m_pool.m_max_datacarrier_bytes, m_pool.m_permit_bare_multisig, m_pool.m_dust_relay_feerate, reason)) {
756 if (ptxConflicting) {
757 if (!
args.m_allow_replacement) {
761 if (!ws.m_conflicts.count(ptxConflicting->
GetHash()))
780 ws.m_conflicts.insert(ptxConflicting->
GetHash());
785 m_view.SetBackend(m_viewmempool);
791 coins_to_uncache.push_back(txin.
prevout);
797 if (!m_view.HaveCoin(txin.
prevout)) {
812 m_view.GetBestBlock();
817 m_view.SetBackend(m_dummy);
819 assert(m_active_chainstate.m_blockman.LookupBlockIndex(m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
826 const std::optional<LockPoints> lock_points{
CalculateLockPointsAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx)};
848 ws.m_modified_fees = ws.m_base_fees;
849 m_pool.ApplyDelta(hash, ws.m_modified_fees);
853 bool fSpendsCoinbase =
false;
855 const Coin &coin = m_view.AccessCoin(txin.
prevout);
857 fSpendsCoinbase =
true;
864 const uint64_t entry_sequence = bypass_limits ? 0 : m_pool.GetSequence();
865 entry.reset(
new CTxMemPoolEntry(ptx, ws.m_base_fees, nAcceptTime, m_active_chainstate.m_chain.Height(), entry_sequence,
866 fSpendsCoinbase, nSigOpsCost, lock_points.value()));
867 ws.m_vsize = entry->GetTxSize();
879 if (!bypass_limits && ws.m_ptx->nVersion != 3 && ws.m_modified_fees < m_pool.m_min_relay_feerate.GetFee(ws.m_vsize)) {
883 strprintf(
"%d < %d", ws.m_modified_fees, m_pool.m_min_relay_feerate.GetFee(ws.m_vsize)));
888 if (!bypass_limits && !
args.m_package_feerates && !
CheckFeeRate(ws.m_vsize, ws.m_modified_fees, state))
return false;
890 ws.m_iters_conflicting = m_pool.GetIterSet(ws.m_conflicts);
897 if (ws.m_conflicts.size() == 1) {
925 assert(ws.m_iters_conflicting.size() == 1);
932 auto ancestors{m_pool.CalculateMemPoolAncestors(*entry, maybe_rbf_limits)};
956 ancestors = m_pool.CalculateMemPoolAncestors(*entry, cpfp_carve_out_limits);
960 ws.m_ancestors = *ancestors;
961 if (
const auto err_string{
SingleV3Checks(ws.m_ptx, ws.m_ancestors, ws.m_conflicts, ws.m_vsize)}) {
975 m_rbf = !ws.m_conflicts.empty();
979 bool MemPoolAccept::ReplacementChecks(Workspace& ws)
985 const uint256& hash = ws.m_hash;
988 CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
1006 if (
const auto err_string{
GetEntriesForConflicts(tx, m_pool, ws.m_iters_conflicting, ws.m_all_conflicting)}) {
1008 "too many potential replacements", *err_string);
1013 "replacement-adds-unconfirmed", *err_string);
1018 ws.m_conflicting_fees += it->GetModifiedFee();
1019 ws.m_conflicting_size += it->GetTxSize();
1021 if (
const auto err_string{
PaysForRBF(ws.m_conflicting_fees, ws.m_modified_fees, ws.m_vsize,
1022 m_pool.m_incremental_relay_feerate, hash)}) {
1031 bool MemPoolAccept::PackageMempoolChecks(
const std::vector<CTransactionRef>& txns,
1032 const int64_t total_vsize,
1039 assert(std::all_of(txns.cbegin(), txns.cend(), [
this](
const auto& tx)
1042 auto result = m_pool.CheckPackageLimits(txns, total_vsize);
1050 bool MemPoolAccept::PolicyScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1061 if (!
CheckInputScripts(tx, state, m_view, scriptVerifyFlags,
true,
false, ws.m_precomputed_txdata)) {
1078 bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1083 const uint256& hash = ws.m_hash;
1101 unsigned int currentBlockScriptVerifyFlags{
GetBlockScriptFlags(*m_active_chainstate.m_chain.Tip(), m_active_chainstate.m_chainman)};
1103 ws.m_precomputed_txdata, m_active_chainstate.CoinsTip())) {
1104 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s\n", hash.
ToString(), state.
ToString());
1111 bool MemPoolAccept::Finalize(
const ATMPArgs&
args, Workspace& ws)
1116 const uint256& hash = ws.m_hash;
1118 const bool bypass_limits =
args.m_bypass_limits;
1120 std::unique_ptr<CTxMemPoolEntry>& entry = ws.m_entry;
1125 LogPrint(
BCLog::MEMPOOL,
"replacing tx %s (wtxid=%s) with %s (wtxid=%s) for %s additional fees, %d delta bytes\n",
1126 it->GetTx().GetHash().ToString(),
1127 it->GetTx().GetWitnessHash().ToString(),
1130 FormatMoney(ws.m_modified_fees - ws.m_conflicting_fees),
1131 (int)entry->GetTxSize() - (int)ws.m_conflicting_size);
1132 TRACE7(mempool, replaced,
1133 it->GetTx().GetHash().data(),
1136 std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).
count(),
1141 ws.m_replaced_transactions.push_back(it->GetSharedTx());
1145 m_pool.addUnchecked(*entry, ws.m_ancestors);
1151 if (!
args.m_package_submission && !bypass_limits) {
1160 bool MemPoolAccept::SubmitPackage(
const ATMPArgs&
args, std::vector<Workspace>& workspaces,
1162 std::map<uint256, MempoolAcceptResult>& results)
1168 assert(std::all_of(workspaces.cbegin(), workspaces.cend(), [
this](
const auto& ws){
1169 return !m_pool.exists(
GenTxid::Txid(ws.m_ptx->GetHash())); }));
1171 bool all_submitted =
true;
1176 for (Workspace& ws : workspaces) {
1177 if (!ConsensusScriptChecks(
args, ws)) {
1181 all_submitted =
false;
1183 strprintf(
"BUG! PolicyScriptChecks succeeded but ConsensusScriptChecks failed: %s",
1184 ws.m_ptx->GetHash().ToString()));
1190 auto ancestors{m_pool.CalculateMemPoolAncestors(*ws.m_entry, m_pool.m_limits)};
1195 all_submitted =
false;
1197 strprintf(
"BUG! Mempool ancestors or descendants were underestimated: %s",
1198 ws.m_ptx->GetHash().ToString()));
1200 ws.m_ancestors = std::move(ancestors).value_or(ws.m_ancestors);
1207 if (!Finalize(
args, ws)) {
1211 all_submitted =
false;
1213 strprintf(
"BUG! Adding to mempool failed: %s", ws.m_ptx->GetHash().ToString()));
1217 std::vector<Wtxid> all_package_wtxids;
1218 all_package_wtxids.reserve(workspaces.size());
1219 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1220 [](
const auto& ws) {
return ws.m_ptx->GetWitnessHash(); });
1223 for (Workspace& ws : workspaces) {
1224 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1225 CFeeRate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1226 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1227 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1228 results.emplace(ws.m_ptx->GetWitnessHash(),
1230 ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
1233 ws.m_vsize, ws.m_entry->GetHeight(),
1234 args.m_bypass_limits,
args.m_package_submission,
1236 m_pool.HasNoInputsOf(tx));
1239 return all_submitted;
1248 const std::vector<Wtxid> single_wtxid{ws.m_ptx->GetWitnessHash()};
1250 if (!PreChecks(
args, ws)) {
1266 const CFeeRate effective_feerate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1268 if (
args.m_test_accept) {
1270 ws.m_base_fees, effective_feerate, single_wtxid);
1273 if (!Finalize(
args, ws)) {
1282 ws.m_vsize, ws.m_entry->GetHeight(),
1283 args.m_bypass_limits,
args.m_package_submission,
1285 m_pool.HasNoInputsOf(tx));
1289 effective_feerate, single_wtxid);
1300 std::vector<Workspace> workspaces{};
1301 workspaces.reserve(txns.size());
1302 std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1303 [](
const auto& tx) {
return Workspace(tx); });
1304 std::map<uint256, MempoolAcceptResult> results;
1309 for (Workspace& ws : workspaces) {
1310 if (!PreChecks(
args, ws)) {
1321 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1326 for (Workspace& ws : workspaces) {
1327 if (
auto err{
PackageV3Checks(ws.m_ptx, ws.m_vsize, txns, ws.m_ancestors)}) {
1342 const auto m_total_vsize = std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1343 [](int64_t
sum,
auto& ws) {
return sum + ws.m_vsize; });
1344 const auto m_total_modified_fees = std::accumulate(workspaces.cbegin(), workspaces.cend(),
CAmount{0},
1345 [](
CAmount sum,
auto& ws) {
return sum + ws.m_modified_fees; });
1346 const CFeeRate package_feerate(m_total_modified_fees, m_total_vsize);
1347 std::vector<Wtxid> all_package_wtxids;
1348 all_package_wtxids.reserve(workspaces.size());
1349 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1350 [](
const auto& ws) {
return ws.m_ptx->GetWitnessHash(); });
1352 if (
args.m_package_feerates &&
1353 !
CheckFeeRate(m_total_vsize, m_total_modified_fees, placeholder_state)) {
1362 std::string err_string;
1363 if (txns.size() > 1 && !PackageMempoolChecks(txns, m_total_vsize, package_state)) {
1367 for (Workspace& ws : workspaces) {
1368 ws.m_package_feerate = package_feerate;
1369 if (!PolicyScriptChecks(
args, ws)) {
1375 if (
args.m_test_accept) {
1376 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1377 CFeeRate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1378 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1379 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1380 results.emplace(ws.m_ptx->GetWitnessHash(),
1382 ws.m_vsize, ws.m_base_fees, effective_feerate,
1383 effective_feerate_wtxids));
1389 if (!SubmitPackage(
args, workspaces, package_state, results)) {
1397 void MemPoolAccept::CleanupTemporaryCoins()
1417 for (
const auto& outpoint : m_viewmempool.GetNonBaseCoins()) {
1420 m_view.Uncache(outpoint);
1423 m_viewmempool.Reset();
1431 if (subpackage.size() > 1) {
1432 return AcceptMultipleTransactions(subpackage,
args);
1434 const auto& tx = subpackage.front();
1435 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(
args);
1436 const auto single_res = AcceptSingleTransaction(tx, single_args);
1446 CleanupTemporaryCoins();
1473 assert(package.size() > 1);
1476 const auto& child = package.back();
1477 std::unordered_set<uint256, SaltedTxidHasher> unconfirmed_parent_txids;
1478 std::transform(package.cbegin(), package.cend() - 1,
1479 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1480 [](
const auto& tx) {
return tx->
GetHash(); });
1487 const CCoinsViewCache& coins_tip_cache = m_active_chainstate.CoinsTip();
1488 for (
const auto& input : child->vin) {
1489 if (!coins_tip_cache.HaveCoinInCache(input.prevout)) {
1490 args.m_coins_to_uncache.push_back(input.prevout);
1496 m_view.SetBackend(m_active_chainstate.CoinsTip());
1497 const auto package_or_confirmed = [
this, &unconfirmed_parent_txids](
const auto& input) {
1498 return unconfirmed_parent_txids.count(input.prevout.hash) > 0 || m_view.HaveCoin(input.prevout);
1500 if (!std::all_of(child->vin.cbegin(), child->vin.cend(), package_or_confirmed)) {
1506 m_view.SetBackend(m_dummy);
1511 std::map<uint256, MempoolAcceptResult> results_final;
1515 std::map<uint256, MempoolAcceptResult> individual_results_nonfinal;
1516 bool quit_early{
false};
1517 std::vector<CTransactionRef> txns_package_eval;
1518 for (
const auto& tx : package) {
1520 const auto& txid = tx->
GetHash();
1534 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1544 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1550 const auto single_package_res = AcceptSubPackage({tx},
args);
1551 const auto& single_res = single_package_res.m_tx_results.at(wtxid);
1556 results_final.emplace(wtxid, single_res);
1570 individual_results_nonfinal.emplace(wtxid, single_res);
1572 individual_results_nonfinal.emplace(wtxid, single_res);
1573 txns_package_eval.push_back(tx);
1578 auto multi_submission_result = quit_early || txns_package_eval.empty() ?
PackageMempoolAcceptResult(package_state_quit_early, {}) :
1579 AcceptSubPackage(txns_package_eval,
args);
1586 for (
const auto& tx : package) {
1588 if (multi_submission_result.m_tx_results.count(wtxid) > 0) {
1590 Assume(results_final.count(wtxid) == 0);
1593 const auto& txresult = multi_submission_result.m_tx_results.at(wtxid);
1600 results_final.emplace(wtxid, txresult);
1602 }
else if (
const auto it{results_final.find(wtxid)}; it != results_final.end()) {
1606 Assume(individual_results_nonfinal.count(wtxid) == 0);
1613 results_final.erase(wtxid);
1616 }
else if (
const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
1619 results_final.emplace(wtxid, it->second);
1622 Assume(results_final.size() == package.size());
1629 int64_t accept_time,
bool bypass_limits,
bool test_accept)
1633 const CChainParams& chainparams{active_chainstate.m_chainman.GetParams()};
1634 assert(active_chainstate.GetMempool() !=
nullptr);
1635 CTxMemPool& pool{*active_chainstate.GetMempool()};
1637 std::vector<COutPoint> coins_to_uncache;
1638 auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept);
1646 for (
const COutPoint& hashTx : coins_to_uncache)
1647 active_chainstate.CoinsTip().Uncache(hashTx);
1648 TRACE2(mempool, rejected,
1660 const Package& package,
bool test_accept)
1663 assert(!package.empty());
1664 assert(std::all_of(package.cbegin(), package.cend(), [](
const auto& tx){
return tx !=
nullptr;}));
1666 std::vector<COutPoint> coins_to_uncache;
1671 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(chainparams,
GetTime(), coins_to_uncache);
1672 return MemPoolAccept(pool, active_chainstate).AcceptMultipleTransactions(package,
args);
1674 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(chainparams,
GetTime(), coins_to_uncache);
1675 return MemPoolAccept(pool, active_chainstate).AcceptPackage(package,
args);
1680 if (test_accept || result.m_state.IsInvalid()) {
1681 for (
const COutPoint& hashTx : coins_to_uncache) {
1700 nSubsidy >>= halvings;
1705 : m_dbview{std::move(db_params), std::move(options)},
1706 m_catcherview(&m_dbview) {}
1708 void CoinsViews::InitCache()
1711 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1718 std::optional<uint256> from_snapshot_blockhash)
1719 : m_mempool(mempool),
1720 m_blockman(blockman),
1721 m_chainman(chainman),
1722 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1728 return m_cached_snapshot_base;
1731 void Chainstate::InitCoinsDB(
1732 size_t cache_size_bytes,
1744 .cache_bytes = cache_size_bytes,
1745 .memory_only = in_memory,
1746 .wipe_data = should_wipe,
1752 void Chainstate::InitCoinsCache(
size_t cache_size_bytes)
1778 if (chain.Tip() ==
nullptr) {
1787 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
1803 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1821 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n", __func__,
1826 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n", __func__,
1881 if (!setup_results)
return false;
1883 const auto [num_elems, approx_size_bytes] = *setup_results;
1884 LogPrintf(
"Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements\n",
1885 approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
1911 std::vector<CScriptCheck>* pvChecks)
1916 pvChecks->reserve(tx.
vin.size());
1933 std::vector<CTxOut> spent_outputs;
1934 spent_outputs.reserve(tx.
vin.size());
1936 for (
const auto& txin : tx.
vin) {
1940 spent_outputs.emplace_back(coin.
out);
1942 txdata.
Init(tx, std::move(spent_outputs));
1946 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
1957 pvChecks->emplace_back(std::move(check));
1958 }
else if (!check()) {
1986 if (cacheFullScriptStore && !pvChecks) {
1997 notifications.
fatalError(strMessage, userMessage);
1998 return state.
Error(strMessage);
2014 if (undo.nHeight == 0) {
2020 undo.nHeight = alternate.
nHeight;
2045 error(
"DisconnectBlock(): failure reading undo data");
2049 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
2050 error(
"DisconnectBlock(): block and undo data inconsistent");
2060 bool fEnforceBIP30 = !((pindex->
nHeight==91722 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
2061 (pindex->
nHeight==91812 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f")));
2064 for (
int i = block.
vtx.size() - 1; i >= 0; i--) {
2068 bool is_bip30_exception = (is_coinbase && !fEnforceBIP30);
2072 for (
size_t o = 0; o < tx.
vout.size(); o++) {
2073 if (!tx.
vout[o].scriptPubKey.IsUnspendable()) {
2078 if (!is_bip30_exception) {
2089 error(
"DisconnectBlock(): transaction and undo data inconsistent");
2092 for (
unsigned int j = tx.
vin.size(); j > 0;) {
2199 const auto time_start{SteadyClock::now()};
2215 if (!
CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) {
2222 return error(
"%s: Consensus::CheckBlock: %s", __func__, state.
ToString());
2233 if (block_hash == params.GetConsensus().hashGenesisBlock) {
2239 bool fScriptChecks =
true;
2248 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2270 const auto time_1{SteadyClock::now()};
2273 Ticks<MillisecondsDouble>(time_1 - time_start),
2315 static constexpr
int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2347 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->
GetBlockHash() == params.GetConsensus().BIP34Hash));
2352 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2353 for (
const auto& tx : block.
vtx) {
2354 for (
size_t o = 0; o < tx->
vout.size(); o++) {
2356 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite transaction\n");
2364 int nLockTimeFlags = 0;
2372 const auto time_2{SteadyClock::now()};
2375 Ticks<MillisecondsDouble>(time_2 - time_1),
2387 std::vector<PrecomputedTransactionData> txsdata(block.
vtx.size());
2389 std::vector<int> prevheights;
2392 int64_t nSigOpsCost = 0;
2393 blockundo.
vtxundo.reserve(block.
vtx.size() - 1);
2394 for (
unsigned int i = 0; i < block.
vtx.size(); i++)
2398 nInputs += tx.
vin.size();
2412 LogPrintf(
"ERROR: %s: accumulated fee in the block out of range.\n", __func__);
2419 prevheights.resize(tx.
vin.size());
2420 for (
size_t j = 0; j < tx.
vin.size(); j++) {
2424 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2425 LogPrintf(
"ERROR: %s: contains a non-BIP68-final transaction\n", __func__);
2436 LogPrintf(
"ERROR: ConnectBlock(): too many sigops\n");
2442 std::vector<CScriptCheck> vChecks;
2443 bool fCacheResults = fJustCheck;
2445 if (fScriptChecks && !
CheckInputScripts(tx, tx_state, view,
flags, fCacheResults, fCacheResults, txsdata[i], parallel_script_checks ? &vChecks :
nullptr)) {
2449 return error(
"ConnectBlock(): CheckInputScripts on %s failed with %s",
2452 control.Add(std::move(vChecks));
2457 blockundo.
vtxundo.emplace_back();
2461 const auto time_3{SteadyClock::now()};
2463 LogPrint(
BCLog::BENCH,
" - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (
unsigned)block.
vtx.size(),
2464 Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.
vtx.size(),
2465 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2470 if (block.
vtx[0]->GetValueOut() > blockReward) {
2471 LogPrintf(
"ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)\n", block.
vtx[0]->GetValueOut(), blockReward);
2475 if (!control.Wait()) {
2476 LogPrintf(
"ERROR: %s: CheckQueue failed\n", __func__);
2479 const auto time_4{SteadyClock::now()};
2481 LogPrint(
BCLog::BENCH,
" - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
2482 Ticks<MillisecondsDouble>(time_4 - time_2),
2483 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2490 if (!
m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2494 const auto time_5{SteadyClock::now()};
2497 Ticks<MillisecondsDouble>(time_5 - time_4),
2509 const auto time_6{SteadyClock::now()};
2512 Ticks<MillisecondsDouble>(time_6 - time_5),
2516 TRACE6(validation, block_connected,
2531 return this->GetCoinsCacheSizeState(
2537 size_t max_coins_cache_size_bytes,
2538 size_t max_mempool_size_bytes)
2543 int64_t nTotalSpace =
2544 max_coins_cache_size_bytes + std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2547 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024;
2548 int64_t large_threshold =
2549 std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2551 if (cacheSize > nTotalSpace) {
2552 LogPrintf(
"Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2554 }
else if (cacheSize > large_threshold) {
2560 bool Chainstate::FlushStateToDisk(
2563 int nManualPruneHeight)
2566 assert(this->CanFlushToDisk());
2567 std::set<int> setFilesToPrune;
2568 bool full_flush_completed =
false;
2575 bool fFlushForPrune =
false;
2576 bool fDoFullFlush =
false;
2584 std::optional<std::string> limiting_lock;
2586 for (
const auto& prune_lock :
m_blockman.m_prune_locks) {
2587 if (prune_lock.second.height_first == std::numeric_limits<int>::max())
continue;
2590 last_prune = std::max(1, std::min(last_prune, lock_height));
2591 if (last_prune == lock_height) {
2592 limiting_lock = prune_lock.first;
2596 if (limiting_lock) {
2597 LogPrint(
BCLog::PRUNE,
"%s limited pruning to height %d\n", limiting_lock.value(), last_prune);
2600 if (nManualPruneHeight > 0) {
2605 std::min(last_prune, nManualPruneHeight),
2613 if (!setFilesToPrune.empty()) {
2614 fFlushForPrune =
true;
2616 m_blockman.m_block_tree_db->WriteFlag(
"prunedblockfiles",
true);
2621 const auto nNow{SteadyClock::now()};
2638 fDoFullFlush = (mode ==
FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2640 if (fDoFullFlush || fPeriodicWrite) {
2665 if (fFlushForPrune) {
2673 if (fDoFullFlush && !
CoinsTip().GetBestBlock().IsNull()) {
2689 full_flush_completed =
true;
2691 int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - nNow)},
2693 (uint64_t)coins_count,
2694 (uint64_t)coins_mem_usage,
2695 (
bool)fFlushForPrune);
2698 if (full_flush_completed) {
2702 }
catch (
const std::runtime_error& e) {
2736 const std::string& func_name,
2737 const std::string&
prefix,
2742 LogPrintf(
"%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)%s\n",
2748 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2749 coins_tip.GetCacheSize(),
2750 !warning_messages.empty() ?
strprintf(
" warning='%s'", warning_messages) :
"");
2753 void Chainstate::UpdateTip(
const CBlockIndex* pindexNew)
2756 const auto& coins_tip = this->
CoinsTip();
2764 constexpr
int BACKGROUND_LOG_INTERVAL = 2000;
2765 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2766 UpdateTipLog(coins_tip, pindexNew, params, __func__,
"[background validation] ",
"");
2820 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2823 return error(
"DisconnectTip(): Failed to read block");
2826 const auto time_start{SteadyClock::now()};
2830 if (DisconnectBlock(block, pindexDelete, view) !=
DISCONNECT_OK)
2832 bool flushed = view.
Flush();
2836 Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
2840 const int max_height_first{pindexDelete->
nHeight - 1};
2841 for (
auto& prune_lock :
m_blockman.m_prune_locks) {
2842 if (prune_lock.second.height_first <= max_height_first)
continue;
2844 prune_lock.second.height_first = max_height_first;
2845 LogPrint(
BCLog::PRUNE,
"%s prune lock moved back to %d\n", prune_lock.first, max_height_first);
2864 UpdateTip(pindexDelete->
pprev);
2929 const auto time_1{SteadyClock::now()};
2930 std::shared_ptr<const CBlock> pthisBlock;
2932 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2936 pthisBlock = pblockNew;
2939 pthisBlock = pblock;
2941 const CBlock& blockConnecting = *pthisBlock;
2943 const auto time_2{SteadyClock::now()};
2944 SteadyClock::time_point time_3;
2948 Ticks<MillisecondsDouble>(time_2 - time_1));
2951 bool rv =
ConnectBlock(blockConnecting, state, pindexNew, view);
2958 time_3 = SteadyClock::now();
2962 Ticks<MillisecondsDouble>(time_3 - time_2),
2965 bool flushed = view.
Flush();
2968 const auto time_4{SteadyClock::now()};
2971 Ticks<MillisecondsDouble>(time_4 - time_3),
2978 const auto time_5{SteadyClock::now()};
2981 Ticks<MillisecondsDouble>(time_5 - time_4),
2991 UpdateTip(pindexNew);
2993 const auto time_6{SteadyClock::now()};
2997 Ticks<MillisecondsDouble>(time_6 - time_5),
3001 Ticks<MillisecondsDouble>(time_6 - time_1),
3010 m_chainman.MaybeCompleteSnapshotValidation();
3038 bool fInvalidAncestor =
false;
3048 if (fFailedChain || fMissingData) {
3055 while (pindexTest != pindexFailed) {
3059 }
else if (fMissingData) {
3064 std::make_pair(pindexFailed->
pprev, pindexFailed));
3067 pindexFailed = pindexFailed->
pprev;
3070 fInvalidAncestor =
true;
3073 pindexTest = pindexTest->
pprev;
3075 if (!fInvalidAncestor)
3107 bool fBlocksDisconnected =
false;
3121 fBlocksDisconnected =
true;
3125 std::vector<CBlockIndex*> vpindexToConnect;
3126 bool fContinue =
true;
3131 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3132 vpindexToConnect.clear();
3133 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3136 vpindexToConnect.push_back(pindexIter);
3137 pindexIter = pindexIter->
pprev;
3143 if (!
ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
3150 fInvalidFound =
true;
3171 if (fBlocksDisconnected) {
3192 bool fNotify =
false;
3193 bool fInitialBlockDownload =
false;
3198 pindexHeader = chainman.m_best_header;
3200 if (pindexHeader != pindexHeaderOld) {
3202 fInitialBlockDownload = chainman.IsInitialBlockDownload();
3203 pindexHeaderOld = pindexHeader;
3221 bool Chainstate::ActivateBestChain(
BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3240 LogPrintf(
"m_disabled is set - this chainstate should not be in operation. " 3247 bool exited_ibd{
false};
3260 LOCK(MempoolMutex());
3263 bool blocks_connected =
false;
3269 if (pindexMostWork ==
nullptr) {
3274 if (pindexMostWork ==
nullptr || pindexMostWork ==
m_chain.
Tip()) {
3278 bool fInvalidFound =
false;
3279 std::shared_ptr<const CBlock> nullBlockPtr;
3280 if (!
ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->
GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) {
3284 blocks_connected =
true;
3286 if (fInvalidFound) {
3288 pindexMostWork =
nullptr;
3293 assert(trace.pblock && trace.pindex);
3306 if (!blocks_connected)
return true;
3311 if (was_in_ibd && !still_in_ibd) {
3361 }
while (pindexNewTip != pindexMostWork);
3401 return ActivateBestChain(state, std::shared_ptr<const CBlock>());
3411 if (pindex->
nHeight == 0)
return false;
3414 bool pindex_was_in_chain =
false;
3415 int disconnected = 0;
3429 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3433 for (
auto& entry :
m_blockman.m_block_index) {
3444 candidate_blocks_by_work.insert(std::make_pair(candidate->
nChainWork, candidate));
3459 LOCK(MempoolMutex());
3461 pindex_was_in_chain =
true;
3474 if (!
ret)
return false;
3494 auto candidate_it = candidate_blocks_by_work.lower_bound(invalid_walk_tip->
pprev->
nChainWork);
3495 while (candidate_it != candidate_blocks_by_work.end()) {
3498 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3506 to_mark_failed = invalid_walk_tip;
3531 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3541 if (pindex_was_in_chain) {
3560 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3561 if (!block_index.IsValid() && block_index.GetAncestor(
nHeight) == pindex) {
3567 if (&block_index ==
m_chainman.m_best_invalid) {
3576 while (pindex !=
nullptr) {
3582 pindex = pindex->
pprev;
3596 if (is_active_chainstate) {
3600 }
else if (!m_disabled) {
3605 if (snapshot_base->GetAncestor(pindex->
nHeight) == pindex) {
3615 pindexNew->
nTx = block.
vtx.size();
3617 pindexNew->nFile = pos.
nFile;
3618 pindexNew->nDataPos = pos.
nPos;
3619 pindexNew->nUndoPos = 0;
3629 std::deque<CBlockIndex*> queue;
3630 queue.push_back(pindexNew);
3633 while (!queue.empty()) {
3639 c->TryAddBlockIndexCandidate(pindex);
3641 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range =
m_blockman.
m_blocks_unlinked.equal_range(pindex);
3642 while (range.first != range.second) {
3643 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3644 queue.push_back(it->second);
3675 "hashMerkleRoot mismatch");
3684 "bad-txns-duplicate",
3685 "duplicate transaction");
3700 if (expect_witness_commitment) {
3705 assert(!block.
vtx.empty() && !block.
vtx[0]->vin.empty());
3706 const auto& witness_stack{block.
vtx[0]->vin[0].scriptWitness.stack};
3708 if (witness_stack.size() != 1 || witness_stack[0].size() != 32) {
3711 "bad-witness-nonce-size",
3712 strprintf(
"%s : invalid witness reserved value size", __func__));
3721 if (memcmp(hash_witness.
begin(), &block.
vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
3724 "bad-witness-merkle-match",
3725 strprintf(
"%s : witness merkle commitment mismatch", __func__));
3734 for (
const auto& tx : block.
vtx) {
3738 "unexpected-witness",
3739 strprintf(
"%s : unexpected witness data found", __func__));
3779 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase())
3781 for (
unsigned int i = 1; i < block.
vtx.size(); i++)
3782 if (block.
vtx[i]->IsCoinBase())
3787 for (
const auto& tx : block.
vtx) {
3797 unsigned int nSigOps = 0;
3798 for (
const auto& tx : block.
vtx)
3805 if (fCheckPOW && fCheckMerkleRoot)
3811 void ChainstateManager::UpdateUncommittedBlockStructures(
CBlock& block,
const CBlockIndex* pindexPrev)
const 3814 static const std::vector<unsigned char>
nonce(32, 0x00);
3817 tx.
vin[0].scriptWitness.stack.resize(1);
3818 tx.
vin[0].scriptWitness.stack[0] =
nonce;
3825 std::vector<unsigned char> commitment;
3827 std::vector<unsigned char>
ret(32, 0x00);
3835 out.scriptPubKey[1] = 0x24;
3836 out.scriptPubKey[2] = 0xaa;
3837 out.scriptPubKey[3] = 0x21;
3838 out.scriptPubKey[4] = 0xa9;
3839 out.scriptPubKey[5] = 0xed;
3840 memcpy(&
out.scriptPubKey[6], witnessroot.
begin(), 32);
3841 commitment = std::vector<unsigned char>(
out.scriptPubKey.begin(),
out.scriptPubKey.end());
3843 tx.vout.push_back(
out);
3846 UpdateUncommittedBlockStructures(block, pindexPrev);
3852 return std::all_of(headers.cbegin(), headers.cend(),
3853 [&](
const auto& header) {
return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
3864 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase()) {
3871 return std::any_of(block.
vtx.begin(), block.
vtx.end(),
3909 assert(pindexPrev !=
nullptr);
3918 if (chainman.m_options.checkpoints_enabled) {
3922 const CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(chainman.GetParams().Checkpoints());
3923 if (pcheckpoint && nHeight < pcheckpoint->
nHeight) {
3924 LogPrintf(
"ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__,
nHeight);
3957 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
3960 bool enforce_locktime_median_time_past{
false};
3962 assert(pindexPrev !=
nullptr);
3963 enforce_locktime_median_time_past =
true;
3966 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
3971 for (
const auto& tx : block.
vtx) {
3981 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
3982 !std::equal(
expect.begin(),
expect.end(), block.
vtx[0]->vin[0].scriptSig.begin())) {
4018 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4020 if (miSelf !=
m_blockman.m_block_index.end()) {
4044 pindexPrev = &((*mi).second);
4079 if (pindexPrev->
GetAncestor(failedit->nHeight) == failedit) {
4082 while (invalid_walk != failedit) {
4085 invalid_walk = invalid_walk->
pprev;
4093 if (!min_pow_checked) {
4111 "Saw new header hash=%s height=%d", hash.
ToString(), pindex->
nHeight);
4145 const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};
4146 LogPrintf(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
4163 auto now = std::chrono::steady_clock::now();
4164 if (now < m_last_presync_update + std::chrono::milliseconds{250})
return;
4165 m_last_presync_update = now;
4169 if (initial_download) {
4171 const double progress{100.0 * height / (height + blocks_left)};
4172 LogPrintf(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
4179 const CBlock& block = *pblock;
4181 if (fNewBlock) *fNewBlock =
false;
4185 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
4187 bool accepted_header{
AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4190 if (!accepted_header)
4214 if (fAlreadyHave)
return true;
4216 if (pindex->
nTx != 0)
return true;
4217 if (!fHasMoreOrSameWork)
return true;
4218 if (fTooFarAhead)
return true;
4229 if (!
CheckBlock(block, state, params.GetConsensus()) ||
4244 if (fNewBlock) *fNewBlock =
true;
4247 if (blockPos.IsNull()) {
4248 state.
Error(
strprintf(
"%s: Failed to find position to write new block to disk", __func__));
4252 }
catch (
const std::runtime_error& e) {
4276 if (new_block) *new_block =
false;
4291 ret =
AcceptBlock(block, state, &pindex, force_processing,
nullptr, new_block, min_pow_checked);
4295 return error(
"%s: AcceptBlock FAILED (%s)", __func__, state.
ToString());
4303 return error(
"%s: ActivateBestChain failed (%s)", __func__, state.
ToString());
4308 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
4309 return error(
"%s: [background] ActivateBestChain failed (%s)", __func__, bg_state.
ToString());
4335 bool fCheckMerkleRoot)
4342 indexDummy.pprev = pindexPrev;
4344 indexDummy.phashBlock = &block_hash;
4348 return error(
"%s: Consensus::ContextualCheckBlockHeader: %s", __func__, state.
ToString());
4350 return error(
"%s: Consensus::CheckBlock: %s", __func__, state.
ToString());
4352 return error(
"%s: Consensus::ContextualCheckBlock: %s", __func__, state.
ToString());
4353 if (!chainstate.
ConnectBlock(block, state, &indexDummy, viewNew,
true)) {
4365 if (!active_chainstate.FlushStateToDisk(
4378 if (tip && tip->GetBlockHash() == coins_cache.
GetBestBlock()) {
4391 LogPrintf(
"Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
4392 tip->GetBlockHash().ToString(),
4414 int nCheckLevel,
int nCheckDepth)
4423 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
4426 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4427 LogPrintf(
"Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4431 int nGoodTransactions = 0;
4434 bool skipped_no_block_data{
false};
4435 bool skipped_l3_checks{
false};
4436 LogPrintf(
"Verification progress: 0%%\n");
4441 const int percentageDone = std::max(1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4442 if (reportDone < percentageDone / 10) {
4444 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
4445 reportDone = percentageDone / 10;
4454 LogPrintf(
"VerifyDB(): block verification stopping at height %d (no data). This could be due to pruning or use of an assumeutxo snapshot.\n", pindex->
nHeight);
4455 skipped_no_block_data =
true;
4465 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensus_params)) {
4466 LogPrintf(
"Verification error: found bad block at %d, hash=%s (%s)\n",
4471 if (nCheckLevel >= 2 && pindex) {
4483 if (nCheckLevel >= 3) {
4492 nGoodTransactions = 0;
4493 pindexFailure = pindex;
4495 nGoodTransactions += block.
vtx.size();
4498 skipped_l3_checks =
true;
4503 if (pindexFailure) {
4504 LogPrintf(
"Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.
m_chain.
Height() - pindexFailure->
nHeight + 1, nGoodTransactions);
4507 if (skipped_l3_checks) {
4508 LogPrintf(
"Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.\n");
4515 if (nCheckLevel >= 4 && !skipped_l3_checks) {
4517 const int percentageDone = std::max(1, std::min(99, 100 - (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (double)nCheckDepth * 50)));
4518 if (reportDone < percentageDone / 10) {
4520 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
4521 reportDone = percentageDone / 10;
4530 if (!chainstate.
ConnectBlock(block, state, pindex, coins)) {
4538 LogPrintf(
"Verification: No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
4540 if (skipped_l3_checks) {
4543 if (skipped_no_block_data) {
4560 if (!tx->IsCoinBase()) {
4561 for (
const CTxIn &txin : tx->vin) {
4579 if (hashHeads.empty())
return true;
4580 if (hashHeads.size() != 2)
return error(
"ReplayBlocks(): unknown inconsistent state");
4589 if (
m_blockman.m_block_index.count(hashHeads[0]) == 0) {
4590 return error(
"ReplayBlocks(): reorganization to unknown block requested");
4592 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
4594 if (!hashHeads[1].IsNull()) {
4595 if (
m_blockman.m_block_index.count(hashHeads[1]) == 0) {
4596 return error(
"ReplayBlocks(): reorganization from unknown block requested");
4598 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
4600 assert(pindexFork !=
nullptr);
4604 while (pindexOld != pindexFork) {
4620 pindexOld = pindexOld->
pprev;
4624 int nForkHeight = pindexFork ? pindexFork->
nHeight : 0;
4651 block = block->pprev;
4657 void Chainstate::ClearBlockIndexCandidates()
4670 if (!
ret)
return false;
4672 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
4674 std::vector<CBlockIndex*> vSortedByHeight{
m_blockman.GetAllBlockIndices()};
4675 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4685 if (pindex == GetSnapshotBaseBlock() ||
4690 chainstate->TryAddBlockIndexCandidate(pindex);
4694 m_best_invalid = pindex;
4697 m_best_header = pindex;
4700 needs_init =
m_blockman.m_block_index.empty();
4710 LogPrintf(
"Initializing databases...\n");
4725 if (
m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
4729 const CBlock& block = params.GenesisBlock();
4731 if (blockPos.IsNull()) {
4732 return error(
"%s: writing genesis block to disk failed", __func__);
4736 }
catch (
const std::runtime_error& e) {
4737 return error(
"%s: failed to write genesis block: %s", __func__, e.what());
4746 std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
4749 assert(!dbp == !blocks_with_unknown_parent);
4751 const auto start{SteadyClock::now()};
4759 uint64_t nRewind = blkdat.GetPos();
4760 while (!blkdat.eof()) {
4763 blkdat.SetPos(nRewind);
4766 unsigned int nSize = 0;
4770 blkdat.FindByte(std::byte(params.MessageStart()[0]));
4771 nRewind = blkdat.GetPos() + 1;
4773 if (buf != params.MessageStart()) {
4780 }
catch (
const std::exception&) {
4787 const uint64_t nBlockPos{blkdat.GetPos()};
4789 dbp->
nPos = nBlockPos;
4790 blkdat.SetLimit(nBlockPos + nSize);
4793 const uint256 hash{header.GetHash()};
4796 nRewind = nBlockPos + nSize;
4797 blkdat.SkipTo(nRewind);
4799 std::shared_ptr<CBlock> pblock{};
4806 header.hashPrevBlock.ToString());
4807 if (dbp && blocks_with_unknown_parent) {
4808 blocks_with_unknown_parent->emplace(header.hashPrevBlock, *dbp);
4817 blkdat.SetPos(nBlockPos);
4818 pblock = std::make_shared<CBlock>();
4820 nRewind = blkdat.GetPos();
4823 if (
AcceptBlock(pblock, state,
nullptr,
true, dbp,
nullptr,
true)) {
4829 }
else if (hash != params.GetConsensus().hashGenesisBlock && pindex->
nHeight % 1000 == 0) {
4835 if (hash == params.GetConsensus().hashGenesisBlock) {
4836 bool genesis_activation_failure =
false;
4837 for (
auto c :
GetAll()) {
4839 if (!c->ActivateBestChain(state,
nullptr)) {
4840 genesis_activation_failure =
true;
4844 if (genesis_activation_failure) {
4857 bool activation_failure =
false;
4858 for (
auto c :
GetAll()) {
4860 if (!c->ActivateBestChain(state, pblock)) {
4862 activation_failure =
true;
4866 if (activation_failure) {
4873 if (!blocks_with_unknown_parent)
continue;
4876 std::deque<uint256> queue;
4877 queue.push_back(hash);
4878 while (!queue.empty()) {
4881 auto range = blocks_with_unknown_parent->equal_range(head);
4882 while (range.first != range.second) {
4883 std::multimap<uint256, FlatFilePos>::iterator it = range.first;
4884 std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
4886 LogPrint(
BCLog::REINDEX,
"%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
4890 if (
AcceptBlock(pblockrecursive, dummy,
nullptr,
true, &it->second,
nullptr,
true)) {
4892 queue.push_back(pblockrecursive->GetHash());
4896 blocks_with_unknown_parent->erase(it);
4900 }
catch (
const std::exception& e) {
4912 LogPrint(
BCLog::REINDEX,
"%s: unexpected data at file offset 0x%x - %s. continuing\n", __func__, (nRewind - 1), e.what());
4915 }
catch (
const std::runtime_error& e) {
4918 LogPrintf(
"Loaded %i blocks from external file in %dms\n", nLoaded, Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
4938 std::multimap<CBlockIndex*,CBlockIndex*> forward;
4939 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
4940 forward.emplace(block_index.pprev, &block_index);
4945 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(
nullptr);
4947 rangeGenesis.first++;
4948 assert(rangeGenesis.first == rangeGenesis.second);
4959 CBlockIndex* pindexFirstNotTransactionsValid =
nullptr;
4961 CBlockIndex* pindexFirstNotScriptsValid =
nullptr;
4963 while (pindex !=
nullptr) {
4965 if (pindexFirstAssumeValid ==
nullptr && pindex->nStatus &
BLOCK_ASSUMED_VALID) pindexFirstAssumeValid = pindex;
4966 if (pindexFirstInvalid ==
nullptr && pindex->nStatus &
BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
4967 if (pindexFirstMissing ==
nullptr && !(pindex->nStatus &
BLOCK_HAVE_DATA)) {
4968 pindexFirstMissing = pindex;
4970 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) pindexFirstNeverProcessed = pindex;
4977 if (pindexFirstNotTransactionsValid ==
nullptr &&
4979 pindexFirstNotTransactionsValid = pindex;
4982 if (pindexFirstNotChainValid ==
nullptr &&
4984 pindexFirstNotChainValid = pindex;
4987 if (pindexFirstNotScriptsValid ==
nullptr &&
4989 pindexFirstNotScriptsValid = pindex;
4994 if (pindex->
pprev ==
nullptr) {
4997 for (
auto c :
GetAll()) {
4998 if (c->m_chain.Genesis() !=
nullptr) {
4999 assert(pindex == c->m_chain.Genesis());
5011 if (pindexFirstAssumeValid ==
nullptr) {
5016 assert(pindexFirstMissing == pindexFirstNeverProcessed);
5039 assert(pindexFirstNotTreeValid ==
nullptr);
5043 if (pindexFirstInvalid ==
nullptr) {
5053 || (pindex->
nChainTx == 0 && prev_chain_tx == 0 && pindex->
pprev)
5057 for (
auto c :
GetAll()) {
5058 if (c->m_chain.Tip() ==
nullptr)
continue;
5060 if (pindexFirstInvalid ==
nullptr) {
5067 if ((pindexFirstMissing ==
nullptr || pindex == c->m_chain.Tip())) {
5071 if (is_active || GetSnapshotBaseBlock()->GetAncestor(pindex->
nHeight) == pindex) {
5072 assert(c->setBlockIndexCandidates.count(pindex));
5080 assert(c->setBlockIndexCandidates.count(pindex) == 0);
5084 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked =
m_blockman.
m_blocks_unlinked.equal_range(pindex->
pprev);
5085 bool foundInUnlinked =
false;
5086 while (rangeUnlinked.first != rangeUnlinked.second) {
5087 assert(rangeUnlinked.first->first == pindex->
pprev);
5088 if (rangeUnlinked.first->second == pindex) {
5089 foundInUnlinked =
true;
5092 rangeUnlinked.first++;
5094 if (pindex->
pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed !=
nullptr && pindexFirstInvalid ==
nullptr) {
5099 if (pindexFirstMissing ==
nullptr)
assert(!foundInUnlinked);
5100 if (pindex->
pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed ==
nullptr && pindexFirstMissing !=
nullptr) {
5111 for (
auto c :
GetAll()) {
5114 if (pindexFirstInvalid ==
nullptr) {
5115 if (is_active || GetSnapshotBaseBlock()->GetAncestor(pindex->
nHeight) == pindex) {
5126 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
5127 if (range.first != range.second) {
5129 pindex = range.first->second;
5138 if (pindex == pindexFirstInvalid) pindexFirstInvalid =
nullptr;
5139 if (pindex == pindexFirstMissing) pindexFirstMissing =
nullptr;
5140 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed =
nullptr;
5141 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid =
nullptr;
5142 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid =
nullptr;
5143 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid =
nullptr;
5144 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid =
nullptr;
5145 if (pindex == pindexFirstAssumeValid) pindexFirstAssumeValid =
nullptr;
5149 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
5150 while (rangePar.first->second != pindex) {
5151 assert(rangePar.first != rangePar.second);
5156 if (rangePar.first != rangePar.second) {
5158 pindex = rangePar.first->second;
5170 assert(nNodes == forward.size());
5177 return strprintf(
"Chainstate [%s] @ height %d (%s)",
5182 bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size)
5195 LogPrintf(
"[%s] resized coinsdb cache to %.1f MiB\n",
5196 this->
ToString(), coinsdb_size * (1.0 / 1024 / 1024));
5197 LogPrintf(
"[%s] resized coinstip cache to %.1f MiB\n",
5198 this->
ToString(), coinstip_size * (1.0 / 1024 / 1024));
5203 if (coinstip_size > old_coinstip_size) {
5216 if (pindex ==
nullptr)
5219 int64_t nNow = time(
nullptr);
5229 return std::min<double>(pindex->
nChainTx / fTxTotal, 1.0);
5235 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
5237 return m_active_chainstate->m_from_snapshot_blockhash;
5239 return std::nullopt;
5245 std::vector<Chainstate*>
out;
5247 for (
Chainstate*
cs : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
5257 assert(!m_ibd_chainstate);
5258 assert(!m_active_chainstate);
5260 m_ibd_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman, *
this);
5261 m_active_chainstate = m_ibd_chainstate.get();
5262 return *m_active_chainstate;
5274 bool existed = fs::remove(base_blockhash_path);
5276 LogPrintf(
"[snapshot] snapshot chainstate dir being removed lacks %s file\n",
5279 }
catch (
const fs::filesystem_error& e) {
5280 LogPrintf(
"[snapshot] failed to remove file %s: %s\n",
5286 LogPrintf(
"Removing leveldb dir at %s\n", path_str);
5290 const bool destroyed =
DestroyDB(path_str);
5293 LogPrintf(
"error: leveldb DestroyDB call failed on %s\n", path_str);
5313 LogPrintf(
"[snapshot] can't activate a snapshot-based chainstate more than once\n");
5319 if (
Assert(m_active_chainstate->GetMempool())->size() > 0) {
5320 LogPrintf(
"[snapshot] can't activate a snapshot when mempool not empty\n");
5325 int64_t current_coinsdb_cache_size{0};
5326 int64_t current_coinstip_cache_size{0};
5334 static constexpr
double IBD_CACHE_PERC = 0.01;
5335 static constexpr
double SNAPSHOT_CACHE_PERC = 0.99;
5347 current_coinsdb_cache_size = this->
ActiveChainstate().m_coinsdb_cache_size_bytes;
5348 current_coinstip_cache_size = this->
ActiveChainstate().m_coinstip_cache_size_bytes;
5353 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
5354 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
5358 return std::make_unique<Chainstate>(
5359 nullptr,
m_blockman, *
this, base_blockhash));
5363 snapshot_chainstate->InitCoinsDB(
5364 static_cast<size_t>(current_coinsdb_cache_size * SNAPSHOT_CACHE_PERC),
5365 in_memory,
false,
"chainstate");
5366 snapshot_chainstate->InitCoinsCache(
5367 static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
5371 LogPrintf(
"[snapshot] activation failed - %s\n", reason);
5372 this->MaybeRebalanceCaches();
5380 snapshot_chainstate.reset();
5384 "Manually remove it before restarting.\n",
fs::PathToString(*snapshot_datadir)));
5392 return cleanup_bad_snapshot(
"population failed");
5401 return cleanup_bad_snapshot(
"work does not exceed active chainstate");
5407 return cleanup_bad_snapshot(
"could not write base blockhash");
5411 assert(!m_snapshot_chainstate);
5412 m_snapshot_chainstate.swap(snapshot_chainstate);
5413 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
5418 Assert(m_active_chainstate->m_mempool->size() == 0);
5419 Assert(!m_snapshot_chainstate->m_mempool);
5420 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5421 m_active_chainstate->m_mempool =
nullptr;
5422 m_active_chainstate = m_snapshot_chainstate.get();
5425 LogPrintf(
"[snapshot] successfully activated snapshot %s\n", base_blockhash.
ToString());
5427 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
5429 this->MaybeRebalanceCaches();
5437 snapshot_loaded ?
"saving snapshot chainstate" :
"flushing coins cache",
5441 coins_cache.
Flush();
5446 const char*
what() const noexcept
override 5448 return "ComputeUTXOStats interrupted.";
5470 if (!snapshot_start_block) {
5473 LogPrintf(
"[snapshot] Did not find snapshot start blockheader %s\n",
5474 base_blockhash.ToString());
5478 int base_height = snapshot_start_block->
nHeight;
5481 if (!maybe_au_data) {
5482 LogPrintf(
"[snapshot] assumeutxo height in snapshot metadata not recognized " 5483 "(%d) - refusing to load snapshot\n", base_height);
5493 LogPrintf(
"[snapshot] activation failed - work does not exceed active chainstate\n");
5502 LogPrintf(
"[snapshot] loading coins from snapshot %s\n", base_blockhash.ToString());
5503 int64_t coins_processed{0};
5505 while (coins_left > 0) {
5507 coins_file >> outpoint;
5509 }
catch (
const std::ios_base::failure&) {
5510 LogPrintf(
"[snapshot] bad snapshot format or truncated snapshot after deserializing %d coins\n",
5511 coins_count - coins_left);
5514 if (coin.
nHeight > base_height ||
5515 outpoint.
n >= std::numeric_limits<decltype(outpoint.
n)>::max()
5517 LogPrintf(
"[snapshot] bad snapshot data after deserializing %d coins\n",
5518 coins_count - coins_left);
5522 LogPrintf(
"[snapshot] bad snapshot data after deserializing %d coins - bad tx out value\n",
5523 coins_count - coins_left);
5532 if (coins_processed % 1000000 == 0) {
5533 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
5535 static_cast<float>(coins_processed) * 100 / static_cast<float>(coins_count),
5543 if (coins_processed % 120000 == 0) {
5549 return snapshot_chainstate.GetCoinsCacheSizeState());
5570 bool out_of_coins{
false};
5572 coins_file >> outpoint;
5573 }
catch (
const std::ios_base::failure&) {
5575 out_of_coins =
true;
5577 if (!out_of_coins) {
5578 LogPrintf(
"[snapshot] bad snapshot - coins left over after deserializing %d coins\n",
5583 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
5586 base_blockhash.ToString());
5597 std::optional<CCoinsStats> maybe_stats;
5605 if (!maybe_stats.has_value()) {
5606 LogPrintf(
"[snapshot] failed to generate coins stats\n");
5612 LogPrintf(
"[snapshot] bad snapshot content hash: expected %s, got %s\n",
5629 constexpr
int AFTER_GENESIS_START{1};
5631 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height(); ++i) {
5632 index = snapshot_chainstate.
m_chain[i];
5667 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
5690 !this->
IsUsable(m_snapshot_chainstate.get()) ||
5691 !this->
IsUsable(m_ibd_chainstate.get()) ||
5692 !m_ibd_chainstate->m_chain.Tip()) {
5698 const int snapshot_base_height = *
Assert(this->GetSnapshotBaseHeight());
5701 if (index_new.
nHeight < snapshot_base_height) {
5711 "%s failed to validate the -assumeutxo snapshot state. " 5712 "This indicates a hardware problem, or a bug in the software, or a " 5713 "bad software modification that allowed an invalid snapshot to be " 5714 "loaded. As a result of this, the node will shut down and stop using any " 5715 "state that was built on the snapshot, resetting the chain height " 5716 "from %d to %d. On the next " 5717 "restart, the node will resume syncing from %d " 5718 "without using any snapshot data. " 5719 "Please report this incident to %s, including how you obtained the snapshot. " 5720 "The invalid snapshot chainstate will be left on disk in case it is " 5721 "helpful in diagnosing the issue that caused this error."),
5726 LogPrintf(
"[snapshot] deleting snapshot, reverting to validated chain, and stopping node\n");
5728 m_active_chainstate = m_ibd_chainstate.get();
5729 m_snapshot_chainstate->m_disabled =
true;
5733 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
5734 if (!rename_result) {
5742 LogPrintf(
"[snapshot] supposed base block %s does not match the " 5743 "snapshot base block %s (height %d). Snapshot is not valid.\n",
5744 index_new.
ToString(), snapshot_blockhash.
ToString(), snapshot_base_height);
5745 handle_invalid_snapshot();
5751 int curr_height = m_ibd_chainstate->m_chain.Height();
5753 assert(snapshot_base_height == curr_height);
5758 CCoinsViewDB& ibd_coins_db = m_ibd_chainstate->CoinsDB();
5759 m_ibd_chainstate->ForceFlushStateToDisk();
5762 if (!maybe_au_data) {
5763 LogPrintf(
"[snapshot] assumeutxo data not found for height " 5764 "(%d) - refusing to validate snapshot\n", curr_height);
5765 handle_invalid_snapshot();
5770 std::optional<CCoinsStats> maybe_ibd_stats;
5771 LogPrintf(
"[snapshot] computing UTXO stats for background chainstate to validate " 5772 "snapshot - this could take a few minutes\n");
5775 CoinStatsHashType::HASH_SERIALIZED,
5784 if (!maybe_ibd_stats) {
5785 LogPrintf(
"[snapshot] failed to generate stats for validation coins db\n");
5789 handle_invalid_snapshot();
5792 const auto& ibd_stats = *maybe_ibd_stats;
5801 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
5802 ibd_stats.hashSerialized.ToString(),
5804 handle_invalid_snapshot();
5808 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
5811 m_ibd_chainstate->m_disabled =
true;
5812 this->MaybeRebalanceCaches();
5820 assert(m_active_chainstate);
5821 return *m_active_chainstate;
5827 return m_snapshot_chainstate && m_active_chainstate == m_snapshot_chainstate.get();
5830 void ChainstateManager::MaybeRebalanceCaches()
5833 bool ibd_usable = this->
IsUsable(m_ibd_chainstate.get());
5834 bool snapshot_usable = this->
IsUsable(m_snapshot_chainstate.get());
5835 assert(ibd_usable || snapshot_usable);
5837 if (ibd_usable && !snapshot_usable) {
5842 else if (snapshot_usable && !ibd_usable) {
5844 LogPrintf(
"[snapshot] allocating all cache to the snapshot chainstate\n");
5848 else if (ibd_usable && snapshot_usable) {
5853 m_ibd_chainstate->ResizeCoinsCaches(
5855 m_snapshot_chainstate->ResizeCoinsCaches(
5858 m_snapshot_chainstate->ResizeCoinsCaches(
5860 m_ibd_chainstate->ResizeCoinsCaches(
5866 void ChainstateManager::ResetChainstates()
5868 m_ibd_chainstate.reset();
5869 m_snapshot_chainstate.reset();
5870 m_active_chainstate =
nullptr;
5880 if (!opts.check_block_index.has_value()) opts.
check_block_index = opts.chainparams.DefaultConsistencyChecks();
5881 if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work =
UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork);
5882 if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
5883 return std::move(opts);
5888 m_interrupt{interrupt},
5889 m_options{
Flatten(std::move(options))},
5890 m_blockman{interrupt, std::move(blockman_options)}
5901 bool ChainstateManager::DetectSnapshotChainstate()
5903 assert(!m_snapshot_chainstate);
5909 if (!base_blockhash) {
5912 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
5915 this->ActivateExistingSnapshot(*base_blockhash);
5921 assert(!m_snapshot_chainstate);
5922 m_snapshot_chainstate =
5923 std::make_unique<Chainstate>(
nullptr,
m_blockman, *
this, base_blockhash);
5924 LogPrintf(
"[snapshot] switching active chainstate to %s\n", m_snapshot_chainstate->ToString());
5927 Assert(m_active_chainstate->m_mempool->size() == 0);
5928 Assert(!m_snapshot_chainstate->m_mempool);
5929 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5930 m_active_chainstate->m_mempool =
nullptr;
5931 m_active_chainstate = m_snapshot_chainstate.get();
5932 return *m_snapshot_chainstate;
5937 return (block_index.
nHeight==91842 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
5938 (block_index.
nHeight==91880 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721"));
5943 return (block_index.
nHeight==91722 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
5944 (block_index.
nHeight==91812 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"));
5951 assert(
cs.m_from_snapshot_blockhash);
5952 auto storage_path_maybe =
cs.CoinsDB().StoragePath();
5954 assert(storage_path_maybe);
5955 return *storage_path_maybe;
5965 auto invalid_path = snapshot_datadir +
"_INVALID";
5968 LogPrintf(
"[snapshot] renaming snapshot datadir %s to %s\n", dbpath, target);
5974 fs::rename(snapshot_datadir, invalid_path);
5975 }
catch (
const fs::filesystem_error& e) {
5979 LogPrintf(
"%s: error renaming file '%s' -> '%s': %s\n",
5980 __func__, src_str, dest_str, e.what());
5982 "Rename of '%s' -> '%s' failed. " 5983 "You should resolve this by manually moving or deleting the invalid " 5984 "snapshot directory %s, otherwise you will encounter the same error again " 5985 "on the next startup."),
5986 src_str, dest_str, src_str)};
5991 bool ChainstateManager::DeleteSnapshotChainstate()
5994 Assert(m_snapshot_chainstate);
5995 Assert(m_ibd_chainstate);
5999 LogPrintf(
"Deletion of %s failed. Please remove it manually to continue reindexing.\n",
6003 m_active_chainstate = m_ibd_chainstate.get();
6004 m_snapshot_chainstate.reset();
6018 const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock()
const 6020 return m_active_chainstate ? m_active_chainstate->SnapshotBase() :
nullptr;
6023 std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const 6025 const CBlockIndex* base = this->GetSnapshotBaseBlock();
6026 return base ? std::make_optional(base->
nHeight) :
std::nullopt;
6029 bool ChainstateManager::ValidatedSnapshotCleanup()
6033 if (!(chainstate && chainstate->HasCoinsViews())) {
6036 return chainstate->CoinsDB().StoragePath();
6038 std::optional<fs::path> ibd_chainstate_path_maybe = get_storage_path(m_ibd_chainstate);
6039 std::optional<fs::path> snapshot_chainstate_path_maybe = get_storage_path(m_snapshot_chainstate);
6048 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
6049 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with " 6050 "in-memory chainstates. You are testing, right?\n");
6054 const auto& snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
6055 const auto& ibd_chainstate_path = *ibd_chainstate_path_maybe;
6063 this->ResetChainstates();
6068 LogPrintf(
"[snapshot] deleting background chainstate directory (now unnecessary) (%s)\n",
6071 fs::path tmp_old{ibd_chainstate_path +
"_todelete"};
6073 auto rename_failed_abort = [
this](
6076 const fs::filesystem_error& err) {
6077 LogPrintf(
"Error renaming path (%s) -> (%s): %s\n",
6080 "Rename of '%s' -> '%s' failed. " 6081 "Cannot clean up the background chainstate leveldb directory.",
6086 fs::rename(ibd_chainstate_path, tmp_old);
6087 }
catch (
const fs::filesystem_error& e) {
6088 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
6092 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to " 6093 "default chainstate directory (%s)\n",
6097 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
6098 }
catch (
const fs::filesystem_error& e) {
6099 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
6106 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the " 6107 "directory is now unnecessary.\n",
6110 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
6116 Chainstate& ChainstateManager::GetChainstateForIndexing()
6121 return (this->
GetAll().size() > 1) ? *m_ibd_chainstate : *m_active_chainstate;
6124 std::pair<int, int> ChainstateManager::GetPruneRange(
const Chainstate& chainstate,
int last_height_can_prune)
6131 if (this->
GetAll().size() > 1 && m_snapshot_chainstate.get() == &chainstate) {
6134 prune_start = *
Assert(GetSnapshotBaseHeight()) + 1;
6137 int max_prune = std::max<int>(
6146 int prune_end = std::min(last_height_can_prune, max_prune);
6148 return {prune_start, prune_end};
std::optional< std::string > PaysForRBF(CAmount original_fees, CAmount replacement_fees, size_t replacement_vsize, CFeeRate relay_fee, const uint256 &txid)
The replacement transaction must pay more fees than the original transactions.
std::shared_ptr< const CTransaction > CTransactionRef
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
static SteadyClock::duration time_index
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
CoinsViewOptions coins_view
static unsigned int GetBlockScriptFlags(const CBlockIndex &block_index, const ChainstateManager &chainman)
std::string GetHex() const
CSHA256 & Write(const unsigned char *data, size_t len)
static void UpdateTipLog(const CCoinsViewCache &coins_tip, const CBlockIndex *tip, const CChainParams ¶ms, const std::string &func_name, const std::string &prefix, const std::string &warning_messages) EXCLUSIVE_LOCKS_REQUIRED(
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
bool IsSpent() const
Either this coin never existed (see e.g.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from. ...
std::optional< std::string > EntriesAndTxidsDisjoint(const CTxMemPool::setEntries &ancestors, const std::set< Txid > &direct_conflicts, const uint256 &txid)
Check the intersection between two sets of transactions (a set of mempool entries and a set of txids)...
const uint256 & AssumedValidBlock() const
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
fs::path path
Location in the filesystem where leveldb data will be stored.
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
std::vector< Coin > vprevout
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::string ToString() const
int64_t EndTime(const Consensus::Params ¶ms) const override
static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE
Maximum age of our tip for us to be considered current for fee estimation.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
invalid by consensus rules
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, CAmount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts) This does not m...
static bool CheckWitnessMalleation(const CBlock &block, bool expect_witness_commitment, BlockValidationState &state)
CheckWitnessMalleation performs checks for block malleation with regard to its witnesses.
bool HaveNumChainTxs() const
Check whether this block's and all previous blocks' transactions have been downloaded (and stored to ...
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
std::optional< std::string > PaysMoreThanConflicts(const CTxMemPool::setEntries &iters_conflicting, CFeeRate replacement_feerate, const uint256 &txid)
Check that the feerate of the replacement transaction(s) is higher than the feerate of each of the tr...
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
static GenTxid Wtxid(const uint256 &hash)
static SteadyClock::duration time_chainstate
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted...
std::condition_variable g_best_block_cv
SteadyClock::time_point m_last_flush
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
bool Error(const std::string &reject_reason)
SynchronizationState
Current sync state passed to tip changed callbacks.
static SteadyClock::duration time_forks
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
static const int WITNESS_SCALE_FACTOR
bool IsPruneMode() const
Whether running in -prune mode.
void Finalize(Span< unsigned char > output)
std::string GetDebugMessage() const
#define LogPrint(category,...)
int64_t GetBlockTime() const
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
int Threshold(const Consensus::Params ¶ms) const override
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
double dTxRate
estimated number of transactions per second after that timestamp
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
descends from failed block
CBlockIndex * pprev
pointer to the index of the predecessor of this block
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
The package itself is invalid (e.g. too many transactions).
bool Flush()
Push the modifications applied to this cache to its base and wipe local state.
std::atomic_bool fReindex
std::optional< std::string > HasNoNewUnconfirmed(const CTransaction &tx, const CTxMemPool &pool, const CTxMemPool::setEntries &iters_conflicting)
The replacement transaction may only include an unconfirmed input if that input was included in one o...
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances...
bool exists(const GenTxid >xid) const
std::function< void()> restart_indexes
Function to restart active indexes; set dynamically to avoid a circular dependency on base/index...
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
We don't have the previous block the checked one is built on.
int64_t BeginTime(const Consensus::Params ¶ms) const override
RecursiveMutex cs_LastBlockFile
The cache is at >= 90% capacity.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Options struct containing limit options for a CTxMemPool.
An in-memory indexed chain of blocks.
const int64_t m_max_size_bytes
bool ShouldCheckBlockIndex() const
size_t DynamicMemoryUsage() const
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
static const int32_t VERSIONBITS_TOP_MASK
What bitmask determines whether versionbits is in use.
reverse_range< T > reverse_iterate(T &x)
uint256 GetRandHash() noexcept
int Period(const Consensus::Params ¶ms) const override
bool DestroyDB(const std::string &path_str)
invalid proof of work or time too old
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
bool Condition(const CBlockIndex *pindex, const Consensus::Params ¶ms) const override
VerifyDBResult VerifyDB(Chainstate &chainstate, const Consensus::Params &consensus_params, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block...
size_t GetSerializeSize(const T &t)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
#define TRACE2(context, event, a, b)
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
transaction was missing some of its inputs
static CSHA256 g_scriptExecutionCacheHasher
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
std::optional< std::string > SingleV3Checks(const CTransactionRef &ptx, const CTxMemPool::setEntries &mempool_ancestors, const std::set< Txid > &direct_conflicts, int64_t vsize)
Must be called for every transaction, even if not v3.
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
bool MoneyRange(const CAmount &nValue)
int Height() const
Return the maximal height in the chain.
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
std::unordered_map< uint256, CBlockIndex, BlockHasher > BlockMap
CTxOut out
unspent transaction output
const CChainParams & chainparams
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache &versionbitscache)
Determine if a deployment is active for the next block.
bool AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't desce...
cache implements a cache with properties similar to a cuckoo-set.
stage after last reached validness failed
std::optional< bool > check_block_index
constexpr const std::byte * begin() const
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx)
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
unsigned int fCoinBase
whether containing transaction was a coinbase
The coins cache is in immediate need of a flush.
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule) ...
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
unsigned int nChainTx
Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
bool IsInterrupted(const T &result)
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
std::vector< CTxOut > m_spent_outputs
std::set< txiter, CompareIteratorByHash > setEntries
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS
For convenience, standard but not mandatory verify flags.
const TxValidationState m_state
Contains information about why the transaction failed.
static bool IsCurrentForFeeEstimation(Chainstate &active_chainstate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
static fs::path GetSnapshotCoinsDBPath(Chainstate &cs) EXCLUSIVE_LOCKS_REQUIRED(
PerBlockConnectTrace()=default
#define TRACE7(context, event, a, b, c, d, e, f, g)
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...
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
User-controlled performance and debug options.
bool ConnectTip(BlockValidationState &state, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, ConnectTrace &connectTrace, DisconnectedBlockTransactions &disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
violated mempool's fee/size/descendant/RBF/etc limits
the block header may be on a too-little-work chain
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
inputs (covered by txid) failed policy rules
undo data available in rev*.dat
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
ThresholdState
BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
void MaybeUpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transac...
const ResultType m_result_type
Result type.
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
transaction spends a coinbase too early, or violates locktime/sequence locks
void LoadExternalBlockFile(AutoFile &file_in, FlatFilePos *dbp=nullptr, std::multimap< uint256, FlatFilePos > *blocks_with_unknown_parent=nullptr)
Import blocks from an external file.
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction. ...
static bool ContextualCheckBlock(const CBlock &block, BlockValidationState &state, const ChainstateManager &chainman, const CBlockIndex *pindexPrev)
NOTE: This function is not currently invoked by ConnectBlock(), so we should consider upgrade issues ...
bool SignalsOptInRBF(const CTransaction &tx)
Check whether the sequence numbers on this transaction are signaling opt-in to replace-by-fee, according to BIP 125.
CChain m_chain
The current chain of blockheaders we consult and build on.
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
int64_t nTime
UNIX timestamp of last known number of transactions.
static const int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule) ...
static SteadyClock::duration time_verify
bool DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache &versionbitscache)
Determine if a deployment is active for this block.
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
const char * what() const noexcept override
bool TestBlockValidity(BlockValidationState &state, const CChainParams &chainparams, Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
Check a block is completely valid from start to finish (only works on top of our current best block) ...
Non-refcounted RAII wrapper for FILE*.
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
static void LimitMempoolSize(CTxMemPool &pool, CCoinsViewCache &coins_cache) EXCLUSIVE_LOCKS_REQUIRED(
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
const std::vector< CTxIn > vin
std::map< uint256, uint32_t > script_flag_exceptions
Hashes of blocks that.
const util::SignalInterrupt & m_interrupt
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
void removeForReorg(CChain &chain, std::function< bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs
After reorg, filter the entries that would no longer be valid in the next block, and update the entri...
CVerifyDB(kernel::Notifications ¬ifications)
std::vector< CBlockIndex * > GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(std::multimap< CBlockIndex *, CBlockIndex * > m_blocks_unlinked
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
uint256 g_best_block
Used to notify getblocktemplate RPC of new tips.
arith_uint256 UintToArith256(const uint256 &a)
VersionBitsCache m_versionbitscache
Track versionbit status.
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
constexpr unsigned char * begin()
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
bool IsUsable(const Chainstate *const cs) const EXCLUSIVE_LOCKS_REQUIRED(
Return true if a chainstate is considered usable.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
int nSubsidyHalvingInterval
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &block, bool force_processing, bool min_pow_checked, bool *new_block) LOCKS_EXCLUDED(cs_main)
Process an incoming block.
std::optional< std::string > GetEntriesForConflicts(const CTransaction &tx, CTxMemPool &pool, const CTxMemPool::setEntries &iters_conflicting, CTxMemPool::setEntries &all_conflicts)
Get all descendants of iters_conflicting.
int64_t nTxCount
total number of transactions between genesis and that timestamp
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate...
int64_t CAmount
Amount in satoshis (Can be negative)
void SetBestBlock(const uint256 &hashBlock)
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, const FlatFilePos *dbp)
Store block on disk.
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned...
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept) EXCLUSIVE_LOCKS_REQUIRED(
Try to add a transaction to the mempool.
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(SteadyClock::time_poin m_last_write)
Check warning conditions and do some notifications on new chain tip set.
std::array< uint8_t, 4 > MessageStartChars
uint256 GetBlockHash() const
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
static CuckooCache::cache< uint256, SignatureCacheHasher > g_scriptExecutionCache
void FindFilesToPrune(std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
Prune block and undo files (blk???.dat and rev???.dat) so that the disk space used is less than a use...
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain, ChainstateManager &chainman)
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip in of this chain.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0...
std::function< FILE *(const fs::path &, const char *)> FopenFn
std::string ToString(const T &t)
Locale-independent version of std::to_string.
bool IsBlockMutated(const CBlock &block, bool check_witness_root)
Check if a block has been mutated (with respect to its merkle root and witness commitments).
AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
bool IsBIP30Repeat(const CBlockIndex &block_index)
Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) ...
void SetfLargeWorkInvalidChainFound(bool flag)
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const std::optional< uint256 > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex * AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove any pruned block & undo files that are still on disk.
static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt &interrupt)
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static SteadyClock::duration time_check
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
The state of a background sync (for net processing)
bool CheckInputScripts(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData &txdata, std::vector< CScriptCheck > *pvChecks=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Check whether all of this transaction's input scripts succeed.
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty...
bool IsSnapshotActive() const
static std::string PathToString(const path &path)
Convert path object to a byte string.
Chainstate stores and provides an API to update our local knowledge of the current best chain...
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Scripts & signatures ok. Implies all parents are either at least VALID_SCRIPTS, or are ASSUMED_VALID...
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, uint32_t flags)
Compute total signature operation cost of a transaction.
Abstract view on the open txout dataset.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
this block was cached as being invalid and we didn't store the reason why
Validation result for package mempool acceptance.
static SteadyClock::duration time_undo
std::atomic< bool > m_cached_finished_ibd
Whether initial block download has ended and IsInitialBlockDownload should return false from now on...
An input of a transaction.
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) LOCKS_EXCLUDED(DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo ConnectBlock)(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the best known block, and make it the tip of the block chain.
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev) const
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
static SteadyClock::duration time_connect_total
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
int64_t descendant_count
The maximum allowed number of transactions in a package including the entry and its descendants...
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied...
Removed for reorganization.
void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
std::string ToString() const
bilingual_str _(const char *psz)
Translation function.
the block failed to meet one of our checkpoints
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
void TransactionAddedToMempool(const NewMempoolTransactionInfo &, uint64_t mempool_sequence)
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex...
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< uint256 > from_snapshot_blockhash=std::nullopt)
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
static feebumper::Result CheckFeeRate(const CWallet &wallet, const CMutableTransaction &mtx, const CFeeRate &newFeerate, const int64_t maxTxSize, CAmount old_fee, std::vector< bilingual_str > &errors)
Check if the user provided a valid feeRate.
kernel::Notifications & GetNotifications() const
const kernel::BlockManagerOpts m_opts
uint256 uint256S(const char *str)
int64_t nPowTargetSpacing
std::string ScriptErrorString(const ScriptError serror)
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
Abstract class that implements BIP9-style threshold logic, and caches results.
bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Whether the chain state needs to be redownloaded due to lack of witness data.
ChainstateManager(const util::SignalInterrupt &interrupt, Options options, node::BlockManager::Options blockman_options)
bool DetectSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &ActivateExistingSnapshot(uint256 base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(std::pair< int, int > GetPruneRange(const Chainstate &chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(CCheckQueue< CScriptCheck > & GetCheckQueue()
When starting up, search the datadir for a chainstate based on a UTXO snapshot that is in the process...
Holds various statistics on transactions within a chain.
void Finalize(unsigned char hash[OUTPUT_SIZE])
bool LoadingBlocks() const
const std::vector< CTxOut > vout
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Remove any entries that are in this block.
bool signet_blocks
If true, witness commitments contain a payload equal to a Bitcoin Script solution to the signet chall...
for(const CTxIn &txin :tx.vin)
std::string ToString() const
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp)
Test whether the LockPoints height and time are still valid on the current chain. ...
const Consensus::Params & GetConsensus() const
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Validate (and maybe submit) a package to the mempool.
std::shared_ptr< const CBlock > pblock
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, ConnectTrace &connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
static void LimitValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
CMainSignals & GetMainSignals()
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
#define LogPrintLevel(category, level,...)
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
bool InitScriptExecutionCache(size_t max_size_bytes)
Initializes the script-execution cache.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
An output of a transaction.
std::string ToString() const
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
std::vector< uint256 > vHave
constexpr const std::byte * data() const
At least one tx is invalid.
uint32_t nMinerConfirmationWindow
CBlockIndex * FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Return the tip of the chain with the most work in it, that isn't known to be invalid (it's however fa...
Parameters that influence chain consensus.
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
An outpoint - a combination of a transaction hash and an index n into its vout.
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
A base class defining functions for notifying about certain kernel events.
void TryAddBlockIndexCandidate(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void AddTransactionsUpdated(unsigned int n)
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
constexpr bool IsNull() const
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Calculates the block height and previous block's median time past at which the transaction will be co...
fails some policy, but might be acceptable if submitted in a (different) package
DisconnectedBlockTransactions.
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Validation result for a transaction evaluated by MemPoolAccept (single or package).
std::optional< std::string > PackageV3Checks(const CTransactionRef &ptx, int64_t vsize, const Package &package, const CTxMemPool::setEntries &mempool_ancestors)
Must be called for every transaction that is submitted within a package, even if not v3...
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define Assume(val)
Assume is the identity function.
256-bit unsigned big integer.
int64_t GetMedianTimePast() const
int64_t ancestor_size_vbytes
The maximum allowed size in virtual bytes of an entry and its ancestors within a package.
block data in blk*.dat was received with a witness-enforcing client
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
#define TRACE5(context, event, a, b, c, d, e)
void BlockConnected(CBlockIndex *pindex, std::shared_ptr< const CBlock > pblock)
bool CheckBlockDataAvailability(const CBlockIndex &upper_block LIFETIMEBOUND, const CBlockIndex &lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetFirstStoredBlock(const CBlockIndex &start_block LIFETIMEBOUND, const CBlockIndex *lower_block=nullptr) EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned)
Check if all blocks in the [upper_block, lower_block] range have data available.
int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Determine what nVersion a new block should use.
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
constexpr int64_t count_seconds(std::chrono::seconds t)
Closure representing one script verification Note that this stores references to the spending transac...
static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST
The maximum number of sigops we're willing to relay/mine in a single tx.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW=true)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
bool IsBIP30Unspendable(const CBlockIndex &block_index)
Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30) ...
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Transaction is missing a witness.
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
void CheckBlockIndex()
Make various assertions about the state of the block index.
int32_t nVersion
block header
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
const CChainParams & GetParams() const
static SteadyClock::duration time_flush
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
std::string get_filesystem_error_message(const fs::filesystem_error &e)
invalid by consensus rules (excluding any below reasons)
static time_point now() noexcept
Return current system time or mocked time, if set.
const CTransaction * ptxTo
bool IsWellFormedPackage(const Package &txns, PackageValidationState &state, bool require_sorted)
Context-free package policy checks:
const arith_uint256 & MinimumChainWork() const
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::vector< CTransactionRef > vtx
const ChainTxData & TxData() const
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'...
kernel::Notifications & m_notifications
the block's data didn't match the data committed to by the PoW
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
static SteadyClock::duration time_connect
#define LogDebug(category,...)
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, const ChainstateManager &chainman, const CBlockIndex *pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
constexpr const unsigned char * data() const
#define LOCKS_EXCLUDED(...)
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
virtual void warning(const bilingual_str &warning)
bool error(const char *fmt, const Args &... args)
The block chain is a tree shaped structure starting with the genesis block at the root...
Undo information for a CBlock.
Serialized script, used inside transaction inputs and outputs.
int64_t descendant_size_vbytes
The maximum allowed size in virtual bytes of an entry and its descendants within a package...
Undo information for a CTransaction.
std::vector< PerBlockConnectTrace > blocksConnected
CCoinsView backed by the coin database (chainstate/)
static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE
The minimum non-witness size for transactions we're willing to relay/mine: one larger than 64...
const ChainstateManager & m_chainman
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
const fs::path blocks_dir
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
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...
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
A block this one builds on is invalid.
int worker_threads_num
Number of script check worker threads. Zero means no parallel verification.
std::string ToString() const
#define TRACE6(context, event, a, b, c, d, e, f)
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network) ...
static void AppendWarning(bilingual_str &res, const bilingual_str &warn)
Private helper function that concatenates warning messages.
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL
Time to wait between writing blocks/block index to disk.
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool RaiseValidity(enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
Application-specific storage settings.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
static bool NotifyHeaderTip(ChainstateManager &chainman) LOCKS_EXCLUDED(cs_main)
bool m_checked_witness_commitment
Holds configuration for use during UTXO snapshot load and validation.
#define AssertLockNotHeld(cs)
bilingual_str ErrorString(const Result< T > &result)
bool AcceptBlock(const std::shared_ptr< const CBlock > &pblock, BlockValidationState &state, CBlockIndex **ppindex, bool fRequested, const FlatFilePos *dbp, bool *fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Sufficiently validate a block for disk storage (and store on disk).
arith_uint256 CalculateHeadersWork(const std::vector< CBlockHeader > &headers)
Return the sum of the work on a given set of headers.
int MinBIP9WarningHeight
Don't warn about unknown BIP 9 activations below this height.
bool FlushChainstateBlockFile(int tip_height)
this node does not have a mempool so can't validate the transaction
A mutable version of CTransaction.
unsigned char * UCharCast(char *c)
uint32_t nRuleChangeActivationThreshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period...
block timestamp was > 2 hours in the future (or our clock is bad)
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time...
bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points)
Check if transaction will be BIP68 final in the next block to be created on top of tip...
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits) ...
static SteadyClock::duration time_post_connect
Mutex m_chainstate_mutex
The ChainState Mutex A lock that must be held when modifying this ChainState - held in ActivateBestCh...
arith_uint256 GetBlockProof(const CBlockIndex &block)
static MempoolAcceptResult Failure(TxValidationState state)
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, unsigned int flags, PrecomputedTransactionData &txdata, CCoinsViewCache &coins_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Checks to avoid mempool polluting consensus critical paths since cached signature and script validity...
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current time before the block ...
std::list< CTransactionRef > take()
Clear all data structures and return the list of transactions.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Different type to mark Mutex at global scope.
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
int nHeight
height of the entry in the chain. The genesis block has height 0
WarningBitsConditionChecker(const ChainstateManager &chainman, int bit)
std::optional< uint256 > SnapshotBlockhash() const
const Consensus::Params & GetConsensus() const
int64_t ancestor_count
The maximum allowed number of transactions in a package including the entry and its ancestors...
bool FatalError(Notifications ¬ifications, BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage)
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
std::vector< PerBlockConnectTrace > & GetBlocksConnected()
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
std::string GetRejectReason() const
full block available in blk*.dat
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
static bool exists(const path &p)
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found.
A hasher class for SHA-256.
int64_t GetTime()
DEPRECATED, see GetTime.
std::vector< CTxUndo > vtxundo
static SynchronizationState GetSynchronizationState(bool init)
static int64_t GetBlockWeight(const CBlock &block)
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
static const int32_t VERSIONBITS_NUM_BITS
Total bits available for versionbits.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
If ASSUMED_VALID is set, it means that this block has not been validated and has validity status less...
bool PreciousBlock(BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(voi ResetBlockFailureFlags)(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as precious and reorganize.
ScriptError GetScriptError() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
std::vector< CTransactionRef > AddTransactionsFromBlock(const std::vector< CTransactionRef > &vtx)
Add transactions from the block, iterating through vtx in reverse order.
CCoinsView that brings transactions from a mempool into view.
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
void ReportHeadersPresync(const arith_uint256 &work, int64_t height, int64_t timestamp)
This is used by net_processing to report pre-synchronization progress of headers, as headers are not ...
static SteadyClock::duration time_total
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void BlockChecked(const CBlock &, const BlockValidationState &)
CHash256 & Write(Span< const unsigned char > input)
otherwise didn't meet our local policy rules
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
unsigned int nTx
Number of transactions in this block.
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...
#define PACKAGE_BUGREPORT
static constexpr unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT
An extra transaction can be added to a package, as long as it only has one ancestor and is no larger ...
static GenTxid Txid(const uint256 &hash)
static MempoolAcceptResult MempoolTxDifferentWitness(const uint256 &other_wtxid)
CTxMemPool * GetMempool()
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL
Time to wait between flushing chainstate to disk.
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex &index)
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
#define Assert(val)
Identity function.
static int64_t num_blocks_total
std::shared_ptr< Chain::Notifications > m_notifications
const Txid & GetHash() const LIFETIMEBOUND
bool m_checked_merkle_root
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
Return the time it would take to redo the work difference between from and to, assuming the current h...
static MempoolAcceptResult Success(std::list< CTransactionRef > &&replaced_txns, int64_t vsize, CAmount fees, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static constexpr TransactionSerParams TX_WITH_WITNESS
static constexpr TransactionSerParams TX_NO_WITNESS
GlobalMutex g_best_block_mutex
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
Used to track blocks whose transactions were applied to the UTXO state as a part of a single Activate...
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &block, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex=nullptr) LOCKS_EXCLUDED(cs_main)
Process incoming block headers.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
PrecomputedTransactionData * txdata
Threshold condition checker that triggers when unknown versionbits are seen on the network...