Bitcoin Core  0.20.99
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 <primitives/block.h>
10 #include <primitives/transaction.h>
11 #include <threadinterrupt.h>
12 #include <validationinterface.h>
13 
14 class CBlockIndex;
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 
79 protected:
80  void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
81 
82  void ChainStateFlushed(const CBlockLocator& locator) override;
83 
85  virtual bool Init();
86 
88  virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
89 
92  virtual bool CommitInternal(CDBBatch& batch);
93 
96  virtual bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
97 
98  virtual DB& GetDB() const = 0;
99 
101  virtual const char* GetName() const = 0;
102 
103 public:
105  virtual ~BaseIndex();
106 
112  bool BlockUntilSyncedToCurrentChain() const;
113 
114  void Interrupt();
115 
118  void Start();
119 
121  void Stop();
122 
124  IndexSummary GetSummary() const;
125 };
126 
127 #endif // BITCOIN_INDEX_BASE_H
CThreadInterrupt m_interrupt
Definition: base.h:60
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:114
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:46
Definition: block.h:62
bool synced
Definition: base.h:18
Implement this to subscribe to events generated in validation.
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:37
std::thread m_thread_sync
Definition: base.h:59
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:88
Base class for indices of blockchain data.
Definition: base.h:27
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:162
std::string name
Definition: base.h:17
int best_block_height
Definition: base.h:19
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137