context.h raw

   1  // Copyright (c) 2019-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_NODE_CONTEXT_H
   6  #define BITCOIN_NODE_CONTEXT_H
   7  
   8  #include <node/mining_types.h>
   9  
  10  #include <atomic>
  11  #include <cstdlib>
  12  #include <functional>
  13  #include <memory>
  14  #include <thread>
  15  #include <vector>
  16  
  17  class ArgsManager;
  18  class AddrMan;
  19  class BanMan;
  20  class BaseIndex;
  21  class CBlockPolicyEstimator;
  22  class CConnman;
  23  class ValidationSignals;
  24  class CScheduler;
  25  class CTxMemPool;
  26  class ChainstateManager;
  27  class ECC_Context;
  28  class NetGroupManager;
  29  class PeerManager;
  30  class TorController;
  31  namespace interfaces {
  32  class Chain;
  33  class ChainClient;
  34  class Mining;
  35  class Init;
  36  class WalletLoader;
  37  } // namespace interfaces
  38  namespace kernel {
  39  struct Context;
  40  }
  41  namespace util {
  42  class SignalInterrupt;
  43  }
  44  
  45  namespace node {
  46  class KernelNotifications;
  47  class Warnings;
  48  
  49  //! NodeContext struct containing references to chain state and connection
  50  //! state.
  51  //!
  52  //! This is used by init, rpc, and test code to pass object references around
  53  //! without needing to declare the same variables and parameters repeatedly, or
  54  //! to use globals. More variables could be added to this struct (particularly
  55  //! references to validation objects) to eliminate use of globals
  56  //! and make code more modular and testable. The struct isn't intended to have
  57  //! any member functions. It should just be a collection of references that can
  58  //! be used without pulling in unwanted dependencies or functionality.
  59  struct NodeContext {
  60      //! libbitcoin_kernel context
  61      std::unique_ptr<kernel::Context> kernel;
  62      std::unique_ptr<ECC_Context> ecc_context;
  63      //! Init interface for initializing current process and connecting to other processes.
  64      interfaces::Init* init{nullptr};
  65      //! Function to request a shutdown.
  66      std::function<bool()> shutdown_request;
  67      //! Interrupt object used to track whether node shutdown was requested.
  68      util::SignalInterrupt* shutdown_signal{nullptr};
  69      std::unique_ptr<AddrMan> addrman;
  70      std::unique_ptr<CConnman> connman;
  71      std::unique_ptr<CTxMemPool> mempool;
  72      std::unique_ptr<const NetGroupManager> netgroupman;
  73      std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
  74      std::unique_ptr<PeerManager> peerman;
  75      std::unique_ptr<TorController> tor_controller;
  76      std::unique_ptr<ChainstateManager> chainman;
  77      std::unique_ptr<BanMan> banman;
  78      ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
  79      std::vector<BaseIndex*> indexes; // raw pointers because memory is not managed by this struct
  80      std::unique_ptr<interfaces::Chain> chain;
  81      //! List of all chain clients (wallet processes or other client) connected to node.
  82      std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
  83      //! Reference to chain client that should used to load or create wallets
  84      //! opened by the gui.
  85      std::unique_ptr<interfaces::Mining> mining;
  86      //! Mining options used to create block templates. This value member is an
  87      //! exception to the dependency guidance above because BlockCreateOptions is
  88      //! a minimal dependency. It could be moved to the BlockTemplateCache
  89      //! proposed in bitcoin/bitcoin#33421.
  90      BlockCreateOptions mining_args;
  91      interfaces::WalletLoader* wallet_loader{nullptr};
  92      std::unique_ptr<CScheduler> scheduler;
  93      std::function<void()> rpc_interruption_point = [] {};
  94      //! Issues blocking calls about sync status, errors and warnings
  95      std::unique_ptr<KernelNotifications> notifications;
  96      //! Issues calls about blocks and transactions
  97      std::unique_ptr<ValidationSignals> validation_signals;
  98      std::atomic<int> exit_status{EXIT_SUCCESS};
  99      //! Manages all the node warnings
 100      std::unique_ptr<node::Warnings> warnings;
 101      std::thread background_init_thread;
 102  
 103      //! Declare default constructor and destructor that are not inline, so code
 104      //! instantiating the NodeContext struct doesn't need to #include class
 105      //! definitions for all the unique_ptr members.
 106      NodeContext();
 107      ~NodeContext();
 108  };
 109  } // namespace node
 110  
 111  #endif // BITCOIN_NODE_CONTEXT_H
 112