Bitcoin Core  21.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 <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 
114 #ifdef ENABLE_EXTERNAL_SIGNER
115  virtual std::vector<ExternalSigner> externalSigners() = 0;
117 #endif
118 
120  virtual int64_t getTotalBytesRecv() = 0;
121 
123  virtual int64_t getTotalBytesSent() = 0;
124 
126  virtual size_t getMempoolSize() = 0;
127 
129  virtual size_t getMempoolDynamicUsage() = 0;
130 
132  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
133 
135  virtual int getNumBlocks() = 0;
136 
138  virtual uint256 getBestBlockHash() = 0;
139 
141  virtual int64_t getLastBlockTime() = 0;
142 
144  virtual double getVerificationProgress() = 0;
145 
147  virtual bool isInitialBlockDownload() = 0;
148 
150  virtual bool getReindex() = 0;
151 
153  virtual bool getImporting() = 0;
154 
156  virtual void setNetworkActive(bool active) = 0;
157 
159  virtual bool getNetworkActive() = 0;
160 
162  virtual CFeeRate getDustRelayFee() = 0;
163 
165  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
166 
168  virtual std::vector<std::string> listRpcCommands() = 0;
169 
171  virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
172 
174  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
175 
177  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
178 
180  virtual WalletClient& walletClient() = 0;
181 
183  using InitMessageFn = std::function<void(const std::string& message)>;
184  virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
185 
187  using MessageBoxFn =
188  std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
189  virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
190 
192  using QuestionFn = std::function<bool(const bilingual_str& message,
193  const std::string& non_interactive_message,
194  const std::string& caption,
195  unsigned int style)>;
196  virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
197 
199  using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
200  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
201 
203  using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
204  virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
205 
207  using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
208  virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
209 
211  using NotifyAlertChangedFn = std::function<void()>;
212  virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
213 
215  using BannedListChangedFn = std::function<void()>;
216  virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
217 
219  using NotifyBlockTipFn =
220  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
221  virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
222 
224  using NotifyHeaderTipFn =
225  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
226  virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
227 
230  virtual NodeContext* context() { return nullptr; }
231  virtual void setContext(NodeContext* context) { }
232 };
233 
235 std::unique_ptr<Node> MakeNode(NodeContext* context = nullptr);
236 
238 struct BlockTip {
240  int64_t block_time;
242 };
243 
244 } // namespace interfaces
245 
246 #endif // BITCOIN_INTERFACES_NODE_H
interfaces::Node::ban
virtual bool ban(const CNetAddr &net_addr, int64_t ban_time_offset)=0
Ban node.
interfaces::Node::NotifyHeaderTipFn
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyHeaderTipFn
Register handler for header tip messages.
Definition: node.h:225
interfaces::BlockTip::block_hash
uint256 block_hash
Definition: node.h:241
NodeId
int64_t NodeId
Definition: net.h:88
interfaces
Definition: dummywallet.cpp:10
interfaces::Node::handleShowProgress
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
interfaces::Node::getNumBlocks
virtual int getNumBlocks()=0
Get num blocks.
interfaces::Node::getLastBlockTime
virtual int64_t getLastBlockTime()=0
Get last block time.
interfaces::Node::handleMessageBox
virtual std::unique_ptr< Handler > handleMessageBox(MessageBoxFn fn)=0
interfaces::Node::getProxy
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
interfaces::Node::initLogging
virtual void initLogging()=0
Init logging.
interfaces::Node::getNetworkActive
virtual bool getNetworkActive()=0
Get network active.
interfaces::Node::handleNotifyNetworkActiveChanged
virtual std::unique_ptr< Handler > handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn)=0
interfaces::Node::handleInitMessage
virtual std::unique_ptr< Handler > handleInitMessage(InitMessageFn fn)=0
flags
int flags
Definition: bitcoin-tx.cpp:512
RPCTimerInterface
RPC timer "driver".
Definition: server.h:60
interfaces::Node::disconnectById
virtual bool disconnectById(NodeId id)=0
Disconnect node by id.
interfaces::Node::handleNotifyHeaderTip
virtual std::unique_ptr< Handler > handleNotifyHeaderTip(NotifyHeaderTipFn fn)=0
bilingual_str
Bilingual messages:
Definition: translation.h:16
CNetAddr
Network address.
Definition: netaddress.h:118
interfaces::Node::setNetworkActive
virtual void setNetworkActive(bool active)=0
Set network active.
interfaces::Node::handleNotifyBlockTip
virtual std::unique_ptr< Handler > handleNotifyBlockTip(NotifyBlockTipFn fn)=0
CCoinControl
Coin Control Features.
Definition: coincontrol.h:23
interfaces::Node::getReindex
virtual bool getReindex()=0
Get reindex.
interfaces::Node::QuestionFn
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:195
interfaces::Node::handleNotifyAlertChanged
virtual std::unique_ptr< Handler > handleNotifyAlertChanged(NotifyAlertChangedFn fn)=0
interfaces::BlockAndHeaderTipInfo
Block and header tip information.
Definition: node.h:44
interfaces::Node::getNodeCount
virtual size_t getNodeCount(ConnectionDirection flags)=0
Get number of connections.
interfaces::Node::disconnectByAddress
virtual bool disconnectByAddress(const CNetAddr &net_addr)=0
Disconnect node by address.
interfaces::Node::getUnspentOutput
virtual bool getUnspentOutput(const COutPoint &output, Coin &coin)=0
Get unspent outputs associated with a transaction.
Network
Network
A network type.
Definition: netaddress.h:45
interfaces::Node::handleBannedListChanged
virtual std::unique_ptr< Handler > handleBannedListChanged(BannedListChangedFn fn)=0
proxyType
Definition: netbase.h:48
CFeeRate
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
UniValue
Definition: univalue.h:19
interfaces::Node::appShutdown
virtual void appShutdown()=0
Stop node.
interfaces::BlockAndHeaderTipInfo::block_time
int64_t block_time
Definition: node.h:47
interfaces::Node::context
virtual NodeContext * context()
Get and set internal node context.
Definition: node.h:230
interfaces::BlockTip::block_time
int64_t block_time
Definition: node.h:240
interfaces::Node::BannedListChangedFn
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:215
interfaces::Node::NotifyBlockTipFn
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:220
net_types.h
interfaces::Node::getHeaderTip
virtual bool getHeaderTip(int &height, int64_t &block_time)=0
Get header tip height and time.
external_signer.h
interfaces::MakeNode
std::unique_ptr< Node > MakeNode(NodeContext *context=nullptr)
Return implementation of Node interface.
Definition: interfaces.cpp:694
interfaces::Node::NodesStats
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:96
interfaces::BlockTip::block_height
int block_height
Definition: node.h:239
Coin
A UTXO entry.
Definition: coins.h:30
interfaces::BlockAndHeaderTipInfo::block_height
int block_height
Definition: node.h:46
interfaces::Node::NotifyAlertChangedFn
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:211
netaddress.h
interfaces::Node::startShutdown
virtual void startShutdown()=0
Start shutdown.
interfaces::Node::listRpcCommands
virtual std::vector< std::string > listRpcCommands()=0
List rpc commands.
interfaces::Node::walletClient
virtual WalletClient & walletClient()=0
Get wallet client.
interfaces::Node::externalSigners
virtual std::vector< ExternalSigner > externalSigners()=0
List external signers.
interfaces::Node::setContext
virtual void setContext(NodeContext *context)
Definition: node.h:231
CSubNet
Definition: netaddress.h:486
interfaces::Node::handleNotifyNumConnectionsChanged
virtual std::unique_ptr< Handler > handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn)=0
interfaces::Node::getVerificationProgress
virtual double getVerificationProgress()=0
Get verification progress.
interfaces::WalletClient
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:312
uint256
256-bit opaque blob.
Definition: uint256.h:124
interfaces::Node::isInitialBlockDownload
virtual bool isInitialBlockDownload()=0
Is initial block download.
interfaces::Node::unban
virtual bool unban(const CSubNet &ip)=0
Unban node.
interfaces::Node::ShowProgressFn
std::function< void(const std::string &title, int progress, bool resume_possible)> ShowProgressFn
Register handler for progress messages.
Definition: node.h:199
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:54
interfaces::BlockAndHeaderTipInfo::header_time
int64_t header_time
Definition: node.h:49
BanMan
Definition: banman.h:57
secure.h
interfaces::BlockAndHeaderTipInfo::verification_progress
double verification_progress
Definition: node.h:50
interfaces::Node::getNodesStats
virtual bool getNodesStats(NodesStats &stats)=0
interfaces::Node::getDustRelayFee
virtual CFeeRate getDustRelayFee()=0
Get dust relay fee.
interfaces::BlockAndHeaderTipInfo::header_height
int header_height
Definition: node.h:48
interfaces::Node::shutdownRequested
virtual bool shutdownRequested()=0
Return whether shutdown was requested.
translation.h
interfaces::Node::getLogCategories
virtual uint32_t getLogCategories()=0
interfaces::Node::getMempoolDynamicUsage
virtual size_t getMempoolDynamicUsage()=0
Get mempool dynamic usage.
interfaces::Node::rpcSetTimerInterfaceIfUnset
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface)=0
Set RPC timer interface if unset.
interfaces::Node::getWarnings
virtual bilingual_str getWarnings()=0
Get warnings.
SynchronizationState
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:103
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
interfaces::Node::rpcUnsetTimerInterface
virtual void rpcUnsetTimerInterface(RPCTimerInterface *iface)=0
Unset RPC timer interface.
netbase.h
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
interfaces::Node::getImporting
virtual bool getImporting()=0
Get importing.
ConnectionDirection
ConnectionDirection
Definition: netbase.h:32
interfaces::Node::getTotalBytesRecv
virtual int64_t getTotalBytesRecv()=0
Get total bytes recv.
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
interfaces::Node::NotifyNumConnectionsChangedFn
std::function< void(int new_num_connections)> NotifyNumConnectionsChangedFn
Register handler for number of connections changed messages.
Definition: node.h:203
CNodeStateStats
Definition: net_processing.h:26
interfaces::Node::getBestBlockHash
virtual uint256 getBestBlockHash()=0
Get best block hash.
interfaces::Node::getMempoolSize
virtual size_t getMempoolSize()=0
Get mempool size.
interfaces::Node::handleQuestion
virtual std::unique_ptr< Handler > handleQuestion(QuestionFn fn)=0
amount.h
interfaces::Node::InitMessageFn
std::function< void(const std::string &message)> InitMessageFn
Register handler for init messages.
Definition: node.h:183
interfaces::Node::executeRpc
virtual UniValue executeRpc(const std::string &command, const UniValue &params, const std::string &uri)=0
Execute rpc command.
interfaces::Node::NotifyNetworkActiveChangedFn
std::function< void(bool network_active)> NotifyNetworkActiveChangedFn
Register handler for network active messages.
Definition: node.h:207
CNodeStats
Definition: net.h:239
interfaces::BlockTip
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:238
interfaces::Node::getTotalBytesSent
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
interfaces::Node::~Node
virtual ~Node()
Definition: node.h:57
interfaces::Node::appInitMain
virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo *tip_info=nullptr)=0
Start node.
interfaces::Node::baseInitialize
virtual bool baseInitialize()=0
Initialize app dependencies.
interfaces::Node::initParameterInteraction
virtual void initParameterInteraction()=0
Init parameter interaction.
interfaces::Node::getBanned
virtual bool getBanned(banmap_t &banmap)=0
Get ban map entries.
interfaces::Node::MessageBoxFn
std::function< bool(const bilingual_str &message, const std::string &caption, unsigned int style)> MessageBoxFn
Register handler for message box messages.
Definition: node.h:188
interfaces::Node::mapPort
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.