mining: add block create option helpers

Move block template defaulting into helper functions for
BlockCreateOptions. FlattenMiningOptions() fills hardcoded defaults and
MergeMiningOptions() overlays defaults without replacing caller-provided
values.

Use the shared option type in BlockAssembler so IPC callers and internal
callers can pass through the same options path. This commit does not
change behavior, except for dropping the "Specified " prefix from startup
option error messages.

Keep the -blockmintxfee ParseMoney check in ReadMiningArgs() instead of
CheckMiningOptions(), because CheckMiningOptions() only sees the parsed
CFeeRate value and not the original string.

Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
Sjors Provoost
2026-05-18 15:58:39 +02:00
parent 128da7c3ff
commit 8daac1d6eb
11 changed files with 151 additions and 85 deletions

View File

@@ -6,13 +6,17 @@
#include <common/args.h>
#include <common/messages.h>
#include <consensus/amount.h>
#include <consensus/consensus.h>
#include <node/mining_types.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <tinyformat.h>
#include <util/moneystr.h>
#include <util/translation.h>
#include <cstdint>
#include <optional>
using common::AmountErrMsg;
using util::Error;
@@ -20,28 +24,60 @@ using util::Result;
namespace node {
Result<void> ReadMiningArgs(const ArgsManager& args)
Result<void> CheckMiningOptions(const BlockCreateOptions& options, bool use_argnames)
{
if (const auto arg{args.GetArg("-blockmintxfee")}) {
if (!ParseMoney(*arg)) {
return Error{AmountErrMsg("blockmintxfee", *arg)};
}
if (options.block_max_weight && *options.block_max_weight > MAX_BLOCK_WEIGHT) {
return Error{Untranslated(strprintf("%s (%d) exceeds consensus maximum block weight (%d)",
use_argnames ? "-blockmaxweight" : "block_max_weight",
*options.block_max_weight, MAX_BLOCK_WEIGHT))};
}
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)};
if (options.block_reserved_weight && *options.block_reserved_weight > MAX_BLOCK_WEIGHT) {
return Error{Untranslated(strprintf("%s (%d) exceeds consensus maximum block weight (%d)",
use_argnames ? "-blockreservedweight" : "block_reserved_weight",
*options.block_reserved_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 (options.block_reserved_weight && *options.block_reserved_weight < MINIMUM_BLOCK_RESERVED_WEIGHT) {
return Error{Untranslated(strprintf("%s (%d) is lower than minimum safety value of (%d)",
use_argnames ? "-blockreservedweight" : "block_reserved_weight",
*options.block_reserved_weight, MINIMUM_BLOCK_RESERVED_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 {};
}
Result<BlockCreateOptions> ReadMiningArgs(const ArgsManager& args)
{
BlockCreateOptions options;
if (const auto arg{args.GetArg("-blockmintxfee")}) {
std::optional<CAmount> block_min_tx_fee{ParseMoney(*arg)};
if (!block_min_tx_fee) return Error{AmountErrMsg("blockmintxfee", *arg)};
options.block_min_fee_rate = CFeeRate{*block_min_tx_fee};
}
if (const auto arg{args.GetBoolArg("-printpriority")}) options.print_modified_fee = *arg;
options.block_reserved_weight = args.GetArg<uint64_t>("-blockreservedweight");
options.block_max_weight = args.GetArg<uint64_t>("-blockmaxweight");
if (auto result{CheckMiningOptions(options, /*use_argnames=*/true)}; !result) return Error{util::ErrorString(result)};
return options;
}
BlockCreateOptions FlattenMiningOptions(BlockCreateOptions options)
{
if (!options.block_min_fee_rate) options.block_min_fee_rate = CFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
if (!options.print_modified_fee) options.print_modified_fee = DEFAULT_PRINT_MODIFIED_FEE;
if (!options.block_reserved_weight) options.block_reserved_weight = DEFAULT_BLOCK_RESERVED_WEIGHT;
if (!options.block_max_weight) options.block_max_weight = DEFAULT_BLOCK_MAX_WEIGHT;
return options;
}
BlockCreateOptions MergeMiningOptions(BlockCreateOptions x, const BlockCreateOptions& y)
{
if (!x.block_min_fee_rate) x.block_min_fee_rate = y.block_min_fee_rate;
if (!x.print_modified_fee) x.print_modified_fee = y.print_modified_fee;
if (!x.block_reserved_weight) x.block_reserved_weight = y.block_reserved_weight;
if (!x.block_max_weight) x.block_max_weight = y.block_max_weight;
return x;
}
} // namespace node