Line data Source code
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 <saltedhasher.h> // For StaticSaltedHasher
14 : #include <support/allocators/secure.h> // For SecureString
15 : #include <uint256.h>
16 : #include <util/settings.h> // For util::SettingsValue
17 : #include <util/translation.h>
18 :
19 : #include <evo/types.h>
20 :
21 : #include <functional>
22 : #include <memory>
23 : #include <stddef.h>
24 : #include <stdint.h>
25 : #include <string>
26 : #include <tuple>
27 : #include <unordered_set>
28 : #include <vector>
29 :
30 : class BanMan;
31 : class CBlockIndex;
32 : class CDeterministicMNList;
33 : class CFeeRate;
34 : class CGovernanceObject;
35 : class CGovernanceVote;
36 : class CKeyID;
37 : class CNodeStats;
38 : class Coin;
39 : class CScript;
40 : class CService;
41 : class RPCTimerInterface;
42 : class UniValue;
43 : class Proxy;
44 : enum class SynchronizationState;
45 : enum class TransactionError;
46 : enum vote_signal_enum_t : int;
47 : enum class MnType : uint16_t;
48 : struct bilingual_str;
49 : struct CNodeStateStats;
50 : namespace node {
51 : struct NodeContext;
52 : } // namespace node
53 : namespace wallet {
54 : class CCoinControl;
55 : } // namespace wallet
56 :
57 : namespace interfaces {
58 : class Handler;
59 : class Wallet; // forward declaration for type-safe wallet parameter
60 : class WalletLoader;
61 : namespace CoinJoin {
62 : class Loader;
63 : } // namespace CoinJoin
64 : struct BlockTip;
65 :
66 : //! Interface for a masternode entry
67 : class MnEntry
68 : {
69 : public:
70 0 : MnEntry(const CDeterministicMNCPtr& dmn) {}
71 0 : virtual ~MnEntry() {}
72 :
73 : MnEntry() = delete;
74 :
75 : virtual bool isBanned() const = 0;
76 : virtual CService getNetInfoPrimary() const = 0;
77 : virtual MnType getType() const = 0;
78 : virtual UniValue toJson() const = 0;
79 : virtual const CKeyID& getKeyIdOwner() const = 0;
80 : virtual const CKeyID& getKeyIdVoting() const = 0;
81 : virtual const COutPoint& getCollateralOutpoint() const = 0;
82 : virtual const CScript& getScriptPayout() const = 0;
83 : virtual const CScript& getScriptOperatorPayout() const = 0;
84 : virtual const int32_t& getLastPaidHeight() const = 0;
85 : virtual const int32_t& getPoSePenalty() const = 0;
86 : virtual const int32_t& getRegisteredHeight() const = 0;
87 : virtual const uint16_t& getOperatorReward() const = 0;
88 : virtual const uint256& getProTxHash() const = 0;
89 : };
90 :
91 : using MnEntryCPtr = std::shared_ptr<const MnEntry>;
92 :
93 : //! Interface for a list of masternode entries
94 : class MnList
95 : {
96 : public:
97 0 : MnList(const CDeterministicMNList& mn_list) {}
98 0 : virtual ~MnList() {}
99 :
100 : MnList() = delete;
101 :
102 : struct Counts {
103 : size_t m_total_evo{0};
104 : size_t m_total_mn{0};
105 : size_t m_total_weighted{0};
106 : size_t m_valid_evo{0};
107 : size_t m_valid_mn{0};
108 : size_t m_valid_weighted{0};
109 :
110 : [[nodiscard]] size_t total() const { return m_total_mn + m_total_evo; }
111 : [[nodiscard]] size_t enabled() const { return m_valid_mn + m_valid_evo; }
112 : };
113 : virtual Counts getCounts() const = 0;
114 : virtual int32_t getHeight() const = 0;
115 : virtual uint256 getBlockHash() const = 0;
116 :
117 : virtual void forEachMN(bool only_valid, std::function<void(const MnEntryCPtr&)> cb) const = 0;
118 : virtual std::vector<MnEntryCPtr> getProjectedMNPayees(const CBlockIndex* pindex) const = 0;
119 :
120 : virtual void setContext(node::NodeContext* context) = 0;
121 : };
122 :
123 : using MnListPtr = std::shared_ptr<MnList>;
124 :
125 : //! Interface for the src/evo part of a dash node (dashd process).
126 : class EVO
127 : {
128 : public:
129 0 : virtual ~EVO() {}
130 : virtual std::pair<MnListPtr, const CBlockIndex*> getListAtChainTip() = 0;
131 0 : virtual void setContext(node::NodeContext* context) {}
132 : };
133 :
134 : //! Interface for the src/governance part of a dash node (dashd process).
135 : class GOV
136 : {
137 : public:
138 0 : virtual ~GOV() {}
139 : virtual void getAllNewerThan(std::vector<CGovernanceObject> &objs, int64_t nMoreThanTime, bool include_postponed = false) = 0;
140 0 : struct Votes {
141 0 : int32_t m_abs{0};
142 0 : int32_t m_no{0};
143 0 : int32_t m_yes{0};
144 : };
145 : virtual Votes getObjVotes(const CGovernanceObject& obj, vote_signal_enum_t vote_signal) = 0;
146 : struct UniqueVoters {
147 : uint16_t m_regular{0};
148 : uint16_t m_evo{0};
149 : };
150 : virtual UniqueVoters getObjUniqueVoters(const CGovernanceObject& obj, vote_signal_enum_t vote_signal) = 0;
151 : virtual bool existsObj(const uint256& hash) = 0;
152 : virtual bool isEnabled() = 0;
153 : virtual bool processVoteAndRelay(const CGovernanceVote& vote, std::string& error) = 0;
154 0 : struct GovernanceInfo {
155 0 : CAmount proposalfee{0};
156 0 : int superblockcycle{0};
157 0 : int superblockmaturitywindow{0};
158 0 : int lastsuperblock{0};
159 0 : int nextsuperblock{0};
160 0 : int fundingthreshold{0};
161 0 : CAmount governancebudget{0};
162 0 : int64_t targetSpacing{0};
163 0 : int relayRequiredConfs{1};
164 0 : int requiredConfs{6};
165 : };
166 : virtual GovernanceInfo getGovernanceInfo() = 0;
167 : virtual std::optional<int32_t> getProposalFundedHeight(const uint256& proposal_hash) = 0;
168 0 : struct FundableResult {
169 : std::unordered_set<uint256, StaticSaltedHasher> hashes;
170 0 : CAmount allocated{0};
171 : };
172 : virtual FundableResult getFundableProposalHashes() = 0;
173 : virtual std::optional<CGovernanceObject> createProposal(int32_t revision, int64_t created_time,
174 : const std::string& data_hex, std::string& error) = 0;
175 : virtual bool submitProposal(const uint256& parent, int32_t revision, int64_t created_time, const std::string& data_hex,
176 : const uint256& fee_txid, std::string& out_object_hash, std::string& error) = 0;
177 0 : virtual void setContext(node::NodeContext* context) {}
178 : };
179 :
180 : //! Interface for the src/llmq part of a dash node (dashd process).
181 : class LLMQ
182 : {
183 : public:
184 0 : virtual ~LLMQ() {}
185 : struct ChainLockInfo {
186 : int32_t m_height{0};
187 : int64_t m_block_time{0};
188 : uint256 m_hash{};
189 : };
190 : virtual ChainLockInfo getBestChainLock() = 0;
191 : struct CreditPoolCounts {
192 : CAmount m_diff{0};
193 : CAmount m_limit{0};
194 : CAmount m_locked{0};
195 : };
196 : virtual CreditPoolCounts getCreditPoolCounts() = 0;
197 : struct InstantSendCounts {
198 : size_t m_verified{0};
199 : size_t m_unverified{0};
200 : size_t m_awaiting_tx{0};
201 : size_t m_unprotected_tx{0};
202 : };
203 : virtual InstantSendCounts getInstantSendCounts() = 0;
204 : virtual size_t getPendingAssetUnlocks() = 0;
205 : struct QuorumInfo {
206 : std::string m_name;
207 : size_t m_count{0};
208 : double m_health{0.0};
209 : bool m_rotates{false};
210 : int32_t m_data_retention_blocks{0};
211 : int32_t m_newest_height{0};
212 : int32_t m_expiry_height{0};
213 : };
214 : virtual std::vector<QuorumInfo> getQuorumStats() = 0;
215 0 : virtual void setContext(node::NodeContext* context) {}
216 : };
217 :
218 : //! Interface for the src/masternode part of a dash node (dashd process).
219 : namespace Masternode
220 : {
221 : class Sync
222 : {
223 : public:
224 0 : virtual ~Sync() {}
225 : virtual bool isBlockchainSynced() = 0;
226 : virtual bool isGovernanceSynced() = 0;
227 : virtual bool isSynced() = 0;
228 : virtual std::string getSyncStatus() = 0;
229 0 : virtual void setContext(node::NodeContext* context) {}
230 : };
231 : }
232 :
233 : namespace CoinJoin {
234 : //! Interface for the global coinjoin options in src/coinjoin
235 : class Options
236 : {
237 : public:
238 : virtual int getSessions() = 0;
239 : virtual int getRounds() = 0;
240 : virtual int getAmount() = 0;
241 : virtual int getDenomsGoal() = 0;
242 : virtual int getDenomsHardCap() = 0;
243 :
244 : virtual void setEnabled(bool fEnabled) = 0;
245 : virtual void setMultiSessionEnabled(bool fEnabled) = 0;
246 : virtual void setSessions(int sessions) = 0;
247 : virtual void setRounds(int nRounds) = 0;
248 : virtual void setAmount(CAmount amount) = 0;
249 : virtual void setDenomsGoal(int denoms_goal) = 0;
250 : virtual void setDenomsHardCap(int denoms_hardcap) = 0;
251 :
252 : virtual bool isMultiSessionEnabled() = 0;
253 : virtual bool isEnabled() = 0;
254 : // Static helpers
255 : virtual bool isCollateralAmount(CAmount nAmount) = 0;
256 : virtual CAmount getMinCollateralAmount() = 0;
257 : virtual CAmount getMaxCollateralAmount() = 0;
258 : virtual CAmount getSmallestDenomination() = 0;
259 : virtual bool isDenominated(CAmount nAmount) = 0;
260 : virtual std::array<CAmount, 5> getStandardDenominations() = 0;
261 : };
262 : }
263 :
264 : //! Block and header tip information
265 : struct BlockAndHeaderTipInfo
266 : {
267 : int block_height;
268 : int64_t block_time;
269 : uint256 block_hash;
270 : int header_height;
271 : int64_t header_time;
272 : double verification_progress;
273 : };
274 :
275 : //! External signer interface used by the GUI.
276 : class ExternalSigner
277 : {
278 : public:
279 0 : virtual ~ExternalSigner() {};
280 :
281 : //! Get signer display name
282 : virtual std::string getName() = 0;
283 : };
284 :
285 : //! Top-level interface for a dash node (dashd process).
286 : class Node
287 : {
288 : public:
289 0 : virtual ~Node() {}
290 :
291 : //! Init logging.
292 : virtual void initLogging() = 0;
293 :
294 : //! Init parameter interaction.
295 : virtual void initParameterInteraction() = 0;
296 :
297 : //! Get warnings.
298 : virtual bilingual_str getWarnings() = 0;
299 :
300 : // Get log flags.
301 : virtual uint64_t getLogCategories() = 0;
302 :
303 : //! Initialize app dependencies.
304 : virtual bool baseInitialize() = 0;
305 :
306 : //! Start node.
307 : virtual bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info = nullptr) = 0;
308 :
309 : //! Stop node.
310 : virtual void appShutdown() = 0;
311 :
312 : //! Prepare shutdown.
313 : virtual void appPrepareShutdown() = 0;
314 :
315 : //! Start shutdown.
316 : virtual void startShutdown() = 0;
317 :
318 : //! Return whether shutdown was requested.
319 : virtual bool shutdownRequested() = 0;
320 :
321 : //! Return whether a particular setting in <datadir>/settings.json is or
322 : //! would be ignored because it is also specified in the command line.
323 : virtual bool isSettingIgnored(const std::string& name) = 0;
324 :
325 : //! Return setting value from <datadir>/settings.json or dash.conf.
326 : virtual util::SettingsValue getPersistentSetting(const std::string& name) = 0;
327 :
328 : //! Update a setting in <datadir>/settings.json.
329 : virtual void updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
330 :
331 : //! Force a setting value to be applied, overriding any other configuration
332 : //! source, but not being persisted.
333 : virtual void forceSetting(const std::string& name, const util::SettingsValue& value) = 0;
334 :
335 : //! Clear all settings in <datadir>/settings.json and store a backup of
336 : //! previous settings in <datadir>/settings.json.bak.
337 : virtual void resetSettings() = 0;
338 :
339 : //! Map port.
340 : virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
341 :
342 : //! Get proxy.
343 : virtual bool getProxy(Network net, Proxy& proxy_info) = 0;
344 :
345 : //! Get number of connections.
346 : virtual size_t getNodeCount(ConnectionDirection flags) = 0;
347 :
348 : //! Get stats for connected nodes.
349 : using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
350 : virtual bool getNodesStats(NodesStats& stats) = 0;
351 :
352 : //! Get ban map entries.
353 : virtual bool getBanned(banmap_t& banmap) = 0;
354 :
355 : //! Ban node.
356 : virtual bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) = 0;
357 :
358 : //! Unban node.
359 : virtual bool unban(const CSubNet& ip) = 0;
360 :
361 : //! Disconnect node by address.
362 : virtual bool disconnectByAddress(const CNetAddr& net_addr) = 0;
363 :
364 : //! Disconnect node by id.
365 : virtual bool disconnectById(NodeId id) = 0;
366 :
367 : //! Return list of external signers (attached devices which can sign transactions).
368 : virtual std::vector<std::unique_ptr<ExternalSigner>> listExternalSigners() = 0;
369 :
370 : //! Get total bytes recv.
371 : virtual int64_t getTotalBytesRecv() = 0;
372 :
373 : //! Get total bytes sent.
374 : virtual int64_t getTotalBytesSent() = 0;
375 :
376 : //! Get mempool size.
377 : virtual size_t getMempoolSize() = 0;
378 :
379 : //! Get mempool dynamic usage.
380 : virtual size_t getMempoolDynamicUsage() = 0;
381 :
382 : //! Get mempool maximum memory usage.
383 : virtual size_t getMempoolMaxUsage() = 0;
384 :
385 : //! Get header tip height and time.
386 : virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
387 :
388 : //! Get num blocks.
389 : virtual int getNumBlocks() = 0;
390 :
391 : //! Get network local addresses.
392 : virtual std::map<CNetAddr, LocalServiceInfo> getNetLocalAddresses() = 0;
393 :
394 : //! Get best block hash.
395 : virtual uint256 getBestBlockHash() = 0;
396 :
397 : //! Get last block time.
398 : virtual int64_t getLastBlockTime() = 0;
399 :
400 : //! Get last block hash.
401 : virtual std::string getLastBlockHash() = 0;
402 :
403 : //! Get verification progress.
404 : virtual double getVerificationProgress() = 0;
405 :
406 : //! Is initial block download.
407 : virtual bool isInitialBlockDownload() = 0;
408 :
409 : //! Is masternode.
410 : virtual bool isMasternode() = 0;
411 :
412 : //! Is loading blocks.
413 : virtual bool isLoadingBlocks() = 0;
414 :
415 : //! Set network active.
416 : virtual void setNetworkActive(bool active) = 0;
417 :
418 : //! Get network active.
419 : virtual bool getNetworkActive() = 0;
420 :
421 : //! Get dust relay fee.
422 : virtual CFeeRate getDustRelayFee() = 0;
423 :
424 : //! Execute rpc command.
425 : virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
426 :
427 : //! List rpc commands.
428 : virtual std::vector<std::string> listRpcCommands() = 0;
429 :
430 : //! Set RPC timer interface if unset.
431 : virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
432 :
433 : //! Unset RPC timer interface.
434 : virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
435 :
436 : //! Get unspent outputs associated with a transaction.
437 : virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
438 :
439 : //! Broadcast transaction.
440 : virtual TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, bilingual_str& err_string) = 0;
441 :
442 : //! Get wallet loader.
443 : virtual WalletLoader& walletLoader() = 0;
444 :
445 : //! Return interface for accessing evo related handler.
446 : virtual EVO& evo() = 0;
447 :
448 : //! Return interface for accessing governance related handler.
449 : virtual GOV& gov() = 0;
450 :
451 : //! Return interface for accessing llmq related handler.
452 : virtual LLMQ& llmq() = 0;
453 :
454 : //! Return interface for accessing masternode related handler.
455 : virtual Masternode::Sync& masternodeSync() = 0;
456 :
457 : //! Return interface for accessing coinjoin options related handler.
458 : virtual CoinJoin::Options& coinJoinOptions() = 0;
459 :
460 : //! Return interface for accessing coinjoin loader handler.
461 : virtual std::unique_ptr<interfaces::CoinJoin::Loader>& coinJoinLoader() = 0;
462 :
463 : //! Register handler for init messages.
464 : using InitMessageFn = std::function<void(const std::string& message)>;
465 : virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
466 :
467 : //! Register handler for message box messages.
468 : using MessageBoxFn =
469 : std::function<bool(const bilingual_str& message, const std::string& caption, unsigned int style)>;
470 : virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
471 :
472 : //! Register handler for question messages.
473 : using QuestionFn = std::function<bool(const bilingual_str& message,
474 : const std::string& non_interactive_message,
475 : const std::string& caption,
476 : unsigned int style)>;
477 : virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
478 :
479 : //! Register handler for progress messages.
480 : using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
481 : virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
482 :
483 : //! Register handler for wallet client constructed messages.
484 : using InitWalletFn = std::function<void()>;
485 : virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) = 0;
486 :
487 : //! Register handler for number of connections changed messages.
488 : using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
489 : virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
490 :
491 : //! Register handler for network active messages.
492 : using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
493 : virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
494 :
495 : //! Register handler for notify alert messages.
496 : using NotifyAlertChangedFn = std::function<void()>;
497 : virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
498 :
499 : //! Register handler for ban list messages.
500 : using BannedListChangedFn = std::function<void()>;
501 : virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
502 :
503 : //! Register handler for block tip messages.
504 : using NotifyBlockTipFn =
505 : std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
506 : virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
507 :
508 : //! Register handler for chainlock messages.
509 : using NotifyChainLockFn =
510 : std::function<void(const std::string& bestChainLockedHash, int32_t bestChainLockedHeight)>;
511 : virtual std::unique_ptr<Handler> handleNotifyChainLock(NotifyChainLockFn fn) = 0;
512 :
513 : //! Register handler for header tip messages.
514 : using NotifyHeaderTipFn =
515 : std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
516 : virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
517 :
518 : //! Register handler for InstantSend data messages.
519 : using NotifyInstantSendChangedFn = std::function<void()>;
520 : virtual std::unique_ptr<Handler> handleNotifyInstantSendChanged(NotifyInstantSendChangedFn fn) = 0;
521 :
522 : //! Register handler for governance data messages.
523 : using NotifyGovernanceChangedFn = std::function<void()>;
524 : virtual std::unique_ptr<Handler> handleNotifyGovernanceChanged(NotifyGovernanceChangedFn fn) = 0;
525 :
526 : //! Register handler for masternode list update messages.
527 : using NotifyMasternodeListChangedFn =
528 : std::function<void(const CDeterministicMNList& newList,
529 : const CBlockIndex* pindex)>;
530 : virtual std::unique_ptr<Handler> handleNotifyMasternodeListChanged(NotifyMasternodeListChangedFn fn) = 0;
531 :
532 : //! Register handler for additional data sync progress update messages.
533 : using NotifyAdditionalDataSyncProgressChangedFn =
534 : std::function<void(double nSyncProgress)>;
535 : virtual std::unique_ptr<Handler> handleNotifyAdditionalDataSyncProgressChanged(NotifyAdditionalDataSyncProgressChangedFn fn) = 0;
536 :
537 : //! Get and set internal node context. Useful for testing, but not
538 : //! accessible across processes.
539 0 : virtual node::NodeContext* context() { return nullptr; }
540 0 : virtual void setContext(node::NodeContext* context) { }
541 : };
542 :
543 : //! Return implementation of Node interface.
544 : std::unique_ptr<Node> MakeNode(node::NodeContext& context);
545 :
546 : //! Block tip (could be a header or not, depends on the subscribed signal).
547 : struct BlockTip {
548 : int block_height;
549 : int64_t block_time;
550 : uint256 block_hash;
551 : };
552 : } // namespace interfaces
553 :
554 : #endif // BITCOIN_INTERFACES_NODE_H
|