kernel: Expose btck_transaction_check consensus function

Add btck_transaction_check() to the libbitcoinkernel C API, exposing
context-free transaction consensus validation (consensus/tx_check.h).

Introduces btck_TxValidationState with introspection and lifecycle
functions. btck_TxValidationResult is exposed for compatibility with
existing validation-state APIs, though btck_transaction_check currently
reaches only UNSET and CONSENSUS.

Includes C++ wrapper and test coverage for btck_transaction_check using
test vectors from tx_valid.json / tx_invalid.json.
This commit is contained in:
w0xlt
2025-11-04 14:41:17 -08:00
parent 404470505a
commit bfbf1a7ef3
4 changed files with 293 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
#include <chain.h>
#include <coins.h>
#include <consensus/tx_check.h>
#include <consensus/validation.h>
#include <dbwrapper.h>
#include <kernel/caches.h>
@@ -146,6 +147,7 @@ struct Handle {
struct btck_BlockTreeEntry: Handle<btck_BlockTreeEntry, CBlockIndex> {};
struct btck_Block : Handle<btck_Block, std::shared_ptr<const CBlock>> {};
struct btck_BlockValidationState : Handle<btck_BlockValidationState, BlockValidationState> {};
struct btck_TxValidationState : Handle<btck_TxValidationState, TxValidationState> {};
namespace {
@@ -1444,3 +1446,49 @@ void btck_block_header_destroy(btck_BlockHeader* header)
{
delete header;
}
btck_ValidationMode btck_tx_validation_state_get_validation_mode(const btck_TxValidationState* state_)
{
const auto& state = btck_TxValidationState::get(state_);
if (state.IsValid()) return btck_ValidationMode_VALID;
if (state.IsInvalid()) return btck_ValidationMode_INVALID;
return btck_ValidationMode_INTERNAL_ERROR;
}
btck_TxValidationState* btck_tx_validation_state_create()
{
return btck_TxValidationState::create();
}
btck_TxValidationResult btck_tx_validation_state_get_tx_validation_result(const btck_TxValidationState* state_)
{
switch (btck_TxValidationState::get(state_).GetResult()) {
case TxValidationResult::TX_RESULT_UNSET: return btck_TxValidationResult_UNSET;
case TxValidationResult::TX_CONSENSUS: return btck_TxValidationResult_CONSENSUS;
case TxValidationResult::TX_INPUTS_NOT_STANDARD: return btck_TxValidationResult_INPUTS_NOT_STANDARD;
case TxValidationResult::TX_NOT_STANDARD: return btck_TxValidationResult_NOT_STANDARD;
case TxValidationResult::TX_MISSING_INPUTS: return btck_TxValidationResult_MISSING_INPUTS;
case TxValidationResult::TX_PREMATURE_SPEND: return btck_TxValidationResult_PREMATURE_SPEND;
case TxValidationResult::TX_WITNESS_MUTATED: return btck_TxValidationResult_WITNESS_MUTATED;
case TxValidationResult::TX_WITNESS_STRIPPED: return btck_TxValidationResult_WITNESS_STRIPPED;
case TxValidationResult::TX_CONFLICT: return btck_TxValidationResult_CONFLICT;
case TxValidationResult::TX_MEMPOOL_POLICY: return btck_TxValidationResult_MEMPOOL_POLICY;
case TxValidationResult::TX_NO_MEMPOOL: return btck_TxValidationResult_NO_MEMPOOL;
case TxValidationResult::TX_RECONSIDERABLE: return btck_TxValidationResult_RECONSIDERABLE;
case TxValidationResult::TX_UNKNOWN: return btck_TxValidationResult_UNKNOWN;
} // no default case, so the compiler can warn about missing cases
assert(false);
}
void btck_tx_validation_state_destroy(btck_TxValidationState* state)
{
delete state;
}
int btck_transaction_check(const btck_Transaction* tx, btck_TxValidationState* validation_state)
{
auto& state = btck_TxValidationState::get(validation_state);
state = TxValidationState{};
const bool ok = CheckTransaction(*btck_Transaction::get(tx), state);
return ok ? 1 : 0;
}