Bitcoin ABC  0.29.4
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 
9 #include <primitives/txid.h>
10 #include <util/settings.h> // For util::SettingsValue
11 
12 #include <cstddef>
13 #include <cstdint>
14 #include <functional>
15 #include <memory>
16 #include <optional>
17 #include <string>
18 #include <vector>
19 
20 class ArgsManager;
21 class CBlock;
22 class CChainParams;
23 class Coin;
24 class Config;
25 class CRPCCommand;
26 class CScheduler;
27 class TxValidationState;
28 
29 enum class MemPoolRemovalReason;
30 
31 struct BlockHash;
32 struct bilingual_str;
33 struct CBlockLocator;
34 namespace node {
35 struct NodeContext;
36 } // namespace node
37 
38 namespace Consensus {
39 struct Params;
40 }
41 
42 namespace interfaces {
43 
44 class Handler;
45 class Wallet;
46 
48 class FoundBlock {
49 public:
51  m_hash = &hash;
52  return *this;
53  }
55  m_height = &height;
56  return *this;
57  }
58  FoundBlock &time(int64_t &time) {
59  m_time = &time;
60  return *this;
61  }
62  FoundBlock &maxTime(int64_t &max_time) {
63  m_max_time = &max_time;
64  return *this;
65  }
66  FoundBlock &mtpTime(int64_t &mtp_time) {
67  m_mtp_time = &mtp_time;
68  return *this;
69  }
71  FoundBlock &inActiveChain(bool &in_active_chain) {
72  m_in_active_chain = &in_active_chain;
73  return *this;
74  }
77  FoundBlock &nextBlock(const FoundBlock &next_block) {
78  m_next_block = &next_block;
79  return *this;
80  }
84  m_data = &data;
85  return *this;
86  }
87 
88  BlockHash *m_hash = nullptr;
89  int *m_height = nullptr;
90  int64_t *m_time = nullptr;
91  int64_t *m_max_time = nullptr;
92  int64_t *m_mtp_time = nullptr;
93  bool *m_in_active_chain = nullptr;
94  const FoundBlock *m_next_block = nullptr;
95  CBlock *m_data = nullptr;
96 };
97 
123 class Chain {
124 public:
125  virtual ~Chain() {}
126 
130  virtual std::optional<int> getHeight() = 0;
131 
133  virtual BlockHash getBlockHash(int height) = 0;
134 
137  virtual bool haveBlockOnDisk(int height) = 0;
138 
141 
145  virtual std::optional<int>
146  findLocatorFork(const CBlockLocator &locator) = 0;
147 
150  virtual bool findBlock(const BlockHash &hash,
151  const FoundBlock &block = {}) = 0;
152 
157  virtual bool
158  findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height,
159  const FoundBlock &block = {}) = 0;
160 
163  virtual bool findAncestorByHeight(const BlockHash &block_hash,
164  int ancestor_height,
165  const FoundBlock &ancestor_out = {}) = 0;
166 
169  virtual bool findAncestorByHash(const BlockHash &block_hash,
170  const BlockHash &ancestor_hash,
171  const FoundBlock &ancestor_out = {}) = 0;
172 
175  virtual bool findCommonAncestor(const BlockHash &block_hash1,
176  const BlockHash &block_hash2,
177  const FoundBlock &ancestor_out = {},
178  const FoundBlock &block1_out = {},
179  const FoundBlock &block2_out = {}) = 0;
180 
184  virtual void findCoins(std::map<COutPoint, Coin> &coins) = 0;
185 
188  virtual double guessVerificationProgress(const BlockHash &block_hash) = 0;
189 
193  virtual bool hasBlocks(const BlockHash &block_hash, int min_height = 0,
194  std::optional<int> max_height = {}) = 0;
195 
200  virtual bool broadcastTransaction(const Config &config,
201  const CTransactionRef &tx,
202  const Amount &max_tx_fee, bool relay,
203  std::string &err_string) = 0;
204 
206  virtual CFeeRate estimateFee() const = 0;
207 
209  virtual CFeeRate relayMinFee() = 0;
210 
213  virtual CFeeRate relayDustFee() = 0;
214 
216  virtual bool havePruned() = 0;
217 
219  virtual bool isReadyToBroadcast() = 0;
220 
222  virtual bool isInitialBlockDownload() = 0;
223 
225  virtual bool shutdownRequested() = 0;
226 
228  virtual void initMessage(const std::string &message) = 0;
229 
231  virtual void initWarning(const bilingual_str &message) = 0;
232 
234  virtual void initError(const bilingual_str &message) = 0;
235 
237  virtual void showProgress(const std::string &title, int progress,
238  bool resume_possible) = 0;
239 
242  public:
243  virtual ~Notifications() {}
245  uint64_t mempool_sequence) {}
247  MemPoolRemovalReason reason,
248  uint64_t mempool_sequence) {}
249  virtual void blockConnected(const CBlock &block, int height) {}
250  virtual void blockDisconnected(const CBlock &block, int height) {}
251  virtual void updatedBlockTip() {}
252  virtual void chainStateFlushed(const CBlockLocator &locator) {}
253  };
254 
256  virtual std::unique_ptr<Handler>
257  handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
258 
261  virtual void waitForNotificationsIfTipChanged(const BlockHash &old_tip) = 0;
262 
265  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand &command) = 0;
266 
268  virtual bool rpcEnableDeprecated(const std::string &method) = 0;
269 
272  virtual void rpcRunLater(const std::string &name, std::function<void()> fn,
273  int64_t seconds) = 0;
274 
276  virtual int rpcSerializationFlags() = 0;
277 
279  virtual util::SettingsValue getSetting(const std::string &arg) = 0;
280 
282  virtual std::vector<util::SettingsValue>
283  getSettingsList(const std::string &arg) = 0;
284 
286  virtual util::SettingsValue getRwSetting(const std::string &name) = 0;
287 
290  virtual bool updateRwSetting(const std::string &name,
291  const util::SettingsValue &value,
292  bool write = true) = 0;
293 
302  virtual void requestMempoolTransactions(Notifications &notifications) = 0;
303 
305  virtual const CChainParams &params() const = 0;
306 };
307 
310 class ChainClient {
311 public:
312  virtual ~ChainClient() {}
313 
315  virtual void registerRpcs() = 0;
316 
318  virtual bool verify() = 0;
319 
321  virtual bool load() = 0;
322 
324  virtual void start(CScheduler &scheduler) = 0;
325 
327  virtual void flush() = 0;
328 
330  virtual void stop() = 0;
331 
333  virtual void setMockTime(int64_t time) = 0;
334 };
335 
337 std::unique_ptr<Chain> MakeChain(node::NodeContext &node,
338  const CChainParams &params);
339 
340 } // namespace interfaces
341 
342 #endif // BITCOIN_INTERFACES_CHAIN_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:19
Definition: block.h:60
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:80
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:241
virtual void transactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Definition: chain.h:244
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:252
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:250
virtual void updatedBlockTip()
Definition: chain.h:251
virtual void transactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Definition: chain.h:246
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:249
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:310
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:312
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:123
virtual void rpcRunLater(const std::string &name, std::function< void()> fn, int64_t seconds)=0
Run function after given number of seconds.
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 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 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 double guessVerificationProgress(const BlockHash &block_hash)=0
Estimate fraction of total transactions verified if blocks up to the specified block hash are verifie...
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 std::optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
virtual bool isReadyToBroadcast()=0
Check if the node is ready to broadcast transactions.
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 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 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::unique_ptr< Handler > handleNotifications(std::shared_ptr< Notifications > notifications)=0
Register handler for notifications.
virtual bool haveBlockOnDisk(int height)=0
Check that the block is available on disk (i.e.
virtual ~Chain()
Definition: chain.h:125
virtual std::unique_ptr< Handler > handleRpc(const CRPCCommand &command)=0
Register handler for RPC.
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 void initWarning(const bilingual_str &message)=0
Send init warning.
virtual CFeeRate relayMinFee()=0
Relay current minimum fee (from -minrelaytxfee settings).
virtual const CChainParams & params() const =0
This Chain's parameters.
virtual std::vector< util::SettingsValue > getSettingsList(const std::string &arg)=0
Get list of settings values.
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:48
CBlock * m_data
Definition: chain.h:95
const FoundBlock * m_next_block
Definition: chain.h:94
BlockHash * m_hash
Definition: chain.h:88
FoundBlock & height(int &height)
Definition: chain.h:54
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:66
FoundBlock & nextBlock(const FoundBlock &next_block)
Return next block in the active chain if current block is in the active chain.
Definition: chain.h:77
FoundBlock & data(CBlock &data)
Read block data from disk.
Definition: chain.h:83
FoundBlock & inActiveChain(bool &in_active_chain)
Return whether block is in the active (most-work) chain.
Definition: chain.h:71
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:62
int64_t * m_max_time
Definition: chain.h:91
int64_t * m_time
Definition: chain.h:90
bool * m_in_active_chain
Definition: chain.h:93
int64_t * m_mtp_time
Definition: chain.h:92
FoundBlock & hash(BlockHash &hash)
Definition: chain.h:50
FoundBlock & time(int64_t &time)
Definition: chain.h:58
std::unique_ptr< Chain > MakeChain(node::NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: interfaces.cpp:795
Definition: init.h:28
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:105
Bilingual messages:
Definition: translation.h:17
NodeContext struct containing references to chain state and connection state.
Definition: context.h:40
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:148