Skip to content

blockstorage: fix partial-mask assertions and genesis availability checks - #36971

Open
l0rinc wants to merge 5 commits into
bitcoin:masterfrom
l0rinc:l0rinc/block-data-availability-mask
Open

l0rinc wants to merge 5 commits into
bitcoin:masterfrom
l0rinc:l0rinc/block-data-availability-mask

Conversation

@l0rinc

@l0rinc l0rinc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Problem: CheckBlockDataAvailability checks whether the upper block has any requested bit. GetFirstBlock requires every requested bit, so a partial match can reach its assertion. At genesis, the existing undo exception can reject an undo-only request or accept a request with missing block data. It also misses ranges containing only genesis.

Fix: Require every requested bit on each block in the range, except that genesis needs no undo data. An empty mask now succeeds for a valid range because it requests no bits. Both index-startup calls supply nonempty masks.

Record the availability results for genesis and partially available blocks before fixing the mask checks.
Use `Assert` for the full-mask precondition in `GetFirstBlock` so the unit test can observe which availability requests violate it.
The genesis undo exception accepts any matching requested flag, so `BLOCK_OPT_WITNESS` can hide missing block data at genesis.
Check the lower block for missing requested bits before walking the range, exempting only genesis undo data.
When both endpoints are the same pruned block with only undo data, a request for block data and undo data now returns `false` before reaching the walker assertion.
Assert the existing `cs_main` requirement on entry so it also covers the early returns.
When the endpoints coincide, checking the lower block covers the whole range.
Return early in that case so neither the upper block check nor `GetFirstBlock` requires undo data from genesis.
This also accepts an empty mask on a single-block range because it requests no bits.
`GetFirstBlock` requires every requested bit on its starting block.
Reject an upper block missing any of those bits before calling the walker.
This also accepts an empty mask on a multi-block range because it requests no bits.
The old genesis branch rejected undo-only requests because it looked for a matching flag after removing the only requested bit.
Genesis has already passed the lower block check.
If the walk stops at its child because genesis lacks undo data, the whole range is available.
Every other lower block has already passed the full mask check, so the walk cannot stop at its child.
@DrahtBot

DrahtBot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Type Reviewers
ACK jeanpablojp

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:

  • #36946 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #35731 (Indexes: Harden the flush-error notification invariant by arejula27)

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.

@jeanpablojp jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 41851dc

Left 2 small nits.

Comment thread src/node/blockstorage.h
Comment on lines 416 to 417
//! Check if all blocks in the [upper_block, lower_block] range have data available as
//! defined by the status mask.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could the comment say every flag is required, like the PR description does?

Suggested change
//! Check if all blocks in the [upper_block, lower_block] range have data available as
//! defined by the status mask.
//! Check if all blocks in the [upper_block, lower_block] range have all
//! block_status flags set.

BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
BOOST_REQUIRE_EQUAL(last_pruned_block->nStatus & BLOCK_HAVE_MASK, BLOCK_HAVE_UNDO);
BOOST_CHECK(!blockman.CheckBlockDataAvailability(*last_pruned_block, *last_pruned_block, BLOCK_HAVE_MASK));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the two empty-mask checks Ralph suggested make sense here. Feel free to ignore, though.

Suggested change
BOOST_CHECK(!blockman.CheckBlockDataAvailability(*last_pruned_block, *last_pruned_block, BLOCK_HAVE_MASK));
BOOST_CHECK(!blockman.CheckBlockDataAvailability(*last_pruned_block, *last_pruned_block, BLOCK_HAVE_MASK));
BOOST_CHECK(blockman.CheckBlockDataAvailability(*last_pruned_block, *last_pruned_block, BlockStatus{}));
BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *last_pruned_block, BlockStatus{}));

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants