Branch data Line data Source code
1 : : // Copyright (c) 2017-present 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_INDEX_BASE_H
6 : : #define BITCOIN_INDEX_BASE_H
7 : :
8 : : #include <attributes.h>
9 : : #include <dbwrapper.h>
10 : : #include <interfaces/chain.h>
11 : : #include <kernel/cs_main.h>
12 : : #include <sync.h>
13 : : #include <uint256.h>
14 : : #include <util/fs.h>
15 : : #include <util/threadinterrupt.h>
16 : : #include <validationinterface.h>
17 : :
18 : : #include <atomic>
19 : : #include <cstddef>
20 : : #include <memory>
21 : : #include <optional>
22 : : #include <string>
23 : : #include <thread>
24 : :
25 : : class CBlock;
26 : : class CBlockIndex;
27 : : class Chainstate;
28 : :
29 : : struct CBlockLocator;
30 [ + + ]: 6 : struct IndexSummary {
31 : : std::string name;
32 : : bool synced{false};
33 : : int best_block_height{0};
34 : : uint256 best_block_hash;
35 : : };
36 : : namespace interfaces {
37 : : struct BlockRef;
38 : : }
39 : : namespace util {
40 : : template <unsigned int num_params>
41 : : struct ConstevalFormatString;
42 : : }
43 : :
44 : : /**
45 : : * Base class for indices of blockchain data. This implements
46 : : * CValidationInterface and ensures blocks are indexed sequentially according
47 : : * to their position in the active chain.
48 : : *
49 : : * In the presence of multiple chainstates (i.e. if a UTXO snapshot is loaded),
50 : : * only the background "IBD" chainstate will be indexed to avoid building the
51 : : * index out of order. When the background chainstate completes validation, the
52 : : * index will be reinitialized and indexing will continue.
53 : : */
54 : : class BaseIndex : public CValidationInterface
55 : : {
56 : : protected:
57 : : /**
58 : : * The database stores a block locator of the chain the database is synced to
59 : : * so that the index can efficiently determine the point it last stopped at.
60 : : * A locator is used instead of a simple hash of the chain tip because blocks
61 : : * and block index entries may not be flushed to disk until after this database
62 : : * is updated.
63 : : */
64 : 10 : class DB : public CDBWrapper
65 : : {
66 : : public:
67 : : DB(const fs::path& path, size_t n_cache_size,
68 : : bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
69 : :
70 : : /// Read block locator of the chain that the index is in sync with.
71 : : /// Note, the returned locator will be empty if no record exists.
72 : : CBlockLocator ReadBestBlock() const;
73 : :
74 : : /// Write block locator of the chain that the index is in sync with.
75 : : void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
76 : : };
77 : :
78 : : private:
79 : : /// Whether the index has been initialized or not.
80 : : std::atomic<bool> m_init{false};
81 : : /// Whether the index is in sync with the main chain. The flag is flipped
82 : : /// from false to true once, after which point this starts processing
83 : : /// ValidationInterface notifications to stay in sync.
84 : : ///
85 : : /// Note that this will latch to true *immediately* upon startup if
86 : : /// `m_chainstate->m_chain` is empty, which will be the case upon startup
87 : : /// with an empty datadir if, e.g., `-txindex=1` is specified.
88 : : std::atomic<bool> m_synced{false};
89 : :
90 : : /// The last block in the chain that the index is in sync with.
91 : : std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
92 : :
93 : : std::thread m_thread_sync;
94 : : CThreadInterrupt m_interrupt;
95 : :
96 : : /// Write the current index state (eg. chain block locator and subclass-specific items) to disk.
97 : : ///
98 : : /// Recommendations for error handling:
99 : : /// If called on a successor of the previous committed best block in the index, the index can
100 : : /// continue processing without risk of corruption, though the index state will need to catch up
101 : : /// from further behind on reboot. If the new state is not a successor of the previous state (due
102 : : /// to a chain reorganization), the index must halt until Commit succeeds or else it could end up
103 : : /// getting corrupted.
104 : : bool Commit();
105 : :
106 : : /// Loop over disconnected blocks and call CustomRemove.
107 : : bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
108 : :
109 : : bool ProcessBlock(const CBlockIndex* pindex, const CBlock* block_data = nullptr);
110 : :
111 : : virtual bool AllowPrune() const = 0;
112 : :
113 : : template <typename... Args>
114 : : void FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args);
115 : :
116 : : protected:
117 : : std::unique_ptr<interfaces::Chain> m_chain;
118 : : Chainstate* m_chainstate{nullptr};
119 : : const std::string m_name;
120 : : const std::string m_thread_name;
121 : :
122 : : void BlockConnected(const kernel::ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
123 : :
124 : : void ChainStateFlushed(const kernel::ChainstateRole& role, const CBlockLocator& locator) override;
125 : :
126 : : /// Initialize internal state from the database and block index.
127 : 3 : [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockRef>& block) { return true; }
128 : :
129 : : /// Write update index entries for a newly connected block.
130 : 0 : [[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
131 : :
132 : : /// Virtual method called internally by Commit that can be overridden to atomically
133 : : /// commit more index state.
134 : 6 : virtual bool CustomCommit(CDBBatch& batch) { return true; }
135 : :
136 : : /// Rewind index by one block during a chain reorg.
137 : 1 : [[nodiscard]] virtual bool CustomRemove(const interfaces::BlockInfo& block) { return true; }
138 : :
139 : : virtual DB& GetDB() const = 0;
140 : :
141 : : /// Update the internal best block index as well as the prune lock.
142 : : void SetBestBlockIndex(const CBlockIndex* block);
143 : :
144 : : public:
145 : : BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name, std::string thread_name);
146 : : /// Destructor interrupts sync thread if running and blocks until it exits.
147 : : virtual ~BaseIndex();
148 : :
149 : : /// Get the name of the index for display in logs.
150 [ + - + - : 41 : const std::string& GetName() const LIFETIMEBOUND { return m_name; }
- - ]
151 : :
152 : : /// Return custom notification options for index.
153 : 217 : [[nodiscard]] virtual interfaces::Chain::NotifyOptions CustomOptions() { return {}; }
154 : :
155 : : /// Blocks the current thread until the index is caught up to the current
156 : : /// state of the block chain. This only blocks if the index has gotten in
157 : : /// sync once and only needs to process blocks in the ValidationInterface
158 : : /// queue. If the index is catching up from far behind, this method does
159 : : /// not block and immediately returns false.
160 : : bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
161 : :
162 : : void Interrupt();
163 : :
164 : : /// Initializes the sync state and registers the instance to the
165 : : /// validation interface so that it stays in sync with blockchain updates.
166 : : [[nodiscard]] bool Init();
167 : :
168 : : /// Starts the initial sync process on a background thread.
169 : : [[nodiscard]] bool StartBackgroundSync();
170 : :
171 : : /// \anchor index_sync
172 : : /// Sync the index with the block index starting from the current best block.
173 : : /// Intended to be run in its own thread, m_thread_sync, and can be
174 : : /// interrupted with m_interrupt. Once the index gets in sync, the m_synced
175 : : /// flag is set and the BlockConnected ValidationInterface callback takes
176 : : /// over and the sync thread exits.
177 : : void Sync();
178 : :
179 : : /// Stops the instance from staying in sync with blockchain updates.
180 : : void Stop();
181 : :
182 : : /// Get a summary of the index and its state.
183 : : IndexSummary GetSummary() const;
184 : : };
185 : :
186 : : #endif // BITCOIN_INDEX_BASE_H
|