Bitcoin Core  0.19.99
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 
14 #include <functional>
15 #include <memory>
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <string>
19 #include <tuple>
20 #include <vector>
21 
22 class BanMan;
23 class CCoinControl;
24 class CFeeRate;
25 class CNodeStats;
26 class Coin;
27 class RPCTimerInterface;
28 class UniValue;
29 class proxyType;
30 struct CNodeStateStats;
31 struct NodeContext;
32 enum class WalletCreationStatus;
33 
34 namespace interfaces {
35 class Handler;
36 class Wallet;
37 
39 class Node
40 {
41 public:
42  virtual ~Node() {}
43 
45  virtual void initError(const std::string& message) = 0;
46 
48  virtual bool parseParameters(int argc, const char* const argv[], std::string& error) = 0;
49 
51  virtual void forceSetArg(const std::string& arg, const std::string& value) = 0;
52 
54  virtual bool softSetArg(const std::string& arg, const std::string& value) = 0;
55 
57  virtual bool softSetBoolArg(const std::string& arg, bool value) = 0;
58 
60  virtual bool readConfigFiles(std::string& error) = 0;
61 
63  virtual void selectParams(const std::string& network) = 0;
64 
66  virtual uint64_t getAssumedBlockchainSize() = 0;
67 
69  virtual uint64_t getAssumedChainStateSize() = 0;
70 
72  virtual std::string getNetwork() = 0;
73 
75  virtual void initLogging() = 0;
76 
78  virtual void initParameterInteraction() = 0;
79 
81  virtual std::string getWarnings() = 0;
82 
83  // Get log flags.
84  virtual uint32_t getLogCategories() = 0;
85 
87  virtual bool baseInitialize() = 0;
88 
90  virtual bool appInitMain() = 0;
91 
93  virtual void appShutdown() = 0;
94 
96  virtual void startShutdown() = 0;
97 
99  virtual bool shutdownRequested() = 0;
100 
102  virtual void setupServerArgs() = 0;
103 
105  virtual void mapPort(bool use_upnp) = 0;
106 
108  virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
109 
111  virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
112 
114  using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
115  virtual bool getNodesStats(NodesStats& stats) = 0;
116 
118  virtual bool getBanned(banmap_t& banmap) = 0;
119 
121  virtual bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) = 0;
122 
124  virtual bool unban(const CSubNet& ip) = 0;
125 
127  virtual bool disconnect(const CNetAddr& net_addr) = 0;
128 
130  virtual bool disconnect(NodeId id) = 0;
131 
133  virtual int64_t getTotalBytesRecv() = 0;
134 
136  virtual int64_t getTotalBytesSent() = 0;
137 
139  virtual size_t getMempoolSize() = 0;
140 
142  virtual size_t getMempoolDynamicUsage() = 0;
143 
145  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
146 
148  virtual int getNumBlocks() = 0;
149 
151  virtual int64_t getLastBlockTime() = 0;
152 
154  virtual double getVerificationProgress() = 0;
155 
157  virtual bool isInitialBlockDownload() = 0;
158 
160  virtual bool getReindex() = 0;
161 
163  virtual bool getImporting() = 0;
164 
166  virtual void setNetworkActive(bool active) = 0;
167 
169  virtual bool getNetworkActive() = 0;
170 
172  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) = 0;
173 
175  virtual CFeeRate getDustRelayFee() = 0;
176 
178  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
179 
181  virtual std::vector<std::string> listRpcCommands() = 0;
182 
184  virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
185 
187  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
188 
190  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
191 
193  virtual std::string getWalletDir() = 0;
194 
196  virtual std::vector<std::string> listWalletDir() = 0;
197 
199  virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
200 
204  virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::vector<std::string>& warnings) = 0;
205 
207  virtual WalletCreationStatus createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::vector<std::string>& warnings, std::unique_ptr<Wallet>& result) = 0;
208 
210  using InitMessageFn = std::function<void(const std::string& message)>;
211  virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
212 
214  using MessageBoxFn =
215  std::function<bool(const std::string& message, const std::string& caption, unsigned int style)>;
216  virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
217 
219  using QuestionFn = std::function<bool(const std::string& message,
220  const std::string& non_interactive_message,
221  const std::string& caption,
222  unsigned int style)>;
223  virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
224 
226  using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
227  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
228 
230  using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
231  virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
232 
234  using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
235  virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
236 
238  using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
239  virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
240 
242  using NotifyAlertChangedFn = std::function<void()>;
243  virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
244 
246  using BannedListChangedFn = std::function<void()>;
247  virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
248 
250  using NotifyBlockTipFn =
251  std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
252  virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
253 
255  using NotifyHeaderTipFn =
256  std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
257  virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
258 
260  virtual NodeContext* context() { return nullptr; }
261 };
262 
264 std::unique_ptr<Node> MakeNode();
265 
266 } // namespace interfaces
267 
268 #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:114
virtual bool appInitMain()=0
Start node.
std::function< bool(const std::string &message, const std::string &non_interactive_message, const std::string &caption, unsigned int style)> QuestionFn
Register handler for question messages.
Definition: node.h:222
virtual void initError(const std::string &message)=0
Send init error.
RPC timer "driver".
Definition: server.h:56
virtual bool isInitialBlockDownload()=0
Is initial block download.
Definition: banman.h:38
virtual bool getNetworkActive()=0
Get network active.
BanReason
Definition: addrdb.h:20
virtual std::string getNetwork()=0
Get network name.
virtual void initLogging()=0
Init logging.
virtual void selectParams(const std::string &network)=0
Choose network parameters.
virtual std::unique_ptr< Wallet > loadWallet(const std::string &name, std::string &error, std::vector< std::string > &warnings)=0
Attempts to load a wallet from file or directory.
virtual WalletCreationStatus createWallet(const SecureString &passphrase, uint64_t wallet_creation_flags, const std::string &name, std::string &error, std::vector< std::string > &warnings, std::unique_ptr< Wallet > &result)=0
Create a wallet from file.
A UTXO entry.
Definition: coins.h:30
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 parseParameters(int argc, const char *const argv[], std::string &error)=0
Set command line arguments.
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 std::vector< std::string > listWalletDir()=0
Return available wallets in wallet directory.
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:238
virtual size_t getMempoolSize()=0
Get mempool size.
virtual uint32_t getLogCategories()=0
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:60
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
WalletCreationStatus
Definition: wallet.h:55
virtual std::unique_ptr< Handler > handleNotifyBlockTip(NotifyBlockTipFn fn)=0
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
Coin Control Features.
Definition: coincontrol.h:22
std::function< void(bool initial_download, int height, int64_t block_time, double verification_progress)> NotifyHeaderTipFn
Register handler for header tip messages.
Definition: node.h:256
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface)=0
Set RPC timer interface if unset.
virtual std::unique_ptr< Handler > handleMessageBox(MessageBoxFn fn)=0
std::function< void(bool initial_download, int height, int64_t block_time, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:251
virtual std::unique_ptr< Handler > handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn)=0
virtual uint64_t getAssumedBlockchainSize()=0
Get the (assumed) blockchain size.
NodeContext struct containing references to chain state and connection state.
Definition: context.h:31
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:242
virtual uint64_t getAssumedChainStateSize()=0
Get the (assumed) chain state size.
virtual std::string getWalletDir()=0
Return default wallet directory.
const char * name
Definition: rest.cpp:40
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, int *returned_target=nullptr)=0
Estimate smart fee.
virtual ~Node()
Definition: node.h:42
Network
Definition: netaddress.h:19
int64_t NodeId
Definition: net.h:93
NumConnections
Definition: net.h:125
virtual std::unique_ptr< Handler > handleBannedListChanged(BannedListChangedFn fn)=0
std::function< void(std::unique_ptr< Wallet > wallet)> LoadWalletFn
Register handler for load wallet messages.
Definition: node.h:230
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
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:210
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:246
int flags
Definition: bitcoin-tx.cpp:508
virtual void initParameterInteraction()=0
Init parameter interaction.
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
virtual bool ban(const CNetAddr &net_addr, BanReason reason, int64_t ban_time_offset)=0
Ban node.
std::function< void(int new_num_connections)> NotifyNumConnectionsChangedFn
Register handler for number of connections changed messages.
Definition: node.h:234
virtual bool readConfigFiles(std::string &error)=0
Load settings from configuration file.
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
virtual bool getReindex()=0
Get reindex.
virtual void setupServerArgs()=0
Setup arguments.
virtual int64_t getLastBlockTime()=0
Get last block time.
virtual void appShutdown()=0
Stop node.
virtual std::unique_ptr< Handler > handleQuestion(QuestionFn fn)=0
virtual void mapPort(bool use_upnp)=0
Map port.
std::unique_ptr< Node > MakeNode()
Return implementation of Node interface.
Definition: node.cpp:327
virtual void forceSetArg(const std::string &arg, const std::string &value)=0
Set a command line argument.
virtual std::vector< std::string > listRpcCommands()=0
List rpc commands.
std::function< bool(const std::string &message, const std::string &caption, unsigned int style)> MessageBoxFn
Register handler for message box messages.
Definition: node.h:215
virtual bool getHeaderTip(int &height, int64_t &block_time)=0
Get header tip height and time.
virtual NodeContext * context()
Return pointer to internal chain interface, useful for testing.
Definition: node.h:260
virtual bool getNodesStats(NodesStats &stats)=0
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
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:226
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
virtual bool disconnect(const CNetAddr &net_addr)=0
Disconnect node by address.
virtual bool softSetArg(const std::string &arg, const std::string &value)=0
Set a command line argument if it doesn&#39;t already have a value.
virtual std::string getWarnings()=0
Get warnings.
virtual std::unique_ptr< Handler > handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn)=0
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
virtual void setNetworkActive(bool active)=0
Set network active.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:39
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
virtual size_t getNodeCount(CConnman::NumConnections flags)=0
Get number of connections.
virtual bool getUnspentOutput(const COutPoint &output, Coin &coin)=0
Get unspent outputs associated with a transaction.
virtual std::unique_ptr< Handler > handleInitMessage(InitMessageFn fn)=0
virtual bool softSetBoolArg(const std::string &arg, bool value)=0
Set a command line boolean argument if it doesn&#39;t already have a value.