Bitcoin Core  0.20.99
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 <optional.h> // For Optional and nullopt
9 #include <primitives/transaction.h> // For CTransactionRef
10 
11 #include <memory>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
17 class CBlock;
18 class CFeeRate;
19 class CRPCCommand;
20 class CScheduler;
21 class Coin;
22 class uint256;
23 enum class RBFTransactionState;
24 struct bilingual_str;
25 struct CBlockLocator;
26 struct FeeCalculation;
27 struct NodeContext;
28 
29 namespace interfaces {
30 
31 class Handler;
32 class Wallet;
33 
36 {
37 public:
38  FoundBlock& hash(uint256& hash) { m_hash = &hash; return *this; }
39  FoundBlock& height(int& height) { m_height = &height; return *this; }
40  FoundBlock& time(int64_t& time) { m_time = &time; return *this; }
41  FoundBlock& maxTime(int64_t& max_time) { m_max_time = &max_time; return *this; }
42  FoundBlock& mtpTime(int64_t& mtp_time) { m_mtp_time = &mtp_time; return *this; }
45  FoundBlock& data(CBlock& data) { m_data = &data; return *this; }
46 
47  uint256* m_hash = nullptr;
48  int* m_height = nullptr;
49  int64_t* m_time = nullptr;
50  int64_t* m_max_time = nullptr;
51  int64_t* m_mtp_time = nullptr;
52  CBlock* m_data = nullptr;
53 };
54 
79 class Chain
80 {
81 public:
82  virtual ~Chain() {}
83 
87  virtual Optional<int> getHeight() = 0;
88 
92  virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
93 
95  virtual uint256 getBlockHash(int height) = 0;
96 
99  virtual bool haveBlockOnDisk(int height) = 0;
100 
106  virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
107 
109  virtual CBlockLocator getTipLocator() = 0;
110 
114  virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
115 
117  virtual bool checkFinalTx(const CTransaction& tx) = 0;
118 
121  virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
122 
127  virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block={}) = 0;
128 
132  virtual bool findNextBlock(const uint256& block_hash, int block_height, const FoundBlock& next={}, bool* reorg=nullptr) = 0;
133 
136  virtual bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out={}) = 0;
137 
140  virtual bool findAncestorByHash(const uint256& block_hash,
141  const uint256& ancestor_hash,
142  const FoundBlock& ancestor_out={}) = 0;
143 
146  virtual bool findCommonAncestor(const uint256& block_hash1,
147  const uint256& block_hash2,
148  const FoundBlock& ancestor_out={},
149  const FoundBlock& block1_out={},
150  const FoundBlock& block2_out={}) = 0;
151 
155  virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
156 
159  virtual double guessVerificationProgress(const uint256& block_hash) = 0;
160 
164  virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, Optional<int> max_height = {}) = 0;
165 
167  virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
168 
170  virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
171 
175  virtual bool broadcastTransaction(const CTransactionRef& tx,
176  const CAmount& max_tx_fee,
177  bool relay,
178  std::string& err_string) = 0;
179 
181  virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
182 
186  virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
187 
189  virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
190 
192  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
193 
195  virtual unsigned int estimateMaxBlocks() = 0;
196 
198  virtual CFeeRate mempoolMinFee() = 0;
199 
201  virtual CFeeRate relayMinFee() = 0;
202 
204  virtual CFeeRate relayIncrementalFee() = 0;
205 
207  virtual CFeeRate relayDustFee() = 0;
208 
210  virtual bool havePruned() = 0;
211 
213  virtual bool isReadyToBroadcast() = 0;
214 
216  virtual bool isInitialBlockDownload() = 0;
217 
219  virtual bool shutdownRequested() = 0;
220 
222  virtual int64_t getAdjustedTime() = 0;
223 
225  virtual void initMessage(const std::string& message) = 0;
226 
228  virtual void initWarning(const bilingual_str& message) = 0;
229 
231  virtual void initError(const bilingual_str& message) = 0;
232 
234  virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
235 
238  {
239  public:
240  virtual ~Notifications() {}
241  virtual void transactionAddedToMempool(const CTransactionRef& tx) {}
242  virtual void transactionRemovedFromMempool(const CTransactionRef& ptx) {}
243  virtual void blockConnected(const CBlock& block, int height) {}
244  virtual void blockDisconnected(const CBlock& block, int height) {}
245  virtual void updatedBlockTip() {}
246  virtual void chainStateFlushed(const CBlockLocator& locator) {}
247  };
248 
250  virtual std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
251 
254  virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
255 
258  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
259 
261  virtual bool rpcEnableDeprecated(const std::string& method) = 0;
262 
264  virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
265 
267  virtual int rpcSerializationFlags() = 0;
268 
277  virtual void requestMempoolTransactions(Notifications& notifications) = 0;
278 };
279 
283 {
284 public:
285  virtual ~ChainClient() {}
286 
288  virtual void registerRpcs() = 0;
289 
291  virtual bool verify() = 0;
292 
294  virtual bool load() = 0;
295 
297  virtual void start(CScheduler& scheduler) = 0;
298 
300  virtual void flush() = 0;
301 
303  virtual void stop() = 0;
304 
306  virtual void setMockTime(int64_t time) = 0;
307 
309  virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
310 };
311 
313 std::unique_ptr<Chain> MakeChain(NodeContext& node);
314 
323 std::unique_ptr<ChainClient> MakeWalletClient(Chain& chain, std::vector<std::string> wallet_filenames);
324 
325 } // namespace interfaces
326 
327 #endif // BITCOIN_INTERFACES_CHAIN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:387
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:35
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:114
A UTXO entry.
Definition: coins.h:30
Bilingual messages:
Definition: translation.h:16
Definition: block.h:62
int64_t * m_max_time
Definition: chain.h:50
std::unique_ptr< ChainClient > MakeWalletClient(Chain &chain, std::vector< std::string > wallet_filenames)
Return implementation of ChainClient interface for a wallet client.
Definition: wallet.cpp:517
virtual void updatedBlockTip()
Definition: chain.h:245
int64_t * m_mtp_time
Definition: chain.h:51
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: chain.cpp:386
RBFTransactionState
Definition: rbf.h:10
uint256 * m_hash
Definition: chain.h:47
virtual void transactionAddedToMempool(const CTransactionRef &tx)
Definition: chain.h:241
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
FoundBlock & data(CBlock &data)
Read block data from disk.
Definition: chain.h:45
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:244
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:243
NodeContext struct containing references to chain state and connection state.
Definition: context.h:34
virtual ~Chain()
Definition: chain.h:82
CBlock * m_data
Definition: chain.h:52
const char * name
Definition: rest.cpp:41
Chain notifications.
Definition: chain.h:237
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:41
virtual ~ChainClient()
Definition: chain.h:285
256-bit opaque blob.
Definition: uint256.h:120
virtual void transactionRemovedFromMempool(const CTransactionRef &ptx)
Definition: chain.h:242
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:282
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:79
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:246
int64_t * m_time
Definition: chain.h:49
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
FoundBlock & hash(uint256 &hash)
Definition: chain.h:38
FoundBlock & height(int &height)
Definition: chain.h:39
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:251
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:42
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
FoundBlock & time(int64_t &time)
Definition: chain.h:40
UniValue stop(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:159