Bitcoin Core 22.99.0
P2P Digital Currency
chain.h
Go to the documentation of this file.
1// Copyright (c) 2018-2020 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_INTERFACES_CHAIN_H
6#define BITCOIN_INTERFACES_CHAIN_H
7
8#include <primitives/transaction.h> // For CTransactionRef
9#include <util/settings.h> // For util::SettingsValue
10
11#include <functional>
12#include <memory>
13#include <optional>
14#include <stddef.h>
15#include <stdint.h>
16#include <string>
17#include <vector>
18
19class ArgsManager;
20class CBlock;
21class CFeeRate;
22class CRPCCommand;
23class CScheduler;
24class Coin;
25class uint256;
26enum class MemPoolRemovalReason;
27enum class RBFTransactionState;
28struct bilingual_str;
29struct CBlockLocator;
30struct FeeCalculation;
31struct NodeContext;
32
33namespace interfaces {
34
35class Handler;
36class Wallet;
37
42{
43public:
44 FoundBlock& hash(uint256& hash) { m_hash = &hash; return *this; }
45 FoundBlock& height(int& height) { m_height = &height; return *this; }
46 FoundBlock& time(int64_t& time) { m_time = &time; return *this; }
47 FoundBlock& maxTime(int64_t& max_time) { m_max_time = &max_time; return *this; }
48 FoundBlock& mtpTime(int64_t& mtp_time) { m_mtp_time = &mtp_time; return *this; }
50 FoundBlock& inActiveChain(bool& in_active_chain) { m_in_active_chain = &in_active_chain; return *this; }
52 FoundBlock& nextBlock(const FoundBlock& next_block) { m_next_block = &next_block; return *this; }
55 FoundBlock& data(CBlock& data) { m_data = &data; return *this; }
56
57 uint256* m_hash = nullptr;
58 int* m_height = nullptr;
59 int64_t* m_time = nullptr;
60 int64_t* m_max_time = nullptr;
61 int64_t* m_mtp_time = nullptr;
62 bool* m_in_active_chain = nullptr;
63 const FoundBlock* m_next_block = nullptr;
64 CBlock* m_data = nullptr;
65 mutable bool found = false;
66};
67
92class Chain
93{
94public:
95 virtual ~Chain() {}
96
100 virtual std::optional<int> getHeight() = 0;
101
103 virtual uint256 getBlockHash(int height) = 0;
104
107 virtual bool haveBlockOnDisk(int height) = 0;
108
111
115 virtual std::optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
116
118 virtual bool checkFinalTx(const CTransaction& tx) = 0;
119
122 virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
123
128 virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block={}) = 0;
129
132 virtual bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out={}) = 0;
133
136 virtual bool findAncestorByHash(const uint256& block_hash,
137 const uint256& ancestor_hash,
138 const FoundBlock& ancestor_out={}) = 0;
139
142 virtual bool findCommonAncestor(const uint256& block_hash1,
143 const uint256& block_hash2,
144 const FoundBlock& ancestor_out={},
145 const FoundBlock& block1_out={},
146 const FoundBlock& block2_out={}) = 0;
147
151 virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
152
155 virtual double guessVerificationProgress(const uint256& block_hash) = 0;
156
160 virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, std::optional<int> max_height = {}) = 0;
161
164
166 virtual bool isInMempool(const uint256& txid) = 0;
167
169 virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
170
174 virtual bool broadcastTransaction(const CTransactionRef& tx,
175 const CAmount& max_tx_fee,
176 bool relay,
177 std::string& err_string) = 0;
178
180 virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) = 0;
181
185 virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
186
188 virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
189
191 virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
192
194 virtual unsigned int estimateMaxBlocks() = 0;
195
197 virtual CFeeRate mempoolMinFee() = 0;
198
200 virtual CFeeRate relayMinFee() = 0;
201
204
206 virtual CFeeRate relayDustFee() = 0;
207
209 virtual bool havePruned() = 0;
210
212 virtual bool isReadyToBroadcast() = 0;
213
215 virtual bool isInitialBlockDownload() = 0;
216
218 virtual bool shutdownRequested() = 0;
219
221 virtual int64_t getAdjustedTime() = 0;
222
224 virtual void initMessage(const std::string& message) = 0;
225
227 virtual void initWarning(const bilingual_str& message) = 0;
228
230 virtual void initError(const bilingual_str& message) = 0;
231
233 virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
234
237 {
238 public:
239 virtual ~Notifications() {}
240 virtual void transactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
241 virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
242 virtual void blockConnected(const CBlock& block, int height) {}
243 virtual void blockDisconnected(const CBlock& block, int height) {}
244 virtual void updatedBlockTip() {}
245 virtual void chainStateFlushed(const CBlockLocator& locator) {}
246 };
247
249 virtual std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
250
253 virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
254
257 virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
258
260 virtual bool rpcEnableDeprecated(const std::string& method) = 0;
261
263 virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
264
266 virtual int rpcSerializationFlags() = 0;
267
269 virtual util::SettingsValue getSetting(const std::string& arg) = 0;
270
272 virtual std::vector<util::SettingsValue> getSettingsList(const std::string& arg) = 0;
273
275 virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
276
279 virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write=true) = 0;
280
289 virtual void requestMempoolTransactions(Notifications& notifications) = 0;
290
292 virtual bool isTaprootActive() = 0;
293};
294
298{
299public:
300 virtual ~ChainClient() {}
301
303 virtual void registerRpcs() = 0;
304
306 virtual bool verify() = 0;
307
309 virtual bool load() = 0;
310
312 virtual void start(CScheduler& scheduler) = 0;
313
315 virtual void flush() = 0;
316
318 virtual void stop() = 0;
319
321 virtual void setMockTime(int64_t time) = 0;
322};
323
325std::unique_ptr<Chain> MakeChain(NodeContext& node);
326
327} // namespace interfaces
328
329#endif // BITCOIN_INTERFACES_CHAIN_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Definition: block.h:63
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:34
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
A UTXO entry.
Definition: coins.h:31
Chain notifications.
Definition: chain.h:237
virtual void transactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Definition: chain.h:241
virtual void transactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Definition: chain.h:240
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:245
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:243
virtual void updatedBlockTip()
Definition: chain.h:244
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:242
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:298
virtual void stop()=0
Shut down client.
virtual void flush()=0
Save state to disk.
virtual bool load()=0
Load saved state.
virtual void registerRpcs()=0
Register rpcs.
virtual void start(CScheduler &scheduler)=0
Start client execution and provide a scheduler.
virtual bool verify()=0
Check for errors before loading.
virtual ~ChainClient()
Definition: chain.h:300
virtual void setMockTime(int64_t time)=0
Set mock time.
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:93
virtual void rpcRunLater(const std::string &name, std::function< void()> fn, int64_t seconds)=0
Run function after given number of seconds. Cancel any previous calls with same name.
virtual std::unique_ptr< Handler > handleRpc(const CRPCCommand &command)=0
Register handler for RPC.
virtual CBlockLocator getTipLocator()=0
Get locator for the current chain tip.
virtual bool isInitialBlockDownload()=0
Check if in IBD.
virtual bool hasDescendantsInMempool(const uint256 &txid)=0
Check if transaction has descendants in mempool.
virtual std::optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
virtual bool rpcEnableDeprecated(const std::string &method)=0
Check if deprecated RPC is enabled.
virtual int rpcSerializationFlags()=0
Current RPC serialization flags.
virtual bool hasBlocks(const uint256 &block_hash, int min_height=0, std::optional< int > max_height={})=0
Return true if data is available for all blocks in the specified range of blocks.
virtual std::unique_ptr< Handler > handleNotifications(std::shared_ptr< Notifications > notifications)=0
Register handler for notifications.
virtual RBFTransactionState isRBFOptIn(const CTransaction &tx)=0
Check if transaction is RBF opt in.
virtual bool findCommonAncestor(const uint256 &block_hash1, const uint256 &block_hash2, const FoundBlock &ancestor_out={}, const FoundBlock &block1_out={}, const FoundBlock &block2_out={})=0
Find most recent common ancestor between two blocks and optionally return block information.
virtual bool updateRwSetting(const std::string &name, const util::SettingsValue &value, bool write=true)=0
Write a setting to <datadir>/settings.json.
virtual uint256 getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock &block={})=0
Find first block in the chain with timestamp >= the given time and height >= than the given height,...
virtual util::SettingsValue getRwSetting(const std::string &name)=0
Return <datadir>/settings.json setting value.
virtual bool findAncestorByHash(const uint256 &block_hash, const uint256 &ancestor_hash, const FoundBlock &ancestor_out={})=0
Return whether block descends from a specified ancestor, and optionally return ancestor information.
virtual void showProgress(const std::string &title, int progress, bool resume_possible)=0
Send progress indicator.
virtual bool havePruned()=0
Check if any block has been pruned.
virtual void findCoins(std::map< COutPoint, Coin > &coins)=0
Look up unspent output information.
virtual bool shutdownRequested()=0
Check if shutdown requested.
virtual void getTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, CAmount *ancestorfees=nullptr)=0
Calculate mempool ancestor and descendant counts for the given transaction.
virtual bool isReadyToBroadcast()=0
Check if the node is ready to broadcast transactions.
virtual int64_t getAdjustedTime()=0
Get adjusted time.
virtual bool findAncestorByHeight(const uint256 &block_hash, int ancestor_height, const FoundBlock &ancestor_out={})=0
Find ancestor of block at specified height and optionally return ancestor information.
virtual util::SettingsValue getSetting(const std::string &arg)=0
Get settings value.
virtual bool findBlock(const uint256 &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
virtual bool isTaprootActive()=0
Check if Taproot has activated.
virtual double guessVerificationProgress(const uint256 &block_hash)=0
Estimate fraction of total transactions verified if blocks up to the specified block hash are verifie...
virtual void waitForNotificationsIfTipChanged(const uint256 &old_tip)=0
Wait for pending notifications to be processed unless block hash points to the current chain tip.
virtual CFeeRate mempoolMinFee()=0
Mempool minimum fee.
virtual void initMessage(const std::string &message)=0
Send init message.
virtual bool isInMempool(const uint256 &txid)=0
Check if transaction is in mempool.
virtual unsigned int estimateMaxBlocks()=0
Fee estimator max target.
virtual bool broadcastTransaction(const CTransactionRef &tx, const CAmount &max_tx_fee, bool relay, std::string &err_string)=0
Transaction is added to memory pool, if the transaction fee is below the amount specified by max_tx_f...
virtual void getPackageLimits(unsigned int &limit_ancestor_count, unsigned int &limit_descendant_count)=0
Get the node's package limits.
virtual bool checkChainLimits(const CTransactionRef &tx)=0
Check if transaction will pass the mempool's chain limits.
virtual std::optional< int > findLocatorFork(const CBlockLocator &locator)=0
Return height of the highest block on chain in common with the locator, which will either be the orig...
virtual bool haveBlockOnDisk(int height)=0
Check that the block is available on disk (i.e.
virtual ~Chain()
Definition: chain.h:95
virtual CFeeRate relayDustFee()=0
Relay dust fee setting (-dustrelayfee), reflecting lowest rate it's economical to spend.
virtual void requestMempoolTransactions(Notifications &notifications)=0
Synchronously send transactionAddedToMempool notifications about all current mempool transactions to ...
virtual void initError(const bilingual_str &message)=0
Send init error.
virtual bool checkFinalTx(const CTransaction &tx)=0
Check if transaction will be final given chain height current time.
virtual std::vector< util::SettingsValue > getSettingsList(const std::string &arg)=0
Get list of settings values.
virtual void initWarning(const bilingual_str &message)=0
Send init warning.
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation *calc=nullptr)=0
Estimate smart fee.
virtual CFeeRate relayMinFee()=0
Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
virtual CFeeRate relayIncrementalFee()=0
Relay incremental fee setting (-incrementalrelayfee), reflecting cost of relay.
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:42
FoundBlock & time(int64_t &time)
Definition: chain.h:46
FoundBlock & hash(uint256 &hash)
Definition: chain.h:44
CBlock * m_data
Definition: chain.h:64
const FoundBlock * m_next_block
Definition: chain.h:63
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:48
int64_t * m_max_time
Definition: chain.h:60
int64_t * m_time
Definition: chain.h:59
bool * m_in_active_chain
Definition: chain.h:62
FoundBlock & nextBlock(const FoundBlock &next_block)
Return next block in the active chain if current block is in the active chain.
Definition: chain.h:52
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:47
uint256 * m_hash
Definition: chain.h:57
int64_t * m_mtp_time
Definition: chain.h:61
FoundBlock & height(int &height)
Definition: chain.h:45
FoundBlock & inActiveChain(bool &in_active_chain)
Return whether block is in the active (most-work) chain.
Definition: chain.h:50
FoundBlock & data(CBlock &data)
Read block data from disk.
Definition: chain.h:55
256-bit opaque blob.
Definition: uint256.h:124
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: interfaces.cpp:714
RBFTransactionState
The rbf state of unconfirmed transactions.
Definition: rbf.h:20
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
const char * name
Definition: rest.cpp:43
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:115
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
Bilingual messages:
Definition: translation.h:16
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:341