Bitcoin Core  0.21.0rc5
P2P Digital Currency
node.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_NODE_H
6 #define BITCOIN_INTERFACES_NODE_H
7 
8 #include <amount.h> // For CAmount
9 #include <net.h> // For CConnman::NumConnections
10 #include <net_types.h> // For banmap_t
11 #include <netaddress.h> // For Network
12 #include <support/allocators/secure.h> // For SecureString
13 #include <util/translation.h>
14 
15 #include <functional>
16 #include <memory>
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <string>
20 #include <tuple>
21 #include <vector>
22 
23 class BanMan;
24 class CCoinControl;
25 class CFeeRate;
26 class CNodeStats;
27 class Coin;
28 class RPCTimerInterface;
29 class UniValue;
30 class proxyType;
31 enum class SynchronizationState;
32 struct CNodeStateStats;
33 struct NodeContext;
34 struct bilingual_str;
35 
36 namespace interfaces {
37 class Handler;
38 class WalletClient;
39 struct BlockTip;
40 
43 {
45  int64_t block_time;
47  int64_t header_time;
49 };
50 
52 class Node
53 {
54 public:
55  virtual ~Node() {}
56 
58  virtual void initLogging() = 0;
59 
61  virtual void initParameterInteraction() = 0;
62 
64  virtual bilingual_str getWarnings() = 0;
65 
66  // Get log flags.
67  virtual uint32_t getLogCategories() = 0;
68 
70  virtual bool baseInitialize() = 0;
71 
73  virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info = nullptr) = 0;
74 
76  virtual void appShutdown() = 0;
77 
79  virtual void startShutdown() = 0;
80 
82  virtual bool shutdownRequested() = 0;
83 
85  virtual void mapPort(bool use_upnp) = 0;
86 
88  virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
89 
92 
94  using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
95  virtual bool getNodesStats(NodesStats& stats) = 0;
96 
98  virtual bool getBanned(banmap_t& banmap) = 0;
99 
101  virtual bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) = 0;
102 
104  virtual bool unban(const CSubNet& ip) = 0;
105 
107  virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
108 
110  virtual bool disconnectById(NodeId id) = 0;
111 
113  virtual int64_t getTotalBytesRecv() = 0;
114 
116  virtual int64_t getTotalBytesSent() = 0;
117 
119  virtual size_t getMempoolSize() = 0;
120 
122  virtual size_t getMempoolDynamicUsage() = 0;
123 
125  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
126 
128  virtual int getNumBlocks() = 0;
129 
131  virtual uint256 getBestBlockHash() = 0;
132 
134  virtual int64_t getLastBlockTime() = 0;
135 
137  virtual double getVerificationProgress() = 0;
138 
140  virtual bool isInitialBlockDownload() = 0;
141 
143  virtual bool getReindex() = 0;
144 
146  virtual bool getImporting() = 0;
147 
149  virtual void setNetworkActive(bool active) = 0;
150 
152  virtual bool getNetworkActive() = 0;
153 
155  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) = 0;
156 
158  virtual CFeeRate getDustRelayFee() = 0;
159 
161  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
162 
164  virtual std::vector<std::string> listRpcCommands() = 0;
165 
168 
170  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
171 
173  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
174 
176  virtual WalletClient& walletClient() = 0;
177 
179  using InitMessageFn = std::function<void(const std::string& message)>;
180  virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
181 
183  using MessageBoxFn =
184  std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
185  virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
186 
188  using QuestionFn = std::function<bool(const bilingual_str& message,
189  const std::string& non_interactive_message,
190  const std::string& caption,
191  unsigned int style)>;
192  virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
193 
195  using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
196  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
197 
199  using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
200  virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
201 
203  using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
204  virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
205 
207  using NotifyAlertChangedFn = std::function<void()>;
208  virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
209 
211  using BannedListChangedFn = std::function<void()>;
212  virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
213 
216  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
217  virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
218 
221  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
222  virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
223 
226  virtual NodeContext* context() { return nullptr; }
227  virtual void setContext(NodeContext* context) { }
228 };
229 
231 std::unique_ptr<Node> MakeNode(NodeContext* context = nullptr);
232 
234 struct BlockTip {
236  int64_t block_time;
238 };
239 
240 } // namespace interfaces
241 
242 #endif // BITCOIN_INTERFACES_NODE_H
int flags
Definition: bitcoin-tx.cpp:506
Definition: banman.h:58
Coin Control Features.
Definition: coincontrol.h:23
NumConnections
Definition: net.h:191
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
Network address.
Definition: netaddress.h:120
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
A UTXO entry.
Definition: coins.h:31
RPC timer "driver".
Definition: server.h:61
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:53
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface)=0
Set RPC timer interface if unset.
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:211
virtual std::unique_ptr< Handler > handleNotifyBlockTip(NotifyBlockTipFn fn)=0
virtual void setContext(NodeContext *context)
Definition: node.h:227
virtual std::unique_ptr< Handler > handleNotifyAlertChanged(NotifyAlertChangedFn fn)=0
virtual bool disconnectById(NodeId id)=0
Disconnect node by id.
virtual std::vector< std::string > listRpcCommands()=0
List rpc commands.
virtual bool baseInitialize()=0
Initialize app dependencies.
virtual std::unique_ptr< Handler > handleNotifyHeaderTip(NotifyHeaderTipFn fn)=0
virtual std::unique_ptr< Handler > handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn)=0
virtual bool getImporting()=0
Get importing.
virtual bool ban(const CNetAddr &net_addr, int64_t ban_time_offset)=0
Ban node.
std::function< void(bool network_active)> NotifyNetworkActiveChangedFn
Register handler for network active messages.
Definition: node.h:203
virtual void setNetworkActive(bool active)=0
Set network active.
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyHeaderTipFn
Register handler for header tip messages.
Definition: node.h:221
virtual bilingual_str getWarnings()=0
Get warnings.
virtual NodeContext * context()
Get and set internal node context.
Definition: node.h:226
virtual std::unique_ptr< Handler > handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn)=0
virtual void appShutdown()=0
Stop node.
std::function< void(const std::string &message)> InitMessageFn
Register handler for init messages.
Definition: node.h:179
virtual std::unique_ptr< Handler > handleQuestion(QuestionFn fn)=0
std::function< bool(const bilingual_str &message, const std::string &non_interactive_message, const std::string &caption, unsigned int style)> QuestionFn
Register handler for question messages.
Definition: node.h:191
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
std::function< bool(const bilingual_str &message, const std::string &caption, unsigned int style)> MessageBoxFn
Register handler for message box messages.
Definition: node.h:184
virtual void rpcUnsetTimerInterface(RPCTimerInterface *iface)=0
Unset RPC timer interface.
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:94
virtual UniValue executeRpc(const std::string &command, const UniValue &params, const std::string &uri)=0
Execute rpc command.
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
virtual void mapPort(bool use_upnp)=0
Map port.
virtual bool getReindex()=0
Get reindex.
virtual void initParameterInteraction()=0
Init parameter interaction.
virtual std::unique_ptr< Handler > handleInitMessage(InitMessageFn fn)=0
virtual void startShutdown()=0
Start shutdown.
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:207
virtual int64_t getLastBlockTime()=0
Get last block time.
virtual bool getBanned(banmap_t &banmap)=0
Get ban map entries.
virtual size_t getMempoolSize()=0
Get mempool size.
virtual bool getNetworkActive()=0
Get network active.
virtual bool unban(const CSubNet &ip)=0
Unban node.
virtual uint32_t getLogCategories()=0
virtual std::unique_ptr< Handler > handleMessageBox(MessageBoxFn fn)=0
virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo *tip_info=nullptr)=0
Start node.
virtual size_t getNodeCount(CConnman::NumConnections flags)=0
Get number of connections.
virtual double getVerificationProgress()=0
Get verification progress.
virtual bool getHeaderTip(int &height, int64_t &block_time)=0
Get header tip height and time.
virtual uint256 getBestBlockHash()=0
Get best block hash.
virtual ~Node()
Definition: node.h:55
virtual bool disconnectByAddress(const CNetAddr &net_addr)=0
Disconnect node by address.
virtual WalletClient & walletClient()=0
Get wallet client.
virtual int64_t getTotalBytesRecv()=0
Get total bytes recv.
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, int *returned_target=nullptr)=0
Estimate smart fee.
virtual std::unique_ptr< Handler > handleBannedListChanged(BannedListChangedFn fn)=0
std::function< void(const std::string &title, int progress, bool resume_possible)> ShowProgressFn
Register handler for progress messages.
Definition: node.h:195
virtual void initLogging()=0
Init logging.
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
virtual size_t getMempoolDynamicUsage()=0
Get mempool dynamic usage.
virtual int getNumBlocks()=0
Get num blocks.
virtual bool shutdownRequested()=0
Return whether shutdown was requested.
virtual bool getNodesStats(NodesStats &stats)=0
virtual bool isInitialBlockDownload()=0
Is initial block download.
std::function< void(int new_num_connections)> NotifyNumConnectionsChangedFn
Register handler for number of connections changed messages.
Definition: node.h:199
virtual bool getUnspentOutput(const COutPoint &output, Coin &coin)=0
Get unspent outputs associated with a transaction.
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:216
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:310
256-bit opaque blob.
Definition: uint256.h:124
std::unique_ptr< Node > MakeNode(NodeContext *context)
Return implementation of Node interface.
Definition: node.cpp:301
int64_t NodeId
Definition: net.h:92
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
Network
A network type.
Definition: netaddress.h:44
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
Bilingual messages:
Definition: translation.h:16
Block and header tip information.
Definition: node.h:43
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:234
uint256 block_hash
Definition: node.h:237
int64_t block_time
Definition: node.h:236
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:106