Coverage Report

Created: 2026-09-02 14:16

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.43k
    static MempoolAcceptResult Failure(TxValidationState state) {
171
9.43k
        return MempoolAcceptResult(state);
172
9.43k
    }
173
174
    static MempoolAcceptResult FeeFailure(TxValidationState state,
175
                                          CFeeRate effective_feerate,
176
190
                                          const std::vector<Wtxid>& wtxids_fee_calculations) {
177
190
        return MempoolAcceptResult(state, effective_feerate, wtxids_fee_calculations);
178
190
    }
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
95
    static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees) {
190
95
        return MempoolAcceptResult(vsize, fees);
191
95
    }
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.43k
        : m_result_type(ResultType::INVALID), m_state(state) {
202
9.43k
            Assume(!state.IsValid()); // Can be invalid or error
203
9.43k
        }
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
190
        : m_result_type(ResultType::INVALID),
223
190
        m_state(state),
224
190
        m_effective_feerate(effective_feerate),
225
190
        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
95
        : 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
794
        : 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.34k
        : 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
307k
        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
332k
    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
427k
{
519
    // No periodic flush needed if at least this much space is free
520
427k
    constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES{int64_t(10_MiB)};
521
427k
    return std::max((total_space * 9) / 10,
522
427k
                    total_space - MAX_BLOCK_COINSDB_USAGE_BYTES);
523
427k
}
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
    //! Cached result of LookupBlockIndex(*m_target_blockhash)
570
    mutable const CBlockIndex* m_cached_target_block GUARDED_BY(::cs_main){nullptr};
571
572
    std::optional<const char*> m_last_script_check_reason_logged GUARDED_BY(::cs_main){};
573
574
public:
575
    //! Reference to a BlockManager instance which itself is shared across all
576
    //! Chainstate instances.
577
    node::BlockManager& m_blockman;
578
579
    //! The chainstate manager that owns this chainstate. The reference is
580
    //! necessary so that this instance can check whether it is the active
581
    //! chainstate within deeply nested method calls.
582
    ChainstateManager& m_chainman;
583
584
    explicit Chainstate(
585
        CTxMemPool* mempool,
586
        node::BlockManager& blockman,
587
        ChainstateManager& chainman,
588
        std::optional<uint256> from_snapshot_blockhash = std::nullopt);
589
590
    //! Return path to chainstate leveldb directory.
591
    fs::path StoragePath() const;
592
593
    //! Return the current role of the chainstate. See `ChainstateManager`
594
    //! documentation for a description of the different types of chainstates.
595
    //!
596
    //! @sa ChainstateRole
597
    kernel::ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
598
599
    /**
600
     * Initialize the CoinsViews UTXO set database management data structures. The in-memory
601
     * cache is initialized separately.
602
     *
603
     * All parameters forwarded to CoinsViews.
604
     */
605
    void InitCoinsDB(
606
        size_t cache_size_bytes,
607
        bool in_memory,
608
        bool should_wipe);
609
610
    //! Initialize the in-memory coins cache (to be done after the health of the on-disk database
611
    //! is verified).
612
    void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
613
614
    //! @returns whether or not the CoinsViews object has been fully initialized and we can
615
    //!          safely flush this object to disk.
616
    bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
617
373k
    {
618
373k
        AssertLockHeld(::cs_main);
619
373k
        return m_coins_views && m_coins_views->m_cacheview;
620
373k
    }
621
622
    //! The current chain of blockheaders we consult and build on.
623
    //! @see CChain, CBlockIndex.
624
    CChain m_chain;
625
626
    //! Assumeutxo state indicating whether all blocks in the chain were
627
    //! validated, or if the chainstate is based on an assumeutxo snapshot and
628
    //! the snapshot has not been validated.
629
    Assumeutxo m_assumeutxo GUARDED_BY(::cs_main);
630
631
    /**
632
     * The blockhash which is the base of the snapshot this chainstate was created from.
633
     *
634
     * std::nullopt if this chainstate was not created from a snapshot.
635
     */
636
    const std::optional<uint256> m_from_snapshot_blockhash;
637
638
    //! Target block for this chainstate. If this is not set, chainstate will
639
    //! target the most-work, valid block. If this is set, ChainstateManager
640
    //! considers this a "historical" chainstate since it will only contain old
641
    //! blocks up to the target block, not newer blocks.
642
    std::optional<uint256> m_target_blockhash GUARDED_BY(::cs_main);
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.
658
    const CBlockIndex* TargetBlock() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
659
    //! Set target block for this chainstate. If null, chainstate will target
660
    //! the most-work valid block. If non-null chainstate will be a historic
661
    //! chainstate and target the specified block.
662
    void SetTargetBlock(CBlockIndex* block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
663
    //! Set target block for this chainstate using just a block hash. Useful
664
    //! when the block database has not been loaded yet.
665
    void SetTargetBlockHash(uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
666
667
    //! Return true if chainstate reached target block.
668
    bool ReachedTarget() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
669
196k
    {
670
196k
        const CBlockIndex* target_block{TargetBlock()};
671
196k
        assert(!target_block || target_block->GetAncestor(m_chain.Height()) == m_chain.Tip());
672
196k
        return target_block && target_block == m_chain.Tip();
673
196k
    }
674
675
    /**
676
     * The set of all CBlockIndex entries that have as much work as our current
677
     * tip or more, and transaction data needed to be validated (with
678
     * BLOCK_VALID_TRANSACTIONS for each block and its parents back to the
679
     * genesis block or an assumeutxo snapshot block). Entries may be failed,
680
     * though, and pruning nodes may be missing the data for the block.
681
     */
682
    std::set<CBlockIndex*, node::CBlockIndexWorkComparator> setBlockIndexCandidates;
683
684
    //! @returns A reference to the in-memory cache of the UTXO set.
685
    CCoinsViewCache& CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
686
2.13M
    {
687
2.13M
        AssertLockHeld(::cs_main);
688
2.13M
        Assert(m_coins_views);
689
2.13M
        return *Assert(m_coins_views->m_cacheview);
690
2.13M
    }
691
692
    //! @returns A reference to the on-disk UTXO set database.
693
    CCoinsViewDB& CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
694
4.71k
    {
695
4.71k
        AssertLockHeld(::cs_main);
696
4.71k
        return Assert(m_coins_views)->m_dbview;
697
4.71k
    }
698
699
    //! @returns A pointer to the mempool.
700
    CTxMemPool* GetMempool()
701
201k
    {
702
201k
        return m_mempool;
703
201k
    }
704
705
    //! @returns A reference to a wrapped view of the in-memory UTXO set that
706
    //!     handles disk read errors gracefully.
707
    CCoinsViewErrorCatcher& CoinsErrorCatcher() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
708
1.08k
    {
709
1.08k
        AssertLockHeld(::cs_main);
710
1.08k
        return Assert(m_coins_views)->m_catcherview;
711
1.08k
    }
712
713
    //! Destructs all objects related to accessing the UTXO set.
714
1.09k
    void ResetCoinsViews() { m_coins_views.reset(); }
715
716
    //! The cache size of the on-disk coins view.
717
    size_t m_coinsdb_cache_size_bytes{0};
718
719
    //! The cache size of the in-memory coins view.
720
    size_t m_coinstip_cache_size_bytes{0};
721
722
    //! Resize the CoinsViews caches dynamically and flush state to disk.
723
    //! @returns true unless an error occurred during the flush.
724
    bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
725
        EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
726
727
    /**
728
     * Update the on-disk chain state.
729
     * The caches and indexes are flushed depending on the mode we're called with
730
     * if they're too large, if it's been a while since the last write,
731
     * or always and in all cases if we're in prune mode and are deleting files.
732
     *
733
     * If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything
734
     * besides checking if we need to prune.
735
     *
736
     * @returns true unless a system error occurred
737
     */
738
    bool FlushStateToDisk(
739
        BlockValidationState& state,
740
        FlushStateMode mode,
741
        int nManualPruneHeight = 0);
742
743
    //! Flush all changes to disk.
744
    void ForceFlushStateToDisk(bool wipe_cache = true);
745
746
    //! Prune blockfiles from the disk if necessary and then flush chainstate changes
747
    //! if we pruned.
748
    void PruneAndFlush();
749
750
    /**
751
     * Find the best known block, and make it the tip of the block chain. The
752
     * result is either failure or an activated best chain. pblock is either
753
     * nullptr or a pointer to a block that is already loaded (to avoid loading
754
     * it again from disk).
755
     *
756
     * ActivateBestChain is split into steps (see ActivateBestChainStep) so that
757
     * we avoid holding cs_main for an extended period of time; the length of this
758
     * call may be quite long during reindexing or a substantial reorg.
759
     *
760
     * May not be called with cs_main held. May not be called in a
761
     * validationinterface callback.
762
     *
763
     * Note that if this is called while a snapshot chainstate is active, and if
764
     * it is called on a validated chainstate whose tip has reached the base
765
     * block of the snapshot, its execution will take *MINUTES* while it hashes
766
     * the UTXO set to verify the assumeutxo value the snapshot was activated
767
     * with. `cs_main` will be held during this time.
768
     *
769
     * @returns true unless a system error occurred
770
     */
771
    bool ActivateBestChain(
772
        BlockValidationState& state,
773
        std::shared_ptr<const CBlock> pblock = nullptr)
774
        EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
775
        LOCKS_EXCLUDED(::cs_main);
776
777
    // Block (dis)connection on a given view:
778
    DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
779
        EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
780
    bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
781
                      CCoinsViewCache& view, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
782
783
    // Apply the effects of a block disconnection on the UTXO set.
784
    bool DisconnectTip(BlockValidationState& state, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
785
786
    // Manual block validity manipulation:
787
    /** Mark a block as precious and reorganize.
788
     *
789
     * May not be called in a validationinterface callback.
790
     */
791
    bool PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
792
        EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
793
        LOCKS_EXCLUDED(::cs_main);
794
795
    /** Mark a block as invalid. */
796
    bool InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex)
797
        EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
798
        LOCKS_EXCLUDED(::cs_main);
799
800
    /** Set invalidity status to all descendants of a block */
801
    void SetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
802
803
    /** Remove invalidity status from a block, its descendants and ancestors and reconsider them for activation */
804
    void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
805
806
    /** Replay blocks that aren't fully applied to the database. */
807
    bool ReplayBlocks();
808
809
    /** Whether the chain state needs to be redownloaded due to lack of witness data */
810
    [[nodiscard]] bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
811
812
    /** Add a block to the candidate set if it has as much work as the current tip. */
813
    void TryAddBlockIndexCandidate(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
814
815
    void PruneBlockIndexCandidates();
816
817
    void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
818
819
    /** Populate the candidate set by calling TryAddBlockIndexCandidate on all valid block indices. */
820
    void PopulateBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
821
822
    /** Find the last common block of this chain and a locator. */
823
    const CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
824
825
    /** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
826
    bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
827
828
    //! Dictates whether we need to flush the cache to disk or not.
829
    //!
830
    //! @return the state of the size of the coins cache.
831
    CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
832
833
    CoinsCacheSizeState GetCoinsCacheSizeState(
834
        size_t max_coins_cache_size_bytes,
835
        size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
836
837
    std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
838
839
    //! Get the last block that was flushed to disk.
840
304
    const CBlockIndex* GetLastFlushedBlock() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { return m_last_flushed_block; }
841
842
    //! Indirection necessary to make lock annotations work with an optional mempool.
843
    RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs)
844
125k
    {
845
125k
        return m_mempool ? &m_mempool->cs : nullptr;
846
125k
    }
847
848
    //! Return the [start, end] (inclusive) of block heights we can prune.
849
    //!
850
    //! start > end is possible, meaning no blocks can be pruned.
851
    std::pair<int, int> GetPruneRange(int last_height_can_prune) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
852
853
protected:
854
    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);
855
    bool ConnectTip(
856
        BlockValidationState& state,
857
        CBlockIndex* pindexNew,
858
        std::shared_ptr<const CBlock> block_to_connect,
859
        std::vector<ConnectedBlock>& connected_blocks,
860
        DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
861
862
    void InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
863
    CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
864
865
    bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
866
867
    void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
868
    void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
869
870
    /**
871
     * Make mempool consistent after a reorg, by re-adding or recursively erasing
872
     * disconnected block transactions from the mempool, and also removing any
873
     * other transactions from the mempool that are no longer valid given the new
874
     * tip/height.
875
     *
876
     * Note: we assume that disconnectpool only contains transactions that are NOT
877
     * confirmed in the current chain nor already in the mempool (otherwise,
878
     * in-mempool descendants of such transactions would be removed).
879
     *
880
     * Passing fAddToMempool=false will skip trying to add the transactions back,
881
     * and instead just erase from the mempool as needed.
882
     */
883
    void MaybeUpdateMempoolForReorg(
884
        DisconnectedBlockTransactions& disconnectpool,
885
        bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
886
887
    /** Check warning conditions and do some notifications on new chain tip set. */
888
    void UpdateTip(const CBlockIndex* pindexNew)
889
        EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
890
891
    NodeClock::time_point m_next_write{NodeClock::time_point::max()};
892
    const CBlockIndex* m_last_flushed_block GUARDED_BY(::cs_main){nullptr};
893
894
    /**
895
     * In case of an invalid snapshot, rename the coins leveldb directory so
896
     * that it can be examined for issue diagnosis.
897
     */
898
    [[nodiscard]] util::Result<void> InvalidateCoinsDBOnDisk() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
899
900
    friend ChainstateManager;
901
};
902
903
enum class SnapshotCompletionResult {
904
    SUCCESS,
905
    SKIPPED,
906
907
    // Expected assumeutxo configuration data is not found for the height of the
908
    // base block.
909
    MISSING_CHAINPARAMS,
910
911
    // Failed to generate UTXO statistics (to check UTXO set hash) for the
912
    // validated chainstate.
913
    STATS_FAILED,
914
915
    // The UTXO set hash of the validated chainstate does not match the one
916
    // expected by assumeutxo chainparams.
917
    HASH_MISMATCH,
918
};
919
920
/**
921
 * Interface for managing multiple \ref Chainstate objects, where each
922
 * chainstate is associated with chainstate* subdirectory in the data directory
923
 * and contains a database of UTXOs existing at a different point in history.
924
 * (See \ref Chainstate class for more information.)
925
 *
926
 * Normally there is exactly one Chainstate, which contains the UTXO set of
927
 * chain tip if syncing is completed, or the UTXO set the most recent validated
928
 * block if the initial sync is still in progress.
929
 *
930
 * However, if an assumeutxo snapshot is loaded before syncing is completed,
931
 * there will be two chainstates. The original fully validated chainstate will
932
 * continue to exist and download new blocks in the background. But the new
933
 * snapshot which is loaded will become a second chainstate. The second
934
 * chainstate will be used as the chain tip for the wallet and RPCs even though
935
 * it is only assumed to be valid. When the initial chainstate catches up to the
936
 * snapshot height and confirms that the assumeutxo snapshot is actually valid,
937
 * the second chainstate will be marked validated and become the only chainstate
938
 * again.
939
 */
940
class ChainstateManager
941
{
942
private:
943
944
    /** The last header for which a headerTip notification was issued. */
945
    CBlockIndex* m_last_notified_header GUARDED_BY(GetMutex()){nullptr};
946
947
    bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex());
948
949
    //! Internal helper for ActivateSnapshot().
950
    //!
951
    //! De-serialization of a snapshot that is created with
952
    //! the dumptxoutset RPC.
953
    //! To reduce space the serialization format of the snapshot avoids
954
    //! duplication of tx hashes. The code takes advantage of the guarantee by
955
    //! leveldb that keys are lexicographically sorted.
956
    [[nodiscard]] util::Result<void> PopulateAndValidateSnapshot(
957
        Chainstate& snapshot_chainstate,
958
        AutoFile& coins_file,
959
        const node::SnapshotMetadata& metadata);
960
961
    /**
962
     * If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
963
     * that it doesn't descend from an invalid block, and then add it to m_block_index.
964
     * Caller must set min_pow_checked=true in order to add a new header to the
965
     * block index (permanent memory storage), indicating that the header is
966
     * known to be part of a sufficiently high-work chain (anti-dos check).
967
     */
968
    bool AcceptBlockHeader(
969
        const CBlockHeader& block,
970
        BlockValidationState& state,
971
        CBlockIndex** ppindex,
972
        bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
973
    friend Chainstate;
974
975
    /** Most recent headers presync progress update, for rate-limiting. */
976
    MockableSteadyClock::time_point m_last_presync_update GUARDED_BY(GetMutex()){};
977
978
    //! A queue for script verifications that have to be performed by worker threads.
979
    CCheckQueue<CScriptCheck> m_script_check_queue;
980
981
    //! Timers and counters used for benchmarking validation in both background
982
    //! and active chainstates.
983
    SteadyClock::duration GUARDED_BY(::cs_main) time_check{};
984
    SteadyClock::duration GUARDED_BY(::cs_main) time_forks{};
985
    SteadyClock::duration GUARDED_BY(::cs_main) time_connect{};
986
    SteadyClock::duration GUARDED_BY(::cs_main) time_verify{};
987
    SteadyClock::duration GUARDED_BY(::cs_main) time_undo{};
988
    SteadyClock::duration GUARDED_BY(::cs_main) time_index{};
989
    SteadyClock::duration GUARDED_BY(::cs_main) time_total{};
990
    int64_t GUARDED_BY(::cs_main) num_blocks_total{0};
991
    SteadyClock::duration GUARDED_BY(::cs_main) time_connect_total{};
992
    SteadyClock::duration GUARDED_BY(::cs_main) time_flush{};
993
    SteadyClock::duration GUARDED_BY(::cs_main) time_chainstate{};
994
    SteadyClock::duration GUARDED_BY(::cs_main) time_post_connect{};
995
996
protected:
997
    CBlockIndex* m_best_invalid GUARDED_BY(::cs_main){nullptr};
998
999
public:
1000
    using Options = kernel::ChainstateManagerOpts;
1001
1002
    explicit ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options);
1003
1004
    //! Function to restart active indexes; set dynamically to avoid a circular
1005
    //! dependency on `base/index.cpp`.
1006
    std::function<void()> snapshot_download_completed = std::function<void()>();
1007
1008
743k
    const CChainParams& GetParams() const { return m_options.chainparams; }
1009
4.28M
    const Consensus::Params& GetConsensus() const { return m_options.chainparams.GetConsensus(); }
1010
    bool ShouldCheckBlockIndex() const;
1011
211k
    const arith_uint256& MinimumChainWork() const { return *Assert(m_options.minimum_chain_work); }
1012
154k
    const uint256& AssumedValidBlock() const { return *Assert(m_options.assumed_valid_block); }
1013
272k
    kernel::Notifications& GetNotifications() const { return m_options.notifications; };
1014
1015
    /**
1016
     * Make various assertions about the state of the block index.
1017
     *
1018
     * By default this only executes fully when using the Regtest chain; see: m_options.check_block_index.
1019
     */
1020
    void CheckBlockIndex() const;
1021
1022
    /**
1023
     * Alias for ::cs_main.
1024
     * Should be used in new code to make it easier to make ::cs_main a member
1025
     * of this class.
1026
     * Generally, methods of this class should be annotated to require this
1027
     * mutex. This will make calling code more verbose, but also help to:
1028
     * - Clarify that the method will acquire a mutex that heavily affects
1029
     *   overall performance.
1030
     * - Force call sites to think how long they need to acquire the mutex to
1031
     *   get consistent results.
1032
     */
1033
746k
    RecursiveMutex& GetMutex() const LOCK_RETURNED(::cs_main) { return ::cs_main; }
1034
1035
    const util::SignalInterrupt& m_interrupt;
1036
    const Options m_options;
1037
    //! A single BlockManager instance is shared across each constructed
1038
    //! chainstate to avoid duplicating block metadata.
1039
    node::BlockManager m_blockman;
1040
1041
    ValidationCache m_validation_cache;
1042
1043
    /**
1044
     * Whether initial block download (IBD) is ongoing.
1045
     *
1046
     * This value is used for lock-free IBD checks, and latches from true to
1047
     * false once block loading has finished and the current chain tip has
1048
     * enough work and is recent.
1049
     */
1050
    std::atomic_bool m_cached_is_ibd{true};
1051
1052
    /**
1053
     * Every received block is assigned a unique and increasing identifier, so we
1054
     * know which one to give priority in case of a fork.
1055
     */
1056
    /** Blocks loaded from disk are assigned id SEQ_ID_INIT_FROM_DISK{1}
1057
     * (SEQ_ID_BEST_CHAIN_FROM_DISK{0} if they belong to the best chain loaded from disk),
1058
     * so start the counter after that. **/
1059
    int32_t nBlockSequenceId GUARDED_BY(::cs_main) = SEQ_ID_INIT_FROM_DISK + 1;
1060
    /** Decreasing counter (used by subsequent preciousblock calls). */
1061
    int32_t nBlockReverseSequenceId = -1;
1062
    /** chainwork for the last block that preciousblock has been applied to. */
1063
    arith_uint256 nLastPreciousChainwork = 0;
1064
1065
    // Reset the memory-only sequence counters we use to track block arrival
1066
    // (used by tests to reset state)
1067
    void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
1068
2
    {
1069
2
        AssertLockHeld(::cs_main);
1070
2
        nBlockSequenceId = SEQ_ID_INIT_FROM_DISK + 1;
1071
2
        nBlockReverseSequenceId = -1;
1072
2
    }
1073
1074
1075
    /** Best header we've seen so far for which the block is not known to be invalid
1076
        (used, among others, for getheaders queries' starting points).
1077
        In case of multiple best headers with the same work, it could point to any
1078
        because CBlockIndexWorkComparator tiebreaker rules are not applied. */
1079
    CBlockIndex* m_best_header GUARDED_BY(::cs_main){nullptr};
1080
1081
    //! The total number of bytes available for us to use across all in-memory
1082
    //! coins caches. This will be split somehow across chainstates.
1083
    size_t m_total_coinstip_cache{0};
1084
    //
1085
    //! The total number of bytes available for us to use across all leveldb
1086
    //! coins databases. This will be split somehow across chainstates.
1087
    size_t m_total_coinsdb_cache{0};
1088
1089
    /// Ensures a genesis block is in the block tree, possibly writing one to disk.
1090
    [[nodiscard]] bool LoadGenesisBlock();
1091
1092
    //! Instantiate a new chainstate.
1093
    //!
1094
    //! @param[in] mempool              The mempool to pass to the chainstate
1095
    //                                  constructor
1096
    Chainstate& InitializeChainstate(CTxMemPool* mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1097
1098
    //! Construct and activate a Chainstate on the basis of UTXO snapshot data.
1099
    //!
1100
    //! Steps:
1101
    //!
1102
    //! - Initialize an unused Chainstate.
1103
    //! - Load its `CoinsViews` contents from `coins_file`.
1104
    //! - Verify that the hash of the resulting coinsdb matches the expected hash
1105
    //!   per assumeutxo chain parameters.
1106
    //! - Wait for our headers chain to include the base block of the snapshot.
1107
    //! - "Fast forward" the tip of the new chainstate to the base of the snapshot.
1108
    //! - Construct the new Chainstate and add it to m_chainstates.
1109
    [[nodiscard]] util::Result<CBlockIndex*> ActivateSnapshot(
1110
        AutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
1111
1112
    //! Try to validate an assumeutxo snapshot by using a validated historical
1113
    //! chainstate targeted at the snapshot block. When the target block is
1114
    //! reached, the UTXO hash is computed and saved to
1115
    //! `validated_cs.m_target_utxohash`, and `unvalidated_cs.m_assumeutxo` will
1116
    //! be updated from UNVALIDATED to either VALIDATED or INVALID depending on
1117
    //! whether the hash matches. The INVALID case should not happen in practice
1118
    //! because the software should refuse to load unrecognized snapshots, but
1119
    //! if it does happen, it is a fatal error.
1120
    SnapshotCompletionResult MaybeValidateSnapshot(Chainstate& validated_cs, Chainstate& unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1121
1122
    //! Return current chainstate targeting the most-work, network tip.
1123
    Chainstate& CurrentChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
1124
4.68M
    {
1125
4.76M
        for (auto& cs : m_chainstates) {
1126
4.76M
            if (cs && cs->m_assumeutxo != Assumeutxo::INVALID && !cs->m_target_blockhash) return *cs;
1127
4.76M
        }
1128
0
        abort();
1129
4.68M
    }
1130
1131
    //! Return historical chainstate targeting a specific block, if any.
1132
    Chainstate* HistoricalChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
1133
472k
    {
1134
475k
        for (auto& cs : m_chainstates) {
1135
475k
            if (cs && cs->m_assumeutxo != Assumeutxo::INVALID && cs->m_target_blockhash && !cs->m_target_utxohash) return cs.get();
1136
475k
        }
1137
469k
        return nullptr;
1138
472k
    }
1139
1140
    //! Return fully validated chainstate that should be used for indexing, to
1141
    //! support indexes that need to index blocks in order and can't start from
1142
    //! the snapshot block.
1143
    Chainstate& ValidatedChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
1144
1.25k
    {
1145
1.26k
        for (auto* cs : {&CurrentChainstate(), HistoricalChainstate()}) {
1146
1.26k
            if (cs && cs->m_assumeutxo == Assumeutxo::VALIDATED) return *cs;
1147
1.26k
        }
1148
0
        abort();
1149
1.25k
    }
1150
1151
    //! Remove a chainstate.
1152
    std::unique_ptr<Chainstate> RemoveChainstate(Chainstate& chainstate) EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
1153
3
    {
1154
6
        auto it{std::find_if(m_chainstates.begin(), m_chainstates.end(), [&](auto& cs) { return cs.get() == &chainstate; })};
1155
3
        if (it != m_chainstates.end()) {
1156
3
            auto ret{std::move(*it)};
1157
3
            m_chainstates.erase(it);
1158
3
            return ret;
1159
3
        }
1160
0
        return nullptr;
1161
3
    }
1162
1163
    //! Alternatives to CurrentChainstate() used by older code to query latest
1164
    //! chainstate information without locking cs_main. Newer code should avoid
1165
    //! querying ChainstateManager and use Chainstate objects directly, or
1166
    //! should use CurrentChainstate() instead.
1167
    //! @{
1168
    Chainstate& ActiveChainstate() const;
1169
3.02M
    CChain& ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChainstate().m_chain; }
1170
113k
    int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Height(); }
1171
442k
    CBlockIndex* ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Tip(); }
1172
    //! @}
1173
1174
    /**
1175
     * Update and possibly latch the IBD status.
1176
     *
1177
     * If block loading has finished and the current chain tip has enough work
1178
     * and is recent, set `m_cached_is_ibd` to false. This function never sets
1179
     * the flag back to true.
1180
     *
1181
     * This should be called after operations that may affect IBD exit
1182
     * conditions (e.g. after updating the active chain tip, or after
1183
     * `ImportBlocks()` finishes).
1184
     */
1185
    void UpdateIBDStatus() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1186
1187
    node::BlockMap& BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
1188
2.41k
    {
1189
2.41k
        AssertLockHeld(::cs_main);
1190
2.41k
        return m_blockman.m_block_index;
1191
2.41k
    }
1192
1193
    /**
1194
     * Track versionbit status
1195
     */
1196
    mutable VersionBitsCache m_versionbitscache;
1197
1198
    /** Check whether we are doing an initial block download (synchronizing from disk or network) */
1199
    bool IsInitialBlockDownload() const noexcept;
1200
1201
    /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).
1202
    * This is also the case in the assumeutxo context, meaning that the progress reported for
1203
    * the snapshot chainstate may suggest that all historical blocks have already been verified
1204
    * even though that may not actually be the case. */
1205
    double GuessVerificationProgress(const CBlockIndex* pindex) const EXCLUSIVE_LOCKS_REQUIRED(GetMutex());
1206
1207
    /** Guess background verification progress in case assume-utxo was used (as a fraction between 0.0=genesis and 1.0=snapshot blocks). */
1208
    double GetBackgroundVerificationProgress(const CBlockIndex& pindex) const EXCLUSIVE_LOCKS_REQUIRED(GetMutex());
1209
1210
    /**
1211
     * Import blocks from an external file
1212
     *
1213
     * During reindexing, this function is called for each block file (datadir/blocks/blk?????.dat).
1214
     * It reads all blocks contained in the given file and attempts to process them (add them to the
1215
     * block index). The blocks may be out of order within each file and across files. Often this
1216
     * function reads a block but finds that its parent hasn't been read yet, so the block can't be
1217
     * processed yet. The function will add an entry to the blocks_with_unknown_parent map (which is
1218
     * passed as an argument), so that when the block's parent is later read and processed, this
1219
     * function can re-read the child block from disk and process it.
1220
     *
1221
     * Because a block's parent may be in a later file, not just later in the same file, the
1222
     * blocks_with_unknown_parent map must be passed in and out with each call. It's a multimap,
1223
     * rather than just a map, because multiple blocks may have the same parent (when chain splits
1224
     * or stale blocks exist). It maps from parent-hash to child-disk-position.
1225
     *
1226
     * This function can also be used to read blocks from user-specified block files using the
1227
     * -loadblock= option. There's no unknown-parent tracking, so the last two arguments are omitted.
1228
     *
1229
     *
1230
     * @param[in]     file_in                       File containing blocks to read
1231
     * @param[in]     dbp                           (optional) Disk block position (only for reindex)
1232
     * @param[in,out] blocks_with_unknown_parent    (optional) Map of disk positions for blocks with
1233
     *                                              unknown parent, key is parent block hash
1234
     *                                              (only used for reindex)
1235
     * */
1236
    void LoadExternalBlockFile(
1237
        AutoFile& file_in,
1238
        FlatFilePos* dbp = nullptr,
1239
        std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent = nullptr);
1240
1241
    /**
1242
     * Process an incoming block. This only returns after the best known valid
1243
     * block is made active. Note that it does not, however, guarantee that the
1244
     * specific block passed to it has been checked for validity!
1245
     *
1246
     * If you want to *possibly* get feedback on whether block is valid, you must
1247
     * install a CValidationInterface (see validationinterface.h) - this will have
1248
     * its BlockChecked method called whenever *any* block completes validation.
1249
     *
1250
     * Note that we guarantee that either the proof-of-work is valid on block, or
1251
     * (and possibly also) BlockChecked will have been called.
1252
     *
1253
     * May not be called in a validationinterface callback.
1254
     *
1255
     * @param[in]   block The block we want to process.
1256
     * @param[in]   force_processing Process this block even if unrequested; used for non-network block sources.
1257
     * @param[in]   min_pow_checked  True if proof-of-work anti-DoS checks have
1258
     *                               been done by caller for headers chain
1259
     *                               (note: only affects headers acceptance; if
1260
     *                               block header is already present in block
1261
     *                               index then this parameter has no effect)
1262
     * @param[out]  new_block A boolean which is set to indicate if the block was first received via this call
1263
     * @returns     If the block was processed, independently of block validity
1264
     */
1265
    bool ProcessNewBlock(const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked, bool* new_block) LOCKS_EXCLUDED(cs_main);
1266
1267
    /**
1268
     * Process incoming block headers.
1269
     *
1270
     * May not be called in a
1271
     * validationinterface callback.
1272
     *
1273
     * @param[in]  headers The block headers themselves
1274
     * @param[in]  min_pow_checked  True if proof-of-work anti-DoS checks have been done by caller for headers chain
1275
     * @param[out] state This may be set to an Error state if any error occurred processing them
1276
     * @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
1277
     * @returns false if AcceptBlockHeader fails on any of the headers, true otherwise (including if headers were already known)
1278
     */
1279
    bool ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
1280
1281
    /**
1282
     * Sufficiently validate a block for disk storage (and store on disk).
1283
     *
1284
     * @param[in]   pblock          The block we want to process.
1285
     * @param[in]   fRequested      Whether we requested this block from a
1286
     *                              peer.
1287
     * @param[in]   dbp             The location on disk, if we are importing
1288
     *                              this block from prior storage.
1289
     * @param[in]   min_pow_checked True if proof-of-work anti-DoS checks have
1290
     *                              been done by caller for headers chain
1291
     *
1292
     * @param[out]  state       The state of the block validation.
1293
     * @param[out]  ppindex     Optional return parameter to get the
1294
     *                          CBlockIndex pointer for this block.
1295
     * @param[out]  fNewBlock   Optional return parameter to indicate if the
1296
     *                          block is new to our storage.
1297
     *
1298
     * @returns   False if the block or header is invalid, or if saving to disk fails (likely a fatal error); true otherwise.
1299
     */
1300
    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);
1301
1302
    void ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1303
1304
    /**
1305
     * Try to add a transaction to the memory pool.
1306
     *
1307
     * @param[in]  tx              The transaction to submit for mempool acceptance.
1308
     * @param[in]  test_accept     When true, run validation checks but don't submit to mempool.
1309
     */
1310
    [[nodiscard]] MempoolAcceptResult ProcessTransaction(const CTransactionRef& tx, bool test_accept=false)
1311
        EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1312
1313
    //! Load the block tree and coins database from disk, initializing state if we're running with -reindex
1314
    bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1315
1316
    //! Check to see if caches are out of balance and if so, call
1317
    //! ResizeCoinsCaches() as needed.
1318
    void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1319
1320
    /**
1321
     * Update uncommitted block structures (currently: only the witness reserved
1322
     * value). This is safe for submitted blocks as long as they honor
1323
     * default_witness_commitment from the template.
1324
     */
1325
    void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev) const;
1326
1327
    /** Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks). */
1328
    void GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev) const;
1329
1330
    /** This is used by net_processing to report pre-synchronization progress of headers, as
1331
     *  headers are not yet fed to validation during that time, but validation is (for now)
1332
     *  responsible for logging and signalling through NotifyHeaderTip, so it needs this
1333
     *  information. */
1334
    void ReportHeadersPresync(int64_t height, int64_t timestamp);
1335
1336
    //! When starting up, search the datadir for a chainstate based on a UTXO
1337
    //! snapshot that is in the process of being validated and load it if found.
1338
    //! Return pointer to the Chainstate if it is loaded.
1339
    Chainstate* LoadAssumeutxoChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1340
1341
    //! Add new chainstate.
1342
    Chainstate& AddChainstate(std::unique_ptr<Chainstate> chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1343
1344
    void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1345
1346
    //! Remove the chainstate and all on-disk artifacts.
1347
    //! Used when reindex{-chainstate} is called during snapshot use.
1348
    [[nodiscard]] bool DeleteChainstate(Chainstate& chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1349
1350
    //! If we have validated a snapshot chain during this runtime, copy its
1351
    //! chainstate directory over to the main `chainstate` location, completing
1352
    //! validation of the snapshot.
1353
    //!
1354
    //! If the cleanup succeeds, the caller will need to ensure chainstates are
1355
    //! reinitialized, since ResetChainstates() will be called before leveldb
1356
    //! directories are moved or deleted.
1357
    //!
1358
    //! @sa node/chainstate:LoadChainstate()
1359
    bool ValidatedSnapshotCleanup(Chainstate& validated_cs, Chainstate& unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1360
1361
    //! Get range of historical blocks to download.
1362
    std::optional<std::pair<const CBlockIndex*, const CBlockIndex*>> GetHistoricalBlockRange() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1363
1364
    //! Call ActivateBestChain() on every chainstate.
1365
    util::Result<void> ActivateBestChains() LOCKS_EXCLUDED(::cs_main);
1366
1367
    //! If, due to invalidation / reconsideration of blocks, the previous
1368
    //! best header is no longer valid / guaranteed to be the most-work
1369
    //! header in our block-index not known to be invalid, recalculate it.
1370
    void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1371
1372
    //! Returns how many blocks the best header is ahead of the current tip,
1373
    //! or nullopt if the best header does not extend the tip.
1374
    std::optional<int> BlocksAheadOfTip() const LOCKS_EXCLUDED(::cs_main);
1375
1376
148k
    CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
1377
1378
    ~ChainstateManager();
1379
1380
    //! List of chainstates. Note: in general, it is not safe to delete
1381
    //! Chainstate objects once they are added to this list because there is no
1382
    //! mutex that can be locked to prevent Chainstate pointers from being used
1383
    //! while they are deleted. (cs_main doesn't work because it is too narrow
1384
    //! and is released in the middle of Chainstate::ActivateBestChain to let
1385
    //! notifications be processed. m_chainstate_mutex doesn't work because it
1386
    //! is not locked at other times when the chainstate is in use.)
1387
    std::vector<std::unique_ptr<Chainstate>> m_chainstates GUARDED_BY(::cs_main);
1388
};
1389
1390
/** Deployment* info via ChainstateManager */
1391
template<typename DEP>
1392
bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const ChainstateManager& chainman, DEP dep)
1393
546k
{
1394
546k
    return DeploymentActiveAfter(pindexPrev, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
1395
546k
}
1396
1397
template<typename DEP>
1398
bool DeploymentActiveAt(const CBlockIndex& index, const ChainstateManager& chainman, DEP dep)
1399
1.24M
{
1400
1.24M
    return DeploymentActiveAt(index, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
1401
1.24M
}
1402
1403
template<typename DEP>
1404
bool DeploymentEnabled(const ChainstateManager& chainman, DEP dep)
1405
570
{
1406
570
    return DeploymentEnabled(chainman.GetConsensus(), dep);
1407
570
}
bool DeploymentEnabled<Consensus::BuriedDeployment>(ChainstateManager const&, Consensus::BuriedDeployment)
Line
Count
Source
1405
475
{
1406
475
    return DeploymentEnabled(chainman.GetConsensus(), dep);
1407
475
}
bool DeploymentEnabled<Consensus::DeploymentPos>(ChainstateManager const&, Consensus::DeploymentPos)
Line
Count
Source
1405
95
{
1406
95
    return DeploymentEnabled(chainman.GetConsensus(), dep);
1407
95
}
1408
1409
/** Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) */
1410
bool IsBIP30Repeat(const CBlockIndex& block_index);
1411
1412
/** Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30) */
1413
bool IsBIP30Unspendable(const uint256& block_hash, int block_height);
1414
1415
// Returns the script flags which should be checked for a given block
1416
script_verify_flags GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman);
1417
1418
#endif // BITCOIN_VALIDATION_H