Bitcoin
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 <uint256.h>
13 #include <validationinterface.h>
14 
15 class CBlockIndex;
16 
23 {
24 protected:
25  class DB : public CDBWrapper
26  {
27  public:
28  DB(const fs::path& path, size_t n_cache_size,
29  bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
30 
32  bool ReadBestBlock(CBlockLocator& locator) const;
33 
35  void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
36  };
37 
38 private:
42  std::atomic<bool> m_synced{false};
43 
45  std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
46 
47  std::thread m_thread_sync;
49 
55  void ThreadSync();
56 
65  bool Commit();
66 
67 protected:
68  void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
69  const std::vector<CTransactionRef>& txn_conflicted) override;
70 
71  void ChainStateFlushed(const CBlockLocator& locator) override;
72 
74  virtual bool Init();
75 
77  virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
78 
81  virtual bool CommitInternal(CDBBatch& batch);
82 
85  virtual bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
86 
87  virtual DB& GetDB() const = 0;
88 
90  virtual const char* GetName() const = 0;
91 
92 public:
94  virtual ~BaseIndex();
95 
102 
103  void Interrupt();
104 
107  void Start();
108 
110  void Stop();
111 };
112 
113 #endif // BITCOIN_INDEX_BASE_H
void Interrupt()
Definition: base.cpp:297
void Start()
Definition: base.cpp:302
Definition: block.h:126
void ThreadSync()
Definition: base.cpp:88
bool BlockUntilSyncedToCurrentChain()
Definition: base.cpp:273
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
Definition: base.cpp:44
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Definition: base.cpp:175
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:45
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:49
Definition: base.h:22
Definition: threadinterrupt.h:20
Definition: base.h:25
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex, const std::vector< CTransactionRef > &txn_conflicted) override
Definition: base.cpp:191
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:316
CThreadInterrupt m_interrupt
Definition: base.h:48
std::atomic< bool > m_synced
Definition: base.h:42
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
Definition: base.cpp:35
std::thread m_thread_sync
Definition: base.h:47
Definition: dbwrapper.h:176
Definition: validationinterface.h: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:31
Definition: chain.h:139
virtual bool CommitInternal(CDBBatch &batch)
Definition: base.cpp:168
virtual const char * GetName() const =0
Get the name of the index for display in logs.
Definition: logging.h:42
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:55
Definition: dbwrapper.h:47
Definition: block.h:72
void ChainStateFlushed(const CBlockLocator &locator) override
Definition: base.cpp:234
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:77
virtual DB & GetDB() const =0
bool Commit()
Definition: base.cpp:159