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