Bitcoin Core  24.99.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 namespace interfaces {
19 class Chain;
20 } // namespace interfaces
21 
22 struct IndexSummary {
23  std::string name;
24  bool synced{false};
26 };
27 
34 {
35 protected:
43  class DB : public CDBWrapper
44  {
45  public:
46  DB(const fs::path& path, size_t n_cache_size,
47  bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
48 
50  bool ReadBestBlock(CBlockLocator& locator) const;
51 
53  void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
54  };
55 
56 private:
64  std::atomic<bool> m_synced{false};
65 
67  std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
68 
69  std::thread m_thread_sync;
71 
73  bool Init();
74 
80  void ThreadSync();
81 
90  bool Commit();
91 
93  bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
94 
95  virtual bool AllowPrune() const = 0;
96 
97 protected:
98  std::unique_ptr<interfaces::Chain> m_chain;
100  const std::string m_name;
101 
102  void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
103 
104  void ChainStateFlushed(const CBlockLocator& locator) override;
105 
107  [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
108 
110  [[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
111 
114  virtual bool CustomCommit(CDBBatch& batch) { return true; }
115 
118  [[nodiscard]] virtual bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) { return true; }
119 
120  virtual DB& GetDB() const = 0;
121 
123  const std::string& GetName() const LIFETIMEBOUND { return m_name; }
124 
126  void SetBestBlockIndex(const CBlockIndex* block);
127 
128 public:
129  BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
131  virtual ~BaseIndex();
132 
138  bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
139 
140  void Interrupt();
141 
144  [[nodiscard]] bool Start();
145 
147  void Stop();
148 
150  IndexSummary GetSummary() const;
151 };
152 
153 #endif // BITCOIN_INDEX_BASE_H
#define LIFETIMEBOUND
Definition: attributes.h:16
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:44
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:71
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:52
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:62
Base class for indices of blockchain data.
Definition: base.h:34
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:408
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:426
bool Init()
Read best block locator and check that data needed to sync has not been pruned.
Definition: base.cpp:85
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:275
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:79
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:114
virtual bool AllowPrune() const =0
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:123
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:64
CThreadInterrupt m_interrupt
Definition: base.h:70
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
Definition: base.cpp:76
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:417
const std::string m_name
Definition: base.h:100
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:321
std::thread m_thread_sync
Definition: base.h:69
bool Commit()
Write the current index state (eg.
Definition: base.cpp:232
bool Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:389
virtual bool CustomInit(const std::optional< interfaces::BlockKey > &block)
Initialize internal state from the database and block index.
Definition: base.h:107
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:118
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:157
virtual DB & GetDB() const =0
Chainstate * m_chainstate
Definition: base.h:99
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRewind.
Definition: base.cpp:251
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:98
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:67
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:110
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:87
Implement this to subscribe to events generated in validation.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:455
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:206
const char * name
Definition: rest.cpp:46
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:121
std::string name
Definition: base.h:23
bool synced
Definition: base.h:24
int best_block_height
Definition: base.h:25
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:82
Hash/height pair to help track and identify blocks.
Definition: chain.h:43
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48