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 <functional>
12 #include <memory>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 
18 class ArgsManager;
19 class CBlock;
20 class CFeeRate;
21 class CRPCCommand;
22 class CScheduler;
23 class Coin;
24 class uint256;
25 enum class MemPoolRemovalReason;
26 enum class RBFTransactionState;
27 struct bilingual_str;
28 struct CBlockLocator;
29 struct FeeCalculation;
30 struct NodeContext;
31 
32 namespace interfaces {
33 
34 class Handler;
35 class Wallet;
36 
39 {
40 public:
41  FoundBlock& hash(uint256& hash) { m_hash = &hash; return *this; }
42  FoundBlock& height(int& height) { m_height = &height; return *this; }
43  FoundBlock& time(int64_t& time) { m_time = &time; return *this; }
44  FoundBlock& maxTime(int64_t& max_time) { m_max_time = &max_time; return *this; }
45  FoundBlock& mtpTime(int64_t& mtp_time) { m_mtp_time = &mtp_time; return *this; }
48  FoundBlock& data(CBlock& data) { m_data = &data; return *this; }
49 
50  uint256* m_hash = nullptr;
51  int* m_height = nullptr;
52  int64_t* m_time = nullptr;
53  int64_t* m_max_time = nullptr;
54  int64_t* m_mtp_time = nullptr;
55  CBlock* m_data = nullptr;
56 };
57 
82 class Chain
83 {
84 public:
85  virtual ~Chain() {}
86 
90  virtual Optional<int> getHeight() = 0;
91 
95  virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
96 
98  virtual uint256 getBlockHash(int height) = 0;
99 
102  virtual bool haveBlockOnDisk(int height) = 0;
103 
109  virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
110 
112  virtual CBlockLocator getTipLocator() = 0;
113 
117  virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
118 
120  virtual bool checkFinalTx(const CTransaction& tx) = 0;
121 
124  virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
125 
130  virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block={}) = 0;
131 
135  virtual bool findNextBlock(const uint256& block_hash, int block_height, const FoundBlock& next={}, bool* reorg=nullptr) = 0;
136 
139  virtual bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out={}) = 0;
140 
143  virtual bool findAncestorByHash(const uint256& block_hash,
144  const uint256& ancestor_hash,
145  const FoundBlock& ancestor_out={}) = 0;
146 
149  virtual bool findCommonAncestor(const uint256& block_hash1,
150  const uint256& block_hash2,
151  const FoundBlock& ancestor_out={},
152  const FoundBlock& block1_out={},
153  const FoundBlock& block2_out={}) = 0;
154 
158  virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
159 
162  virtual double guessVerificationProgress(const uint256& block_hash) = 0;
163 
167  virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, Optional<int> max_height = {}) = 0;
168 
170  virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
171 
173  virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
174 
178  virtual bool broadcastTransaction(const CTransactionRef& tx,
179  const CAmount& max_tx_fee,
180  bool relay,
181  std::string& err_string) = 0;
182 
184  virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
185 
189  virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
190 
192  virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
193 
195  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
196 
198  virtual unsigned int estimateMaxBlocks() = 0;
199 
201  virtual CFeeRate mempoolMinFee() = 0;
202 
204  virtual CFeeRate relayMinFee() = 0;
205 
207  virtual CFeeRate relayIncrementalFee() = 0;
208 
210  virtual CFeeRate relayDustFee() = 0;
211 
213  virtual bool havePruned() = 0;
214 
216  virtual bool isReadyToBroadcast() = 0;
217 
219  virtual bool isInitialBlockDownload() = 0;
220 
222  virtual bool shutdownRequested() = 0;
223 
225  virtual int64_t getAdjustedTime() = 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, bool resume_possible) = 0;
238 
241  {
242  public:
243  virtual ~Notifications() {}
244  virtual void transactionAddedToMempool(const CTransactionRef& tx) {}
246  virtual void blockConnected(const CBlock& block, int height) {}
247  virtual void blockDisconnected(const CBlock& block, int height) {}
248  virtual void updatedBlockTip() {}
249  virtual void chainStateFlushed(const CBlockLocator& locator) {}
250  };
251 
253  virtual std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
254 
257  virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
258 
261  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
262 
264  virtual bool rpcEnableDeprecated(const std::string& method) = 0;
265 
267  virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
268 
270  virtual int rpcSerializationFlags() = 0;
271 
280  virtual void requestMempoolTransactions(Notifications& notifications) = 0;
281 };
282 
286 {
287 public:
288  virtual ~ChainClient() {}
289 
291  virtual void registerRpcs() = 0;
292 
294  virtual bool verify() = 0;
295 
297  virtual bool load() = 0;
298 
300  virtual void start(CScheduler& scheduler) = 0;
301 
303  virtual void flush() = 0;
304 
306  virtual void stop() = 0;
307 
309  virtual void setMockTime(int64_t time) = 0;
310 
312  virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
313 };
314 
316 std::unique_ptr<Chain> MakeChain(NodeContext& node);
317 
326 std::unique_ptr<ChainClient> MakeWalletClient(Chain& chain, ArgsManager& args, std::vector<std::string> wallet_filenames);
327 
328 } // namespace interfaces
329 
330 #endif // BITCOIN_INTERFACES_CHAIN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:389
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:38
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
virtual void transactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason)
Definition: chain.h:245
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:53
virtual void updatedBlockTip()
Definition: chain.h:248
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:365
int64_t * m_mtp_time
Definition: chain.h:54
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:50
virtual void transactionAddedToMempool(const CTransactionRef &tx)
Definition: chain.h:244
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:48
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:247
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:246
NodeContext struct containing references to chain state and connection state.
Definition: context.h:35
virtual ~Chain()
Definition: chain.h:85
CBlock * m_data
Definition: chain.h:55
const char * name
Definition: rest.cpp:41
Chain notifications.
Definition: chain.h:240
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:44
virtual ~ChainClient()
Definition: chain.h:288
static RPCHelpMan stop()
Definition: server.cpp:155
256-bit opaque blob.
Definition: uint256.h:123
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:285
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:82
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:249
int64_t * m_time
Definition: chain.h:52
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
FoundBlock & hash(uint256 &hash)
Definition: chain.h:41
FoundBlock & height(int &height)
Definition: chain.h:42
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:253
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:45
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:32
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:527
FoundBlock & time(int64_t &time)
Definition: chain.h:43