feat(c-bindings): Pack txid with txs on c block subscriptions (#3011)

This commit is contained in:
Daniel Sanchez
2026-06-23 12:53:54 +02:00
committed by GitHub
parent 0eda662eb5
commit 113faae6fd
4 changed files with 48 additions and 4 deletions
Generated
+1
View File
@@ -4158,6 +4158,7 @@ dependencies = [
"logos-blockchain-wallet-service",
"multiaddr",
"overwatch",
"serde",
"serde_json",
"serde_yaml",
"tempfile",
+1
View File
@@ -22,6 +22,7 @@ lb-utils = { workspace = true }
lb-wallet-service = { workspace = true }
multiaddr = { workspace = true }
overwatch = { workspace = true }
serde = { features = ["derive"], workspace = true }
serde_json = { workspace = true }
tempfile = { workspace = true }
tokio = { features = ["rt-multi-thread"], workspace = true }
+41 -1
View File
@@ -2,11 +2,15 @@ use std::ffi::c_char;
use lb_api_service::http::storage::StorageAdapter as _;
use lb_chain_service::api::CryptarchiaServiceApi;
use lb_core::block::Block as CoreBlock;
use lb_core::{
block::Block as CoreBlock,
mantle::{StorageSize, Transaction, TransactionHasher, TxHash},
};
use lb_node::{
ApiStorageAdapter, RuntimeServiceId, SignedMantleTx, StorageService,
generic_services::CryptarchiaService,
};
use serde::Serialize;
use crate::{
LogosBlockchainNode, OperationStatus,
@@ -15,6 +19,29 @@ use crate::{
logging, return_error_if_null_pointer,
};
#[derive(Serialize)]
#[serde(rename = "SignedMantleTx")]
pub struct TxWithId {
id: TxHash,
#[serde(flatten)]
tx: SignedMantleTx,
}
impl Transaction for TxWithId {
const HASHER: TransactionHasher<Self> = |tx| <SignedMantleTx as Transaction>::HASHER(&tx.tx);
type Hash = <SignedMantleTx as Transaction>::Hash;
fn as_signing(&self) -> Vec<u8> {
self.tx.as_signing()
}
}
impl StorageSize for TxWithId {
fn storage_size(&self) -> usize {
self.tx.storage_size()
}
}
pub fn subscribe_to_new_blocks_sync(
node: &LogosBlockchainNode,
mut callback_per_block: BoxedCallback<*const c_char>,
@@ -52,6 +79,19 @@ pub fn subscribe_to_new_blocks_sync(
ApiStorageAdapter::<RuntimeServiceId>::get_block(relay, event.block_id)
.await;
if let Ok(Some(block)) = res {
let txs_with_id: Vec<TxWithId> = block
.transactions()
.map(|tx| TxWithId {
id: tx.hash(),
tx: tx.clone(),
})
.collect();
let block: CoreBlock<TxWithId> = CoreBlock::reconstruct(
block.header().clone(),
txs_with_id,
*block.signature(),
)
.expect("Block should always build from valid block");
callback_per_block(Block::from(block).as_ptr());
} else {
logging::error!(
+5 -3
View File
@@ -1,6 +1,8 @@
use std::ffi::{CString, c_char};
use lb_core::{block::Block as CoreBlock, mantle::SignedMantleTx};
use lb_core::block::Block as CoreBlock;
use crate::api::subscriptions::TxWithId;
#[repr(C)]
pub struct Block(CString); // JSON representation of a block
@@ -11,8 +13,8 @@ impl Block {
}
}
impl From<CoreBlock<SignedMantleTx>> for Block {
fn from(value: CoreBlock<SignedMantleTx>) -> Self {
impl From<CoreBlock<TxWithId>> for Block {
fn from(value: CoreBlock<TxWithId>) -> Self {
Self(
CString::new(
serde_json::to_string(&value)