Coverage Report

Created: 2026-09-14 20:36

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