Bitcoin Core 31.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 <attributes.h>
9#include <dbwrapper.h>
10#include <interfaces/chain.h>
11#include <kernel/cs_main.h>
12#include <sync.h>
13#include <uint256.h>
14#include <util/fs.h>
15#include <util/threadinterrupt.h>
16#include <validationinterface.h>
17
18#include <atomic>
19#include <cstddef>
20#include <memory>
21#include <optional>
22#include <string>
23#include <thread>
24
25class CBlock;
26class CBlockIndex;
27class Chainstate;
28
29struct CBlockLocator;
31 std::string name;
32 bool synced{false};
35};
36namespace interfaces {
37struct BlockRef;
38}
39namespace util {
40template <unsigned int num_params>
41struct ConstevalFormatString;
42}
43
55{
56protected:
64 class DB : public CDBWrapper
65 {
66 public:
67 DB(const fs::path& path, size_t n_cache_size,
68 bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false, bool f_bloom = true);
69
73
75 void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
76 };
77
78private:
80 std::atomic<bool> m_init{false};
88 std::atomic<bool> m_synced{false};
89
91 std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
92
95
100 void Commit();
101
103 bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
104
105 bool ProcessBlock(const CBlockIndex* pindex, const CBlock* block_data = nullptr);
106
107 virtual bool AllowPrune() const = 0;
108
109 template <typename... Args>
110 void FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args);
111
112protected:
113 std::unique_ptr<interfaces::Chain> m_chain;
115 const std::string m_name;
116 const std::string m_thread_name;
117
118 void BlockConnected(const kernel::ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
119
120 void ChainStateFlushed(const kernel::ChainstateRole& role, const CBlockLocator& locator) override;
121
123 [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockRef>& block) { return true; }
124
126 [[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
127
130 virtual bool CustomCommit(CDBBatch& batch) { return true; }
131
133 [[nodiscard]] virtual bool CustomRemove(const interfaces::BlockInfo& block) { return true; }
134
135 virtual DB& GetDB() const = 0;
136
138 void SetBestBlockIndex(const CBlockIndex* block);
139
140public:
141 BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name, std::string thread_name);
143 virtual ~BaseIndex();
144
146 const std::string& GetName() const LIFETIMEBOUND { return m_name; }
147
149 [[nodiscard]] virtual interfaces::Chain::NotifyOptions CustomOptions() { return {}; }
150
156 bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
157
158 void Interrupt();
159
162 [[nodiscard]] bool Init();
163
165 [[nodiscard]] bool StartBackgroundSync();
166
173 void Sync();
174
176 void Stop();
177
179 IndexSummary GetSummary() const;
180};
181
182#endif // BITCOIN_INDEX_BASE_H
#define LIFETIMEBOUND
Definition: attributes.h:16
ArgsManager & args
Definition: bitcoind.cpp:280
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:65
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false, bool f_bloom=true)
Definition: base.cpp:68
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:91
CBlockLocator ReadBestBlock() const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:79
Base class for indices of blockchain data.
Definition: base.h:55
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:477
virtual bool CustomInit(const std::optional< interfaces::BlockRef > &block)
Initialize internal state from the database and block index.
Definition: base.h:123
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:503
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:99
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:130
void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:344
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:146
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:464
virtual bool AllowPrune() const =0
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:88
void Commit()
Write the current index state (eg.
Definition: base.cpp:277
CThreadInterrupt m_interrupt
Definition: base.h:94
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:488
const std::string m_name
Definition: base.h:115
virtual DB & GetDB() const =0
void Sync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:208
std::thread m_thread_sync
Definition: base.h:93
virtual interfaces::Chain::NotifyOptions CustomOptions()
Return custom notification options for index.
Definition: base.h:149
bool ProcessBlock(const CBlockIndex *pindex, const CBlock *block_data=nullptr)
Definition: base.cpp:175
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name, std::string thread_name)
Definition: base.cpp:96
void FatalErrorf(util::ConstevalFormatString< sizeof...(Args)> fmt, const Args &... args)
Definition: base.cpp:53
Chainstate * m_chainstate
Definition: base.h:114
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRemove.
Definition: base.cpp:306
virtual bool CustomRemove(const interfaces::BlockInfo &block)
Rewind index by one block during a chain reorg.
Definition: base.h:133
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
Definition: base.cpp:469
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:113
std::atomic< bool > m_init
Whether the index has been initialized or not.
Definition: base.h:80
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:91
const std::string m_thread_name
Definition: base.h:116
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:126
void ChainStateFlushed(const kernel::ChainstateRole &role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:396
Definition: block.h:74
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:88
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:554
Definition: init.h:13
256-bit opaque blob.
Definition: uint256.h:196
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
std::thread thread
Thread variable should be after other struct members so the thread does not start until the other mem...
const char * name
Definition: rest.cpp:50
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:117
std::string name
Definition: base.h:31
bool synced
Definition: base.h:32
uint256 best_block_hash
Definition: base.h:34
int best_block_height
Definition: base.h:33
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:19
Options specifying which chain notifications are required.
Definition: chain.h:319
Information about chainstate that notifications are sent from.
Definition: types.h:18
A wrapper for a compile-time partially validated format string.
Definition: string.h:96
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48