node.h raw

   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 <support/allocators/secure.h>
  15  #include <util/log.h>
  16  #include <util/translation.h>
  17  
  18  #include <cstddef>
  19  #include <cstdint>
  20  #include <functional>
  21  #include <memory>
  22  #include <optional>
  23  #include <string>
  24  #include <tuple>
  25  #include <vector>
  26  
  27  class BanMan;
  28  class CFeeRate;
  29  class CNodeStats;
  30  class Coin;
  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      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 enable) = 0;
 125  
 126      //! Get proxy.
 127      virtual std::optional<Proxy> getProxy(Network net) = 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      //! Get unspent output associated with a transaction.
 209      virtual std::optional<Coin> getUnspentOutput(const COutPoint& output) = 0;
 210  
 211      //! Broadcast transaction.
 212      virtual node::TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) = 0;
 213  
 214      //! Get wallet loader.
 215      virtual WalletLoader& walletLoader() = 0;
 216  
 217      //! Register handler for init messages.
 218      using InitMessageFn = std::function<void(const std::string& message)>;
 219      virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
 220  
 221      //! Register handler for message box messages.
 222      using MessageBoxFn = std::function<void(const bilingual_str& message, unsigned int style)>;
 223      virtual std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) = 0;
 224  
 225      //! Register handler for question messages.
 226      using QuestionFn = std::function<bool(const bilingual_str& message,
 227          const std::string& non_interactive_message,
 228          unsigned int style)>;
 229      virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
 230  
 231      //! Register handler for progress messages.
 232      using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
 233      virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
 234  
 235      //! Register handler for wallet loader constructed messages.
 236      using InitWalletFn = std::function<void()>;
 237      virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) = 0;
 238  
 239      //! Register handler for number of connections changed messages.
 240      using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
 241      virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
 242  
 243      //! Register handler for network active messages.
 244      using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
 245      virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
 246  
 247      //! Register handler for notify alert messages.
 248      using NotifyAlertChangedFn = std::function<void()>;
 249      virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
 250  
 251      //! Register handler for ban list messages.
 252      using BannedListChangedFn = std::function<void()>;
 253      virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
 254  
 255      //! Register handler for block tip messages.
 256      using NotifyBlockTipFn =
 257          std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
 258      virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
 259  
 260      //! Register handler for header tip messages.
 261      using NotifyHeaderTipFn =
 262          std::function<void(SynchronizationState, interfaces::BlockTip tip, bool presync)>;
 263      virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
 264  
 265      //! Get and set internal node context. Useful for testing, but not
 266      //! accessible across processes.
 267      virtual node::NodeContext* context() { return nullptr; }
 268      virtual void setContext(node::NodeContext* context) { }
 269  };
 270  
 271  //! Return implementation of Node interface.
 272  std::unique_ptr<Node> MakeNode(node::NodeContext& context);
 273  
 274  //! Block tip (could be a header or not, depends on the subscribed signal).
 275  struct BlockTip {
 276      int block_height;
 277      int64_t block_time;
 278      uint256 block_hash;
 279  };
 280  
 281  } // namespace interfaces
 282  
 283  #endif // BITCOIN_INTERFACES_NODE_H
 284