kernel: Expose context-free block validation

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 <yuvichh01@gmail.com>
This commit is contained in:
w0xlt
2025-11-19 13:16:19 -08:00
parent 71f827c3c2
commit 0587c56091
4 changed files with 130 additions and 0 deletions

View File

@@ -1129,6 +1129,19 @@ btck_Block* btck_block_copy(const btck_Block* block)
return btck_Block::copy(block);
}
int btck_block_check(const btck_Block* block, const btck_ConsensusParams* consensus_params, btck_BlockCheckFlags flags, btck_BlockValidationState* validation_state)
{
auto& state = btck_BlockValidationState::get(validation_state);
state = BlockValidationState{};
const bool check_pow = (flags & btck_BlockCheckFlags_POW) != 0;
const bool check_merkle = (flags & btck_BlockCheckFlags_MERKLE) != 0;
const bool result = CheckBlock(*btck_Block::get(block), state, btck_ConsensusParams::get(consensus_params), /*fCheckPOW=*/check_pow, /*fCheckMerkleRoot=*/check_merkle);
return result ? 1 : 0;
}
size_t btck_block_count_transactions(const btck_Block* block)
{
return btck_Block::get(block)->vtx.size();