context.h raw

   1  // Copyright (c) 2019-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_NODE_CONTEXT_H
   6  #define LIMENKA_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  namespace interfaces {
  29  class Chain;
  30  class ChainClient;
  31  class Mining;
  32  class Init;
  33  class WalletLoader;
  34  } // namespace interfaces
  35  namespace kernel {
  36  struct Context;
  37  }
  38  namespace util {
  39  class SignalInterrupt;
  40  }
  41  
  42  namespace node {
  43  class MempoolBridge;
  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      //! liblimenka_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      //! Cross-chain mempool gossip bridge (spamchain, blakecoin, ...).
  71      std::shared_ptr<MempoolBridge> mempool_bridge;
  72      std::unique_ptr<const NetGroupManager> netgroupman;
  73      std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
  74      std::unique_ptr<PeerManager> peerman;
  75      std::unique_ptr<ChainstateManager> chainman;
  76      std::unique_ptr<BanMan> banman;
  77      ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
  78      std::vector<BaseIndex*> indexes; // raw pointers because memory is not managed by this struct
  79      std::unique_ptr<interfaces::Chain> chain;
  80      //! List of all chain clients (wallet processes or other client) connected to node.
  81      std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
  82      //! Reference to chain client that should used to load or create wallets
  83      //! opened by the gui.
  84      std::unique_ptr<interfaces::Mining> mining;
  85      interfaces::WalletLoader* wallet_loader{nullptr};
  86      std::unique_ptr<CScheduler> scheduler;
  87      std::function<void()> rpc_interruption_point = [] {};
  88      //! Issues blocking calls about sync status, errors and warnings
  89      std::unique_ptr<KernelNotifications> notifications;
  90      //! Issues calls about blocks and transactions
  91      std::unique_ptr<ValidationSignals> validation_signals;
  92      std::atomic<int> exit_status{EXIT_SUCCESS};
  93      //! Manages all the node warnings
  94      std::unique_ptr<node::Warnings> warnings;
  95      std::thread background_init_thread;
  96  
  97      //! Declare default constructor and destructor that are not inline, so code
  98      //! instantiating the NodeContext struct doesn't need to #include class
  99      //! definitions for all the unique_ptr members.
 100      NodeContext();
 101      ~NodeContext();
 102  };
 103  } // namespace node
 104  
 105  #endif // LIMENKA_NODE_CONTEXT_H
 106