From 2c7d90a6d67a159332d109aab278632d64078f0b Mon Sep 17 00:00:00 2001 From: ismaelsadeeq Date: Tue, 14 Jan 2025 14:16:57 -0500 Subject: [PATCH 1/6] miner: bugfix: fix duplicate weight reservation in block assembler - This commit renamed coinbase_max_additional_weight to block_reserved_weight. - Also clarify that the reservation is for block header, transaction count and coinbase transaction. --- src/init.cpp | 1 + src/ipc/capnp/mining.capnp | 2 +- src/node/miner.cpp | 14 +++++++------- src/node/types.h | 8 ++++---- src/policy/policy.h | 3 ++- src/test/fuzz/mini_miner.cpp | 8 +++++--- test/functional/mining_basic.py | 3 ++- test/functional/test_framework/messages.py | 1 + 8 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 1f597cb7cb2..82ad068a1b6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/src/ipc/capnp/mining.capnp b/src/ipc/capnp/mining.capnp index 50b07bda708..e57d6679e18 100644 --- a/src/ipc/capnp/mining.capnp +++ b/src/ipc/capnp/mining.capnp @@ -35,7 +35,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") { struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") { useMempool @0 :Bool $Proxy.name("use_mempool"); - coinbaseMaxAdditionalWeight @1 :UInt64 $Proxy.name("coinbase_max_additional_weight"); + blockReservedWeight @1 :UInt64 $Proxy.name("block_reserved_weight"); coinbaseOutputMaxAdditionalSigops @2 :UInt64 $Proxy.name("coinbase_output_max_additional_sigops"); } diff --git a/src/node/miner.cpp b/src/node/miner.cpp index 0ff5a1eadea..4f4a273bc9c 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -67,11 +67,11 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman) static BlockAssembler::Options ClampOptions(BlockAssembler::Options options) { - Assert(options.coinbase_max_additional_weight <= DEFAULT_BLOCK_MAX_WEIGHT); + Assert(options.block_reserved_weight <= MAX_BLOCK_WEIGHT); Assert(options.coinbase_output_max_additional_sigops <= MAX_BLOCK_SIGOPS_COST); - // Limit weight to between coinbase_max_additional_weight and DEFAULT_BLOCK_MAX_WEIGHT for sanity: - // Coinbase (reserved) outputs can safely exceed -blockmaxweight, but the rest of the block template will be empty. - options.nBlockMaxWeight = std::clamp(options.nBlockMaxWeight, options.coinbase_max_additional_weight, DEFAULT_BLOCK_MAX_WEIGHT); + // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity: + // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty. + options.nBlockMaxWeight = std::clamp(options.nBlockMaxWeight, options.block_reserved_weight, MAX_BLOCK_WEIGHT); return options; } @@ -97,8 +97,8 @@ void BlockAssembler::resetBlock() { inBlock.clear(); - // Reserve space for coinbase tx - nBlockWeight = m_options.coinbase_max_additional_weight; + // Reserve space for fixed-size block header, txs count, and coinbase tx. + nBlockWeight = m_options.block_reserved_weight; nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops; // These counters do not include coinbase tx @@ -386,7 +386,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda ++nConsecutiveFailed; if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight > - m_options.nBlockMaxWeight - m_options.coinbase_max_additional_weight) { + m_options.nBlockMaxWeight - m_options.block_reserved_weight) { // Give up if we're close to full and haven't succeeded in a while break; } diff --git a/src/node/types.h b/src/node/types.h index 4b0de084ab3..290bbc23f14 100644 --- a/src/node/types.h +++ b/src/node/types.h @@ -14,6 +14,7 @@ #define BITCOIN_NODE_TYPES_H #include +#include #include