Hi everyone! Welcome to the 4th episode of Inside Bitcoin Code!
As we discussed in IBC#003, a NodeContext
object is instantiated in the main()
function. Let’s find it out what it is and what is needed for inside Bitcoin Core.
NodeContext
: What is it?
A NodeContext
object contains references to the chain state and the connection state. This object is needed in order to avoid multiple declaration of the same variables and parameters, thus making it possible to access them from different parts of the code.
It is defined as a struct
(a similar concept to classes1) containing a collection of references.
NodeContext
: The structure
Here, the complete NodeContext
structure is reported:
struct NodeContext {
//! libbitcoin_kernel context
std::unique_ptr<kernel::Context> kernel;
//! Init interface for initializing current process and connecting to other processes.
interfaces::Init* init{nullptr};
std::unique_ptr<AddrMan> addrman;
std::unique_ptr<CConnman> connman;
std::unique_ptr<CTxMemPool> mempool;
std::unique_ptr<const NetGroupManager> netgroupman;
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
std::unique_ptr<PeerManager> peerman;
std::unique_ptr<ChainstateManager> chainman;
std::unique_ptr<BanMan> banman;
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
std::unique_ptr<interfaces::Chain> chain;
//! List of all chain clients (wallet processes or other client) connected to node.
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
//! Reference to chain client that should used to load or create wallets
//! opened by the gui.
interfaces::WalletLoader* wallet_loader{nullptr};
std::unique_ptr<CScheduler> scheduler;
std::function<void()> rpc_interruption_point = [] {};
std::unique_ptr<KernelNotifications> notifications;
//! Declare default constructor and destructor that are not inline, so code
//! instantiating the NodeContext struct doesn't need to #include class
//! definitions for all the unique_ptr members.
NodeContext();
~NodeContext();
};
As it can be noted, all the members of this struct are defined as pointers2, both raw and smart (we will talk about them in Learn2Code soon).
NodeContext
: Step-by-step
Let’s look at the structure in detail:
Structure holding the kernel library’s logically global state
std::unique_ptr<kernel::Context> kernel;
Raw pointer to the interface initialization; initializes the current process and connects to other process.
interfaces::Init* init{nullptr};
Address manager
std::unique_ptr<AddrMan> addrman;
Connection manager
std::unique_ptr<CConnman> connman;
Mempool
std::unique_ptr<CTxMemPool> mempool;
Network group manager
std::unique_ptr<const NetGroupManager> netgroupman;
Fee-rate estimator
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
Connection manager between peer nodes
std::unique_ptr<PeerManager> peerman;
Interface for creating and interacting with one or two chain states
std::unique_ptr<ChainstateManager> chainman;
Ban manager, manages banned peers
std::unique_ptr<BanMan> banman;
Command line arguments validation and interpretation
ArgsManager* args{nullptr};
Interface to give clients ability to access the chain state
std::unique_ptr<interfaces::Chain> chain;
List of all chain clients (i.e. wallets) connected to the node
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
Reference used to create or load wallets
interfaces::WalletLoader* wallet_loader{nullptr};
Class for background tasks
std::unique_ptr<CScheduler> scheduler;
Functions that calls an error if RPC is not running
std::function<void()> rpc_interruption_point = [] {};
Manages notifications from the kernel
std::unique_ptr<KernelNotifications> notifications;
Default constructor and destructor for the
NodeContext
structureNodeContext(); ~NodeContext();
Aaaaand we are done for today!
Thank you for reading Inside Bitcoin Code! If you have any suggestion, question or request leave a comment.
See you next week!
And just like that, that block of code was explained. This is still my favorite newsletter to see in my inbox.