/ src / node / chainstate.h
chainstate.h
 1  // Copyright (c) 2021-2022 The Bitcoin Core developers
 2  // Distributed under the MIT software license, see the accompanying
 3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 4  
 5  #ifndef BITCOIN_NODE_CHAINSTATE_H
 6  #define BITCOIN_NODE_CHAINSTATE_H
 7  
 8  #include <util/translation.h>
 9  #include <validation.h>
10  
11  #include <cstdint>
12  #include <functional>
13  #include <tuple>
14  
15  class CTxMemPool;
16  
17  namespace node {
18  
19  struct CacheSizes;
20  
21  struct ChainstateLoadOptions {
22      CTxMemPool* mempool{nullptr};
23      bool block_tree_db_in_memory{false};
24      bool coins_db_in_memory{false};
25      bool reindex{false};
26      bool reindex_chainstate{false};
27      bool prune{false};
28      //! Setting require_full_verification to true will require all checks at
29      //! check_level (below) to succeed for loading to succeed. Setting it to
30      //! false will skip checks if cache is not big enough to run them, so may be
31      //! helpful for running with a small cache.
32      bool require_full_verification{true};
33      int64_t check_blocks{DEFAULT_CHECKBLOCKS};
34      int64_t check_level{DEFAULT_CHECKLEVEL};
35      std::function<void()> coins_error_cb;
36  };
37  
38  //! Chainstate load status. Simple applications can just check for the success
39  //! case, and treat other cases as errors. More complex applications may want to
40  //! try reindexing in the generic failure case, and pass an interrupt callback
41  //! and exit cleanly in the interrupted case.
42  enum class ChainstateLoadStatus {
43      SUCCESS,
44      FAILURE, //!< Generic failure which reindexing may fix
45      FAILURE_FATAL, //!< Fatal error which should not prompt to reindex
46      FAILURE_INCOMPATIBLE_DB,
47      FAILURE_INSUFFICIENT_DBCACHE,
48      INTERRUPTED,
49  };
50  
51  //! Chainstate load status code and optional error string.
52  using ChainstateLoadResult = std::tuple<ChainstateLoadStatus, bilingual_str>;
53  
54  /** This sequence can have 4 types of outcomes:
55   *
56   *  1. Success
57   *  2. Shutdown requested
58   *    - nothing failed but a shutdown was triggered in the middle of the
59   *      sequence
60   *  3. Soft failure
61   *    - a failure that might be recovered from with a reindex
62   *  4. Hard failure
63   *    - a failure that definitively cannot be recovered from with a reindex
64   *
65   *  LoadChainstate returns a (status code, error string) tuple.
66   */
67  ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
68                                      const ChainstateLoadOptions& options);
69  ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options);
70  } // namespace node
71  
72  #endif // BITCOIN_NODE_CHAINSTATE_H