From 6054ae113a0d2c4cbced51c1b7e9209a6e5a8851 Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Thu, 7 May 2026 14:29:06 +0300 Subject: [PATCH] fix(indexer_ffi): suggestion fix --- indexer_ffi/Cargo.toml | 2 +- indexer_ffi/src/api/types/account.rs | 55 ++++++++++-- indexer_ffi/src/api/types/block.rs | 51 ++++++------ indexer_ffi/src/api/types/transaction.rs | 101 ++++++++++++++--------- integration_tests/tests/indexer_ffi.rs | 6 +- 5 files changed, 138 insertions(+), 77 deletions(-) diff --git a/indexer_ffi/Cargo.toml b/indexer_ffi/Cargo.toml index 1deb8fad..1e6b1468 100644 --- a/indexer_ffi/Cargo.toml +++ b/indexer_ffi/Cargo.toml @@ -7,7 +7,7 @@ version = "0.1.0" [dependencies] nssa.workspace = true indexer_service.workspace = true -indexer_service_rpc.workspace = true +indexer_service_rpc = { workspace = true, features = ["client"] } indexer_service_protocol.workspace = true url.workspace = true diff --git a/indexer_ffi/src/api/types/account.rs b/indexer_ffi/src/api/types/account.rs index 7893657b..6c35347f 100644 --- a/indexer_ffi/src/api/types/account.rs +++ b/indexer_ffi/src/api/types/account.rs @@ -7,7 +7,6 @@ use crate::api::types::{FfiBytes32, FfiProgramId, FfiU128}; /// Note: `balance` and `nonce` are u128 values represented as little-endian /// byte arrays since C doesn't have native u128 support. #[repr(C)] -#[derive(Clone)] pub struct FfiAccount { pub program_owner: FfiProgramId, /// Balance as little-endian [u8; 16]. @@ -32,31 +31,69 @@ impl From<&nssa::AccountId> for FfiBytes32 { impl From for FfiAccount { fn from(value: nssa::Account) -> Self { - let (data, data_len, data_cap) = value.data.into_inner().into_raw_parts(); + let nssa::Account { + program_owner, + balance, + data, + nonce, + } = value; + + let (data, data_len, data_cap) = data.into_inner().into_raw_parts(); let program_owner = FfiProgramId { - data: value.program_owner, + data: program_owner, }; Self { program_owner, - balance: value.balance.into(), + balance: balance.into(), data, data_len, data_cap, - nonce: value.nonce.0.into(), + nonce: nonce.0.into(), } } } impl From for indexer_service_protocol::Account { fn from(value: FfiAccount) -> Self { + let FfiAccount { + program_owner, + balance, + data, + data_cap, + data_len, + nonce, + } = value; + Self { - program_owner: ProgramId(value.program_owner.data), - balance: value.balance.into(), + program_owner: ProgramId(program_owner.data), + balance: balance.into(), data: indexer_service_protocol::Data(unsafe { - Vec::from_raw_parts(value.data, value.data_len, value.data_cap) + Vec::from_raw_parts(data, data_len, data_cap) }), - nonce: value.nonce.into(), + nonce: nonce.into(), + } + } +} + +impl From<&FfiAccount> for indexer_service_protocol::Account { + fn from(value: &FfiAccount) -> Self { + let &FfiAccount { + program_owner, + balance, + data, + data_cap, + data_len, + nonce, + } = value; + + Self { + program_owner: ProgramId(program_owner.data), + balance: balance.into(), + data: indexer_service_protocol::Data(unsafe { + Vec::from_raw_parts(data, data_len, data_cap) + }), + nonce: nonce.into(), } } } diff --git a/indexer_ffi/src/api/types/block.rs b/indexer_ffi/src/api/types/block.rs index f7e0c778..bca2fdb5 100644 --- a/indexer_ffi/src/api/types/block.rs +++ b/indexer_ffi/src/api/types/block.rs @@ -17,38 +17,27 @@ pub struct FfiBlock { impl From for FfiBlock { fn from(value: Block) -> Self { + let Block { + header, + body, + bedrock_status, + bedrock_parent_id, + } = value; + Self { - header: value.header.into(), - body: value - .body + header: header.into(), + body: body .transactions .into_iter() .map(Into::into) .collect::>() .into(), - bedrock_status: value.bedrock_status.into(), - bedrock_parent_id: value.bedrock_parent_id.into(), + bedrock_status: bedrock_status.into(), + bedrock_parent_id: bedrock_parent_id.into(), } } } -// impl From> for Block { -// fn from(value: Box) -> Self { -// Self { -// header: BlockHeader { -// block_id: value.header.block_id, -// prev_block_hash: HashType(value.header.prev_block_hash.data), -// hash: HashType(value.header.hash.data), -// timestamp: value.header.timestamp, -// signature: Signature(value.header.signature.data), -// }, -// body: (), -// bedrock_status: value.bedrock_status.into(), -// bedrock_parent_id: MantleMsgId(value.bedrock_parent_id.data), -// } -// } -// } - pub type FfiBlockOpt = FfiOption; #[repr(C)] @@ -62,12 +51,20 @@ pub struct FfiBlockHeader { impl From for FfiBlockHeader { fn from(value: BlockHeader) -> Self { + let BlockHeader { + block_id, + prev_block_hash, + hash, + timestamp, + signature, + } = value; + Self { - block_id: value.block_id, - prev_block_hash: value.prev_block_hash.into(), - hash: value.hash.into(), - timestamp: value.timestamp, - signature: value.signature.into(), + block_id, + prev_block_hash: prev_block_hash.into(), + hash: hash.into(), + timestamp, + signature: signature.into(), } } } diff --git a/indexer_ffi/src/api/types/transaction.rs b/indexer_ffi/src/api/types/transaction.rs index ff14276f..ee3bd01b 100644 --- a/indexer_ffi/src/api/types/transaction.rs +++ b/indexer_ffi/src/api/types/transaction.rs @@ -24,11 +24,16 @@ pub struct FfiPublicTransactionBody { impl From for FfiPublicTransactionBody { fn from(value: PublicTransaction) -> Self { + let PublicTransaction { + hash, + message, + witness_set, + } = value; + Self { - hash: value.hash.into(), - message: value.message.into(), - witness_set: value - .witness_set + hash: hash.into(), + message: message.into(), + witness_set: witness_set .signatures_and_public_keys .into_iter() .map(Into::into) @@ -88,21 +93,26 @@ pub struct FfiPublicMessage { impl From for FfiPublicMessage { fn from(value: PublicMessage) -> Self { + let PublicMessage { + program_id, + account_ids, + nonces, + instruction_data, + } = value; + Self { - program_id: value.program_id.into(), - account_ids: value - .account_ids + program_id: program_id.into(), + account_ids: account_ids .into_iter() .map(Into::into) .collect::>() .into(), - nonces: value - .nonces + nonces: nonces .into_iter() .map(Into::into) .collect::>() .into(), - instruction_data: value.instruction_data.into(), + instruction_data: instruction_data.into(), } } } @@ -117,18 +127,22 @@ pub struct FfiPrivateTransactionBody { impl From for FfiPrivateTransactionBody { fn from(value: PrivacyPreservingTransaction) -> Self { + let PrivacyPreservingTransaction { + hash, + message, + witness_set, + } = value; + Self { - hash: value.hash.into(), - message: value.message.into(), - witness_set: value - .witness_set + hash: hash.into(), + message: message.into(), + witness_set: witness_set .signatures_and_public_keys .into_iter() .map(Into::into) .collect::>() .into(), - proof: value - .witness_set + proof: witness_set .proof .expect("Private execution: proof must be present") .0 @@ -229,21 +243,29 @@ pub struct FfiPrivacyPreservingMessage { impl From for FfiPrivacyPreservingMessage { fn from(value: PrivacyPreservingMessage) -> Self { + let PrivacyPreservingMessage { + public_account_ids, + nonces, + public_post_states, + encrypted_private_post_states, + new_commitments, + new_nullifiers, + block_validity_window, + timestamp_validity_window, + } = value; + Self { - public_account_ids: value - .public_account_ids + public_account_ids: public_account_ids .into_iter() .map(Into::into) .collect::>() .into(), - nonces: value - .nonces + nonces: nonces .into_iter() .map(Into::into) .collect::>() .into(), - public_post_states: value - .public_post_states + public_post_states: public_post_states .into_iter() .map(|acc_ind| -> nssa::Account { acc_ind.try_into().expect("Source is in blocks, must fit") @@ -251,26 +273,23 @@ impl From for FfiPrivacyPreservingMessage { .map(Into::into) .collect::>() .into(), - encrypted_private_post_states: value - .encrypted_private_post_states + encrypted_private_post_states: encrypted_private_post_states .into_iter() .map(Into::into) .collect::>() .into(), - new_commitments: value - .new_commitments + new_commitments: new_commitments .into_iter() .map(|comm| FfiBytes32 { data: comm.0 }) .collect::>() .into(), - new_nullifiers: value - .new_nullifiers + new_nullifiers: new_nullifiers .into_iter() .map(Into::into) .collect::>() .into(), - block_validity_window: cast_validity_window(value.block_validity_window), - timestamp_validity_window: cast_validity_window(value.timestamp_validity_window), + block_validity_window: cast_validity_window(block_validity_window), + timestamp_validity_window: cast_validity_window(timestamp_validity_window), } } } @@ -299,10 +318,16 @@ pub struct FfiEncryptedAccountData { impl From for FfiEncryptedAccountData { fn from(value: EncryptedAccountData) -> Self { + let EncryptedAccountData { + ciphertext, + epk, + view_tag, + } = value; + Self { - ciphertext: value.ciphertext.0.into(), - epk: value.epk.0.into(), - view_tag: value.view_tag, + ciphertext: ciphertext.0.into(), + epk: epk.0.into(), + view_tag, } } } @@ -341,9 +366,11 @@ impl From> for ProgramDeploymentTransac impl From for FfiProgramDeploymentTransactionBody { fn from(value: ProgramDeploymentTransaction) -> Self { + let ProgramDeploymentTransaction { hash, message } = value; + Self { - hash: value.hash.into(), - message: value.message.bytecode.into(), + hash: hash.into(), + message: message.bytecode.into(), } } } @@ -378,7 +405,7 @@ impl From for FfiTransaction { private_body: Box::into_raw(Box::new(priv_tx.into())), program_deployment_body: std::ptr::null_mut(), }, - kind: FfiTransactionKind::Public, + kind: FfiTransactionKind::Private, }, Transaction::ProgramDeployment(pr_dep_tx) => Self { body: FfiTransactionBody { @@ -386,7 +413,7 @@ impl From for FfiTransaction { private_body: std::ptr::null_mut(), program_deployment_body: Box::into_raw(Box::new(pr_dep_tx.into())), }, - kind: FfiTransactionKind::Public, + kind: FfiTransactionKind::ProgramDeploy, }, } } diff --git a/integration_tests/tests/indexer_ffi.rs b/integration_tests/tests/indexer_ffi.rs index 96196fbd..bbc329e3 100644 --- a/integration_tests/tests/indexer_ffi.rs +++ b/integration_tests/tests/indexer_ffi.rs @@ -218,7 +218,7 @@ fn indexer_ffi_state_consistency() -> Result<()> { assert!(acc1_ind_state_ffi.error.is_ok()); - let acc1_ind_state_pre = unsafe { &*acc1_ind_state_ffi.value }.clone(); + let acc1_ind_state_pre = unsafe { &*acc1_ind_state_ffi.value }; let acc1_ind_state: indexer_service_protocol::Account = acc1_ind_state_pre.into(); let acc2_ind_state_ffi = @@ -226,7 +226,7 @@ fn indexer_ffi_state_consistency() -> Result<()> { assert!(acc2_ind_state_ffi.error.is_ok()); - let acc2_ind_state_pre = unsafe { &*acc2_ind_state_ffi.value }.clone(); + let acc2_ind_state_pre = unsafe { &*acc2_ind_state_ffi.value }; let acc2_ind_state: indexer_service_protocol::Account = acc2_ind_state_pre.into(); info!("Checking correct state transition"); @@ -320,7 +320,7 @@ fn indexer_ffi_state_consistency_with_labels() -> Result<()> { assert!(acc1_ind_state_ffi.error.is_ok()); - let acc1_ind_state_pre = unsafe { &*acc1_ind_state_ffi.value }.clone(); + let acc1_ind_state_pre = unsafe { &*acc1_ind_state_ffi.value }; let acc1_ind_state: indexer_service_protocol::Account = acc1_ind_state_pre.into(); let acc1_seq_state =