6 #ifndef BITCOIN_VALIDATION_H 7 #define BITCOIN_VALIDATION_H 9 #if defined(HAVE_CONFIG_H) 50 static const bool DEFAULT_WHITELISTRELAY =
true;
52 static const bool DEFAULT_WHITELISTFORCERELAY =
true;
54 static const unsigned int DEFAULT_MIN_RELAY_TX_FEE = 1000;
56 static const CAmount DEFAULT_TRANSACTION_MAXFEE = COIN / 10;
58 static const CAmount HIGH_TX_FEE_PER_KB = COIN / 100;
60 static const CAmount HIGH_MAX_TX_FEE = 100 * HIGH_TX_FEE_PER_KB;
62 static const unsigned int DEFAULT_ANCESTOR_LIMIT = 25;
64 static const unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT = 101;
66 static const unsigned int DEFAULT_DESCENDANT_LIMIT = 25;
68 static const unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT = 101;
70 static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336;
72 static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000;
74 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000;
76 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000;
78 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000;
81 static const int MAX_SCRIPTCHECK_THREADS = 16;
83 static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
85 static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
87 static const unsigned int BLOCK_STALLING_TIMEOUT = 2;
90 static const unsigned int MAX_HEADERS_RESULTS = 2000;
93 static const int MAX_CMPCTBLOCK_DEPTH = 5;
95 static const int MAX_BLOCKTXN_DEPTH = 10;
100 static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
102 static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60;
104 static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
106 static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
108 static const int64_t BLOCK_DOWNLOAD_TIMEOUT_BASE = 1000000;
110 static const int64_t BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 500000;
112 static const int64_t DEFAULT_MAX_TIP_AGE = 24 * 60 * 60;
114 static const int64_t MAX_FEE_ESTIMATION_TIP_AGE = 3 * 60 * 60;
117 static const bool DEFAULT_PERMIT_BAREMULTISIG =
true;
118 static const bool DEFAULT_CHECKPOINTS_ENABLED =
true;
119 static const bool DEFAULT_TXINDEX =
false;
120 static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
122 static const bool DEFAULT_PERSIST_MEMPOOL =
true;
124 static const bool DEFAULT_ENABLE_REPLACEMENT =
true;
126 static const bool DEFAULT_FEEFILTER =
true;
129 static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
132 static const int MAX_UNCONNECTING_HEADERS = 10;
134 static const bool DEFAULT_PEERBLOOMFILTERS =
true;
137 static const int DEFAULT_STOPATHEIGHT = 0;
149 typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher>
BlockMap;
183 static const uint64_t nMinDiskSpace = 52428800;
193 static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
195 static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
197 static const signed int DEFAULT_CHECKBLOCKS = 6;
198 static const unsigned int DEFAULT_CHECKLEVEL = 3;
208 static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
247 bool CheckDiskSpace(uint64_t nAdditionalBytes = 0,
bool blocks_dir = false);
375 std::swap(ptxTo, check.
ptxTo);
376 std::swap(m_tx_out, check.
m_tx_out);
377 std::swap(nIn, check.
nIn);
378 std::swap(nFlags, check.
nFlags);
381 std::swap(txdata, check.
txdata);
461 extern std::unique_ptr<CCoinsViewCache>
pcoinsTip;
464 extern std::unique_ptr<CBlockTreeDB>
pblocktree;
484 static const unsigned int REJECT_INTERNAL = 0x100;
486 static const unsigned int REJECT_HIGHFEE = 0x100;
503 #endif // BITCOIN_VALIDATION_H bool ProcessNewBlock(const CChainParams &chainparams, const std::shared_ptr< const CBlock > pblock, bool fForceProcessing, bool *fNewBlock) LOCKS_EXCLUDED(cs_main)
Process an incoming block.
void FlushStateToDisk()
Flush all state, indexes and buffers to disk.
bool CheckFinalTx(const CTransaction &tx, int flags=-1) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Transaction validation functions.
int64_t nMaxTipAge
If the tip is older than this (in seconds), the node is considered to be in initial block download...
bool AcceptToMemoryPool(CTxMemPool &pool, CValidationState &state, const CTransactionRef &tx, bool *pfMissingInputs, std::list< CTransactionRef > *plTxnReplaced, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
(try to) add transaction to memory pool plTxnReplaced will be appended to with all transactions repla...
bool IsNullDummyEnabled(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms)
Check whether NULLDUMMY (BIP 147) has activated.
bool LoadExternalBlockFile(const CChainParams &chainparams, FILE *fileIn, CDiskBlockPos *dbp=nullptr)
Import blocks from an external file.
enum ScriptError_t ScriptError
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
void PruneOneBlockFile(const int fileNumber)
Mark one block file as pruned.
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
std::atomic_bool g_is_mempool_loaded
void swap(CScriptCheck &check)
An in-memory indexed chain of blocks.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
void PruneBlockFilesManual(int nManualPruneHeight)
Prune block files up to a given height.
CBlockIndex * pindexBestHeader
Best header we've seen so far (used for getheaders queries' starting points).
bool CheckBlock(const CBlock &block, CValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW=true, bool fCheckMerkleRoot=true)
Functions for validating blocks and updating the block tree.
CBlockPolicyEstimator feeEstimator
bool CheckSequenceLocks(const CTxMemPool &pool, const CTransaction &tx, int flags, LockPoints *lp=nullptr, bool useExistingLockPoints=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Check if transaction will be BIP 68 final in the next block to be created.
bool InvalidateBlock(CValidationState &state, const CChainParams &chainparams, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as invalid.
bool LoadChainTip(const CChainParams &chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
bool TestBlockValidity(CValidationState &state, const CChainParams &chainparams, 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) ...
bool LoadMempool()
Load the mempool from disk.
void ResetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove invalidity status from a block and its descendants.
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).
std::shared_ptr< const CTransaction > CTransactionRef
bool ActivateBestChain(CValidationState &state, const CChainParams &chainparams, std::shared_ptr< const CBlock > pblock=std::shared_ptr< const CBlock >())
Find the best known block, and make it the tip of the block chain.
BIP9Stats VersionBitsTipStatistics(const Consensus::Params ¶ms, Consensus::DeploymentPos pos)
Get the numerical statistics for the BIP9 state for a given deployment at the current tip...
bool PreciousBlock(CValidationState &state, const CChainParams ¶ms, CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main)
Mark a block as precious and reorganize.
CChain & chainActive
The currently-connected chain of blocks (protected by cs_main).
CScriptCheck(const CTxOut &outIn, const CTransaction &txToIn, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData *txdataIn)
int64_t CAmount
Amount in satoshis (Can be negative)
#define AssertLockHeld(cs)
bool LoadBlockIndex(const CChainParams &chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex...
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
void UpdateUncommittedBlockStructures(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Update uncommitted block structures (currently: only the witness reserved value). ...
Access to the block database (blocks/index/)
Abstract view on the open txout dataset.
bool GetTransaction(const uint256 &hash, CTransactionRef &tx, const Consensus::Params ¶ms, uint256 &hashBlock, bool fAllowSlow=false, CBlockIndex *blockIndex=nullptr)
Retrieve a transaction (from memory pool, or from disk, if possible)
bool fHavePruned
Pruning-related variables and constants.
void InitScriptExecutionCache()
Initializes the script-execution cache.
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
Translation to a filesystem path.
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
bool TestLockPointValidity(const LockPoints *lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Test whether the LockPoints height and time are still valid on the current chain. ...
Holds various statistics on transactions within a chain.
arith_uint256 nMinimumChainWork
Minimum work we will assume exists on some valid chain.
An output of a transaction.
ThresholdState VersionBitsTipState(const Consensus::Params ¶ms, Consensus::DeploymentPos pos)
Get the BIP9 state for a given deployment at the current tip.
std::atomic_bool fImporting
Parameters that influence chain consensus.
std::string FormatStateMessage(const CValidationState &state)
Convert CValidationState to a human-readable message for logging.
bool ReplayBlocks(const CChainParams ¶ms, CCoinsView *view)
Replay blocks that aren't fully applied to the database.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool CheckDiskSpace(uint64_t nAdditionalBytes=0, bool blocks_dir=false)
Check whether enough disk space is available for an incoming block.
int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms)
Determine what nVersion a new block should use.
size_t operator()(const uint256 &hash) const
256-bit unsigned big integer.
uint64_t nPruneTarget
Number of MiB of block files that we're trying to stay below.
Closure representing one script verification Note that this stores references to the spending transac...
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
std::condition_variable g_best_block_cv
std::atomic_bool fReindex
bool RewindBlockIndex(const CChainParams ¶ms)
When there are blocks in the active chain with missing data, rewind the chainstate and remove them fr...
Capture information about block/transaction validation.
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &block, CValidationState &state, const CChainParams &chainparams, const CBlockIndex **ppindex=nullptr, CBlockHeader *first_invalid=nullptr) LOCKS_EXCLUDED(cs_main)
Process incoming block headers.
const CTransaction * ptxTo
uint64_t nLastBlockWeight
#define EXCLUSIVE_LOCKS_REQUIRED(...)
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
FILE * OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly=false)
Open a block file (blk?????.dat)
#define LOCKS_EXCLUDED(...)
bool IsWitnessEnabled(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms)
Check whether witness commitments are required for block.
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
Serialized script, used inside transaction inputs and outputs.
CCoinsView backed by the coin database (chainstate/)
CAmount maxTxFee
Absolute maximum transaction fee (in satoshis) used by wallet and mempool (rejects high fee in sendra...
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, int nHeight)
Apply the effects of this transaction on the UTXO set represented by view.
uint256 hashAssumeValid
Block hash whose ancestors we will assume to have valid scripts without checking them.
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
std::unique_ptr< CCoinsViewDB > pcoinsdbview
Global variable that points to the coins database (protected by cs_main)
void PruneAndFlush()
Prune block files and flush state to disk.
Fee rate in satoshis per kilobyte: CAmount / kB.
void ThreadScriptCheck()
Run an instance of the script checking thread.
bool error(const char *fmt, const Args &... args)
bool IsInitialBlockDownload()
Check whether we are doing an initial block download (synchronizing from disk or network) ...
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const CDiskBlockPos &pos, const CMessageHeader::MessageStartChars &message_start)
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block between the parameter chain and a locator.
CScript COINBASE_FLAGS
Constant stuff for coinbase transactions we create:
The basic transaction that is broadcasted on the network and contained in blocks. ...
std::unordered_map< uint256, CBlockIndex *, BlockHasher > BlockMap
bool fPruneMode
True if we're running in -prune mode.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
full block available in blk*.dat
int GetSpendHeight(const CCoinsViewCache &inputs)
Return the spend height, which is one more than the inputs.GetBestBlock().
int VersionBitsTipStateSinceHeight(const Consensus::Params ¶ms, Consensus::DeploymentPos pos)
Get the block height at which the BIP9 deployment switched into the state for the block building on t...
uint64_t GetCheapHash() const
A cheap hash function that just returns 64 bits from the result, it can be used when the contents are...
const std::string strMessageMagic
bool LoadGenesisBlock(const CChainParams &chainparams)
Ensures we have a genesis block in the block tree, possibly writing one to disk.
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
VersionBitsCache versionbitscache
ScriptError GetScriptError() const
bool DumpMempool()
Dump the mempool to disk.
std::unique_ptr< CBlockTreeDB > pblocktree
Global variable that points to the active block tree (protected by cs_main)
void UnloadBlockIndex()
Unload database information.
unsigned int nTx
Number of transactions in this block.
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
std::unique_ptr< CCoinsViewCache > pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
PrecomputedTransactionData * txdata
CBlockIndex * LookupBlockIndex(const uint256 &hash)