Bitcoin ABC 0.30.7
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 <threadinterrupt.h>
10#include <validationinterface.h>
11
12class CBlock;
13class CBlockIndex;
14class Chainstate;
15
17 std::string name;
18 bool synced{false};
20};
21
28protected:
36 class DB : public CDBWrapper {
37 public:
38 DB(const fs::path &path, size_t n_cache_size, bool f_memory = false,
39 bool f_wipe = false, bool f_obfuscate = false);
40
42 bool ReadBestBlock(CBlockLocator &locator) const;
43
45 void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator);
46 };
47
48private:
52 std::atomic<bool> m_synced{false};
53
55 std::atomic<const CBlockIndex *> m_best_block_index{nullptr};
56
57 std::thread m_thread_sync;
59
65 void ThreadSync();
66
77 bool Commit();
78
79 virtual bool AllowPrune() const = 0;
80
81protected:
83
84 void BlockConnected(const std::shared_ptr<const CBlock> &block,
85 const CBlockIndex *pindex) override;
86
87 void ChainStateFlushed(const CBlockLocator &locator) override;
88
89 const CBlockIndex *CurrentIndex() { return m_best_block_index.load(); };
90
92 [[nodiscard]] virtual bool Init();
93
95 virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) {
96 return true;
97 }
98
101 virtual bool CommitInternal(CDBBatch &batch);
102
105 virtual bool Rewind(const CBlockIndex *current_tip,
106 const CBlockIndex *new_tip);
107
108 virtual DB &GetDB() const = 0;
109
111 virtual const char *GetName() const = 0;
112
114 void SetBestBlockIndex(const CBlockIndex *block);
115
116public:
118 virtual ~BaseIndex();
119
125 bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
126
127 void Interrupt();
128
131 [[nodiscard]] bool Start(Chainstate &active_chainstate);
132
134 void Stop();
135
137 IndexSummary GetSummary() const;
138};
139
140#endif // BITCOIN_INDEX_BASE_H
The database stores a block locator of the chain the database is synced to so that the TxIndex can ef...
Definition: base.h:36
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:58
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:37
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:50
Base class for indices of blockchain data.
Definition: base.h:27
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:392
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:409
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:68
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:257
virtual const char * GetName() const =0
Get the name of the index for display in logs.
bool Start(Chainstate &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:378
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:63
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:55
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:226
bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(void Interrupt()
Blocks the current thread until the index is caught up to the current state of the block chain.
Definition: base.cpp:374
virtual bool AllowPrune() const =0
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:52
CThreadInterrupt m_interrupt
Definition: base.h:58
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:400
virtual DB & GetDB() const =0
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:308
std::thread m_thread_sync
Definition: base.h:57
bool Commit()
Write the current index state (eg.
Definition: base.cpp:217
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:95
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:144
Chainstate * m_chainstate
Definition: base.h:82
const CBlockIndex * CurrentIndex()
Definition: base.h:89
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:237
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:78
A helper class for interruptible sleeps.
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:699
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:105
std::string name
Definition: base.h:17
bool synced
Definition: base.h:18
int best_block_height
Definition: base.h:19
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:55