Bitcoin Core  22.0.0
P2P Digital Currency
node.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_NODE_H
6 #define BITCOIN_INTERFACES_NODE_H
7 
8 #include <amount.h> // For CAmount
9 #include <external_signer.h>
10 #include <net.h> // For NodeId
11 #include <net_types.h> // For banmap_t
12 #include <netaddress.h> // For Network
13 #include <netbase.h> // For ConnectionDirection
14 #include <support/allocators/secure.h> // For SecureString
15 #include <util/translation.h>
16 
17 #include <functional>
18 #include <memory>
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <string>
22 #include <tuple>
23 #include <vector>
24 
25 class BanMan;
26 class CCoinControl;
27 class CFeeRate;
28 class CNodeStats;
29 class Coin;
30 class RPCTimerInterface;
31 class UniValue;
32 class proxyType;
33 enum class SynchronizationState;
34 struct CNodeStateStats;
35 struct NodeContext;
36 struct bilingual_str;
37 
38 namespace interfaces {
39 class Handler;
40 class WalletClient;
41 struct BlockTip;
42 
45 {
47  int64_t block_time;
49  int64_t header_time;
51 };
52 
54 class Node
55 {
56 public:
57  virtual ~Node() {}
58 
60  virtual void initLogging() = 0;
61 
63  virtual void initParameterInteraction() = 0;
64 
66  virtual bilingual_str getWarnings() = 0;
67 
68  // Get log flags.
69  virtual uint32_t getLogCategories() = 0;
70 
72  virtual bool baseInitialize() = 0;
73 
75  virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info = nullptr) = 0;
76 
78  virtual void appShutdown() = 0;
79 
81  virtual void startShutdown() = 0;
82 
84  virtual bool shutdownRequested() = 0;
85 
87  virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
88 
90  virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
91 
93  virtual size_t getNodeCount(ConnectionDirection flags) = 0;
94 
96  using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
97  virtual bool getNodesStats(NodesStats& stats) = 0;
98 
100  virtual bool getBanned(banmap_t& banmap) = 0;
101 
103  virtual bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) = 0;
104 
106  virtual bool unban(const CSubNet& ip) = 0;
107 
109  virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
110 
112  virtual bool disconnectById(NodeId id) = 0;
113 
115  virtual std::vector<ExternalSigner> externalSigners() = 0;
116 
118  virtual int64_t getTotalBytesRecv() = 0;
119 
121  virtual int64_t getTotalBytesSent() = 0;
122 
124  virtual size_t getMempoolSize() = 0;
125 
127  virtual size_t getMempoolDynamicUsage() = 0;
128 
130  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
131 
133  virtual int getNumBlocks() = 0;
134 
136  virtual uint256 getBestBlockHash() = 0;
137 
139  virtual int64_t getLastBlockTime() = 0;
140 
142  virtual double getVerificationProgress() = 0;
143 
145  virtual bool isInitialBlockDownload() = 0;
146 
148  virtual bool getReindex() = 0;
149 
151  virtual bool getImporting() = 0;
152 
154  virtual void setNetworkActive(bool active) = 0;
155 
157  virtual bool getNetworkActive() = 0;
158 
160  virtual CFeeRate getDustRelayFee() = 0;
161 
163  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
164 
166  virtual std::vector<std::string> listRpcCommands() = 0;
167 
169  virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
170 
172  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
173 
175  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
176 
178  virtual WalletClient& walletClient() = 0;
179 
181  using InitMessageFn = std::function<void(const std::string& message)>;
182  virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
183 
185  using MessageBoxFn =
186  std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
187  virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
188 
190  using QuestionFn = std::function<bool(const bilingual_str& message,
191  const std::string& non_interactive_message,
192  const std::string& caption,
193  unsigned int style)>;
194  virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
195 
197  using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
198  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
199 
201  using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
202  virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
203 
205  using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
206  virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
207 
209  using NotifyAlertChangedFn = std::function<void()>;
210  virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
211 
213  using BannedListChangedFn = std::function<void()>;
214  virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
215 
217  using NotifyBlockTipFn =
218  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
219  virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
220 
222  using NotifyHeaderTipFn =
223  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
224  virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
225 
228  virtual NodeContext* context() { return nullptr; }
229  virtual void setContext(NodeContext* context) { }
230 };
231 
233 std::unique_ptr<Node> MakeNode(NodeContext* context = nullptr);
234 
236 struct BlockTip {
238  int64_t block_time;
240 };
241 
242 } // namespace interfaces
243 
244 #endif // BITCOIN_INTERFACES_NODE_H
virtual std::unique_ptr< Handler > handleNotifyHeaderTip(NotifyHeaderTipFn fn)=0
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:96
int64_t block_time
Definition: node.h:238
RPC timer "driver".
Definition: server.h:60
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:236
virtual bool isInitialBlockDownload()=0
Is initial block download.
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:102
Definition: banman.h:58
virtual bool getNetworkActive()=0
Get network active.
virtual void initLogging()=0
Init logging.
A UTXO entry.
Definition: coins.h:30
Bilingual messages:
Definition: translation.h:16
virtual double getVerificationProgress()=0
Get verification progress.
virtual size_t getMempoolDynamicUsage()=0
Get mempool dynamic usage.
virtual bool baseInitialize()=0
Initialize app dependencies.
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
virtual bool unban(const CSubNet &ip)=0
Unban node.
virtual void startShutdown()=0
Start shutdown.
virtual UniValue executeRpc(const std::string &command, const UniValue &params, const std::string &uri)=0
Execute rpc command.
virtual void rpcUnsetTimerInterface(RPCTimerInterface *iface)=0
Unset RPC timer interface.
virtual int64_t getTotalBytesRecv()=0
Get total bytes recv.
virtual bool getImporting()=0
Get importing.
std::function< void(bool network_active)> NotifyNetworkActiveChangedFn
Register handler for network active messages.
Definition: node.h:205
virtual size_t getMempoolSize()=0
Get mempool size.
virtual uint32_t getLogCategories()=0
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:218
Wallet chain client that in addition to having chain client methods for starting up, shutting down, and registering RPCs, also has additional methods (called by the GUI) to load and create wallets.
Definition: wallet.h:312
virtual std::unique_ptr< Handler > handleNotifyBlockTip(NotifyBlockTipFn fn)=0
virtual void setContext(NodeContext *context)
Definition: node.h:229
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
virtual bool ban(const CNetAddr &net_addr, int64_t ban_time_offset)=0
Ban node.
virtual size_t getNodeCount(ConnectionDirection flags)=0
Get number of connections.
Coin Control Features.
Definition: coincontrol.h:23
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface)=0
Set RPC timer interface if unset.
virtual std::unique_ptr< Handler > handleMessageBox(MessageBoxFn fn)=0
virtual std::unique_ptr< Handler > handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn)=0
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyHeaderTipFn
Register handler for header tip messages.
Definition: node.h:223
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:209
virtual bilingual_str getWarnings()=0
Get warnings.
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
virtual ~Node()
Definition: node.h:57
virtual bool disconnectByAddress(const CNetAddr &net_addr)=0
Disconnect node by address.
Network
A network type.
Definition: netaddress.h:45
Block and header tip information.
Definition: node.h:44
int64_t NodeId
Definition: net.h:88
virtual std::unique_ptr< Handler > handleBannedListChanged(BannedListChangedFn fn)=0
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
virtual std::vector< ExternalSigner > externalSigners()=0
List external signers.
virtual bool shutdownRequested()=0
Return whether shutdown was requested.
std::function< void(const std::string &message)> InitMessageFn
Register handler for init messages.
Definition: node.h:181
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:213
int flags
Definition: bitcoin-tx.cpp:512
virtual void initParameterInteraction()=0
Init parameter interaction.
std::function< void(int new_num_connections)> NotifyNumConnectionsChangedFn
Register handler for number of connections changed messages.
Definition: node.h:201
Network address.
Definition: netaddress.h:121
virtual bool getReindex()=0
Get reindex.
256-bit opaque blob.
Definition: uint256.h:124
virtual int64_t getLastBlockTime()=0
Get last block time.
virtual void appShutdown()=0
Stop node.
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:193
virtual uint256 getBestBlockHash()=0
Get best block hash.
virtual WalletClient & walletClient()=0
Get wallet client.
virtual std::vector< std::string > listRpcCommands()=0
List rpc commands.
virtual bool getHeaderTip(int &height, int64_t &block_time)=0
Get header tip height and time.
virtual NodeContext * context()
Get and set internal node context.
Definition: node.h:228
virtual bool getNodesStats(NodesStats &stats)=0
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
virtual bool getBanned(banmap_t &banmap)=0
Get ban map entries.
std::function< void(const std::string &title, int progress, bool resume_possible)> ShowProgressFn
Register handler for progress messages.
Definition: node.h:197
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
virtual int getNumBlocks()=0
Get num blocks.
virtual std::unique_ptr< Handler > handleNotifyAlertChanged(NotifyAlertChangedFn 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:186
virtual bool disconnectById(NodeId id)=0
Disconnect node by id.
virtual std::unique_ptr< Handler > handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn)=0
ConnectionDirection
Definition: netbase.h:32
std::unique_ptr< Node > MakeNode(NodeContext *context=nullptr)
Return implementation of Node interface.
Definition: interfaces.cpp:704
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
virtual void setNetworkActive(bool active)=0
Set network active.
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:54
uint256 block_hash
Definition: node.h:239
virtual bool getUnspentOutput(const COutPoint &output, Coin &coin)=0
Get unspent outputs associated with a transaction.
virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo *tip_info=nullptr)=0
Start node.
virtual std::unique_ptr< Handler > handleInitMessage(InitMessageFn fn)=0