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