Skip to content

Commit d443301

Browse files
committed
mining: enforce minimum reserved weight for IPC
Previously a lower value was silently clamped to MINIMUM_BLOCK_RESERVED_WEIGHT.
1 parent b9e18ae commit d443301

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/node/interfaces.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include <any>
6868
#include <memory>
6969
#include <optional>
70+
#include <stdexcept>
7071
#include <utility>
7172

7273
#include <boost/signals2/signal.hpp>
@@ -963,6 +964,16 @@ class MinerImpl : public Mining
963964

964965
std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options) override
965966
{
967+
// Reject too-small values instead of clamping so callers don't silently
968+
// end up mining with different options than requested. This matches the
969+
// behavior of the `-blockreservedweight` startup option, which rejects
970+
// values below MINIMUM_BLOCK_RESERVED_WEIGHT.
971+
if (options.block_reserved_weight && options.block_reserved_weight < MINIMUM_BLOCK_RESERVED_WEIGHT) {
972+
throw std::runtime_error(strprintf("block_reserved_weight (%zu) must be at least %u weight units",
973+
*options.block_reserved_weight,
974+
MINIMUM_BLOCK_RESERVED_WEIGHT));
975+
}
976+
966977
// Ensure m_tip_block is set so consumers of BlockTemplate can rely on that.
967978
if (!waitTipChanged(uint256::ZERO, MillisecondsDouble::max())) return {};
968979

src/node/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ struct BlockCreateOptions {
4141
bool use_mempool{true};
4242
/**
4343
* The default reserved weight for the fixed-size block header,
44-
* transaction count and coinbase transaction.
44+
* transaction count and coinbase transaction. Minimum: 2000 weight units
45+
* (MINIMUM_BLOCK_RESERVED_WEIGHT).
4546
*
4647
* Cap'n Proto IPC clients do not currently have a way of leaving this field
4748
* unset and will always provide a value.

test/functional/interface_ipc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ async def wait_for_block():
272272
assert empty_template is not None
273273
block = await self.parse_and_deserialize_block(empty_template, ctx)
274274
assert_equal(len(block.vtx), 1)
275+
276+
self.log.debug("Enforce minimum reserved weight for IPC clients too")
277+
opts.blockReservedWeight = 0
278+
try:
279+
await mining.createNewBlock(opts)
280+
raise AssertionError("createNewBlock unexpectedly succeeded")
281+
except capnp.lib.capnp.KjException as e:
282+
assert_equal(e.description, "remote exception: std::exception: block_reserved_weight (0) must be at least 2000 weight units")
283+
assert_equal(e.type, "FAILED")
284+
275285
# Restore opts
276286
opts.blockReservedWeight = 4000
277287

0 commit comments

Comments
 (0)