Skip to content

feat(core): expose Block::check#177

Open
pzafonte wants to merge 2 commits into
sedited:masterfrom
pzafonte:context-free-block-validation
Open

feat(core): expose Block::check#177
pzafonte wants to merge 2 commits into
sedited:masterfrom
pzafonte:context-free-block-validation

Conversation

@pzafonte
Copy link
Copy Markdown
Contributor

@pzafonte pzafonte commented May 7, 2026

Wraps btck_block_check from bitcoin/bitcoin#33908. The function performs, context-free validation of a block: size, weight, coinbase, transactions, sigops, without chainstate or block index access. Proof-of-work and merkle-root checks are optional via the BLOCK_CHECK_BASE / _POW / _MERKLE / _ALL flags.

Exposed as Block::check returning BlockCheckResult { Valid, Invalid(BlockValidationState) }. Raw btck_BlockCheckFlags_* constants are added to the sys crate's hand-rolled bindings; user-facing BLOCK_CHECK_* aliases in src/core/block.rs are re-exported via a block_check_flags submodule.

Tests are split per scenario as unit tests in src/core/block.rs (valid block, mutated merkle root, invalid PoW, tampered coinbase). A follow-up commit reuses MAINNET_BLOCK_1_HEX in test_block_hash_display and test_block_hash_ref_display since they validate the same block.

Closes #155.

@sedited
Copy link
Copy Markdown
Owner

sedited commented May 14, 2026

Can you rebase and undraft this?

@pzafonte pzafonte force-pushed the context-free-block-validation branch from 0533bfd to f3f404f Compare May 15, 2026 05:12
@pzafonte pzafonte marked this pull request as ready for review May 15, 2026 05:12
Copy link
Copy Markdown
Collaborator

@alexanderwiederin alexanderwiederin left a comment

Choose a reason for hiding this comment

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

Cool!

Could you also please add the change to the CHANGELOG?

Comment thread src/core/block.rs Outdated
Comment thread src/core/block.rs Outdated
Comment thread src/core/block.rs Outdated
Comment thread src/core/block.rs
Comment thread src/core/block.rs Outdated
Comment thread tests/test.rs Outdated
Comment thread tests/test.rs Outdated
Comment thread tests/test.rs Outdated
Comment thread tests/test.rs Outdated
Comment thread tests/test.rs Outdated
@pzafonte pzafonte force-pushed the context-free-block-validation branch 2 times, most recently from 57e5d47 to ec259f8 Compare May 15, 2026 13:36
@pzafonte
Copy link
Copy Markdown
Contributor Author

pzafonte commented May 15, 2026

Thanks for the review. I addressed everything in the latest commit except the as_ptr() as *mut. I'll rebase and switch the cast in Block::check to as_mut_ptr() once #178 goes through.

@pzafonte pzafonte changed the title feat(core): expose Block::check_context_free feat(core): expose Block::check May 15, 2026
Copy link
Copy Markdown
Collaborator

@alexanderwiederin alexanderwiederin left a comment

Choose a reason for hiding this comment

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

Nice turnaround.

Please rebase for #178 and #179.

The question on the method name was genuine; I am not sure whether Block::check() is better than Block::check_context_free(). Any thoughts from you or other reviewers?

Comment thread src/core/block.rs Outdated
Comment thread src/core/block.rs
Comment thread src/core/block.rs Outdated
@pzafonte
Copy link
Copy Markdown
Contributor Author

The question on the method name was genuine; I am not sure whether Block::check() is better than Block::check_context_free(). Any thoughts from you or other reviewers?

I lean towards Block::check_context_free() because it's self-documenting and there could plausibly be a contextual block check down the road.

@pzafonte
Copy link
Copy Markdown
Contributor Author

Changes updated in 26121eb

Reuses MAINNET_BLOCK_1_HEX in test_block_hash_display and test_block_hash_ref_display in 60e9bf6

Copy link
Copy Markdown
Collaborator

@alexanderwiederin alexanderwiederin left a comment

Choose a reason for hiding this comment

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

I lean towards Block::check_context_free() because it's self-documenting and there could plausibly be a contextual block check down the road.

Thanks for this. I do think there is merit to the check_context_free, but I doubt that a contextual block check would be called "check". The current proposals lean towards "connect" (bitcoin/bitcoin#32317) or "validate" (bitcoin/bitcoin#35187) and would be on the chainstate manager.

Either way, I wanted to hear your opinion on this.

I think check is more idiomatic Rust - discoverability is better addressed by the doc comments you've already added and the flags parameter, which signal what kind of check this is at the call site.

Comment thread src/core/block.rs Outdated
@sedited
Copy link
Copy Markdown
Owner

sedited commented May 20, 2026

@pzafonte this will require a rebase and probably a change to the bindings now.

@pzafonte pzafonte force-pushed the context-free-block-validation branch from 60e9bf6 to b1632cd Compare May 20, 2026 17:05
@pzafonte
Copy link
Copy Markdown
Contributor Author

pzafonte commented May 20, 2026

Makes sense, if other contextual variants use connect or validate then there's no collision. If any future check arrives then that method can take the qualifier, which is why I implicitly agreed with your suggestion.

Rebased on current master. Addressed the remaining feedback.

Comment thread src/core/block.rs Outdated
Comment thread src/core/block.rs Outdated
@pzafonte pzafonte force-pushed the context-free-block-validation branch from b1632cd to fa474d3 Compare May 21, 2026 17:16
Copy link
Copy Markdown
Collaborator

@alexanderwiederin alexanderwiederin left a comment

Choose a reason for hiding this comment

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

Almost there - thanks for pushing through!

Comment thread src/core/block.rs
Comment on lines +160 to +172
pub type BlockCheckFlags = btck_BlockCheckFlags;

/// Run only base context-free block checks (no PoW, no Merkle root).
pub const BLOCK_CHECK_BASE: btck_BlockCheckFlags = btck_BlockCheckFlags_BASE;
pub const BLOCK_CHECK_BASE: BlockCheckFlags = btck_BlockCheckFlags_BASE;

/// Enable Proof-of-Work verification via the block header.
pub const BLOCK_CHECK_POW: btck_BlockCheckFlags = btck_BlockCheckFlags_POW;
pub const BLOCK_CHECK_POW: BlockCheckFlags = btck_BlockCheckFlags_POW;

/// Enable Merkle-root verification (and mutation detection).
pub const BLOCK_CHECK_MERKLE: btck_BlockCheckFlags = btck_BlockCheckFlags_MERKLE;
pub const BLOCK_CHECK_MERKLE: BlockCheckFlags = btck_BlockCheckFlags_MERKLE;

/// Enable all available context-free block checks (PoW + Merkle root).
pub const BLOCK_CHECK_ALL: btck_BlockCheckFlags = btck_BlockCheckFlags_ALL;
pub const BLOCK_CHECK_ALL: BlockCheckFlags = btck_BlockCheckFlags_ALL;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be in the previous commit.

Comment thread src/core/block.rs
state::context::ChainParams,
KernelError,
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// Bitmask of flags controlling which checks [`Block::check`] performs.

I think a doc comment would make sense here.

pzafonte added 2 commits May 22, 2026 10:12
Wraps btck_block_check from bitcoin/bitcoin#33908, which performs
context-free validation of a block (size, weight, coinbase,
transactions, sigops) without chainstate or block index access.
Proof-of-work and merkle-root checks are optional via the
BLOCK_CHECK_* flags.

Block::check returns a BlockCheckResult enum carrying the
validation state on failure. Raw btck_BlockCheckFlags_*
constants are added to the sys crate's hand-rolled bindings.
Adds an AsPtr<btck_ChainParameters> impl for ChainParams.

Tests are split per scenario in src/core/block.rs: valid block,
mutated merkle root, invalid PoW, tampered coinbase.
Reuses the constant introduced for the Block::check tests, removing the duplicated inline hex from test_block_hash_display and test_block_hash_ref_display.
@pzafonte pzafonte force-pushed the context-free-block-validation branch from fa474d3 to 4b6e6a4 Compare May 22, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add context-free Block Validation

3 participants