From 46272e9b2a609d7f06c1538db492f644893db3e7 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 16 Feb 2026 14:51:12 +0100 Subject: [PATCH] Fix SV2 reserved weight floor calculation Use std::max so block_reserved_weight enforces the 2000 weight minimum rather than capping at 2000. Add a focused regression test that covers the floor behavior and documents the min-vs-cap failure mode. Reported by xyephy: https://github.com/stratum-mining/sv2-tp/pull/80#issuecomment-3887068616 Assisted-by: OpenAI GPT-5.3-Codex --- src/sv2/block_options.h | 2 ++ src/sv2/template_provider.cpp | 11 +++++------ src/test/sv2_template_provider_tests.cpp | 11 +++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/sv2/block_options.h b/src/sv2/block_options.h index c636346b..4ebadbe0 100644 --- a/src/sv2/block_options.h +++ b/src/sv2/block_options.h @@ -14,6 +14,8 @@ namespace node { //! Default reserved weight for block assembly scaffolding (header, coinbase, etc). static constexpr unsigned int DEFAULT_BLOCK_RESERVED_WEIGHT{8000}; +//! Minimum reserved weight enforced by block assembly. +static constexpr size_t MIN_BLOCK_RESERVED_WEIGHT{2000}; struct BlockCreateOptions { /** Set false to omit mempool transactions from templates. */ diff --git a/src/sv2/template_provider.cpp b/src/sv2/template_provider.cpp index 8152c3f5..31705dc5 100644 --- a/src/sv2/template_provider.cpp +++ b/src/sv2/template_provider.cpp @@ -258,14 +258,13 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id) const size_t block_reserved_floor{1168}; // Reserve a little more so that if the above calculation is // wrong or there's an implementation error, we don't produce - // an invalid bock when the template is completely full. + // an invalid block when the template is completely full. const size_t block_reserved_padding{400}; - // Bitcoin Core enforces a mimimum block reserved weight of 2000. - options.block_reserved_weight = std::min(size_t(2000), - block_reserved_floor + - block_reserved_padding + - client->m_coinbase_tx_outputs_size * 4); + // Bitcoin Core enforces a minimum block reserved weight of 2000. + options.block_reserved_weight = std::max( + node::MIN_BLOCK_RESERVED_WEIGHT, + block_reserved_floor + block_reserved_padding + client->m_coinbase_tx_outputs_size * 4); } return true; }; diff --git a/src/test/sv2_template_provider_tests.cpp b/src/test/sv2_template_provider_tests.cpp index a7e3f9f2..698285eb 100644 --- a/src/test/sv2_template_provider_tests.cpp +++ b/src/test/sv2_template_provider_tests.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include +#include #include #include #include @@ -27,6 +29,15 @@ BOOST_FIXTURE_TEST_SUITE(sv2_template_provider_tests, Sv2BasicTestingSetup) +BOOST_AUTO_TEST_CASE(block_reserved_weight_floor) +{ + node::BlockCreateOptions options{}; + // Guard against regressions where the reserved weight floor is treated as a cap. + options.block_reserved_weight = 1800; + options.block_reserved_weight = std::max(node::MIN_BLOCK_RESERVED_WEIGHT, options.block_reserved_weight); + BOOST_REQUIRE_EQUAL(options.block_reserved_weight, node::MIN_BLOCK_RESERVED_WEIGHT); +} + BOOST_AUTO_TEST_CASE(client_tests) { TPTester tester{};