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