Bitcoin Core 28.99.0
P2P Digital Currency
base.h
Go to the documentation of this file.
1// Copyright (c) 2017-present 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 <interfaces/types.h>
11#include <util/string.h>
13#include <validationinterface.h>
14
15#include <string>
16
17class CBlock;
18class CBlockIndex;
19class Chainstate;
21namespace interfaces {
22class Chain;
23} // namespace interfaces
24
26 std::string name;
27 bool synced{false};
30};
31
43{
44protected:
52 class DB : public CDBWrapper
53 {
54 public:
55 DB(const fs::path& path, size_t n_cache_size,
56 bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
57
59 bool ReadBestBlock(CBlockLocator& locator) const;
60
62 void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
63 };
64
65private:
67 std::atomic<bool> m_init{false};
75 std::atomic<bool> m_synced{false};
76
78 std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
79
80 std::thread m_thread_sync;
82
91 bool Commit();
92
94 bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
95
96 virtual bool AllowPrune() const = 0;
97
98 template <typename... Args>
99 void FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args);
100
101protected:
102 std::unique_ptr<interfaces::Chain> m_chain;
104 const std::string m_name;
105
106 void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
107
108 void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override;
109
111 [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockRef>& block) { return true; }
112
114 [[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
115
118 virtual bool CustomCommit(CDBBatch& batch) { return true; }
119
122 [[nodiscard]] virtual bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) { return true; }
123
124 virtual DB& GetDB() const = 0;
125
127 void SetBestBlockIndex(const CBlockIndex* block);
128
129public:
130 BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
132 virtual ~BaseIndex();
133
135 const std::string& GetName() const LIFETIMEBOUND { return m_name; }
136
142 bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
143
144 void Interrupt();
145
148 [[nodiscard]] bool Init();
149
151 [[nodiscard]] bool StartBackgroundSync();
152
158 void Sync();
159
161 void Stop();
162
164 IndexSummary GetSummary() const;
165};
166
167#endif // BITCOIN_INDEX_BASE_H
#define LIFETIMEBOUND
Definition: attributes.h:16
ArgsManager & args
Definition: bitcoind.cpp:277
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:53
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:65
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:46
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:56
Base class for indices of blockchain data.
Definition: base.h:43
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:410
virtual bool CustomInit(const std::optional< interfaces::BlockRef > &block)
Initialize internal state from the database and block index.
Definition: base.h:111
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:436
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition: base.cpp:79
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:73
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:118
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:135
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:397
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:271
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:75
CThreadInterrupt m_interrupt
Definition: base.h:81
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
Definition: base.cpp:70
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:421
const std::string m_name
Definition: base.h:104
virtual DB & GetDB() const =0
void Sync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:144
std::thread m_thread_sync
Definition: base.h:80
bool Commit()
Write the current index state (eg.
Definition: base.cpp:227
virtual bool CustomRewind(const interfaces::BlockRef &current_tip, const interfaces::BlockRef &new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Definition: base.h:122
void FatalErrorf(util::ConstevalFormatString< sizeof...(Args)> fmt, const Args &... args)
Definition: base.cpp:31
Chainstate * m_chainstate
Definition: base.h:103
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRewind.
Definition: base.cpp:247
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
Definition: base.cpp:402
void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:328
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:102
std::atomic< bool > m_init
Whether the index has been initialized or not.
Definition: base.h:67
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:78
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:114
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:141
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:74
A helper class for interruptible sleeps.
Implement this to subscribe to events generated in validation and mempool.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:505
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:866
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
256-bit opaque blob.
Definition: uint256.h:190
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:25
const char * name
Definition: rest.cpp:49
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:124
std::string name
Definition: base.h:26
bool synced
Definition: base.h:27
uint256 best_block_hash
Definition: base.h:29
int best_block_height
Definition: base.h:28
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:78
Hash/height pair to help track and identify blocks.
Definition: types.h:13
A wrapper for a compile-time partially validated format string.
Definition: string.h:92
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48