Bitcoin Core 22.99.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 <consensus/amount.h>
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
25class BanMan;
26class CCoinControl;
27class CFeeRate;
28class CNodeStats;
29class Coin;
31class UniValue;
32class proxyType;
33enum class SynchronizationState;
34struct CNodeStateStats;
35struct NodeContext;
36struct bilingual_str;
37
38namespace interfaces {
39class Handler;
40class WalletClient;
41struct BlockTip;
42
45{
47 int64_t block_time;
49 int64_t header_time;
51};
52
54class Node
55{
56public:
57 virtual ~Node() {}
58
60 virtual void initLogging() = 0;
61
63 virtual void initParameterInteraction() = 0;
64
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
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
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
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
170
173
175 virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
176
179
181 using InitMessageFn = std::function<void(const std::string& message)>;
182 virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
183
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
218 std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
219 virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
220
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
233std::unique_ptr<Node> MakeNode(NodeContext& context);
234
236struct BlockTip {
238 int64_t block_time;
240};
241
242} // namespace interfaces
243
244#endif // BITCOIN_INTERFACES_NODE_H
int flags
Definition: bitcoin-tx.cpp:525
Definition: banman.h:59
Coin Control Features.
Definition: coincontrol.h:29
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
Network address.
Definition: netaddress.h:119
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:60
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:55
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:213
virtual void setContext(NodeContext *context)
Definition: node.h:229
virtual bool disconnectById(NodeId id)=0
Disconnect node by id.
virtual bool baseInitialize()=0
Initialize app dependencies.
virtual bool getImporting()=0
Get importing.
virtual std::unique_ptr< Handler > handleInitMessage(InitMessageFn fn)=0
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:205
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
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:223
virtual bilingual_str getWarnings()=0
Get warnings.
virtual std::vector< ExternalSigner > externalSigners()=0
List external signers.
virtual std::vector< std::string > listRpcCommands()=0
List rpc commands.
virtual void appShutdown()=0
Stop node.
std::function< void(const std::string &message)> InitMessageFn
Register handler for init messages.
Definition: node.h:181
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
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 std::unique_ptr< Handler > handleNotifyBlockTip(NotifyBlockTipFn fn)=0
virtual void rpcUnsetTimerInterface(RPCTimerInterface *iface)=0
Unset RPC timer interface.
virtual std::unique_ptr< Handler > handleNotifyAlertChanged(NotifyAlertChangedFn fn)=0
virtual std::unique_ptr< Handler > handleNotifyHeaderTip(NotifyHeaderTipFn fn)=0
virtual std::unique_ptr< Handler > handleMessageBox(MessageBoxFn fn)=0
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:96
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 bool getReindex()=0
Get reindex.
virtual void initParameterInteraction()=0
Init parameter interaction.
virtual void startShutdown()=0
Start shutdown.
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:209
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 bool appInitMain(interfaces::BlockAndHeaderTipInfo *tip_info=nullptr)=0
Start node.
virtual std::unique_ptr< Handler > handleQuestion(QuestionFn fn)=0
virtual size_t getNodeCount(ConnectionDirection 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:57
virtual bool disconnectByAddress(const CNetAddr &net_addr)=0
Disconnect node by address.
virtual NodeContext * context()
Get and set internal node context.
Definition: node.h:228
virtual int64_t getTotalBytesRecv()=0
Get total bytes recv.
virtual std::unique_ptr< Handler > handleBannedListChanged(BannedListChangedFn fn)=0
virtual WalletClient & walletClient()=0
Get wallet client.
std::function< void(const std::string &title, int progress, bool resume_possible)> ShowProgressFn
Register handler for progress messages.
Definition: node.h:197
virtual void initLogging()=0
Init logging.
virtual std::unique_ptr< Handler > handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn)=0
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
virtual size_t getMempoolDynamicUsage()=0
Get mempool dynamic usage.
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.
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:201
virtual bool getUnspentOutput(const COutPoint &output, Coin &coin)=0
Get unspent outputs associated with a transaction.
virtual std::unique_ptr< Handler > handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn)=0
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:218
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:313
256-bit opaque blob.
Definition: uint256.h:124
static CService ip(uint32_t i)
std::unique_ptr< Node > MakeNode(NodeContext &context)
Return implementation of Node interface.
Definition: interfaces.cpp:713
int64_t NodeId
Definition: net.h:87
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:41
Network
A network type.
Definition: netaddress.h:45
ConnectionDirection
Definition: netbase.h:32
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
Bilingual messages:
Definition: translation.h:16
Block and header tip information.
Definition: node.h:45
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:236
uint256 block_hash
Definition: node.h:239
int64_t block_time
Definition: node.h:238
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:93