Bitcoin ABC 0.31.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <interfaces/chain.h>
10#include <threadinterrupt.h>
11#include <validationinterface.h>
12
13#include <string>
14
15class CBlock;
16class CBlockIndex;
17class Chainstate;
18
20 std::string name;
21 bool synced{false};
24};
25
32protected:
40 class DB : public CDBWrapper {
41 public:
42 DB(const fs::path &path, size_t n_cache_size, bool f_memory = false,
43 bool f_wipe = false, bool f_obfuscate = false);
44
46 bool ReadBestBlock(CBlockLocator &locator) const;
47
49 void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator);
50 };
51
52private:
54 std::atomic<bool> m_init{false};
62 std::atomic<bool> m_synced{false};
63
65 std::atomic<const CBlockIndex *> m_best_block_index{nullptr};
66
67 std::thread m_thread_sync;
69
75 void ThreadSync();
76
87 bool Commit();
88
89 virtual bool AllowPrune() const = 0;
90
91protected:
92 std::unique_ptr<interfaces::Chain> m_chain;
94 const std::string m_name;
95
97 const std::shared_ptr<const CBlock> &block,
98 const CBlockIndex *pindex) override;
99
101 const CBlockLocator &locator) override;
102
104 [[nodiscard]] virtual bool
105 CustomInit(const std::optional<interfaces::BlockKey> &block) {
106 return true;
107 }
108
110 virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) {
111 return true;
112 }
113
116 virtual bool CommitInternal(CDBBatch &batch);
117
120 virtual bool Rewind(const CBlockIndex *current_tip,
121 const CBlockIndex *new_tip);
122
123 virtual DB &GetDB() const = 0;
124
126 const std::string &GetName() const LIFETIMEBOUND { return m_name; }
127
129 void SetBestBlockIndex(const CBlockIndex *block);
130
131public:
132 BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
134 virtual ~BaseIndex();
135
141 bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
142
143 void Interrupt();
144
147 [[nodiscard]] bool Init();
148
150 [[nodiscard]] bool StartBackgroundSync();
151
153 void Stop();
154
156 IndexSummary GetSummary() const;
157};
158
159#endif // BITCOIN_INDEX_BASE_H
#define LIFETIMEBOUND
Definition: attributes.h:16
The database stores a block locator of the chain the database is synced to so that the TxIndex can ef...
Definition: base.h:40
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:62
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:41
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:54
Base class for indices of blockchain data.
Definition: base.h:31
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:387
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:409
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition: base.cpp:75
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:70
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:65
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:223
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:126
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:373
virtual bool AllowPrune() const =0
void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:254
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:62
CThreadInterrupt m_interrupt
Definition: base.h:68
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
Definition: base.cpp:67
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:395
const std::string m_name
Definition: base.h:94
virtual DB & GetDB() const =0
std::thread m_thread_sync
Definition: base.h:67
bool Commit()
Write the current index state (eg.
Definition: base.cpp:214
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:110
virtual bool CustomInit(const std::optional< interfaces::BlockKey > &block)
Initialize internal state from the database and block index.
Definition: base.h:105
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:141
Chainstate * m_chainstate
Definition: base.h:93
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:234
bool StartBackgroundSync()
Starts the initial sync process.
Definition: base.cpp:377
void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:306
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:92
std::atomic< bool > m_init
Whether the index has been initialized or not.
Definition: base.h:54
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:700
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
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:14
const char * name
Definition: rest.cpp:47
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
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:20
bool synced
Definition: base.h:21
BlockHash best_block_hash
Definition: base.h:23
int best_block_height
Definition: base.h:22
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:55