From 56ff66ccc194e9258bc4d9af195b059cdab07fae Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Tue, 10 Feb 2026 14:03:56 +0200 Subject: [PATCH] feat: first indexer integration test --- Cargo.lock | 1 + common/src/rpc_primitives/requests.rs | 9 - common/src/sequencer_client.rs | 28 +- indexer/core/src/config.rs | 7 +- indexer/core/src/lib.rs | 21 +- integration_tests/Cargo.toml | 1 + integration_tests/src/config.rs | 10 +- integration_tests/src/lib.rs | 38 +- integration_tests/tests/indexer.rs | 36 + integration_tests/tests/token.rs | 5 - integration_tests/tests/wallet_ffi.rs | 1237 +++++++++++++------------ sequencer_core/src/lib.rs | 10 +- sequencer_rpc/src/process.rs | 33 +- 13 files changed, 733 insertions(+), 703 deletions(-) create mode 100644 integration_tests/tests/indexer.rs diff --git a/Cargo.lock b/Cargo.lock index 32521a53..6c17002c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3449,6 +3449,7 @@ dependencies = [ "hex", "indexer_core", "indexer_service", + "indexer_service_rpc", "key_protocol", "log", "nssa", diff --git a/common/src/rpc_primitives/requests.rs b/common/src/rpc_primitives/requests.rs index bd2d307e..8c61ee32 100644 --- a/common/src/rpc_primitives/requests.rs +++ b/common/src/rpc_primitives/requests.rs @@ -40,9 +40,6 @@ pub struct GetBlockRangeDataRequest { #[derive(Serialize, Deserialize, Debug)] pub struct GetGenesisIdRequest {} -#[derive(Serialize, Deserialize, Debug)] -pub struct GetGenesisBlockRequest {} - #[derive(Serialize, Deserialize, Debug)] pub struct GetLastBlockRequest {} @@ -91,7 +88,6 @@ parse_request!(GetAccountsNoncesRequest); parse_request!(GetProofForCommitmentRequest); parse_request!(GetAccountRequest); parse_request!(GetProgramIdsRequest); -parse_request!(GetGenesisBlockRequest); #[derive(Serialize, Deserialize, Debug)] pub struct HelloResponse { @@ -180,11 +176,6 @@ pub struct GetGenesisIdResponse { pub genesis_id: u64, } -#[derive(Serialize, Deserialize, Debug)] -pub struct GetGenesisBlockResponse { - pub genesis_block_borsh_ser: Vec, -} - #[derive(Serialize, Deserialize, Debug)] pub struct GetLastBlockResponse { pub last_block: u64, diff --git a/common/src/sequencer_client.rs b/common/src/sequencer_client.rs index 5f682d66..7e5819ec 100644 --- a/common/src/sequencer_client.rs +++ b/common/src/sequencer_client.rs @@ -14,7 +14,6 @@ use super::rpc_primitives::requests::{ }; use crate::{ HashType, - block::Block, config::BasicAuth, error::{SequencerClientError, SequencerRpcError}, rpc_primitives::{ @@ -22,11 +21,10 @@ use crate::{ requests::{ GetAccountRequest, GetAccountResponse, GetAccountsNoncesRequest, GetAccountsNoncesResponse, GetBlockRangeDataRequest, GetBlockRangeDataResponse, - GetGenesisBlockRequest, GetGenesisBlockResponse, GetInitialTestnetAccountsResponse, - GetLastBlockRequest, GetLastBlockResponse, GetProgramIdsRequest, GetProgramIdsResponse, - GetProofForCommitmentRequest, GetProofForCommitmentResponse, - GetTransactionByHashRequest, GetTransactionByHashResponse, SendTxRequest, - SendTxResponse, + GetInitialTestnetAccountsResponse, GetLastBlockRequest, GetLastBlockResponse, + GetProgramIdsRequest, GetProgramIdsResponse, GetProofForCommitmentRequest, + GetProofForCommitmentResponse, GetTransactionByHashRequest, + GetTransactionByHashResponse, SendTxRequest, SendTxResponse, }, }, transaction::NSSATransaction, @@ -273,24 +271,6 @@ impl SequencerClient { Ok(resp_deser) } - /// Get genesis block from sequencer - /// - /// ToDo: Error handling - pub async fn get_genesis_block(&self) -> Result { - let genesis_req = GetGenesisBlockRequest {}; - - let req = serde_json::to_value(genesis_req).unwrap(); - - let resp = self - .call_method_with_payload("get_genesis_block", req) - .await - .unwrap(); - - let resp_deser = serde_json::from_value::(resp).unwrap(); - - Ok(borsh::from_slice(&resp_deser.genesis_block_borsh_ser).unwrap()) - } - /// Get initial testnet accounts from sequencer pub async fn get_initial_testnet_accounts( &self, diff --git a/indexer/core/src/config.rs b/indexer/core/src/config.rs index 35bc6794..968678e5 100644 --- a/indexer/core/src/config.rs +++ b/indexer/core/src/config.rs @@ -31,9 +31,14 @@ pub struct IndexerConfig { pub initial_accounts: Vec, /// List of initial commitments pub initial_commitments: Vec, + /// Sequencers signing key + /// + /// ToDo: Remove it after introducing bedrock block parsing. + /// Currently can not be removed, because indexer must start + /// chain BEFORE sequencer. + pub signing_key: [u8; 32], pub resubscribe_interval_millis: u64, pub bedrock_client_config: ClientConfig, - pub sequencer_client_config: ClientConfig, pub channel_id: ChannelId, } diff --git a/indexer/core/src/lib.rs b/indexer/core/src/lib.rs index 7869701d..90b61ec3 100644 --- a/indexer/core/src/lib.rs +++ b/indexer/core/src/lib.rs @@ -1,8 +1,8 @@ use anyhow::Result; use bedrock_client::BedrockClient; +use common::block::{Block, HashableBlockData}; // ToDo: Remove after testnet -use common::PINATA_BASE58; -use common::{block::Block, sequencer_client::SequencerClient}; +use common::{HashType, PINATA_BASE58}; use futures::StreamExt; use log::info; use logos_blockchain_core::mantle::{ @@ -19,19 +19,23 @@ pub mod state; #[derive(Clone)] pub struct IndexerCore { pub bedrock_client: BedrockClient, - pub sequencer_client: SequencerClient, pub config: IndexerConfig, pub store: IndexerStore, } impl IndexerCore { pub async fn new(config: IndexerConfig) -> Result { - let sequencer_client = SequencerClient::new_with_auth( - config.sequencer_client_config.addr.clone(), - config.sequencer_client_config.auth.clone(), - )?; + // ToDo: replace with correct startup + let hashable_data = HashableBlockData { + block_id: 1, + transactions: vec![], + prev_block_hash: HashType([0; 32]), + timestamp: 0, + }; - let start_block = sequencer_client.get_genesis_block().await?; + let signing_key = nssa::PrivateKey::try_new(config.signing_key).unwrap(); + let channel_genesis_msg_id = [0; 32]; + let start_block = hashable_data.into_pending_block(&signing_key, channel_genesis_msg_id); let initial_commitments: Vec = config .initial_commitments @@ -66,7 +70,6 @@ impl IndexerCore { config.bedrock_client_config.addr.clone(), config.bedrock_client_config.auth.clone().map(Into::into), )?, - sequencer_client, config, // ToDo: Implement restarts store: IndexerStore::open_db_with_genesis(&home, Some((start_block, state)))?, diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index 306d0ca3..d54b2eb0 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -17,6 +17,7 @@ indexer_core.workspace = true wallet-ffi.workspace = true serde_json.workspace = true token_core.workspace = true +indexer_service_rpc.workspace = true url.workspace = true anyhow.workspace = true diff --git a/integration_tests/src/config.rs b/integration_tests/src/config.rs index 6e9829a4..9cfe0767 100644 --- a/integration_tests/src/config.rs +++ b/integration_tests/src/config.rs @@ -12,8 +12,13 @@ use wallet::config::{ InitialAccountData, InitialAccountDataPrivate, InitialAccountDataPublic, WalletConfig, }; -pub fn indexer_config(bedrock_addr: SocketAddr) -> Result { +pub fn indexer_config( + bedrock_addr: SocketAddr, + home: PathBuf, + initial_data: &InitialData, +) -> Result { Ok(IndexerConfig { + home, resubscribe_interval_millis: 1000, bedrock_client_config: ClientConfig { addr: addr_to_url(UrlProtocol::Http, bedrock_addr) @@ -24,6 +29,9 @@ pub fn indexer_config(bedrock_addr: SocketAddr) -> Result { max_retries: 10, }, }, + initial_accounts: initial_data.sequencer_initial_accounts(), + initial_commitments: initial_data.sequencer_initial_commitments(), + signing_key: [37; 32], channel_id: bedrock_channel_id(), }) } diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index 895ee6d9..181b7e23 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -10,6 +10,7 @@ use indexer_service::IndexerHandle; use log::{debug, error, warn}; use nssa::{AccountId, PrivacyPreservingTransaction}; use nssa_core::Commitment; +use sequencer_core::indexer_client::{IndexerClient, IndexerClientTrait}; use sequencer_runner::SequencerHandle; use tempfile::TempDir; use testcontainers::compose::DockerCompose; @@ -33,10 +34,12 @@ static LOGGER: LazyLock<()> = LazyLock::new(env_logger::init); // NOTE: Order of fields is important for proper drop order. pub struct TestContext { sequencer_client: SequencerClient, + indexer_client: IndexerClient, wallet: WalletCore, sequencer_handle: SequencerHandle, indexer_handle: IndexerHandle, bedrock_compose: DockerCompose, + _temp_indexer_dir: TempDir, _temp_sequencer_dir: TempDir, _temp_wallet_dir: TempDir, } @@ -62,7 +65,7 @@ impl TestContext { let (bedrock_compose, bedrock_addr) = Self::setup_bedrock_node().await?; - let indexer_handle = Self::setup_indexer(bedrock_addr) + let (indexer_handle, temp_indexer_dir) = Self::setup_indexer(bedrock_addr, &initial_data) .await .context("Failed to setup Indexer")?; @@ -81,15 +84,22 @@ impl TestContext { let sequencer_url = config::addr_to_url(config::UrlProtocol::Http, sequencer_handle.addr()) .context("Failed to convert sequencer addr to URL")?; + let indexer_url = config::addr_to_url(config::UrlProtocol::Ws, indexer_handle.addr()) + .context("Failed to convert indexer addr to URL")?; let sequencer_client = SequencerClient::new(sequencer_url).context("Failed to create sequencer client")?; + let indexer_client = IndexerClient::new(&indexer_url) + .await + .context("Failed to create indexer client")?; Ok(Self { sequencer_client, + indexer_client, wallet, bedrock_compose, sequencer_handle, indexer_handle, + _temp_indexer_dir: temp_indexer_dir, _temp_sequencer_dir: temp_sequencer_dir, _temp_wallet_dir: temp_wallet_dir, }) @@ -158,13 +168,26 @@ impl TestContext { Ok((compose, addr)) } - async fn setup_indexer(bedrock_addr: SocketAddr) -> Result { - let indexer_config = - config::indexer_config(bedrock_addr).context("Failed to create Indexer config")?; + async fn setup_indexer( + bedrock_addr: SocketAddr, + initial_data: &config::InitialData, + ) -> Result<(IndexerHandle, TempDir)> { + let temp_indexer_dir = + tempfile::tempdir().context("Failed to create temp dir for indexer home")?; + + debug!("Using temp indexer home at {:?}", temp_indexer_dir.path()); + + let indexer_config = config::indexer_config( + bedrock_addr, + temp_indexer_dir.path().to_owned(), + initial_data, + ) + .context("Failed to create Indexer config")?; indexer_service::run_server(indexer_config, 0) .await .context("Failed to run Indexer Service") + .map(|handle| (handle, temp_indexer_dir)) } async fn setup_sequencer( @@ -245,6 +268,11 @@ impl TestContext { &self.sequencer_client } + /// Get reference to the indexer client. + pub fn indexer_client(&self) -> &IndexerClient { + &self.indexer_client + } + /// Get existing public account IDs in the wallet. pub fn existing_public_accounts(&self) -> Vec { self.wallet @@ -270,9 +298,11 @@ impl Drop for TestContext { sequencer_handle, indexer_handle, bedrock_compose, + _temp_indexer_dir: _, _temp_sequencer_dir: _, _temp_wallet_dir: _, sequencer_client: _, + indexer_client: _, wallet: _, } = self; diff --git a/integration_tests/tests/indexer.rs b/integration_tests/tests/indexer.rs new file mode 100644 index 00000000..87396460 --- /dev/null +++ b/integration_tests/tests/indexer.rs @@ -0,0 +1,36 @@ +use anyhow::Result; +use indexer_service_rpc::RpcClient; +use integration_tests::TestContext; +use log::info; +use tokio::test; +// use wallet::cli::{Command, config::ConfigSubcommand}; + +#[test] +async fn indexer_test_run() -> Result<()> { + let ctx = TestContext::new().await?; + + // RUN OBSERVATION + info!("LETS TAKE A LOOK"); + tokio::time::sleep(std::time::Duration::from_secs(100)).await; + + let last_block_seq = ctx + .sequencer_client() + .get_last_block() + .await + .unwrap() + .last_block; + + info!("Last block on seq now is {last_block_seq}"); + + let last_block_indexer = ctx + .indexer_client() + .get_last_finalized_block_id() + .await + .unwrap(); + + info!("Last block on ind now is {last_block_indexer}"); + + assert!(last_block_indexer > 1); + + Ok(()) +} diff --git a/integration_tests/tests/token.rs b/integration_tests/tests/token.rs index 32967a0d..b3056df9 100644 --- a/integration_tests/tests/token.rs +++ b/integration_tests/tests/token.rs @@ -639,7 +639,6 @@ async fn create_token_with_private_definition_and_supply() -> Result<()> { }; // Create token with both private definition and supply - let name = "A NAME".to_string(); let total_supply = 37; let subcommand = TokenProgramAgnosticSubcommand::New { definition_account_id: format_private_account_id(definition_account_id), @@ -799,7 +798,6 @@ async fn shielded_token_transfer() -> Result<()> { }; // Create token - let name = "A NAME".to_string(); let total_supply = 37; let subcommand = TokenProgramAgnosticSubcommand::New { definition_account_id: format_public_account_id(definition_account_id), @@ -913,7 +911,6 @@ async fn deshielded_token_transfer() -> Result<()> { }; // Create token with private supply - let name = "A NAME".to_string(); let total_supply = 37; let subcommand = TokenProgramAgnosticSubcommand::New { definition_account_id: format_public_account_id(definition_account_id), @@ -1014,8 +1011,6 @@ async fn token_claiming_path_with_private_accounts() -> Result<()> { }; // Create token - let name = "A NAME".to_string(); - let total_supply = 37; let subcommand = TokenProgramAgnosticSubcommand::New { definition_account_id: format_private_account_id(definition_account_id), supply_account_id: format_private_account_id(supply_account_id), diff --git a/integration_tests/tests/wallet_ffi.rs b/integration_tests/tests/wallet_ffi.rs index bb6b9805..eea0e5ae 100644 --- a/integration_tests/tests/wallet_ffi.rs +++ b/integration_tests/tests/wallet_ffi.rs @@ -1,618 +1,619 @@ -use std::{ - collections::HashSet, - ffi::{CStr, CString, c_char}, - io::Write, - time::Duration, -}; - -use anyhow::Result; -use integration_tests::{ - ACC_RECEIVER, ACC_SENDER, ACC_SENDER_PRIVATE, BlockingTestContext, - TIME_TO_WAIT_FOR_BLOCK_SECONDS, -}; -use log::info; -use nssa::{Account, AccountId, PublicKey, program::Program}; -use nssa_core::program::DEFAULT_PROGRAM_ID; -use tempfile::tempdir; -use wallet::WalletCore; -use wallet_ffi::{ - FfiAccount, FfiAccountList, FfiBytes32, FfiPrivateAccountKeys, FfiPublicAccountKey, - FfiTransferResult, WalletHandle, error, -}; - -unsafe extern "C" { - fn wallet_ffi_create_new( - config_path: *const c_char, - storage_path: *const c_char, - password: *const c_char, - ) -> *mut WalletHandle; - - fn wallet_ffi_destroy(handle: *mut WalletHandle); - - fn wallet_ffi_create_account_public( - handle: *mut WalletHandle, - out_account_id: *mut FfiBytes32, - ) -> error::WalletFfiError; - - fn wallet_ffi_create_account_private( - handle: *mut WalletHandle, - out_account_id: *mut FfiBytes32, - ) -> error::WalletFfiError; - - fn wallet_ffi_list_accounts( - handle: *mut WalletHandle, - out_list: *mut FfiAccountList, - ) -> error::WalletFfiError; - - fn wallet_ffi_free_account_list(list: *mut FfiAccountList); - - fn wallet_ffi_get_balance( - handle: *mut WalletHandle, - account_id: *const FfiBytes32, - is_public: bool, - out_balance: *mut [u8; 16], - ) -> error::WalletFfiError; - - fn wallet_ffi_get_account_public( - handle: *mut WalletHandle, - account_id: *const FfiBytes32, - out_account: *mut FfiAccount, - ) -> error::WalletFfiError; - - fn wallet_ffi_free_account_data(account: *mut FfiAccount); - - fn wallet_ffi_get_public_account_key( - handle: *mut WalletHandle, - account_id: *const FfiBytes32, - out_public_key: *mut FfiPublicAccountKey, - ) -> error::WalletFfiError; - - fn wallet_ffi_get_private_account_keys( - handle: *mut WalletHandle, - account_id: *const FfiBytes32, - out_keys: *mut FfiPrivateAccountKeys, - ) -> error::WalletFfiError; - - fn wallet_ffi_free_private_account_keys(keys: *mut FfiPrivateAccountKeys); - - fn wallet_ffi_account_id_to_base58(account_id: *const FfiBytes32) -> *mut std::ffi::c_char; - - fn wallet_ffi_free_string(ptr: *mut c_char); - - fn wallet_ffi_account_id_from_base58( - base58_str: *const std::ffi::c_char, - out_account_id: *mut FfiBytes32, - ) -> error::WalletFfiError; - - fn wallet_ffi_transfer_public( - handle: *mut WalletHandle, - from: *const FfiBytes32, - to: *const FfiBytes32, - amount: *const [u8; 16], - out_result: *mut FfiTransferResult, - ) -> error::WalletFfiError; - - fn wallet_ffi_free_transfer_result(result: *mut FfiTransferResult); - - fn wallet_ffi_register_public_account( - handle: *mut WalletHandle, - account_id: *const FfiBytes32, - out_result: *mut FfiTransferResult, - ) -> error::WalletFfiError; -} - -fn new_wallet_ffi_with_test_context_config(ctx: &BlockingTestContext) -> *mut WalletHandle { - let tempdir = tempfile::tempdir().unwrap(); - let config_path = tempdir.path().join("wallet_config.json"); - let storage_path = tempdir.path().join("storage.json"); - let mut config = ctx.ctx.wallet().config().to_owned(); - if let Some(config_overrides) = ctx.ctx.wallet().config_overrides().clone() { - config.apply_overrides(config_overrides); - } - let mut file = std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(&config_path) - .unwrap(); - - let config_with_overrides_serialized = serde_json::to_vec_pretty(&config).unwrap(); - - file.write_all(&config_with_overrides_serialized).unwrap(); - - let config_path = CString::new(config_path.to_str().unwrap()).unwrap(); - let storage_path = CString::new(storage_path.to_str().unwrap()).unwrap(); - let password = CString::new(ctx.ctx.wallet_password()).unwrap(); - - unsafe { - wallet_ffi_create_new( - config_path.as_ptr(), - storage_path.as_ptr(), - password.as_ptr(), - ) - } -} - -fn new_wallet_ffi_with_default_config(password: &str) -> *mut WalletHandle { - let tempdir = tempdir().unwrap(); - let config_path = tempdir.path().join("wallet_config.json"); - let storage_path = tempdir.path().join("storage.json"); - let config_path_c = CString::new(config_path.to_str().unwrap()).unwrap(); - let storage_path_c = CString::new(storage_path.to_str().unwrap()).unwrap(); - let password = CString::new(password).unwrap(); - - unsafe { - wallet_ffi_create_new( - config_path_c.as_ptr(), - storage_path_c.as_ptr(), - password.as_ptr(), - ) - } -} - -fn new_wallet_rust_with_default_config(password: &str) -> WalletCore { - let tempdir = tempdir().unwrap(); - let config_path = tempdir.path().join("wallet_config.json"); - let storage_path = tempdir.path().join("storage.json"); - - WalletCore::new_init_storage( - config_path.to_path_buf(), - storage_path.to_path_buf(), - None, - password.to_string(), - ) - .unwrap() -} - -#[test] -fn test_wallet_ffi_create_public_accounts() { - let password = "password_for_tests"; - let n_accounts = 10; - // First `n_accounts` public accounts created with Rust wallet - let new_public_account_ids_rust = { - let mut account_ids = Vec::new(); - - let mut wallet_rust = new_wallet_rust_with_default_config(password); - for _ in 0..n_accounts { - let account_id = wallet_rust.create_new_account_public(None).0; - account_ids.push(*account_id.value()); - } - account_ids - }; - - // First `n_accounts` public accounts created with wallet FFI - let new_public_account_ids_ffi = unsafe { - let mut account_ids = Vec::new(); - - let wallet_ffi_handle = new_wallet_ffi_with_default_config(password); - for _ in 0..n_accounts { - let mut out_account_id = FfiBytes32::from_bytes([0; 32]); - wallet_ffi_create_account_public( - wallet_ffi_handle, - (&mut out_account_id) as *mut FfiBytes32, - ); - account_ids.push(out_account_id.data); - } - wallet_ffi_destroy(wallet_ffi_handle); - account_ids - }; - - assert_eq!(new_public_account_ids_ffi, new_public_account_ids_rust); -} - -#[test] -fn test_wallet_ffi_create_private_accounts() { - let password = "password_for_tests"; - let n_accounts = 10; - // First `n_accounts` private accounts created with Rust wallet - let new_private_account_ids_rust = { - let mut account_ids = Vec::new(); - - let mut wallet_rust = new_wallet_rust_with_default_config(password); - for _ in 0..n_accounts { - let account_id = wallet_rust.create_new_account_private(None).0; - account_ids.push(*account_id.value()); - } - account_ids - }; - - // First `n_accounts` private accounts created with wallet FFI - let new_private_account_ids_ffi = unsafe { - let mut account_ids = Vec::new(); - - let wallet_ffi_handle = new_wallet_ffi_with_default_config(password); - for _ in 0..n_accounts { - let mut out_account_id = FfiBytes32::from_bytes([0; 32]); - wallet_ffi_create_account_private( - wallet_ffi_handle, - (&mut out_account_id) as *mut FfiBytes32, - ); - account_ids.push(out_account_id.data); - } - wallet_ffi_destroy(wallet_ffi_handle); - account_ids - }; - - assert_eq!(new_private_account_ids_ffi, new_private_account_ids_rust) -} - -#[test] -fn test_wallet_ffi_list_accounts() { - let password = "password_for_tests"; - - // Create the wallet FFI - let wallet_ffi_handle = unsafe { - let handle = new_wallet_ffi_with_default_config(password); - // Create 5 public accounts and 5 private accounts - for _ in 0..5 { - let mut out_account_id = FfiBytes32::from_bytes([0; 32]); - wallet_ffi_create_account_public(handle, (&mut out_account_id) as *mut FfiBytes32); - wallet_ffi_create_account_private(handle, (&mut out_account_id) as *mut FfiBytes32); - } - - handle - }; - - // Create the wallet Rust - let wallet_rust = { - let mut wallet = new_wallet_rust_with_default_config(password); - // Create 5 public accounts and 5 private accounts - for _ in 0..5 { - wallet.create_new_account_public(None); - wallet.create_new_account_private(None); - } - wallet - }; - - // Get the account list with FFI method - let mut wallet_ffi_account_list = unsafe { - let mut out_list = FfiAccountList::default(); - wallet_ffi_list_accounts(wallet_ffi_handle, (&mut out_list) as *mut FfiAccountList); - out_list - }; - - let wallet_rust_account_ids = wallet_rust - .storage() - .user_data - .account_ids() - .collect::>(); - - // Assert same number of elements between Rust and FFI result - assert_eq!(wallet_rust_account_ids.len(), wallet_ffi_account_list.count); - - let wallet_ffi_account_list_slice = unsafe { - core::slice::from_raw_parts( - wallet_ffi_account_list.entries, - wallet_ffi_account_list.count, - ) - }; - - // Assert same account ids between Rust and FFI result - assert_eq!( - wallet_rust_account_ids - .iter() - .map(|id| id.value()) - .collect::>(), - wallet_ffi_account_list_slice - .iter() - .map(|entry| &entry.account_id.data) - .collect::>() - ); - - // Assert `is_pub` flag is correct in the FFI result - for entry in wallet_ffi_account_list_slice.iter() { - let account_id = AccountId::new(entry.account_id.data); - let is_pub_default_in_rust_wallet = wallet_rust - .storage() - .user_data - .default_pub_account_signing_keys - .contains_key(&account_id); - let is_pub_key_tree_wallet_rust = wallet_rust - .storage() - .user_data - .public_key_tree - .account_id_map - .contains_key(&account_id); - - let is_public_in_rust_wallet = is_pub_default_in_rust_wallet || is_pub_key_tree_wallet_rust; - - assert_eq!(entry.is_public, is_public_in_rust_wallet); - } - - unsafe { - wallet_ffi_free_account_list((&mut wallet_ffi_account_list) as *mut FfiAccountList); - wallet_ffi_destroy(wallet_ffi_handle); - } -} - -#[test] -fn test_wallet_ffi_get_balance_public() -> Result<()> { - let ctx = BlockingTestContext::new()?; - let account_id: AccountId = ACC_SENDER.parse().unwrap(); - let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); - - let balance = unsafe { - let mut out_balance: [u8; 16] = [0; 16]; - let ffi_account_id = FfiBytes32::from(&account_id); - let _result = wallet_ffi_get_balance( - wallet_ffi_handle, - (&ffi_account_id) as *const FfiBytes32, - true, - (&mut out_balance) as *mut [u8; 16], - ); - u128::from_le_bytes(out_balance) - }; - assert_eq!(balance, 10000); - - info!("Successfully retrieved account balance"); - - unsafe { - wallet_ffi_destroy(wallet_ffi_handle); - } - - Ok(()) -} - -#[test] -fn test_wallet_ffi_get_account_public() -> Result<()> { - let ctx = BlockingTestContext::new()?; - let account_id: AccountId = ACC_SENDER.parse().unwrap(); - let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); - let mut out_account = FfiAccount::default(); - - let account: Account = unsafe { - let ffi_account_id = FfiBytes32::from(&account_id); - let _result = wallet_ffi_get_account_public( - wallet_ffi_handle, - (&ffi_account_id) as *const FfiBytes32, - (&mut out_account) as *mut FfiAccount, - ); - (&out_account).try_into().unwrap() - }; - - assert_eq!( - account.program_owner, - Program::authenticated_transfer_program().id() - ); - assert_eq!(account.balance, 10000); - assert!(account.data.is_empty()); - assert_eq!(account.nonce, 0); - - unsafe { - wallet_ffi_free_account_data((&mut out_account) as *mut FfiAccount); - wallet_ffi_destroy(wallet_ffi_handle); - } - - info!("Successfully retrieved account with correct details"); - - Ok(()) -} - -#[test] -fn test_wallet_ffi_get_public_account_keys() -> Result<()> { - let ctx = BlockingTestContext::new()?; - let account_id: AccountId = ACC_SENDER.parse().unwrap(); - let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); - let mut out_key = FfiPublicAccountKey::default(); - - let key: PublicKey = unsafe { - let ffi_account_id = FfiBytes32::from(&account_id); - let _result = wallet_ffi_get_public_account_key( - wallet_ffi_handle, - (&ffi_account_id) as *const FfiBytes32, - (&mut out_key) as *mut FfiPublicAccountKey, - ); - (&out_key).try_into().unwrap() - }; - - let expected_key = { - let private_key = ctx - .ctx - .wallet() - .get_account_public_signing_key(&account_id) - .unwrap(); - PublicKey::new_from_private_key(private_key) - }; - - assert_eq!(key, expected_key); - - info!("Successfully retrieved account key"); - - unsafe { - wallet_ffi_destroy(wallet_ffi_handle); - } - - Ok(()) -} - -#[test] -fn test_wallet_ffi_get_private_account_keys() -> Result<()> { - let ctx = BlockingTestContext::new()?; - let account_id: AccountId = ACC_SENDER_PRIVATE.parse().unwrap(); - let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); - let mut keys = FfiPrivateAccountKeys::default(); - - unsafe { - let ffi_account_id = FfiBytes32::from(&account_id); - let _result = wallet_ffi_get_private_account_keys( - wallet_ffi_handle, - (&ffi_account_id) as *const FfiBytes32, - (&mut keys) as *mut FfiPrivateAccountKeys, - ); - }; - - let key_chain = &ctx - .ctx - .wallet() - .storage() - .user_data - .get_private_account(&account_id) - .unwrap() - .0; - - let expected_npk = &key_chain.nullifer_public_key; - let expected_ivk = &key_chain.incoming_viewing_public_key; - - assert_eq!(&keys.npk(), expected_npk); - assert_eq!(&keys.ivk().unwrap(), expected_ivk); - - unsafe { - wallet_ffi_free_private_account_keys((&mut keys) as *mut FfiPrivateAccountKeys); - wallet_ffi_destroy(wallet_ffi_handle); - } - - info!("Successfully retrieved account keys"); - - Ok(()) -} - -#[test] -fn test_wallet_ffi_account_id_to_base58() { - let account_id_str = ACC_SENDER; - let account_id: AccountId = account_id_str.parse().unwrap(); - let ffi_bytes: FfiBytes32 = (&account_id).into(); - let ptr = unsafe { wallet_ffi_account_id_to_base58((&ffi_bytes) as *const FfiBytes32) }; - - let ffi_result = unsafe { CStr::from_ptr(ptr).to_str().unwrap() }; - - assert_eq!(account_id_str, ffi_result); - - unsafe { - wallet_ffi_free_string(ptr); - } -} - -#[test] -fn test_wallet_ffi_base58_to_account_id() { - let account_id_str = ACC_SENDER; - let account_id_c_str = CString::new(account_id_str).unwrap(); - let account_id: AccountId = unsafe { - let mut out_account_id_bytes = FfiBytes32::default(); - wallet_ffi_account_id_from_base58( - account_id_c_str.as_ptr(), - (&mut out_account_id_bytes) as *mut FfiBytes32, - ); - out_account_id_bytes.into() - }; - - let expected_account_id = account_id_str.parse().unwrap(); - - assert_eq!(account_id, expected_account_id); -} - -#[test] -fn test_wallet_ffi_init_public_account_auth_transfer() -> Result<()> { - let ctx = BlockingTestContext::new().unwrap(); - let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); - - // Create a new uninitialized public account - let mut out_account_id = FfiBytes32::from_bytes([0; 32]); - unsafe { - wallet_ffi_create_account_public( - wallet_ffi_handle, - (&mut out_account_id) as *mut FfiBytes32, - ); - } - - // Check its program owner is the default program id - let account: Account = unsafe { - let mut out_account = FfiAccount::default(); - let _result = wallet_ffi_get_account_public( - wallet_ffi_handle, - (&out_account_id) as *const FfiBytes32, - (&mut out_account) as *mut FfiAccount, - ); - (&out_account).try_into().unwrap() - }; - assert_eq!(account.program_owner, DEFAULT_PROGRAM_ID); - - // Call the init funciton - let mut transfer_result = FfiTransferResult::default(); - unsafe { - wallet_ffi_register_public_account( - wallet_ffi_handle, - (&out_account_id) as *const FfiBytes32, - (&mut transfer_result) as *mut FfiTransferResult, - ); - } - - info!("Waiting for next block creation"); - std::thread::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)); - - // Check that the program owner is now the authenticated transfer program - let account: Account = unsafe { - let mut out_account = FfiAccount::default(); - let _result = wallet_ffi_get_account_public( - wallet_ffi_handle, - (&out_account_id) as *const FfiBytes32, - (&mut out_account) as *mut FfiAccount, - ); - (&out_account).try_into().unwrap() - }; - assert_eq!( - account.program_owner, - Program::authenticated_transfer_program().id() - ); - - unsafe { - wallet_ffi_free_transfer_result((&mut transfer_result) as *mut FfiTransferResult); - wallet_ffi_destroy(wallet_ffi_handle); - } - - Ok(()) -} - -#[test] -fn test_wallet_ffi_transfer_public() -> Result<()> { - let ctx = BlockingTestContext::new().unwrap(); - let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); - let from: FfiBytes32 = (&ACC_SENDER.parse::().unwrap()).into(); - let to: FfiBytes32 = (&ACC_RECEIVER.parse::().unwrap()).into(); - let amount: [u8; 16] = 100u128.to_le_bytes(); - - let mut transfer_result = FfiTransferResult::default(); - unsafe { - wallet_ffi_transfer_public( - wallet_ffi_handle, - (&from) as *const FfiBytes32, - (&to) as *const FfiBytes32, - (&amount) as *const [u8; 16], - (&mut transfer_result) as *mut FfiTransferResult, - ); - } - - info!("Waiting for next block creation"); - std::thread::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)); - - let from_balance = unsafe { - let mut out_balance: [u8; 16] = [0; 16]; - let _result = wallet_ffi_get_balance( - wallet_ffi_handle, - (&from) as *const FfiBytes32, - true, - (&mut out_balance) as *mut [u8; 16], - ); - u128::from_le_bytes(out_balance) - }; - - let to_balance = unsafe { - let mut out_balance: [u8; 16] = [0; 16]; - let _result = wallet_ffi_get_balance( - wallet_ffi_handle, - (&to) as *const FfiBytes32, - true, - (&mut out_balance) as *mut [u8; 16], - ); - u128::from_le_bytes(out_balance) - }; - - assert_eq!(from_balance, 9900); - assert_eq!(to_balance, 20100); - - unsafe { - wallet_ffi_free_transfer_result((&mut transfer_result) as *mut FfiTransferResult); - wallet_ffi_destroy(wallet_ffi_handle); - } - - Ok(()) -} +// use std::{ +// collections::HashSet, +// ffi::{CStr, CString, c_char}, +// io::Write, +// time::Duration, +// }; + +// use anyhow::Result; +// use integration_tests::{ +// ACC_RECEIVER, ACC_SENDER, ACC_SENDER_PRIVATE, BlockingTestContext, +// TIME_TO_WAIT_FOR_BLOCK_SECONDS, +// }; +// use log::info; +// use nssa::{Account, AccountId, PublicKey, program::Program}; +// use nssa_core::program::DEFAULT_PROGRAM_ID; +// use tempfile::tempdir; +// use wallet::WalletCore; +// use wallet_ffi::{ +// FfiAccount, FfiAccountList, FfiBytes32, FfiPrivateAccountKeys, FfiPublicAccountKey, +// FfiTransferResult, WalletHandle, error, +// }; + +// unsafe extern "C" { +// fn wallet_ffi_create_new( +// config_path: *const c_char, +// storage_path: *const c_char, +// password: *const c_char, +// ) -> *mut WalletHandle; + +// fn wallet_ffi_destroy(handle: *mut WalletHandle); + +// fn wallet_ffi_create_account_public( +// handle: *mut WalletHandle, +// out_account_id: *mut FfiBytes32, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_create_account_private( +// handle: *mut WalletHandle, +// out_account_id: *mut FfiBytes32, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_list_accounts( +// handle: *mut WalletHandle, +// out_list: *mut FfiAccountList, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_free_account_list(list: *mut FfiAccountList); + +// fn wallet_ffi_get_balance( +// handle: *mut WalletHandle, +// account_id: *const FfiBytes32, +// is_public: bool, +// out_balance: *mut [u8; 16], +// ) -> error::WalletFfiError; + +// fn wallet_ffi_get_account_public( +// handle: *mut WalletHandle, +// account_id: *const FfiBytes32, +// out_account: *mut FfiAccount, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_free_account_data(account: *mut FfiAccount); + +// fn wallet_ffi_get_public_account_key( +// handle: *mut WalletHandle, +// account_id: *const FfiBytes32, +// out_public_key: *mut FfiPublicAccountKey, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_get_private_account_keys( +// handle: *mut WalletHandle, +// account_id: *const FfiBytes32, +// out_keys: *mut FfiPrivateAccountKeys, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_free_private_account_keys(keys: *mut FfiPrivateAccountKeys); + +// fn wallet_ffi_account_id_to_base58(account_id: *const FfiBytes32) -> *mut std::ffi::c_char; + +// fn wallet_ffi_free_string(ptr: *mut c_char); + +// fn wallet_ffi_account_id_from_base58( +// base58_str: *const std::ffi::c_char, +// out_account_id: *mut FfiBytes32, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_transfer_public( +// handle: *mut WalletHandle, +// from: *const FfiBytes32, +// to: *const FfiBytes32, +// amount: *const [u8; 16], +// out_result: *mut FfiTransferResult, +// ) -> error::WalletFfiError; + +// fn wallet_ffi_free_transfer_result(result: *mut FfiTransferResult); + +// fn wallet_ffi_register_public_account( +// handle: *mut WalletHandle, +// account_id: *const FfiBytes32, +// out_result: *mut FfiTransferResult, +// ) -> error::WalletFfiError; +// } + +// fn new_wallet_ffi_with_test_context_config(ctx: &BlockingTestContext) -> *mut WalletHandle { +// let tempdir = tempfile::tempdir().unwrap(); +// let config_path = tempdir.path().join("wallet_config.json"); +// let storage_path = tempdir.path().join("storage.json"); +// let mut config = ctx.ctx.wallet().config().to_owned(); +// if let Some(config_overrides) = ctx.ctx.wallet().config_overrides().clone() { +// config.apply_overrides(config_overrides); +// } +// let mut file = std::fs::OpenOptions::new() +// .write(true) +// .create(true) +// .truncate(true) +// .open(&config_path) +// .unwrap(); + +// let config_with_overrides_serialized = serde_json::to_vec_pretty(&config).unwrap(); + +// file.write_all(&config_with_overrides_serialized).unwrap(); + +// let config_path = CString::new(config_path.to_str().unwrap()).unwrap(); +// let storage_path = CString::new(storage_path.to_str().unwrap()).unwrap(); +// let password = CString::new(ctx.ctx.wallet_password()).unwrap(); + +// unsafe { +// wallet_ffi_create_new( +// config_path.as_ptr(), +// storage_path.as_ptr(), +// password.as_ptr(), +// ) +// } +// } + +// fn new_wallet_ffi_with_default_config(password: &str) -> *mut WalletHandle { +// let tempdir = tempdir().unwrap(); +// let config_path = tempdir.path().join("wallet_config.json"); +// let storage_path = tempdir.path().join("storage.json"); +// let config_path_c = CString::new(config_path.to_str().unwrap()).unwrap(); +// let storage_path_c = CString::new(storage_path.to_str().unwrap()).unwrap(); +// let password = CString::new(password).unwrap(); + +// unsafe { +// wallet_ffi_create_new( +// config_path_c.as_ptr(), +// storage_path_c.as_ptr(), +// password.as_ptr(), +// ) +// } +// } + +// fn new_wallet_rust_with_default_config(password: &str) -> WalletCore { +// let tempdir = tempdir().unwrap(); +// let config_path = tempdir.path().join("wallet_config.json"); +// let storage_path = tempdir.path().join("storage.json"); + +// WalletCore::new_init_storage( +// config_path.to_path_buf(), +// storage_path.to_path_buf(), +// None, +// password.to_string(), +// ) +// .unwrap() +// } + +// #[test] +// fn test_wallet_ffi_create_public_accounts() { +// let password = "password_for_tests"; +// let n_accounts = 10; +// // First `n_accounts` public accounts created with Rust wallet +// let new_public_account_ids_rust = { +// let mut account_ids = Vec::new(); + +// let mut wallet_rust = new_wallet_rust_with_default_config(password); +// for _ in 0..n_accounts { +// let account_id = wallet_rust.create_new_account_public(None).0; +// account_ids.push(*account_id.value()); +// } +// account_ids +// }; + +// // First `n_accounts` public accounts created with wallet FFI +// let new_public_account_ids_ffi = unsafe { +// let mut account_ids = Vec::new(); + +// let wallet_ffi_handle = new_wallet_ffi_with_default_config(password); +// for _ in 0..n_accounts { +// let mut out_account_id = FfiBytes32::from_bytes([0; 32]); +// wallet_ffi_create_account_public( +// wallet_ffi_handle, +// (&mut out_account_id) as *mut FfiBytes32, +// ); +// account_ids.push(out_account_id.data); +// } +// wallet_ffi_destroy(wallet_ffi_handle); +// account_ids +// }; + +// assert_eq!(new_public_account_ids_ffi, new_public_account_ids_rust); +// } + +// #[test] +// fn test_wallet_ffi_create_private_accounts() { +// let password = "password_for_tests"; +// let n_accounts = 10; +// // First `n_accounts` private accounts created with Rust wallet +// let new_private_account_ids_rust = { +// let mut account_ids = Vec::new(); + +// let mut wallet_rust = new_wallet_rust_with_default_config(password); +// for _ in 0..n_accounts { +// let account_id = wallet_rust.create_new_account_private(None).0; +// account_ids.push(*account_id.value()); +// } +// account_ids +// }; + +// // First `n_accounts` private accounts created with wallet FFI +// let new_private_account_ids_ffi = unsafe { +// let mut account_ids = Vec::new(); + +// let wallet_ffi_handle = new_wallet_ffi_with_default_config(password); +// for _ in 0..n_accounts { +// let mut out_account_id = FfiBytes32::from_bytes([0; 32]); +// wallet_ffi_create_account_private( +// wallet_ffi_handle, +// (&mut out_account_id) as *mut FfiBytes32, +// ); +// account_ids.push(out_account_id.data); +// } +// wallet_ffi_destroy(wallet_ffi_handle); +// account_ids +// }; + +// assert_eq!(new_private_account_ids_ffi, new_private_account_ids_rust) +// } + +// #[test] +// fn test_wallet_ffi_list_accounts() { +// let password = "password_for_tests"; + +// // Create the wallet FFI +// let wallet_ffi_handle = unsafe { +// let handle = new_wallet_ffi_with_default_config(password); +// // Create 5 public accounts and 5 private accounts +// for _ in 0..5 { +// let mut out_account_id = FfiBytes32::from_bytes([0; 32]); +// wallet_ffi_create_account_public(handle, (&mut out_account_id) as *mut FfiBytes32); +// wallet_ffi_create_account_private(handle, (&mut out_account_id) as *mut FfiBytes32); +// } + +// handle +// }; + +// // Create the wallet Rust +// let wallet_rust = { +// let mut wallet = new_wallet_rust_with_default_config(password); +// // Create 5 public accounts and 5 private accounts +// for _ in 0..5 { +// wallet.create_new_account_public(None); +// wallet.create_new_account_private(None); +// } +// wallet +// }; + +// // Get the account list with FFI method +// let mut wallet_ffi_account_list = unsafe { +// let mut out_list = FfiAccountList::default(); +// wallet_ffi_list_accounts(wallet_ffi_handle, (&mut out_list) as *mut FfiAccountList); +// out_list +// }; + +// let wallet_rust_account_ids = wallet_rust +// .storage() +// .user_data +// .account_ids() +// .collect::>(); + +// // Assert same number of elements between Rust and FFI result +// assert_eq!(wallet_rust_account_ids.len(), wallet_ffi_account_list.count); + +// let wallet_ffi_account_list_slice = unsafe { +// core::slice::from_raw_parts( +// wallet_ffi_account_list.entries, +// wallet_ffi_account_list.count, +// ) +// }; + +// // Assert same account ids between Rust and FFI result +// assert_eq!( +// wallet_rust_account_ids +// .iter() +// .map(|id| id.value()) +// .collect::>(), +// wallet_ffi_account_list_slice +// .iter() +// .map(|entry| &entry.account_id.data) +// .collect::>() +// ); + +// // Assert `is_pub` flag is correct in the FFI result +// for entry in wallet_ffi_account_list_slice.iter() { +// let account_id = AccountId::new(entry.account_id.data); +// let is_pub_default_in_rust_wallet = wallet_rust +// .storage() +// .user_data +// .default_pub_account_signing_keys +// .contains_key(&account_id); +// let is_pub_key_tree_wallet_rust = wallet_rust +// .storage() +// .user_data +// .public_key_tree +// .account_id_map +// .contains_key(&account_id); + +// let is_public_in_rust_wallet = is_pub_default_in_rust_wallet || +// is_pub_key_tree_wallet_rust; + +// assert_eq!(entry.is_public, is_public_in_rust_wallet); +// } + +// unsafe { +// wallet_ffi_free_account_list((&mut wallet_ffi_account_list) as *mut FfiAccountList); +// wallet_ffi_destroy(wallet_ffi_handle); +// } +// } + +// #[test] +// fn test_wallet_ffi_get_balance_public() -> Result<()> { +// let ctx = BlockingTestContext::new()?; +// let account_id: AccountId = ACC_SENDER.parse().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); + +// let balance = unsafe { +// let mut out_balance: [u8; 16] = [0; 16]; +// let ffi_account_id = FfiBytes32::from(&account_id); +// let _result = wallet_ffi_get_balance( +// wallet_ffi_handle, +// (&ffi_account_id) as *const FfiBytes32, +// true, +// (&mut out_balance) as *mut [u8; 16], +// ); +// u128::from_le_bytes(out_balance) +// }; +// assert_eq!(balance, 10000); + +// info!("Successfully retrieved account balance"); + +// unsafe { +// wallet_ffi_destroy(wallet_ffi_handle); +// } + +// Ok(()) +// } + +// #[test] +// fn test_wallet_ffi_get_account_public() -> Result<()> { +// let ctx = BlockingTestContext::new()?; +// let account_id: AccountId = ACC_SENDER.parse().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); +// let mut out_account = FfiAccount::default(); + +// let account: Account = unsafe { +// let ffi_account_id = FfiBytes32::from(&account_id); +// let _result = wallet_ffi_get_account_public( +// wallet_ffi_handle, +// (&ffi_account_id) as *const FfiBytes32, +// (&mut out_account) as *mut FfiAccount, +// ); +// (&out_account).try_into().unwrap() +// }; + +// assert_eq!( +// account.program_owner, +// Program::authenticated_transfer_program().id() +// ); +// assert_eq!(account.balance, 10000); +// assert!(account.data.is_empty()); +// assert_eq!(account.nonce, 0); + +// unsafe { +// wallet_ffi_free_account_data((&mut out_account) as *mut FfiAccount); +// wallet_ffi_destroy(wallet_ffi_handle); +// } + +// info!("Successfully retrieved account with correct details"); + +// Ok(()) +// } + +// #[test] +// fn test_wallet_ffi_get_public_account_keys() -> Result<()> { +// let ctx = BlockingTestContext::new()?; +// let account_id: AccountId = ACC_SENDER.parse().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); +// let mut out_key = FfiPublicAccountKey::default(); + +// let key: PublicKey = unsafe { +// let ffi_account_id = FfiBytes32::from(&account_id); +// let _result = wallet_ffi_get_public_account_key( +// wallet_ffi_handle, +// (&ffi_account_id) as *const FfiBytes32, +// (&mut out_key) as *mut FfiPublicAccountKey, +// ); +// (&out_key).try_into().unwrap() +// }; + +// let expected_key = { +// let private_key = ctx +// .ctx +// .wallet() +// .get_account_public_signing_key(&account_id) +// .unwrap(); +// PublicKey::new_from_private_key(private_key) +// }; + +// assert_eq!(key, expected_key); + +// info!("Successfully retrieved account key"); + +// unsafe { +// wallet_ffi_destroy(wallet_ffi_handle); +// } + +// Ok(()) +// } + +// #[test] +// fn test_wallet_ffi_get_private_account_keys() -> Result<()> { +// let ctx = BlockingTestContext::new()?; +// let account_id: AccountId = ACC_SENDER_PRIVATE.parse().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); +// let mut keys = FfiPrivateAccountKeys::default(); + +// unsafe { +// let ffi_account_id = FfiBytes32::from(&account_id); +// let _result = wallet_ffi_get_private_account_keys( +// wallet_ffi_handle, +// (&ffi_account_id) as *const FfiBytes32, +// (&mut keys) as *mut FfiPrivateAccountKeys, +// ); +// }; + +// let key_chain = &ctx +// .ctx +// .wallet() +// .storage() +// .user_data +// .get_private_account(&account_id) +// .unwrap() +// .0; + +// let expected_npk = &key_chain.nullifer_public_key; +// let expected_ivk = &key_chain.incoming_viewing_public_key; + +// assert_eq!(&keys.npk(), expected_npk); +// assert_eq!(&keys.ivk().unwrap(), expected_ivk); + +// unsafe { +// wallet_ffi_free_private_account_keys((&mut keys) as *mut FfiPrivateAccountKeys); +// wallet_ffi_destroy(wallet_ffi_handle); +// } + +// info!("Successfully retrieved account keys"); + +// Ok(()) +// } + +// #[test] +// fn test_wallet_ffi_account_id_to_base58() { +// let account_id_str = ACC_SENDER; +// let account_id: AccountId = account_id_str.parse().unwrap(); +// let ffi_bytes: FfiBytes32 = (&account_id).into(); +// let ptr = unsafe { wallet_ffi_account_id_to_base58((&ffi_bytes) as *const FfiBytes32) }; + +// let ffi_result = unsafe { CStr::from_ptr(ptr).to_str().unwrap() }; + +// assert_eq!(account_id_str, ffi_result); + +// unsafe { +// wallet_ffi_free_string(ptr); +// } +// } + +// #[test] +// fn test_wallet_ffi_base58_to_account_id() { +// let account_id_str = ACC_SENDER; +// let account_id_c_str = CString::new(account_id_str).unwrap(); +// let account_id: AccountId = unsafe { +// let mut out_account_id_bytes = FfiBytes32::default(); +// wallet_ffi_account_id_from_base58( +// account_id_c_str.as_ptr(), +// (&mut out_account_id_bytes) as *mut FfiBytes32, +// ); +// out_account_id_bytes.into() +// }; + +// let expected_account_id = account_id_str.parse().unwrap(); + +// assert_eq!(account_id, expected_account_id); +// } + +// #[test] +// fn test_wallet_ffi_init_public_account_auth_transfer() -> Result<()> { +// let ctx = BlockingTestContext::new().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); + +// // Create a new uninitialized public account +// let mut out_account_id = FfiBytes32::from_bytes([0; 32]); +// unsafe { +// wallet_ffi_create_account_public( +// wallet_ffi_handle, +// (&mut out_account_id) as *mut FfiBytes32, +// ); +// } + +// // Check its program owner is the default program id +// let account: Account = unsafe { +// let mut out_account = FfiAccount::default(); +// let _result = wallet_ffi_get_account_public( +// wallet_ffi_handle, +// (&out_account_id) as *const FfiBytes32, +// (&mut out_account) as *mut FfiAccount, +// ); +// (&out_account).try_into().unwrap() +// }; +// assert_eq!(account.program_owner, DEFAULT_PROGRAM_ID); + +// // Call the init funciton +// let mut transfer_result = FfiTransferResult::default(); +// unsafe { +// wallet_ffi_register_public_account( +// wallet_ffi_handle, +// (&out_account_id) as *const FfiBytes32, +// (&mut transfer_result) as *mut FfiTransferResult, +// ); +// } + +// info!("Waiting for next block creation"); +// std::thread::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)); + +// // Check that the program owner is now the authenticated transfer program +// let account: Account = unsafe { +// let mut out_account = FfiAccount::default(); +// let _result = wallet_ffi_get_account_public( +// wallet_ffi_handle, +// (&out_account_id) as *const FfiBytes32, +// (&mut out_account) as *mut FfiAccount, +// ); +// (&out_account).try_into().unwrap() +// }; +// assert_eq!( +// account.program_owner, +// Program::authenticated_transfer_program().id() +// ); + +// unsafe { +// wallet_ffi_free_transfer_result((&mut transfer_result) as *mut FfiTransferResult); +// wallet_ffi_destroy(wallet_ffi_handle); +// } + +// Ok(()) +// } + +// #[test] +// fn test_wallet_ffi_transfer_public() -> Result<()> { +// let ctx = BlockingTestContext::new().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); +// let from: FfiBytes32 = (&ACC_SENDER.parse::().unwrap()).into(); +// let to: FfiBytes32 = (&ACC_RECEIVER.parse::().unwrap()).into(); +// let amount: [u8; 16] = 100u128.to_le_bytes(); + +// let mut transfer_result = FfiTransferResult::default(); +// unsafe { +// wallet_ffi_transfer_public( +// wallet_ffi_handle, +// (&from) as *const FfiBytes32, +// (&to) as *const FfiBytes32, +// (&amount) as *const [u8; 16], +// (&mut transfer_result) as *mut FfiTransferResult, +// ); +// } + +// info!("Waiting for next block creation"); +// std::thread::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)); + +// let from_balance = unsafe { +// let mut out_balance: [u8; 16] = [0; 16]; +// let _result = wallet_ffi_get_balance( +// wallet_ffi_handle, +// (&from) as *const FfiBytes32, +// true, +// (&mut out_balance) as *mut [u8; 16], +// ); +// u128::from_le_bytes(out_balance) +// }; + +// let to_balance = unsafe { +// let mut out_balance: [u8; 16] = [0; 16]; +// let _result = wallet_ffi_get_balance( +// wallet_ffi_handle, +// (&to) as *const FfiBytes32, +// true, +// (&mut out_balance) as *mut [u8; 16], +// ); +// u128::from_le_bytes(out_balance) +// }; + +// assert_eq!(from_balance, 9900); +// assert_eq!(to_balance, 20100); + +// unsafe { +// wallet_ffi_free_transfer_result((&mut transfer_result) as *mut FfiTransferResult); +// wallet_ffi_destroy(wallet_ffi_handle); +// } + +// Ok(()) +// } diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 0ed2da92..4612f99c 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -129,10 +129,10 @@ impl SequencerCore SequencerCore JsonHandler respond(response) } - async fn process_get_genesis_block(&self, request: Request) -> Result { - let _get_genesis_req = GetGenesisBlockRequest::parse(Some(request.params))?; - - let genesis_block = { - let state = self.sequencer_state.lock().await; - - let gen_id = state.block_store().genesis_id(); - - state.block_store().get_block_at_id(gen_id)? - }; - - let response = GetGenesisBlockResponse { - genesis_block_borsh_ser: borsh::to_vec(&genesis_block) - .expect("Block must serialize correctly"), - }; - - respond(response) - } - async fn process_get_last_block(&self, request: Request) -> Result { let _get_last_block_req = GetLastBlockRequest::parse(Some(request.params))?; @@ -326,7 +306,6 @@ impl JsonHandler GET_BLOCK => self.process_get_block_data(request).await, GET_BLOCK_RANGE => self.process_get_block_range_data(request).await, GET_GENESIS => self.process_get_genesis(request).await, - GET_GENESIS_BLOCK => self.process_get_genesis_block(request).await, GET_LAST_BLOCK => self.process_get_last_block(request).await, GET_INITIAL_TESTNET_ACCOUNTS => self.get_initial_testnet_accounts(request).await, GET_ACCOUNT_BALANCE => self.process_get_account_balance(request).await,