mining: parse block creation args in mining_args

Move the argument parsing for -blockmaxweight, -blockreservedweight,
-blockmintxfee out of init.cpp to a dedicated mining_args.cpp.

This is mostly a refactor and keeps the existing validation checks. It
does switch the weight arguments to GetArg<uint64_t>, introduced in
bitcoin/bitcoin#34582, so very large or negative weight values can be
reported differently in error messages.
This commit is contained in:
Sjors Provoost
2026-02-12 19:25:26 +01:00
parent 020166080c
commit fa81e51eae
4 changed files with 70 additions and 22 deletions

View File

@@ -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

View File

@@ -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);

47
src/node/mining_args.cpp Normal file
View File

@@ -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

18
src/node/mining_args.h Normal file
View File

@@ -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