Bitcoin ABC  0.22.13
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 
11 #include <cstddef>
12 #include <cstdint>
13 #include <functional>
14 #include <memory>
15 #include <optional>
16 #include <string>
17 #include <vector>
18 
19 class ArgsManager;
20 class CBlock;
21 class CChainParams;
22 class Coin;
23 class Config;
24 class CRPCCommand;
25 class CScheduler;
26 class TxValidationState;
27 
28 struct BlockHash;
29 struct bilingual_str;
30 struct CBlockLocator;
31 struct NodeContext;
32 
33 namespace Consensus {
34 struct Params;
35 }
36 
37 namespace interfaces {
38 
39 class Handler;
40 class Wallet;
41 
43 class FoundBlock {
44 public:
46  m_hash = &hash;
47  return *this;
48  }
49  FoundBlock &height(int &height) {
50  m_height = &height;
51  return *this;
52  }
53  FoundBlock &time(int64_t &time) {
54  m_time = &time;
55  return *this;
56  }
57  FoundBlock &maxTime(int64_t &max_time) {
58  m_max_time = &max_time;
59  return *this;
60  }
61  FoundBlock &mtpTime(int64_t &mtp_time) {
62  m_mtp_time = &mtp_time;
63  return *this;
64  }
68  m_data = &data;
69  return *this;
70  }
71 
72  BlockHash *m_hash = nullptr;
73  int *m_height = nullptr;
74  int64_t *m_time = nullptr;
75  int64_t *m_max_time = nullptr;
76  int64_t *m_mtp_time = nullptr;
77  CBlock *m_data = nullptr;
78 };
79 
105 class Chain {
106 public:
107  virtual ~Chain() {}
108 
112  virtual std::optional<int> getHeight() = 0;
113 
117  virtual std::optional<int> getBlockHeight(const BlockHash &hash) = 0;
118 
120  virtual BlockHash getBlockHash(int height) = 0;
121 
124  virtual bool haveBlockOnDisk(int height) = 0;
125 
132  virtual std::optional<int>
133  findFirstBlockWithTimeAndHeight(int64_t time, int height,
134  BlockHash *hash) = 0;
135 
137  virtual CBlockLocator getTipLocator() = 0;
138 
142  virtual std::optional<int>
143  findLocatorFork(const CBlockLocator &locator) = 0;
144 
146  virtual bool
147  contextualCheckTransactionForCurrentBlock(const CTransaction &tx,
148  TxValidationState &state) = 0;
149 
152  virtual bool findBlock(const BlockHash &hash,
153  const FoundBlock &block = {}) = 0;
154 
159  virtual bool
160  findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height,
161  const FoundBlock &block = {}) = 0;
162 
166  virtual bool findNextBlock(const BlockHash &block_hash, int block_height,
167  const FoundBlock &next = {},
168  bool *reorg = nullptr) = 0;
169 
172  virtual bool findAncestorByHeight(const BlockHash &block_hash,
173  int ancestor_height,
174  const FoundBlock &ancestor_out = {}) = 0;
175 
178  virtual bool findAncestorByHash(const BlockHash &block_hash,
179  const BlockHash &ancestor_hash,
180  const FoundBlock &ancestor_out = {}) = 0;
181 
184  virtual bool findCommonAncestor(const BlockHash &block_hash1,
185  const BlockHash &block_hash2,
186  const FoundBlock &ancestor_out = {},
187  const FoundBlock &block1_out = {},
188  const FoundBlock &block2_out = {}) = 0;
189 
193  virtual void findCoins(std::map<COutPoint, Coin> &coins) = 0;
194 
197  virtual double guessVerificationProgress(const BlockHash &block_hash) = 0;
198 
202  virtual bool hasBlocks(const BlockHash &block_hash, int min_height = 0,
203  std::optional<int> max_height = {}) = 0;
204 
206  virtual bool hasDescendantsInMempool(const TxId &txid) = 0;
207 
212  virtual bool broadcastTransaction(const Config &config,
213  const CTransactionRef &tx,
214  const Amount &max_tx_fee, bool relay,
215  std::string &err_string) = 0;
216 
219  virtual void getTransactionAncestry(const TxId &txid, size_t &ancestors,
220  size_t &descendants) = 0;
221 
225  virtual void getPackageLimits(size_t &limit_ancestor_count,
226  size_t &limit_descendant_count) = 0;
227 
229  virtual bool checkChainLimits(const CTransactionRef &tx) = 0;
230 
232  virtual CFeeRate estimateFee() const = 0;
233 
235  virtual CFeeRate relayMinFee() = 0;
236 
239  virtual CFeeRate relayDustFee() = 0;
240 
242  virtual bool havePruned() = 0;
243 
245  virtual bool isReadyToBroadcast() = 0;
246 
248  virtual bool isInitialBlockDownload() = 0;
249 
251  virtual bool shutdownRequested() = 0;
252 
254  virtual int64_t getAdjustedTime() = 0;
255 
257  virtual void initMessage(const std::string &message) = 0;
258 
260  virtual void initWarning(const bilingual_str &message) = 0;
261 
263  virtual void initError(const bilingual_str &message) = 0;
264 
266  virtual void showProgress(const std::string &title, int progress,
267  bool resume_possible) = 0;
268 
271  public:
272  virtual ~Notifications() {}
273  virtual void transactionAddedToMempool(const CTransactionRef &tx) {}
275  }
276  virtual void blockConnected(const CBlock &block, int height) {}
277  virtual void blockDisconnected(const CBlock &block, int height) {}
278  virtual void updatedBlockTip() {}
279  virtual void chainStateFlushed(const CBlockLocator &locator) {}
280  };
281 
283  virtual std::unique_ptr<Handler>
284  handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
285 
288  virtual void waitForNotificationsIfTipChanged(const BlockHash &old_tip) = 0;
289 
292  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand &command) = 0;
293 
295  virtual bool rpcEnableDeprecated(const std::string &method) = 0;
296 
299  virtual void rpcRunLater(const std::string &name, std::function<void()> fn,
300  int64_t seconds) = 0;
301 
303  virtual int rpcSerializationFlags() = 0;
304 
313  virtual void requestMempoolTransactions(Notifications &notifications) = 0;
314 
316  virtual const CChainParams &params() const = 0;
317 };
318 
321 class ChainClient {
322 public:
323  virtual ~ChainClient() {}
324 
326  virtual void registerRpcs() = 0;
327 
329  virtual bool verify(const CChainParams &chainParams) = 0;
330 
332  virtual bool load(const CChainParams &chainParams) = 0;
333 
335  virtual void start(CScheduler &scheduler) = 0;
336 
338  virtual void flush() = 0;
339 
341  virtual void stop() = 0;
342 
344  virtual void setMockTime(int64_t time) = 0;
345 
347  virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
348 };
349 
351 std::unique_ptr<Chain> MakeChain(NodeContext &node, const CChainParams &params);
352 
361 std::unique_ptr<ChainClient>
362 MakeWalletClient(Chain &chain, ArgsManager &args,
363  std::vector<std::string> wallet_filenames);
364 
365 } // namespace interfaces
366 
367 #endif // BITCOIN_INTERFACES_CHAIN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:43
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:110
A UTXO entry.
Definition: coins.h:27
Bilingual messages:
Definition: translation.h:17
Definition: block.h:62
Definition: amount.h:17
virtual void updatedBlockTip()
Definition: chain.h:278
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
FoundBlock & hash(BlockHash &hash)
Definition: chain.h:45
virtual void transactionAddedToMempool(const CTransactionRef &tx)
Definition: chain.h:273
FoundBlock & data(CBlock &data)
Read block data from disk.
Definition: chain.h:67
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:277
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:276
Definition: config.h:19
NodeContext struct containing references to chain state and connection state.
Definition: context.h:35
virtual ~Chain()
Definition: chain.h:107
const char * name
Definition: rest.cpp:43
Chain notifications.
Definition: chain.h:270
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:57
virtual ~ChainClient()
Definition: chain.h:323
virtual void transactionRemovedFromMempool(const CTransactionRef &ptx)
Definition: chain.h:274
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:321
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:105
const CChainParams & Params()
Return the currently selected parameters.
A TxId is the identifier of a transaction.
Definition: txid.h:14
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:279
static UniValue stop(const Config &config, const JSONRPCRequest &jsonRequest)
Definition: server.cpp:198
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
FoundBlock & height(int &height)
Definition: chain.h:49
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:61
std::unique_ptr< ChainClient > MakeWalletClient(Chain &chain, ArgsManager &args, std::vector< std::string > wallet_filenames)
Return implementation of ChainClient interface for a wallet client.
Definition: wallet.cpp:532
FoundBlock & time(int64_t &time)
Definition: chain.h:53
std::unique_ptr< Chain > MakeChain(NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: chain.cpp:448