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