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() ||
317 true,
false).m_result_type !=
323 vHashUpdate.push_back((*it)->GetHash());
364 it->UpdateLockPoints(*new_lock_points);
371 if (it->GetSpendsCoinbase()) {
377 if (coin.IsCoinBase() && mempool_spend_height - coin.nHeight <
COINBASE_MATURITY) {
412 if (coin.
IsSpent())
return false;
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;
479 const std::optional<CFeeRate> m_client_maxfeerate;
482 static ATMPArgs SingleAccept(
const CChainParams& chainparams, int64_t accept_time,
483 bool bypass_limits, std::vector<COutPoint>& coins_to_uncache,
485 return ATMPArgs{ chainparams,
498 static ATMPArgs PackageTestAccept(
const CChainParams& chainparams, int64_t accept_time,
499 std::vector<COutPoint>& coins_to_uncache) {
500 return ATMPArgs{ chainparams,
513 static ATMPArgs PackageChildWithParents(
const CChainParams& chainparams, int64_t accept_time,
514 std::vector<COutPoint>& coins_to_uncache, std::optional<CFeeRate>& client_maxfeerate) {
515 return ATMPArgs{ chainparams,
528 static ATMPArgs SingleInPackageAccept(
const ATMPArgs& package_args) {
529 return ATMPArgs{ package_args.m_chainparams,
530 package_args.m_accept_time,
532 package_args.m_coins_to_uncache,
533 package_args.m_test_accept,
537 package_args.m_client_maxfeerate,
547 std::vector<COutPoint>& coins_to_uncache,
549 bool allow_replacement,
550 bool package_submission,
551 bool package_feerates,
552 std::optional<CFeeRate> client_maxfeerate)
553 : m_chainparams{chainparams},
554 m_accept_time{accept_time},
555 m_bypass_limits{bypass_limits},
556 m_coins_to_uncache{coins_to_uncache},
557 m_test_accept{test_accept},
558 m_allow_replacement{allow_replacement},
559 m_package_submission{package_submission},
560 m_package_feerates{package_feerates},
561 m_client_maxfeerate{client_maxfeerate}
602 explicit Workspace(
const CTransactionRef& ptx) : m_ptx(ptx), m_hash(ptx->GetHash()) {}
605 std::set<Txid> m_conflicts;
616 std::unique_ptr<CTxMemPoolEntry> m_entry;
620 std::list<CTransactionRef> m_replaced_transactions;
623 bool m_sibling_eviction{
false};
635 size_t m_conflicting_size{0};
662 bool PackageMempoolChecks(
const std::vector<CTransactionRef>& txns,
685 std::map<uint256, MempoolAcceptResult>& results)
693 CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
694 if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
698 if (package_fee < m_pool.m_min_relay_feerate.GetFee(package_size)) {
700 strprintf(
"%d < %d", package_fee, m_pool.m_min_relay_feerate.GetFee(package_size)));
718 bool MemPoolAccept::PreChecks(ATMPArgs&
args, Workspace& ws)
724 const Txid& hash = ws.m_hash;
727 const int64_t nAcceptTime =
args.m_accept_time;
728 const bool bypass_limits =
args.m_bypass_limits;
729 std::vector<COutPoint>& coins_to_uncache =
args.m_coins_to_uncache;
733 std::unique_ptr<CTxMemPoolEntry>& entry = ws.m_entry;
745 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)) {
773 if (ptxConflicting) {
774 if (!
args.m_allow_replacement) {
778 if (!ws.m_conflicts.count(ptxConflicting->
GetHash()))
797 ws.m_conflicts.insert(ptxConflicting->
GetHash());
802 m_view.SetBackend(m_viewmempool);
808 coins_to_uncache.push_back(txin.
prevout);
814 if (!m_view.HaveCoin(txin.
prevout)) {
829 m_view.GetBestBlock();
834 m_view.SetBackend(m_dummy);
836 assert(m_active_chainstate.m_blockman.LookupBlockIndex(m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
843 const std::optional<LockPoints> lock_points{
CalculateLockPointsAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx)};
865 ws.m_modified_fees = ws.m_base_fees;
866 m_pool.ApplyDelta(hash, ws.m_modified_fees);
870 bool fSpendsCoinbase =
false;
872 const Coin &coin = m_view.AccessCoin(txin.
prevout);
874 fSpendsCoinbase =
true;
881 const uint64_t entry_sequence = bypass_limits ? 0 : m_pool.GetSequence();
882 entry.reset(
new CTxMemPoolEntry(ptx, ws.m_base_fees, nAcceptTime, m_active_chainstate.m_chain.Height(), entry_sequence,
883 fSpendsCoinbase, nSigOpsCost, lock_points.value()));
884 ws.m_vsize = entry->GetTxSize();
896 if (!bypass_limits && ws.m_ptx->nVersion != 3 && ws.m_modified_fees < m_pool.m_min_relay_feerate.GetFee(ws.m_vsize)) {
900 strprintf(
"%d < %d", ws.m_modified_fees, m_pool.m_min_relay_feerate.GetFee(ws.m_vsize)));
905 if (!bypass_limits && !
args.m_package_feerates && !
CheckFeeRate(ws.m_vsize, ws.m_modified_fees, state))
return false;
907 ws.m_iters_conflicting = m_pool.GetIterSet(ws.m_conflicts);
914 if (ws.m_conflicts.size() == 1) {
942 assert(ws.m_iters_conflicting.size() == 1);
949 auto ancestors{m_pool.CalculateMemPoolAncestors(*entry, maybe_rbf_limits)};
973 ancestors = m_pool.CalculateMemPoolAncestors(*entry, cpfp_carve_out_limits);
977 ws.m_ancestors = *ancestors;
981 if (
const auto err{
SingleV3Checks(ws.m_ptx, ws.m_ancestors, ws.m_conflicts, ws.m_vsize)}) {
983 if (err->second !=
nullptr &&
args.m_allow_replacement) {
986 ws.m_conflicts.insert(err->second->GetHash());
990 ws.m_iters_conflicting.insert(m_pool.GetIter(err->second->GetHash()).value());
991 ws.m_sibling_eviction =
true;
1011 m_rbf = !ws.m_conflicts.empty();
1015 bool MemPoolAccept::ReplacementChecks(Workspace& ws)
1021 const uint256& hash = ws.m_hash;
1024 CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
1039 strprintf(
"insufficient fee%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1043 if (
const auto err_string{
GetEntriesForConflicts(tx, m_pool, ws.m_iters_conflicting, ws.m_all_conflicting)}) {
1045 strprintf(
"too many potential replacements%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1050 Assume(!ws.m_sibling_eviction);
1052 strprintf(
"replacement-adds-unconfirmed%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1057 ws.m_conflicting_fees += it->GetModifiedFee();
1058 ws.m_conflicting_size += it->GetTxSize();
1060 if (
const auto err_string{
PaysForRBF(ws.m_conflicting_fees, ws.m_modified_fees, ws.m_vsize,
1061 m_pool.m_incremental_relay_feerate, hash)}) {
1066 strprintf(
"insufficient fee%s", ws.m_sibling_eviction ?
" (including sibling eviction)" :
""), *err_string);
1071 bool MemPoolAccept::PackageMempoolChecks(
const std::vector<CTransactionRef>& txns,
1072 const int64_t total_vsize,
1079 assert(std::all_of(txns.cbegin(), txns.cend(), [
this](
const auto& tx)
1080 { return !m_pool.exists(GenTxid::Txid(tx->GetHash()));}));
1082 auto result = m_pool.CheckPackageLimits(txns, total_vsize);
1090 bool MemPoolAccept::PolicyScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1101 if (!
CheckInputScripts(tx, state, m_view, scriptVerifyFlags,
true,
false, ws.m_precomputed_txdata)) {
1118 bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs&
args, Workspace& ws)
1123 const uint256& hash = ws.m_hash;
1141 unsigned int currentBlockScriptVerifyFlags{
GetBlockScriptFlags(*m_active_chainstate.m_chain.Tip(), m_active_chainstate.m_chainman)};
1143 ws.m_precomputed_txdata, m_active_chainstate.CoinsTip())) {
1144 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s\n", hash.
ToString(), state.
ToString());
1151 bool MemPoolAccept::Finalize(
const ATMPArgs&
args, Workspace& ws)
1156 const uint256& hash = ws.m_hash;
1158 const bool bypass_limits =
args.m_bypass_limits;
1160 std::unique_ptr<CTxMemPoolEntry>& entry = ws.m_entry;
1165 LogPrint(
BCLog::MEMPOOL,
"replacing tx %s (wtxid=%s) with %s (wtxid=%s) for %s additional fees, %d delta bytes\n",
1166 it->GetTx().GetHash().ToString(),
1167 it->GetTx().GetWitnessHash().ToString(),
1170 FormatMoney(ws.m_modified_fees - ws.m_conflicting_fees),
1171 (
int)entry->GetTxSize() - (
int)ws.m_conflicting_size);
1172 TRACE7(mempool, replaced,
1173 it->GetTx().GetHash().data(),
1176 std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count(),
1181 ws.m_replaced_transactions.push_back(it->GetSharedTx());
1185 m_pool.addUnchecked(*entry, ws.m_ancestors);
1191 if (!
args.m_package_submission && !bypass_limits) {
1200 bool MemPoolAccept::SubmitPackage(
const ATMPArgs&
args, std::vector<Workspace>& workspaces,
1202 std::map<uint256, MempoolAcceptResult>& results)
1208 assert(std::all_of(workspaces.cbegin(), workspaces.cend(), [
this](
const auto& ws){
1209 return !m_pool.exists(GenTxid::Txid(ws.m_ptx->GetHash())); }));
1211 bool all_submitted =
true;
1216 for (Workspace& ws : workspaces) {
1217 if (!ConsensusScriptChecks(
args, ws)) {
1221 all_submitted =
false;
1223 strprintf(
"BUG! PolicyScriptChecks succeeded but ConsensusScriptChecks failed: %s",
1224 ws.m_ptx->GetHash().ToString()));
1230 auto ancestors{m_pool.CalculateMemPoolAncestors(*ws.m_entry, m_pool.m_limits)};
1235 all_submitted =
false;
1237 strprintf(
"BUG! Mempool ancestors or descendants were underestimated: %s",
1238 ws.m_ptx->GetHash().ToString()));
1240 ws.m_ancestors = std::move(ancestors).value_or(ws.m_ancestors);
1247 if (!Finalize(
args, ws)) {
1251 all_submitted =
false;
1253 strprintf(
"BUG! Adding to mempool failed: %s", ws.m_ptx->GetHash().ToString()));
1257 std::vector<Wtxid> all_package_wtxids;
1258 all_package_wtxids.reserve(workspaces.size());
1259 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1260 [](
const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1263 for (Workspace& ws : workspaces) {
1264 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1265 CFeeRate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1266 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1267 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1268 results.emplace(ws.m_ptx->GetWitnessHash(),
1270 ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
1271 if (!m_pool.m_signals)
continue;
1274 ws.m_vsize, ws.m_entry->GetHeight(),
1275 args.m_bypass_limits,
args.m_package_submission,
1277 m_pool.HasNoInputsOf(tx));
1278 m_pool.m_signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1280 return all_submitted;
1289 const std::vector<Wtxid> single_wtxid{ws.m_ptx->GetWitnessHash()};
1291 if (!PreChecks(
args, ws)) {
1300 if (
args.m_client_maxfeerate &&
CFeeRate(ws.m_modified_fees, ws.m_vsize) >
args.m_client_maxfeerate.value()) {
1313 const CFeeRate effective_feerate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1315 if (
args.m_test_accept) {
1317 ws.m_base_fees, effective_feerate, single_wtxid);
1320 if (!Finalize(
args, ws)) {
1327 if (m_pool.m_signals) {
1330 ws.m_vsize, ws.m_entry->GetHeight(),
1331 args.m_bypass_limits,
args.m_package_submission,
1333 m_pool.HasNoInputsOf(tx));
1334 m_pool.m_signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1338 effective_feerate, single_wtxid);
1349 std::vector<Workspace> workspaces{};
1350 workspaces.reserve(txns.size());
1351 std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1352 [](
const auto& tx) { return Workspace(tx); });
1353 std::map<uint256, MempoolAcceptResult> results;
1358 for (Workspace& ws : workspaces) {
1359 if (!PreChecks(
args, ws)) {
1368 if (
args.m_client_maxfeerate &&
CFeeRate(ws.m_modified_fees, ws.m_vsize) >
args.m_client_maxfeerate.value()) {
1380 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1385 for (Workspace& ws : workspaces) {
1386 if (
auto err{
PackageV3Checks(ws.m_ptx, ws.m_vsize, txns, ws.m_ancestors)}) {
1401 const auto m_total_vsize = std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1402 [](int64_t
sum,
auto& ws) { return sum + ws.m_vsize; });
1403 const auto m_total_modified_fees = std::accumulate(workspaces.cbegin(), workspaces.cend(),
CAmount{0},
1404 [](
CAmount sum,
auto& ws) { return sum + ws.m_modified_fees; });
1405 const CFeeRate package_feerate(m_total_modified_fees, m_total_vsize);
1406 std::vector<Wtxid> all_package_wtxids;
1407 all_package_wtxids.reserve(workspaces.size());
1408 std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1409 [](
const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1411 if (
args.m_package_feerates &&
1412 !
CheckFeeRate(m_total_vsize, m_total_modified_fees, placeholder_state)) {
1421 std::string err_string;
1422 if (txns.size() > 1 && !PackageMempoolChecks(txns, m_total_vsize, package_state)) {
1426 for (Workspace& ws : workspaces) {
1427 ws.m_package_feerate = package_feerate;
1428 if (!PolicyScriptChecks(
args, ws)) {
1434 if (
args.m_test_accept) {
1435 const auto effective_feerate =
args.m_package_feerates ? ws.m_package_feerate :
1436 CFeeRate{ws.m_modified_fees,
static_cast<uint32_t
>(ws.m_vsize)};
1437 const auto effective_feerate_wtxids =
args.m_package_feerates ? all_package_wtxids :
1438 std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1439 results.emplace(ws.m_ptx->GetWitnessHash(),
1441 ws.m_vsize, ws.m_base_fees, effective_feerate,
1442 effective_feerate_wtxids));
1448 if (!SubmitPackage(
args, workspaces, package_state, results)) {
1456 void MemPoolAccept::CleanupTemporaryCoins()
1476 for (
const auto& outpoint : m_viewmempool.GetNonBaseCoins()) {
1479 m_view.Uncache(outpoint);
1482 m_viewmempool.Reset();
1490 if (subpackage.size() > 1) {
1491 return AcceptMultipleTransactions(subpackage,
args);
1493 const auto& tx = subpackage.front();
1494 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(
args);
1495 const auto single_res = AcceptSingleTransaction(tx, single_args);
1505 CleanupTemporaryCoins();
1532 assert(package.size() > 1);
1535 const auto& child = package.back();
1536 std::unordered_set<uint256, SaltedTxidHasher> unconfirmed_parent_txids;
1537 std::transform(package.cbegin(), package.cend() - 1,
1538 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1539 [](
const auto& tx) { return tx->GetHash(); });
1546 const CCoinsViewCache& coins_tip_cache = m_active_chainstate.CoinsTip();
1547 for (
const auto& input : child->vin) {
1549 args.m_coins_to_uncache.push_back(input.prevout);
1555 m_view.SetBackend(m_active_chainstate.CoinsTip());
1556 const auto package_or_confirmed = [
this, &unconfirmed_parent_txids](
const auto& input) {
1557 return unconfirmed_parent_txids.count(input.prevout.hash) > 0 || m_view.HaveCoin(input.prevout);
1559 if (!std::all_of(child->vin.cbegin(), child->vin.cend(), package_or_confirmed)) {
1565 m_view.SetBackend(m_dummy);
1570 std::map<uint256, MempoolAcceptResult> results_final;
1574 std::map<uint256, MempoolAcceptResult> individual_results_nonfinal;
1575 bool quit_early{
false};
1576 std::vector<CTransactionRef> txns_package_eval;
1577 for (
const auto& tx : package) {
1579 const auto& txid = tx->
GetHash();
1593 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1603 const auto& entry{*
Assert(m_pool.GetEntry(txid))};
1609 const auto single_package_res = AcceptSubPackage({tx},
args);
1610 const auto& single_res = single_package_res.m_tx_results.at(wtxid);
1615 results_final.emplace(wtxid, single_res);
1629 individual_results_nonfinal.emplace(wtxid, single_res);
1631 individual_results_nonfinal.emplace(wtxid, single_res);
1632 txns_package_eval.push_back(tx);
1637 auto multi_submission_result = quit_early || txns_package_eval.empty() ?
PackageMempoolAcceptResult(package_state_quit_early, {}) :
1638 AcceptSubPackage(txns_package_eval,
args);
1645 for (
const auto& tx : package) {
1647 if (multi_submission_result.m_tx_results.count(wtxid) > 0) {
1649 Assume(results_final.count(wtxid) == 0);
1652 const auto& txresult = multi_submission_result.m_tx_results.at(wtxid);
1659 results_final.emplace(wtxid, txresult);
1661 }
else if (
const auto it{results_final.find(wtxid)}; it != results_final.end()) {
1665 Assume(individual_results_nonfinal.count(wtxid) == 0);
1672 results_final.erase(wtxid);
1675 }
else if (
const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
1678 results_final.emplace(wtxid, it->second);
1681 Assume(results_final.size() == package.size());
1688 int64_t accept_time,
bool bypass_limits,
bool test_accept)
1692 const CChainParams& chainparams{active_chainstate.m_chainman.GetParams()};
1693 assert(active_chainstate.GetMempool() !=
nullptr);
1694 CTxMemPool& pool{*active_chainstate.GetMempool()};
1696 std::vector<COutPoint> coins_to_uncache;
1697 auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept);
1705 for (
const COutPoint& hashTx : coins_to_uncache)
1706 active_chainstate.CoinsTip().Uncache(hashTx);
1707 TRACE2(mempool, rejected,
1719 const Package& package,
bool test_accept, std::optional<CFeeRate> client_maxfeerate)
1722 assert(!package.empty());
1723 assert(std::all_of(package.cbegin(), package.cend(), [](
const auto& tx){return tx != nullptr;}));
1725 std::vector<COutPoint> coins_to_uncache;
1730 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(chainparams,
GetTime(), coins_to_uncache);
1731 return MemPoolAccept(pool, active_chainstate).AcceptMultipleTransactions(package,
args);
1733 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(chainparams,
GetTime(), coins_to_uncache, client_maxfeerate);
1734 return MemPoolAccept(pool, active_chainstate).AcceptPackage(package,
args);
1739 if (test_accept || result.m_state.IsInvalid()) {
1740 for (
const COutPoint& hashTx : coins_to_uncache) {
1759 nSubsidy >>= halvings;
1764 : m_dbview{
std::move(db_params),
std::move(options)},
1765 m_catcherview(&m_dbview) {}
1767 void CoinsViews::InitCache()
1770 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1777 std::optional<uint256> from_snapshot_blockhash)
1778 : m_mempool(mempool),
1779 m_blockman(blockman),
1780 m_chainman(chainman),
1781 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1787 return m_cached_snapshot_base;
1790 void Chainstate::InitCoinsDB(
1791 size_t cache_size_bytes,
1803 .cache_bytes = cache_size_bytes,
1804 .memory_only = in_memory,
1805 .wipe_data = should_wipe,
1811 void Chainstate::InitCoinsCache(
size_t cache_size_bytes)
1837 if (chain.Tip() ==
nullptr) {
1846 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
1862 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1880 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n", __func__,
1885 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n", __func__,
1940 if (!setup_results)
return false;
1942 const auto [num_elems, approx_size_bytes] = *setup_results;
1943 LogPrintf(
"Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements\n",
1944 approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
1970 std::vector<CScriptCheck>* pvChecks)
1975 pvChecks->reserve(tx.
vin.size());
1992 std::vector<CTxOut> spent_outputs;
1993 spent_outputs.reserve(tx.
vin.size());
1995 for (
const auto& txin : tx.
vin) {
1999 spent_outputs.emplace_back(coin.
out);
2001 txdata.
Init(tx, std::move(spent_outputs));
2005 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
2016 pvChecks->emplace_back(std::move(check));
2017 }
else if (!check()) {
2045 if (cacheFullScriptStore && !pvChecks) {
2056 notifications.
fatalError(strMessage, userMessage);
2057 return state.
Error(strMessage);
2073 if (undo.nHeight == 0) {
2079 undo.nHeight = alternate.
nHeight;
2104 LogError(
"DisconnectBlock(): failure reading undo data\n");
2108 if (blockUndo.
vtxundo.size() + 1 != block.
vtx.size()) {
2109 LogError(
"DisconnectBlock(): block and undo data inconsistent\n");
2119 bool fEnforceBIP30 = !((pindex->
nHeight==91722 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
2120 (pindex->
nHeight==91812 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f")));
2123 for (
int i = block.
vtx.size() - 1; i >= 0; i--) {
2127 bool is_bip30_exception = (is_coinbase && !fEnforceBIP30);
2131 for (
size_t o = 0; o < tx.
vout.size(); o++) {
2132 if (!tx.
vout[o].scriptPubKey.IsUnspendable()) {
2137 if (!is_bip30_exception) {
2148 LogError(
"DisconnectBlock(): transaction and undo data inconsistent\n");
2151 for (
unsigned int j = tx.
vin.size(); j > 0;) {
2258 const auto time_start{SteadyClock::now()};
2274 if (!
CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) {
2293 if (block_hash == params.GetConsensus().hashGenesisBlock) {
2299 bool fScriptChecks =
true;
2308 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2330 const auto time_1{SteadyClock::now()};
2333 Ticks<MillisecondsDouble>(time_1 - time_start),
2375 static constexpr
int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2407 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->
GetBlockHash() == params.GetConsensus().BIP34Hash));
2412 if (fEnforceBIP30 || pindex->
nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2413 for (
const auto& tx : block.
vtx) {
2414 for (
size_t o = 0; o < tx->
vout.size(); o++) {
2416 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite transaction\n");
2424 int nLockTimeFlags = 0;
2432 const auto time_2{SteadyClock::now()};
2435 Ticks<MillisecondsDouble>(time_2 - time_1),
2447 std::vector<PrecomputedTransactionData> txsdata(block.
vtx.size());
2449 std::vector<int> prevheights;
2452 int64_t nSigOpsCost = 0;
2453 blockundo.
vtxundo.reserve(block.
vtx.size() - 1);
2454 for (
unsigned int i = 0; i < block.
vtx.size(); i++)
2458 nInputs += tx.
vin.size();
2473 LogPrintf(
"ERROR: %s: accumulated fee in the block out of range.\n", __func__);
2480 prevheights.resize(tx.
vin.size());
2481 for (
size_t j = 0; j < tx.
vin.size(); j++) {
2485 if (!
SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2486 LogPrintf(
"ERROR: %s: contains a non-BIP68-final transaction\n", __func__);
2497 LogPrintf(
"ERROR: ConnectBlock(): too many sigops\n");
2503 std::vector<CScriptCheck> vChecks;
2504 bool fCacheResults = fJustCheck;
2506 if (fScriptChecks && !
CheckInputScripts(tx, tx_state, view,
flags, fCacheResults, fCacheResults, txsdata[i], parallel_script_checks ? &vChecks :
nullptr)) {
2510 LogError(
"ConnectBlock(): CheckInputScripts on %s failed with %s\n",
2514 control.
Add(std::move(vChecks));
2519 blockundo.
vtxundo.emplace_back();
2523 const auto time_3{SteadyClock::now()};
2525 LogPrint(
BCLog::BENCH,
" - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (
unsigned)block.
vtx.size(),
2526 Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.
vtx.size(),
2527 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2532 if (block.
vtx[0]->GetValueOut() > blockReward) {
2533 LogPrintf(
"ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)\n", block.
vtx[0]->GetValueOut(), blockReward);
2537 if (!control.
Wait()) {
2538 LogPrintf(
"ERROR: %s: CheckQueue failed\n", __func__);
2541 const auto time_4{SteadyClock::now()};
2543 LogPrint(
BCLog::BENCH,
" - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
2544 Ticks<MillisecondsDouble>(time_4 - time_2),
2545 nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2552 if (!
m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2556 const auto time_5{SteadyClock::now()};
2559 Ticks<MillisecondsDouble>(time_5 - time_4),
2571 const auto time_6{SteadyClock::now()};
2574 Ticks<MillisecondsDouble>(time_6 - time_5),
2578 TRACE6(validation, block_connected,
2593 return this->GetCoinsCacheSizeState(
2599 size_t max_coins_cache_size_bytes,
2600 size_t max_mempool_size_bytes)
2605 int64_t nTotalSpace =
2606 max_coins_cache_size_bytes + std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2609 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024;
2610 int64_t large_threshold =
2611 std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2613 if (cacheSize > nTotalSpace) {
2614 LogPrintf(
"Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2616 }
else if (cacheSize > large_threshold) {
2622 bool Chainstate::FlushStateToDisk(
2625 int nManualPruneHeight)
2628 assert(this->CanFlushToDisk());
2629 std::set<int> setFilesToPrune;
2630 bool full_flush_completed =
false;
2637 bool fFlushForPrune =
false;
2638 bool fDoFullFlush =
false;
2646 std::optional<std::string> limiting_lock;
2648 for (
const auto& prune_lock :
m_blockman.m_prune_locks) {
2649 if (prune_lock.second.height_first == std::numeric_limits<int>::max())
continue;
2652 last_prune = std::max(1, std::min(last_prune, lock_height));
2653 if (last_prune == lock_height) {
2654 limiting_lock = prune_lock.first;
2658 if (limiting_lock) {
2659 LogPrint(
BCLog::PRUNE,
"%s limited pruning to height %d\n", limiting_lock.value(), last_prune);
2662 if (nManualPruneHeight > 0) {
2667 std::min(last_prune, nManualPruneHeight),
2675 if (!setFilesToPrune.empty()) {
2676 fFlushForPrune =
true;
2678 m_blockman.m_block_tree_db->WriteFlag(
"prunedblockfiles",
true);
2683 const auto nNow{SteadyClock::now()};
2700 fDoFullFlush = (mode ==
FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2702 if (fDoFullFlush || fPeriodicWrite) {
2727 if (fFlushForPrune) {
2735 if (fDoFullFlush && !
CoinsTip().GetBestBlock().IsNull()) {
2751 full_flush_completed =
true;
2753 int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - nNow)},
2755 (uint64_t)coins_count,
2756 (uint64_t)coins_mem_usage,
2757 (
bool)fFlushForPrune);
2764 }
catch (
const std::runtime_error& e) {
2798 const std::string& func_name,
2799 const std::string&
prefix,
2804 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",
2812 !warning_messages.empty() ?
strprintf(
" warning='%s'", warning_messages) :
"");
2815 void Chainstate::UpdateTip(
const CBlockIndex* pindexNew)
2818 const auto& coins_tip = this->
CoinsTip();
2826 constexpr
int BACKGROUND_LOG_INTERVAL = 2000;
2827 if (pindexNew->
nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2828 UpdateTipLog(coins_tip, pindexNew, params, __func__,
"[background validation] ",
"");
2882 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2885 LogError(
"DisconnectTip(): Failed to read block\n");
2889 const auto time_start{SteadyClock::now()};
2893 if (DisconnectBlock(block, pindexDelete, view) !=
DISCONNECT_OK) {
2897 bool flushed = view.
Flush();
2901 Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
2905 const int max_height_first{pindexDelete->
nHeight - 1};
2906 for (
auto& prune_lock :
m_blockman.m_prune_locks) {
2907 if (prune_lock.second.height_first <= max_height_first)
continue;
2909 prune_lock.second.height_first = max_height_first;
2910 LogPrint(
BCLog::PRUNE,
"%s prune lock moved back to %d\n", prune_lock.first, max_height_first);
2929 UpdateTip(pindexDelete->
pprev);
2996 const auto time_1{SteadyClock::now()};
2997 std::shared_ptr<const CBlock> pthisBlock;
2999 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3003 pthisBlock = pblockNew;
3006 pthisBlock = pblock;
3008 const CBlock& blockConnecting = *pthisBlock;
3010 const auto time_2{SteadyClock::now()};
3011 SteadyClock::time_point time_3;
3015 Ticks<MillisecondsDouble>(time_2 - time_1));
3018 bool rv =
ConnectBlock(blockConnecting, state, pindexNew, view);
3028 time_3 = SteadyClock::now();
3032 Ticks<MillisecondsDouble>(time_3 - time_2),
3035 bool flushed = view.
Flush();
3038 const auto time_4{SteadyClock::now()};
3041 Ticks<MillisecondsDouble>(time_4 - time_3),
3048 const auto time_5{SteadyClock::now()};
3051 Ticks<MillisecondsDouble>(time_5 - time_4),
3061 UpdateTip(pindexNew);
3063 const auto time_6{SteadyClock::now()};
3067 Ticks<MillisecondsDouble>(time_6 - time_5),
3071 Ticks<MillisecondsDouble>(time_6 - time_1),
3080 m_chainman.MaybeCompleteSnapshotValidation();
3108 bool fInvalidAncestor =
false;
3118 if (fFailedChain || fMissingData) {
3125 while (pindexTest != pindexFailed) {
3129 }
else if (fMissingData) {
3134 std::make_pair(pindexFailed->
pprev, pindexFailed));
3137 pindexFailed = pindexFailed->
pprev;
3140 fInvalidAncestor =
true;
3143 pindexTest = pindexTest->
pprev;
3145 if (!fInvalidAncestor)
3177 bool fBlocksDisconnected =
false;
3191 fBlocksDisconnected =
true;
3195 std::vector<CBlockIndex*> vpindexToConnect;
3196 bool fContinue =
true;
3201 int nTargetHeight = std::min(
nHeight + 32, pindexMostWork->
nHeight);
3202 vpindexToConnect.clear();
3203 vpindexToConnect.reserve(nTargetHeight -
nHeight);
3206 vpindexToConnect.push_back(pindexIter);
3207 pindexIter = pindexIter->
pprev;
3213 if (!
ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
3220 fInvalidFound =
true;
3241 if (fBlocksDisconnected) {
3262 bool fNotify =
false;
3263 bool fInitialBlockDownload =
false;
3268 pindexHeader = chainman.m_best_header;
3270 if (pindexHeader != pindexHeaderOld) {
3272 fInitialBlockDownload = chainman.IsInitialBlockDownload();
3273 pindexHeaderOld = pindexHeader;
3286 if (signals.CallbacksPending() > 10) {
3287 signals.SyncWithValidationInterfaceQueue();
3291 bool Chainstate::ActivateBestChain(
BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3310 LogPrintf(
"m_disabled is set - this chainstate should not be in operation. "
3317 bool exited_ibd{
false};
3330 LOCK(MempoolMutex());
3333 bool blocks_connected =
false;
3339 if (pindexMostWork ==
nullptr) {
3344 if (pindexMostWork ==
nullptr || pindexMostWork ==
m_chain.
Tip()) {
3348 bool fInvalidFound =
false;
3349 std::shared_ptr<const CBlock> nullBlockPtr;
3350 if (!
ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->
GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) {
3354 blocks_connected =
true;
3356 if (fInvalidFound) {
3358 pindexMostWork =
nullptr;
3363 assert(trace.pblock && trace.pindex);
3378 if (!blocks_connected)
return true;
3383 if (was_in_ibd && !still_in_ibd) {
3435 }
while (pindexNewTip != pindexMostWork);
3475 return ActivateBestChain(state, std::shared_ptr<const CBlock>());
3485 if (pindex->
nHeight == 0)
return false;
3488 bool pindex_was_in_chain =
false;
3489 int disconnected = 0;
3503 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3507 for (
auto& entry :
m_blockman.m_block_index) {
3518 candidate_blocks_by_work.insert(std::make_pair(candidate->
nChainWork, candidate));
3533 LOCK(MempoolMutex());
3535 pindex_was_in_chain =
true;
3548 if (!
ret)
return false;
3568 auto candidate_it = candidate_blocks_by_work.lower_bound(invalid_walk_tip->
pprev->
nChainWork);
3569 while (candidate_it != candidate_blocks_by_work.end()) {
3572 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3580 to_mark_failed = invalid_walk_tip;
3605 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3615 if (pindex_was_in_chain) {
3634 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
3635 if (!block_index.IsValid() && block_index.GetAncestor(
nHeight) == pindex) {
3641 if (&block_index ==
m_chainman.m_best_invalid) {
3650 while (pindex !=
nullptr) {
3656 pindex = pindex->
pprev;
3670 if (is_active_chainstate) {
3674 }
else if (!m_disabled) {
3679 if (snapshot_base->GetAncestor(pindex->
nHeight) == pindex) {
3689 pindexNew->
nTx = block.
vtx.size();
3691 pindexNew->nFile = pos.
nFile;
3692 pindexNew->nDataPos = pos.
nPos;
3693 pindexNew->nUndoPos = 0;
3703 std::deque<CBlockIndex*> queue;
3704 queue.push_back(pindexNew);
3707 while (!queue.empty()) {
3713 c->TryAddBlockIndexCandidate(pindex);
3715 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range =
m_blockman.
m_blocks_unlinked.equal_range(pindex);
3716 while (range.first != range.second) {
3717 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3718 queue.push_back(it->second);
3749 "hashMerkleRoot mismatch");
3758 "bad-txns-duplicate",
3759 "duplicate transaction");
3774 if (expect_witness_commitment) {
3779 assert(!block.
vtx.empty() && !block.
vtx[0]->vin.empty());
3780 const auto& witness_stack{block.
vtx[0]->vin[0].scriptWitness.stack};
3782 if (witness_stack.size() != 1 || witness_stack[0].size() != 32) {
3785 "bad-witness-nonce-size",
3786 strprintf(
"%s : invalid witness reserved value size", __func__));
3795 if (memcmp(hash_witness.
begin(), &block.
vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
3798 "bad-witness-merkle-match",
3799 strprintf(
"%s : witness merkle commitment mismatch", __func__));
3808 for (
const auto& tx : block.
vtx) {
3812 "unexpected-witness",
3813 strprintf(
"%s : unexpected witness data found", __func__));
3853 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase())
3855 for (
unsigned int i = 1; i < block.
vtx.size(); i++)
3856 if (block.
vtx[i]->IsCoinBase())
3861 for (
const auto& tx : block.
vtx) {
3871 unsigned int nSigOps = 0;
3872 for (
const auto& tx : block.
vtx)
3879 if (fCheckPOW && fCheckMerkleRoot)
3885 void ChainstateManager::UpdateUncommittedBlockStructures(
CBlock& block,
const CBlockIndex* pindexPrev)
const
3888 static const std::vector<unsigned char>
nonce(32, 0x00);
3891 tx.
vin[0].scriptWitness.stack.resize(1);
3892 tx.
vin[0].scriptWitness.stack[0] =
nonce;
3899 std::vector<unsigned char> commitment;
3901 std::vector<unsigned char>
ret(32, 0x00);
3909 out.scriptPubKey[1] = 0x24;
3910 out.scriptPubKey[2] = 0xaa;
3911 out.scriptPubKey[3] = 0x21;
3912 out.scriptPubKey[4] = 0xa9;
3913 out.scriptPubKey[5] = 0xed;
3914 memcpy(&
out.scriptPubKey[6], witnessroot.
begin(), 32);
3915 commitment = std::vector<unsigned char>(
out.scriptPubKey.begin(),
out.scriptPubKey.end());
3920 UpdateUncommittedBlockStructures(block, pindexPrev);
3926 return std::all_of(headers.cbegin(), headers.cend(),
3927 [&](
const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
3938 if (block.
vtx.empty() || !block.
vtx[0]->IsCoinBase()) {
3945 return std::any_of(block.
vtx.begin(), block.
vtx.end(),
3946 [](
auto& tx) { return GetSerializeSize(TX_NO_WITNESS(tx)) == 64; });
3983 assert(pindexPrev !=
nullptr);
3992 if (chainman.m_options.checkpoints_enabled) {
3996 const CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(chainman.GetParams().Checkpoints());
3997 if (pcheckpoint && nHeight < pcheckpoint->
nHeight) {
3998 LogPrintf(
"ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__,
nHeight);
4031 const int nHeight = pindexPrev ==
nullptr ? 0 : pindexPrev->
nHeight + 1;
4034 bool enforce_locktime_median_time_past{
false};
4036 assert(pindexPrev !=
nullptr);
4037 enforce_locktime_median_time_past =
true;
4040 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
4045 for (
const auto& tx : block.
vtx) {
4055 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4056 !std::equal(
expect.begin(),
expect.end(), block.
vtx[0]->vin[0].scriptSig.begin())) {
4092 BlockMap::iterator miSelf{
m_blockman.m_block_index.find(hash)};
4094 if (miSelf !=
m_blockman.m_block_index.end()) {
4118 pindexPrev = &((*mi).second);
4153 if (pindexPrev->
GetAncestor(failedit->nHeight) == failedit) {
4156 while (invalid_walk != failedit) {
4159 invalid_walk = invalid_walk->
pprev;
4167 if (!min_pow_checked) {
4185 "Saw new header hash=%s height=%d", hash.
ToString(), pindex->
nHeight);
4219 const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};
4220 LogPrintf(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
4237 auto now = std::chrono::steady_clock::now();
4238 if (now < m_last_presync_update + std::chrono::milliseconds{250})
return;
4239 m_last_presync_update = now;
4243 if (initial_download) {
4245 const double progress{100.0 * height / (height + blocks_left)};
4246 LogPrintf(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
4253 const CBlock& block = *pblock;
4255 if (fNewBlock) *fNewBlock =
false;
4259 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
4261 bool accepted_header{
AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4264 if (!accepted_header)
4288 if (fAlreadyHave)
return true;
4290 if (pindex->
nTx != 0)
return true;
4291 if (!fHasMoreOrSameWork)
return true;
4292 if (fTooFarAhead)
return true;
4303 if (!
CheckBlock(block, state, params.GetConsensus()) ||
4320 if (fNewBlock) *fNewBlock =
true;
4323 if (blockPos.IsNull()) {
4324 state.
Error(
strprintf(
"%s: Failed to find position to write new block to disk", __func__));
4328 }
catch (
const std::runtime_error& e) {
4352 if (new_block) *new_block =
false;
4367 ret =
AcceptBlock(block, state, &pindex, force_processing,
nullptr, new_block, min_pow_checked);
4382 LogError(
"%s: ActivateBestChain failed (%s)\n", __func__, state.
ToString());
4388 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
4389 LogError(
"%s: [background] ActivateBestChain failed (%s)\n", __func__, bg_state.
ToString());
4416 bool fCheckMerkleRoot)
4423 indexDummy.
pprev = pindexPrev;
4429 LogError(
"%s: Consensus::ContextualCheckBlockHeader: %s\n", __func__, state.
ToString());
4437 LogError(
"%s: Consensus::ContextualCheckBlock: %s\n", __func__, state.
ToString());
4440 if (!chainstate.
ConnectBlock(block, state, &indexDummy, viewNew,
true)) {
4452 if (!active_chainstate.FlushStateToDisk(
4478 LogPrintf(
"Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
4501 int nCheckLevel,
int nCheckDepth)
4510 if (nCheckDepth <= 0 || nCheckDepth > chainstate.
m_chain.
Height()) {
4513 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4514 LogPrintf(
"Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4518 int nGoodTransactions = 0;
4521 bool skipped_no_block_data{
false};
4522 bool skipped_l3_checks{
false};
4523 LogPrintf(
"Verification progress: 0%%\n");
4528 const int percentageDone = std::max(1, std::min(99, (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (
double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4529 if (reportDone < percentageDone / 10) {
4531 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
4532 reportDone = percentageDone / 10;
4541 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);
4542 skipped_no_block_data =
true;
4552 if (nCheckLevel >= 1 && !
CheckBlock(block, state, consensus_params)) {
4553 LogPrintf(
"Verification error: found bad block at %d, hash=%s (%s)\n",
4558 if (nCheckLevel >= 2 && pindex) {
4570 if (nCheckLevel >= 3) {
4579 nGoodTransactions = 0;
4580 pindexFailure = pindex;
4582 nGoodTransactions += block.
vtx.size();
4585 skipped_l3_checks =
true;
4590 if (pindexFailure) {
4591 LogPrintf(
"Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.
m_chain.
Height() - pindexFailure->
nHeight + 1, nGoodTransactions);
4594 if (skipped_l3_checks) {
4595 LogPrintf(
"Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.\n");
4602 if (nCheckLevel >= 4 && !skipped_l3_checks) {
4604 const int percentageDone = std::max(1, std::min(99, 100 - (
int)(((
double)(chainstate.
m_chain.
Height() - pindex->
nHeight)) / (
double)nCheckDepth * 50)));
4605 if (reportDone < percentageDone / 10) {
4607 LogPrintf(
"Verification progress: %d%%\n", percentageDone);
4608 reportDone = percentageDone / 10;
4617 if (!chainstate.
ConnectBlock(block, state, pindex, coins)) {
4625 LogPrintf(
"Verification: No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
4627 if (skipped_l3_checks) {
4630 if (skipped_no_block_data) {
4648 if (!tx->IsCoinBase()) {
4649 for (
const CTxIn &txin : tx->vin) {
4667 if (hashHeads.empty())
return true;
4668 if (hashHeads.size() != 2) {
4669 LogError(
"ReplayBlocks(): unknown inconsistent state\n");
4680 if (
m_blockman.m_block_index.count(hashHeads[0]) == 0) {
4681 LogError(
"ReplayBlocks(): reorganization to unknown block requested\n");
4684 pindexNew = &(
m_blockman.m_block_index[hashHeads[0]]);
4686 if (!hashHeads[1].IsNull()) {
4687 if (
m_blockman.m_block_index.count(hashHeads[1]) == 0) {
4688 LogError(
"ReplayBlocks(): reorganization from unknown block requested\n");
4691 pindexOld = &(
m_blockman.m_block_index[hashHeads[1]]);
4693 assert(pindexFork !=
nullptr);
4697 while (pindexOld != pindexFork) {
4715 pindexOld = pindexOld->
pprev;
4719 int nForkHeight = pindexFork ? pindexFork->
nHeight : 0;
4746 block = block->pprev;
4752 void Chainstate::ClearBlockIndexCandidates()
4765 if (!
ret)
return false;
4767 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
4769 std::vector<CBlockIndex*> vSortedByHeight{
m_blockman.GetAllBlockIndices()};
4770 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4780 if (pindex == GetSnapshotBaseBlock() ||
4785 chainstate->TryAddBlockIndexCandidate(pindex);
4789 m_best_invalid = pindex;
4792 m_best_header = pindex;
4795 needs_init =
m_blockman.m_block_index.empty();
4805 LogPrintf(
"Initializing databases...\n");
4820 if (
m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
4824 const CBlock& block = params.GenesisBlock();
4826 if (blockPos.IsNull()) {
4827 LogError(
"%s: writing genesis block to disk failed\n", __func__);
4832 }
catch (
const std::runtime_error& e) {
4833 LogError(
"%s: failed to write genesis block: %s\n", __func__, e.what());
4843 std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
4846 assert(!dbp == !blocks_with_unknown_parent);
4848 const auto start{SteadyClock::now()};
4856 uint64_t nRewind = blkdat.GetPos();
4857 while (!blkdat.eof()) {
4860 blkdat.SetPos(nRewind);
4863 unsigned int nSize = 0;
4867 blkdat.FindByte(std::byte(params.MessageStart()[0]));
4868 nRewind = blkdat.GetPos() + 1;
4870 if (buf != params.MessageStart()) {
4877 }
catch (
const std::exception&) {
4884 const uint64_t nBlockPos{blkdat.GetPos()};
4886 dbp->
nPos = nBlockPos;
4887 blkdat.SetLimit(nBlockPos + nSize);
4893 nRewind = nBlockPos + nSize;
4894 blkdat.SkipTo(nRewind);
4896 std::shared_ptr<CBlock> pblock{};
4904 if (dbp && blocks_with_unknown_parent) {
4905 blocks_with_unknown_parent->emplace(header.
hashPrevBlock, *dbp);
4914 blkdat.SetPos(nBlockPos);
4915 pblock = std::make_shared<CBlock>();
4917 nRewind = blkdat.GetPos();
4920 if (
AcceptBlock(pblock, state,
nullptr,
true, dbp,
nullptr,
true)) {
4926 }
else if (hash != params.GetConsensus().hashGenesisBlock && pindex->
nHeight % 1000 == 0) {
4932 if (hash == params.GetConsensus().hashGenesisBlock) {
4933 bool genesis_activation_failure =
false;
4934 for (
auto c :
GetAll()) {
4936 if (!c->ActivateBestChain(state,
nullptr)) {
4937 genesis_activation_failure =
true;
4941 if (genesis_activation_failure) {
4954 bool activation_failure =
false;
4955 for (
auto c :
GetAll()) {
4957 if (!c->ActivateBestChain(state, pblock)) {
4959 activation_failure =
true;
4963 if (activation_failure) {
4970 if (!blocks_with_unknown_parent)
continue;
4973 std::deque<uint256> queue;
4974 queue.push_back(hash);
4975 while (!queue.empty()) {
4978 auto range = blocks_with_unknown_parent->equal_range(head);
4979 while (range.first != range.second) {
4980 std::multimap<uint256, FlatFilePos>::iterator it = range.first;
4981 std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
4983 LogPrint(
BCLog::REINDEX,
"%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
4987 if (
AcceptBlock(pblockrecursive, dummy,
nullptr,
true, &it->second,
nullptr,
true)) {
4989 queue.push_back(pblockrecursive->GetHash());
4993 blocks_with_unknown_parent->erase(it);
4997 }
catch (
const std::exception& e) {
5009 LogPrint(
BCLog::REINDEX,
"%s: unexpected data at file offset 0x%x - %s. continuing\n", __func__, (nRewind - 1), e.what());
5012 }
catch (
const std::runtime_error& e) {
5015 LogPrintf(
"Loaded %i blocks from external file in %dms\n", nLoaded, Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
5035 std::multimap<CBlockIndex*,CBlockIndex*> forward;
5036 for (
auto& [
_, block_index] :
m_blockman.m_block_index) {
5037 forward.emplace(block_index.pprev, &block_index);
5042 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(
nullptr);
5044 rangeGenesis.first++;
5045 assert(rangeGenesis.first == rangeGenesis.second);
5056 CBlockIndex* pindexFirstNotTransactionsValid =
nullptr;
5058 CBlockIndex* pindexFirstNotScriptsValid =
nullptr;
5060 while (pindex !=
nullptr) {
5062 if (pindexFirstAssumeValid ==
nullptr && pindex->nStatus &
BLOCK_ASSUMED_VALID) pindexFirstAssumeValid = pindex;
5063 if (pindexFirstInvalid ==
nullptr && pindex->nStatus &
BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
5064 if (pindexFirstMissing ==
nullptr && !(pindex->nStatus &
BLOCK_HAVE_DATA)) {
5065 pindexFirstMissing = pindex;
5067 if (pindexFirstNeverProcessed ==
nullptr && pindex->
nTx == 0) pindexFirstNeverProcessed = pindex;
5074 if (pindexFirstNotTransactionsValid ==
nullptr &&
5076 pindexFirstNotTransactionsValid = pindex;
5079 if (pindexFirstNotChainValid ==
nullptr &&
5081 pindexFirstNotChainValid = pindex;
5084 if (pindexFirstNotScriptsValid ==
nullptr &&
5086 pindexFirstNotScriptsValid = pindex;
5091 if (pindex->
pprev ==
nullptr) {
5094 for (
auto c :
GetAll()) {
5095 if (c->m_chain.Genesis() !=
nullptr) {
5096 assert(pindex == c->m_chain.Genesis());
5108 if (pindexFirstAssumeValid ==
nullptr) {
5113 assert(pindexFirstMissing == pindexFirstNeverProcessed);
5136 assert(pindexFirstNotTreeValid ==
nullptr);
5140 if (pindexFirstInvalid ==
nullptr) {
5150 || (pindex->
nChainTx == 0 && prev_chain_tx == 0 && pindex->
pprev)
5154 for (
auto c :
GetAll()) {
5155 if (c->m_chain.Tip() ==
nullptr)
continue;
5157 if (pindexFirstInvalid ==
nullptr) {
5164 if ((pindexFirstMissing ==
nullptr || pindex == c->m_chain.Tip())) {
5168 if (is_active || GetSnapshotBaseBlock()->GetAncestor(pindex->
nHeight) == pindex) {
5169 assert(c->setBlockIndexCandidates.count(pindex));
5177 assert(c->setBlockIndexCandidates.count(pindex) == 0);
5181 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked =
m_blockman.
m_blocks_unlinked.equal_range(pindex->
pprev);
5182 bool foundInUnlinked =
false;
5183 while (rangeUnlinked.first != rangeUnlinked.second) {
5184 assert(rangeUnlinked.first->first == pindex->
pprev);
5185 if (rangeUnlinked.first->second == pindex) {
5186 foundInUnlinked =
true;
5189 rangeUnlinked.first++;
5191 if (pindex->
pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed !=
nullptr && pindexFirstInvalid ==
nullptr) {
5196 if (pindexFirstMissing ==
nullptr)
assert(!foundInUnlinked);
5197 if (pindex->
pprev && (pindex->nStatus &
BLOCK_HAVE_DATA) && pindexFirstNeverProcessed ==
nullptr && pindexFirstMissing !=
nullptr) {
5208 for (
auto c :
GetAll()) {
5211 if (pindexFirstInvalid ==
nullptr) {
5212 if (is_active || GetSnapshotBaseBlock()->GetAncestor(pindex->
nHeight) == pindex) {
5223 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
5224 if (range.first != range.second) {
5226 pindex = range.first->second;
5235 if (pindex == pindexFirstInvalid) pindexFirstInvalid =
nullptr;
5236 if (pindex == pindexFirstMissing) pindexFirstMissing =
nullptr;
5237 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed =
nullptr;
5238 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid =
nullptr;
5239 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid =
nullptr;
5240 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid =
nullptr;
5241 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid =
nullptr;
5242 if (pindex == pindexFirstAssumeValid) pindexFirstAssumeValid =
nullptr;
5246 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
5247 while (rangePar.first->second != pindex) {
5248 assert(rangePar.first != rangePar.second);
5253 if (rangePar.first != rangePar.second) {
5255 pindex = rangePar.first->second;
5267 assert(nNodes == forward.size());
5274 return strprintf(
"Chainstate [%s] @ height %d (%s)",
5279 bool Chainstate::ResizeCoinsCaches(
size_t coinstip_size,
size_t coinsdb_size)
5292 LogPrintf(
"[%s] resized coinsdb cache to %.1f MiB\n",
5293 this->
ToString(), coinsdb_size * (1.0 / 1024 / 1024));
5294 LogPrintf(
"[%s] resized coinstip cache to %.1f MiB\n",
5295 this->
ToString(), coinstip_size * (1.0 / 1024 / 1024));
5300 if (coinstip_size > old_coinstip_size) {
5313 if (pindex ==
nullptr)
5316 int64_t nNow = time(
nullptr);
5326 return std::min<double>(pindex->
nChainTx / fTxTotal, 1.0);
5332 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
5334 return m_active_chainstate->m_from_snapshot_blockhash;
5336 return std::nullopt;
5342 std::vector<Chainstate*>
out;
5344 for (
Chainstate*
cs : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
5354 assert(!m_ibd_chainstate);
5355 assert(!m_active_chainstate);
5357 m_ibd_chainstate = std::make_unique<Chainstate>(mempool,
m_blockman, *
this);
5358 m_active_chainstate = m_ibd_chainstate.get();
5359 return *m_active_chainstate;
5371 bool existed = fs::remove(base_blockhash_path);
5373 LogPrintf(
"[snapshot] snapshot chainstate dir being removed lacks %s file\n",
5376 }
catch (
const fs::filesystem_error& e) {
5377 LogPrintf(
"[snapshot] failed to remove file %s: %s\n",
5383 LogPrintf(
"Removing leveldb dir at %s\n", path_str);
5387 const bool destroyed =
DestroyDB(path_str);
5390 LogPrintf(
"error: leveldb DestroyDB call failed on %s\n", path_str);
5410 LogPrintf(
"[snapshot] can't activate a snapshot-based chainstate more than once\n");
5416 if (
Assert(m_active_chainstate->GetMempool())->size() > 0) {
5417 LogPrintf(
"[snapshot] can't activate a snapshot when mempool not empty\n");
5422 int64_t current_coinsdb_cache_size{0};
5423 int64_t current_coinstip_cache_size{0};
5431 static constexpr
double IBD_CACHE_PERC = 0.01;
5432 static constexpr
double SNAPSHOT_CACHE_PERC = 0.99;
5444 current_coinsdb_cache_size = this->
ActiveChainstate().m_coinsdb_cache_size_bytes;
5445 current_coinstip_cache_size = this->
ActiveChainstate().m_coinstip_cache_size_bytes;
5450 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
5451 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
5455 return std::make_unique<Chainstate>(
5456 nullptr,
m_blockman, *
this, base_blockhash));
5460 snapshot_chainstate->InitCoinsDB(
5461 static_cast<size_t>(current_coinsdb_cache_size * SNAPSHOT_CACHE_PERC),
5462 in_memory,
false,
"chainstate");
5463 snapshot_chainstate->InitCoinsCache(
5464 static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
5468 LogPrintf(
"[snapshot] activation failed - %s\n", reason);
5469 this->MaybeRebalanceCaches();
5477 snapshot_chainstate.reset();
5481 "Manually remove it before restarting.\n",
fs::PathToString(*snapshot_datadir)));
5489 return cleanup_bad_snapshot(
"population failed");
5498 return cleanup_bad_snapshot(
"work does not exceed active chainstate");
5504 return cleanup_bad_snapshot(
"could not write base blockhash");
5508 assert(!m_snapshot_chainstate);
5509 m_snapshot_chainstate.swap(snapshot_chainstate);
5510 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
5515 Assert(m_active_chainstate->m_mempool->size() == 0);
5516 Assert(!m_snapshot_chainstate->m_mempool);
5517 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5518 m_active_chainstate->m_mempool =
nullptr;
5519 m_active_chainstate = m_snapshot_chainstate.get();
5522 LogPrintf(
"[snapshot] successfully activated snapshot %s\n", base_blockhash.
ToString());
5524 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
5526 this->MaybeRebalanceCaches();
5534 snapshot_loaded ?
"saving snapshot chainstate" :
"flushing coins cache",
5538 coins_cache.
Flush();
5543 const char*
what() const noexcept
override
5545 return "ComputeUTXOStats interrupted.";
5567 if (!snapshot_start_block) {
5570 LogPrintf(
"[snapshot] Did not find snapshot start blockheader %s\n",
5575 int base_height = snapshot_start_block->
nHeight;
5578 if (!maybe_au_data) {
5579 LogPrintf(
"[snapshot] assumeutxo height in snapshot metadata not recognized "
5580 "(%d) - refusing to load snapshot\n", base_height);
5590 LogPrintf(
"[snapshot] activation failed - work does not exceed active chainstate\n");
5599 LogPrintf(
"[snapshot] loading coins from snapshot %s\n", base_blockhash.
ToString());
5600 int64_t coins_processed{0};
5602 while (coins_left > 0) {
5604 coins_file >> outpoint;
5606 }
catch (
const std::ios_base::failure&) {
5607 LogPrintf(
"[snapshot] bad snapshot format or truncated snapshot after deserializing %d coins\n",
5608 coins_count - coins_left);
5611 if (coin.
nHeight > base_height ||
5612 outpoint.
n >= std::numeric_limits<decltype(outpoint.
n)>::max()
5614 LogPrintf(
"[snapshot] bad snapshot data after deserializing %d coins\n",
5615 coins_count - coins_left);
5619 LogPrintf(
"[snapshot] bad snapshot data after deserializing %d coins - bad tx out value\n",
5620 coins_count - coins_left);
5629 if (coins_processed % 1000000 == 0) {
5630 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
5632 static_cast<float>(coins_processed) * 100 /
static_cast<float>(coins_count),
5640 if (coins_processed % 120000 == 0) {
5646 return snapshot_chainstate.GetCoinsCacheSizeState());
5667 bool out_of_coins{
false};
5669 coins_file >> outpoint;
5670 }
catch (
const std::ios_base::failure&) {
5672 out_of_coins =
true;
5674 if (!out_of_coins) {
5675 LogPrintf(
"[snapshot] bad snapshot - coins left over after deserializing %d coins\n",
5680 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
5694 std::optional<CCoinsStats> maybe_stats;
5702 if (!maybe_stats.has_value()) {
5703 LogPrintf(
"[snapshot] failed to generate coins stats\n");
5709 LogPrintf(
"[snapshot] bad snapshot content hash: expected %s, got %s\n",
5726 constexpr
int AFTER_GENESIS_START{1};
5728 for (
int i = AFTER_GENESIS_START; i <= snapshot_chainstate.
m_chain.
Height(); ++i) {
5729 index = snapshot_chainstate.
m_chain[i];
5764 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
5786 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
5787 !this->IsUsable(m_snapshot_chainstate.get()) ||
5788 !this->IsUsable(m_ibd_chainstate.get()) ||
5789 !m_ibd_chainstate->m_chain.Tip()) {
5795 const int snapshot_base_height = *
Assert(this->GetSnapshotBaseHeight());
5798 if (index_new.
nHeight < snapshot_base_height) {
5808 "%s failed to validate the -assumeutxo snapshot state. "
5809 "This indicates a hardware problem, or a bug in the software, or a "
5810 "bad software modification that allowed an invalid snapshot to be "
5811 "loaded. As a result of this, the node will shut down and stop using any "
5812 "state that was built on the snapshot, resetting the chain height "
5813 "from %d to %d. On the next "
5814 "restart, the node will resume syncing from %d "
5815 "without using any snapshot data. "
5816 "Please report this incident to %s, including how you obtained the snapshot. "
5817 "The invalid snapshot chainstate will be left on disk in case it is "
5818 "helpful in diagnosing the issue that caused this error."),
5823 LogPrintf(
"[snapshot] deleting snapshot, reverting to validated chain, and stopping node\n");
5825 m_active_chainstate = m_ibd_chainstate.get();
5826 m_snapshot_chainstate->m_disabled =
true;
5830 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
5831 if (!rename_result) {
5839 LogPrintf(
"[snapshot] supposed base block %s does not match the "
5840 "snapshot base block %s (height %d). Snapshot is not valid.\n",
5841 index_new.
ToString(), snapshot_blockhash.
ToString(), snapshot_base_height);
5842 handle_invalid_snapshot();
5848 int curr_height = m_ibd_chainstate->m_chain.Height();
5850 assert(snapshot_base_height == curr_height);
5855 CCoinsViewDB& ibd_coins_db = m_ibd_chainstate->CoinsDB();
5856 m_ibd_chainstate->ForceFlushStateToDisk();
5859 if (!maybe_au_data) {
5860 LogPrintf(
"[snapshot] assumeutxo data not found for height "
5861 "(%d) - refusing to validate snapshot\n", curr_height);
5862 handle_invalid_snapshot();
5867 std::optional<CCoinsStats> maybe_ibd_stats;
5868 LogPrintf(
"[snapshot] computing UTXO stats for background chainstate to validate "
5869 "snapshot - this could take a few minutes\n");
5872 CoinStatsHashType::HASH_SERIALIZED,
5881 if (!maybe_ibd_stats) {
5882 LogPrintf(
"[snapshot] failed to generate stats for validation coins db\n");
5886 handle_invalid_snapshot();
5889 const auto& ibd_stats = *maybe_ibd_stats;
5898 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
5899 ibd_stats.hashSerialized.ToString(),
5901 handle_invalid_snapshot();
5905 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
5908 m_ibd_chainstate->m_disabled =
true;
5909 this->MaybeRebalanceCaches();
5917 assert(m_active_chainstate);
5918 return *m_active_chainstate;
5924 return m_snapshot_chainstate && m_active_chainstate == m_snapshot_chainstate.get();
5927 void ChainstateManager::MaybeRebalanceCaches()
5930 bool ibd_usable = this->
IsUsable(m_ibd_chainstate.get());
5931 bool snapshot_usable = this->
IsUsable(m_snapshot_chainstate.get());
5932 assert(ibd_usable || snapshot_usable);
5934 if (ibd_usable && !snapshot_usable) {
5939 else if (snapshot_usable && !ibd_usable) {
5941 LogPrintf(
"[snapshot] allocating all cache to the snapshot chainstate\n");
5945 else if (ibd_usable && snapshot_usable) {
5950 m_ibd_chainstate->ResizeCoinsCaches(
5952 m_snapshot_chainstate->ResizeCoinsCaches(
5955 m_snapshot_chainstate->ResizeCoinsCaches(
5957 m_ibd_chainstate->ResizeCoinsCaches(
5963 void ChainstateManager::ResetChainstates()
5965 m_ibd_chainstate.reset();
5966 m_snapshot_chainstate.reset();
5967 m_active_chainstate =
nullptr;
5977 if (!opts.check_block_index.has_value()) opts.
check_block_index = opts.chainparams.DefaultConsistencyChecks();
5978 if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work =
UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork);
5979 if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
5980 return std::move(opts);
5984 : m_script_check_queue{128, options.worker_threads_num},
5985 m_interrupt{interrupt},
5987 m_blockman{interrupt,
std::move(blockman_options)}
5998 bool ChainstateManager::DetectSnapshotChainstate()
6000 assert(!m_snapshot_chainstate);
6006 if (!base_blockhash) {
6009 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
6012 this->ActivateExistingSnapshot(*base_blockhash);
6018 assert(!m_snapshot_chainstate);
6019 m_snapshot_chainstate =
6020 std::make_unique<Chainstate>(
nullptr,
m_blockman, *
this, base_blockhash);
6021 LogPrintf(
"[snapshot] switching active chainstate to %s\n", m_snapshot_chainstate->ToString());
6024 Assert(m_active_chainstate->m_mempool->size() == 0);
6025 Assert(!m_snapshot_chainstate->m_mempool);
6026 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
6027 m_active_chainstate->m_mempool =
nullptr;
6028 m_active_chainstate = m_snapshot_chainstate.get();
6029 return *m_snapshot_chainstate;
6034 return (block_index.
nHeight==91842 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
6035 (block_index.
nHeight==91880 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721"));
6040 return (block_index.
nHeight==91722 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
6041 (block_index.
nHeight==91812 && block_index.
GetBlockHash() ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"));
6048 assert(
cs.m_from_snapshot_blockhash);
6049 auto storage_path_maybe =
cs.CoinsDB().StoragePath();
6051 assert(storage_path_maybe);
6052 return *storage_path_maybe;
6062 auto invalid_path = snapshot_datadir +
"_INVALID";
6065 LogPrintf(
"[snapshot] renaming snapshot datadir %s to %s\n", dbpath, target);
6071 fs::rename(snapshot_datadir, invalid_path);
6072 }
catch (
const fs::filesystem_error& e) {
6076 LogPrintf(
"%s: error renaming file '%s' -> '%s': %s\n",
6077 __func__, src_str, dest_str, e.what());
6079 "Rename of '%s' -> '%s' failed. "
6080 "You should resolve this by manually moving or deleting the invalid "
6081 "snapshot directory %s, otherwise you will encounter the same error again "
6082 "on the next startup."),
6083 src_str, dest_str, src_str)};
6088 bool ChainstateManager::DeleteSnapshotChainstate()
6091 Assert(m_snapshot_chainstate);
6092 Assert(m_ibd_chainstate);
6096 LogPrintf(
"Deletion of %s failed. Please remove it manually to continue reindexing.\n",
6100 m_active_chainstate = m_ibd_chainstate.get();
6101 m_snapshot_chainstate.reset();
6115 const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock()
const
6117 return m_active_chainstate ? m_active_chainstate->SnapshotBase() :
nullptr;
6120 std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const
6122 const CBlockIndex* base = this->GetSnapshotBaseBlock();
6123 return base ? std::make_optional(base->
nHeight) :
std::nullopt;
6126 bool ChainstateManager::ValidatedSnapshotCleanup()
6130 if (!(chainstate && chainstate->HasCoinsViews())) {
6133 return chainstate->CoinsDB().StoragePath();
6135 std::optional<fs::path> ibd_chainstate_path_maybe = get_storage_path(m_ibd_chainstate);
6136 std::optional<fs::path> snapshot_chainstate_path_maybe = get_storage_path(m_snapshot_chainstate);
6145 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
6146 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with "
6147 "in-memory chainstates. You are testing, right?\n");
6151 const auto& snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
6152 const auto& ibd_chainstate_path = *ibd_chainstate_path_maybe;
6160 this->ResetChainstates();
6165 LogPrintf(
"[snapshot] deleting background chainstate directory (now unnecessary) (%s)\n",
6168 fs::path tmp_old{ibd_chainstate_path +
"_todelete"};
6170 auto rename_failed_abort = [
this](
6173 const fs::filesystem_error& err) {
6174 LogPrintf(
"Error renaming path (%s) -> (%s): %s\n",
6177 "Rename of '%s' -> '%s' failed. "
6178 "Cannot clean up the background chainstate leveldb directory.",
6183 fs::rename(ibd_chainstate_path, tmp_old);
6184 }
catch (
const fs::filesystem_error& e) {
6185 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
6189 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to "
6190 "default chainstate directory (%s)\n",
6194 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
6195 }
catch (
const fs::filesystem_error& e) {
6196 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
6203 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the "
6204 "directory is now unnecessary.\n",
6207 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
6213 Chainstate& ChainstateManager::GetChainstateForIndexing()
6218 return (this->
GetAll().size() > 1) ? *m_ibd_chainstate : *m_active_chainstate;
6221 std::pair<int, int> ChainstateManager::GetPruneRange(
const Chainstate& chainstate,
int last_height_can_prune)
6228 if (this->
GetAll().size() > 1 && m_snapshot_chainstate.get() == &chainstate) {
6231 prune_start = *
Assert(GetSnapshotBaseHeight()) + 1;
6234 int max_prune = std::max<int>(
6243 int prune_end = std::min(last_height_can_prune, max_prune);
6245 return {prune_start, prune_end};
bool MoneyRange(const CAmount &nValue)
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
arith_uint256 UintToArith256(const uint256 &a)
#define PACKAGE_BUGREPORT
arith_uint256 GetBlockProof(const CBlockIndex &block)
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
Return the time it would take to redo the work difference between from and to, assuming the current h...
@ BLOCK_VALID_CHAIN
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends,...
@ BLOCK_VALID_MASK
All validity bits.
@ BLOCK_ASSUMED_VALID
If ASSUMED_VALID is set, it means that this block has not been validated and has validity status less...
@ BLOCK_VALID_TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
@ BLOCK_VALID_SCRIPTS
Scripts & signatures ok. Implies all parents are either at least VALID_SCRIPTS, or are ASSUMED_VALID.
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
@ BLOCK_HAVE_UNDO
undo data available in rev*.dat
@ BLOCK_HAVE_DATA
full block available in blk*.dat
@ BLOCK_FAILED_CHILD
descends from failed block
@ BLOCK_FAILED_VALID
stage after last reached validness failed
@ BLOCK_OPT_WITNESS
block data in blk*.dat was received with a witness-enforcing client
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
Abstract class that implements BIP9-style threshold logic, and caches results.
Non-refcounted RAII wrapper for FILE*.
std::string ToString() const
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
bool m_checked_merkle_root
std::vector< CTransactionRef > vtx
bool m_checked_witness_commitment
The block chain is a tree shaped structure starting with the genesis block at the root,...
std::string ToString() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
bool HaveNumChainTxs() const
Check whether this block's and all previous blocks' transactions have been downloaded (and stored to ...
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
uint256 GetBlockHash() const
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
bool RaiseValidity(enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
unsigned int nTx
Number of transactions in this block.
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.
int32_t nVersion
block header
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
int nHeight
height of the entry in the chain. The genesis block has height 0
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
An in-memory indexed chain of blocks.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
int Height() const
Return the maximal height in the chain.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip in of this chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const ChainTxData & TxData() const
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
const Consensus::Params & GetConsensus() const
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
void Add(std::vector< T > &&vChecks)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SetBestBlock(const uint256 &hashBlock)
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
bool Flush()
Push the modifications applied to this cache to its base and wipe local state.
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
CCoinsView backed by the coin database (chainstate/)
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Abstract view on the open txout dataset.
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
CCoinsView that brings transactions from a mempool into view.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
CHash256 & Write(Span< const unsigned char > input)
void Finalize(Span< unsigned char > output)
An outpoint - a combination of a transaction hash and an index n into its vout.
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
Closure representing one script verification Note that this stores references to the spending transac...
ScriptError GetScriptError() const
PrecomputedTransactionData * txdata
const CTransaction * ptxTo
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
const Txid & GetHash() const LIFETIMEBOUND
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
const std::vector< CTxOut > vout
const std::vector< CTxIn > vin
An input of a transaction.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
void AddTransactionsUpdated(unsigned int n)
const int64_t m_max_size_bytes
CTransactionRef get(const uint256 &hash) const
size_t DynamicMemoryUsage() const
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...
std::set< txiter, CompareIteratorByHash > setEntries
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
bool exists(const GenTxid >xid) const
An output of a transaction.
Undo information for a CTransaction.
std::vector< Coin > vprevout
VerifyDBResult VerifyDB(Chainstate &chainstate, const Consensus::Params &consensus_params, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::Notifications & m_notifications
CVerifyDB(kernel::Notifications ¬ifications)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
CTxMemPool * GetMempool()
Mutex m_chainstate_mutex
The ChainState Mutex A lock that must be held when modifying this ChainState - held in ActivateBestCh...
CChain m_chain
The current chain of blockheaders we consult and build on.
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
bool 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.
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
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.
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
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.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
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...
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.
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
void TryAddBlockIndexCandidate(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
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...
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.
SteadyClock::time_point m_last_flush
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< uint256 > from_snapshot_blockhash=std::nullopt)
bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Whether the chain state needs to be redownloaded due to lack of witness data.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
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...
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
const CChainParams & GetParams() const
const arith_uint256 & MinimumChainWork() const
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...
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
bool ShouldCheckBlockIndex() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
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.
bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
The state of a background sync (for net processing)
std::atomic< bool > m_cached_finished_ibd
Whether initial block download has ended and IsInitialBlockDownload should return false from now on.
std::function< void()> restart_indexes
Function to restart active indexes; set dynamically to avoid a circular dependency on base/index....
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
const util::SignalInterrupt & m_interrupt
void LoadExternalBlockFile(AutoFile &file_in, FlatFilePos *dbp=nullptr, std::multimap< uint256, FlatFilePos > *blocks_with_unknown_parent=nullptr)
Import blocks from an external file.
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
VersionBitsCache m_versionbitscache
Track versionbit status.
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool IsSnapshotActive() const
ChainstateManager(const util::SignalInterrupt &interrupt, Options options, node::BlockManager::Options blockman_options)
void CheckBlockIndex()
Make various assertions about the state of the block index.
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
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...
std::optional< uint256 > SnapshotBlockhash() const
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...
const uint256 & AssumedValidBlock() const
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 ...
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
std::set< CBlockIndex * > m_failed_blocks
In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to conn...
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.
kernel::Notifications & GetNotifications() const
bool IsUsable(const Chainstate *const cs) const EXCLUSIVE_LOCKS_REQUIRED(
Return true if a chainstate is considered usable.
const Consensus::Params & GetConsensus() const
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
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).
CTxOut out
unspent transaction output
bool IsSpent() const
Either this coin never existed (see e.g.
uint32_t nHeight
at which height this containing transaction was included in the active block chain
unsigned int fCoinBase
whether containing transaction was a coinbase
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
Used to track blocks whose transactions were applied to the UTXO state as a part of a single Activate...
std::vector< PerBlockConnectTrace > blocksConnected
std::vector< PerBlockConnectTrace > & GetBlocksConnected()
void BlockConnected(CBlockIndex *pindex, std::shared_ptr< const CBlock > pblock)
cache implements a cache with properties similar to a cuckoo-set.
DisconnectedBlockTransactions.
std::list< CTransactionRef > take()
Clear all data structures and return the list of transactions.
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Remove any entries that are in this block.
std::vector< CTransactionRef > AddTransactionsFromBlock(const std::vector< CTransactionRef > &vtx)
Add transactions from the block, iterating through vtx in reverse order.
static GenTxid Wtxid(const uint256 &hash)
static GenTxid Txid(const uint256 &hash)
Different type to mark Mutex at global scope.
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
void BlockChecked(const CBlock &, const BlockValidationState &)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
std::string GetRejectReason() const
std::string GetDebugMessage() const
bool Error(const std::string &reject_reason)
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
std::string ToString() const
int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Determine what nVersion a new block should use.
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Threshold condition checker that triggers when unknown versionbits are seen on the network.
int64_t EndTime(const Consensus::Params ¶ms) const override
bool Condition(const CBlockIndex *pindex, const Consensus::Params ¶ms) const override
int Threshold(const Consensus::Params ¶ms) const override
int64_t BeginTime(const Consensus::Params ¶ms) const override
int Period(const Consensus::Params ¶ms) const override
WarningBitsConditionChecker(const ChainstateManager &chainman, int bit)
const ChainstateManager & m_chainman
256-bit unsigned big integer.
constexpr bool IsNull() const
std::string ToString() const
constexpr const unsigned char * data() const
constexpr unsigned char * begin()
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
A base class defining functions for notifying about certain kernel events.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
virtual void fatalError(const std::string &debug_message, const bilingual_str &user_message={})
The fatal error notification is sent to notify the user when an error occurs in kernel code that can'...
virtual void warning(const bilingual_str &warning)
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex &index)
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
const kernel::BlockManagerOpts m_opts
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
RecursiveMutex cs_LastBlockFile
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, const FlatFilePos *dbp)
Store block on disk.
bool FlushChainstateBlockFile(int tip_height)
void FindFilesToPrune(std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
Prune block and undo files (blk???.dat and rev???.dat) so that the disk space used is less than a use...
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool LoadingBlocks() const
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
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.
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
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.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
bool IsPruneMode() const
Whether running in -prune mode.
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain, ChainstateManager &chainman)
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
std::string ToString() const
constexpr const std::byte * begin() const
std::string GetHex() const
constexpr const std::byte * data() const
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction.
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
static int64_t GetBlockWeight(const CBlock &block)
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/RBF/etc limits
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_INPUTS_NOT_STANDARD
inputs (covered by txid) failed policy rules
@ TX_WITNESS_STRIPPED
Transaction is missing a witness.
@ TX_CONFLICT
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_WITNESS_MUTATED
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_CONSENSUS
invalid by consensus rules
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits)
static const int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule)
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
static const int WITNESS_SCALE_FACTOR
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool DestroyDB(const std::string &path_str)
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 DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache &versionbitscache)
Determine if a deployment is active for this block.
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SCRIPT_VERIFY_NULLDUMMY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
#define LogPrintLevel(category, level,...)
#define LogPrint(category,...)
#define LogDebug(category,...)
@ REPLACED
Removed for replacement.
@ REORG
Removed for reorganization.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
std::array< uint8_t, 4 > MessageStartChars
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
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 exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
std::string get_filesystem_error_message(const fs::filesystem_error &e)
std::function< FILE *(const fs::path &, const char *)> FopenFn
bool IsInterrupted(const T &result)
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 fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
std::unordered_map< uint256, CBlockIndex, BlockHasher > BlockMap
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > constexpr ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
std::atomic_bool fReindex
bilingual_str ErrorString(const Result< T > &result)
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.
std::shared_ptr< Chain::Notifications > m_notifications
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
bool IsWellFormedPackage(const Package &txns, PackageValidationState &state, bool require_sorted)
Context-free package policy checks:
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
@ PCKG_MEMPOOL_ERROR
Mempool logic error.
@ PCKG_TX
At least one tx is invalid.
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...
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...
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::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)...
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.
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
static constexpr 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 constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
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 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
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS
For convenience, standard but not mandatory verify flags.
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
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.
static constexpr TransactionSerParams TX_NO_WITNESS
static constexpr TransactionSerParams TX_WITH_WITNESS
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
reverse_range< T > reverse_iterate(T &x)
std::string ScriptErrorString(const ScriptError serror)
size_t GetSerializeSize(const T &t)
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
unsigned char * UCharCast(char *c)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Holds configuration for use during UTXO snapshot load and validation.
AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
unsigned int nChainTx
Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< uint256 > vHave
A mutable version of CTransaction.
std::vector< CTxOut > vout
Holds various statistics on transactions within a chain.
double dTxRate
estimated number of transactions per second after that timestamp
int64_t nTime
UNIX timestamp of last known number of transactions.
int64_t nTxCount
total number of transactions between genesis and that timestamp
User-controlled performance and debug options.
Parameters that influence chain consensus.
uint32_t nMinerConfirmationWindow
std::map< uint256, uint32_t > script_flag_exceptions
Hashes of blocks that.
bool signet_blocks
If true, witness commitments contain a payload equal to a Bitcoin Script solution to the signet chall...
int nSubsidyHalvingInterval
int MinBIP9WarningHeight
Don't warn about unknown BIP 9 activations below this height.
uint32_t nRuleChangeActivationThreshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,...
int64_t nPowTargetSpacing
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
const TxValidationState m_state
Contains information about why the transaction failed.
static MempoolAcceptResult MempoolTxDifferentWitness(const uint256 &other_wtxid)
@ INVALID
Fully validated, valid.
static MempoolAcceptResult Failure(TxValidationState state)
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees)
static MempoolAcceptResult Success(std::list< CTransactionRef > &&replaced_txns, int64_t vsize, CAmount fees, CFeeRate effective_feerate, const std::vector< Wtxid > &wtxids_fee_calculations)
static time_point now() noexcept
Return current system time or mocked time, if set.
Validation result for package mempool acceptance.
std::shared_ptr< const CBlock > pblock
PerBlockConnectTrace()=default
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
std::vector< CTxOut > m_spent_outputs
const char * what() const noexcept override
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
const fs::path blocks_dir
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
std::optional< bool > check_block_index
ValidationSignals * signals
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
const CChainParams & chainparams
CoinsViewOptions coins_view
Options struct containing limit options for a CTxMemPool.
int64_t descendant_count
The maximum allowed number of transactions in a package including the entry and its descendants.
int64_t descendant_size_vbytes
The maximum allowed size in virtual bytes of an entry and its descendants within a package.
int64_t ancestor_count
The maximum allowed number of transactions in a package including the entry and its ancestors.
int64_t ancestor_size_vbytes
The maximum allowed size in virtual bytes of an entry and its ancestors within a package.
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define LOCKS_EXCLUDED(...)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t count_seconds(std::chrono::seconds t)
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
#define TRACE6(context, event, a, b, c, d, e, f)
#define TRACE5(context, event, a, b, c, d, e)
#define TRACE2(context, event, a, b)
#define TRACE7(context, event, a, b, c, d, e, f, g)
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Calculates the block height and previous block's median time past at which the transaction will be co...
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, uint32_t flags)
Compute total signature operation cost of a transaction.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
bool 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 TestLockPointValidity(CChain &active_chain, const LockPoints &lp)
Test whether the LockPoints height and time are still valid on the current chain.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0....
uint256 uint256S(const char *str)
bool SignalsOptInRBF(const CTransaction &tx)
Check whether the sequence numbers on this transaction are signaling opt-in to replace-by-fee,...
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.
std::optional< std::pair< std::string, CTransactionRef > > 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.
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(
static bool NotifyHeaderTip(ChainstateManager &chainman) LOCKS_EXCLUDED(cs_main)
static CSHA256 g_scriptExecutionCacheHasher
static void LimitMempoolSize(CTxMemPool &pool, CCoinsViewCache &coins_cache) EXCLUSIVE_LOCKS_REQUIRED(
static int64_t num_blocks_total
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).
static unsigned int GetBlockScriptFlags(const CBlockIndex &block_index, const ChainstateManager &chainman)
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept, std::optional< CFeeRate > client_maxfeerate)
Validate (and maybe submit) a package to the mempool.
bool FatalError(Notifications ¬ifications, BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage)
GlobalMutex g_best_block_mutex
static SteadyClock::duration time_connect_total
std::condition_variable g_best_block_cv
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.
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...
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
static SynchronizationState GetSynchronizationState(bool init)
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static bool ContextualCheckBlock(const CBlock &block, BlockValidationState &state, const ChainstateManager &chainman, const CBlockIndex *pindexPrev)
NOTE: This function is not currently invoked by ConnectBlock(), so we should consider upgrade issues ...
bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
static SteadyClock::duration time_post_connect
static SteadyClock::duration time_chainstate
bool InitScriptExecutionCache(size_t max_size_bytes)
Initializes the script-execution cache.
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL
Time to wait between flushing chainstate to disk.
static SteadyClock::duration time_forks
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.
static bool CheckWitnessMalleation(const CBlock &block, bool expect_witness_commitment, BlockValidationState &state)
CheckWitnessMalleation performs checks for block malleation with regard to its witnesses.
static SteadyClock::duration time_verify
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
static void AppendWarning(bilingual_str &res, const bilingual_str &warn)
Private helper function that concatenates warning messages.
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static SteadyClock::duration time_check
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
bool IsBIP30Unspendable(const CBlockIndex &block_index)
Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30)
static SteadyClock::duration time_index
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 SteadyClock::duration time_connect
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE
Maximum age of our tip for us to be considered current for fee estimation.
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
static SteadyClock::duration time_undo
for(const CTxIn &txin :tx.vin)
static CuckooCache::cache< uint256, SignatureCacheHasher > g_scriptExecutionCache
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)
static bool IsCurrentForFeeEstimation(Chainstate &active_chainstate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
static SteadyClock::duration time_total
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW=true)
arith_uint256 CalculateClaimedHeadersWork(const std::vector< CBlockHeader > &headers)
Return the sum of the claimed work on a given set of headers.
bool IsBIP30Repeat(const CBlockIndex &block_index)
Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30)
static SteadyClock::duration time_flush
static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt &interrupt)
static fs::path GetSnapshotCoinsDBPath(Chainstate &cs) EXCLUSIVE_LOCKS_REQUIRED(
uint256 g_best_block
Used to notify getblocktemplate RPC of new tips.
static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL
Time to wait between writing blocks/block index to disk.
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, const ChainstateManager &chainman, const CBlockIndex *pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
static void LimitValidationInterfaceQueue(ValidationSignals &signals) LOCKS_EXCLUDED(cs_main)
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
@ BASE_BLOCKHASH_MISMATCH
SynchronizationState
Current sync state passed to tip changed callbacks.
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.
static const int32_t VERSIONBITS_NUM_BITS
Total bits available for versionbits.
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.
static const int32_t VERSIONBITS_TOP_MASK
What bitmask determines whether versionbits is in use.
ThresholdState
BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
void SetfLargeWorkInvalidChainFound(bool flag)