Bitcoin ABC 0.31.6
P2P Digital Currency
chain.h
Go to the documentation of this file.
1// Copyright (c) 2018-2019 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
10#include <primitives/txid.h>
11#include <util/settings.h> // For util::SettingsValue
12
13#include <cstddef>
14#include <cstdint>
15#include <functional>
16#include <memory>
17#include <optional>
18#include <string>
19#include <vector>
20
21class ArgsManager;
22class CBlock;
23class CChainParams;
24class Coin;
25class Config;
26class CRPCCommand;
27class CScheduler;
29
30enum class MemPoolRemovalReason;
31enum class ChainstateRole;
32
33struct bilingual_str;
34struct CBlockLocator;
35namespace node {
36struct NodeContext;
37} // namespace node
38
39namespace Consensus {
40struct Params;
41}
42
43namespace interfaces {
44
45class Handler;
46class Wallet;
47
49struct BlockKey {
51 int height = -1;
52};
53
56public:
58 m_hash = &hash;
59 return *this;
60 }
63 return *this;
64 }
65 FoundBlock &time(int64_t &time) {
66 m_time = &time;
67 return *this;
68 }
69 FoundBlock &maxTime(int64_t &max_time) {
70 m_max_time = &max_time;
71 return *this;
72 }
73 FoundBlock &mtpTime(int64_t &mtp_time) {
74 m_mtp_time = &mtp_time;
75 return *this;
76 }
78 FoundBlock &inActiveChain(bool &in_active_chain) {
79 m_in_active_chain = &in_active_chain;
80 return *this;
81 }
85 return *this;
86 }
89 FoundBlock &nextBlock(const FoundBlock &next_block) {
90 m_next_block = &next_block;
91 return *this;
92 }
96 m_data = &data;
97 return *this;
98 }
99
100 BlockHash *m_hash = nullptr;
101 int *m_height = nullptr;
102 int64_t *m_time = nullptr;
103 int64_t *m_max_time = nullptr;
104 int64_t *m_mtp_time = nullptr;
105 bool *m_in_active_chain = nullptr;
107 const FoundBlock *m_next_block = nullptr;
108 CBlock *m_data = nullptr;
109};
110
136class Chain {
137public:
138 virtual ~Chain() {}
139
143 virtual std::optional<int> getHeight() = 0;
144
146 virtual BlockHash getBlockHash(int height) = 0;
147
150 virtual bool haveBlockOnDisk(int height) = 0;
151
154
158 virtual std::optional<int>
159 findLocatorFork(const CBlockLocator &locator) = 0;
160
163 virtual bool findBlock(const BlockHash &hash,
164 const FoundBlock &block = {}) = 0;
165
170 virtual bool
171 findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height,
172 const FoundBlock &block = {}) = 0;
173
176 virtual bool findAncestorByHeight(const BlockHash &block_hash,
177 int ancestor_height,
178 const FoundBlock &ancestor_out = {}) = 0;
179
182 virtual bool findAncestorByHash(const BlockHash &block_hash,
183 const BlockHash &ancestor_hash,
184 const FoundBlock &ancestor_out = {}) = 0;
185
188 virtual bool findCommonAncestor(const BlockHash &block_hash1,
189 const BlockHash &block_hash2,
190 const FoundBlock &ancestor_out = {},
191 const FoundBlock &block1_out = {},
192 const FoundBlock &block2_out = {}) = 0;
193
197 virtual void findCoins(std::map<COutPoint, Coin> &coins) = 0;
198
201 virtual double guessVerificationProgress(const BlockHash &block_hash) = 0;
202
206 virtual bool hasBlocks(const BlockHash &block_hash, int min_height = 0,
207 std::optional<int> max_height = {}) = 0;
208
213 virtual bool broadcastTransaction(const Config &config,
214 const CTransactionRef &tx,
215 const Amount &max_tx_fee, bool relay,
216 std::string &err_string) = 0;
217
219 virtual CFeeRate estimateFee() const = 0;
220
222 virtual CFeeRate relayMinFee() = 0;
223
226 virtual CFeeRate relayDustFee() = 0;
227
229 virtual bool havePruned() = 0;
230
232 virtual std::optional<int> getPruneHeight() = 0;
233
235 virtual bool isReadyToBroadcast() = 0;
236
238 virtual bool isInitialBlockDownload() = 0;
239
241 virtual bool shutdownRequested() = 0;
242
244 virtual void initMessage(const std::string &message) = 0;
245
247 virtual void initWarning(const bilingual_str &message) = 0;
248
250 virtual void initError(const bilingual_str &message) = 0;
251
253 virtual void showProgress(const std::string &title, int progress,
254 bool resume_possible) = 0;
255
258 public:
259 virtual ~Notifications() {}
261 uint64_t mempool_sequence) {}
264 uint64_t mempool_sequence) {}
265 virtual void blockConnected(ChainstateRole role, const CBlock &block,
266 int height) {}
267 virtual void blockDisconnected(const CBlock &block, int height) {}
268 virtual void updatedBlockTip() {}
270 const CBlockLocator &locator) {}
271 };
272
274 virtual std::unique_ptr<Handler>
275 handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
276
279 virtual void waitForNotificationsIfTipChanged(const BlockHash &old_tip) = 0;
280
283 virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand &command) = 0;
284
286 virtual bool rpcEnableDeprecated(const std::string &method) = 0;
287
290 virtual void rpcRunLater(const std::string &name, std::function<void()> fn,
291 int64_t seconds) = 0;
292
294 virtual int rpcSerializationFlags() = 0;
295
297 virtual util::SettingsValue getSetting(const std::string &arg) = 0;
298
300 virtual std::vector<util::SettingsValue>
301 getSettingsList(const std::string &arg) = 0;
302
304 virtual util::SettingsValue getRwSetting(const std::string &name) = 0;
305
308 virtual bool updateRwSetting(const std::string &name,
309 const util::SettingsValue &value,
310 bool write = true) = 0;
311
320 virtual void requestMempoolTransactions(Notifications &notifications) = 0;
321
323 virtual bool hasAssumedValidChain() = 0;
324
327 virtual node::NodeContext *context() { return nullptr; }
328
330 virtual const CChainParams &params() const = 0;
331};
332
336public:
337 virtual ~ChainClient() {}
338
340 virtual void registerRpcs() = 0;
341
343 virtual bool verify() = 0;
344
346 virtual bool load() = 0;
347
349 virtual void start(CScheduler &scheduler) = 0;
350
352 virtual void flush() = 0;
353
355 virtual void stop() = 0;
356
358 virtual void setMockTime(int64_t time) = 0;
359};
360
362std::unique_ptr<Chain> MakeChain(node::NodeContext &node,
363 const CChainParams &params);
364
365} // namespace interfaces
366
367#endif // BITCOIN_INTERFACES_CHAIN_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
Definition: block.h:60
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:41
A UTXO entry.
Definition: coins.h:28
Definition: config.h:19
Chain notifications.
Definition: chain.h:257
virtual void chainStateFlushed(ChainstateRole role, const CBlockLocator &locator)
Definition: chain.h:269
virtual void transactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Definition: chain.h:260
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:267
virtual void updatedBlockTip()
Definition: chain.h:268
virtual void transactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Definition: chain.h:262
virtual void blockConnected(ChainstateRole role, const CBlock &block, int height)
Definition: chain.h:265
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:335
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:337
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:136
virtual void rpcRunLater(const std::string &name, std::function< void()> fn, int64_t seconds)=0
Run function after given number of seconds.
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 findAncestorByHash(const BlockHash &block_hash, const BlockHash &ancestor_hash, const FoundBlock &ancestor_out={})=0
Return whether block descends from a specified ancestor, and optionally return ancestor information.
virtual bool isInitialBlockDownload()=0
Check if in IBD.
virtual std::optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
virtual BlockHash getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
virtual bool rpcEnableDeprecated(const std::string &method)=0
Check if deprecated RPC is enabled.
virtual int rpcSerializationFlags()=0
Current RPC serialization flags.
virtual bool findBlock(const BlockHash &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
virtual std::unique_ptr< Handler > handleNotifications(std::shared_ptr< Notifications > notifications)=0
Register handler for notifications.
virtual bool findCommonAncestor(const BlockHash &block_hash1, const BlockHash &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 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 bool broadcastTransaction(const Config &config, const CTransactionRef &tx, const Amount &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 util::SettingsValue getRwSetting(const std::string &name)=0
Return <datadir>/settings.json setting value.
virtual node::NodeContext * context()
Get internal node context.
Definition: chain.h:327
virtual double guessVerificationProgress(const BlockHash &block_hash)=0
Estimate fraction of total transactions verified if blocks up to the specified block hash are verifie...
virtual const CChainParams & params() const =0
This Chain's parameters.
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 bool isReadyToBroadcast()=0
Check if the node is ready to broadcast transactions.
virtual bool hasAssumedValidChain()=0
Return true if an assumed-valid chain is in use.
virtual bool hasBlocks(const BlockHash &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 util::SettingsValue getSetting(const std::string &arg)=0
Get settings value.
virtual bool findAncestorByHeight(const BlockHash &block_hash, int ancestor_height, const FoundBlock &ancestor_out={})=0
Find ancestor of block at specified height and optionally return ancestor information.
virtual void initMessage(const std::string &message)=0
Send init message.
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:138
virtual CFeeRate relayDustFee()=0
Relay dust fee setting (-dustrelayfee), reflecting lowest rate it's economical to spend.
virtual CFeeRate estimateFee() const =0
Estimate fee.
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 void waitForNotificationsIfTipChanged(const BlockHash &old_tip)=0
Wait for pending notifications to be processed unless block hash points to the current chain tip.
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 relayMinFee()=0
Relay current minimum fee (from -minrelaytxfee settings).
virtual std::optional< int > getPruneHeight()=0
Get the current prune height.
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:55
FoundBlock & time(int64_t &time)
Definition: chain.h:65
const FoundBlock * m_next_block
Definition: chain.h:107
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:73
BlockHash * m_hash
Definition: chain.h:100
int64_t * m_max_time
Definition: chain.h:103
FoundBlock & locator(CBlockLocator &locator)
Return locator if block is in the active chain.
Definition: chain.h:83
int64_t * m_time
Definition: chain.h:102
bool * m_in_active_chain
Definition: chain.h:105
FoundBlock & nextBlock(const FoundBlock &next_block)
Return next block in the active chain if current block is in the active chain.
Definition: chain.h:89
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:69
int64_t * m_mtp_time
Definition: chain.h:104
FoundBlock & height(int &height)
Definition: chain.h:61
FoundBlock & hash(BlockHash &hash)
Definition: chain.h:57
FoundBlock & inActiveChain(bool &in_active_chain)
Return whether block is in the active (most-work) chain.
Definition: chain.h:78
FoundBlock & data(CBlock &data)
Read block data from disk.
Definition: chain.h:95
CBlockLocator * m_locator
Definition: chain.h:106
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:14
std::unique_ptr< Chain > MakeChain(node::NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: interfaces.cpp:825
Definition: init.h:31
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
const char * name
Definition: rest.cpp:47
Definition: amount.h:19
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:109
Bilingual messages:
Definition: translation.h:17
Hash/height pair to help track and identify blocks.
Definition: chain.h:49
BlockHash hash
Definition: chain.h:50
NodeContext struct containing references to chain state and connection state.
Definition: context.h:46
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:153