Bitcoin ABC  0.26.3
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 
197  virtual bool hasDescendantsInMempool(const TxId &txid) = 0;
198 
203  virtual bool broadcastTransaction(const Config &config,
204  const CTransactionRef &tx,
205  const Amount &max_tx_fee, bool relay,
206  std::string &err_string) = 0;
207 
210  virtual void getTransactionAncestry(const TxId &txid, size_t &ancestors,
211  size_t &descendants,
212  size_t *ancestorsize = nullptr,
213  Amount *ancestorfees = nullptr) = 0;
214 
218  virtual void getPackageLimits(size_t &limit_ancestor_count,
219  size_t &limit_descendant_count) = 0;
220 
222  virtual bool checkChainLimits(const CTransactionRef &tx) = 0;
223 
225  virtual CFeeRate estimateFee() const = 0;
226 
228  virtual CFeeRate relayMinFee() = 0;
229 
232  virtual CFeeRate relayDustFee() = 0;
233 
235  virtual bool havePruned() = 0;
236 
238  virtual bool isReadyToBroadcast() = 0;
239 
241  virtual bool isInitialBlockDownload() = 0;
242 
244  virtual bool shutdownRequested() = 0;
245 
247  virtual int64_t getAdjustedTime() = 0;
248 
250  virtual void initMessage(const std::string &message) = 0;
251 
253  virtual void initWarning(const bilingual_str &message) = 0;
254 
256  virtual void initError(const bilingual_str &message) = 0;
257 
259  virtual void showProgress(const std::string &title, int progress,
260  bool resume_possible) = 0;
261 
264  public:
265  virtual ~Notifications() {}
267  uint64_t mempool_sequence) {}
269  MemPoolRemovalReason reason,
270  uint64_t mempool_sequence) {}
271  virtual void blockConnected(const CBlock &block, int height) {}
272  virtual void blockDisconnected(const CBlock &block, int height) {}
273  virtual void updatedBlockTip() {}
274  virtual void chainStateFlushed(const CBlockLocator &locator) {}
275  };
276 
278  virtual std::unique_ptr<Handler>
279  handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
280 
283  virtual void waitForNotificationsIfTipChanged(const BlockHash &old_tip) = 0;
284 
287  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand &command) = 0;
288 
290  virtual bool rpcEnableDeprecated(const std::string &method) = 0;
291 
294  virtual void rpcRunLater(const std::string &name, std::function<void()> fn,
295  int64_t seconds) = 0;
296 
298  virtual int rpcSerializationFlags() = 0;
299 
301  virtual util::SettingsValue getRwSetting(const std::string &name) = 0;
302 
304  virtual bool updateRwSetting(const std::string &name,
305  const util::SettingsValue &value) = 0;
306 
315  virtual void requestMempoolTransactions(Notifications &notifications) = 0;
316 
318  virtual const CChainParams &params() const = 0;
319 };
320 
323 class ChainClient {
324 public:
325  virtual ~ChainClient() {}
326 
328  virtual void registerRpcs() = 0;
329 
331  virtual bool verify() = 0;
332 
334  virtual bool load() = 0;
335 
337  virtual void start(CScheduler &scheduler) = 0;
338 
340  virtual void flush() = 0;
341 
343  virtual void stop() = 0;
344 
346  virtual void setMockTime(int64_t time) = 0;
347 };
348 
350 std::unique_ptr<Chain> MakeChain(node::NodeContext &node,
351  const CChainParams &params);
352 
353 } // namespace interfaces
354 
355 #endif // BITCOIN_INTERFACES_CHAIN_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: block.h:55
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
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:27
Definition: config.h:17
Chain notifications.
Definition: chain.h:263
virtual void transactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Definition: chain.h:266
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:274
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:272
virtual void updatedBlockTip()
Definition: chain.h:273
virtual void transactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Definition: chain.h:268
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:271
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:323
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:325
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 void getTransactionAncestry(const TxId &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, Amount *ancestorfees=nullptr)=0
Calculate mempool ancestor and descendant counts for the given transaction.
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 updateRwSetting(const std::string &name, const util::SettingsValue &value)=0
Write a setting to <datadir>/settings.json.
virtual bool isReadyToBroadcast()=0
Check if the node is ready to broadcast transactions.
virtual int64_t getAdjustedTime()=0
Get adjusted time.
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 void getPackageLimits(size_t &limit_ancestor_count, size_t &limit_descendant_count)=0
Get the node's package 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 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 bool checkChainLimits(const CTransactionRef &tx)=0
Check if transaction will pass the mempool's chain limits.
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 bool hasDescendantsInMempool(const TxId &txid)=0
Check if transaction has descendants in mempool.
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.
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:778
Definition: init.h:28
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:321
const char * name
Definition: rest.cpp:49
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:100
A TxId is the identifier of a transaction.
Definition: txid.h:14
Bilingual messages:
Definition: translation.h:17
NodeContext struct containing references to chain state and connection state.
Definition: context.h:38
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:290