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 <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);
234 
236 struct BlockTip {
238  int64_t block_time;
240 };
241 
242 } // namespace interfaces
243 
244 #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:223
interfaces::BlockTip::block_hash
uint256 block_hash
Definition: node.h:239
NodeId
int64_t NodeId
Definition: net.h:87
interfaces
Definition: dummywallet.cpp:11
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:513
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:193
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:228
interfaces::BlockTip::block_time
int64_t block_time
Definition: node.h:238
interfaces::Node::BannedListChangedFn
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:213
interfaces::Node::NotifyBlockTipFn
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:218
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::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:237
ip
static CService ip(uint32_t i)
Definition: denialofservice_tests.cpp:30
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:209
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:229
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:197
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:58
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::MakeNode
std::unique_ptr< Node > MakeNode(NodeContext &context)
Return implementation of Node interface.
Definition: interfaces.cpp:713
interfaces::Node::getWarnings
virtual bilingual_str getWarnings()=0
Get warnings.
SynchronizationState
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:93
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:41
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:201
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:181
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:205
CNodeStats
Definition: net.h:238
interfaces::BlockTip
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:236
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:186
interfaces::Node::mapPort
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.