Skip to content

Commit b623fab

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

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-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>
@@ -969,6 +970,16 @@ class MinerImpl : public Mining
969970

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

src/node/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ struct BlockCreateOptions {
4343
bool use_mempool{true};
4444
/**
4545
* The default reserved weight for the fixed-size block header,
46-
* transaction count and coinbase transaction.
46+
* transaction count and coinbase transaction. Minimum: 2000 weight units
47+
* (MINIMUM_BLOCK_RESERVED_WEIGHT).
4748
*
4849
* Providing a value overrides the `-blockreservedweight` startup setting.
4950
* Cap'n Proto IPC clients currently cannot leave this field unset, so they

test/functional/interface_ipc_mining.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ async def async_routine():
257257
empty_block = await mining_get_block(empty_template, ctx)
258258
assert_equal(len(empty_block.vtx), 1)
259259

260+
self.log.debug("Enforce minimum reserved weight for IPC clients too")
261+
opts.blockReservedWeight = 0
262+
try:
263+
await mining.createNewBlock(opts)
264+
raise AssertionError("createNewBlock unexpectedly succeeded")
265+
except capnp.lib.capnp.KjException as e:
266+
assert_equal(e.description, "remote exception: std::exception: block_reserved_weight (0) must be at least 2000 weight units")
267+
assert_equal(e.type, "FAILED")
268+
260269
asyncio.run(capnp.run(async_routine()))
261270

262271
def run_coinbase_and_submission_test(self):

0 commit comments

Comments
 (0)