From 3bb6498fb0f6ac611ba4093ef774a3152b93a2c6 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 18 May 2026 14:17:29 +0200 Subject: [PATCH] mining: store block create options in NodeContext Read configured mining options once during startup instead of parsing options like -blockmaxweight each time a block template is generated. Store the parsed startup options as BlockCreateOptions. Members left unset keep representing unset options; hardcoded defaults are applied by FlattenMiningOptions() before the options are used. IPC overrides and node defaults can then be merged with the same option type used by BlockAssembler. Co-authored-by: Ryan Ofsky --- src/init.cpp | 3 +++ src/node/context.h | 7 ++++++ src/node/interfaces.cpp | 3 +-- src/node/mining_args.cpp | 2 ++ src/rpc/mining.cpp | 45 ++++++++++++++++++++++++++++------ src/test/miner_tests.cpp | 2 +- src/test/util/setup_common.cpp | 5 ++++ 7 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index adf94665f59..aab7a52ebea 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1310,6 +1310,9 @@ static ChainstateLoadResult InitAndLoadChainstate( if (!mempool_error.empty()) { return {ChainstateLoadStatus::FAILURE_FATAL, mempool_error}; } + auto mining_args{node::ReadMiningArgs(args)}; + Assert(mining_args); // no error can happen, already checked in AppInitParameterInteraction + node.mining_args = std::move(*mining_args); LogInfo("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)", cache_sizes.coins / double(1_MiB), mempool_opts.max_size_bytes / double(1_MiB)); diff --git a/src/node/context.h b/src/node/context.h index 848c872fd4a..b8b3274f090 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_NODE_CONTEXT_H #define BITCOIN_NODE_CONTEXT_H +#include + #include #include #include @@ -81,6 +83,11 @@ struct NodeContext { //! Reference to chain client that should used to load or create wallets //! opened by the gui. std::unique_ptr mining; + //! Mining options used to create block templates. This value member is an + //! exception to the dependency guidance above because BlockCreateOptions is + //! a minimal dependency. It could be moved to the BlockTemplateCache + //! proposed in bitcoin/bitcoin#33421. + BlockCreateOptions mining_args; interfaces::WalletLoader* wallet_loader{nullptr}; std::unique_ptr scheduler; std::function rpc_interruption_point = [] {}; diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 5317af9ff97..5aa6e27155d 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -988,8 +988,7 @@ public: // Also wait during the final catch-up moments after IBD. if (!CooldownIfHeadersAhead(chainman(), notifications(), *maybe_tip, m_interrupt_mining)) return {}; } - const auto args_options{*Assert(ReadMiningArgs(*Assert(m_node.args)))}; - const BlockCreateOptions create_options{MergeMiningOptions(options, args_options)}; + const BlockCreateOptions create_options{MergeMiningOptions(options, m_node.mining_args)}; return std::make_unique(create_options, BlockAssembler{ chainman().ActiveChainstate(), diff --git a/src/node/mining_args.cpp b/src/node/mining_args.cpp index 33e623a14be..ec173464bb6 100644 --- a/src/node/mining_args.cpp +++ b/src/node/mining_args.cpp @@ -13,10 +13,12 @@ #include #include #include +#include #include #include #include +#include #include using common::AmountErrMsg; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 62e64a5427c..e89a0e61ee1 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -5,48 +5,77 @@ #include // IWYU pragma: keep +#include + +#include +#include #include #include #include -#include #include #include #include #include #include #include -#include -#include -#include +#include +#include #include #include +#include +#include #include #include #include #include #include -#include +#include +#include #include +#include +#include #include #include +#include +#include #include #include #include #include