Bitcoin Core  0.20.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 #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 enum class WalletCreationStatus;
33 struct CNodeStateStats;
34 struct NodeContext;
35 struct bilingual_str;
36 
37 namespace interfaces {
38 class Handler;
39 class Wallet;
40 struct BlockTip;
41 
43 class Node
44 {
45 public:
46  virtual ~Node() {}
47 
49  virtual void initError(const bilingual_str& message) = 0;
50 
52  virtual bool parseParameters(int argc, const char* const argv[], std::string& error) = 0;
53 
55  virtual void forceSetArg(const std::string& arg, const std::string& value) = 0;
56 
58  virtual bool softSetArg(const std::string& arg, const std::string& value) = 0;
59 
61  virtual bool softSetBoolArg(const std::string& arg, bool value) = 0;
62 
64  virtual bool readConfigFiles(std::string& error) = 0;
65 
67  virtual void selectParams(const std::string& network) = 0;
68 
70  virtual uint64_t getAssumedBlockchainSize() = 0;
71 
73  virtual uint64_t getAssumedChainStateSize() = 0;
74 
76  virtual std::string getNetwork() = 0;
77 
79  virtual void initLogging() = 0;
80 
82  virtual void initParameterInteraction() = 0;
83 
85  virtual bilingual_str getWarnings() = 0;
86 
87  // Get log flags.
88  virtual uint32_t getLogCategories() = 0;
89 
91  virtual bool baseInitialize() = 0;
92 
94  virtual bool appInitMain() = 0;
95 
97  virtual void appShutdown() = 0;
98 
100  virtual void startShutdown() = 0;
101 
103  virtual bool shutdownRequested() = 0;
104 
106  virtual void setupServerArgs() = 0;
107 
109  virtual void mapPort(bool use_upnp) = 0;
110 
112  virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
113 
115  virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
116 
118  using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
119  virtual bool getNodesStats(NodesStats& stats) = 0;
120 
122  virtual bool getBanned(banmap_t& banmap) = 0;
123 
125  virtual bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) = 0;
126 
128  virtual bool unban(const CSubNet& ip) = 0;
129 
131  virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
132 
134  virtual bool disconnectById(NodeId id) = 0;
135 
137  virtual int64_t getTotalBytesRecv() = 0;
138 
140  virtual int64_t getTotalBytesSent() = 0;
141 
143  virtual size_t getMempoolSize() = 0;
144 
146  virtual size_t getMempoolDynamicUsage() = 0;
147 
149  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
150 
152  virtual int getNumBlocks() = 0;
153 
155  virtual uint256 getBestBlockHash() = 0;
156 
158  virtual int64_t getLastBlockTime() = 0;
159 
161  virtual double getVerificationProgress() = 0;
162 
164  virtual bool isInitialBlockDownload() = 0;
165 
167  virtual bool getReindex() = 0;
168 
170  virtual bool getImporting() = 0;
171 
173  virtual void setNetworkActive(bool active) = 0;
174 
176  virtual bool getNetworkActive() = 0;
177 
179  virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) = 0;
180 
182  virtual CFeeRate getDustRelayFee() = 0;
183 
185  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
186 
188  virtual std::vector<std::string> listRpcCommands() = 0;
189 
191  virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
192 
194  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
195 
197  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
198 
200  virtual std::string getWalletDir() = 0;
201 
203  virtual std::vector<std::string> listWalletDir() = 0;
204 
206  virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
207 
211  virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
212 
214  virtual std::unique_ptr<Wallet> createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, WalletCreationStatus& status) = 0;
215 
217  using InitMessageFn = std::function<void(const std::string& message)>;
218  virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
219 
221  using MessageBoxFn =
222  std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
223  virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
224 
226  using QuestionFn = std::function<bool(const bilingual_str& message,
227  const std::string& non_interactive_message,
228  const std::string& caption,
229  unsigned int style)>;
230  virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
231 
233  using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
234  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
235 
237  using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
238  virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
239 
241  using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
242  virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
243 
245  using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
246  virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
247 
249  using NotifyAlertChangedFn = std::function<void()>;
250  virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
251 
253  using BannedListChangedFn = std::function<void()>;
254  virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
255 
257  using NotifyBlockTipFn =
258  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
259  virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
260 
262  using NotifyHeaderTipFn =
263  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
264  virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
265 
267  virtual NodeContext* context() { return nullptr; }
268 };
269 
271 std::unique_ptr<Node> MakeNode();
272 
274 struct BlockTip {
276  int64_t block_time;
278 };
279 
280 } // namespace interfaces
281 
282 #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:118
virtual bool appInitMain()=0
Start node.
int64_t block_time
Definition: node.h:276
RPC timer "driver".
Definition: server.h:59
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:274
virtual bool isInitialBlockDownload()=0
Is initial block download.
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:109
Definition: banman.h:41
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.
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 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:245
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 void initError(const bilingual_str &message)=0
Send init error.
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:258
virtual std::unique_ptr< Wallet > loadWallet(const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Attempts to load a wallet from file or directory.
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
WalletCreationStatus
Definition: wallet.h:59
virtual std::unique_ptr< Handler > handleNotifyBlockTip(NotifyBlockTipFn fn)=0
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
Coin Control Features.
Definition: coincontrol.h:22
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
virtual uint64_t getAssumedBlockchainSize()=0
Get the (assumed) blockchain size.
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyHeaderTipFn
Register handler for header tip messages.
Definition: node.h:263
NodeContext struct containing references to chain state and connection state.
Definition: context.h:34
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:249
virtual uint64_t getAssumedChainStateSize()=0
Get the (assumed) chain state size.
virtual std::unique_ptr< Wallet > createWallet(const SecureString &passphrase, uint64_t wallet_creation_flags, const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings, WalletCreationStatus &status)=0
Create a wallet from file.
virtual std::string getWalletDir()=0
Return default wallet directory.
const char * name
Definition: rest.cpp:41
virtual bilingual_str getWarnings()=0
Get warnings.
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:46
virtual bool disconnectByAddress(const CNetAddr &net_addr)=0
Disconnect node by address.
Network
Definition: netaddress.h:19
int64_t NodeId
Definition: net.h:90
NumConnections
Definition: net.h:122
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:237
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:217
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:253
int flags
Definition: bitcoin-tx.cpp:509
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:241
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.
256-bit opaque blob.
Definition: uint256.h:120
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::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:229
virtual uint256 getBestBlockHash()=0
Get best block hash.
std::unique_ptr< Node > MakeNode()
Return implementation of Node interface.
Definition: node.cpp:345
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.
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:267
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:233
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:222
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 bool disconnectById(NodeId id)=0
Disconnect node by id.
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:43
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
uint256 block_hash
Definition: node.h:277
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.