interfaces: make Mining use const NodeContext
What changed, and why it matters
This is a small, safe code cleanup change. It makes the mining-related interface accept a read-only (const) view of the node's internal state instead of a mutable one. It does not fix a security bug or change runtime behavior in any meaningful way.
No security action needed. Treat as a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors the Mining interface (interfaces::Mining, BlockTemplateImpl, MinerImpl, MakeMining) to use const node::NodeContext& instead of node::NodeContext&. It also replaces one internal use of context()->mempool.get() with m_node.mempool.get() because context() now returns a const pointer. There are no functional changes to consensus, networking, or validation logic.
Changed components
src/interfaces/mining.hsrc/node/interfaces.cppInspect captured patch +11 / −11
diff --git a/src/interfaces/mining.h b/src/interfaces/mining.h
index ca4f7381..30dade3e 100644
--- a/src/interfaces/mining.h
+++ b/src/interfaces/mining.h
@@ -159,7 +159,7 @@ public:
//! Get internal node context. Useful for RPC and testing,
//! but not accessible across processes.
- virtual node::NodeContext* context() { return nullptr; }
+ virtual const node::NodeContext* context() { return nullptr; }
};
//! Return implementation of Mining interface.
@@ -167,7 +167,7 @@ public:
//! @param[in] wait_loaded waits for chainstate data to be loaded before
//! returning. Used to prevent external clients from
//! being able to crash the node during startup.
-std::unique_ptr<Mining> MakeMining(node::NodeContext& node, bool wait_loaded=true);
+std::unique_ptr<Mining> MakeMining(const node::NodeContext& node, bool wait_loaded=true);
} // namespace interfaces
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
index 16db8692..7abd4b75 100644
--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -869,9 +869,9 @@ class BlockTemplateImpl : public BlockTemplate
public:
explicit BlockTemplateImpl(BlockAssembler::Options assemble_options,
std::unique_ptr<CBlockTemplate> block_template,
- NodeContext& node) : m_assemble_options(std::move(assemble_options)),
- m_block_template(std::move(block_template)),
- m_node(node)
+ const NodeContext& node) : m_assemble_options(std::move(assemble_options)),
+ m_block_template(std::move(block_template)),
+ m_node(node)
{
assert(m_block_template);
}
@@ -931,13 +931,13 @@ public:
bool m_interrupt_wait{false};
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
KernelNotifications& notifications() { return *Assert(m_node.notifications); }
- NodeContext& m_node;
+ const NodeContext& m_node;
};
class MinerImpl : public Mining
{
public:
- explicit MinerImpl(NodeContext& node) : m_node(node) {}
+ explicit MinerImpl(const NodeContext& node) : m_node(node) {}
bool isTestChain() override
{
@@ -993,7 +993,7 @@ public:
BlockAssembler::Options assemble_options{options};
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
- return std::make_unique<BlockTemplateImpl>(assemble_options, BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(), m_node);
+ return std::make_unique<BlockTemplateImpl>(assemble_options, BlockAssembler{chainman().ActiveChainstate(), m_node.mempool.get(), assemble_options}.CreateNewBlock(), m_node);
}
void interrupt() override
@@ -1010,12 +1010,12 @@ public:
return state.IsValid();
}
- NodeContext* context() override { return &m_node; }
+ const NodeContext* context() override { return &m_node; }
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
KernelNotifications& notifications() { return *Assert(m_node.notifications); }
// Treat as if guarded by notifications().m_tip_block_mutex
bool m_interrupt_mining{false};
- NodeContext& m_node;
+ const NodeContext& m_node;
};
class RpcImpl : public Rpc
@@ -1041,7 +1041,7 @@ public:
namespace interfaces {
std::unique_ptr<Node> MakeNode(node::NodeContext& context) { return std::make_unique<node::NodeImpl>(context); }
std::unique_ptr<Chain> MakeChain(node::NodeContext& context) { return std::make_unique<node::ChainImpl>(context); }
-std::unique_ptr<Mining> MakeMining(node::NodeContext& context, bool wait_loaded)
+std::unique_ptr<Mining> MakeMining(const node::NodeContext& context, bool wait_loaded)
{
if (wait_loaded) {
node::KernelNotifications& kernel_notifications(*Assert(context.notifications));
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.