Bitcoin Core  27.99.0
P2P Digital Currency
Classes | Namespaces | Enumerations | Functions | Variables
validation.h File Reference
#include <arith_uint256.h>
#include <attributes.h>
#include <chain.h>
#include <checkqueue.h>
#include <kernel/chain.h>
#include <consensus/amount.h>
#include <deploymentstatus.h>
#include <kernel/chainparams.h>
#include <kernel/chainstatemanager_opts.h>
#include <kernel/cs_main.h>
#include <node/blockstorage.h>
#include <policy/feerate.h>
#include <policy/packages.h>
#include <policy/policy.h>
#include <script/script_error.h>
#include <sync.h>
#include <txdb.h>
#include <txmempool.h>
#include <uint256.h>
#include <util/check.h>
#include <util/fs.h>
#include <util/hasher.h>
#include <util/result.h>
#include <util/translation.h>
#include <versionbits.h>
#include <atomic>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <stdint.h>
#include <string>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>
Include dependency graph for validation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  MempoolAcceptResult
 Validation result for a transaction evaluated by MemPoolAccept (single or package). More...
 
struct  PackageMempoolAcceptResult
 Validation result for package mempool acceptance. More...
 
class  CScriptCheck
 Closure representing one script verification Note that this stores references to the spending transaction. More...
 
class  CVerifyDB
 RAII wrapper for VerifyDB: Verify consistency of the block and coin databases. More...
 
class  CoinsViews
 A convenience class for constructing the CCoinsView* hierarchy used to facilitate access to the UTXO set. More...
 
class  Chainstate
 Chainstate stores and provides an API to update our local knowledge of the current best chain. More...
 
class  ChainstateManager
 Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate generated by downloading blocks, and an optional snapshot chainstate loaded from a UTXO snapshot. More...
 

Namespaces

 node
 
 Consensus
 Transaction validation functions.
 
 util
 

Enumerations

enum class  SynchronizationState { INIT_REINDEX , INIT_DOWNLOAD , POST_INIT }
 Current sync state passed to tip changed callbacks. More...
 
enum class  VerifyDBResult {
  SUCCESS , CORRUPTED_BLOCK_DB , INTERRUPTED , SKIPPED_L3_CHECKS ,
  SKIPPED_MISSING_BLOCKS
}
 
enum  DisconnectResult { DISCONNECT_OK , DISCONNECT_UNCLEAN , DISCONNECT_FAILED }
 
enum class  FlushStateMode { NONE , IF_NEEDED , PERIODIC , ALWAYS }
 
enum class  CoinsCacheSizeState { CRITICAL = 2 , LARGE = 1 , OK = 0 }
 
enum class  SnapshotCompletionResult {
  SUCCESS , SKIPPED , MISSING_CHAINPARAMS , STATS_FAILED ,
  HASH_MISMATCH , BASE_BLOCKHASH_MISMATCH
}
 

Functions

CAmount GetBlockSubsidy (int nHeight, const Consensus::Params &consensusParams)
 
bool FatalError (kernel::Notifications &notifications, BlockValidationState &state, const bilingual_str &message)
 
double GuessVerificationProgress (const ChainTxData &data, const CBlockIndex *pindex)
 Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). More...
 
void PruneBlockFilesManual (Chainstate &active_chainstate, int nManualPruneHeight)
 Prune block files up to a given height. More...
 
MempoolAcceptResult AcceptToMemoryPool (Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Try to add a transaction to the mempool. More...
 
PackageMempoolAcceptResult ProcessNewPackage (Chainstate &active_chainstate, CTxMemPool &pool, const Package &txns, bool test_accept, const std::optional< CFeeRate > &client_maxfeerate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Validate (and maybe submit) a package to the mempool. More...
 
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. More...
 
bool InitScriptExecutionCache (size_t max_size_bytes)
 Initializes the script-execution cache. More...
 
bool CheckBlock (const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW=true, bool fCheckMerkleRoot=true)
 Functions for validating blocks and updating the block tree. More...
 
bool TestBlockValidity (BlockValidationState &state, const CChainParams &chainparams, Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev, bool fCheckPOW=true, bool fCheckMerkleRoot=true) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Check a block is completely valid from start to finish (only works on top of our current best block) More...
 
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. More...
 
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). More...
 
arith_uint256 CalculateClaimedHeadersWork (const std::vector< CBlockHeader > &headers)
 Return the sum of the claimed work on a given set of headers. More...
 
template<typename DEP >
bool DeploymentActiveAfter (const CBlockIndex *pindexPrev, const ChainstateManager &chainman, DEP dep)
 Deployment* info via ChainstateManager. More...
 
template<typename DEP >
bool DeploymentActiveAt (const CBlockIndex &index, const ChainstateManager &chainman, DEP dep)
 
template<typename DEP >
bool DeploymentEnabled (const ChainstateManager &chainman, DEP dep)
 
bool IsBIP30Repeat (const CBlockIndex &block_index)
 Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) More...
 
bool IsBIP30Unspendable (const CBlockIndex &block_index)
 Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30) More...
 

Variables

static const unsigned int MIN_BLOCKS_TO_KEEP = 288
 Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned. More...
 
static const signed int DEFAULT_CHECKBLOCKS = 6
 
static constexpr int DEFAULT_CHECKLEVEL {3}
 
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024
 
GlobalMutex g_best_block_mutex
 
std::condition_variable g_best_block_cv
 
uint256 g_best_block
 Used to notify getblocktemplate RPC of new tips. More...
 
const std::vector< std::string > CHECKLEVEL_DOC
 Documentation for argument 'checklevel'. More...
 
bool CheckFinalTxAtTip(const CBlockIndex &active_chain_tip, const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(std::optional< LockPointsCalculateLockPointsAtTip (CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
 Check if transaction will be final in the next block to be created. More...
 

Enumeration Type Documentation

◆ CoinsCacheSizeState

enum CoinsCacheSizeState
strong
Enumerator
CRITICAL 

The coins cache is in immediate need of a flush.

LARGE 

The cache is at >= 90% capacity.

OK 

Definition at line 466 of file validation.h.

◆ DisconnectResult

Enumerator
DISCONNECT_OK 
DISCONNECT_UNCLEAN 
DISCONNECT_FAILED 

Definition at line 414 of file validation.h.

◆ FlushStateMode

enum FlushStateMode
strong
See also
Chainstate::FlushStateToDisk
Enumerator
NONE 
IF_NEEDED 
PERIODIC 
ALWAYS 

Definition at line 424 of file validation.h.

◆ SnapshotCompletionResult

Enumerator
SUCCESS 
SKIPPED 
MISSING_CHAINPARAMS 
STATS_FAILED 
HASH_MISMATCH 
BASE_BLOCKHASH_MISMATCH 

Definition at line 800 of file validation.h.

◆ SynchronizationState

enum SynchronizationState
strong

Current sync state passed to tip changed callbacks.

Enumerator
INIT_REINDEX 
INIT_DOWNLOAD 
POST_INIT 

Definition at line 80 of file validation.h.

◆ VerifyDBResult

enum VerifyDBResult
strong
Enumerator
SUCCESS 
CORRUPTED_BLOCK_DB 
INTERRUPTED 
SKIPPED_L3_CHECKS 
SKIPPED_MISSING_BLOCKS 

Definition at line 389 of file validation.h.

Function Documentation

◆ AcceptToMemoryPool()

MempoolAcceptResult AcceptToMemoryPool ( Chainstate active_chainstate,
const CTransactionRef tx,
int64_t  accept_time,
bool  bypass_limits,
bool  test_accept 
)

Try to add a transaction to the mempool.

This is an internal function and is exposed only for testing. Client code should use ChainstateManager::ProcessTransaction()

Parameters
[in]active_chainstateReference to the active chainstate.
[in]txThe transaction to submit for mempool acceptance.
[in]accept_timeThe timestamp for adding the transaction to the mempool. It is also used to determine when the entry expires.
[in]bypass_limitsWhen true, don't enforce mempool fee and capacity limits, and set entry_sequence to zero.
[in]test_acceptWhen true, run validation checks but don't submit to mempool.
Returns
a MempoolAcceptResult indicating whether the transaction was accepted/rejected with reason.

Definition at line 1748 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateClaimedHeadersWork()

arith_uint256 CalculateClaimedHeadersWork ( const std::vector< CBlockHeader > &  headers)

Return the sum of the claimed work on a given set of headers.

No verification of PoW is done.

Definition at line 4043 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckBlock()

bool CheckBlock ( const CBlock block,
BlockValidationState state,
const Consensus::Params consensusParams,
bool  fCheckPOW = true,
bool  fCheckMerkleRoot = true 
)

Functions for validating blocks and updating the block tree.

Context-independent validity checks

Definition at line 3902 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckSequenceLocksAtTip()

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.

Parameters
[in]tipChain tip to check tx sequence locks against. For example, the tip of the current active chain.
[in]lock_pointsLockPoints containing the height and time at which this transaction is final. Simulates calling SequenceLocks() with data from the tip passed in. The LockPoints should not be considered valid if CheckSequenceLocksAtTip returns false.

Definition at line 242 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeploymentActiveAfter()

template<typename DEP >
bool DeploymentActiveAfter ( const CBlockIndex pindexPrev,
const ChainstateManager chainman,
DEP  dep 
)

Deployment* info via ChainstateManager.

Definition at line 1287 of file validation.h.

Here is the call graph for this function:

◆ DeploymentActiveAt()

template<typename DEP >
bool DeploymentActiveAt ( const CBlockIndex index,
const ChainstateManager chainman,
DEP  dep 
)

Definition at line 1293 of file validation.h.

Here is the call graph for this function:

◆ DeploymentEnabled()

template<typename DEP >
bool DeploymentEnabled ( const ChainstateManager chainman,
DEP  dep 
)

Definition at line 1299 of file validation.h.

Here is the call graph for this function:

◆ FatalError()

bool FatalError ( kernel::Notifications notifications,
BlockValidationState state,
const bilingual_str message 
)

Definition at line 2115 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetBlockSubsidy()

CAmount GetBlockSubsidy ( int  nHeight,
const Consensus::Params consensusParams 
)

Definition at line 1811 of file validation.cpp.

Here is the caller graph for this function:

◆ GuessVerificationProgress()

double GuessVerificationProgress ( const ChainTxData data,
const CBlockIndex pindex 
)

Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).

Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).

Definition at line 5456 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HasValidProofOfWork()

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.

Definition at line 4006 of file validation.cpp.

◆ InitScriptExecutionCache()

bool InitScriptExecutionCache ( size_t  max_size_bytes)

Initializes the script-execution cache.

Definition at line 1990 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsBIP30Repeat()

bool IsBIP30Repeat ( const CBlockIndex block_index)

Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30)

Definition at line 6180 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsBIP30Unspendable()

bool IsBIP30Unspendable ( const CBlockIndex block_index)

Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30)

Definition at line 6186 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsBlockMutated()

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).

Definition at line 4012 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProcessNewPackage()

PackageMempoolAcceptResult ProcessNewPackage ( Chainstate active_chainstate,
CTxMemPool pool,
const Package txns,
bool  test_accept,
const std::optional< CFeeRate > &  client_maxfeerate 
)

Validate (and maybe submit) a package to the mempool.

See doc/policy/packages.md for full details on package validation rules.

Parameters
[in]test_acceptWhen true, run validation checks but don't submit to mempool.
[in]client_maxfeerateIf exceeded by an individual transaction, rest of (sub)package evaluation is aborted. Only for sanity checks against local submission of transactions.
Returns
a PackageMempoolAcceptResult which includes a MempoolAcceptResult for each transaction. If a transaction fails, validation will exit early and some results may be missing. It is also possible for the package to be partially submitted.

Definition at line 1779 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PruneBlockFilesManual()

void PruneBlockFilesManual ( Chainstate active_chainstate,
int  nManualPruneHeight 
)

Prune block files up to a given height.

Definition at line 4539 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestBlockValidity()

bool TestBlockValidity ( BlockValidationState state,
const CChainParams chainparams,
Chainstate chainstate,
const CBlock block,
CBlockIndex pindexPrev,
bool  fCheckPOW = true,
bool  fCheckMerkleRoot = true 
)

Check a block is completely valid from start to finish (only works on top of our current best block)

Definition at line 4500 of file validation.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ CalculateLockPointsAtTip

bool CheckFinalTxAtTip (const CBlockIndex& active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED( std::optional<LockPoints> CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx) ( CBlockIndex tip,
const CCoinsView coins_view,
const CTransaction tx 
)

Check if transaction will be final in the next block to be created.

Calculate LockPoints required to check if transaction will be BIP68 final in the next block to be created on top of tip.

Parameters
[in]tipChain tip for which tx sequence locks are calculated. For example, the tip of the current active chain.
[in]coins_viewAny CCoinsView that provides access to the relevant coins for checking sequence locks. For example, it can be a CCoinsViewCache that isn't connected to anything but contains all the relevant coins, or a CCoinsViewMemPool that is connected to the mempool and chainstate UTXO set. In the latter case, the caller is responsible for holding the appropriate locks to ensure that calls to GetCoin() return correct coins.
[in]txThe transaction being evaluated.
Returns
The resulting height and time calculated and the hash of the block needed for calculation, or std::nullopt if there is an error.

Definition at line 312 of file validation.h.

◆ CHECKLEVEL_DOC

const std::vector<std::string> CHECKLEVEL_DOC
extern

Documentation for argument 'checklevel'.

Definition at line 94 of file validation.cpp.

◆ DEFAULT_CHECKBLOCKS

const signed int DEFAULT_CHECKBLOCKS = 6
static

Definition at line 67 of file validation.h.

◆ DEFAULT_CHECKLEVEL

constexpr int DEFAULT_CHECKLEVEL {3}
staticconstexpr

Definition at line 68 of file validation.h.

◆ g_best_block

uint256 g_best_block
extern

Used to notify getblocktemplate RPC of new tips.

Definition at line 111 of file validation.cpp.

◆ g_best_block_cv

std::condition_variable g_best_block_cv
extern

Definition at line 110 of file validation.cpp.

◆ g_best_block_mutex

GlobalMutex g_best_block_mutex
extern

Definition at line 109 of file validation.cpp.

◆ MIN_BLOCKS_TO_KEEP

const unsigned int MIN_BLOCKS_TO_KEEP = 288
static

Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned.

Definition at line 66 of file validation.h.

◆ MIN_DISK_SPACE_FOR_BLOCK_FILES

const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024
static

Definition at line 77 of file validation.h.