Bitcoin Core  21.99.0
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 #include <util/settings.h> // For util::SettingsValue
11 
12 #include <functional>
13 #include <memory>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <string>
17 #include <vector>
18 
19 class ArgsManager;
20 class CBlock;
21 class CFeeRate;
22 class CRPCCommand;
23 class CScheduler;
24 class Coin;
25 class uint256;
26 enum class MemPoolRemovalReason;
27 enum class RBFTransactionState;
28 struct bilingual_str;
29 struct CBlockLocator;
30 struct FeeCalculation;
31 struct NodeContext;
32 
33 namespace interfaces {
34 
35 class Handler;
36 class Wallet;
37 
40 {
41 public:
42  FoundBlock& hash(uint256& hash) { m_hash = &hash; return *this; }
43  FoundBlock& height(int& height) { m_height = &height; return *this; }
44  FoundBlock& time(int64_t& time) { m_time = &time; return *this; }
45  FoundBlock& maxTime(int64_t& max_time) { m_max_time = &max_time; return *this; }
46  FoundBlock& mtpTime(int64_t& mtp_time) { m_mtp_time = &mtp_time; return *this; }
48  FoundBlock& inActiveChain(bool& in_active_chain) { m_in_active_chain = &in_active_chain; return *this; }
50  FoundBlock& nextBlock(const FoundBlock& next_block) { m_next_block = &next_block; return *this; }
53  FoundBlock& data(CBlock& data) { m_data = &data; return *this; }
54 
55  uint256* m_hash = nullptr;
56  int* m_height = nullptr;
57  int64_t* m_time = nullptr;
58  int64_t* m_max_time = nullptr;
59  int64_t* m_mtp_time = nullptr;
60  bool* m_in_active_chain = nullptr;
61  const FoundBlock* m_next_block = nullptr;
62  CBlock* m_data = nullptr;
63 };
64 
89 class Chain
90 {
91 public:
92  virtual ~Chain() {}
93 
97  virtual Optional<int> getHeight() = 0;
98 
100  virtual uint256 getBlockHash(int height) = 0;
101 
104  virtual bool haveBlockOnDisk(int height) = 0;
105 
107  virtual CBlockLocator getTipLocator() = 0;
108 
112  virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
113 
115  virtual bool checkFinalTx(const CTransaction& tx) = 0;
116 
119  virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
120 
125  virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block={}) = 0;
126 
129  virtual bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out={}) = 0;
130 
133  virtual bool findAncestorByHash(const uint256& block_hash,
134  const uint256& ancestor_hash,
135  const FoundBlock& ancestor_out={}) = 0;
136 
139  virtual bool findCommonAncestor(const uint256& block_hash1,
140  const uint256& block_hash2,
141  const FoundBlock& ancestor_out={},
142  const FoundBlock& block1_out={},
143  const FoundBlock& block2_out={}) = 0;
144 
148  virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
149 
152  virtual double guessVerificationProgress(const uint256& block_hash) = 0;
153 
157  virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, Optional<int> max_height = {}) = 0;
158 
160  virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
161 
163  virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
164 
168  virtual bool broadcastTransaction(const CTransactionRef& tx,
169  const CAmount& max_tx_fee,
170  bool relay,
171  std::string& err_string) = 0;
172 
174  virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
175 
179  virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
180 
182  virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
183 
185  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
186 
188  virtual unsigned int estimateMaxBlocks() = 0;
189 
191  virtual CFeeRate mempoolMinFee() = 0;
192 
194  virtual CFeeRate relayMinFee() = 0;
195 
197  virtual CFeeRate relayIncrementalFee() = 0;
198 
200  virtual CFeeRate relayDustFee() = 0;
201 
203  virtual bool havePruned() = 0;
204 
206  virtual bool isReadyToBroadcast() = 0;
207 
209  virtual bool isInitialBlockDownload() = 0;
210 
212  virtual bool shutdownRequested() = 0;
213 
215  virtual int64_t getAdjustedTime() = 0;
216 
218  virtual void initMessage(const std::string& message) = 0;
219 
221  virtual void initWarning(const bilingual_str& message) = 0;
222 
224  virtual void initError(const bilingual_str& message) = 0;
225 
227  virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
228 
231  {
232  public:
233  virtual ~Notifications() {}
234  virtual void transactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
235  virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
236  virtual void blockConnected(const CBlock& block, int height) {}
237  virtual void blockDisconnected(const CBlock& block, int height) {}
238  virtual void updatedBlockTip() {}
239  virtual void chainStateFlushed(const CBlockLocator& locator) {}
240  };
241 
243  virtual std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
244 
247  virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
248 
251  virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
252 
254  virtual bool rpcEnableDeprecated(const std::string& method) = 0;
255 
257  virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
258 
260  virtual int rpcSerializationFlags() = 0;
261 
263  virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
264 
266  virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
267 
276  virtual void requestMempoolTransactions(Notifications& notifications) = 0;
277 };
278 
282 {
283 public:
284  virtual ~ChainClient() {}
285 
287  virtual void registerRpcs() = 0;
288 
290  virtual bool verify() = 0;
291 
293  virtual bool load() = 0;
294 
296  virtual void start(CScheduler& scheduler) = 0;
297 
299  virtual void flush() = 0;
300 
302  virtual void stop() = 0;
303 
305  virtual void setMockTime(int64_t time) = 0;
306 };
307 
309 std::unique_ptr<Chain> MakeChain(NodeContext& node);
310 
311 } // namespace interfaces
312 
313 #endif // BITCOIN_INTERFACES_CHAIN_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:39
virtual void transactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Definition: chain.h:235
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:58
virtual void transactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Definition: chain.h:234
virtual void updatedBlockTip()
Definition: chain.h:238
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:392
int64_t * m_mtp_time
Definition: chain.h:59
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: interfaces.cpp:667
RBFTransactionState
The rbf state of unconfirmed transactions.
Definition: rbf.h:11
uint256 * m_hash
Definition: chain.h:55
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:53
virtual void blockDisconnected(const CBlock &block, int height)
Definition: chain.h:237
virtual void blockConnected(const CBlock &block, int height)
Definition: chain.h:236
NodeContext struct containing references to chain state and connection state.
Definition: context.h:37
virtual ~Chain()
Definition: chain.h:92
CBlock * m_data
Definition: chain.h:62
const char * name
Definition: rest.cpp:41
Chain notifications.
Definition: chain.h:230
const FoundBlock * m_next_block
Definition: chain.h:61
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:45
virtual ~ChainClient()
Definition: chain.h:284
static RPCHelpMan stop()
Definition: server.cpp:155
256-bit opaque blob.
Definition: uint256.h:124
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:281
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:89
virtual void chainStateFlushed(const CBlockLocator &locator)
Definition: chain.h:239
int64_t * m_time
Definition: chain.h:57
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
FoundBlock & hash(uint256 &hash)
Definition: chain.h:42
bool * m_in_active_chain
Definition: chain.h:60
FoundBlock & inActiveChain(bool &in_active_chain)
Return whether block is in the active (most-work) chain.
Definition: chain.h:48
FoundBlock & height(int &height)
Definition: chain.h:43
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:46
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:32
FoundBlock & nextBlock(const FoundBlock &next_block)
Return next block in the active chain if current block is in the active chain.
Definition: chain.h:50
std::optional< T > Optional
Substitute for C++17 std::optional DEPRECATED use std::optional in new code.
Definition: optional.h:14
FoundBlock & time(int64_t &time)
Definition: chain.h:44