<blockquote>
<p>While interfaces::Chain is used for external components such as wallet, the only possible reason that comes to my mind is that the chain member is for external users of bitcoind. And by this fact, the implementation of interface::chain must be only a simple wrapper over the ChainstateManager, because I think all logic is implemented there.</p>
</blockquote>
<p>Yes. Bitcoin Core has many internal interfaces that are meant to be independent. According to the documentation, <code>interfaces::Chain</code> is used by wallet to access the node's latest chain state. The goal of the interfaces is to not require node, wallet and GUI to run in the same process.</p>
<blockquote>
<p>ChainstateManager manages Chainstates which are m_ibd_chainstate and m_snapshot_chainstate.</p>
</blockquote>
<p>Correct. It's an interface to create and interact with two chainstates (an IBD chainstate generated by downloading blocks, and an optional snapshot chainstate loaded from a UTXO snapshot).</p>

CC BY-SA 4.0
2024-09-20T22:24:53.363Z
2024-09-20T22:24:53.363Z
brunoerg
<p>In the <a href="https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/node/context.h#L43" rel="nofollow noreferrer"><code>NodeContext</code></a> structure there are two members named <a href="https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/node/context.h#L54" rel="nofollow noreferrer"><code>chainman</code></a> and <a href="https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/node/context.h#L57" rel="nofollow noreferrer"><code>chain</code></a>. <code>chainman</code>
is instance of <code>ChainstateManager</code> and <code>chain</code> is instance of <code>interfaces::Chain</code>. What is the differences between these two? What is the differences and why do we need both?</p>
<pre><code>//! NodeContext struct containing references to chain state and connection
//! state.
//!
//! This is used by init, rpc, and test code to pass object references around
//! without needing to declare the same variables and parameters repeatedly, or
//! to use globals. More variables could be added to this struct (particularly
//! references to validation objects) to eliminate use of globals
//! and make code more modular and testable. The struct isn't intended to have
//! any member functions. It should just be a collection of references that can
//! be used without pulling in unwanted dependencies or functionality.
struct NodeContext {
    //! libbitcoin_kernel context
    std::unique_ptr&lt;kernel::Context&gt; kernel;
    //! Init interface for initializing current process and connecting to other processes.
    interfaces::Init* init{nullptr};
    std::unique_ptr&lt;AddrMan&gt; addrman;
    std::unique_ptr&lt;CConnman&gt; connman;
    std::unique_ptr&lt;CTxMemPool&gt; mempool;
    std::unique_ptr&lt;const NetGroupManager&gt; netgroupman;
    std::unique_ptr&lt;CBlockPolicyEstimator&gt; fee_estimator;
    std::unique_ptr&lt;PeerManager&gt; peerman;
    std::unique_ptr&lt;ChainstateManager&gt; chainman;
    std::unique_ptr&lt;BanMan&gt; banman;
    ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
    std::unique_ptr&lt;interfaces::Chain&gt; chain;
    //! List of all chain clients (wallet processes or other client) connected to node.
    std::vector&lt;std::unique_ptr&lt;interfaces::ChainClient&gt;&gt; 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&lt;CScheduler&gt; scheduler;
    std::function&lt;void()&gt; rpc_interruption_point = [] {};

    //! 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();
};

</code></pre>
<p>My assumptions are:</p>
<ul>
<li><code>ChainstateManager</code> manages <code>Chainstate</code>s which are <code>m_ibd_chainstate</code> and <code>m_snapshot_chainstate</code>.</li>
<li><code>Chainstate</code> has all data of a chain.</li>
</ul>
<p>While <code>interfaces::Chain</code> is used for external components such as wallet, the only possible reason that comes to my mind is that the <code>chain</code> member is for external users of <code>bitcoind</code>. And by this fact, the implementation of <code>interface::chain</code> must be only a simple wrapper over the <code>ChainstateManager</code>, because I think all logic is implemented there.</p>

<bitcoin-core-development><c++>
What is the difference between chain and chainman in the NodeContext in the Bitcoin Core?
Amir reza Riahi
# What is the difference between chain and chainman in the NodeContext in the Bitcoin Core?

(Asked by: username_0 on 2023-04-27)

In the [`NodeContext`](https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/node/context.h#L43) structure there are two members named [`chainman`](https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/node/context.h#L54) and [`chain`](https://github.com/bitcoin/bitcoin/blob/306ccd4927a2efe325c8d84be1bdb79edeb29b04/src/node/context.h#L57). `chainman`
is instance of `ChainstateManager` and `chain` is instance of `interfaces::Chain`. What is the differences between these two? What is the differences and why do we need both?

```
//! NodeContext struct containing references to chain state and connection
//! state.
//!
//! This is used by init, rpc, and test code to pass object references around
//! without needing to declare the same variables and parameters repeatedly, or
//! to use globals. More variables could be added to this struct (particularly
//! references to validation objects) to eliminate use of globals
//! and make code more modular and testable. The struct isn't intended to have
//! any member functions. It should just be a collection of references that can
//! be used without pulling in unwanted dependencies or functionality.
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 = [] {};

    //! 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();
};

```

My assumptions are:

* `ChainstateManager` manages `Chainstate`s which are `m_ibd_chainstate` and `m_snapshot_chainstate`.
* `Chainstate` has all data of a chain.

While `interfaces::Chain` is used for external components such as wallet, the only possible reason that comes to my mind is that the `chain` member is for external users of `bitcoind`. And by this fact, the implementation of `interface::chain` must be only a simple wrapper over the `ChainstateManager`, because I think all logic is implemented there.

**Tags:** bitcoin-core-development, c++


---

## 1 answers

### Answer by username_1 on 2024-09-20. Score: 0

> While interfaces::Chain is used for external components such as wallet, the only possible reason that comes to my mind is that the chain member is for external users of bitcoind. And by this fact, the implementation of interface::chain must be only a simple wrapper over the ChainstateManager, because I think all logic is implemented there.

Yes. Bitcoin Core has many internal interfaces that are meant to be independent. According to the documentation, `interfaces::Chain` is used by wallet to access the node's latest chain state. The goal of the interfaces is to not require node, wallet and GUI to run in the same process.

> ChainstateManager manages Chainstates which are m\_ibd\_chainstate and m\_snapshot\_chainstate.

Correct. It's an interface to create and interact with two chainstates (an IBD chainstate generated by downloading blocks, and an optional snapshot chainstate loaded from a UTXO snapshot).


---

