Skip to content

Commit c504b69

Browse files
Sjorsryanofsky
andcommitted
refactor: add coinbase constraints to BlockCreateOptions
When generating a block template through e.g. getblocktemplate RPC, we reserve 4000 weight units and 400 sigops. Pools use this space for their coinbase outputs. At least one pool patched their Bitcoin Core node to adjust these hardcoded values. They eventually produced an invalid block which exceeded the sigops limit. https://bitcoin.stackexchange.com/questions/117837/how-many-sigops-are-in-the-invalid-block-783426 The existince of such patches suggests it may be useful to make this value configurable. This commit would make such a change easier. The main motivation however is that the Stratum v2 spec requires the pool to communicate the maximum bytes they intend to add to the coinbase outputs. A proposed change to the spec would also require them to communicate the maximum number of sigops. This commit also documents what happens when -blockmaxweight is lower than the coinbase reserved value. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
1 parent 6b4c817 commit c504b69

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/node/miner.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
5959

6060
static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
6161
{
62-
// Limit weight to between 4K and DEFAULT_BLOCK_MAX_WEIGHT for sanity:
63-
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, 4000, DEFAULT_BLOCK_MAX_WEIGHT);
62+
Assert(options.coinbase_max_additional_weight <= DEFAULT_BLOCK_MAX_WEIGHT);
63+
Assert(options.coinbase_output_max_additional_sigops <= MAX_BLOCK_SIGOPS_COST);
64+
// Limit weight to between coinbase_max_additional_weight and DEFAULT_BLOCK_MAX_WEIGHT for sanity:
65+
// Coinbase (reserved) outputs can safely exceed -blockmaxweight, but the rest of the block template will be empty.
66+
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, options.coinbase_max_additional_weight, DEFAULT_BLOCK_MAX_WEIGHT);
6467
return options;
6568
}
6669

@@ -87,8 +90,8 @@ void BlockAssembler::resetBlock()
8790
inBlock.clear();
8891

8992
// Reserve space for coinbase tx
90-
nBlockWeight = 4000;
91-
nBlockSigOpsCost = 400;
93+
nBlockWeight = m_options.coinbase_max_additional_weight;
94+
nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
9295

9396
// These counters do not include coinbase tx
9497
nBlockTx = 0;
@@ -379,7 +382,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
379382
++nConsecutiveFailed;
380383

381384
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
382-
m_options.nBlockMaxWeight - 4000) {
385+
m_options.nBlockMaxWeight - m_options.coinbase_max_additional_weight) {
383386
// Give up if we're close to full and haven't succeeded in a while
384387
break;
385388
}

src/node/types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ struct BlockCreateOptions {
3232
* Set false to omit mempool transactions
3333
*/
3434
bool use_mempool{true};
35+
/**
36+
* The maximum additional weight which the pool will add to the coinbase
37+
* scriptSig, witness and outputs. This must include any additional
38+
* weight needed for larger CompactSize encoded lengths.
39+
*/
40+
size_t coinbase_max_additional_weight{4000};
41+
/**
42+
* The maximum additional sigops which the pool will add in coinbase
43+
* transaction outputs.
44+
*/
45+
size_t coinbase_output_max_additional_sigops{400};
3546
};
3647
} // namespace node
3748

0 commit comments

Comments
 (0)