Bitcoin Core  22.99.0
P2P Digital Currency
base.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 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 <threadinterrupt.h>
10 #include <validationinterface.h>
11 
12 class CBlock;
13 class CBlockIndex;
14 class CChainState;
15 
16 struct IndexSummary {
17  std::string name;
18  bool synced{false};
20 };
21 
28 {
29 protected:
37  class DB : public CDBWrapper
38  {
39  public:
40  DB(const fs::path& path, size_t n_cache_size,
41  bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
42 
44  bool ReadBestBlock(CBlockLocator& locator) const;
45 
47  void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
48  };
49 
50 private:
54  std::atomic<bool> m_synced{false};
55 
57  std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
58 
59  std::thread m_thread_sync;
61 
67  void ThreadSync();
68 
77  bool Commit();
78 protected:
80 
81  void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
82 
83  void ChainStateFlushed(const CBlockLocator& locator) override;
84 
85  const CBlockIndex* CurrentIndex() { return m_best_block_index.load(); };
86 
88  [[nodiscard]] virtual bool Init();
89 
91  virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
92 
95  virtual bool CommitInternal(CDBBatch& batch);
96 
99  virtual bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
100 
101  virtual DB& GetDB() const = 0;
102 
104  virtual const char* GetName() const = 0;
105 
106 public:
108  virtual ~BaseIndex();
109 
115  bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
116 
117  void Interrupt();
118 
121  [[nodiscard]] bool Start(CChainState& active_chainstate);
122 
124  void Stop();
125 
127  IndexSummary GetSummary() const;
128 };
129 
130 #endif // BITCOIN_INDEX_BASE_H
CValidationInterface
Implement this to subscribe to events generated in validation.
Definition: validationinterface.h:78
threadinterrupt.h
BaseIndex::ChainStateFlushed
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:273
BaseIndex::m_synced
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:54
dbwrapper.h
IndexSummary::synced
bool synced
Definition: base.h:18
BaseIndex::m_best_block_index
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:57
IndexSummary::name
std::string name
Definition: base.h:17
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:47
BaseIndex::Init
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:55
IndexSummary::best_block_height
int best_block_height
Definition: base.h:19
BaseIndex::GetSummary
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:364
BaseIndex::m_chainstate
CChainState * m_chainstate
Definition: base.h:79
validationinterface.h
BaseIndex::m_thread_sync
std::thread m_thread_sync
Definition: base.h:59
BaseIndex::WriteBlock
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:91
BaseIndex
Base class for indices of blockchain data.
Definition: base.h:27
BaseIndex::DB::WriteBestBlock
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
Definition: base.cpp:44
BaseIndex::CurrentIndex
const CBlockIndex * CurrentIndex()
Definition: base.h:85
BaseIndex::~BaseIndex
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:49
BaseIndex::Stop
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:355
BaseIndex::DB::ReadBestBlock
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
Definition: base.cpp:35
BaseIndex::Rewind
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Definition: base.cpp:211
Interrupt
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:158
BaseIndex::DB::DB
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:31
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:556
CDBWrapper
Definition: dbwrapper.h:176
CBlock
Definition: block.h:62
LOCKS_EXCLUDED
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:116
BaseIndex::CommitInternal
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:204
BaseIndex::BlockConnected
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:231
BaseIndex::DB
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:37
IndexSummary
Definition: base.h:16
BaseIndex::GetDB
virtual DB & GetDB() const =0
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:114
BaseIndex::ThreadSync
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:124
BaseIndex::GetName
virtual const char * GetName() const =0
Get the name of the index for display in logs.
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:137
CThreadInterrupt
Definition: threadinterrupt.h:19
BaseIndex::Start
bool Start(CChainState &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:341
BaseIndex::m_interrupt
CThreadInterrupt m_interrupt
Definition: base.h:60
BaseIndex::Commit
bool Commit()
Write the current index state (eg.
Definition: base.cpp:195