Bitcoin Core  28.1.0
P2P Digital Currency
base.h
Go to the documentation of this file.
1 // Copyright (c) 2017-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_INDEX_BASE_H
6 #define BITCOIN_INDEX_BASE_H
7 
8 #include <dbwrapper.h>
9 #include <interfaces/chain.h>
10 #include <util/threadinterrupt.h>
11 #include <validationinterface.h>
12 
13 #include <string>
14 
15 class CBlock;
16 class CBlockIndex;
17 class Chainstate;
18 class ChainstateManager;
19 namespace interfaces {
20 class Chain;
21 } // namespace interfaces
22 
23 struct IndexSummary {
24  std::string name;
25  bool synced{false};
28 };
29 
41 {
42 protected:
50  class DB : public CDBWrapper
51  {
52  public:
53  DB(const fs::path& path, size_t n_cache_size,
54  bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
55 
57  bool ReadBestBlock(CBlockLocator& locator) const;
58 
60  void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
61  };
62 
63 private:
65  std::atomic<bool> m_init{false};
73  std::atomic<bool> m_synced{false};
74 
76  std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
77 
78  std::thread m_thread_sync;
80 
89  bool Commit();
90 
92  bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
93 
94  virtual bool AllowPrune() const = 0;
95 
96  template <typename... Args>
97  void FatalErrorf(const char* fmt, const Args&... args);
98 
99 protected:
100  std::unique_ptr<interfaces::Chain> m_chain;
102  const std::string m_name;
103 
104  void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
105 
106  void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override;
107 
109  [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
110 
112  [[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
113 
116  virtual bool CustomCommit(CDBBatch& batch) { return true; }
117 
120  [[nodiscard]] virtual bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) { return true; }
121 
122  virtual DB& GetDB() const = 0;
123 
125  void SetBestBlockIndex(const CBlockIndex* block);
126 
127 public:
128  BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
130  virtual ~BaseIndex();
131 
133  const std::string& GetName() const LIFETIMEBOUND { return m_name; }
134 
140  bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
141 
142  void Interrupt();
143 
146  [[nodiscard]] bool Init();
147 
149  [[nodiscard]] bool StartBackgroundSync();
150 
156  void Sync();
157 
159  void Stop();
160 
162  IndexSummary GetSummary() const;
163 };
164 
165 #endif // BITCOIN_INDEX_BASE_H
bool Commit()
Write the current index state (eg.
Definition: base.cpp:226
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition: base.cpp:78
CThreadInterrupt m_interrupt
Definition: base.h:79
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
Definition: base.cpp:69
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:123
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:73
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:72
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:112
Definition: block.h:68
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:870
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:435
Interrupt(node)
const std::string m_name
Definition: base.h:102
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRewind.
Definition: base.cpp:246
bool synced
Definition: base.h:25
Implement this to subscribe to events generated in validation and mempool.
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:409
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:50
std::thread m_thread_sync
Definition: base.h:78
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:84
void FatalErrorf(const char *fmt, const Args &... args)
Definition: base.cpp:30
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:72
Base class for indices of blockchain data.
Definition: base.h:40
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:64
virtual bool CustomInit(const std::optional< interfaces::BlockKey > &block)
Initialize internal state from the database and block index.
Definition: base.h:109
void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:327
ArgsManager & args
Definition: bitcoind.cpp:270
#define LIFETIMEBOUND
Definition: attributes.h:16
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:512
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:25
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:133
const char * name
Definition: rest.cpp:49
std::atomic< bool > m_init
Whether the index has been initialized or not.
Definition: base.h:65
void Sync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:143
A helper class for interruptible sleeps.
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
Definition: base.cpp:401
256-bit opaque blob.
Definition: uint256.h:178
virtual bool CustomRewind(const interfaces::BlockKey &current_tip, const interfaces::BlockKey &new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Definition: base.h:120
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:116
std::string name
Definition: base.h:24
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
int best_block_height
Definition: base.h:26
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
Hash/height pair to help track and identify blocks.
Definition: chain.h:45
Chainstate * m_chainstate
Definition: base.h:101
void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:270
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false)
Definition: base.cpp:45
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:55
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
virtual DB & GetDB() const =0
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:76
uint256 best_block_hash
Definition: base.h:27
virtual bool AllowPrune() const =0
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:100
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:420