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), with optional proof-of-work and merkle-root checks toggled via the BLOCK_CHECK_BASE / _POW / _MERKLE / _ALL flags. Useful for early rejection of invalid blocks before any chainstate-dependent validation runs.

The Rust signature mirrors the C contract: validation_state is filled in-place and the function returns a bool. Adds an AsPtr<btck_ChainParameters> impl on ChainParams and re-exports the owned BlockValidationState type since callers now need to construct one. The flag constants follow the existing VERIFY_* pattern and are also exposed via a block_check_flags submodule mirroring verify_flags.

Test mirrors upstream's btck_check_block_context_free case-by-case (valid block, mutated merkle, bad PoW, tampered coinbase, truncated bytes), reusing a single state to verify the documented reset-on-entry behavior.

Draft until subtree is synced, but tested locally on updated subree.

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
/// # Ok(())
/// # }
/// ```
pub fn check_context_free(
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.

I think we can call the method check(). What do you think?

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
self.inner,
consensus_params,
flags,
validation_state.as_ptr() as *mut btck_BlockValidationState,
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.

The .as_ptr() as *mut is a bit of a faux pas. Added PR to remove the implicit cast, which can be used here.

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 added 2 commits May 18, 2026 18:36
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.  Adds an AsPtr<btck_ChainParameters>
impl for ChainParams. Raw BTCK_BLOCK_CHECK_FLAGS_* constants live
in src/ffi/constants.rs with user-facing BLOCK_CHECK_* aliases,
mirroring the VERIFY_* pattern and exposed via a block_check_flags
submodule.

Tests live as unit tests in src/core/block.rs, split per scenario:
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 ec259f8 to 60e9bf6 Compare May 18, 2026 22:38
@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

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