fix(indexer_ffi): suggestion fix

This commit is contained in:
Pravdyvy 2026-05-07 14:29:06 +03:00
parent 0e914adf0a
commit 6054ae113a
5 changed files with 138 additions and 77 deletions

View File

@ -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

View File

@ -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<nssa::Account> 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<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(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(),
}
}
}

View File

@ -17,38 +17,27 @@ pub struct FfiBlock {
impl From<Block> 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::<Vec<_>>()
.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<Box<FfiBlock>> for Block {
// fn from(value: Box<FfiBlock>) -> 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<FfiBlock>;
#[repr(C)]
@ -62,12 +51,20 @@ pub struct FfiBlockHeader {
impl From<BlockHeader> 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(),
}
}
}

View File

@ -24,11 +24,16 @@ pub struct FfiPublicTransactionBody {
impl From<PublicTransaction> 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<PublicMessage> 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::<Vec<_>>()
.into(),
nonces: value
.nonces
nonces: nonces
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
.into(),
instruction_data: value.instruction_data.into(),
instruction_data: instruction_data.into(),
}
}
}
@ -117,18 +127,22 @@ pub struct FfiPrivateTransactionBody {
impl From<PrivacyPreservingTransaction> 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::<Vec<_>>()
.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<PrivacyPreservingMessage> 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::<Vec<_>>()
.into(),
nonces: value
.nonces
nonces: nonces
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
.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<PrivacyPreservingMessage> for FfiPrivacyPreservingMessage {
.map(Into::into)
.collect::<Vec<_>>()
.into(),
encrypted_private_post_states: value
.encrypted_private_post_states
encrypted_private_post_states: encrypted_private_post_states
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
.into(),
new_commitments: value
.new_commitments
new_commitments: new_commitments
.into_iter()
.map(|comm| FfiBytes32 { data: comm.0 })
.collect::<Vec<_>>()
.into(),
new_nullifiers: value
.new_nullifiers
new_nullifiers: new_nullifiers
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
.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<EncryptedAccountData> 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<Box<FfiProgramDeploymentTransactionBody>> for ProgramDeploymentTransac
impl From<ProgramDeploymentTransaction> 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<Transaction> 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<Transaction> 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,
},
}
}

View File

@ -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 =