mirror of
https://github.com/logos-co/nomos-node.git
synced 2026-08-27 09:31:10 +00:00
feat(c-bindings): Pack txid with txs on c block subscriptions (#3011)
This commit is contained in:
Generated
+1
@@ -4158,6 +4158,7 @@ dependencies = [
|
||||
"logos-blockchain-wallet-service",
|
||||
"multiaddr",
|
||||
"overwatch",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"tempfile",
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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!(
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user