Skip to content

Commit d77ba11

Browse files
committed
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: #80 (comment) Assisted-by: OpenAI GPT-5.3-Codex
1 parent f4dfc9e commit d77ba11

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/sv2/block_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace node {
1414

1515
//! Default reserved weight for block assembly scaffolding (header, coinbase, etc).
1616
static constexpr unsigned int DEFAULT_BLOCK_RESERVED_WEIGHT{8000};
17+
//! Minimum reserved weight enforced by block assembly.
18+
static constexpr size_t MIN_BLOCK_RESERVED_WEIGHT{2000};
1719

1820
struct BlockCreateOptions {
1921
/** Set false to omit mempool transactions from templates. */

src/sv2/template_provider.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,13 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
266266
const size_t block_reserved_floor{1168};
267267
// Reserve a little more so that if the above calculation is
268268
// wrong or there's an implementation error, we don't produce
269-
// an invalid bock when the template is completely full.
269+
// an invalid block when the template is completely full.
270270
const size_t block_reserved_padding{400};
271271

272-
// Bitcoin Core enforces a mimimum block reserved weight of 2000.
273-
options.block_reserved_weight = std::min(size_t(2000),
274-
block_reserved_floor +
275-
block_reserved_padding +
276-
client->m_coinbase_tx_outputs_size * 4);
272+
// Bitcoin Core enforces a minimum block reserved weight of 2000.
273+
options.block_reserved_weight = std::max(
274+
node::MIN_BLOCK_RESERVED_WEIGHT,
275+
block_reserved_floor + block_reserved_padding + client->m_coinbase_tx_outputs_size * 4);
277276
}
278277
return true;
279278
};

src/test/sv2_template_provider_tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <boost/test/unit_test.hpp>
22
#include <interfaces/mining.h>
3+
#include <sv2/block_options.h>
34
#include <interfaces/init.h>
45
#include <sv2/messages.h>
56
#include <test/sv2_test_setup.h>
@@ -16,6 +17,7 @@
1617
#include <test/sv2_mock_mining.h>
1718

1819
#include <future>
20+
#include <algorithm>
1921
#include <memory>
2022
#include <string>
2123
#include <thread>
@@ -27,6 +29,15 @@
2729

2830
BOOST_FIXTURE_TEST_SUITE(sv2_template_provider_tests, Sv2BasicTestingSetup)
2931

32+
BOOST_AUTO_TEST_CASE(block_reserved_weight_floor)
33+
{
34+
node::BlockCreateOptions options{};
35+
// Guard against regressions where the reserved weight floor is treated as a cap.
36+
options.block_reserved_weight = 1800;
37+
options.block_reserved_weight = std::max(node::MIN_BLOCK_RESERVED_WEIGHT, options.block_reserved_weight);
38+
BOOST_REQUIRE_EQUAL(options.block_reserved_weight, node::MIN_BLOCK_RESERVED_WEIGHT);
39+
}
40+
3041
BOOST_AUTO_TEST_CASE(client_tests)
3142
{
3243
TPTester tester{};

0 commit comments

Comments
 (0)