Bitcoin ABC  0.22.13
P2P Digital Currency
base.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 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 
22 protected:
23  class DB : public CDBWrapper {
24  public:
25  DB(const fs::path &path, size_t n_cache_size, bool f_memory = false,
26  bool f_wipe = false, bool f_obfuscate = false);
27 
29  bool ReadBestBlock(CBlockLocator &locator) const;
30 
32  void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator);
33  };
34 
35 private:
39  std::atomic<bool> m_synced{false};
40 
42  std::atomic<const CBlockIndex *> m_best_block_index{nullptr};
43 
44  std::thread m_thread_sync;
46 
52  void ThreadSync();
53 
64  bool Commit();
65 
66 protected:
67  void BlockConnected(const std::shared_ptr<const CBlock> &block,
68  const CBlockIndex *pindex) override;
69 
70  void ChainStateFlushed(const CBlockLocator &locator) override;
71 
73  virtual bool Init();
74 
76  virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) {
77  return true;
78  }
79 
82  virtual bool CommitInternal(CDBBatch &batch);
83 
86  virtual bool Rewind(const CBlockIndex *current_tip,
87  const CBlockIndex *new_tip);
88 
89  virtual DB &GetDB() const = 0;
90 
92  virtual const char *GetName() const = 0;
93 
94 public:
96  virtual ~BaseIndex();
97 
103  bool BlockUntilSyncedToCurrentChain() const;
104 
105  void Interrupt();
106 
109  void Start();
110 
112  void Stop();
113 };
114 
115 #endif // BITCOIN_INDEX_BASE_H
bool Commit()
Write the current index state (eg.
Definition: base.cpp:160
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:54
CThreadInterrupt m_interrupt
Definition: base.h:45
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:239
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:110
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:39
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:48
Definition: block.h:62
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:176
Implement this to subscribe to events generated in validation.
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:321
std::thread m_thread_sync
Definition: base.h:44
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:76
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:49
void Interrupt()
Definition: base.cpp:304
Base class for indices of blockchain data.
Definition: base.h:21
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
Definition: base.cpp:44
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:42
void Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:308
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:169
A helper class for interruptible sleeps.
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:192
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:86
virtual const char * GetName() const =0
Get the name of the index for display in logs.
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:32
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
Definition: base.cpp:36
virtual DB & GetDB() const =0
bool BlockUntilSyncedToCurrentChain() const
Blocks the current thread until the index is caught up to the current state of the block chain...
Definition: base.cpp:280