kernel: Add function for copying block data to C header

This adds a function for streaming bytes into a user-owned data
structure.

Use it in the tests for verifying the implementation of the validation
interface's `BlockChecked` method.
This commit is contained in:
TheCharlatan
2024-06-01 11:52:52 +02:00
parent b30e15f432
commit a263a4caf2
4 changed files with 55 additions and 8 deletions

View File

@@ -967,6 +967,17 @@ const btck_Transaction* btck_block_get_transaction_at(const btck_Block* block, s
return btck_Transaction::ref(&btck_Block::get(block)->vtx[index]);
}
int btck_block_to_bytes(const btck_Block* block, btck_WriteBytes writer, void* user_data)
{
try {
WriterStream ws{writer, user_data};
ws << TX_WITH_WITNESS(*btck_Block::get(block));
return 0;
} catch (...) {
return -1;
}
}
void btck_block_destroy(btck_Block* block)
{
delete block;

View File

@@ -249,6 +249,11 @@ typedef void (*btck_ValidationInterfacePoWValidBlock)(void* user_data, btck_Bloc
typedef void (*btck_ValidationInterfaceBlockConnected)(void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry);
typedef void (*btck_ValidationInterfaceBlockDisconnected)(void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry);
/**
* Function signature for serializing data.
*/
typedef int (*btck_WriteBytes)(const void* bytes, size_t size, void* userdata);
/**
* Whether a validated data structure is valid, invalid, or an error was
* encountered during processing.
@@ -389,11 +394,6 @@ typedef uint8_t btck_ChainType;
#define btck_ChainType_SIGNET ((btck_ChainType)(3))
#define btck_ChainType_REGTEST ((btck_ChainType)(4))
/**
* Function signature for serializing data.
*/
typedef int (*btck_WriteBytes)(const void* bytes, size_t size, void* userdata);
/** @name Transaction
* Functions for working with transactions.
*/
@@ -983,6 +983,21 @@ BITCOINKERNEL_API size_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_count_trans
BITCOINKERNEL_API const btck_Transaction* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_get_transaction_at(
const btck_Block* block, size_t transaction_index) BITCOINKERNEL_ARG_NONNULL(1);
/**
* @brief Serializes the block through the passed in callback to bytes.
* This is consensus serialization that is also used for the P2P network.
*
* @param[in] block Non-null.
* @param[in] writer Non-null, callback to a write bytes function.
* @param[in] user_data Holds a user-defined opaque structure that will be
* passed back through the writer callback.
* @return 0 on success.
*/
BITCOINKERNEL_API int btck_block_to_bytes(
const btck_Block* block,
btck_WriteBytes writer,
void* user_data) BITCOINKERNEL_ARG_NONNULL(1, 2);
/**
* Destroy the block.
*/

View File

@@ -548,6 +548,11 @@ public:
}
MAKE_RANGE_METHOD(Transactions, Block, &Block::CountTransactions, &Block::GetTransaction, *this)
std::vector<std::byte> ToBytes() const
{
return write_bytes(get(), btck_block_to_bytes);
}
};
inline void logging_disable()