Coverage Report

Created: 2024-11-15 12:18

/root/bitcoin/src/interfaces/node.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2018-2022 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 <common/settings.h>
9
#include <consensus/amount.h>          // For CAmount
10
#include <logging.h>                   // For BCLog::CategoryMask
11
#include <net.h>                       // For NodeId
12
#include <net_types.h>                 // For banmap_t
13
#include <netaddress.h>                // For Network
14
#include <netbase.h>                   // For ConnectionDirection
15
#include <support/allocators/secure.h> // For SecureString
16
#include <util/translation.h>
17
18
#include <functional>
19
#include <memory>
20
#include <stddef.h>
21
#include <stdint.h>
22
#include <string>
23
#include <tuple>
24
#include <vector>
25
26
class BanMan;
27
class CFeeRate;
28
class CNodeStats;
29
class Coin;
30
class RPCTimerInterface;
31
class UniValue;
32
class Proxy;
33
enum class SynchronizationState;
34
struct CNodeStateStats;
35
struct bilingual_str;
36
namespace node {
37
enum class TransactionError;
38
struct NodeContext;
39
} // namespace node
40
namespace wallet {
41
class CCoinControl;
42
} // namespace wallet
43
44
namespace interfaces {
45
class Handler;
46
class WalletLoader;
47
struct BlockTip;
48
49
//! Block and header tip information
50
struct BlockAndHeaderTipInfo
51
{
52
    int block_height;
53
    int64_t block_time;
54
    int header_height;
55
    int64_t header_time;
56
    double verification_progress;
57
};
58
59
//! External signer interface used by the GUI.
60
class ExternalSigner
61
{
62
public:
63
    virtual ~ExternalSigner() = default;
64
65
    //! Get signer display name
66
    virtual std::string getName() = 0;
67
};
68
69
//! Top-level interface for a bitcoin node (bitcoind process).
70
class Node
71
{
72
public:
73
0
    virtual ~Node() = default;
74
75
    //! Init logging.
76
    virtual void initLogging() = 0;
77
78
    //! Init parameter interaction.
79
    virtual void initParameterInteraction() = 0;
80
81
    //! Get warnings.
82
    virtual bilingual_str getWarnings() = 0;
83
84
    //! Get exit status.
85
    virtual int getExitStatus() = 0;
86
87
    // Get log flags.
88
    virtual BCLog::CategoryMask getLogCategories() = 0;
89
90
    //! Initialize app dependencies.
91
    virtual bool baseInitialize() = 0;
92
93
    //! Start node.
94
    virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info = nullptr) = 0;
95
96
    //! Stop node.
97
    virtual void appShutdown() = 0;
98
99
    //! Start shutdown.
100
    virtual void startShutdown() = 0;
101
102
    //! Return whether shutdown was requested.
103
    virtual bool shutdownRequested() = 0;
104
105
    //! Return whether a particular setting in <datadir>/settings.json is or
106
    //! would be ignored because it is also specified in the command line.
107
    virtual bool isSettingIgnored(const std::string& name) = 0;
108
109
    //! Return setting value from <datadir>/settings.json or bitcoin.conf.
110
    virtual common::SettingsValue getPersistentSetting(const std::string& name) = 0;
111
112
    //! Update a setting in <datadir>/settings.json.
113
    virtual void updateRwSetting(const std::string& name, const common::SettingsValue& value) = 0;
114
115
    //! Force a setting value to be applied, overriding any other configuration
116
    //! source, but not being persisted.
117
    virtual void forceSetting(const std::string& name, const common::SettingsValue& value) = 0;
118
119
    //! Clear all settings in <datadir>/settings.json and store a backup of
120
    //! previous settings in <datadir>/settings.json.bak.
121
    virtual void resetSettings() = 0;
122
123
    //! Map port.
124
    virtual void mapPort(bool use_pcp) = 0;
125
126
    //! Get proxy.
127
    virtual bool getProxy(Network net, Proxy& proxy_info) = 0;
128
129
    //! Get number of connections.
130
    virtual size_t getNodeCount(ConnectionDirection flags) = 0;
131
132
    //! Get stats for connected nodes.
133
    using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
134
    virtual bool getNodesStats(NodesStats& stats) = 0;
135
136
    //! Get ban map entries.
137
    virtual bool getBanned(banmap_t& banmap) = 0;
138
139
    //! Ban node.
140
    virtual bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) = 0;
141
142
    //! Unban node.
143
    virtual bool unban(const CSubNet& ip) = 0;
144
145
    //! Disconnect node by address.
146
    virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
147
148
    //! Disconnect node by id.
149
    virtual bool disconnectById(NodeId id) = 0;
150
151
    //! Return list of external signers (attached devices which can sign transactions).
152
    virtual std::vector<std::unique_ptr<ExternalSigner>> listExternalSigners() = 0;
153
154
    //! Get total bytes recv.
155
    virtual int64_t getTotalBytesRecv() = 0;
156
157
    //! Get total bytes sent.
158
    virtual int64_t getTotalBytesSent() = 0;
159
160
    //! Get mempool size.
161
    virtual size_t getMempoolSize() = 0;
162
163
    //! Get mempool dynamic usage.
164
    virtual size_t getMempoolDynamicUsage() = 0;
165
166
    //! Get mempool maximum memory usage.
167
    virtual size_t getMempoolMaxUsage() = 0;
168
169
    //! Get header tip height and time.
170
    virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
171
172
    //! Get num blocks.
173
    virtual int getNumBlocks() = 0;
174
175
    //! Get network local addresses.
176
    virtual std::map<CNetAddr, LocalServiceInfo> getNetLocalAddresses() = 0;
177
178
    //! Get best block hash.
179
    virtual uint256 getBestBlockHash() = 0;
180
181
    //! Get last block time.
182
    virtual int64_t getLastBlockTime() = 0;
183
184
    //! Get verification progress.
185
    virtual double getVerificationProgress() = 0;
186
187
    //! Is initial block download.
188
    virtual bool isInitialBlockDownload() = 0;
189
190
    //! Is loading blocks.
191
    virtual bool isLoadingBlocks() = 0;
192
193
    //! Set network active.
194
    virtual void setNetworkActive(bool active) = 0;
195
196
    //! Get network active.
197
    virtual bool getNetworkActive() = 0;
198
199
    //! Get dust relay fee.
200
    virtual CFeeRate getDustRelayFee() = 0;
201
202
    //! Execute rpc command.
203
    virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
204
205
    //! List rpc commands.
206
    virtual std::vector<std::string> listRpcCommands() = 0;
207
208
    //! Set RPC timer interface if unset.
209
    virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
210
211
    //! Unset RPC timer interface.
212
    virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
213
214
    //! Get unspent output associated with a transaction.
215
    virtual std::optional<Coin> getUnspentOutput(const COutPoint& output) = 0;
216
217
    //! Broadcast transaction.
218
    virtual node::TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) = 0;
219
220
    //! Get wallet loader.
221
    virtual WalletLoader& walletLoader() = 0;
222
223
    //! Register handler for init messages.
224
    using InitMessageFn = std::function<void(const std::string& message)>;
225
    virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
226
227
    //! Register handler for message box messages.
228
    using MessageBoxFn =
229
        std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
230
    virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
231
232
    //! Register handler for question messages.
233
    using QuestionFn = std::function<bool(const bilingual_str& message,
234
        const std::string& non_interactive_message,
235
        const std::string& caption,
236
        unsigned int style)>;
237
    virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
238
239
    //! Register handler for progress messages.
240
    using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
241
    virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
242
243
    //! Register handler for wallet loader constructed messages.
244
    using InitWalletFn = std::function<void()>;
245
    virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) = 0;
246
247
    //! Register handler for number of connections changed messages.
248
    using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
249
    virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
250
251
    //! Register handler for network active messages.
252
    using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
253
    virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
254
255
    //! Register handler for notify alert messages.
256
    using NotifyAlertChangedFn = std::function<void()>;
257
    virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
258
259
    //! Register handler for ban list messages.
260
    using BannedListChangedFn = std::function<void()>;
261
    virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
262
263
    //! Register handler for block tip messages.
264
    using NotifyBlockTipFn =
265
        std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
266
    virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
267
268
    //! Register handler for header tip messages.
269
    using NotifyHeaderTipFn =
270
        std::function<void(SynchronizationState, interfaces::BlockTip tip, bool presync)>;
271
    virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
272
273
    //! Get and set internal node context. Useful for testing, but not
274
    //! accessible across processes.
275
0
    virtual node::NodeContext* context() { return nullptr; }
276
0
    virtual void setContext(node::NodeContext* context) { }
277
};
278
279
//! Return implementation of Node interface.
280
std::unique_ptr<Node> MakeNode(node::NodeContext& context);
281
282
//! Block tip (could be a header or not, depends on the subscribed signal).
283
struct BlockTip {
284
    int block_height;
285
    int64_t block_time;
286
    uint256 block_hash;
287
};
288
289
} // namespace interfaces
290
291
#endif // BITCOIN_INTERFACES_NODE_H