Coverage Report

Created: 2024-08-21 05:08

/workdir/bitcoin/src/validation.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2022 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
#include <arith_uint256.h>
10
#include <attributes.h>
11
#include <chain.h>
12
#include <checkqueue.h>
13
#include <consensus/amount.h>
14
#include <cuckoocache.h>
15
#include <deploymentstatus.h>
16
#include <kernel/chain.h>
17
#include <kernel/chainparams.h>
18
#include <kernel/chainstatemanager_opts.h>
19
#include <kernel/cs_main.h> // IWYU pragma: export
20
#include <node/blockstorage.h>
21
#include <policy/feerate.h>
22
#include <policy/packages.h>
23
#include <policy/policy.h>
24
#include <script/script_error.h>
25
#include <script/sigcache.h>
26
#include <sync.h>
27
#include <txdb.h>
28
#include <txmempool.h> // For CTxMemPool::cs
29
#include <uint256.h>
30
#include <util/check.h>
31
#include <util/fs.h>
32
#include <util/hasher.h>
33
#include <util/result.h>
34
#include <util/translation.h>
35
#include <versionbits.h>
36
37
#include <atomic>
38
#include <map>
39
#include <memory>
40
#include <optional>
41
#include <set>
42
#include <stdint.h>
43
#include <string>
44
#include <thread>
45
#include <type_traits>
46
#include <utility>
47
#include <vector>
48
49
class Chainstate;
50
class CTxMemPool;
51
class ChainstateManager;
52
struct ChainTxData;
53
class DisconnectedBlockTransactions;
54
struct PrecomputedTransactionData;
55
struct LockPoints;
56
struct AssumeutxoData;
57
namespace node {
58
class SnapshotMetadata;
59
} // namespace node
60
namespace Consensus {
61
struct Params;
62
} // namespace Consensus
63
namespace util {
64
class SignalInterrupt;
65
} // namespace util
66
67
/** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned. */
68
static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
69
static const signed int DEFAULT_CHECKBLOCKS = 6;
70
static constexpr int DEFAULT_CHECKLEVEL{3};
71
// Require that user allocate at least 550 MiB for block & undo files (blk???.dat and rev???.dat)
72
// At 1MB per block, 288 blocks = 288MB.
73
// Add 15% for Undo data = 331MB
74
// Add 20% for Orphan block rate = 397MB
75
// We want the low water mark after pruning to be at least 397 MB and since we prune in
76
// full block file chunks, we need the high water mark which triggers the prune to be
77
// one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB
78
// Setting the target to >= 550 MiB will make it likely we can respect the target.
79
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
80
81
/** Current sync state passed to tip changed callbacks. */
82
enum class SynchronizationState {
83
    INIT_REINDEX,
84
    INIT_DOWNLOAD,
85
    POST_INIT
86
};
87
88
extern GlobalMutex g_best_block_mutex;
89
extern std::condition_variable g_best_block_cv;
90
/** Used to notify getblocktemplate RPC of new tips. */
91
extern uint256 g_best_block;
92
93
/** Documentation for argument 'checklevel'. */
94
extern const std::vector<std::string> CHECKLEVEL_DOC;
95
96
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
97
98
bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const bilingual_str& message);
99
100
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
101
double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex* pindex);
102
103
/** Prune block files up to a given height */
104
void PruneBlockFilesManual(Chainstate& active_chainstate, int nManualPruneHeight);
105
106
/**
107
* Validation result for a transaction evaluated by MemPoolAccept (single or package).
108
* Here are the expected fields and properties of a result depending on its ResultType, applicable to
109
* results returned from package evaluation:
110
*+---------------------------+----------------+-------------------+------------------+----------------+-------------------+
111
*| Field or property         |    VALID       |                 INVALID              |  MEMPOOL_ENTRY | DIFFERENT_WITNESS |
112
*|                           |                |--------------------------------------|                |                   |
113
*|                           |                | TX_RECONSIDERABLE |     Other        |                |                   |
114
*+---------------------------+----------------+-------------------+------------------+----------------+-------------------+
115
*| txid in mempool?          | yes            | no                | no*              | yes            | yes               |
116
*| wtxid in mempool?         | yes            | no                | no*              | yes            | no                |
117
*| m_state                   | yes, IsValid() | yes, IsInvalid()  | yes, IsInvalid() | yes, IsValid() | yes, IsValid()    |
118
*| m_vsize                   | yes            | no                | no               | yes            | no                |
119
*| m_base_fees               | yes            | no                | no               | yes            | no                |
120
*| m_effective_feerate       | yes            | yes               | no               | no             | no                |
121
*| m_wtxids_fee_calculations | yes            | yes               | no               | no             | no                |
122
*| m_other_wtxid             | no             | no                | no               | no             | yes               |
123
*+---------------------------+----------------+-------------------+------------------+----------------+-------------------+
124
* (*) Individual transaction acceptance doesn't return MEMPOOL_ENTRY and DIFFERENT_WITNESS. It returns
125
* INVALID, with the errors txn-already-in-mempool and txn-same-nonwitness-data-in-mempool
126
* respectively. In those cases, the txid or wtxid may be in the mempool for a TX_CONFLICT.
127
*/
128
struct MempoolAcceptResult {
129
    /** Used to indicate the results of mempool validation. */
130
    enum class ResultType {
131
        VALID, //!> Fully validated, valid.
132
        INVALID, //!> Invalid.
133
        MEMPOOL_ENTRY, //!> Valid, transaction was already in the mempool.
134
        DIFFERENT_WITNESS, //!> Not validated. A same-txid-different-witness tx (see m_other_wtxid) already exists in the mempool and was not replaced.
135
    };
136
    /** Result type. Present in all MempoolAcceptResults. */
137
    const ResultType m_result_type;
138
139
    /** Contains information about why the transaction failed. */
140
    const TxValidationState m_state;
141
142
    /** Mempool transactions replaced by the tx. */
143
    const std::list<CTransactionRef> m_replaced_transactions;
144
    /** Virtual size as used by the mempool, calculated using serialized size and sigops. */
145
    const std::optional<int64_t> m_vsize;
146
    /** Raw base fees in satoshis. */
147
    const std::optional<CAmount> m_base_fees;
148
    /** The feerate at which this transaction was considered. This includes any fee delta added
149
     * using prioritisetransaction (i.e. modified fees). If this transaction was submitted as a
150
     * package, this is the package feerate, which may also include its descendants and/or
151
     * ancestors (see m_wtxids_fee_calculations below).
152
     */
153
    const std::optional<CFeeRate> m_effective_feerate;
154
    /** Contains the wtxids of the transactions used for fee-related checks. Includes this
155
     * transaction's wtxid and may include others if this transaction was validated as part of a
156
     * package. This is not necessarily equivalent to the list of transactions passed to
157
     * ProcessNewPackage().
158
     * Only present when m_result_type = ResultType::VALID. */
159
    const std::optional<std::vector<Wtxid>> m_wtxids_fee_calculations;
160
161
    /** The wtxid of the transaction in the mempool which has the same txid but different witness. */
162
    const std::optional<uint256> m_other_wtxid;
163
164
0
    static MempoolAcceptResult Failure(TxValidationState state) {
165
0
        return MempoolAcceptResult(state);
166
0
    }
167
168
    static MempoolAcceptResult FeeFailure(TxValidationState state,
169
                                          CFeeRate effective_feerate,
170
0
                                          const std::vector<Wtxid>& wtxids_fee_calculations) {
171
0
        return MempoolAcceptResult(state, effective_feerate, wtxids_fee_calculations);
172
0
    }
173
174
    static MempoolAcceptResult Success(std::list<CTransactionRef>&& replaced_txns,
175
                                       int64_t vsize,
176
                                       CAmount fees,
177
                                       CFeeRate effective_feerate,
178
0
                                       const std::vector<Wtxid>& wtxids_fee_calculations) {
179
0
        return MempoolAcceptResult(std::move(replaced_txns), vsize, fees,
180
0
                                   effective_feerate, wtxids_fee_calculations);
181
0
    }
182
183
0
    static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees) {
184
0
        return MempoolAcceptResult(vsize, fees);
185
0
    }
186
187
0
    static MempoolAcceptResult MempoolTxDifferentWitness(const uint256& other_wtxid) {
188
0
        return MempoolAcceptResult(other_wtxid);
189
0
    }
190
191
// Private constructors. Use static methods MempoolAcceptResult::Success, etc. to construct.
192
private:
193
    /** Constructor for failure case */
194
    explicit MempoolAcceptResult(TxValidationState state)
195
0
        : m_result_type(ResultType::INVALID), m_state(state) {
196
0
            Assume(!state.IsValid()); // Can be invalid or error
197
0
        }
198
199
    /** Constructor for success case */
200
    explicit MempoolAcceptResult(std::list<CTransactionRef>&& replaced_txns,
201
                                 int64_t vsize,
202
                                 CAmount fees,
203
                                 CFeeRate effective_feerate,
204
                                 const std::vector<Wtxid>& wtxids_fee_calculations)
205
0
        : m_result_type(ResultType::VALID),
206
0
        m_replaced_transactions(std::move(replaced_txns)),
207
0
        m_vsize{vsize},
208
0
        m_base_fees(fees),
209
0
        m_effective_feerate(effective_feerate),
210
0
        m_wtxids_fee_calculations(wtxids_fee_calculations) {}
211
212
    /** Constructor for fee-related failure case */
213
    explicit MempoolAcceptResult(TxValidationState state,
214
                                 CFeeRate effective_feerate,
215
                                 const std::vector<Wtxid>& wtxids_fee_calculations)
216
0
        : m_result_type(ResultType::INVALID),
217
0
        m_state(state),
218
0
        m_effective_feerate(effective_feerate),
219
0
        m_wtxids_fee_calculations(wtxids_fee_calculations) {}
220
221
    /** Constructor for already-in-mempool case. It wouldn't replace any transactions. */
222
    explicit MempoolAcceptResult(int64_t vsize, CAmount fees)
223
0
        : m_result_type(ResultType::MEMPOOL_ENTRY), m_vsize{vsize}, m_base_fees(fees) {}
224
225
    /** Constructor for witness-swapped case. */
226
    explicit MempoolAcceptResult(const uint256& other_wtxid)
227
0
        : m_result_type(ResultType::DIFFERENT_WITNESS), m_other_wtxid(other_wtxid) {}
228
};
229
230
/**
231
* Validation result for package mempool acceptance.
232
*/
233
struct PackageMempoolAcceptResult
234
{
235
    PackageValidationState m_state;
236
    /**
237
    * Map from wtxid to finished MempoolAcceptResults. The client is responsible
238
    * for keeping track of the transaction objects themselves. If a result is not
239
    * present, it means validation was unfinished for that transaction. If there
240
    * was a package-wide error (see result in m_state), m_tx_results will be empty.
241
    */
242
    std::map<uint256, MempoolAcceptResult> m_tx_results;
243
244
    explicit PackageMempoolAcceptResult(PackageValidationState state,
245
                                        std::map<uint256, MempoolAcceptResult>&& results)
246
0
        : m_state{state}, m_tx_results(std::move(results)) {}
247
248
    explicit PackageMempoolAcceptResult(PackageValidationState state, CFeeRate feerate,
249
                                        std::map<uint256, MempoolAcceptResult>&& results)
250
0
        : m_state{state}, m_tx_results(std::move(results)) {}
251
252
    /** Constructor to create a PackageMempoolAcceptResult from a single MempoolAcceptResult */
253
    explicit PackageMempoolAcceptResult(const uint256& wtxid, const MempoolAcceptResult& result)
254
0
        : m_tx_results{ {wtxid, result} } {}
255
};
256
257
/**
258
 * Try to add a transaction to the mempool. This is an internal function and is exposed only for testing.
259
 * Client code should use ChainstateManager::ProcessTransaction()
260
 *
261
 * @param[in]  active_chainstate  Reference to the active chainstate.
262
 * @param[in]  tx                 The transaction to submit for mempool acceptance.
263
 * @param[in]  accept_time        The timestamp for adding the transaction to the mempool.
264
 *                                It is also used to determine when the entry expires.
265
 * @param[in]  bypass_limits      When true, don't enforce mempool fee and capacity limits,
266
 *                                and set entry_sequence to zero.
267
 * @param[in]  test_accept        When true, run validation checks but don't submit to mempool.
268
 *
269
 * @returns a MempoolAcceptResult indicating whether the transaction was accepted/rejected with reason.
270
 */
271
MempoolAcceptResult AcceptToMemoryPool(Chainstate& active_chainstate, const CTransactionRef& tx,
272
                                       int64_t accept_time, bool bypass_limits, bool test_accept)
273
    EXCLUSIVE_LOCKS_REQUIRED(cs_main);
274
275
/**
276
* Validate (and maybe submit) a package to the mempool. See doc/policy/packages.md for full details
277
* on package validation rules.
278
* @param[in]    test_accept         When true, run validation checks but don't submit to mempool.
279
* @param[in]    client_maxfeerate    If exceeded by an individual transaction, rest of (sub)package evaluation is aborted.
280
*                                   Only for sanity checks against local submission of transactions.
281
* @returns a PackageMempoolAcceptResult which includes a MempoolAcceptResult for each transaction.
282
* If a transaction fails, validation will exit early and some results may be missing. It is also
283
* possible for the package to be partially submitted.
284
*/
285
PackageMempoolAcceptResult ProcessNewPackage(Chainstate& active_chainstate, CTxMemPool& pool,
286
                                                   const Package& txns, bool test_accept, const std::optional<CFeeRate>& client_maxfeerate)
287
                                                   EXCLUSIVE_LOCKS_REQUIRED(cs_main);
288
289
/* Mempool validation helper functions */
290
291
/**
292
 * Check if transaction will be final in the next block to be created.
293
 */
294
bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
295
296
/**
297
 * Calculate LockPoints required to check if transaction will be BIP68 final in the next block
298
 * to be created on top of tip.
299
 *
300
 * @param[in]   tip             Chain tip for which tx sequence locks are calculated. For
301
 *                              example, the tip of the current active chain.
302
 * @param[in]   coins_view      Any CCoinsView that provides access to the relevant coins for
303
 *                              checking sequence locks. For example, it can be a CCoinsViewCache
304
 *                              that isn't connected to anything but contains all the relevant
305
 *                              coins, or a CCoinsViewMemPool that is connected to the
306
 *                              mempool and chainstate UTXO set. In the latter case, the caller
307
 *                              is responsible for holding the appropriate locks to ensure that
308
 *                              calls to GetCoin() return correct coins.
309
 * @param[in]   tx              The transaction being evaluated.
310
 *
311
 * @returns The resulting height and time calculated and the hash of the block needed for
312
 *          calculation, or std::nullopt if there is an error.
313
 */
314
std::optional<LockPoints> CalculateLockPointsAtTip(
315
    CBlockIndex* tip,
316
    const CCoinsView& coins_view,
317
    const CTransaction& tx);
318
319
/**
320
 * Check if transaction will be BIP68 final in the next block to be created on top of tip.
321
 * @param[in]   tip             Chain tip to check tx sequence locks against. For example,
322
 *                              the tip of the current active chain.
323
 * @param[in]   lock_points     LockPoints containing the height and time at which this
324
 *                              transaction is final.
325
 * Simulates calling SequenceLocks() with data from the tip passed in.
326
 * The LockPoints should not be considered valid if CheckSequenceLocksAtTip returns false.
327
 */
328
bool CheckSequenceLocksAtTip(CBlockIndex* tip,
329
                             const LockPoints& lock_points);
330
331
/**
332
 * Closure representing one script verification
333
 * Note that this stores references to the spending transaction
334
 */
335
class CScriptCheck
336
{
337
private:
338
    CTxOut m_tx_out;
339
    const CTransaction *ptxTo;
340
    unsigned int nIn;
341
    unsigned int nFlags;
342
    bool cacheStore;
343
    ScriptError error{SCRIPT_ERR_UNKNOWN_ERROR};
344
    PrecomputedTransactionData *txdata;
345
    SignatureCache* m_signature_cache;
346
347
public:
348
    CScriptCheck(const CTxOut& outIn, const CTransaction& txToIn, SignatureCache& signature_cache, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData* txdataIn) :
349
0
        m_tx_out(outIn), ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), txdata(txdataIn), m_signature_cache(&signature_cache) { }
350
351
    CScriptCheck(const CScriptCheck&) = delete;
352
    CScriptCheck& operator=(const CScriptCheck&) = delete;
353
0
    CScriptCheck(CScriptCheck&&) = default;
354
0
    CScriptCheck& operator=(CScriptCheck&&) = default;
355
356
    bool operator()();
357
358
0
    ScriptError GetScriptError() const { return error; }
359
};
360
361
// CScriptCheck is used a lot in std::vector, make sure that's efficient
362
static_assert(std::is_nothrow_move_assignable_v<CScriptCheck>);
363
static_assert(std::is_nothrow_move_constructible_v<CScriptCheck>);
364
static_assert(std::is_nothrow_destructible_v<CScriptCheck>);
365
366
/**
367
 * Convenience class for initializing and passing the script execution cache
368
 * and signature cache.
369
 */
370
class ValidationCache
371
{
372
private:
373
    //! Pre-initialized hasher to avoid having to recreate it for every hash calculation.
374
    CSHA256 m_script_execution_cache_hasher;
375
376
public:
377
    CuckooCache::cache<uint256, SignatureCacheHasher> m_script_execution_cache;
378
    SignatureCache m_signature_cache;
379
380
    ValidationCache(size_t script_execution_cache_bytes, size_t signature_cache_bytes);
381
382
    ValidationCache(const ValidationCache&) = delete;
383
    ValidationCache& operator=(const ValidationCache&) = delete;
384
385
    //! Return a copy of the pre-initialized hasher.
386
0
    CSHA256 ScriptExecutionCacheHasher() const { return m_script_execution_cache_hasher; }
387
};
388
389
/** Functions for validating blocks and updating the block tree */
390
391
/** Context-independent validity checks */
392
bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
393
394
/** Check a block is completely valid from start to finish (only works on top of our current best block) */
395
bool TestBlockValidity(BlockValidationState& state,
396
                       const CChainParams& chainparams,
397
                       Chainstate& chainstate,
398
                       const CBlock& block,
399
                       CBlockIndex* pindexPrev,
400
                       bool fCheckPOW = true,
401
                       bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
402
403
/** Check with the proof of work on each blockheader matches the value in nBits */
404
bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams);
405
406
/** Check if a block has been mutated (with respect to its merkle root and witness commitments). */
407
bool IsBlockMutated(const CBlock& block, bool check_witness_root);
408
409
/** Return the sum of the claimed work on a given set of headers. No verification of PoW is done. */
410
arith_uint256 CalculateClaimedHeadersWork(const std::vector<CBlockHeader>& headers);
411
412
enum class VerifyDBResult {
413
    SUCCESS,
414
    CORRUPTED_BLOCK_DB,
415
    INTERRUPTED,
416
    SKIPPED_L3_CHECKS,
417
    SKIPPED_MISSING_BLOCKS,
418
};
419
420
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
421
class CVerifyDB
422
{
423
private:
424
    kernel::Notifications& m_notifications;
425
426
public:
427
    explicit CVerifyDB(kernel::Notifications& notifications);
428
    ~CVerifyDB();
429
    [[nodiscard]] VerifyDBResult VerifyDB(
430
        Chainstate& chainstate,
431
        const Consensus::Params& consensus_params,
432
        CCoinsView& coinsview,
433
        int nCheckLevel,
434
        int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
435
};
436
437
enum DisconnectResult
438
{
439
    DISCONNECT_OK,      // All good.
440
    DISCONNECT_UNCLEAN, // Rolled back, but UTXO set was inconsistent with block.
441
    DISCONNECT_FAILED   // Something else went wrong.
442
};
443
444
class ConnectTrace;
445
446
/** @see Chainstate::FlushStateToDisk */
447
enum class FlushStateMode {
448
    NONE,
449
    IF_NEEDED,
450
    PERIODIC,
451
    ALWAYS
452
};
453
454
/**
455
 * A convenience class for constructing the CCoinsView* hierarchy used
456
 * to facilitate access to the UTXO set.
457
 *
458
 * This class consists of an arrangement of layered CCoinsView objects,
459
 * preferring to store and retrieve coins in memory via `m_cacheview` but
460
 * ultimately falling back on cache misses to the canonical store of UTXOs on
461
 * disk, `m_dbview`.
462
 */
463
class CoinsViews {
464
465
public:
466
    //! The lowest level of the CoinsViews cache hierarchy sits in a leveldb database on disk.
467
    //! All unspent coins reside in this store.
468
    CCoinsViewDB m_dbview GUARDED_BY(cs_main);
469
470
    //! This view wraps access to the leveldb instance and handles read errors gracefully.
471
    CCoinsViewErrorCatcher m_catcherview GUARDED_BY(cs_main);
472
473
    //! This is the top layer of the cache hierarchy - it keeps as many coins in memory as
474
    //! can fit per the dbcache setting.
475
    std::unique_ptr<CCoinsViewCache> m_cacheview GUARDED_BY(cs_main);
476
477
    //! This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it
478
    //! *does not* create a CCoinsViewCache instance by default. This is done separately because the
479
    //! presence of the cache has implications on whether or not we're allowed to flush the cache's
480
    //! state to disk, which should not be done until the health of the database is verified.
481
    //!
482
    //! All arguments forwarded onto CCoinsViewDB.
483
    CoinsViews(DBParams db_params, CoinsViewOptions options);
484
485
    //! Initialize the CCoinsViewCache member.
486
    void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
487
};
488
489
enum class CoinsCacheSizeState
490
{
491
    //! The coins cache is in immediate need of a flush.
492
    CRITICAL = 2,
493
    //! The cache is at >= 90% capacity.
494
    LARGE = 1,
495
    OK = 0
496
};
497
498
/**
499
 * Chainstate stores and provides an API to update our local knowledge of the
500
 * current best chain.
501
 *
502
 * Eventually, the API here is targeted at being exposed externally as a
503
 * consumable library, so any functions added must only call
504
 * other class member functions, pure functions in other parts of the consensus
505
 * library, callbacks via the validation interface, or read/write-to-disk
506
 * functions (eventually this will also be via callbacks).
507
 *
508
 * Anything that is contingent on the current tip of the chain is stored here,
509
 * whereas block information and metadata independent of the current tip is
510
 * kept in `BlockManager`.
511
 */
512
class Chainstate
513
{
514
protected:
515
    /**
516
     * The ChainState Mutex
517
     * A lock that must be held when modifying this ChainState - held in ActivateBestChain() and
518
     * InvalidateBlock()
519
     */
520
    Mutex m_chainstate_mutex;
521
522
    //! Optional mempool that is kept in sync with the chain.
523
    //! Only the active chainstate has a mempool.
524
    CTxMemPool* m_mempool;
525
526
    //! Manages the UTXO set, which is a reflection of the contents of `m_chain`.
527
    std::unique_ptr<CoinsViews> m_coins_views;
528
529
    //! This toggle exists for use when doing background validation for UTXO
530
    //! snapshots.
531
    //!
532
    //! In the expected case, it is set once the background validation chain reaches the
533
    //! same height as the base of the snapshot and its UTXO set is found to hash to
534
    //! the expected assumeutxo value. It signals that we should no longer connect
535
    //! blocks to the background chainstate. When set on the background validation
536
    //! chainstate, it signifies that we have fully validated the snapshot chainstate.
537
    //!
538
    //! In the unlikely case that the snapshot chainstate is found to be invalid, this
539
    //! is set to true on the snapshot chainstate.
540
    bool m_disabled GUARDED_BY(::cs_main) {false};
541
542
    //! Cached result of LookupBlockIndex(*m_from_snapshot_blockhash)
543
    const CBlockIndex* m_cached_snapshot_base GUARDED_BY(::cs_main) {nullptr};
544
545
public:
546
    //! Reference to a BlockManager instance which itself is shared across all
547
    //! Chainstate instances.
548
    node::BlockManager& m_blockman;
549
550
    //! The chainstate manager that owns this chainstate. The reference is
551
    //! necessary so that this instance can check whether it is the active
552
    //! chainstate within deeply nested method calls.
553
    ChainstateManager& m_chainman;
554
555
    explicit Chainstate(
556
        CTxMemPool* mempool,
557
        node::BlockManager& blockman,
558
        ChainstateManager& chainman,
559
        std::optional<uint256> from_snapshot_blockhash = std::nullopt);
560
561
    //! Return the current role of the chainstate. See `ChainstateManager`
562
    //! documentation for a description of the different types of chainstates.
563
    //!
564
    //! @sa ChainstateRole
565
    ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
566
567
    /**
568
     * Initialize the CoinsViews UTXO set database management data structures. The in-memory
569
     * cache is initialized separately.
570
     *
571
     * All parameters forwarded to CoinsViews.
572
     */
573
    void InitCoinsDB(
574
        size_t cache_size_bytes,
575
        bool in_memory,
576
        bool should_wipe,
577
        fs::path leveldb_name = "chainstate");
578
579
    //! Initialize the in-memory coins cache (to be done after the health of the on-disk database
580
    //! is verified).
581
    void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
582
583
    //! @returns whether or not the CoinsViews object has been fully initialized and we can
584
    //!          safely flush this object to disk.
585
    bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
586
0
    {
587
0
        AssertLockHeld(::cs_main);
588
0
        return m_coins_views && m_coins_views->m_cacheview;
  Branch (588:16): [True: 0, False: 0]
  Branch (588:33): [True: 0, False: 0]
589
0
    }
590
591
    //! The current chain of blockheaders we consult and build on.
592
    //! @see CChain, CBlockIndex.
593
    CChain m_chain;
594
595
    /**
596
     * The blockhash which is the base of the snapshot this chainstate was created from.
597
     *
598
     * std::nullopt if this chainstate was not created from a snapshot.
599
     */
600
    const std::optional<uint256> m_from_snapshot_blockhash;
601
602
    /**
603
     * The base of the snapshot this chainstate was created from.
604
     *
605
     * nullptr if this chainstate was not created from a snapshot.
606
     */
607
    const CBlockIndex* SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
608
609
    /**
610
     * The set of all CBlockIndex entries that have as much work as our current
611
     * tip or more, and transaction data needed to be validated (with
612
     * BLOCK_VALID_TRANSACTIONS for each block and its parents back to the
613
     * genesis block or an assumeutxo snapshot block). Entries may be failed,
614
     * though, and pruning nodes may be missing the data for the block.
615
     */
616
    std::set<CBlockIndex*, node::CBlockIndexWorkComparator> setBlockIndexCandidates;
617
618
    //! @returns A reference to the in-memory cache of the UTXO set.
619
    CCoinsViewCache& CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
620
0
    {
621
0
        AssertLockHeld(::cs_main);
622
0
        Assert(m_coins_views);
623
0
        return *Assert(m_coins_views->m_cacheview);
624
0
    }
625
626
    //! @returns A reference to the on-disk UTXO set database.
627
    CCoinsViewDB& CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
628
0
    {
629
0
        AssertLockHeld(::cs_main);
630
0
        return Assert(m_coins_views)->m_dbview;
631
0
    }
632
633
    //! @returns A pointer to the mempool.
634
    CTxMemPool* GetMempool()
635
0
    {
636
0
        return m_mempool;
637
0
    }
638
639
    //! @returns A reference to a wrapped view of the in-memory UTXO set that
640
    //!     handles disk read errors gracefully.
641
    CCoinsViewErrorCatcher& CoinsErrorCatcher() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
642
0
    {
643
0
        AssertLockHeld(::cs_main);
644
0
        return Assert(m_coins_views)->m_catcherview;
645
0
    }
646
647
    //! Destructs all objects related to accessing the UTXO set.
648
0
    void ResetCoinsViews() { m_coins_views.reset(); }
649
650
    //! Does this chainstate have a UTXO set attached?
651
0
    bool HasCoinsViews() const { return (bool)m_coins_views; }
652
653
    //! The cache size of the on-disk coins view.
654
    size_t m_coinsdb_cache_size_bytes{0};
655
656
    //! The cache size of the in-memory coins view.
657
    size_t m_coinstip_cache_size_bytes{0};
658
659
    //! Resize the CoinsViews caches dynamically and flush state to disk.
660
    //! @returns true unless an error occurred during the flush.
661
    bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
662
        EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
663
664
    /**
665
     * Update the on-disk chain state.
666
     * The caches and indexes are flushed depending on the mode we're called with
667
     * if they're too large, if it's been a while since the last write,
668
     * or always and in all cases if we're in prune mode and are deleting files.
669
     *
670
     * If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything
671
     * besides checking if we need to prune.
672
     *
673
     * @returns true unless a system error occurred
674
     */
675
    bool FlushStateToDisk(
676
        BlockValidationState& state,
677
        FlushStateMode mode,
678
        int nManualPruneHeight = 0);
679
680
    //! Unconditionally flush all changes to disk.
681
    void ForceFlushStateToDisk();
682
683
    //! Prune blockfiles from the disk if necessary and then flush chainstate changes
684
    //! if we pruned.
685
    void PruneAndFlush();
686
687
    /**
688
     * Find the best known block, and make it the tip of the block chain. The
689
     * result is either failure or an activated best chain. pblock is either
690
     * nullptr or a pointer to a block that is already loaded (to avoid loading
691
     * it again from disk).
692
     *
693
     * ActivateBestChain is split into steps (see ActivateBestChainStep) so that
694
     * we avoid holding cs_main for an extended period of time; the length of this
695
     * call may be quite long during reindexing or a substantial reorg.
696
     *
697
     * May not be called with cs_main held. May not be called in a
698
     * validationinterface callback.
699
     *
700
     * Note that if this is called while a snapshot chainstate is active, and if
701
     * it is called on a background chainstate whose tip has reached the base block
702
     * of the snapshot, its execution will take *MINUTES* while it hashes the
703
     * background UTXO set to verify the assumeutxo value the snapshot was activated
704
     * with. `cs_main` will be held during this time.
705
     *
706
     * @returns true unless a system error occurred
707
     */
708
    bool ActivateBestChain(
709
        BlockValidationState& state,
710
        std::shared_ptr<const CBlock> pblock = nullptr)
711
        EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
712
        LOCKS_EXCLUDED(::cs_main);
713
714
    // Block (dis)connection on a given view:
715
    DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
716
        EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
717
    bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
718
                      CCoinsViewCache& view, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
719
720
    // Apply the effects of a block disconnection on the UTXO set.
721
    bool DisconnectTip(BlockValidationState& state, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
722
723
    // Manual block validity manipulation:
724
    /** Mark a block as precious and reorganize.
725
     *
726
     * May not be called in a validationinterface callback.
727
     */
728
    bool PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
729
        EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
730
        LOCKS_EXCLUDED(::cs_main);
731
732
    /** Mark a block as invalid. */
733
    bool InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex)
734
        EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
735
        LOCKS_EXCLUDED(::cs_main);
736
737
    /** Remove invalidity status from a block and its descendants. */
738
    void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
739
740
    /** Replay blocks that aren't fully applied to the database. */
741
    bool ReplayBlocks();
742
743
    /** Whether the chain state needs to be redownloaded due to lack of witness data */
744
    [[nodiscard]] bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
745
    /** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
746
    bool LoadGenesisBlock();
747
748
    void TryAddBlockIndexCandidate(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
749
750
    void PruneBlockIndexCandidates();
751
752
    void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
753
754
    /** Find the last common block of this chain and a locator. */
755
    const CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
756
757
    /** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
758
    bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
759
760
    //! Dictates whether we need to flush the cache to disk or not.
761
    //!
762
    //! @return the state of the size of the coins cache.
763
    CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
764
765
    CoinsCacheSizeState GetCoinsCacheSizeState(
766
        size_t max_coins_cache_size_bytes,
767
        size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
768
769
    std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
770
771
    //! Indirection necessary to make lock annotations work with an optional mempool.
772
    RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs)
773
0
    {
774
0
        return m_mempool ? &m_mempool->cs : nullptr;
  Branch (774:16): [True: 0, False: 0]
775
0
    }
776
777
private:
778
    bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
779
    bool ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
780
781
    void InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
782
    CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
783
784
    bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
785
786
    void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
787
    void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
788
789
    /**
790
     * Make mempool consistent after a reorg, by re-adding or recursively erasing
791
     * disconnected block transactions from the mempool, and also removing any
792
     * other transactions from the mempool that are no longer valid given the new
793
     * tip/height.
794
     *
795
     * Note: we assume that disconnectpool only contains transactions that are NOT
796
     * confirmed in the current chain nor already in the mempool (otherwise,
797
     * in-mempool descendants of such transactions would be removed).
798
     *
799
     * Passing fAddToMempool=false will skip trying to add the transactions back,
800
     * and instead just erase from the mempool as needed.
801
     */
802
    void MaybeUpdateMempoolForReorg(
803
        DisconnectedBlockTransactions& disconnectpool,
804
        bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
805
806
    /** Check warning conditions and do some notifications on new chain tip set. */
807
    void UpdateTip(const CBlockIndex* pindexNew)
808
        EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
809
810
    SteadyClock::time_point m_last_write{};
811
    SteadyClock::time_point m_last_flush{};
812
813
    /**
814
     * In case of an invalid snapshot, rename the coins leveldb directory so
815
     * that it can be examined for issue diagnosis.
816
     */
817
    [[nodiscard]] util::Result<void> InvalidateCoinsDBOnDisk() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
818
819
    friend ChainstateManager;
820
};
821
822
enum class SnapshotCompletionResult {
823
    SUCCESS,
824
    SKIPPED,
825
826
    // Expected assumeutxo configuration data is not found for the height of the
827
    // base block.
828
    MISSING_CHAINPARAMS,
829
830
    // Failed to generate UTXO statistics (to check UTXO set hash) for the background
831
    // chainstate.
832
    STATS_FAILED,
833
834
    // The UTXO set hash of the background validation chainstate does not match
835
    // the one expected by assumeutxo chainparams.
836
    HASH_MISMATCH,
837
838
    // The blockhash of the current tip of the background validation chainstate does
839
    // not match the one expected by the snapshot chainstate.
840
    BASE_BLOCKHASH_MISMATCH,
841
};
842
843
/**
844
 * Provides an interface for creating and interacting with one or two
845
 * chainstates: an IBD chainstate generated by downloading blocks, and
846
 * an optional snapshot chainstate loaded from a UTXO snapshot. Managed
847
 * chainstates can be maintained at different heights simultaneously.
848
 *
849
 * This class provides abstractions that allow the retrieval of the current
850
 * most-work chainstate ("Active") as well as chainstates which may be in
851
 * background use to validate UTXO snapshots.
852
 *
853
 * Definitions:
854
 *
855
 * *IBD chainstate*: a chainstate whose current state has been "fully"
856
 *   validated by the initial block download process.
857
 *
858
 * *Snapshot chainstate*: a chainstate populated by loading in an
859
 *    assumeutxo UTXO snapshot.
860
 *
861
 * *Active chainstate*: the chainstate containing the current most-work
862
 *    chain. Consulted by most parts of the system (net_processing,
863
 *    wallet) as a reflection of the current chain and UTXO set.
864
 *    This may either be an IBD chainstate or a snapshot chainstate.
865
 *
866
 * *Background IBD chainstate*: an IBD chainstate for which the
867
 *    IBD process is happening in the background while use of the
868
 *    active (snapshot) chainstate allows the rest of the system to function.
869
 */
870
class ChainstateManager
871
{
872
private:
873
    //! The chainstate used under normal operation (i.e. "regular" IBD) or, if
874
    //! a snapshot is in use, for background validation.
875
    //!
876
    //! Its contents (including on-disk data) will be deleted *upon shutdown*
877
    //! after background validation of the snapshot has completed. We do not
878
    //! free the chainstate contents immediately after it finishes validation
879
    //! to cautiously avoid a case where some other part of the system is still
880
    //! using this pointer (e.g. net_processing).
881
    //!
882
    //! Once this pointer is set to a corresponding chainstate, it will not
883
    //! be reset until init.cpp:Shutdown().
884
    //!
885
    //! It is important for the pointer to not be deleted until shutdown,
886
    //! because cs_main is not always held when the pointer is accessed, for
887
    //! example when calling ActivateBestChain, so there's no way you could
888
    //! prevent code from using the pointer while deleting it.
889
    std::unique_ptr<Chainstate> m_ibd_chainstate GUARDED_BY(::cs_main);
890
891
    //! A chainstate initialized on the basis of a UTXO snapshot. If this is
892
    //! non-null, it is always our active chainstate.
893
    //!
894
    //! Once this pointer is set to a corresponding chainstate, it will not
895
    //! be reset until init.cpp:Shutdown().
896
    //!
897
    //! It is important for the pointer to not be deleted until shutdown,
898
    //! because cs_main is not always held when the pointer is accessed, for
899
    //! example when calling ActivateBestChain, so there's no way you could
900
    //! prevent code from using the pointer while deleting it.
901
    std::unique_ptr<Chainstate> m_snapshot_chainstate GUARDED_BY(::cs_main);
902
903
    //! Points to either the ibd or snapshot chainstate; indicates our
904
    //! most-work chain.
905
    Chainstate* m_active_chainstate GUARDED_BY(::cs_main) {nullptr};
906
907
    CBlockIndex* m_best_invalid GUARDED_BY(::cs_main){nullptr};
908
909
    /** The last header for which a headerTip notification was issued. */
910
    CBlockIndex* m_last_notified_header GUARDED_BY(GetMutex()){nullptr};
911
912
    bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex());
913
914
    //! Internal helper for ActivateSnapshot().
915
    //!
916
    //! De-serialization of a snapshot that is created with
917
    //! CreateUTXOSnapshot() in rpc/blockchain.cpp.
918
    //! To reduce space the serialization format of the snapshot avoids
919
    //! duplication of tx hashes. The code takes advantage of the guarantee by
920
    //! leveldb that keys are lexicographically sorted.
921
    [[nodiscard]] util::Result<void> PopulateAndValidateSnapshot(
922
        Chainstate& snapshot_chainstate,
923
        AutoFile& coins_file,
924
        const node::SnapshotMetadata& metadata);
925
926
    /**
927
     * If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
928
     * that it doesn't descend from an invalid block, and then add it to m_block_index.
929
     * Caller must set min_pow_checked=true in order to add a new header to the
930
     * block index (permanent memory storage), indicating that the header is
931
     * known to be part of a sufficiently high-work chain (anti-dos check).
932
     */
933
    bool AcceptBlockHeader(
934
        const CBlockHeader& block,
935
        BlockValidationState& state,
936
        CBlockIndex** ppindex,
937
        bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
938
    friend Chainstate;
939
940
    /** Most recent headers presync progress update, for rate-limiting. */
941
    std::chrono::time_point<std::chrono::steady_clock> m_last_presync_update GUARDED_BY(::cs_main) {};
942
943
    std::array<ThresholdConditionCache, VERSIONBITS_NUM_BITS> m_warningcache GUARDED_BY(::cs_main);
944
945
    //! Return true if a chainstate is considered usable.
946
    //!
947
    //! This is false when a background validation chainstate has completed its
948
    //! validation of an assumed-valid chainstate, or when a snapshot
949
    //! chainstate has been found to be invalid.
950
40.0k
    bool IsUsable(const Chainstate* const cs) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
951
40.0k
        return cs && !cs->m_disabled;
  Branch (951:16): [True: 13.6k, False: 26.3k]
  Branch (951:22): [True: 13.6k, False: 0]
952
40.0k
    }
953
954
    //! A queue for script verifications that have to be performed by worker threads.
955
    CCheckQueue<CScriptCheck> m_script_check_queue;
956
957
    //! Timers and counters used for benchmarking validation in both background
958
    //! and active chainstates.
959
    SteadyClock::duration GUARDED_BY(::cs_main) time_check{};
960
    SteadyClock::duration GUARDED_BY(::cs_main) time_forks{};
961
    SteadyClock::duration GUARDED_BY(::cs_main) time_connect{};
962
    SteadyClock::duration GUARDED_BY(::cs_main) time_verify{};
963
    SteadyClock::duration GUARDED_BY(::cs_main) time_undo{};
964
    SteadyClock::duration GUARDED_BY(::cs_main) time_index{};
965
    SteadyClock::duration GUARDED_BY(::cs_main) time_total{};
966
    int64_t GUARDED_BY(::cs_main) num_blocks_total{0};
967
    SteadyClock::duration GUARDED_BY(::cs_main) time_connect_total{};
968
    SteadyClock::duration GUARDED_BY(::cs_main) time_flush{};
969
    SteadyClock::duration GUARDED_BY(::cs_main) time_chainstate{};
970
    SteadyClock::duration GUARDED_BY(::cs_main) time_post_connect{};
971
972
public:
973
    using Options = kernel::ChainstateManagerOpts;
974
975
    explicit ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options);
976
977
    //! Function to restart active indexes; set dynamically to avoid a circular
978
    //! dependency on `base/index.cpp`.
979
    std::function<void()> restart_indexes = std::function<void()>();
980
981
6.56k
    const CChainParams& GetParams() const { return m_options.chainparams; }
982
42.8k
    const Consensus::Params& GetConsensus() const { return m_options.chainparams.GetConsensus(); }
983
    bool ShouldCheckBlockIndex() const;
984
23.2k
    const arith_uint256& MinimumChainWork() const { return *Assert(m_options.minimum_chain_work); }
985
0
    const uint256& AssumedValidBlock() const { return *Assert(m_options.assumed_valid_block); }
986
14
    kernel::Notifications& GetNotifications() const { return m_options.notifications; };
987
988
    /**
989
     * Make various assertions about the state of the block index.
990
     *
991
     * By default this only executes fully when using the Regtest chain; see: m_options.check_block_index.
992
     */
993
    void CheckBlockIndex();
994
995
    /**
996
     * Alias for ::cs_main.
997
     * Should be used in new code to make it easier to make ::cs_main a member
998
     * of this class.
999
     * Generally, methods of this class should be annotated to require this
1000
     * mutex. This will make calling code more verbose, but also help to:
1001
     * - Clarify that the method will acquire a mutex that heavily affects
1002
     *   overall performance.
1003
     * - Force call sites to think how long they need to acquire the mutex to
1004
     *   get consistent results.
1005
     */
1006
13.6k
    RecursiveMutex& GetMutex() const LOCK_RETURNED(::cs_main) { return ::cs_main; }
1007
1008
    const util::SignalInterrupt& m_interrupt;
1009
    const Options m_options;
1010
    std::thread m_thread_load;
1011
    //! A single BlockManager instance is shared across each constructed
1012
    //! chainstate to avoid duplicating block metadata.
1013
    node::BlockManager m_blockman;
1014
1015
    ValidationCache m_validation_cache;
1016
1017
    /**
1018
     * Whether initial block download has ended and IsInitialBlockDownload
1019
     * should return false from now on.
1020
     *
1021
     * Mutable because we need to be able to mark IsInitialBlockDownload()
1022
     * const, which latches this for caching purposes.
1023
     */
1024
    mutable std::atomic<bool> m_cached_finished_ibd{false};
1025
1026
    /**
1027
     * Every received block is assigned a unique and increasing identifier, so we
1028
     * know which one to give priority in case of a fork.
1029
     */
1030
    /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
1031
    int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 1;
1032
    /** Decreasing counter (used by subsequent preciousblock calls). */
1033
    int32_t nBlockReverseSequenceId = -1;
1034
    /** chainwork for the last block that preciousblock has been applied to. */
1035
    arith_uint256 nLastPreciousChainwork = 0;
1036
1037
    // Reset the memory-only sequence counters we use to track block arrival
1038
    // (used by tests to reset state)
1039
    void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
1040
0
    {
1041
0
        AssertLockHeld(::cs_main);
1042
0
        nBlockSequenceId = 1;
1043
0
        nBlockReverseSequenceId = -1;
1044
0
    }
1045
1046
1047
    /**
1048
     * In order to efficiently track invalidity of headers, we keep the set of
1049
     * blocks which we tried to connect and found to be invalid here (ie which
1050
     * were set to BLOCK_FAILED_VALID since the last restart). We can then
1051
     * walk this set and check if a new header is a descendant of something in
1052
     * this set, preventing us from having to walk m_block_index when we try
1053
     * to connect a bad block and fail.
1054
     *
1055
     * While this is more complicated than marking everything which descends
1056
     * from an invalid block as invalid at the time we discover it to be
1057
     * invalid, doing so would require walking all of m_block_index to find all
1058
     * descendants. Since this case should be very rare, keeping track of all
1059
     * BLOCK_FAILED_VALID blocks in a set should be just fine and work just as
1060
     * well.
1061
     *
1062
     * Because we already walk m_block_index in height-order at startup, we go
1063
     * ahead and mark descendants of invalid blocks as FAILED_CHILD at that time,
1064
     * instead of putting things in this set.
1065
     */
1066
    std::set<CBlockIndex*> m_failed_blocks;
1067
1068
    /** Best header we've seen so far (used for getheaders queries' starting points). */
1069
    CBlockIndex* m_best_header GUARDED_BY(::cs_main){nullptr};
1070
1071
    //! The total number of bytes available for us to use across all in-memory
1072
    //! coins caches. This will be split somehow across chainstates.
1073
    int64_t m_total_coinstip_cache{0};
1074
    //
1075
    //! The total number of bytes available for us to use across all leveldb
1076
    //! coins databases. This will be split somehow across chainstates.
1077
    int64_t m_total_coinsdb_cache{0};
1078
1079
    //! Instantiate a new chainstate.
1080
    //!
1081
    //! @param[in] mempool              The mempool to pass to the chainstate
1082
    //                                  constructor
1083
    Chainstate& InitializeChainstate(CTxMemPool* mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1084
1085
    //! Get all chainstates currently being used.
1086
    std::vector<Chainstate*> GetAll();
1087
1088
    //! Construct and activate a Chainstate on the basis of UTXO snapshot data.
1089
    //!
1090
    //! Steps:
1091
    //!
1092
    //! - Initialize an unused Chainstate.
1093
    //! - Load its `CoinsViews` contents from `coins_file`.
1094
    //! - Verify that the hash of the resulting coinsdb matches the expected hash
1095
    //!   per assumeutxo chain parameters.
1096
    //! - Wait for our headers chain to include the base block of the snapshot.
1097
    //! - "Fast forward" the tip of the new chainstate to the base of the snapshot.
1098
    //! - Move the new chainstate to `m_snapshot_chainstate` and make it our
1099
    //!   ChainstateActive().
1100
    [[nodiscard]] util::Result<CBlockIndex*> ActivateSnapshot(
1101
        AutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
1102
1103
    //! Once the background validation chainstate has reached the height which
1104
    //! is the base of the UTXO snapshot in use, compare its coins to ensure
1105
    //! they match those expected by the snapshot.
1106
    //!
1107
    //! If the coins match (expected), then mark the validation chainstate for
1108
    //! deletion and continue using the snapshot chainstate as active.
1109
    //! Otherwise, revert to using the ibd chainstate and shutdown.
1110
    SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1111
1112
    //! Returns nullptr if no snapshot has been loaded.
1113
    const CBlockIndex* GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1114
1115
    //! The most-work chain.
1116
    Chainstate& ActiveChainstate() const;
1117
39.3k
    CChain& ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChainstate().m_chain; }
1118
0
    int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Height(); }
1119
0
    CBlockIndex* ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Tip(); }
1120
1121
    //! The state of a background sync (for net processing)
1122
12.6k
    bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) {
1123
12.6k
        return IsUsable(m_snapshot_chainstate.get()) && IsUsable(m_ibd_chainstate.get());
  Branch (1123:16): [True: 0, False: 12.6k]
  Branch (1123:57): [True: 0, False: 0]
1124
12.6k
    }
1125
1126
    //! The tip of the background sync chain
1127
0
    const CBlockIndex* GetBackgroundSyncTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) {
1128
0
        return BackgroundSyncInProgress() ? m_ibd_chainstate->m_chain.Tip() : nullptr;
  Branch (1128:16): [True: 0, False: 0]
1129
0
    }
1130
1131
    node::BlockMap& BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
1132
0
    {
1133
0
        AssertLockHeld(::cs_main);
1134
0
        return m_blockman.m_block_index;
1135
0
    }
1136
1137
    /**
1138
     * Track versionbit status
1139
     */
1140
    mutable VersionBitsCache m_versionbitscache;
1141
1142
    //! @returns true if a snapshot-based chainstate is in use. Also implies
1143
    //!          that a background validation chainstate is also in use.
1144
    bool IsSnapshotActive() const;
1145
1146
    std::optional<uint256> SnapshotBlockhash() const;
1147
1148
    //! Is there a snapshot in use and has it been fully validated?
1149
    bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
1150
0
    {
1151
0
        return m_snapshot_chainstate && m_ibd_chainstate && m_ibd_chainstate->m_disabled;
  Branch (1151:16): [True: 0, False: 0]
  Branch (1151:41): [True: 0, False: 0]
  Branch (1151:61): [True: 0, False: 0]
1152
0
    }
1153
1154
    /** Check whether we are doing an initial block download (synchronizing from disk or network) */
1155
    bool IsInitialBlockDownload() const;
1156
1157
    /**
1158
     * Import blocks from an external file
1159
     *
1160
     * During reindexing, this function is called for each block file (datadir/blocks/blk?????.dat).
1161
     * It reads all blocks contained in the given file and attempts to process them (add them to the
1162
     * block index). The blocks may be out of order within each file and across files. Often this
1163
     * function reads a block but finds that its parent hasn't been read yet, so the block can't be
1164
     * processed yet. The function will add an entry to the blocks_with_unknown_parent map (which is
1165
     * passed as an argument), so that when the block's parent is later read and processed, this
1166
     * function can re-read the child block from disk and process it.
1167
     *
1168
     * Because a block's parent may be in a later file, not just later in the same file, the
1169
     * blocks_with_unknown_parent map must be passed in and out with each call. It's a multimap,
1170
     * rather than just a map, because multiple blocks may have the same parent (when chain splits
1171
     * or stale blocks exist). It maps from parent-hash to child-disk-position.
1172
     *
1173
     * This function can also be used to read blocks from user-specified block files using the
1174
     * -loadblock= option. There's no unknown-parent tracking, so the last two arguments are omitted.
1175
     *
1176
     *
1177
     * @param[in]     file_in                       File containing blocks to read
1178
     * @param[in]     dbp                           (optional) Disk block position (only for reindex)
1179
     * @param[in,out] blocks_with_unknown_parent    (optional) Map of disk positions for blocks with
1180
     *                                              unknown parent, key is parent block hash
1181
     *                                              (only used for reindex)
1182
     * */
1183
    void LoadExternalBlockFile(
1184
        AutoFile& file_in,
1185
        FlatFilePos* dbp = nullptr,
1186
        std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent = nullptr);
1187
1188
    /**
1189
     * Process an incoming block. This only returns after the best known valid
1190
     * block is made active. Note that it does not, however, guarantee that the
1191
     * specific block passed to it has been checked for validity!
1192
     *
1193
     * If you want to *possibly* get feedback on whether block is valid, you must
1194
     * install a CValidationInterface (see validationinterface.h) - this will have
1195
     * its BlockChecked method called whenever *any* block completes validation.
1196
     *
1197
     * Note that we guarantee that either the proof-of-work is valid on block, or
1198
     * (and possibly also) BlockChecked will have been called.
1199
     *
1200
     * May not be called in a validationinterface callback.
1201
     *
1202
     * @param[in]   block The block we want to process.
1203
     * @param[in]   force_processing Process this block even if unrequested; used for non-network block sources.
1204
     * @param[in]   min_pow_checked  True if proof-of-work anti-DoS checks have
1205
     *                               been done by caller for headers chain
1206
     *                               (note: only affects headers acceptance; if
1207
     *                               block header is already present in block
1208
     *                               index then this parameter has no effect)
1209
     * @param[out]  new_block A boolean which is set to indicate if the block was first received via this call
1210
     * @returns     If the block was processed, independently of block validity
1211
     */
1212
    bool ProcessNewBlock(const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked, bool* new_block) LOCKS_EXCLUDED(cs_main);
1213
1214
    /**
1215
     * Process incoming block headers.
1216
     *
1217
     * May not be called in a
1218
     * validationinterface callback.
1219
     *
1220
     * @param[in]  block The block headers themselves
1221
     * @param[in]  min_pow_checked  True if proof-of-work anti-DoS checks have been done by caller for headers chain
1222
     * @param[out] state This may be set to an Error state if any error occurred processing them
1223
     * @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
1224
     */
1225
    bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
1226
1227
    /**
1228
     * Sufficiently validate a block for disk storage (and store on disk).
1229
     *
1230
     * @param[in]   pblock          The block we want to process.
1231
     * @param[in]   fRequested      Whether we requested this block from a
1232
     *                              peer.
1233
     * @param[in]   dbp             The location on disk, if we are importing
1234
     *                              this block from prior storage.
1235
     * @param[in]   min_pow_checked True if proof-of-work anti-DoS checks have
1236
     *                              been done by caller for headers chain
1237
     *
1238
     * @param[out]  state       The state of the block validation.
1239
     * @param[out]  ppindex     Optional return parameter to get the
1240
     *                          CBlockIndex pointer for this block.
1241
     * @param[out]  fNewBlock   Optional return parameter to indicate if the
1242
     *                          block is new to our storage.
1243
     *
1244
     * @returns   False if the block or header is invalid, or if saving to disk fails (likely a fatal error); true otherwise.
1245
     */
1246
    bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockValidationState& state, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1247
1248
    void ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1249
1250
    /**
1251
     * Try to add a transaction to the memory pool.
1252
     *
1253
     * @param[in]  tx              The transaction to submit for mempool acceptance.
1254
     * @param[in]  test_accept     When true, run validation checks but don't submit to mempool.
1255
     */
1256
    [[nodiscard]] MempoolAcceptResult ProcessTransaction(const CTransactionRef& tx, bool test_accept=false)
1257
        EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1258
1259
    //! Load the block tree and coins database from disk, initializing state if we're running with -reindex
1260
    bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1261
1262
    //! Check to see if caches are out of balance and if so, call
1263
    //! ResizeCoinsCaches() as needed.
1264
    void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1265
1266
    /** Update uncommitted block structures (currently: only the witness reserved value). This is safe for submitted blocks. */
1267
    void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev) const;
1268
1269
    /** Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks). */
1270
    std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev) const;
1271
1272
    /** This is used by net_processing to report pre-synchronization progress of headers, as
1273
     *  headers are not yet fed to validation during that time, but validation is (for now)
1274
     *  responsible for logging and signalling through NotifyHeaderTip, so it needs this
1275
     *  information. */
1276
    void ReportHeadersPresync(const arith_uint256& work, int64_t height, int64_t timestamp);
1277
1278
    //! When starting up, search the datadir for a chainstate based on a UTXO
1279
    //! snapshot that is in the process of being validated.
1280
    bool DetectSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1281
1282
    void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1283
1284
    //! Remove the snapshot-based chainstate and all on-disk artifacts.
1285
    //! Used when reindex{-chainstate} is called during snapshot use.
1286
    [[nodiscard]] bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1287
1288
    //! Switch the active chainstate to one based on a UTXO snapshot that was loaded
1289
    //! previously.
1290
    Chainstate& ActivateExistingSnapshot(uint256 base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1291
1292
    //! If we have validated a snapshot chain during this runtime, copy its
1293
    //! chainstate directory over to the main `chainstate` location, completing
1294
    //! validation of the snapshot.
1295
    //!
1296
    //! If the cleanup succeeds, the caller will need to ensure chainstates are
1297
    //! reinitialized, since ResetChainstates() will be called before leveldb
1298
    //! directories are moved or deleted.
1299
    //!
1300
    //! @sa node/chainstate:LoadChainstate()
1301
    bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1302
1303
    //! @returns the chainstate that indexes should consult when ensuring that an
1304
    //!   index is synced with a chain where we can expect block index entries to have
1305
    //!   BLOCK_HAVE_DATA beneath the tip.
1306
    //!
1307
    //!   In other words, give us the chainstate for which we can reasonably expect
1308
    //!   that all blocks beneath the tip have been indexed. In practice this means
1309
    //!   when using an assumed-valid chainstate based upon a snapshot, return only the
1310
    //!   fully validated chain.
1311
    Chainstate& GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1312
1313
    //! Return the [start, end] (inclusive) of block heights we can prune.
1314
    //!
1315
    //! start > end is possible, meaning no blocks can be pruned.
1316
    std::pair<int, int> GetPruneRange(
1317
        const Chainstate& chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1318
1319
    //! Return the height of the base block of the snapshot in use, if one exists, else
1320
    //! nullopt.
1321
    std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1322
1323
0
    CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
1324
1325
    ~ChainstateManager();
1326
};
1327
1328
/** Deployment* info via ChainstateManager */
1329
template<typename DEP>
1330
bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const ChainstateManager& chainman, DEP dep)
1331
7.06k
{
1332
7.06k
    return DeploymentActiveAfter(pindexPrev, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
1333
7.06k
}
1334
1335
template<typename DEP>
1336
bool DeploymentActiveAt(const CBlockIndex& index, const ChainstateManager& chainman, DEP dep)
1337
0
{
1338
0
    return DeploymentActiveAt(index, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
1339
0
}
1340
1341
template<typename DEP>
1342
bool DeploymentEnabled(const ChainstateManager& chainman, DEP dep)
1343
0
{
1344
0
    return DeploymentEnabled(chainman.GetConsensus(), dep);
1345
0
}
Unexecuted instantiation: bool DeploymentEnabled<Consensus::BuriedDeployment>(ChainstateManager const&, Consensus::BuriedDeployment)
Unexecuted instantiation: bool DeploymentEnabled<Consensus::DeploymentPos>(ChainstateManager const&, Consensus::DeploymentPos)
1346
1347
/** Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) */
1348
bool IsBIP30Repeat(const CBlockIndex& block_index);
1349
1350
/** Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30) */
1351
bool IsBIP30Unspendable(const CBlockIndex& block_index);
1352
1353
#endif // BITCOIN_VALIDATION_H