-
Notifications
You must be signed in to change notification settings - Fork 38.7k
kernel: add context‑free block validation API (btck_check_block_context_free) with POW/Merkle flags
#33908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33908. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. LLM Linter (✨ experimental)Possible typos and grammar issues:
drahtbot_id_5_m |
10ce82d to
e6d571b
Compare
|
Concept ACK, Approach NACK from my side. Currently, The use of bitflags for extensibility look good imo. |
|
Both @w0xlt and @yuvicc, please don't introduce more boolean traps. |
Library users currently need to maintain a full context object to perform context-free block validation. Exposing an opaque `btck_ConsensusParams` struct allows callers to supply only the required consensus parameters, resulting in a lighter-weight API and a clearer expression of the actual validation behavior. Co-authored-by: yuvicc <[email protected]>
e6d571b to
c8d4aed
Compare
This change exposes create and destroy functions for `btck_BlockValidationState` in the C API and splits the C++ wrapper into an owning `BlockValidationState` (which allocates memory) and a non-owning `BlockValidationStateView`. Co-authored-by: yuvicc <[email protected]>
This introduces a context-free validation entry point for full blocks in the kernel C and C++ APIs. * Add `btck_block_check`, a C function that wraps `CheckBlock` and runs header and body checks for a `btck_Block` using `btck_ConsensusParams`. Callers provide a `btck_BlockValidationState` to receive the result and supply a `btck_BlockCheckFlags` bitmask to control POW and merkle-root verification. * Add `btck_BlockCheckFlags` in the C API, plus the corresponding `BlockCheckFlags` scoped enum in the C++ wrapper, including a `*_ALL` convenience value. * Add `Block::Check()` to the C++ wrapper to mirror the new C function and return a bool while filling a `BlockValidationState`. * Add a test `(btck_check_block_context_free)` that verifies a known valid mainnet block passes with `BlockCheckFlags::ALL` and that truncated block data fails deserialization. Co-authored-by: yuvicc <[email protected]>
c8d4aed to
824b5c4
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
Thanks for the review @yuvicc and @purpleKarrot . Great idea exposing the chain parameter class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK
Thanks for taking my suggestion.
| * @return The block validation state, or null on error. | ||
| */ | ||
| BITCOINKERNEL_API btck_BlockValidationState* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_validation_state_create(); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we also expose _copy API here:
| /** | |
| * @brief Copies the block validation state. | |
| * | |
| * @param[in] block_validation_state Non-null. | |
| * @return The copied block validation state. | |
| */ | |
| BITCOINKERNEL_API btck_BlockValidationState* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_validation_state_copy( | |
| const btck_BlockValidationState* block_validation_state) BITCOINKERNEL_ARG_NONNULL(1); | |
| explicit BlockValidationStateView(const btck_BlockValidationState* ptr) : View{ptr} {} | ||
| }; | ||
|
|
||
| class BlockValidationState : public UniqueHandle<btck_BlockValidationState, btck_block_validation_state_destroy>, public BlockValidationStateApi<BlockValidationState> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then use Handle instead of UniqueHandle here:
| class BlockValidationState : public UniqueHandle<btck_BlockValidationState, btck_block_validation_state_destroy>, public BlockValidationStateApi<BlockValidationState> | |
| class BlockValidationState : public Handle<btck_BlockValidationState, btck_block_validation_state_copy, btck_block_validation_state_destroy>, public BlockValidationStateApi<BlockValidationState> |
| class BlockValidationState : public UniqueHandle<btck_BlockValidationState, btck_block_validation_state_destroy>, public BlockValidationStateApi<BlockValidationState> | ||
| { | ||
| public: | ||
| BlockValidationState() : UniqueHandle{btck_block_validation_state_create()} {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here:
| BlockValidationState() : UniqueHandle{btck_block_validation_state_create()} {} | |
| explicit BlockValidationState() : Handle{btck_block_validation_state_create()} {} |
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
I think the most important question is this: Is there a valid use case for an invalid block? |
This PR exposes Bitcoin Core’s context‑free block checks to library users via a new C API entry point,
btck_check_block_context_free.Callers can validate a block’s structure (size/weight, coinbase rules, per‑tx context‑free checks) and optionally re‑run Proof‑of‑Work and Merkle‑root verification without touching chainstate, the block index, or the UTXO set.
Rationale
Clients embedding the kernel need a pure block sanity check without requiring node state or disk writes (candidate block validation, for example). This API offers that surface in a single call, with optional PoW/Merkle toggles to avoid redundant work when the header has already been validated or when Merkle verification is deferred.