99 "level 0 reads the blocks from disk",
100 "level 1 verifies block validity",
101 "level 2 verifies undo data",
102 "level 3 checks disconnection of tip blocks",
103 "level 4 tries to reconnect the blocks",
104 "each level includes the checks of the previous levels",
121 : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(
true),
122 checkMerkleRoot(
true) {}
164 for (
size_t i = 0; i < tx.
vin.size(); ++i) {
168 LogPrintf(
"ERROR: %s: Missing input %d in transaction \'%s\'\n",
292 : m_pool(mempool), m_view(&m_dummy),
301 const bool m_bypass_limits;
309 std::vector<COutPoint> &m_coins_to_uncache;
310 const bool m_test_accept;
311 const unsigned int m_heightOverride;
317 const bool m_package_submission;
323 const bool m_package_feerates;
467 std::unique_ptr<CTxMemPoolEntry> m_entry;
505 const uint32_t m_next_block_script_verify_flags;
506 int m_sig_checks_standard;
527 bool Finalize(
const ATMPArgs &args, Workspace &
ws)
537 std::map<TxId, MempoolAcceptResult> &
results)
552 return state.Invalid(
554 "mempool min fee not met",
561 return state.Invalid(
563 "min relay fee not met",
580bool MemPoolAccept::PreChecks(ATMPArgs &args, Workspace &
ws) {
585 const TxId &txid =
ws.m_ptx->GetId();
603 if (m_pool.m_require_standard &&
605 m_pool.m_permit_bare_multisig,
606 m_pool.m_dust_relay_feerate, reason)) {
615 *
Assert(m_active_chainstate.m_chain.Tip()),
616 args.m_config.GetChainParams().GetConsensus(), tx,
ctxState)) {
625 if (m_pool.exists(txid)) {
627 "txn-already-in-mempool");
635 "finalized-tx-conflict");
640 "txn-mempool-conflict");
644 m_view.SetBackend(m_viewmempool);
657 if (!m_view.HaveCoin(
txin.prevout)) {
659 for (
size_t out = 0; out < tx.
vout.size(); out++) {
664 "txn-already-known");
671 "bad-txns-inputs-missingorspent");
676 if (!m_view.HaveInputs(tx)) {
678 "bad-txns-inputs-spent");
682 m_view.GetBestBlock();
687 m_view.SetBackend(m_dummy);
689 assert(m_active_chainstate.m_blockman.LookupBlockIndex(
690 m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
699 m_active_chainstate.m_chain.Tip(), m_view, tx)};
710 m_active_chainstate.m_chain.Height() + 1,
717 if (m_pool.m_require_standard &&
720 "bad-txns-nonstandard-inputs");
724 ws.m_modified_fees =
ws.m_base_fees;
725 m_pool.ApplyDelta(txid,
ws.m_modified_fees);
734 ws.m_precomputed_txdata,
ws.m_sig_checks_standard)) {
739 ws.m_entry = std::make_unique<CTxMemPoolEntry>(
744 ws.m_vsize =
ws.m_entry->GetTxVirtualSize();
754 m_pool.m_min_relay_feerate.GetFee(
ws.m_ptx->GetTotalSize())) {
761 m_pool.m_min_relay_feerate.GetFee(nSize)));
774bool MemPoolAccept::ConsensusScriptChecks(
const ATMPArgs &args, Workspace &
ws) {
794 tx, state, m_view, m_pool,
ws.m_next_block_script_verify_flags,
796 m_active_chainstate.CoinsTip())) {
800 LogPrintf(
"BUG! PLEASE REPORT THIS! CheckInputScripts failed against "
801 "latest-block but not STANDARD flags %s, %s\n",
811 "%s: BUG! PLEASE REPORT THIS! SigChecks count differed between "
812 "standard and consensus flags in %s",
818bool MemPoolAccept::Finalize(
const ATMPArgs &args, Workspace &
ws) {
821 const TxId &txid =
ws.m_ptx->GetId();
828 m_pool.addUnchecked(entry);
836 m_pool.LimitSize(m_active_chainstate.CoinsTip());
837 if (!m_pool.exists(txid)) {
852 for (
const CTxIn &input :
ptx->vin) {
861bool MemPoolAccept::SubmitPackage(
862 const ATMPArgs &args, std::vector<Workspace> &
workspaces,
864 std::map<TxId, MempoolAcceptResult> &
results) {
870 [
this](
const auto &
ws) { return !m_pool.exists(ws.m_ptx->GetId()); }));
887 strprintf(
"BUG! PolicyScriptChecks succeeded but "
888 "ConsensusScriptChecks failed: %s",
889 ws.m_ptx->GetId().ToString()));
899 if (!Finalize(args,
ws)) {
905 strprintf(
"BUG! Adding to mempool failed: %s",
906 ws.m_ptx->GetId().ToString()));
912 m_pool.LimitSize(m_active_chainstate.CoinsTip());
918 [](
const auto &
ws) { return ws.m_ptx->GetId(); });
924 args.m_package_feerates
925 ?
ws.m_package_feerate
930 : std::vector<TxId>({
ws.m_ptx->GetId()});
937 std::make_shared<
const std::vector<Coin>>(
939 m_pool.GetAndIncrementSequence());
963 if (
ws.m_state.GetResult() ==
978 const TxId txid =
ptx->GetId();
988 if (
auto it = m_pool.mapNextTx.lower_bound(
COutPoint{txid, 0});
989 it != m_pool.mapNextTx.end() && it->first->GetTxId() == txid) {
990 LogPrintf(
"%s: BUG! PLEASE REPORT THIS! Attempt to add txid %s, but "
991 "its outputs are already spent in the "
995 "txn-child-before-parent");
1002 if (args.m_test_accept) {
1007 if (!Finalize(args,
ws)) {
1020 m_pool.GetAndIncrementSequence());
1027 const std::vector<CTransactionRef> &
txns, ATMPArgs &args) {
1041 [
this](
const auto &tx) {
1043 tx, GetNextBlockScriptFlags(m_active_chainstate.m_chain.Tip(),
1044 m_active_chainstate.m_chainman));
1046 std::map<TxId, MempoolAcceptResult>
results;
1056 "transaction failed");
1066 m_viewmempool.PackageAddTransaction(
ws.m_ptx);
1084 [](
int64_t sum,
auto &
ws) { return sum + ws.m_ptx->GetTotalSize(); });
1087 [](
int64_t sum,
auto &
ws) { return sum + ws.m_vsize; });
1090 [](
Amount sum,
auto &
ws) { return sum + ws.m_modified_fees; });
1096 [](
const auto &
ws) { return ws.m_ptx->GetId(); });
1098 if (args.m_package_feerates &&
1102 "transaction failed");
1114 if (args.m_test_accept &&
1118 args.m_package_feerates
1119 ?
ws.m_package_feerate
1124 : std::vector<TxId>{
ws.m_ptx->GetId()};
1135 if (args.m_test_accept) {
1148MemPoolAccept::AcceptSubPackage(
const std::vector<CTransactionRef> &
subpackage,
1158 ATMPArgs
single_args = ATMPArgs::SingleInPackageAccept(args);
1164 "transaction failed");
1197 for (
const auto &outpoint : m_viewmempool.GetNonBaseCoins()) {
1201 m_view.Uncache(outpoint);
1204 m_viewmempool.Reset();
1229 "package-not-child-with-parents");
1242 [](
const auto &tx) { return tx->GetId(); });
1252 for (
const auto &input :
child->vin) {
1254 args.m_coins_to_uncache.push_back(input.prevout);
1261 m_view.SetBackend(m_active_chainstate.CoinsTip());
1263 const auto &input) {
1265 m_view.HaveCoin(input.prevout);
1267 if (!std::all_of(
child->vin.cbegin(),
child->vin.cend(),
1271 "package-not-child-with-unconfirmed-parents");
1277 m_view.SetBackend(m_dummy);
1291 for (
const auto &tx :
package) {
1292 const auto &txid = tx->
GetId();
1295 if (m_pool.exists(txid)) {
1309 auto iter = m_pool.GetIter(txid);
1312 (*
iter.value())->GetTxSize(),
1313 (*
iter.value())->GetFee()));
1324 assert(m_pool.exists(txid));
1363 m_pool.LimitSize(m_active_chainstate.CoinsTip());
1365 for (
const auto &tx :
package) {
1366 const auto &txid = tx->
GetId();
1378 !m_pool.exists(txid)) {
1380 "transaction failed");
1396 if (!m_pool.exists(tx->
GetId())) {
1398 "transaction failed");
1432 auto args = MemPoolAccept::ATMPArgs::SingleAccept(
1436 .AcceptSingleTransaction(tx, args);
1463 [](
const auto &tx) { return tx != nullptr; }));
1471 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(
1474 .AcceptMultipleTransactions(
package, args);
1476 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(
1479 .AcceptPackage(
package, args);
1513void CoinsViews::InitCache() {
1521 : m_mempool(mempool), m_blockman(
blockman), m_chainman(chainman),
1575 LogPrintf(
"Leaving InitialBlockDownload (latching to false)\n");
1602 std::string warning =
1603 std::string(
"'Warning: Large-work fork detected, forking after "
1610 LogPrintf(
"%s: Warning: Large fork found\n forking the "
1611 "chain at height %d (%s)\n lasting to height %d "
1612 "(%s).\nChain state database corruption likely.\n",
1619 LogPrintf(
"%s: Warning: Found invalid chain at least ~6 blocks "
1620 "longer than our best chain.\nChain state database "
1621 "corruption likely.\n",
1679 LogPrintf(
"%s: invalid block=%s height=%d log2_work=%f date=%s\n",
1686 LogPrintf(
"%s: current best=%s height=%d log2_work=%f date=%s\n",
1688 log(
tip->nChainWork.getdouble()) /
log(2.0),
1698 pindex->nStatus = pindex->nStatus.withFailed();
1714 txundo.vprevout.emplace_back();
1715 bool is_spent =
view.SpendCoin(
txin.prevout, &
txundo.vprevout.back());
1727 const CScript &scriptSig =
ptxTo->vin[
nIn].scriptSig;
1753 std::vector<CScriptCheck> *
pvChecks) {
1768 (pBlockLimitSigChecks &&
1771 "too-many-sigchecks");
1778 for (
size_t i = 0; i < tx.
vin.size(); i++) {
1795 pvChecks->push_back(std::move(check));
1818 strprintf(
"non-mandatory-script-verify-flag (%s)",
1822 scriptError =
check2.GetScriptError();
1834 strprintf(
"mandatory-script-verify-flag-failed (%s)",
1863 if (
view.HaveCoin(out)) {
1868 if (
undo.GetHeight() == 0) {
1906 error(
"DisconnectBlock(): failure reading undo data");
1918 if (
blockUndo.vtxundo.size() + 1 != block.
vtx.size()) {
1919 error(
"DisconnectBlock(): block and undo data inconsistent");
1924 for (
size_t i = 1; i < block.
vtx.size(); i++) {
1927 if (
txundo.vprevout.size() != tx.
vin.size()) {
1928 error(
"DisconnectBlock(): transaction and undo data inconsistent");
1932 for (
size_t j = 0;
j < tx.
vin.size();
j++) {
1945 for (
const auto &
ptx : block.
vtx) {
1952 for (
size_t o = 0;
o < tx.
vout.size();
o++) {
1953 if (tx.
vout[
o].scriptPubKey.IsUnspendable()) {
1959 bool is_spent =
view.SpendCoin(out, &coin);
2103 return AbortNode(state,
"Corrupt block found indicating potential "
2104 "hardware failure; shutting down");
2113 assert(hashPrevBlock ==
view.GetBestBlock());
2136 BlockMap::const_iterator it{
2139 if (it->second.GetAncestor(pindex->
nHeight) == pindex &&
2191 uint256S(
"0x00000000000a4d0a398161ffc163c503763"
2192 "b1f4360639393e0e4c8e300e0caec")) ||
2195 uint256S(
"0x00000000000743f190a18c5577a3c2d2a1f"
2196 "610ae9601ac046a38084ccb7cd721")));
2271 for (
const auto &tx : block.
vtx) {
2272 for (
size_t o = 0;
o < tx->
vout.size();
o++) {
2274 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite "
2321 for (
const auto &
ptx : block.
vtx) {
2324 }
catch (
const std::logic_error &
e) {
2333 LogPrintf(
"ERROR: ConnectBlock(): tried to overwrite transaction\n");
2342 for (
const auto &
ptx : block.
vtx) {
2359 return error(
"%s: Consensus::CheckTxInputs: %s, %s",
__func__,
2366 LogPrintf(
"ERROR: %s: accumulated fee in the block out of range.\n",
2369 "bad-txns-accumulated-fee-outofrange");
2381 for (
size_t j = 0;
j < tx.
vin.size();
j++) {
2386 LogPrintf(
"ERROR: %s: contains a non-BIP68-final transaction\n",
2389 "bad-txns-nonfinal");
2404 std::vector<CScriptCheck>
vChecks;
2417 "ConnectBlock(): CheckInputScripts on %s failed with %s",
2435 " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) "
2436 "[%.2fs (%.2fms/blk)]\n",
2445 LogPrintf(
"ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs "
2458 "blk-bad-inputs",
"parallel script check failed");
2465 " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n",
2562 std::numeric_limits<int>::max()) {
2600 "prunedblockfiles",
true);
2635 return AbortNode(state,
"Disk space is too low!",
2636 _(
"Disk space is too low!"));
2655 state,
"Failed to write to block index database");
2672 strprintf(
"write coins cache to disk (%d coins, %.2fkB)",
2683 48 * 2 * 2 *
CoinsTip().GetCacheSize())) {
2684 return AbortNode(state,
"Disk space is too low!",
2685 _(
"Disk space is too low!"));
2691 return AbortNode(state,
"Failed to write to coin database");
2707 }
catch (
const std::runtime_error &
e) {
2708 return AbortNode(state, std::string(
"System error while flushing: ") +
2734 const std::string &
prefix)
2737 LogPrintf(
"%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
2738 "date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
2740 tip->nVersion,
log(
tip->nChainWork.getdouble()) /
log(2.0),
2741 tip->GetChainTxCount(),
2744 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2762 "[background validation] ");
2805 std::shared_ptr<CBlock>
pblock = std::make_shared<CBlock>();
2808 return error(
"DisconnectTip(): Failed to read block");
2818 return error(
"DisconnectTip(): DisconnectBlock %s failed",
2857 "Disconnecting mempool due to rewind of upgrade block\n");
2891 const std::shared_ptr<const CBlock> &
pblock,
2906 std::shared_ptr<CBlock>
pblockNew = std::make_shared<CBlock>();
2908 return AbortNode(state,
"Failed to read block");
2985 std::make_unique<StakingRewardsPolicy>(
2991 std::make_unique<PreConsensusPolicy>(
2998 [&](
const auto &
policy) {
2999 bool ret = (*policy)(blockPolicyState);
3002 "Park block because it "
3003 "violated a block policy: %s\n",
3004 blockPolicyState.ToString());
3018 " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3039 " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3055 "Disconnecting mempool due to acceptance of upgrade block\n");
3068 " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3078 if (
this != &m_chainman.ActiveChainstate()) {
3082 m_chainman.MaybeCompleteSnapshotValidation();
3101 std::set<CBlockIndex *, CBlockIndexWorkComparator>::reverse_iterator
3115 LogPrintf(
"Park block %s because it forks prior to the "
3116 "avalanche finalized chaintip.\n",
3172 LogPrintf(
"Unpark chain up to block %s as it has "
3173 "accumulated enough PoW.\n",
3210 LogPrintf(
"Considered switching to better tip %s but that chain "
3211 "contains a%s%s%s block.\n",
3312 "Failed to disconnect block; see debug.log for details");
3343 : std::shared_ptr<const CBlock>(),
3391 "Updating mempool due to reorganization or "
3392 "rules upgrade/downgrade\n");
3400 if (fInvalidFound) {
3437 chainstate.m_chainman.GetNotifications().headerTip(
3453 std::shared_ptr<const CBlock>
pblock,
3475 LogPrintf(
"m_disabled is set - this chainstate should not be in "
3476 "operation. Please report this as a bug. %s\n",
3589 avalanche->computeStakingReward(pindex);
3668template <
typename Func>
struct Defer {
3685 int disconnected = 0;
3705 for (
auto &entry :
m_blockman.m_block_index) {
3829 .withParkedParent());
3923template <
typename F>
3946template <
typename F,
typename C,
typename AC>
3982 return status.withClearedFailureFlags();
3985 return status.withClearedFailureFlags();
3988 return status.withFailedParent(false);
3998 return status.withClearedParkedFlags();
4001 return fClearChildren ? status.withClearedParkedFlags()
4002 : status.withParkedParent(false);
4005 return status.withParkedParent(false);
4025 "The block to mark finalized by avalanche is not on the "
4026 "active chain: %s\n",
4078 std::deque<CBlockIndex *> queue;
4083 while (!queue.empty()) {
4101 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4102 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4105 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4107 queue.push_back(it->second);
4135 "high-hash",
"proof of work failed");
4161 "bad-txnmrklroot",
"hashMerkleRoot mismatch");
4169 "bad-txns-duplicate",
"duplicate transaction");
4178 if (block.
vtx.empty()) {
4180 "bad-cb-missing",
"first tx is not coinbase");
4189 "bad-blk-length",
"size limits failed");
4195 "bad-blk-length",
"size limits failed");
4203 strprintf(
"Coinbase check failed (txid %s) %s",
4204 block.
vtx[0]->GetId().ToString(),
4210 for (
size_t i = 1; i < block.
vtx.size(); i++) {
4211 auto *tx = block.
vtx[i].get();
4216 strprintf(
"Transaction check failed (txid %s) %s",
4232 [&](
const auto &header) {
4233 return CheckProofOfWork(
4234 header.GetHash(), header.nBits, consensusParams);
4273 "bad-diffbits",
"incorrect proof of work");
4277 if (chainman.m_options.checkpoints_enabled) {
4285 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4288 "checkpoint mismatch");
4299 "ERROR: %s: forked chain older than last checkpoint "
4303 "bad-fork-prior-to-checkpoint");
4310 "time-too-old",
"block's timestamp is too early");
4314 if (block.
Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4317 "block timestamp too far in the future");
4402 for (
const auto &
ptx : block.
vtx) {
4415 strprintf(
"Transaction order is invalid (%s < %s)",
4417 prevTx->GetId().ToString()));
4438 if (block.
vtx[0]->vin[0].scriptSig.size() <
expect.size() ||
4440 block.
vtx[0]->vin[0].scriptSig.begin())) {
4443 "block height mismatch in coinbase");
4474 if (pindex->nStatus.isInvalid()) {
4487 "%s: Consensus::CheckBlockHeader: %s, %s\n",
__func__,
4493 BlockMap::iterator
mi{
4497 "header %s has prev block not found: %s\n",
4500 "prev-blk-not-found");
4507 "header %s has prev block invalid: %s\n", hash.
ToString(),
4517 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4558 "header %s has prev block invalid: %s\n",
4569 "%s: not adding new block header %s, missing anti-dos "
4570 "proof-of-work validation\n",
4573 "too-little-chainwork");
4618 LogPrintf(
"Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4655 const double progress{100.0 * height / (height +
blocks_left)};
4656 LogPrintf(
"Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4715 LogPrintf(
"Chain tip timestamp-to-received-time difference: hash=%s, "
4718 LogPrintf(
"New block timestamp-to-received-time difference: hash=%s, "
4745 if (pindex->
nTx != 0) {
4776 pindex->nStatus = pindex->nStatus.withFailed();
4793 LogPrintf(
"Park block %s as it would cause a deep reorg.\n",
4795 pindex->nStatus = pindex->nStatus.withParked();
4816 "%s: Failed to find position to write new block to disk",
4821 }
catch (
const std::runtime_error &
e) {
4822 return AbortNode(state, std::string(
"System error: ") +
e.what());
4921 adjusted_time_callback())) {
4922 return error(
"%s: Consensus::ContextualCheckBlockHeader: %s",
__func__,
4933 return error(
"%s: Consensus::ContextualCheckBlock: %s",
__func__,
4988 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5014 chainstate.m_chain.Tip()->pprev ==
nullptr) {
5035 LogPrintf(
"Verification progress: 0%%\n");
5040 pindex = pindex->
pprev) {
5042 1, std::min(99, (
int)(((
double)(
chainstate.m_chain.Height() -
5058 !pindex->nStatus.hasData()) {
5061 LogPrintf(
"VerifyDB(): block verification stopping at height %d "
5062 "(no data). This could be due to pruning or use of an "
5063 "assumeutxo snapshot.\n",
5072 if (!
chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5074 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5083 "Verification error: found bad block at %d, hash=%s (%s)\n",
5094 LogPrintf(
"Verification error: found bad undo data at %d, "
5111 chainstate.DisconnectBlock(block, pindex, coins);
5113 LogPrintf(
"Verification error: irrecoverable inconsistency "
5114 "in block data at %d, hash=%s\n",
5136 LogPrintf(
"Verification error: coin database inconsistencies found "
5137 "(last %i blocks, %i good transactions before that)\n",
5143 LogPrintf(
"Skipped verification of level >=3 (insufficient database "
5144 "cache size). Consider increasing -dbcache.\n");
5154 1, std::min(99, 100 -
int(
double(
chainstate.m_chain.Height() -
5166 if (!
chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5167 LogPrintf(
"Verification error: ReadBlockFromDisk failed at %d, "
5172 if (!
chainstate.ConnectBlock(block, state, pindex, coins,
5174 LogPrintf(
"Verification error: found unconnectable block at "
5175 "%d, hash=%s (%s)\n",
5186 LogPrintf(
"Verification: No coin database inconsistencies in last %i "
5187 "blocks (%i transactions)\n",
5209 return error(
"ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s",
5223 for (
const CTxIn &
txin : tx->
vin) {
5237 std::vector<BlockHash>
hashHeads =
db.GetHeadBlocks();
5243 return error(
"ReplayBlocks(): unknown inconsistent state");
5258 "ReplayBlocks(): reorganization to unknown block requested");
5267 "ReplayBlocks(): reorganization from unknown block requested");
5281 return error(
"RollbackBlock(): ReadBlockFromDisk() failed at "
5292 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s",
5315 _(
"Replaying blocks…"),
5366 LogPrintf(
"Missing block index, stopping the headers time "
5367 "dumping after %d blocks.\n",
5376 index = index->
pprev;
5380 throw std::runtime_error(
strprintf(
"Failed to commit to file %s",
5391 throw std::runtime_error(
strprintf(
"Rename failed from %s to %s",
5395 }
catch (
const std::exception &
e) {
5396 LogPrintf(
"Failed to dump the headers time: %s.\n",
e.what());
5400 LogPrintf(
"Successfully dumped the last %d headers time to %s.\n",
5416 LogPrintf(
"Failed to open header times from disk, skipping.\n");
5425 LogPrintf(
"Unsupported header times file version, skipping.\n");
5441 LogPrintf(
"Missing index for block %s, stopping the headers "
5442 "time loading after %d blocks.\n",
5449 }
catch (
const std::exception &
e) {
5450 LogPrintf(
"Failed to read the headers time file data on disk: %s.\n",
5468 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5479 if (block->IsAssumedValid()) {
5499 LogPrintf(
"Saw first assumedvalid block at height %d (%s)\n",
5540 chainstate->setBlockIndexCandidates.insert(pindex);
5545 if (pindex->nStatus.isInvalid() &&
5551 if (pindex->nStatus.isOnParkedChain() &&
5574 LogPrintf(
"Initializing databases...\n");
5597 return error(
"%s: writing genesis block to disk failed",
__func__);
5602 }
catch (
const std::runtime_error &
e) {
5603 return error(
"%s: failed to write genesis block: %s",
__func__,
5642 unsigned int nSize = 0;
5647 nRewind =
blkdat.GetPos() + 1;
5659 }
catch (
const std::exception &) {
5684 std::shared_ptr<CBlock>
pblock{};
5693 "%s: Out of order block %s, parent %s not known\n",
5706 if (!pindex || !pindex->nStatus.hasData()) {
5710 pblock = std::make_shared<CBlock>();
5712 nRewind =
blkdat.GetPos();
5723 pindex->
nHeight % 1000 == 0) {
5726 "Block Import: already had block %s at height %d\n",
5727 hash.ToString(), pindex->
nHeight);
5752 "failed to activate chain (%s)\n",
5766 std::deque<BlockHash> queue;
5767 queue.push_back(hash);
5768 while (!queue.empty()) {
5773 std::multimap<BlockHash, FlatFilePos>::iterator it =
5776 std::make_shared<CBlock>();
5781 "%s: Processing out of order child %s of %s\n",
5787 &it->second,
nullptr,
true)) {
5797 }
catch (
const std::exception &
e) {
5818 "%s: unexpected data at file offset 0x%x - %s. "
5823 }
catch (
const std::runtime_error &
e) {
5824 AbortNode(std::string(
"System error: ") +
e.what());
5848 std::multimap<CBlockIndex *, CBlockIndex *>
forward;
5855 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
5856 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
5888 while (pindex !=
nullptr) {
5925 if (pindex->
pprev ==
nullptr) {
5946 assert(pindex->nStatus.hasData() == (pindex->
nTx > 0));
5948 }
else if (pindex->nStatus.hasData()) {
5953 if (pindex->nStatus.hasUndo()) {
5954 assert(pindex->nStatus.hasData());
5965 assert((pindex->nStatus.getValidity() >=
6003 assert(!pindex->nStatus.isInvalid());
6009 assert(!pindex->nStatus.isOnParkedChain());
6023 assert(pindex->nStatus.isOnParkedChain() ||
6044 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6045 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6057 if (pindex->
pprev && pindex->nStatus.hasData() &&
6065 if (!pindex->nStatus.hasData()) {
6074 if (pindex->
pprev && pindex->nStatus.hasData() &&
6102 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6103 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6107 pindex =
range.first->second;
6144 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6145 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6147 while (
rangePar.first->second != pindex) {
6172std::string Chainstate::ToString() {
6175 return strprintf(
"Chainstate [%s] @ height %d (%s)",
6194 coinsdb_size * (1.0 / 1024 / 1024));
6196 coinstip_size * (1.0 / 1024 / 1024));
6218 if (pindex ==
nullptr) {
6237 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6239 return m_active_chainstate->m_from_snapshot_blockhash;
6241 return std::nullopt;
6246 std::vector<Chainstate *> out;
6261 assert(!m_active_chainstate);
6265 return *m_active_chainstate;
6290 LogPrintf(
"[snapshot] snapshot chainstate dir being removed "
6294 }
catch (
const fs::filesystem_error &
e) {
6295 LogPrintf(
"[snapshot] failed to remove file %s: %s\n",
6327 LogPrintf(
"[snapshot] can't activate a snapshot-based chainstate more "
6410 strprintf(
"Failed to remove snapshot chainstate dir (%s). "
6411 "Manually remove it before restarting.\n",
6427 LogPrintf(
"[snapshot] successfully activated snapshot %s\n",
6443 :
"flushing coins cache",
6444 coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
6452 return "ComputeUTXOStats interrupted by shutdown.";
6479 LogPrintf(
"[snapshot] Did not find snapshot start blockheader %s\n",
6488 LogPrintf(
"[snapshot] assumeutxo height in snapshot metadata not "
6489 "recognized (%d) - refusing to load snapshot\n",
6501 LogPrintf(
"[snapshot] loading coins from snapshot %s\n",
6509 }
catch (
const std::ios_base::failure &) {
6510 LogPrintf(
"[snapshot] bad snapshot format or truncated snapshot "
6511 "after deserializing %d coins\n",
6518 std::numeric_limits<
decltype(outpoint.GetN())>::max()) {
6520 "[snapshot] bad snapshot data after deserializing %d coins\n",
6524 coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint),
6531 LogPrintf(
"[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6534 static_cast<float>(coins_count),
6535 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6574 }
catch (
const std::ios_base::failure &) {
6579 LogPrintf(
"[snapshot] bad snapshot - coins left over after "
6580 "deserializing %d coins\n",
6585 LogPrintf(
"[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6586 coins_count,
coins_cache.DynamicMemoryUsage() / (1000 * 1000),
6602 maybe_stats = ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED,
6609 LogPrintf(
"[snapshot] failed to generate coins stats\n");
6617 LogPrintf(
"[snapshot] bad snapshot content hash: expected %s, got %s\n",
6618 au_data.hash_serialized.ToString(),
6655 index->nStatus = index->nStatus.withAssumedValid();
6670 LogPrintf(
"[snapshot] validated snapshot (%.2f MB)\n",
6671 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6715 _(
"%s failed to validate the -assumeutxo snapshot state. "
6716 "This indicates a hardware problem, or a bug in the software, or "
6717 "a bad software modification that allowed an invalid snapshot to "
6718 "be loaded. As a result of this, the node will shut down and "
6719 "stop using any state that was built on the snapshot, resetting "
6720 "the chain height from %d to %d. On the next restart, the node "
6721 "will resume syncing from %d without using any snapshot data. "
6722 "Please report this incident to %s, including how you obtained "
6723 "the snapshot. The invalid snapshot chainstate will be left on "
6724 "disk in case it is helpful in diagnosing the issue that caused "
6730 LogPrintf(
"[snapshot] deleting snapshot, reverting to validated chain, "
6731 "and stopping node\n");
6749 "[snapshot] supposed base block %s does not match the "
6750 "snapshot base block %s (height %d). Snapshot is not valid.\n",
6771 LogPrintf(
"[snapshot] assumeutxo data not found for height "
6772 "(%d) - refusing to validate snapshot\n",
6781 "[snapshot] computing UTXO stats for background chainstate to validate "
6782 "snapshot - this could take a few minutes\n");
6793 "[snapshot] failed to generate stats for validation coins db\n");
6809 LogPrintf(
"[snapshot] hash mismatch: actual=%s, expected=%s\n",
6811 au_data.hash_serialized.ToString());
6816 LogPrintf(
"[snapshot] snapshot beginning at %s has been fully validated\n",
6827 assert(m_active_chainstate);
6828 return *m_active_chainstate;
6836void ChainstateManager::MaybeRebalanceCaches() {
6843 LogPrintf(
"[snapshot] allocating all cache to the IBD chainstate\n");
6851 "[snapshot] allocating all cache to the snapshot chainstate\n");
6875void ChainstateManager::ResetChainstates() {
6878 m_active_chainstate =
nullptr;
6887 if (!opts.check_block_index.has_value()) {
6889 opts.config.GetChainParams().DefaultConsistencyChecks();
6892 if (!opts.minimum_chain_work.has_value()) {
6894 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
6896 if (!opts.assumed_valid_block.has_value()) {
6897 opts.assumed_valid_block =
6898 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
6900 Assert(opts.adjusted_time_callback);
6901 return std::move(opts);
6909bool ChainstateManager::DetectSnapshotChainstate(
CTxMemPool *mempool) {
6920 LogPrintf(
"[snapshot] detected active snapshot chainstate (%s) - loading\n",
6928ChainstateManager::ActivateExistingSnapshot(
CTxMemPool *mempool,
6933 LogPrintf(
"[snapshot] switching active chainstate to %s\n",
6962 }
catch (
const fs::filesystem_error &
e) {
6969 "You should resolve this by manually "
6970 "moving or deleting the invalid "
6971 "snapshot directory %s, otherwise you "
6972 "will encounter the same error again "
6973 "on the next startup."),
6979const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock()
const {
6987std::optional<int> ChainstateManager::GetSnapshotBaseHeight()
const {
6992bool ChainstateManager::ValidatedSnapshotCleanup() {
7014 LogPrintf(
"[snapshot] snapshot chainstate cleanup cannot happen with "
7015 "in-memory chainstates. You are testing, right?\n");
7033 LogPrintf(
"[snapshot] deleting background chainstate directory (now "
7034 "unnecessary) (%s)\n",
7040 const fs::filesystem_error &
err) {
7044 "Rename of '%s' -> '%s' failed. "
7045 "Cannot clean up the background chainstate leveldb directory.",
7051 }
catch (
const fs::filesystem_error &
e) {
7056 LogPrintf(
"[snapshot] moving snapshot chainstate (%s) to "
7057 "default chainstate directory (%s)\n",
7063 }
catch (
const fs::filesystem_error &
e) {
7071 LogPrintf(
"Deletion of %s failed. Please remove it manually, as the "
7072 "directory is now unnecessary.\n",
7075 LogPrintf(
"[snapshot] deleted background chainstate directory (%s)\n",
bool IsDAAEnabled(const Consensus::Params ¶ms, int nHeight)
bool IsUAHFenabled(const Consensus::Params ¶ms, int nHeight)
static bool IsPhononEnabled(const Consensus::Params ¶ms, int32_t nHeight)
static bool IsGravitonEnabled(const Consensus::Params ¶ms, int32_t nHeight)
bool IsMagneticAnomalyEnabled(const Consensus::Params ¶ms, int32_t nHeight)
Check if Nov 15, 2018 HF has activated using block height.
bool MoneyRange(const Amount nValue)
static constexpr Amount SATOSHI
static constexpr Amount COIN
arith_uint256 UintToArith256(const uint256 &a)
@ CHAIN
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends,...
@ TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
@ SCRIPTS
Scripts & signatures ok.
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
arith_uint256 GetBlockProof(const CBlockIndex &block)
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
Return the time it would take to redo the work difference between from and to, assuming the current h...
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
fs::path GetBlocksDirPath() const
Get blocks directory path.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Non-refcounted RAII wrapper for FILE*.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
uint64_t getExcessiveBlockSize() const
BlockValidationOptions withCheckPoW(bool _checkPoW=true) const
BlockValidationOptions withCheckMerkleRoot(bool _checkMerkleRoot=true) const
BlockValidationOptions(const Config &config)
std::string ToString() const
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
int64_t GetHeaderReceivedTime() const
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
const BlockHash * phashBlock
pointer to the hash of the block, if any.
bool HaveTxsDownloaded() const
Check whether this block's and all previous blocks' transactions have been downloaded (and stored to ...
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
int64_t GetReceivedTimeDiff() const
int64_t GetBlockTime() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
bool UpdateChainStats()
Update chain tx stats.
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
unsigned int nTx
Number of transactions in this block.
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
int64_t nTimeReceived
(memory only) block header metadata
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
int Height() const
Return the maximal height in the chain.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const CBlock & GenesisBlock() const
const CMessageHeader::MessageMagic & DiskMagic() const
const ChainTxData & TxData() const
const Consensus::Params & GetConsensus() const
uint64_t PruneAfterHeight() const
const MapAssumeutxo & Assumeutxo() const
Get allowed assumeutxo configuration.
const CCheckpointData & Checkpoints() const
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Queue for verifications that have to be performed.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SetBestBlock(const BlockHash &hashBlock)
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool 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)
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
CCoinsView backed by the coin database (chainstate/)
std::optional< fs::path > StoragePath()
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Abstract view on the open txout dataset.
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
CCoinsView that brings transactions from a mempool into view.
Fee rate in satoshis per kilobyte: Amount / kB.
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, uint64_t mempool_sequence)
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockChecked(const CBlock &, const BlockValidationState &)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void ChainStateFlushed(const CBlockLocator &)
An outpoint - a combination of a transaction hash and an index n into its vout.
const TxId & GetTxId() const
void insert(Span< const uint8_t > vKey)
bool contains(Span< const uint8_t > vKey) const
Closure representing one script verification.
ScriptError GetScriptError() const
ScriptExecutionMetrics GetScriptExecutionMetrics() const
TxSigCheckLimiter * pTxLimitSigChecks
ScriptExecutionMetrics metrics
PrecomputedTransactionData txdata
const CTransaction * ptxTo
CheckInputsLimiter * pBlockLimitSigChecks
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
const std::vector< CTxIn > vin
An input of a transaction.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
void AddTransactionsUpdated(unsigned int n)
const int64_t m_max_size_bytes
size_t DynamicMemoryUsage() const
void SetLoadTried(bool load_tried)
Set whether or not we've made an attempt to load the mempool (regardless of whether the attempt was s...
Restore the UTXO in a Coin at a given COutPoint.
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::Notifications & m_notifications
Chainstate stores and provides an API to update our local knowledge of the current best chain.
std::set< CBlockIndex *, CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors...
bool IsBlockAvalancheFinalized(const CBlockIndex *pindex) const EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Checks if a block is finalized by avalanche voting.
const std::optional< BlockHash > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(
Initialize the in-memory coins cache (to be done after the health of the on-disk database is verified...
void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mutex m_chainstate_mutex
The ChainState Mutex.
void UnloadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void UpdateFlags(CBlockIndex *pindex, CBlockIndex *&pindexReset, F f, C fChild, AC fAncestorWasChanged) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CChain m_chain
The current chain of blockheaders we consult and build on.
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, const avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
void UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< BlockHash > from_snapshot_blockhash=std::nullopt)
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Mutex cs_avalancheFinalizedBlockIndex
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(std::chrono::microsecond m_last_write)
Check warning conditions and do some notifications on new chain tip set.
void UnparkBlockAndChildren(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block and its descendants.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
void LoadMempool(const fs::path &load_path, fsbridge::FopenFn mockable_fopen_function=fsbridge::fopen)
Load the persisted mempool from disk.
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
std::chrono::microseconds m_last_flush
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
bool UnwindBlock(BlockValidationState &state, CBlockIndex *pindex, bool invalidate) EXCLUSIVE_LOCKS_REQUIRED(m_chainstate_mutex
bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as invalid.
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
CRollingBloomFilter m_filterParkingPoliciesApplied
Filter to prevent parking a block due to block policies more than once.
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
void ResetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove invalidity status from a block and its descendants.
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) EXCLUSIVE_LOCKS_REQUIRED(void LoadExternalBlockFile(FILE *fileIn, FlatFilePos *dbp=nullptr, std::multimap< BlockHash, FlatFilePos > *blocks_with_unknown_parent=nullptr, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Resize the CoinsViews caches dynamically and flush state to disk.
void CheckBlockIndex()
Make various assertions about the state of the block index.
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo ConnectBlock)(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, BlockValidationOptions options, Amount *blockFees=nullptr, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of this block (with given index) on the UTXO set represented by coins.
CBlockIndex const * m_best_fork_tip
bool ConnectTip(BlockValidationState &state, BlockPolicyValidationState &blockPolicyState, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, DisconnectedBlockTransactions &disconnectpool, const avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
bool FlushStateToDisk(BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
Update the on-disk chain state.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
CBlockIndex const * m_best_fork_base
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main
std::atomic< bool > m_cached_finished_ibd
Whether this chainstate is undergoing initial block download.
void UnparkBlock(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block.
bool PreciousBlock(BlockValidationState &state, CBlockIndex *pindex, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as precious and reorganize.
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr, bool skip_checkblockindex=false) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
void ClearAvalancheFinalizedBlock() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Clear avalanche finalization.
const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
std::atomic< int32_t > nBlockSequenceId
Every received block is assigned a unique and increasing identifier, so we know which one to give pri...
CBlockIndex * FindMostWorkChain(std::vector< const CBlockIndex * > &blocksToReconcile, bool fAutoUnpark) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Return the tip of the chain with the most work in it, that isn't known to be invalid (it's however fa...
bool UpdateFlagsForBlock(CBlockIndex *pindexBase, CBlockIndex *pindex, F f) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool AcceptBlock(const std::shared_ptr< const CBlock > &pblock, BlockValidationState &state, bool fRequested, const FlatFilePos *dbp, bool *fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Store a block on disk.
bool ParkBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Park a block.
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(RecursiveMutex * MempoolMutex() const LOCK_RETURNED(m_mempool -> cs)
Dictates whether we need to flush the cache to disk or not.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(bool IsUsable(const Chainstate *const pchainstate) const EXCLUSIVE_LOCKS_REQUIRED(
Returns nullptr if no snapshot has been loaded.
bool DetectSnapshotChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &ActivateExistingSnapshot(CTxMemPool *mempool, BlockHash base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(boo DumpRecentHeadersTime)(const fs::path &filePath) const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
When starting up, search the datadir for a chainstate based on a UTXO snapshot that is in the process...
const Config & GetConfig() const
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
bool AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't desce...
kernel::Notifications & GetNotifications() const
bool ShouldCheckBlockIndex() const
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &block, bool force_processing, bool min_pow_checked, bool *new_block, avalanche::Processor *const avalanche=nullptr) LOCKS_EXCLUDED(cs_main)
Process an incoming block.
bool LoadRecentHeadersTime(const fs::path &filePath) EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Load the recent block headers reception time from a file.
std::optional< BlockHash > SnapshotBlockhash() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
SnapshotCompletionResult MaybeCompleteSnapshotValidation(std::function< void(bilingual_str)> shutdown_fnc=[](bilingual_str msg) { AbortNode(msg.original, msg);}) EXCLUSIVE_LOCKS_REQUIRED(Chainstate & ActiveChainstate() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
bool IsSnapshotActive() const
const CChainParams & GetParams() const
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &block, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex=nullptr, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) LOCKS_EXCLUDED(cs_main)
Process incoming block headers.
const Consensus::Params & GetConsensus() const
const arith_uint256 & MinimumChainWork() const
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(void ReportHeadersPresync(const arith_uint256 &work, int64_t height, int64_t timestamp)
Check to see if caches are out of balance and if so, call ResizeCoinsCaches() as needed.
const BlockHash & AssumedValidBlock() const
ChainstateManager(Options options, node::BlockManager::Options blockman_options)
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
std::set< CBlockIndex * > m_failed_blocks
In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to conn...
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
uint32_t GetHeight() const
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
virtual const CChainParams & GetChainParams() const =0
void importMempool(CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
Different type to mark Mutex at global scope.
static RCUPtr acquire(T *&ptrIn)
Acquire ownership of some pointer.
The script cache is a map using a key/value element, that caches the success of executing a specific ...
static TxSigCheckLimiter getDisabled()
bool Error(const std::string &reject_reason)
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
std::string ToString() const
256-bit unsigned big integer.
std::string ToString() const
std::string GetHex() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
A base class defining functions for notifying about certain kernel events.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
virtual void warning(const std::string &warning)
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
virtual void blockTip(SynchronizationState state, CBlockIndex &index)
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
RecursiveMutex cs_LastBlockFile
void FindFilesToPrune(std::set< int > &setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex &start_block) EXCLUSIVE_LOCKS_REQUIRED(bool m_have_pruned
Find the first block that is not pruned.
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const FlatFilePos *dbp)
Store block on disk.
bool LoadingBlocks() const
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex * AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove any pruned block & undo files that are still on disk.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
bool IsPruneMode() const
Whether running in -prune mode.
std::vector< CBlockIndex * > GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(std::multimap< CBlockIndex *, CBlockIndex * > m_blocks_unlinked
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
const Coin & AccessByTxid(const CCoinsViewCache &view, const TxId &txid)
Utility function to find any unspent output with a given txid.
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_CHILD_BEFORE_PARENT
This tx outputs are already spent in the mempool.
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/etc limits
@ TX_PACKAGE_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_DUPLICATE
Tx already in mempool or in the chain.
@ TX_INPUTS_NOT_STANDARD
inputs failed policy rules
@ TX_CONFLICT
Tx conflicts with a finalized tx, i.e.
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_AVALANCHE_RECONSIDERABLE
fails some policy, but might be reconsidered by avalanche voting
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_CONSENSUS
invalid by consensus rules
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
uint64_t GetMaxBlockSigChecksCount(uint64_t maxBlockSize)
Compute the maximum number of sigchecks that can be contained in a block given the MAXIMUM block size...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::BuriedDeployment dep)
Determine if a deployment is active for the next block.
bool RenameOver(fs::path src, fs::path dest)
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror)
Execute an unlocking and locking script together.
std::map< int, const AssumeutxoData > MapAssumeutxo
bool error(const char *fmt, const Args &...args)
#define LogPrint(category,...)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
bool CheckBlock(const CCheckpointData &data, int nHeight, const BlockHash &hash)
Returns true if block passes checkpoint checks.
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, Amount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts).
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
std::string get_filesystem_error_message(const fs::filesystem_error &e)
std::function< FILE *(const fs::path &, const char *)> FopenFn
std::optional< CCoinsStats > ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView *view, node::BlockManager &blockman, const std::function< void()> &interruption_point)
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
bool LoadMempool(CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, FopenFn mockable_fopen_function)
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::optional< fs::path > FindSnapshotChainstateDir()
Return a path to the snapshot-based chainstate dir, if one exists.
std::unordered_map< BlockHash, CBlockIndex, BlockHasher > BlockMap
std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir)
static constexpr unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
std::atomic_bool fReindex
Implement std::hash so RCUPtr can be used as a key for maps or sets.
bilingual_str ErrorString(const Result< T > &result)
std::shared_ptr< Chain::Notifications > m_notifications
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
bool CheckPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
@ PCKG_MEMPOOL_ERROR
Mempool logic error.
@ PCKG_TX
At least one tx is invalid.
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, uint32_t flags)
Check transaction inputs to mitigate two potential denial-of-service attacks:
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
static constexpr uint32_t STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
static constexpr uint32_t STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
reverse_range< T > reverse_iterate(T &x)
std::string ScriptErrorString(const ScriptError serror)
@ SIGCHECKS_LIMIT_EXCEEDED
@ SCRIPT_VERIFY_SIGPUSHONLY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_ENABLE_REPLAY_PROTECTION
@ SCRIPT_ENABLE_SCHNORR_MULTISIG
@ SCRIPT_VERIFY_STRICTENC
@ SCRIPT_ENFORCE_SIGCHECKS
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_MINIMALDATA
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
@ SCRIPT_ENABLE_SIGHASH_FORKID
void AddKeyInScriptCache(ScriptCacheKey key, int nSigChecks)
Add an entry in the cache.
bool IsKeyInScriptCache(ScriptCacheKey key, bool erase, int &nSigChecksOut)
Check if a given key is in the cache, and if so, return its values.
static std::string ToString(const CService &ip)
size_t GetSerializeSize(const T &t, int nVersion=0)
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
void StartShutdown()
Request shutdown of the application.
static constexpr Amount zero() noexcept
Holds configuration for use during UTXO snapshot load and validation.
A BlockHash is a unqiue identifier for a block.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Holds various statistics on transactions within a chain.
User-controlled performance and debug options.
Parameters that influence chain consensus.
BlockHash hashGenesisBlock
int schumpeterActivationTime
Unix time used for MTP activation of 15 May 2025 12:00:00 UTC upgrade.
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.
@ VALID
Fully validated, valid.
static MempoolAcceptResult Failure(TxValidationState state)
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
static MempoolAcceptResult Success(int64_t vsize, Amount fees, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
Constructor for success case.
static MempoolAcceptResult MempoolTx(int64_t vsize, Amount fees)
Constructor for already-in-mempool case.
std::chrono::time_point< NodeClock > time_point
Validation result for package mempool acceptance.
Precompute sighash midstate to avoid quadratic hashing.
const char * what() const override
A TxId is the identifier of a transaction.
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
const std::function< NodeClock::time_point()> adjusted_time_callback
std::optional< bool > check_block_index
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
bool store_recent_headers_time
If set, store and load the last few block headers reception time to speed up RTT bootstraping.
CoinsViewOptions coins_view
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define AssertLockHeld(cs)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define LOCKS_EXCLUDED(...)
#define NO_THREAD_SAFETY_ANALYSIS
int64_t GetTimeMicros()
Returns the system time (not mockable)
int64_t GetTimeMillis()
Returns the system time (not mockable)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
#define TRACE6(context, event, a, b, c, d, e, f)
#define TRACE5(context, event, a, b, c, d, e)
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
bool CheckRegularTransaction(const CTransaction &tx, TxValidationState &state)
Context-independent validity checks for coinbase and non-coinbase transactions.
bool CheckCoinbase(const CTransaction &tx, TxValidationState &state)
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Calculates the block height and previous block's median time past at which the transaction will be co...
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
bool ContextualCheckTransaction(const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state, int nHeight, int64_t nMedianTimePast)
Context dependent validity checks for non coinbase transactions.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
uint256 uint256S(const char *str)
uint256 from const char *.
DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &coins)
Undo a block from the block and the undoblock data.
static bool DeleteCoinsDBFromDisk(const fs::path &db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
void StartScriptCheckWorkerThreads(int threads_num)
Run instances of script checking worker threads.
static int64_t nTimeConnectTotal
GlobalMutex g_best_block_mutex
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
std::condition_variable g_best_block_cv
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
Calculate LockPoints required to check if transaction will be BIP68 final in the next block to be cre...
arith_uint256 CalculateHeadersWork(const std::vector< CBlockHeader > &headers)
Return the sum of the work on a given set of headers.
DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view)
Undo a block from the block and the undoblock data.
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index require cs_main if pindex h...
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept, unsigned int heightOverride)
Try to add a transaction to the mempool.
static int64_t nBlocksTotal
static int64_t nTimePostConnect
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params ¶ms, BlockValidationOptions validationOptions)
Return true if the provided block header is valid.
static SynchronizationState GetSynchronizationState(bool init)
static void SnapshotUTXOHashBreakpoint()
static int64_t nTimeFlush
static bool ContextualCheckBlock(const CBlock &block, BlockValidationState &state, const ChainstateManager &chainman, const CBlockIndex *pindexPrev)
NOTE: This function is not currently invoked by ConnectBlock(), so we should consider upgrade issues ...
bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
static uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex, const ChainstateManager &chainman)
const CBlockIndex * g_best_block
Used to notify getblocktemplate RPC of new tips.
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL
Time to wait between flushing chainstate to disk.
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Validate (and maybe submit) a package to the mempool.
static CCheckQueue< CScriptCheck > scriptcheckqueue(128)
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams &chainparams)
Return the expected assumeutxo value for a given height, if one exists.
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
static int64_t nTimeVerify
void StopScriptCheckWorkerThreads()
Stop all of the script checking worker threads.
static void LimitValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
return CheckInputScripts(tx, state, view, flags, true, true, txdata, nSigChecksOut)
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, const uint32_t flags, PrecomputedTransactionData &txdata, int &nSigChecksOut, CCoinsViewCache &coins_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Checks to avoid mempool polluting consensus critical paths since cached signature and script validity...
void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Mark all the coins corresponding to a given transaction inputs as spent.
static int64_t nTimeTotal
static int64_t nTimeConnect
static bool NotifyHeaderTip(Chainstate &chainstate) LOCKS_EXCLUDED(cs_main)
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params ¶ms, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
bool ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state)
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
DisconnectResult UndoCoinSpend(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static int64_t nTimeIndex
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
bool AbortNode(BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage)
void UpdateCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Apply the effects of this transaction on the UTXO set represented by view.
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, ChainstateManager &chainman, const CBlockIndex *pindexPrev, NodeClock::time_point now, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
static int64_t nTimeForks
static constexpr uint64_t HEADERS_TIME_VERSION
static int64_t nTimeCheck
static void UpdateTipLog(const CCoinsViewCache &coins_tip, const CBlockIndex *tip, const CChainParams ¶ms, const std::string &func_name, const std::string &prefix) EXCLUSIVE_LOCKS_REQUIRED(
static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL
Time to wait between writing blocks/block index to disk.
static int64_t nTimeChainState
static int64_t nTimeReadFromDisk
static bool IsReplayProtectionEnabled(const Consensus::Params ¶ms, int64_t nMedianTimePast)
bool ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params ¶ms, const CTransaction &tx, TxValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(bool TestBlockValidity(BlockValidationState &state, const CChainParams ¶ms, Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev, const std::function< NodeClock::time_point()> &adjusted_time_callback, BlockValidationOptions validationOptions) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
This is a variant of ContextualCheckTransaction which computes the contextual check for a transaction...
GlobalMutex g_best_block_mutex
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
std::condition_variable g_best_block_cv
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).
#define MIN_TRANSACTION_SIZE
bool CheckInputScripts(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const uint32_t flags, bool sigCacheStore, bool scriptCacheStore, const PrecomputedTransactionData &txdata, int &nSigChecksOut, TxSigCheckLimiter &txLimitSigChecks, CheckInputsLimiter *pBlockLimitSigChecks, std::vector< CScriptCheck > *pvChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Check whether all of this transaction's input scripts succeed.
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
const CBlockIndex * g_best_block
Used to notify getblocktemplate RPC of new tips.
@ BASE_BLOCKHASH_MISMATCH
SynchronizationState
Current sync state passed to tip changed callbacks.
bool AbortNode(BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage=bilingual_str{})
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept=false, unsigned int heightOverride=0) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the mempool.
void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Mark all the coins corresponding to a given transaction inputs as spent.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params ¶ms, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.
bool DeploymentActiveAt(const CBlockIndex &index, const ChainstateManager &chainman, DEP dep)
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams ¶ms)
Return the expected assumeutxo value for a given height, if one exists.
static const int DEFAULT_STOPATHEIGHT
Default for -stopatheight.
CMainSignals & GetMainSignals()
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static const int PROTOCOL_VERSION
network protocol versioning
void SetfLargeWorkInvalidChainFound(bool flag)
void SetfLargeWorkForkFound(bool flag)
bool GetfLargeWorkForkFound()