Bitcoin Core  23.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> // For CAmount
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/settings.h> // For util::SettingsValue
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 CFeeRate;
27 class CNodeStats;
28 class Coin;
29 class RPCTimerInterface;
30 class UniValue;
31 class Proxy;
32 enum class SynchronizationState;
33 enum class TransactionError;
34 struct CNodeStateStats;
35 struct bilingual_str;
36 namespace node {
37 struct NodeContext;
38 } // namespace node
39 namespace wallet {
40 class CCoinControl;
41 } // namespace wallet
42 
43 namespace interfaces {
44 class Handler;
45 class WalletLoader;
46 struct BlockTip;
47 
50 {
52  int64_t block_time;
54  int64_t header_time;
56 };
57 
60 {
61 public:
62  virtual ~ExternalSigner() {};
63 
65  virtual std::string getName() = 0;
66 };
67 
69 class Node
70 {
71 public:
72  virtual ~Node() {}
73 
75  virtual void initLogging() = 0;
76 
78  virtual void initParameterInteraction() = 0;
79 
81  virtual bilingual_str getWarnings() = 0;
82 
83  // Get log flags.
84  virtual uint32_t getLogCategories() = 0;
85 
87  virtual bool baseInitialize() = 0;
88 
90  virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info = nullptr) = 0;
91 
93  virtual void appShutdown() = 0;
94 
96  virtual void startShutdown() = 0;
97 
99  virtual bool shutdownRequested() = 0;
100 
103  virtual bool isSettingIgnored(const std::string& name) = 0;
104 
106  virtual util::SettingsValue getPersistentSetting(const std::string& name) = 0;
107 
109  virtual void updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
110 
113  virtual void forceSetting(const std::string& name, const util::SettingsValue& value) = 0;
114 
117  virtual void resetSettings() = 0;
118 
120  virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
121 
123  virtual bool getProxy(Network net, Proxy& proxy_info) = 0;
124 
126  virtual size_t getNodeCount(ConnectionDirection flags) = 0;
127 
129  using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
130  virtual bool getNodesStats(NodesStats& stats) = 0;
131 
133  virtual bool getBanned(banmap_t& banmap) = 0;
134 
136  virtual bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) = 0;
137 
139  virtual bool unban(const CSubNet& ip) = 0;
140 
142  virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
143 
145  virtual bool disconnectById(NodeId id) = 0;
146 
148  virtual std::vector<std::unique_ptr<ExternalSigner>> listExternalSigners() = 0;
149 
151  virtual int64_t getTotalBytesRecv() = 0;
152 
154  virtual int64_t getTotalBytesSent() = 0;
155 
157  virtual size_t getMempoolSize() = 0;
158 
160  virtual size_t getMempoolDynamicUsage() = 0;
161 
163  virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
164 
166  virtual int getNumBlocks() = 0;
167 
169  virtual uint256 getBestBlockHash() = 0;
170 
172  virtual int64_t getLastBlockTime() = 0;
173 
175  virtual double getVerificationProgress() = 0;
176 
178  virtual bool isInitialBlockDownload() = 0;
179 
181  virtual bool getReindex() = 0;
182 
184  virtual bool getImporting() = 0;
185 
187  virtual void setNetworkActive(bool active) = 0;
188 
190  virtual bool getNetworkActive() = 0;
191 
193  virtual CFeeRate getDustRelayFee() = 0;
194 
196  virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
197 
199  virtual std::vector<std::string> listRpcCommands() = 0;
200 
202  virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
203 
205  virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
206 
208  virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
209 
211  virtual TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) = 0;
212 
214  virtual WalletLoader& walletLoader() = 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 InitWalletFn = std::function<void()>;
238  virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn 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 
268  virtual node::NodeContext* context() { return nullptr; }
270 };
271 
273 std::unique_ptr<Node> MakeNode(node::NodeContext& context);
274 
276 struct BlockTip {
278  int64_t block_time;
280 };
281 
282 } // namespace interfaces
283 
284 #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:263
interfaces::BlockTip::block_hash
uint256 block_hash
Definition: node.h:279
NodeId
int64_t NodeId
Definition: net.h:92
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:237
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::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:526
RPCTimerInterface
RPC timer "driver".
Definition: server.h:59
interfaces::Node::updateRwSetting
virtual void updateRwSetting(const std::string &name, const util::SettingsValue &value)=0
Update a setting in <datadir>/settings.json.
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:18
CNetAddr
Network address.
Definition: netaddress.h:117
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:43
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:229
interfaces::Node::handleNotifyAlertChanged
virtual std::unique_ptr< Handler > handleNotifyAlertChanged(NotifyAlertChangedFn fn)=0
interfaces::BlockAndHeaderTipInfo
Block and header tip information.
Definition: node.h:49
wallet
Definition: node.h:39
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:414
Network
Network
A network type.
Definition: netaddress.h:44
interfaces::Node::setContext
virtual void setContext(node::NodeContext *context)
Definition: node.h:269
interfaces::Node::handleBannedListChanged
virtual std::unique_ptr< Handler > handleBannedListChanged(BannedListChangedFn fn)=0
CFeeRate
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
UniValue
Definition: univalue.h:18
interfaces::Node::appShutdown
virtual void appShutdown()=0
Stop node.
interfaces::BlockAndHeaderTipInfo::block_time
int64_t block_time
Definition: node.h:52
interfaces::BlockTip::block_time
int64_t block_time
Definition: node.h:278
interfaces::Node::forceSetting
virtual void forceSetting(const std::string &name, const util::SettingsValue &value)=0
Force a setting value to be applied, overriding any other configuration source, but not being persist...
interfaces::Node::BannedListChangedFn
std::function< void()> BannedListChangedFn
Register handler for ban list messages.
Definition: node.h:253
interfaces::Node::NotifyBlockTipFn
std::function< void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)> NotifyBlockTipFn
Register handler for block tip messages.
Definition: node.h:258
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
settings.h
Proxy
Definition: netbase.h:48
interfaces::Node::NodesStats
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:129
interfaces::ExternalSigner
External signer interface used by the GUI.
Definition: node.h:59
interfaces::BlockTip::block_height
int block_height
Definition: node.h:277
ip
static CService ip(uint32_t i)
Definition: denialofservice_tests.cpp:29
Coin
A UTXO entry.
Definition: coins.h:30
interfaces::BlockAndHeaderTipInfo::block_height
int block_height
Definition: node.h:51
interfaces::Node::NotifyAlertChangedFn
std::function< void()> NotifyAlertChangedFn
Register handler for notify alert messages.
Definition: node.h:249
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:784
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::getPersistentSetting
virtual util::SettingsValue getPersistentSetting(const std::string &name)=0
Return setting value from <datadir>/settings.json or bitcoin.conf.
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:467
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:268
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:62
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:233
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:69
interfaces::BlockAndHeaderTipInfo::header_time
int64_t header_time
Definition: node.h:54
BanMan
Definition: banman.h:58
secure.h
interfaces::BlockAndHeaderTipInfo::verification_progress
double verification_progress
Definition: node.h:55
name
const char * name
Definition: rest.cpp:46
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:53
command
const auto command
Definition: bitcoin-wallet.cpp:118
interfaces::Node::shutdownRequested
virtual bool shutdownRequested()=0
Return whether shutdown was requested.
interfaces::Node::resetSettings
virtual void resetSettings()=0
Clear all settings in <datadir>/settings.json and store a backup of previous settings in <datadir>/se...
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:25
SynchronizationState
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:88
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:34
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::getProxy
virtual bool getProxy(Network net, Proxy &proxy_info)=0
Get proxy.
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:241
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:217
interfaces::Node::isSettingIgnored
virtual bool isSettingIgnored(const std::string &name)=0
Return whether a particular setting in <datadir>/settings.json is or would be ignored because it is a...
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:245
CNodeStats
Definition: net.h:186
interfaces::BlockTip
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:276
interfaces::Node::getTotalBytesSent
virtual int64_t getTotalBytesSent()=0
Get total bytes sent.
interfaces::Node::~Node
virtual ~Node()
Definition: node.h:72
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:222
interfaces::Node::mapPort
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.