Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_VALIDATION_H7#define BITCOIN_VALIDATION_H9#include <arith_uint256.h>10#include <attributes.h>11#include <chain.h>12#include <checkqueue.h>13#include <coins.h>14#include <consensus/amount.h>15#include <cuckoocache.h>16#include <deploymentstatus.h>17#include <kernel/chain.h>18#include <kernel/chainparams.h>19#include <kernel/chainstatemanager_opts.h>20#include <kernel/cs_main.h> // IWYU pragma: export21#include <node/blockstorage.h>22#include <policy/feerate.h>23#include <policy/packages.h>24#include <policy/policy.h>25#include <script/script_error.h>26#include <script/sigcache.h>27#include <script/verify_flags.h>28#include <sync.h>29#include <txdb.h>30#include <txmempool.h>31#include <uint256.h>32#include <util/byte_units.h>33#include <util/check.h>34#include <util/fs.h>35#include <util/hasher.h>36#include <util/result.h>37#include <util/time.h>38#include <util/translation.h>39#include <versionbits.h>41#include <algorithm>42#include <atomic>43#include <cstdint>44#include <map>45#include <memory>46#include <optional>47#include <set>48#include <span>49#include <string>50#include <type_traits>51#include <utility>52#include <vector>54class Chainstate;55class CTxMemPool;56class ChainstateManager;57struct ChainTxData;58class DisconnectedBlockTransactions;59struct PrecomputedTransactionData;60struct LockPoints;61struct AssumeutxoData;62namespace kernel {63struct ChainstateRole;64} // namespace kernel65namespace node {66class SnapshotMetadata;67} // namespace node68namespace Consensus {69struct Params;70} // namespace Consensus71namespace util {72class SignalInterrupt;73} // namespace util75/** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned. */76inline constexpr unsigned int MIN_BLOCKS_TO_KEEP = 288;77inline constexpr signed int DEFAULT_CHECKBLOCKS = 6;78inline constexpr int DEFAULT_CHECKLEVEL{3};79// Require that user allocate at least 550 MiB for block & undo files (blk???.dat and rev???.dat)80// At 1MB per block, 288 blocks = 288MB.81// Add 15% for Undo data = 331MB82// Add 20% for Orphan block rate = 397MB83// We want the low water mark after pruning to be at least 397 MB and since we prune in84// full block file chunks, we need the high water mark which triggers the prune to be85// one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB86// Setting the target to >= 550 MiB will make it likely we can respect the target.87inline constexpr uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES{550_MiB};89/** Maximum number of dedicated script-checking threads allowed */90inline constexpr int MAX_SCRIPTCHECK_THREADS{15};92/** Maximum number of dedicated threads allowed for prefetching block input prevouts */93inline constexpr int32_t MAX_PREVOUTFETCH_THREADS{16};95/** Current sync state passed to tip changed callbacks. */96enum class SynchronizationState {97INIT_REINDEX,98INIT_DOWNLOAD,99POST_INIT100};102/** Documentation for argument 'checklevel'. */103extern const std::vector<std::string> CHECKLEVEL_DOC;105CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);107bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const bilingual_str& message);109/** Prune block files up to a given height */110void PruneBlockFilesManual(Chainstate& active_chainstate, int nManualPruneHeight);112/**113* Validation result for a transaction evaluated by MemPoolAccept (single or package).114* Here are the expected fields and properties of a result depending on its ResultType, applicable to115* results returned from package evaluation:116*+---------------------------+----------------+-------------------+------------------+----------------+-------------------+117*| Field or property | VALID | INVALID | MEMPOOL_ENTRY | DIFFERENT_WITNESS |118*| | |--------------------------------------| | |119*| | | TX_RECONSIDERABLE | Other | | |120*+---------------------------+----------------+-------------------+------------------+----------------+-------------------+121*| txid in mempool? | yes | no | no* | yes | yes |122*| wtxid in mempool? | yes | no | no* | yes | no |123*| m_state | yes, IsValid() | yes, IsInvalid() | yes, IsInvalid() | yes, IsValid() | yes, IsValid() |124*| m_vsize | yes | no | no | yes | no |125*| m_base_fees | yes | no | no | yes | no |126*| m_effective_feerate | yes | yes | no | no | no |127*| m_wtxids_fee_calculations | yes | yes | no | no | no |128*| m_other_wtxid | no | no | no | no | yes |129*+---------------------------+----------------+-------------------+------------------+----------------+-------------------+130* (*) Individual transaction acceptance doesn't return MEMPOOL_ENTRY and DIFFERENT_WITNESS. It returns131* INVALID, with the errors txn-already-in-mempool and txn-same-nonwitness-data-in-mempool132* respectively. In those cases, the txid or wtxid may be in the mempool for a TX_CONFLICT.133*/134struct MempoolAcceptResult {135/** Used to indicate the results of mempool validation. */136enum class ResultType {137VALID, //!> Fully validated, valid.138INVALID, //!> Invalid.139MEMPOOL_ENTRY, //!> Valid, transaction was already in the mempool.140DIFFERENT_WITNESS, //!> Not validated. A same-txid-different-witness tx (see m_other_wtxid) already exists in the mempool and was not replaced.141};142/** Result type. Present in all MempoolAcceptResults. */143const ResultType m_result_type;145/** Contains information about why the transaction failed. */146const TxValidationState m_state;148/** Mempool transactions replaced by the tx. */149const std::list<CTransactionRef> m_replaced_transactions;150/** Virtual size as used by the mempool, calculated using serialized size and sigops. */151const std::optional<int64_t> m_vsize;152/** Raw base fees in satoshis. */153const std::optional<CAmount> m_base_fees;154/** The feerate at which this transaction was considered. This includes any fee delta added155* using prioritisetransaction (i.e. modified fees). If this transaction was submitted as a156* package, this is the package feerate, which may also include its descendants and/or157* ancestors (see m_wtxids_fee_calculations below).158*/159const std::optional<CFeeRate> m_effective_feerate;160/** Contains the wtxids of the transactions used for fee-related checks. Includes this161* transaction's wtxid and may include others if this transaction was validated as part of a162* package. This is not necessarily equivalent to the list of transactions passed to163* ProcessNewPackage().164* Only present when m_result_type = ResultType::VALID. */165const std::optional<std::vector<Wtxid>> m_wtxids_fee_calculations;167/** The wtxid of the transaction in the mempool which has the same txid but different witness. */168const std::optional<Wtxid> m_other_wtxid;170static MempoolAcceptResult Failure(TxValidationState state) {171return MempoolAcceptResult(state);172}174static MempoolAcceptResult FeeFailure(TxValidationState state,175CFeeRate effective_feerate,176const std::vector<Wtxid>& wtxids_fee_calculations) {177return MempoolAcceptResult(state, effective_feerate, wtxids_fee_calculations);178}180static MempoolAcceptResult Success(std::list<CTransactionRef>&& replaced_txns,181int64_t vsize,182CAmount fees,183CFeeRate effective_feerate,184const std::vector<Wtxid>& wtxids_fee_calculations) {185return MempoolAcceptResult(std::move(replaced_txns), vsize, fees,186effective_feerate, wtxids_fee_calculations);187}189static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees) {190return MempoolAcceptResult(vsize, fees);191}193static MempoolAcceptResult MempoolTxDifferentWitness(const Wtxid& other_wtxid) {194return MempoolAcceptResult(other_wtxid);195}197// Private constructors. Use static methods MempoolAcceptResult::Success, etc. to construct.198private:199/** Constructor for failure case */200explicit MempoolAcceptResult(TxValidationState state)201: m_result_type(ResultType::INVALID), m_state(state) {202Assume(!state.IsValid()); // Can be invalid or error203}205/** Constructor for success case */206explicit MempoolAcceptResult(std::list<CTransactionRef>&& replaced_txns,207int64_t vsize,208CAmount fees,209CFeeRate effective_feerate,210const std::vector<Wtxid>& wtxids_fee_calculations)211: m_result_type(ResultType::VALID),212m_replaced_transactions(std::move(replaced_txns)),213m_vsize{vsize},214m_base_fees(fees),215m_effective_feerate(effective_feerate),216m_wtxids_fee_calculations(wtxids_fee_calculations) {}218/** Constructor for fee-related failure case */219explicit MempoolAcceptResult(TxValidationState state,220CFeeRate effective_feerate,221const std::vector<Wtxid>& wtxids_fee_calculations)222: m_result_type(ResultType::INVALID),223m_state(state),224m_effective_feerate(effective_feerate),225m_wtxids_fee_calculations(wtxids_fee_calculations) {}227/** Constructor for already-in-mempool case. It wouldn't replace any transactions. */228explicit MempoolAcceptResult(int64_t vsize, CAmount fees)229: m_result_type(ResultType::MEMPOOL_ENTRY), m_vsize{vsize}, m_base_fees(fees) {}231/** Constructor for witness-swapped case. */232explicit MempoolAcceptResult(const Wtxid& other_wtxid)233: m_result_type(ResultType::DIFFERENT_WITNESS), m_other_wtxid(other_wtxid) {}234};236/**237* Validation result for package mempool acceptance.238*/239struct PackageMempoolAcceptResult240{241PackageValidationState m_state;242/**243* Map from wtxid to finished MempoolAcceptResults. The client is responsible244* for keeping track of the transaction objects themselves. If a result is not245* present, it means validation was unfinished for that transaction. If there246* was a package-wide error (see result in m_state), m_tx_results will be empty.247*/248std::map<Wtxid, MempoolAcceptResult> m_tx_results;250explicit PackageMempoolAcceptResult(PackageValidationState state,
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.