Bitcoin Core  0.19.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 CBlockLocator;
25 struct FeeCalculation;
26 struct NodeContext;
27 
28 namespace interfaces {
29 
30 class Handler;
31 class Wallet;
32 
54 class Chain
55 {
56 public:
57  virtual ~Chain() {}
58 
64  class Lock
65  {
66  public:
67  virtual ~Lock() {}
68 
72  virtual Optional<int> getHeight() = 0;
73 
77  virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
78 
80  virtual uint256 getBlockHash(int height) = 0;
81 
83  virtual int64_t getBlockTime(int height) = 0;
84 
87  virtual int64_t getBlockMedianTimePast(int height) = 0;
88 
91  virtual bool haveBlockOnDisk(int height) = 0;
92 
98  virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
99 
102  virtual Optional<int> findPruned(int start_height = 0, Optional<int> stop_height = nullopt) = 0;
103 
110  virtual Optional<int> findFork(const uint256& hash, Optional<int>* height) = 0;
111 
113  virtual CBlockLocator getTipLocator() = 0;
114 
118  virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
119 
121  virtual bool checkFinalTx(const CTransaction& tx) = 0;
122  };
123 
126  virtual std::unique_ptr<Lock> lock(bool try_lock = false) = 0;
127 
134  virtual bool findBlock(const uint256& hash,
135  CBlock* block = nullptr,
136  int64_t* time = nullptr,
137  int64_t* max_time = nullptr) = 0;
138 
142  virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
143 
146  virtual double guessVerificationProgress(const uint256& block_hash) = 0;
147 
149  virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
150 
152  virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
153 
157  virtual bool broadcastTransaction(const CTransactionRef& tx, std::string& err_string, const CAmount& max_tx_fee, bool relay) = 0;
158 
160  virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
161 
165  virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
166 
168  virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
169 
171  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
172 
174  virtual unsigned int estimateMaxBlocks() = 0;
175 
177  virtual CFeeRate mempoolMinFee() = 0;
178 
180  virtual CFeeRate relayMinFee() = 0;
181 
183  virtual CFeeRate relayIncrementalFee() = 0;
184 
186  virtual CFeeRate relayDustFee() = 0;
187 
189  virtual bool havePruned() = 0;
190 
192  virtual bool isReadyToBroadcast() = 0;
193 
195  virtual bool isInitialBlockDownload() = 0;
196 
198  virtual bool shutdownRequested() = 0;
199 
201  virtual int64_t getAdjustedTime() = 0;
202 
204  virtual void initMessage(const std::string& message) = 0;
205 
207  virtual void initWarning(const std::string& message) = 0;
208 
210  virtual void initError(const std::string& message) = 0;
211 
213  virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
214 
217  {
218  public:
219  virtual ~Notifications() {}
220  virtual void TransactionAddedToMempool(const CTransactionRef& tx) {}
221  virtual void TransactionRemovedFromMempool(const CTransactionRef& ptx) {}
222  virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted, int height) {}
223  virtual void BlockDisconnected(const CBlock& block, int height) {}
224  virtual void UpdatedBlockTip() {}
225  virtual void ChainStateFlushed(const CBlockLocator& locator) {}
226  };
227 
229  virtual std::unique_ptr<Handler> handleNotifications(Notifications& notifications) = 0;
230 
233  virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
234 
237  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
238 
240  virtual bool rpcEnableDeprecated(const std::string& method) = 0;
241 
243  virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
244 
246  virtual int rpcSerializationFlags() = 0;
247 
256  virtual void requestMempoolTransactions(Notifications& notifications) = 0;
257 };
258 
262 {
263 public:
264  virtual ~ChainClient() {}
265 
267  virtual void registerRpcs() = 0;
268 
270  virtual bool verify() = 0;
271 
273  virtual bool load() = 0;
274 
276  virtual void start(CScheduler& scheduler) = 0;
277 
279  virtual void flush() = 0;
280 
282  virtual void stop() = 0;
283 };
284 
286 std::unique_ptr<Chain> MakeChain(NodeContext& node);
287 
296 std::unique_ptr<ChainClient> MakeWalletClient(Chain& chain, std::vector<std::string> wallet_filenames);
297 
298 } // namespace interfaces
299 
300 #endif // BITCOIN_INTERFACES_CHAIN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
virtual void findCoins(std::map< COutPoint, Coin > &coins)=0
Look up unspent output information.
virtual void getPackageLimits(unsigned int &limit_ancestor_count, unsigned int &limit_descendant_count)=0
Get the node&#39;s package limits.
virtual void ChainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:225
virtual int rpcSerializationFlags()=0
Current RPC serialization flags.
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:126
virtual bool haveBlockOnDisk(int height)=0
Check that the block is available on disk (i.e.
A UTXO entry.
Definition: coins.h:30
Definition: block.h:72
virtual CFeeRate mempoolMinFee()=0
Mempool minimum fee.
virtual void rpcRunLater(const std::string &name, std::function< void()> fn, int64_t seconds)=0
Run function after given number of seconds. Cancel any previous calls with same name.
virtual int64_t getBlockTime(int height)=0
Get block time. Height must be valid or this function will abort.
virtual CFeeRate relayDustFee()=0
Relay dust fee setting (-dustrelayfee), reflecting lowest rate it&#39;s economical to spend...
virtual int64_t getAdjustedTime()=0
Get adjusted time.
virtual bool isInitialBlockDownload()=0
Check if in IBD.
Interface for querying locked chain state, used by legacy code that assumes state won&#39;t change betwee...
Definition: chain.h:64
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:543
virtual Optional< int > getBlockHeight(const uint256 &hash)=0
Get block height above genesis block.
static auto & nullopt
Substitute for C++17 std::nullopt.
Definition: optional.h:24
virtual void TransactionAddedToMempool(const CTransactionRef &tx)
Definition: chain.h:220
virtual Optional< int > findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256 *hash)=0
Return height of the first block in the chain with timestamp equal or greater than the given time and...
virtual void initError(const std::string &message)=0
Send init error.
virtual double guessVerificationProgress(const uint256 &block_hash)=0
Estimate fraction of total transactions verified if blocks up to the specified block hash are verifie...
virtual int64_t getBlockMedianTimePast(int height)=0
Get block median time past.
virtual Optional< int > findPruned(int start_height=0, Optional< int > stop_height=nullopt)=0
Return height of last block in the specified range which is pruned, or nullopt if no block in the ran...
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: chain.cpp:378
RBFTransactionState
Definition: rbf.h:10
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
virtual bool hasDescendantsInMempool(const uint256 &txid)=0
Check if transaction has descendants in mempool.
virtual void BlockDisconnected(const CBlock &block, int height)
Definition: chain.h:223
NodeContext struct containing references to chain state and connection state.
Definition: context.h:31
virtual ~Chain()
Definition: chain.h:57
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation *calc=nullptr)=0
Estimate smart fee.
const char * name
Definition: rest.cpp:40
virtual Optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
Chain notifications.
Definition: chain.h:216
virtual std::unique_ptr< Handler > handleRpc(const CRPCCommand &command)=0
Register handler for RPC.
virtual ~ChainClient()
Definition: chain.h:264
virtual void waitForNotificationsIfTipChanged(const uint256 &old_tip)=0
Wait for pending notifications to be processed unless block hash points to the current chain tip...
virtual CBlockLocator getTipLocator()=0
Get locator for the current chain tip.
virtual void requestMempoolTransactions(Notifications &notifications)=0
Synchronously send TransactionAddedToMempool notifications about all current mempool transactions to ...
virtual bool broadcastTransaction(const CTransactionRef &tx, std::string &err_string, const CAmount &max_tx_fee, bool relay)=0
Transaction is added to memory pool, if the transaction fee is below the amount specified by max_tx_f...
virtual std::unique_ptr< Lock > lock(bool try_lock=false)=0
Return Lock interface.
virtual void getTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants)=0
Calculate mempool ancestor and descendant counts for the given transaction.
256-bit opaque blob.
Definition: uint256.h:120
virtual bool isReadyToBroadcast()=0
Check if the node is ready to broadcast transactions.
virtual bool checkChainLimits(const CTransactionRef &tx)=0
Check if transaction will pass the mempool&#39;s chain limits.
virtual std::unique_ptr< Handler > handleNotifications(Notifications &notifications)=0
Register handler for notifications.
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:261
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:54
virtual void UpdatedBlockTip()
Definition: chain.h:224
virtual void BlockConnected(const CBlock &block, const std::vector< CTransactionRef > &tx_conflicted, int height)
Definition: chain.h:222
virtual void initWarning(const std::string &message)=0
Send init warning.
virtual void initMessage(const std::string &message)=0
Send init message.
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
virtual RBFTransactionState isRBFOptIn(const CTransaction &tx)=0
Check if transaction is RBF opt in.
virtual 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 ~Lock()
Definition: chain.h:67
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx)
Definition: chain.h:221
virtual void showProgress(const std::string &title, int progress, bool resume_possible)=0
Send progress indicator.
virtual unsigned int estimateMaxBlocks()=0
Fee estimator max target.
virtual bool shutdownRequested()=0
Check if shutdown requested.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
virtual bool checkFinalTx(const CTransaction &tx)=0
Check if transaction will be final given chain height current time.
virtual bool rpcEnableDeprecated(const std::string &method)=0
Check if deprecated RPC is enabled.
virtual Optional< int > findFork(const uint256 &hash, Optional< int > *height)=0
Return height of the specified block if it is on the chain, otherwise return the height of the highes...
virtual CFeeRate relayIncrementalFee()=0
Relay incremental fee setting (-incrementalrelayfee), reflecting cost of relay.
virtual CFeeRate relayMinFee()=0
Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
virtual uint256 getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
virtual bool findBlock(const uint256 &hash, CBlock *block=nullptr, int64_t *time=nullptr, int64_t *max_time=nullptr)=0
Return whether node has the block and optionally return block metadata or contents.
virtual bool havePruned()=0
Check if any block has been pruned.
UniValue stop(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:154