Skip to content

Commit 2c7d90a

Browse files
committed
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.
1 parent 5acf12b commit 2c7d90a

File tree

8 files changed

+23
-17
lines changed

8 files changed

+23
-17
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <common/args.h>
2020
#include <common/system.h>
2121
#include <consensus/amount.h>
22+
#include <consensus/consensus.h>
2223
#include <deploymentstatus.h>
2324
#include <hash.h>
2425
#include <httprpc.h>

src/ipc/capnp/mining.capnp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
3535

3636
struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
3737
useMempool @0 :Bool $Proxy.name("use_mempool");
38-
coinbaseMaxAdditionalWeight @1 :UInt64 $Proxy.name("coinbase_max_additional_weight");
38+
blockReservedWeight @1 :UInt64 $Proxy.name("block_reserved_weight");
3939
coinbaseOutputMaxAdditionalSigops @2 :UInt64 $Proxy.name("coinbase_output_max_additional_sigops");
4040
}
4141

src/node/miner.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
6767

6868
static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
6969
{
70-
Assert(options.coinbase_max_additional_weight <= DEFAULT_BLOCK_MAX_WEIGHT);
70+
Assert(options.block_reserved_weight <= MAX_BLOCK_WEIGHT);
7171
Assert(options.coinbase_output_max_additional_sigops <= MAX_BLOCK_SIGOPS_COST);
72-
// Limit weight to between coinbase_max_additional_weight and DEFAULT_BLOCK_MAX_WEIGHT for sanity:
73-
// Coinbase (reserved) outputs can safely exceed -blockmaxweight, but the rest of the block template will be empty.
74-
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, options.coinbase_max_additional_weight, DEFAULT_BLOCK_MAX_WEIGHT);
72+
// Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
73+
// block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
74+
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, options.block_reserved_weight, MAX_BLOCK_WEIGHT);
7575
return options;
7676
}
7777

@@ -97,8 +97,8 @@ void BlockAssembler::resetBlock()
9797
{
9898
inBlock.clear();
9999

100-
// Reserve space for coinbase tx
101-
nBlockWeight = m_options.coinbase_max_additional_weight;
100+
// Reserve space for fixed-size block header, txs count, and coinbase tx.
101+
nBlockWeight = m_options.block_reserved_weight;
102102
nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
103103

104104
// These counters do not include coinbase tx
@@ -386,7 +386,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
386386
++nConsecutiveFailed;
387387

388388
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
389-
m_options.nBlockMaxWeight - m_options.coinbase_max_additional_weight) {
389+
m_options.nBlockMaxWeight - m_options.block_reserved_weight) {
390390
// Give up if we're close to full and haven't succeeded in a while
391391
break;
392392
}

src/node/types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define BITCOIN_NODE_TYPES_H
1515

1616
#include <cstddef>
17+
#include <policy/policy.h>
1718
#include <script/script.h>
1819

1920
namespace node {
@@ -34,11 +35,10 @@ struct BlockCreateOptions {
3435
*/
3536
bool use_mempool{true};
3637
/**
37-
* The maximum additional weight which the pool will add to the coinbase
38-
* scriptSig, witness and outputs. This must include any additional
39-
* weight needed for larger CompactSize encoded lengths.
38+
* The default reserved weight for the fixed-size block header,
39+
* transaction count and coinbase transaction.
4040
*/
41-
size_t coinbase_max_additional_weight{4000};
41+
size_t block_reserved_weight{DEFAULT_BLOCK_RESERVED_WEIGHT};
4242
/**
4343
* The maximum additional sigops which the pool will add in coinbase
4444
* transaction outputs.

src/policy/policy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class CFeeRate;
2020
class CScript;
2121

2222
/** Default for -blockmaxweight, which controls the range of block weights the mining code will create **/
23-
static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT{MAX_BLOCK_WEIGHT - 4000};
23+
static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT{MAX_BLOCK_WEIGHT};
24+
static constexpr unsigned int DEFAULT_BLOCK_RESERVED_WEIGHT{8000};
2425
/** Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by mining code **/
2526
static constexpr unsigned int DEFAULT_BLOCK_MIN_TX_FEE{1000};
2627
/** The maximum weight for transactions we're willing to relay/mine */

src/test/fuzz/mini_miner.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <node/miner.h>
1515
#include <node/mini_miner.h>
16+
#include <node/types.h>
1617
#include <primitives/transaction.h>
1718
#include <random.h>
1819
#include <txmempool.h>
@@ -157,9 +158,10 @@ FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
157158
}
158159
}
159160

160-
// Stop if pool reaches DEFAULT_BLOCK_MAX_WEIGHT because BlockAssembler will stop when the
161+
const auto block_adjusted_max_weight = MAX_BLOCK_WEIGHT - DEFAULT_BLOCK_RESERVED_WEIGHT;
162+
// Stop if pool reaches block_adjusted_max_weight because BlockAssembler will stop when the
161163
// block template reaches that, but the MiniMiner will keep going.
162-
if (pool.GetTotalTxSize() + GetVirtualTransactionSize(*tx) >= DEFAULT_BLOCK_MAX_WEIGHT) break;
164+
if (pool.GetTotalTxSize() + GetVirtualTransactionSize(*tx) >= block_adjusted_max_weight) break;
163165
TestMemPoolEntryHelper entry;
164166
const CAmount fee{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
165167
assert(MoneyRange(fee));
@@ -181,7 +183,7 @@ FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
181183

182184
node::BlockAssembler::Options miner_options;
183185
miner_options.blockMinFeeRate = target_feerate;
184-
miner_options.nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
186+
miner_options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
185187
miner_options.test_block_validity = false;
186188
miner_options.coinbase_output_script = CScript() << OP_0;
187189

test/functional/mining_basic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
CBlock,
2727
CBlockHeader,
2828
COIN,
29+
DEFAULT_BLOCK_RESERVED_WEIGHT,
2930
ser_uint256,
3031
)
3132
from test_framework.p2p import P2PDataStore
@@ -77,7 +78,7 @@ def mine_chain(self):
7778
mining_info = self.nodes[0].getmininginfo()
7879
assert_equal(mining_info['blocks'], 200)
7980
assert_equal(mining_info['currentblocktx'], 0)
80-
assert_equal(mining_info['currentblockweight'], 4000)
81+
assert_equal(mining_info['currentblockweight'], DEFAULT_BLOCK_RESERVED_WEIGHT)
8182

8283
self.log.info('test blockversion')
8384
self.restart_node(0, extra_args=[f'-mocktime={t}', '-blockversion=1337'])

test/functional/test_framework/messages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
MAX_LOCATOR_SZ = 101
3535
MAX_BLOCK_WEIGHT = 4000000
36+
DEFAULT_BLOCK_RESERVED_WEIGHT = 8000
3637
MAX_BLOOM_FILTER_SIZE = 36000
3738
MAX_BLOOM_HASH_FUNCS = 50
3839

0 commit comments

Comments
 (0)