Branch data Line data Source code
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 <utility>
14 : : #include <vector>
15 : :
16 : : class ArgsManager;
17 : : class AddrMan;
18 : : class BanMan;
19 : : class BaseIndex;
20 : : class BlockFilterIndex;
21 : : class CConnman;
22 : : class CoinStatsIndex;
23 : : class FeeRateEstimatorManager;
24 : : class ValidationSignals;
25 : : class CScheduler;
26 : : class CTxMemPool;
27 : : class ChainstateManager;
28 : : class ECC_Context;
29 : : class NetGroupManager;
30 : : class PeerManager;
31 : : class TorController;
32 : : class TxIndex;
33 : : class TxoSpenderIndex;
34 : : namespace interfaces {
35 : : class Chain;
36 : : class ChainClient;
37 : : class Init;
38 : : class WalletLoader;
39 : : } // namespace interfaces
40 : : namespace kernel {
41 : : struct Context;
42 : : }
43 : : namespace util {
44 : : class SignalInterrupt;
45 : : }
46 : :
47 : : namespace node {
48 : : class BlockTemplateManager;
49 : : class KernelNotifications;
50 : : class Warnings;
51 : :
52 : : // A custom deleter lets NodeContext own indexes without node/context.cpp
53 : : // including index headers. Index code includes node/context.h, so including
54 : : // index headers there would create a circular dependency.
55 : : struct IndexDeleter {
56 : : void operator()(BlockFilterIndex* index) const noexcept;
57 : : void operator()(CoinStatsIndex* index) const noexcept;
58 : : void operator()(TxIndex* index) const noexcept;
59 : : void operator()(TxoSpenderIndex* index) const noexcept;
60 : : };
61 : :
62 : : //! Construct an index with the out-of-line deleter used by NodeContext.
63 : : template <typename Index, typename... Args>
64 : 169 : std::unique_ptr<Index, IndexDeleter> MakeIndex(Args&&... args)
65 : : {
66 [ + + ]: 176 : return std::unique_ptr<Index, IndexDeleter>{new Index(std::forward<Args>(args)...)};
67 : : }
68 : :
69 : : //! NodeContext struct containing references to chain state and connection
70 : : //! state.
71 : : //!
72 : : //! This is used by init, rpc, and test code to pass object references around
73 : : //! without needing to declare the same variables and parameters repeatedly, or
74 : : //! to use globals. More variables could be added to this struct (particularly
75 : : //! references to validation objects) to eliminate use of globals
76 : : //! and make code more modular and testable. The struct isn't intended to have
77 : : //! any member functions. It should just be a collection of references that can
78 : : //! be used without pulling in unwanted dependencies or functionality.
79 : : struct NodeContext {
80 : : //! libbitcoin_kernel context
81 : : std::unique_ptr<kernel::Context> kernel;
82 : : std::unique_ptr<ECC_Context> ecc_context;
83 : : //! Init interface for initializing current process and connecting to other processes.
84 : : interfaces::Init* init{nullptr};
85 : : //! Function to request a shutdown.
86 : : std::function<bool()> shutdown_request;
87 : : //! Interrupt object used to track whether node shutdown was requested.
88 : : util::SignalInterrupt* shutdown_signal{nullptr};
89 : : std::unique_ptr<AddrMan> addrman;
90 : : std::unique_ptr<CConnman> connman;
91 : : std::unique_ptr<CTxMemPool> mempool;
92 : : std::unique_ptr<const NetGroupManager> netgroupman;
93 : : std::unique_ptr<FeeRateEstimatorManager> fee_estimator_man;
94 : : std::unique_ptr<PeerManager> peerman;
95 : : std::unique_ptr<TorController> tor_controller;
96 : : std::unique_ptr<ChainstateManager> chainman;
97 : : std::unique_ptr<BanMan> banman;
98 : : ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
99 : : std::vector<BaseIndex*> indexes; // raw pointers because memory is managed by index owner members below
100 : : std::unique_ptr<interfaces::Chain> chain;
101 : : //! List of all chain clients (wallet processes or other client) connected to node.
102 : : std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
103 : : interfaces::WalletLoader* wallet_loader{nullptr};
104 : : std::unique_ptr<CScheduler> scheduler;
105 : : std::function<void()> rpc_interruption_point = [] {};
106 : : //! Issues blocking calls about sync status, errors and warnings
107 : : std::unique_ptr<KernelNotifications> notifications;
108 : : //! Must be destroyed before its dependencies (holds references to them).
109 : : std::unique_ptr<BlockTemplateManager> block_template_manager;
110 : : //! Issues calls about blocks and transactions
111 : : std::unique_ptr<ValidationSignals> validation_signals;
112 : : std::atomic<int> exit_status{EXIT_SUCCESS};
113 : : //! Manages all the node warnings
114 : : std::unique_ptr<node::Warnings> warnings;
115 : : //! Destroy indexes while they can still unregister from validation_signals
116 : : //! and report fatal errors through exit_status and warnings.
117 : : std::vector<std::unique_ptr<BlockFilterIndex, IndexDeleter>> block_filter_indexes;
118 : : std::unique_ptr<TxIndex, IndexDeleter> txindex;
119 : : std::unique_ptr<TxoSpenderIndex, IndexDeleter> txospenderindex;
120 : : std::unique_ptr<CoinStatsIndex, IndexDeleter> coin_stats_index;
121 : : std::thread background_init_thread;
122 : :
123 : : //! Declare default constructor and destructor that are not inline, so code
124 : : //! instantiating the NodeContext struct doesn't need to #include class
125 : : //! definitions for all the unique_ptr members.
126 : : NodeContext();
127 : : ~NodeContext();
128 : : };
129 : : } // namespace node
130 : :
131 : : #endif // BITCOIN_NODE_CONTEXT_H
|