Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2020 The Bitcoin Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : #ifndef BITCOIN_VALIDATION_H
7 : #define BITCOIN_VALIDATION_H
8 :
9 : #if defined(HAVE_CONFIG_H)
10 : #include <config/bitcoin-config.h>
11 : #endif
12 :
13 : #include <amount.h>
14 : #include <coins.h>
15 : #include <crypto/common.h> // for ReadLE64
16 : #include <fs.h>
17 : #include <optional.h>
18 : #include <policy/feerate.h>
19 : #include <protocol.h> // For CMessageHeader::MessageStartChars
20 : #include <script/script_error.h>
21 : #include <sync.h>
22 : #include <txmempool.h> // For CTxMemPool::cs
23 : #include <txdb.h>
24 : #include <versionbits.h>
25 : #include <serialize.h>
26 :
27 : #include <atomic>
28 : #include <map>
29 : #include <memory>
30 : #include <set>
31 : #include <stdint.h>
32 : #include <string>
33 : #include <utility>
34 : #include <vector>
35 :
36 : class CChainState;
37 : class BlockValidationState;
38 : class CBlockIndex;
39 : class CBlockTreeDB;
40 : class CBlockUndo;
41 : class CChainParams;
42 : class CInv;
43 : class CConnman;
44 : class CScriptCheck;
45 : class CBlockPolicyEstimator;
46 : class CTxMemPool;
47 : class ChainstateManager;
48 : class TxValidationState;
49 : struct ChainTxData;
50 :
51 : struct DisconnectedBlockTransactions;
52 : struct PrecomputedTransactionData;
53 : struct LockPoints;
54 :
55 : /** Default for -minrelaytxfee, minimum relay fee for transactions */
56 : static const unsigned int DEFAULT_MIN_RELAY_TX_FEE = 1000;
57 : /** Default for -limitancestorcount, max number of in-mempool ancestors */
58 : static const unsigned int DEFAULT_ANCESTOR_LIMIT = 25;
59 : /** Default for -limitancestorsize, maximum kilobytes of tx + all in-mempool ancestors */
60 : static const unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT = 101;
61 : /** Default for -limitdescendantcount, max number of in-mempool descendants */
62 : static const unsigned int DEFAULT_DESCENDANT_LIMIT = 25;
63 : /** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */
64 : static const unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT = 101;
65 : /** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
66 : static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336;
67 : /** The maximum size of a blk?????.dat file (since 0.8) */
68 : static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
69 : /** Maximum number of dedicated script-checking threads allowed */
70 : static const int MAX_SCRIPTCHECK_THREADS = 15;
71 : /** -par default (number of script-checking threads, 0 = auto) */
72 : static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
73 : static const int64_t DEFAULT_MAX_TIP_AGE = 24 * 60 * 60;
74 : static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
75 : static const bool DEFAULT_TXINDEX = false;
76 : static const char* const DEFAULT_BLOCKFILTERINDEX = "0";
77 : /** Default for -persistmempool */
78 : static const bool DEFAULT_PERSIST_MEMPOOL = true;
79 : /** Default for using fee filter */
80 : static const bool DEFAULT_FEEFILTER = true;
81 : /** Default for -stopatheight */
82 : static const int DEFAULT_STOPATHEIGHT = 0;
83 : /** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ::ChainActive().Tip() will not be pruned. */
84 : static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
85 : static const signed int DEFAULT_CHECKBLOCKS = 6;
86 : static const unsigned int DEFAULT_CHECKLEVEL = 3;
87 : // Require that user allocate at least 550 MiB for block & undo files (blk???.dat and rev???.dat)
88 : // At 1MB per block, 288 blocks = 288MB.
89 : // Add 15% for Undo data = 331MB
90 : // Add 20% for Orphan block rate = 397MB
91 : // We want the low water mark after pruning to be at least 397 MB and since we prune in
92 : // full block file chunks, we need the high water mark which triggers the prune to be
93 : // one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB
94 : // Setting the target to >= 550 MiB will make it likely we can respect the target.
95 : static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
96 : /** Minimum size of a witness commitment structure. Defined in BIP 141. **/
97 : static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
98 :
99 : struct BlockHasher
100 : {
101 : // this used to call `GetCheapHash()` in uint256, which was later moved; the
102 : // cheap hash function simply calls ReadLE64() however, so the end result is
103 : // identical
104 1086897 : size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
105 : };
106 :
107 : /** Current sync state passed to tip changed callbacks. */
108 : enum class SynchronizationState {
109 : INIT_REINDEX,
110 : INIT_DOWNLOAD,
111 : POST_INIT
112 : };
113 :
114 : extern RecursiveMutex cs_main;
115 : extern CBlockPolicyEstimator feeEstimator;
116 : typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
117 : extern Mutex g_best_block_mutex;
118 : extern std::condition_variable g_best_block_cv;
119 : extern uint256 g_best_block;
120 : extern std::atomic_bool fImporting;
121 : extern std::atomic_bool fReindex;
122 : /** Whether there are dedicated script-checking threads running.
123 : * False indicates all script checking is done on the main threadMessageHandler thread.
124 : */
125 : extern bool g_parallel_script_checks;
126 : extern bool fRequireStandard;
127 : extern bool fCheckBlockIndex;
128 : extern bool fCheckpointsEnabled;
129 : /** A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) */
130 : extern CFeeRate minRelayTxFee;
131 : /** If the tip is older than this (in seconds), the node is considered to be in initial block download. */
132 : extern int64_t nMaxTipAge;
133 :
134 : /** Block hash whose ancestors we will assume to have valid scripts without checking them. */
135 : extern uint256 hashAssumeValid;
136 :
137 : /** Minimum work we will assume exists on some valid chain. */
138 : extern arith_uint256 nMinimumChainWork;
139 :
140 : /** Best header we've seen so far (used for getheaders queries' starting points). */
141 : extern CBlockIndex *pindexBestHeader;
142 :
143 : /** Pruning-related variables and constants */
144 : /** True if any block files have ever been pruned. */
145 : extern bool fHavePruned;
146 : /** True if we're running in -prune mode. */
147 : extern bool fPruneMode;
148 : /** Number of MiB of block files that we're trying to stay below. */
149 : extern uint64_t nPruneTarget;
150 : /** Documentation for argument 'checklevel'. */
151 : extern const std::vector<std::string> CHECKLEVEL_DOC;
152 :
153 : /** Open a block file (blk?????.dat) */
154 : FILE* OpenBlockFile(const FlatFilePos &pos, bool fReadOnly = false);
155 : /** Translation to a filesystem path */
156 : fs::path GetBlockPosFilename(const FlatFilePos &pos);
157 : /** Import blocks from an external file */
158 : void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFilePos* dbp = nullptr);
159 : /** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
160 : bool LoadGenesisBlock(const CChainParams& chainparams);
161 : /** Unload database information */
162 : void UnloadBlockIndex(CTxMemPool* mempool);
163 : /** Run an instance of the script checking thread */
164 : void ThreadScriptCheck(int worker_num);
165 : /**
166 : * Return transaction from the block at block_index.
167 : * If block_index is not provided, fall back to mempool.
168 : * If mempool is not provided or the tx couldn't be found in mempool, fall back to g_txindex.
169 : *
170 : * @param[in] block_index The block to read from disk, or nullptr
171 : * @param[in] mempool If block_index is not provided, look in the mempool, if provided
172 : * @param[in] hash The txid
173 : * @param[in] consensusParams The params
174 : * @param[out] hashBlock The hash of block_index, if the tx was found via block_index
175 : * @returns The tx if found, otherwise nullptr
176 : */
177 : CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
178 : /**
179 : * Find the best known block, and make it the tip of the block chain
180 : *
181 : * May not be called with cs_main held. May not be called in a
182 : * validationinterface callback.
183 : */
184 : bool ActivateBestChain(BlockValidationState& state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>());
185 : CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
186 :
187 : /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
188 : double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex* pindex);
189 :
190 : /** Calculate the amount of disk space the block & undo files currently use */
191 : uint64_t CalculateCurrentUsage();
192 :
193 : /**
194 : * Actually unlink the specified files
195 : */
196 : void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
197 :
198 : /** Prune block files up to a given height */
199 : void PruneBlockFilesManual(int nManualPruneHeight);
200 :
201 : /** (try to) add transaction to memory pool
202 : * plTxnReplaced will be appended to with all transactions replaced from mempool **/
203 : bool AcceptToMemoryPool(CTxMemPool& pool, TxValidationState &state, const CTransactionRef &tx,
204 : std::list<CTransactionRef>* plTxnReplaced,
205 : bool bypass_limits, const CAmount nAbsurdFee, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
206 :
207 : /** Get the BIP9 state for a given deployment at the current tip. */
208 : ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos);
209 :
210 : /** Get the numerical statistics for the BIP9 state for a given deployment at the current tip. */
211 : BIP9Stats VersionBitsTipStatistics(const Consensus::Params& params, Consensus::DeploymentPos pos);
212 :
213 : /** Get the block height at which the BIP9 deployment switched into the state for the block building on the current tip. */
214 : int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::DeploymentPos pos);
215 :
216 :
217 : /** Apply the effects of this transaction on the UTXO set represented by view */
218 : void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight);
219 :
220 : /** Transaction validation functions */
221 :
222 : /**
223 : * Check if transaction will be final in the next block to be created.
224 : *
225 : * Calls IsFinalTx() with current block height and appropriate block time.
226 : *
227 : * See consensus/consensus.h for flag definitions.
228 : */
229 : bool CheckFinalTx(const CTransaction &tx, int flags = -1) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
230 :
231 : /**
232 : * Test whether the LockPoints height and time are still valid on the current chain
233 : */
234 : bool TestLockPointValidity(const LockPoints* lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
235 :
236 : /**
237 : * Check if transaction will be BIP 68 final in the next block to be created.
238 : *
239 : * Simulates calling SequenceLocks() with data from the tip of the current active chain.
240 : * Optionally stores in LockPoints the resulting height and time calculated and the hash
241 : * of the block needed for calculation or skips the calculation and uses the LockPoints
242 : * passed in for evaluation.
243 : * The LockPoints should not be considered valid if CheckSequenceLocks returns false.
244 : *
245 : * See consensus/consensus.h for flag definitions.
246 : */
247 : bool CheckSequenceLocks(const CTxMemPool& pool, const CTransaction& tx, int flags, LockPoints* lp = nullptr, bool useExistingLockPoints = false) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs);
248 :
249 : /**
250 : * Closure representing one script verification
251 : * Note that this stores references to the spending transaction
252 : */
253 2098124 : class CScriptCheck
254 : {
255 : private:
256 : CTxOut m_tx_out;
257 : const CTransaction *ptxTo;
258 : unsigned int nIn;
259 : unsigned int nFlags;
260 : bool cacheStore;
261 : ScriptError error;
262 : PrecomputedTransactionData *txdata;
263 :
264 : public:
265 488824 : CScriptCheck(): ptxTo(nullptr), nIn(0), nFlags(0), cacheStore(false), error(SCRIPT_ERR_UNKNOWN_ERROR) {}
266 807790 : CScriptCheck(const CTxOut& outIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData* txdataIn) :
267 807790 : m_tx_out(outIn), ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), error(SCRIPT_ERR_UNKNOWN_ERROR), txdata(txdataIn) { }
268 :
269 : bool operator()();
270 :
271 244407 : void swap(CScriptCheck &check) {
272 244407 : std::swap(ptxTo, check.ptxTo);
273 244407 : std::swap(m_tx_out, check.m_tx_out);
274 244407 : std::swap(nIn, check.nIn);
275 244407 : std::swap(nFlags, check.nFlags);
276 244407 : std::swap(cacheStore, check.cacheStore);
277 244407 : std::swap(error, check.error);
278 244407 : std::swap(txdata, check.txdata);
279 244407 : }
280 :
281 63404 : ScriptError GetScriptError() const { return error; }
282 : };
283 :
284 : /** Initializes the script-execution cache */
285 : void InitScriptExecutionCache();
286 :
287 :
288 : /** Functions for disk access for blocks */
289 : bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
290 : bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
291 : bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
292 : bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
293 :
294 : bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
295 :
296 : /** Functions for validating blocks and updating the block tree */
297 :
298 : /** Context-independent validity checks */
299 : bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
300 :
301 : /** Check a block is completely valid from start to finish (only works on top of our current best block) */
302 : bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
303 :
304 : /** Check whether witness commitments are required for a block, and whether to enforce NULLDUMMY (BIP 147) rules.
305 : * Note that transaction witness validation rules are always enforced when P2SH is enforced. */
306 : bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);
307 :
308 : /** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
309 : int GetWitnessCommitmentIndex(const CBlock& block);
310 :
311 : /** Update uncommitted block structures (currently: only the witness reserved value). This is safe for submitted blocks. */
312 : void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams);
313 :
314 : /** Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks). */
315 : std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams);
316 :
317 : /** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
318 : class CVerifyDB {
319 : public:
320 : CVerifyDB();
321 : ~CVerifyDB();
322 : bool VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
323 : };
324 :
325 : CBlockIndex* LookupBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
326 :
327 : /** Find the last common block between the parameter chain and a locator. */
328 : CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
329 :
330 : enum DisconnectResult
331 : {
332 : DISCONNECT_OK, // All good.
333 : DISCONNECT_UNCLEAN, // Rolled back, but UTXO set was inconsistent with block.
334 : DISCONNECT_FAILED // Something else went wrong.
335 : };
336 :
337 : class ConnectTrace;
338 :
339 : /** @see CChainState::FlushStateToDisk */
340 : enum class FlushStateMode {
341 : NONE,
342 : IF_NEEDED,
343 : PERIODIC,
344 : ALWAYS
345 : };
346 :
347 : struct CBlockIndexWorkComparator
348 : {
349 : bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const;
350 : };
351 :
352 : /**
353 : * Maintains a tree of blocks (stored in `m_block_index`) which is consulted
354 : * to determine where the most-work tip is.
355 : *
356 : * This data is used mostly in `CChainState` - information about, e.g.,
357 : * candidate tips is not maintained here.
358 : */
359 2574 : class BlockManager {
360 : public:
361 : BlockMap m_block_index GUARDED_BY(cs_main);
362 :
363 : /** In order to efficiently track invalidity of headers, we keep the set of
364 : * blocks which we tried to connect and found to be invalid here (ie which
365 : * were set to BLOCK_FAILED_VALID since the last restart). We can then
366 : * walk this set and check if a new header is a descendant of something in
367 : * this set, preventing us from having to walk m_block_index when we try
368 : * to connect a bad block and fail.
369 : *
370 : * While this is more complicated than marking everything which descends
371 : * from an invalid block as invalid at the time we discover it to be
372 : * invalid, doing so would require walking all of m_block_index to find all
373 : * descendants. Since this case should be very rare, keeping track of all
374 : * BLOCK_FAILED_VALID blocks in a set should be just fine and work just as
375 : * well.
376 : *
377 : * Because we already walk m_block_index in height-order at startup, we go
378 : * ahead and mark descendants of invalid blocks as FAILED_CHILD at that time,
379 : * instead of putting things in this set.
380 : */
381 : std::set<CBlockIndex*> m_failed_blocks;
382 :
383 : /**
384 : * All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
385 : * Pruned nodes may have entries where B is missing data.
386 : */
387 : std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
388 :
389 : /**
390 : * Load the blocktree off disk and into memory. Populate certain metadata
391 : * per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
392 : * collections like setDirtyBlockIndex.
393 : *
394 : * @param[out] block_index_candidates Fill this set with any valid blocks for
395 : * which we've downloaded all transactions.
396 : */
397 : bool LoadBlockIndex(
398 : const Consensus::Params& consensus_params,
399 : CBlockTreeDB& blocktree,
400 : std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
401 : EXCLUSIVE_LOCKS_REQUIRED(cs_main);
402 :
403 : /** Clear all data members. */
404 : void Unload() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
405 :
406 : CBlockIndex* AddToBlockIndex(const CBlockHeader& block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
407 : /** Create a new block index entry for a given block hash */
408 : CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
409 :
410 : /**
411 : * If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
412 : * that it doesn't descend from an invalid block, and then add it to m_block_index.
413 : */
414 : bool AcceptBlockHeader(
415 : const CBlockHeader& block,
416 : BlockValidationState& state,
417 : const CChainParams& chainparams,
418 : CBlockIndex** ppindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
419 : };
420 :
421 : /**
422 : * A convenience class for constructing the CCoinsView* hierarchy used
423 : * to facilitate access to the UTXO set.
424 : *
425 : * This class consists of an arrangement of layered CCoinsView objects,
426 : * preferring to store and retrieve coins in memory via `m_cacheview` but
427 : * ultimately falling back on cache misses to the canonical store of UTXOs on
428 : * disk, `m_dbview`.
429 : */
430 1206 : class CoinsViews {
431 :
432 : public:
433 : //! The lowest level of the CoinsViews cache hierarchy sits in a leveldb database on disk.
434 : //! All unspent coins reside in this store.
435 : CCoinsViewDB m_dbview GUARDED_BY(cs_main);
436 :
437 : //! This view wraps access to the leveldb instance and handles read errors gracefully.
438 : CCoinsViewErrorCatcher m_catcherview GUARDED_BY(cs_main);
439 :
440 : //! This is the top layer of the cache hierarchy - it keeps as many coins in memory as
441 : //! can fit per the dbcache setting.
442 : std::unique_ptr<CCoinsViewCache> m_cacheview GUARDED_BY(cs_main);
443 :
444 : //! This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it
445 : //! *does not* create a CCoinsViewCache instance by default. This is done separately because the
446 : //! presence of the cache has implications on whether or not we're allowed to flush the cache's
447 : //! state to disk, which should not be done until the health of the database is verified.
448 : //!
449 : //! All arguments forwarded onto CCoinsViewDB.
450 : CoinsViews(std::string ldb_name, size_t cache_size_bytes, bool in_memory, bool should_wipe);
451 :
452 : //! Initialize the CCoinsViewCache member.
453 : void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
454 : };
455 :
456 : enum class CoinsCacheSizeState
457 : {
458 : //! The coins cache is in immediate need of a flush.
459 : CRITICAL = 2,
460 : //! The cache is at >= 90% capacity.
461 : LARGE = 1,
462 : OK = 0
463 : };
464 :
465 : /**
466 : * CChainState stores and provides an API to update our local knowledge of the
467 : * current best chain.
468 : *
469 : * Eventually, the API here is targeted at being exposed externally as a
470 : * consumable libconsensus library, so any functions added must only call
471 : * other class member functions, pure functions in other parts of the consensus
472 : * library, callbacks via the validation interface, or read/write-to-disk
473 : * functions (eventually this will also be via callbacks).
474 : *
475 : * Anything that is contingent on the current tip of the chain is stored here,
476 : * whereas block information and metadata independent of the current tip is
477 : * kept in `BlockMetadataManager`.
478 : */
479 1206 : class CChainState {
480 : private:
481 :
482 : /**
483 : * Every received block is assigned a unique and increasing identifier, so we
484 : * know which one to give priority in case of a fork.
485 : */
486 : RecursiveMutex cs_nBlockSequenceId;
487 : /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
488 : int32_t nBlockSequenceId = 1;
489 : /** Decreasing counter (used by subsequent preciousblock calls). */
490 : int32_t nBlockReverseSequenceId = -1;
491 : /** chainwork for the last block that preciousblock has been applied to. */
492 : arith_uint256 nLastPreciousChainwork = 0;
493 :
494 : /**
495 : * the ChainState CriticalSection
496 : * A lock that must be held when modifying this ChainState - held in ActivateBestChain()
497 : */
498 : RecursiveMutex m_cs_chainstate;
499 :
500 : /**
501 : * Whether this chainstate is undergoing initial block download.
502 : *
503 : * Mutable because we need to be able to mark IsInitialBlockDownload()
504 : * const, which latches this for caching purposes.
505 : */
506 : mutable std::atomic<bool> m_cached_finished_ibd{false};
507 :
508 : //! Reference to a BlockManager instance which itself is shared across all
509 : //! CChainState instances. Keeping a local reference allows us to test more
510 : //! easily as opposed to referencing a global.
511 : BlockManager& m_blockman;
512 :
513 : //! mempool that is kept in sync with the chain
514 : CTxMemPool& m_mempool;
515 :
516 : //! Manages the UTXO set, which is a reflection of the contents of `m_chain`.
517 : std::unique_ptr<CoinsViews> m_coins_views;
518 :
519 : public:
520 : explicit CChainState(CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash = uint256());
521 :
522 : /**
523 : * Initialize the CoinsViews UTXO set database management data structures. The in-memory
524 : * cache is initialized separately.
525 : *
526 : * All parameters forwarded to CoinsViews.
527 : */
528 : void InitCoinsDB(
529 : size_t cache_size_bytes,
530 : bool in_memory,
531 : bool should_wipe,
532 : std::string leveldb_name = "chainstate");
533 :
534 : //! Initialize the in-memory coins cache (to be done after the health of the on-disk database
535 : //! is verified).
536 : void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
537 :
538 : //! @returns whether or not the CoinsViews object has been fully initialized and we can
539 : //! safely flush this object to disk.
540 163195 : bool CanFlushToDisk() EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
541 163195 : return m_coins_views && m_coins_views->m_cacheview;
542 : }
543 :
544 : //! The current chain of blockheaders we consult and build on.
545 : //! @see CChain, CBlockIndex.
546 : CChain m_chain;
547 :
548 : /**
549 : * The blockhash which is the base of the snapshot this chainstate was created from.
550 : *
551 : * IsNull() if this chainstate was not created from a snapshot.
552 : */
553 : const uint256 m_from_snapshot_blockhash{};
554 :
555 : /**
556 : * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
557 : * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
558 : * missing the data for the block.
559 : */
560 : std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
561 :
562 : //! @returns A reference to the in-memory cache of the UTXO set.
563 1511700 : CCoinsViewCache& CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
564 : {
565 1511700 : assert(m_coins_views->m_cacheview);
566 1511700 : return *m_coins_views->m_cacheview.get();
567 : }
568 :
569 : //! @returns A reference to the on-disk UTXO set database.
570 1350 : CCoinsViewDB& CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
571 : {
572 1350 : return m_coins_views->m_dbview;
573 : }
574 :
575 : //! @returns A reference to a wrapped view of the in-memory UTXO set that
576 : //! handles disk read errors gracefully.
577 498 : CCoinsViewErrorCatcher& CoinsErrorCatcher() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
578 : {
579 498 : return m_coins_views->m_catcherview;
580 : }
581 :
582 : //! Destructs all objects related to accessing the UTXO set.
583 498 : void ResetCoinsViews() { m_coins_views.reset(); }
584 :
585 : //! The cache size of the on-disk coins view.
586 : size_t m_coinsdb_cache_size_bytes{0};
587 :
588 : //! The cache size of the in-memory coins view.
589 : size_t m_coinstip_cache_size_bytes{0};
590 :
591 : //! Resize the CoinsViews caches dynamically and flush state to disk.
592 : //! @returns true unless an error occurred during the flush.
593 : bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
594 : EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
595 :
596 : /**
597 : * Update the on-disk chain state.
598 : * The caches and indexes are flushed depending on the mode we're called with
599 : * if they're too large, if it's been a while since the last write,
600 : * or always and in all cases if we're in prune mode and are deleting files.
601 : *
602 : * If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything
603 : * besides checking if we need to prune.
604 : *
605 : * @returns true unless a system error occurred
606 : */
607 : bool FlushStateToDisk(
608 : const CChainParams& chainparams,
609 : BlockValidationState &state,
610 : FlushStateMode mode,
611 : int nManualPruneHeight = 0);
612 :
613 : //! Unconditionally flush all changes to disk.
614 : void ForceFlushStateToDisk();
615 :
616 : //! Prune blockfiles from the disk if necessary and then flush chainstate changes
617 : //! if we pruned.
618 : void PruneAndFlush();
619 :
620 : /**
621 : * Make the best chain active, in multiple steps. The result is either failure
622 : * or an activated best chain. pblock is either nullptr or a pointer to a block
623 : * that is already loaded (to avoid loading it again from disk).
624 : *
625 : * ActivateBestChain is split into steps (see ActivateBestChainStep) so that
626 : * we avoid holding cs_main for an extended period of time; the length of this
627 : * call may be quite long during reindexing or a substantial reorg.
628 : *
629 : * May not be called with cs_main held. May not be called in a
630 : * validationinterface callback.
631 : *
632 : * @returns true unless a system error occurred
633 : */
634 : bool ActivateBestChain(
635 : BlockValidationState& state,
636 : const CChainParams& chainparams,
637 : std::shared_ptr<const CBlock> pblock) LOCKS_EXCLUDED(cs_main);
638 :
639 : bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
640 :
641 : // Block (dis)connection on a given view:
642 : DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view);
643 : bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
644 : CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
645 :
646 : // Apply the effects of a block disconnection on the UTXO set.
647 : bool DisconnectTip(BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool.cs);
648 :
649 : // Manual block validity manipulation:
650 : bool PreciousBlock(BlockValidationState& state, const CChainParams& params, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
651 : bool InvalidateBlock(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
652 : void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
653 :
654 : /** Replay blocks that aren't fully applied to the database. */
655 : bool ReplayBlocks(const CChainParams& params);
656 : bool RewindBlockIndex(const CChainParams& params) LOCKS_EXCLUDED(cs_main);
657 : bool LoadGenesisBlock(const CChainParams& chainparams);
658 :
659 : void PruneBlockIndexCandidates();
660 :
661 : void UnloadBlockIndex();
662 :
663 : /** Check whether we are doing an initial block download (synchronizing from disk or network) */
664 : bool IsInitialBlockDownload() const;
665 :
666 : /**
667 : * Make various assertions about the state of the block index.
668 : *
669 : * By default this only executes fully when using the Regtest chain; see: fCheckBlockIndex.
670 : */
671 : void CheckBlockIndex(const Consensus::Params& consensusParams);
672 :
673 : /** Load the persisted mempool from disk */
674 : void LoadMempool(const ArgsManager& args);
675 :
676 : /** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
677 : bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
678 :
679 : //! Dictates whether we need to flush the cache to disk or not.
680 : //!
681 : //! @return the state of the size of the coins cache.
682 : CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
683 : EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
684 :
685 : CoinsCacheSizeState GetCoinsCacheSizeState(
686 : const CTxMemPool* tx_pool,
687 : size_t max_coins_cache_size_bytes,
688 : size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
689 :
690 : std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
691 :
692 : private:
693 : bool ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool.cs);
694 : bool ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool.cs);
695 :
696 : void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
697 : CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
698 : void ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
699 :
700 : bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs, const CChainParams& params) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
701 :
702 : //! Mark a block as not having block data
703 : void EraseBlockData(CBlockIndex* index) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
704 :
705 : friend ChainstateManager;
706 : };
707 :
708 : /** Mark a block as precious and reorganize.
709 : *
710 : * May not be called in a
711 : * validationinterface callback.
712 : */
713 : bool PreciousBlock(BlockValidationState& state, const CChainParams& params, CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main);
714 :
715 : /** Mark a block as invalid. */
716 : bool InvalidateBlock(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
717 :
718 : /** Remove invalidity status from a block and its descendants. */
719 : void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
720 :
721 : /**
722 : * Provides an interface for creating and interacting with one or two
723 : * chainstates: an IBD chainstate generated by downloading blocks, and
724 : * an optional snapshot chainstate loaded from a UTXO snapshot. Managed
725 : * chainstates can be maintained at different heights simultaneously.
726 : *
727 : * This class provides abstractions that allow the retrieval of the current
728 : * most-work chainstate ("Active") as well as chainstates which may be in
729 : * background use to validate UTXO snapshots.
730 : *
731 : * Definitions:
732 : *
733 : * *IBD chainstate*: a chainstate whose current state has been "fully"
734 : * validated by the initial block download process.
735 : *
736 : * *Snapshot chainstate*: a chainstate populated by loading in an
737 : * assumeutxo UTXO snapshot.
738 : *
739 : * *Active chainstate*: the chainstate containing the current most-work
740 : * chain. Consulted by most parts of the system (net_processing,
741 : * wallet) as a reflection of the current chain and UTXO set.
742 : * This may either be an IBD chainstate or a snapshot chainstate.
743 : *
744 : * *Background IBD chainstate*: an IBD chainstate for which the
745 : * IBD process is happening in the background while use of the
746 : * active (snapshot) chainstate allows the rest of the system to function.
747 : *
748 : * *Validated chainstate*: the most-work chainstate which has been validated
749 : * locally via initial block download. This will be the snapshot chainstate
750 : * if a snapshot was loaded and all blocks up to the snapshot starting point
751 : * have been downloaded and validated (via background validation), otherwise
752 : * it will be the IBD chainstate.
753 : */
754 3858 : class ChainstateManager
755 : {
756 : private:
757 : //! The chainstate used under normal operation (i.e. "regular" IBD) or, if
758 : //! a snapshot is in use, for background validation.
759 : //!
760 : //! Its contents (including on-disk data) will be deleted *upon shutdown*
761 : //! after background validation of the snapshot has completed. We do not
762 : //! free the chainstate contents immediately after it finishes validation
763 : //! to cautiously avoid a case where some other part of the system is still
764 : //! using this pointer (e.g. net_processing).
765 : //!
766 : //! Once this pointer is set to a corresponding chainstate, it will not
767 : //! be reset until init.cpp:Shutdown(). This means it is safe to acquire
768 : //! the contents of this pointer with ::cs_main held, release the lock,
769 : //! and then use the reference without concern of it being deconstructed.
770 : //!
771 : //! This is especially important when, e.g., calling ActivateBestChain()
772 : //! on all chainstates because we are not able to hold ::cs_main going into
773 : //! that call.
774 : std::unique_ptr<CChainState> m_ibd_chainstate;
775 :
776 : //! A chainstate initialized on the basis of a UTXO snapshot. If this is
777 : //! non-null, it is always our active chainstate.
778 : //!
779 : //! Once this pointer is set to a corresponding chainstate, it will not
780 : //! be reset until init.cpp:Shutdown(). This means it is safe to acquire
781 : //! the contents of this pointer with ::cs_main held, release the lock,
782 : //! and then use the reference without concern of it being deconstructed.
783 : //!
784 : //! This is especially important when, e.g., calling ActivateBestChain()
785 : //! on all chainstates because we are not able to hold ::cs_main going into
786 : //! that call.
787 : std::unique_ptr<CChainState> m_snapshot_chainstate;
788 :
789 : //! Points to either the ibd or snapshot chainstate; indicates our
790 : //! most-work chain.
791 : //!
792 : //! Once this pointer is set to a corresponding chainstate, it will not
793 : //! be reset until init.cpp:Shutdown(). This means it is safe to acquire
794 : //! the contents of this pointer with ::cs_main held, release the lock,
795 : //! and then use the reference without concern of it being deconstructed.
796 : //!
797 : //! This is especially important when, e.g., calling ActivateBestChain()
798 : //! on all chainstates because we are not able to hold ::cs_main going into
799 : //! that call.
800 643 : CChainState* m_active_chainstate{nullptr};
801 :
802 : //! If true, the assumed-valid chainstate has been fully validated
803 : //! by the background validation chainstate.
804 643 : bool m_snapshot_validated{false};
805 :
806 : // For access to m_active_chainstate.
807 : friend CChainState& ChainstateActive();
808 : friend CChain& ChainActive();
809 :
810 : public:
811 : //! A single BlockManager instance is shared across each constructed
812 : //! chainstate to avoid duplicating block metadata.
813 : BlockManager m_blockman GUARDED_BY(::cs_main);
814 :
815 : //! The total number of bytes available for us to use across all in-memory
816 : //! coins caches. This will be split somehow across chainstates.
817 643 : int64_t m_total_coinstip_cache{0};
818 : //
819 : //! The total number of bytes available for us to use across all leveldb
820 : //! coins databases. This will be split somehow across chainstates.
821 643 : int64_t m_total_coinsdb_cache{0};
822 :
823 : //! Instantiate a new chainstate and assign it based upon whether it is
824 : //! from a snapshot.
825 : //!
826 : //! @param[in] mempool The mempool to pass to the chainstate
827 : // constructor
828 : //! @param[in] snapshot_blockhash If given, signify that this chainstate
829 : //! is based on a snapshot.
830 : CChainState& InitializeChainstate(CTxMemPool& mempool, const uint256& snapshot_blockhash = uint256())
831 : EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
832 :
833 : //! Get all chainstates currently being used.
834 : std::vector<CChainState*> GetAll();
835 :
836 : //! The most-work chain.
837 : CChainState& ActiveChainstate() const;
838 5668 : CChain& ActiveChain() const { return ActiveChainstate().m_chain; }
839 2 : int ActiveHeight() const { return ActiveChain().Height(); }
840 2 : CBlockIndex* ActiveTip() const { return ActiveChain().Tip(); }
841 :
842 1085126 : BlockMap& BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
843 : {
844 1085126 : return m_blockman.m_block_index;
845 : }
846 :
847 : bool IsSnapshotActive() const;
848 :
849 : Optional<uint256> SnapshotBlockhash() const;
850 :
851 : //! Is there a snapshot in use and has it been fully validated?
852 3614 : bool IsSnapshotValidated() const { return m_snapshot_validated; }
853 :
854 : //! @returns true if this chainstate is being used to validate an active
855 : //! snapshot in the background.
856 : bool IsBackgroundIBD(CChainState* chainstate) const;
857 :
858 : //! Return the most-work chainstate that has been fully validated.
859 : //!
860 : //! During background validation of a snapshot, this is the IBD chain. After
861 : //! background validation has completed, this is the snapshot chain.
862 : CChainState& ValidatedChainstate() const;
863 :
864 2 : CChain& ValidatedChain() const { return ValidatedChainstate().m_chain; }
865 1 : CBlockIndex* ValidatedTip() const { return ValidatedChain().Tip(); }
866 :
867 : /**
868 : * Process an incoming block. This only returns after the best known valid
869 : * block is made active. Note that it does not, however, guarantee that the
870 : * specific block passed to it has been checked for validity!
871 : *
872 : * If you want to *possibly* get feedback on whether pblock is valid, you must
873 : * install a CValidationInterface (see validationinterface.h) - this will have
874 : * its BlockChecked method called whenever *any* block completes validation.
875 : *
876 : * Note that we guarantee that either the proof-of-work is valid on pblock, or
877 : * (and possibly also) BlockChecked will have been called.
878 : *
879 : * May not be called in a
880 : * validationinterface callback.
881 : *
882 : * @param[in] pblock The block we want to process.
883 : * @param[in] fForceProcessing Process this block even if unrequested; used for non-network block sources.
884 : * @param[out] fNewBlock A boolean which is set to indicate if the block was first received via this call
885 : * @returns If the block was processed, independently of block validity
886 : */
887 : bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool* fNewBlock) LOCKS_EXCLUDED(cs_main);
888 :
889 : /**
890 : * Process incoming block headers.
891 : *
892 : * May not be called in a
893 : * validationinterface callback.
894 : *
895 : * @param[in] block The block headers themselves
896 : * @param[out] state This may be set to an Error state if any error occurred processing them
897 : * @param[in] chainparams The params for the chain we want to connect to
898 : * @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
899 : */
900 : bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, BlockValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
901 :
902 : //! Mark one block file as pruned (modify associated database entries)
903 : void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
904 :
905 : //! Load the block tree and coins database from disk, initializing state if we're running with -reindex
906 : bool LoadBlockIndex(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
907 :
908 : //! Unload block index and chain data before shutdown.
909 : void Unload() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
910 :
911 : //! Clear (deconstruct) chainstate data.
912 : void Reset();
913 :
914 : //! Check to see if caches are out of balance and if so, call
915 : //! ResizeCoinsCaches() as needed.
916 : void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
917 : };
918 :
919 : /** DEPRECATED! Please use node.chainman instead. May only be used in validation.cpp internally */
920 : extern ChainstateManager g_chainman GUARDED_BY(::cs_main);
921 :
922 : /** Please prefer the identical ChainstateManager::ActiveChainstate */
923 : CChainState& ChainstateActive();
924 :
925 : /** Please prefer the identical ChainstateManager::ActiveChain */
926 : CChain& ChainActive();
927 :
928 : /** Global variable that points to the active block tree (protected by cs_main) */
929 : extern std::unique_ptr<CBlockTreeDB> pblocktree;
930 :
931 : /**
932 : * Return the spend height, which is one more than the inputs.GetBestBlock().
933 : * While checking, GetBestBlock() refers to the parent block. (protected by cs_main)
934 : * This is also true for mempool checks.
935 : */
936 : int GetSpendHeight(const CCoinsViewCache& inputs);
937 :
938 : extern VersionBitsCache versionbitscache;
939 :
940 : /**
941 : * Determine what nVersion a new block should use.
942 : */
943 : int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params);
944 :
945 : /** Get block file info entry for one block file */
946 : CBlockFileInfo* GetBlockFileInfo(size_t n);
947 :
948 : /** Dump the mempool to disk. */
949 : bool DumpMempool(const CTxMemPool& pool);
950 :
951 : /** Load the mempool from disk. */
952 : bool LoadMempool(CTxMemPool& pool);
953 :
954 : //! Check whether the block associated with this index entry is pruned or not.
955 4234 : inline bool IsBlockPruned(const CBlockIndex* pblockindex)
956 : {
957 4234 : return (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
958 : }
959 :
960 : #endif // BITCOIN_VALIDATION_H
|