mining: parse block creation args in mining_args
What changed, and why it matters
This commit is a straightforward code cleanup: it moves the command-line argument parsing for block-mining settings out of the large init.cpp file into a new dedicated file. The validation checks remain the same, and the only functional change is that error messages for extremely large or negative weight values may now be phrased slightly differently. There is no security issue here.
No security action needed. Treat as normal code maintenance/refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors parsing of -blockmaxweight, -blockreservedweight, and -blockmintxfee from AppInitParameterInteraction in src/init.cpp into a new node::ReadMiningArgs helper in src/node/mining_args.cpp. The same validation logic is preserved. The weight arguments now use GetArg
Changed components
src/init.cppsrc/node/mining_args.cppsrc/node/mining_args.hsrc/CMakeLists.txtInspect captured patch +70 / −22
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 43923996..b5f8cd1f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -232,6 +232,7 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
node/mempool_persist.cpp
node/mempool_persist_args.cpp
node/miner.cpp
+ node/mining_args.cpp
node/mini_miner.cpp
node/minisketchwrapper.cpp
node/peerman_args.cpp
diff --git a/src/init.cpp b/src/init.cpp
index c53e5ed6..adf94665 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -58,6 +58,7 @@
#include <node/mempool_persist.h>
#include <node/mempool_persist_args.h>
#include <node/miner.h>
+#include <node/mining_args.h>
#include <node/peerman_args.h>
#include <policy/feerate.h>
#include <policy/fees/block_policy_estimator.h>
@@ -124,7 +125,6 @@
#include <node/data/ip_asn.dat.h>
#endif
-using common::AmountErrMsg;
using common::InvalidPortErrMsg;
using common::ResolveErrMsg;
@@ -1074,27 +1074,9 @@ bool AppInitParameterInteraction(const ArgsManager& args)
return InitError(Untranslated("peertimeout must be a positive integer."));
}
- if (const auto arg{args.GetArg("-blockmintxfee")}) {
- if (!ParseMoney(*arg)) {
- return InitError(AmountErrMsg("blockmintxfee", *arg));
- }
- }
-
- {
- const auto max_block_weight = args.GetIntArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
- if (max_block_weight > MAX_BLOCK_WEIGHT) {
- return InitError(strprintf(_("Specified -blockmaxweight (%d) exceeds consensus maximum block weight (%d)"), max_block_weight, MAX_BLOCK_WEIGHT));
- }
- }
-
- {
- const auto block_reserved_weight = args.GetIntArg("-blockreservedweight", DEFAULT_BLOCK_RESERVED_WEIGHT);
- if (block_reserved_weight > MAX_BLOCK_WEIGHT) {
- return InitError(strprintf(_("Specified -blockreservedweight (%d) exceeds consensus maximum block weight (%d)"), block_reserved_weight, MAX_BLOCK_WEIGHT));
- }
- if (block_reserved_weight < MINIMUM_BLOCK_RESERVED_WEIGHT) {
- return InitError(strprintf(_("Specified -blockreservedweight (%d) is lower than minimum safety value of (%d)"), block_reserved_weight, MINIMUM_BLOCK_RESERVED_WEIGHT));
- }
+ auto mining_result{node::ReadMiningArgs(args)};
+ if (!mining_result) {
+ return InitError(util::ErrorString(mining_result));
}
nBytesPerSigOp = args.GetIntArg("-bytespersigop", nBytesPerSigOp);
diff --git a/src/node/mining_args.cpp b/src/node/mining_args.cpp
new file mode 100644
index 00000000..ece422a8
--- /dev/null
+++ b/src/node/mining_args.cpp
@@ -0,0 +1,47 @@
+// Copyright (c) The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <node/mining_args.h>
+
+#include <common/args.h>
+#include <common/messages.h>
+#include <consensus/consensus.h>
+#include <node/mining_types.h>
+#include <tinyformat.h>
+#include <util/moneystr.h>
+#include <util/translation.h>
+
+#include <cstdint>
+
+using common::AmountErrMsg;
+using util::Error;
+using util::Result;
+
+namespace node {
+
+Result<void> ReadMiningArgs(const ArgsManager& args)
+{
+ if (const auto arg{args.GetArg("-blockmintxfee")}) {
+ if (!ParseMoney(*arg)) {
+ return Error{AmountErrMsg("blockmintxfee", *arg)};
+ }
+ }
+
+ const uint64_t max_block_weight{args.GetArg<uint64_t>("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT)};
+ if (max_block_weight > MAX_BLOCK_WEIGHT) {
+ return Error{strprintf(_("Specified -blockmaxweight (%d) exceeds consensus maximum block weight (%d)"), max_block_weight, MAX_BLOCK_WEIGHT)};
+ }
+
+ const uint64_t block_reserved_weight{args.GetArg<uint64_t>("-blockreservedweight", DEFAULT_BLOCK_RESERVED_WEIGHT)};
+ if (block_reserved_weight > MAX_BLOCK_WEIGHT) {
+ return Error{strprintf(_("Specified -blockreservedweight (%d) exceeds consensus maximum block weight (%d)"), block_reserved_weight, MAX_BLOCK_WEIGHT)};
+ }
+ if (block_reserved_weight < MINIMUM_BLOCK_RESERVED_WEIGHT) {
+ return Error{strprintf(_("Specified -blockreservedweight (%d) is lower than minimum safety value of (%d)"), block_reserved_weight, MINIMUM_BLOCK_RESERVED_WEIGHT)};
+ }
+
+ return {};
+}
+
+} // namespace node
diff --git a/src/node/mining_args.h b/src/node/mining_args.h
new file mode 100644
index 00000000..01e695ee
--- /dev/null
+++ b/src/node/mining_args.h
@@ -0,0 +1,18 @@
+// Copyright (c) The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_NODE_MINING_ARGS_H
+#define BITCOIN_NODE_MINING_ARGS_H
+
+#include <util/result.h>
+
+class ArgsManager;
+
+namespace node {
+
+[[nodiscard]] util::Result<void> ReadMiningArgs(const ArgsManager& args);
+
+} // namespace node
+
+#endif // BITCOIN_NODE_MINING_ARGS_H
Why this scored 13/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.