Skip to content

mining: include chunks that reach block limits - #36156

Open
l0rinc wants to merge 5 commits into
bitcoin:masterfrom
l0rinc:l0rinc/mine-exact-limit-chunks
Open

l0rinc wants to merge 5 commits into
bitcoin:masterfrom
l0rinc:l0rinc/mine-exact-limit-chunks

Conversation

@l0rinc

@l0rinc l0rinc commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem: BlockAssembler fills block templates with mempool chunks that fit the configured weight limit and the consensus sigops limit, but currently rejects a chunk when it brings either total exactly to its limit.
Equality is valid because block_max_weight is a maximum and BIP 141 defines both consensus limits using ≤.
The >= checks can therefore leave valid capacity unused and omit a fee-paying chunk.
Mining option validation also permits block_max_weight == MAX_BLOCK_WEIGHT and coinbase_output_max_additional_sigops == MAX_BLOCK_SIGOPS_COST, while the chunk checks reject an accounted total equal to either limit.
A 2016 review comment on the original package-selection PR raised the same inclusive-limit point.

Fix: Use strict greater-than comparisons so exact-limit chunks are included.
Extend the existing chunk-limit test to cover exact fits and one-unit overages for both weight and sigops.
Clarify how mining software reserves weight and sigops for the completed block.

This addresses the first issue in #35596.

@DrahtBot DrahtBot added the Mining label Sep 3, 2026
@DrahtBot

DrahtBot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36156.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK ismaelsadeeq

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36852 (test: split CreateNewBlock_validity into independent test cases by yuvicc)
  • #36525 (mining: add Block interface, fix template data race by Sjors)
  • #36318 (validation: Ensure Invalid ValidationState has result by optout21)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@ajtowns

ajtowns commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

This seems backwards to me: the risk of a mistake here is that the block becomes invalid losing all possible reward from mining it, while the benefit of success is perhaps a single larger / additional transaction at the bottom of the block. Currently the bottom transaction in a block seems to pay less than 1000sats in total fees, so generously, that's less than 80c per block, as compared to the subsidy of around $240k per block.

@ismaelsadeeq

Copy link
Copy Markdown
Member

Concept ACK

Looks correct to me.

This seems backwards to me: the risk of a mistake here is that the block becomes invalid losing all possible reward from mining it, while the benefit of success is perhaps a single larger / additional transaction at the bottom of the block.

This fix seems straightforward and correct, with good tests. Will the mistake come from the individual chunks sigops or weight accounting error? If indeed we do have that error, we still could end up creating an invalid block even without this fix.

If we are confident that our accounting of weight and sigops is correct without this PR, and that the block assembler will not create an invalid block, then fixing this issue is okay.

Currently the bottom transaction in a block seems to pay less than 1000sats in total fees, so generously, that's less than 80c per block, as compared to the subsidy of around $240k per block.

I think it's less about the revenue but more about making the block assembler code correct and reflect consensus allowed limits, and making it explicit that filled blocks may be created, are valid and will be accepted as part of the blockchain.

For me the more risky part is when we allow this minor bugs in the code, it incentivise pool companies to patch the code themselves to fix it, fwiw F2Pool did fix these bugs and have their custom binaries which create filled blocks.

@ajtowns

ajtowns commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

For me the more risky part is when we allow this minor bugs in the code, it incentivise pool companies to patch the code themselves to fix it, fwiw F2Pool did fix these bugs and have their custom binaries which create filled blocks.

The issue you're referencing was in regards to 500vb of wasted space, versus this PR which is a delta of 1vb of space. The block you're referring to has a 2127 WU unused:

$ bitcoin-cli getblock 00000000000000000001eac8304428ea2fc5fc56e9ffefec848efde3c5a4f279 | jq 4e6-.weight
2127

It's certainly bad if mining pools introduce bugs in their software that invalidate blocks while trying to get to the very edge of valid behaviour, but at least it only impacts a single pool that way. I don't see why we would want to play the same stupid game and risk winning the same stupid prize. Off by one errors are common; removing a defensive buffer against that might be appropriate if there was a significant win to be had, but there isn't here.

Anyway, I've said my piece, I'll unsubscribe.

Record that `BlockAssembler` omits a chunk at `block_max_weight` exactly, while rejecting one weight unit over.
The configured `block_max_weight` is inclusive, so accept a chunk whose accounted weight reaches it exactly.
Record that `BlockAssembler` omits a chunk at `MAX_BLOCK_SIGOPS_COST` exactly, while rejecting one cost unit over.
Consensus permits the sigops cost limit exactly, so accept chunks that reach `MAX_BLOCK_SIGOPS_COST`.
@l0rinc l0rinc changed the title mining: include chunks that exactly fill block limits mining: include chunks that reach block limits Sep 26, 2026
@l0rinc
l0rinc force-pushed the l0rinc/mine-exact-limit-chunks branch from dc73908 to 8e7de8e Compare September 28, 2026 03:36
@l0rinc

l0rinc commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

While looking into why the current check uses >=, I found a comment on the original package checks also suggesting >.
Given that mining options already allow a block_max_weight of MAX_BLOCK_WEIGHT and a coinbase sigops reservation of MAX_BLOCK_SIGOPS_COST, I think accepting equality in chunk selection too would make these limits more consistent.

I understand the concern about risking an invalid block for such a small gain, so I added test cases one unit over each limit and clarified the existing reservations in the docs. I’d like to hear how others weigh that against the risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants