Bitcoin Core  22.99.0
P2P Digital Currency
node.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 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 <net.h> // For NodeId
10 #include <net_types.h> // For banmap_t
11 #include <netaddress.h> // For Network
12 #include <netbase.h> // For ConnectionDirection
13 #include <support/allocators/secure.h> // For SecureString
14 #include <util/translation.h>
15 
16 #include <functional>
17 #include <memory>
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <string>
21 #include <tuple>
22 #include <vector>
23 
24 class BanMan;
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 TransactionError;
33 struct CNodeStateStats;
34 struct bilingual_str;
35 namespace node {
36 struct NodeContext;
37 } // namespace node
38 namespace wallet {
39 class CCoinControl;
40 } // namespace wallet
41 
42 namespace interfaces {
43 class Handler;
44 class WalletLoader;
45 struct BlockTip;
46 
49 {
51  int64_t block_time;
53  int64_t header_time;
55 };
56 
59 {
60 public:
61  virtual ~ExternalSigner() {};
62 
64  virtual std::string getName() = 0;
65 };
66 
68 class Node
69 {
70 public:
71  virtual ~Node() {}
72 
74  virtual void initLogging() = 0;
75 
77  virtual void initParameterInteraction() = 0;
78 
80  virtual bilingual_str getWarnings() = 0;
81 
82  // Get log flags.
83  virtual uint32_t getLogCategories() = 0;
84 
86  virtual bool baseInitialize() = 0;
87 
89  virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info = nullptr) = 0;
90 
92  virtual void appShutdown() = 0;
93 
95  virtual void startShutdown() = 0;
96 
98  virtual bool shutdownRequested() = 0;
99 
101  virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
102 
104  virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
105 
107  virtual size_t getNodeCount(ConnectionDirection flags) = 0;
108 
110  using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
111  virtual bool getNodesStats(NodesStats& stats) = 0;
112 
114  virtual bool getBanned(banmap_t& banmap) = 0;
115 
117  virtual bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) = 0;
118 
120  virtual bool unban(const CSubNet& ip) = 0;
121 
123  virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
124 
126  virtual bool disconnectById(NodeId id) = 0;
127 
129  virtual std::vector<std::unique_ptr<ExternalSigner>> listExternalSigners() = 0;
130 
132  virtual int64_t getTotalBytesRecv() = 0;
133 
135  virtual int64_t getTotalBytesSent() = 0;
136 
138  virtual size_t getMempoolSize() = 0;
139 
141  virtual size_t getMempoolDynamicUsage() = 0;
142 
144  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
145 
147  virtual int getNumBlocks() = 0;
148 
150  virtual uint256 getBestBlockHash() = 0;
151 
153  virtual int64_t getLastBlockTime() = 0;
154 
156  virtual double getVerificationProgress() = 0;
157 
159  virtual bool isInitialBlockDownload() = 0;
160 
162  virtual bool getReindex() = 0;
163 
165  virtual bool getImporting() = 0;
166 
168  virtual void setNetworkActive(bool active) = 0;
169 
171  virtual bool getNetworkActive() = 0;
172 
174  virtual CFeeRate getDustRelayFee() = 0;
175 
177  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
178 
180  virtual std::vector<std::string> listRpcCommands() = 0;
181 
183  virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
184 
186  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
187 
189  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
190 
192  virtual TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) = 0;
193 
195  virtual WalletLoader& walletLoader() = 0;
196 
198  using InitMessageFn = std::function<void(const std::string& message)>;
199  virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
200 
202  using MessageBoxFn =
203  std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
204  virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
205 
207  using QuestionFn = std::function<bool(const bilingual_str& message,
208  const std::string& non_interactive_message,
209  const std::string& caption,
210  unsigned int style)>;
211  virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
212 
214  using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
215  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
216 
218  using InitWalletFn = std::function<void()>;
219  virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) = 0;
220 
222  using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
223  virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
224 
226  using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
227  virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
228 
230  using NotifyAlertChangedFn = std::function<void()>;
231  virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
232 
234  using BannedListChangedFn = std::function<void()>;
235  virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
236 
238  using NotifyBlockTipFn =
239  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
240  virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
241 
243  using NotifyHeaderTipFn =
244  std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
245  virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
246 
249  virtual node::NodeContext* context() { return nullptr; }
251 };
252 
254 std::unique_ptr<Node> MakeNode(node::NodeContext& context);
255 
257 struct BlockTip {
259  int64_t block_time;
261 };
262 
263 } // namespace interfaces
264 
265 #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:244
interfaces::BlockTip::block_hash
uint256 block_hash
Definition: node.h:260
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::InitWalletFn
std::function< void()> InitWalletFn
Register handler for wallet loader constructed messages.
Definition: node.h:218
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:529
RPCTimerInterface
RPC timer "driver".
Definition: server.h:59
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
interfaces::Node::handleInitWallet
virtual std::unique_ptr< Handler > handleInitWallet(InitWalletFn fn)=0
node::NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:40
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:210
interfaces::Node::handleNotifyAlertChanged
virtual std::unique_ptr< Handler > handleNotifyAlertChanged(NotifyAlertChangedFn fn)=0
interfaces::BlockAndHeaderTipInfo
Block and header tip information.
Definition: node.h:48
wallet
Definition: node.h:38
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.
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:406
Network
Network
A network type.
Definition: netaddress.h:45
interfaces::Node::setContext
virtual void setContext(node::NodeContext *context)
Definition: node.h:250
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:17
interfaces::Node::appShutdown
virtual void appShutdown()=0
Stop node.
interfaces::BlockAndHeaderTipInfo::block_time
int64_t block_time
Definition: node.h:51
interfaces::BlockTip::block_time
int64_t block_time
Definition: node.h:259
interfaces::Node::BannedListChangedFn
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:234
interfaces::Node::NotifyBlockTipFn
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:239
net_types.h
interfaces::Node::getHeaderTip
virtual bool getHeaderTip(int &height, int64_t &block_time)=0
Get header tip height and time.
TransactionError
TransactionError
Definition: error.h:22
interfaces::Node::NodesStats
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:110
interfaces::ExternalSigner
External signer interface used by the GUI.
Definition: node.h:58
interfaces::BlockTip::block_height
int block_height
Definition: node.h:258
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:50
interfaces::Node::NotifyAlertChangedFn
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:230
interfaces::Node::broadcastTransaction
virtual TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string &err_string)=0
Broadcast transaction.
interfaces::MakeNode
std::unique_ptr< Node > MakeNode(node::NodeContext &context)
Return implementation of Node interface.
Definition: interfaces.cpp:727
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::WalletLoader
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:319
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
context
WalletContext context
Definition: notifications.cpp:37
CSubNet
Definition: netaddress.h:475
interfaces::Node::handleNotifyNumConnectionsChanged
virtual std::unique_ptr< Handler > handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn)=0
interfaces::Node::context
virtual node::NodeContext * context()
Get and set internal node context.
Definition: node.h:249
interfaces::Node::getVerificationProgress
virtual double getVerificationProgress()=0
Get verification progress.
uint256
256-bit opaque blob.
Definition: uint256.h:126
interfaces::ExternalSigner::~ExternalSigner
virtual ~ExternalSigner()
Definition: node.h:61
interfaces::Node::walletLoader
virtual WalletLoader & walletLoader()=0
Get wallet loader.
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:214
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:68
interfaces::BlockAndHeaderTipInfo::header_time
int64_t header_time
Definition: node.h:53
BanMan
Definition: banman.h:58
secure.h
interfaces::BlockAndHeaderTipInfo::verification_progress
double verification_progress
Definition: node.h:54
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:52
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.
node
Definition: init.h:22
SynchronizationState
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:105
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::listExternalSigners
virtual std::vector< std::unique_ptr< ExternalSigner > > listExternalSigners()=0
Return list of external signers (attached devices which can sign transactions).
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.
interfaces::Node::NotifyNumConnectionsChangedFn
std::function< void(int new_num_connections)> NotifyNumConnectionsChangedFn
Register handler for number of connections changed messages.
Definition: node.h:222
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:198
interfaces::Node::executeRpc
virtual UniValue executeRpc(const std::string &command, const UniValue &params, const std::string &uri)=0
Execute rpc command.
wallet::CCoinControl
Coin Control Features.
Definition: coincontrol.h:29
interfaces::ExternalSigner::getName
virtual std::string getName()=0
Get signer display name.
interfaces::Node::NotifyNetworkActiveChangedFn
std::function< void(bool network_active)> NotifyNetworkActiveChangedFn
Register handler for network active messages.
Definition: node.h:226
CNodeStats
Definition: net.h:239
interfaces::BlockTip
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:257
interfaces::Node::getTotalBytesSent
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
interfaces::Node::~Node
virtual ~Node()
Definition: node.h:71
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:203
interfaces::Node::mapPort
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.