From 9614c3143bd7c305546b4c203a4501f54f4e1c69 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 16 Oct 2025 15:58:35 +0300 Subject: [PATCH] feat: integration test and async fs --- integration_tests/src/lib.rs | 196 ++++++++++++++---- wallet/src/cli/chain.rs | 6 +- .../src/cli/native_token_transfer_program.rs | 12 +- wallet/src/cli/pinata_program.rs | 2 +- wallet/src/cli/token_program.rs | 6 +- wallet/src/helperfunctions.rs | 21 +- wallet/src/lib.rs | 23 +- 7 files changed, 191 insertions(+), 75 deletions(-) diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index a560e9f..a1aa3e0 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -118,7 +118,7 @@ pub async fn test_success() { }, ); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -152,13 +152,13 @@ pub async fn test_success_move_to_another_account() { RegisterSubcommand::RegisterAccountPublic {}, )); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); wallet::execute_subcommand(command).await.unwrap(); - let persistent_accounts = fetch_persistent_accounts().unwrap(); + let persistent_accounts = fetch_persistent_accounts().await.unwrap(); let mut new_persistent_account_addr = String::new(); @@ -216,7 +216,7 @@ pub async fn test_failure() { }, ); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -256,7 +256,7 @@ pub async fn test_success_two_transactions() { }, ); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -317,7 +317,7 @@ pub async fn test_success_two_transactions() { pub async fn test_get_account() { info!("test_get_account"); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); let account = seq_client @@ -338,7 +338,7 @@ pub async fn test_get_account() { /// This test creates a new token using the token program. After creating the token, the test executes a /// token transfer to a new account. pub async fn test_success_token_program() { - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); // Create new account for the token definition wallet::execute_subcommand(Command::Chain(ChainSubcommand::Register( @@ -359,7 +359,7 @@ pub async fn test_success_token_program() { .await .unwrap(); - let persistent_accounts = fetch_persistent_accounts().unwrap(); + let persistent_accounts = fetch_persistent_accounts().await.unwrap(); let mut new_persistent_accounts_addr = Vec::new(); @@ -485,7 +485,7 @@ pub async fn test_success_token_program() { /// This test creates a new private token using the token program. After creating the token, the test executes a /// private token transfer to a new account. All accounts are owned except definition. pub async fn test_success_token_program_private_owned() { - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); // Create new account for the token definition (public) let SubcommandReturnValue::RegisterAccount { @@ -556,8 +556,10 @@ pub async fn test_success_token_program_private_owned() { ] ); - let wallet_config = fetch_config().unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_config = fetch_config().await.unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&supply_addr) @@ -579,8 +581,10 @@ pub async fn test_success_token_program_private_owned() { info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let wallet_config = fetch_config().unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_config = fetch_config().await.unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&supply_addr) @@ -607,8 +611,10 @@ pub async fn test_success_token_program_private_owned() { info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let wallet_config = fetch_config().unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_config = fetch_config().await.unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&supply_addr) @@ -624,7 +630,7 @@ pub async fn test_success_token_program_private_owned() { /// This test creates a new private token using the token program. After creating the token, the test executes a /// private token transfer to a new account. pub async fn test_success_token_program_private_claiming_path() { - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); // Create new account for the token definition (public) let SubcommandReturnValue::RegisterAccount { @@ -695,8 +701,10 @@ pub async fn test_success_token_program_private_claiming_path() { ] ); - let wallet_config = fetch_config().unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_config = fetch_config().await.unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&supply_addr) @@ -740,8 +748,10 @@ pub async fn test_success_token_program_private_claiming_path() { wallet::execute_subcommand(command).await.unwrap(); - let wallet_config = fetch_config().unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_config = fetch_config().await.unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&supply_addr) @@ -772,9 +782,11 @@ pub async fn test_success_private_transfer_to_another_owned_account() { info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&from) @@ -812,9 +824,11 @@ pub async fn test_success_private_transfer_to_another_foreign_account() { info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&from) @@ -844,9 +858,11 @@ pub async fn test_success_private_transfer_to_another_owned_account_claiming_pat panic!("FAILED TO REGISTER ACCOUNT"); }; - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config.clone()).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config.clone()) + .await + .unwrap(); let (to_keys, _) = wallet_storage .storage @@ -880,7 +896,9 @@ pub async fn test_success_private_transfer_to_another_owned_account_claiming_pat }, )); wallet::execute_subcommand(command).await.unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&from) @@ -899,6 +917,78 @@ pub async fn test_success_private_transfer_to_another_owned_account_claiming_pat info!("Success!"); } +pub async fn test_success_private_transfer_to_another_owned_account_cont_run_path() { + info!("test_success_private_transfer_to_another_owned_account_cont_run_path"); + let continious_run_handle = tokio::spawn(wallet::execute_continious_run()); + + let from: Address = ACC_SENDER_PRIVATE.parse().unwrap(); + + let command = Command::Chain(ChainSubcommand::Register( + RegisterSubcommand::RegisterAccountPrivate {}, + )); + + let sub_ret = wallet::execute_subcommand(command).await.unwrap(); + let SubcommandReturnValue::RegisterAccount { addr: to_addr } = sub_ret else { + panic!("FAILED TO REGISTER ACCOUNT"); + }; + + let wallet_config = fetch_config().await.unwrap(); + let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config.clone()) + .await + .unwrap(); + + let (to_keys, _) = wallet_storage + .storage + .user_data + .user_private_accounts + .get(&to_addr) + .cloned() + .unwrap(); + + let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Private( + NativeTokenTransferProgramSubcommandPrivate::SendNativeTokenTransferPrivateForeignAccount { + from: from.to_string(), + to_npk: hex::encode(to_keys.nullifer_public_key.0), + to_ipk: hex::encode(to_keys.incoming_viewing_public_key.0), + amount: 100, + }, + )); + + let sub_ret = wallet::execute_subcommand(command).await.unwrap(); + let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = sub_ret else { + panic!("FAILED TO SEND TX"); + }; + + let tx = fetch_privacy_preserving_tx(&seq_client, tx_hash.clone()).await; + + println!("Waiting for next blocks to check if continoius run fetch account"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); + + let new_commitment1 = wallet_storage + .get_private_account_commitment(&from) + .unwrap(); + assert_eq!(tx.message.new_commitments[0], new_commitment1); + + assert_eq!(tx.message.new_commitments.len(), 2); + for commitment in tx.message.new_commitments.into_iter() { + assert!(verify_commitment_is_in_state(commitment, &seq_client).await); + } + + let to_res_acc = wallet_storage.get_account_private(&to_addr).unwrap(); + + assert_eq!(to_res_acc.balance, 100); + + continious_run_handle.abort(); + + info!("Success!"); +} + pub async fn test_success_deshielded_transfer_to_another_account() { info!("test_success_deshielded_transfer_to_another_account"); let from: Address = ACC_SENDER_PRIVATE.parse().unwrap(); @@ -911,9 +1001,11 @@ pub async fn test_success_deshielded_transfer_to_another_account() { }, ); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config.clone()).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config.clone()) + .await + .unwrap(); let from_acc = wallet_storage.get_account_private(&from).unwrap(); assert_eq!(from_acc.balance, 10000); @@ -923,7 +1015,9 @@ pub async fn test_success_deshielded_transfer_to_another_account() { info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let from_acc = wallet_storage.get_account_private(&from).unwrap(); let new_commitment = wallet_storage @@ -954,7 +1048,7 @@ pub async fn test_success_shielded_transfer_to_another_owned_account() { }, )); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); wallet::execute_subcommand(command).await.unwrap(); @@ -962,8 +1056,10 @@ pub async fn test_success_shielded_transfer_to_another_owned_account() { info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let wallet_config = fetch_config().unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_config = fetch_config().await.unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let acc_to = wallet_storage.get_account_private(&to).unwrap(); let new_commitment = wallet_storage.get_private_account_commitment(&to).unwrap(); @@ -995,7 +1091,7 @@ pub async fn test_success_shielded_transfer_to_another_foreign_account() { amount: 100, })); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -1037,7 +1133,7 @@ pub async fn test_pinata() { }, )); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -1085,7 +1181,7 @@ pub async fn test_pinata_private_receiver() { }, )); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -1120,9 +1216,11 @@ pub async fn test_pinata_private_receiver() { )); wallet::execute_subcommand(command).await.unwrap(); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&ACC_SENDER_PRIVATE.parse().unwrap()) @@ -1159,7 +1257,7 @@ pub async fn test_pinata_private_receiver_new_account() { }, )); - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); @@ -1181,9 +1279,11 @@ pub async fn test_pinata_private_receiver_new_account() { .unwrap() .balance; - let wallet_config = fetch_config().unwrap(); + let wallet_config = fetch_config().await.unwrap(); let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + let wallet_storage = WalletCore::start_from_config_update_chain(wallet_config) + .await + .unwrap(); let new_commitment1 = wallet_storage .get_private_account_commitment(&winner_addr) @@ -1287,6 +1387,12 @@ pub async fn main_tests_runner() -> Result<()> { "test_pinata_private_receiver_new_account" => { test_cleanup_wrap!(home_dir, test_pinata_private_receiver_new_account); } + "test_success_private_transfer_to_another_owned_account_cont_run_path" => { + test_cleanup_wrap!( + home_dir, + test_success_private_transfer_to_another_owned_account_cont_run_path + ); + } "all" => { test_cleanup_wrap!(home_dir, test_success_move_to_another_account); test_cleanup_wrap!(home_dir, test_success); @@ -1322,6 +1428,10 @@ pub async fn main_tests_runner() -> Result<()> { test_cleanup_wrap!(home_dir, test_success_token_program_private_owned); test_cleanup_wrap!(home_dir, test_success_token_program_private_claiming_path); test_cleanup_wrap!(home_dir, test_pinata_private_receiver_new_account); + test_cleanup_wrap!( + home_dir, + test_success_private_transfer_to_another_owned_account_cont_run_path + ); } "all_private" => { test_cleanup_wrap!( @@ -1352,6 +1462,10 @@ pub async fn main_tests_runner() -> Result<()> { test_cleanup_wrap!(home_dir, test_success_token_program_private_owned); test_cleanup_wrap!(home_dir, test_success_token_program_private_claiming_path); test_cleanup_wrap!(home_dir, test_pinata_private_receiver_new_account); + test_cleanup_wrap!( + home_dir, + test_success_private_transfer_to_another_owned_account_cont_run_path + ); } _ => { anyhow::bail!("Unknown test name"); diff --git a/wallet/src/cli/chain.rs b/wallet/src/cli/chain.rs index ad58301..258c545 100644 --- a/wallet/src/cli/chain.rs +++ b/wallet/src/cli/chain.rs @@ -180,7 +180,7 @@ impl WalletSubcommand for FetchSubcommand { .insert_private_account_data(acc_addr, res_acc_to); } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -201,7 +201,7 @@ impl WalletSubcommand for RegisterSubcommand { println!("Generated new account with addr {addr}"); - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -223,7 +223,7 @@ impl WalletSubcommand for RegisterSubcommand { hex::encode(key.incoming_viewing_public_key.to_bytes()) ); - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); diff --git a/wallet/src/cli/native_token_transfer_program.rs b/wallet/src/cli/native_token_transfer_program.rs index 6d43d64..eb6310c 100644 --- a/wallet/src/cli/native_token_transfer_program.rs +++ b/wallet/src/cli/native_token_transfer_program.rs @@ -152,7 +152,7 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandPrivate { .decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -194,7 +194,7 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandPrivate { .decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -240,7 +240,7 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandShielded { .decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -273,7 +273,7 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandShielded { let tx_hash = res.tx_hash; - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -323,7 +323,7 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommand { )?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -347,7 +347,7 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommand { println!("Transaction data is {transfer_tx:?}"); - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); diff --git a/wallet/src/cli/pinata_program.rs b/wallet/src/cli/pinata_program.rs index b197ae0..6ccfb97 100644 --- a/wallet/src/cli/pinata_program.rs +++ b/wallet/src/cli/pinata_program.rs @@ -131,7 +131,7 @@ impl WalletSubcommand for PinataProgramSubcommandPrivate { )?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); diff --git a/wallet/src/cli/token_program.rs b/wallet/src/cli/token_program.rs index 7a17730..7bd3e88 100644 --- a/wallet/src/cli/token_program.rs +++ b/wallet/src/cli/token_program.rs @@ -174,7 +174,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { )?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -231,7 +231,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { )?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); @@ -281,7 +281,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { )?; } - let path = wallet_core.store_persistent_accounts()?; + let path = wallet_core.store_persistent_accounts().await?; println!("Stored persistent accounts at {path:#?}"); diff --git a/wallet/src/helperfunctions.rs b/wallet/src/helperfunctions.rs index 20f4eec..a67b8ec 100644 --- a/wallet/src/helperfunctions.rs +++ b/wallet/src/helperfunctions.rs @@ -1,7 +1,8 @@ use base64::{Engine, engine::general_purpose::STANDARD as BASE64}; use nssa_core::account::Nonce; use rand::{RngCore, rngs::OsRng}; -use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr}; +use std::{path::PathBuf, str::FromStr}; +use tokio::io::AsyncReadExt; use anyhow::Result; use key_protocol::key_protocol_core::NSSAUserData; @@ -22,25 +23,25 @@ pub fn get_home() -> Result { } /// Fetch config from `NSSA_WALLET_HOME_DIR` -pub fn fetch_config() -> Result { +pub async fn fetch_config() -> Result { let config_home = get_home()?; - let file = File::open(config_home.join("wallet_config.json"))?; - let reader = BufReader::new(file); + let config_contents = tokio::fs::read(config_home.join("wallet_config.json")).await?; - Ok(serde_json::from_reader(reader)?) + Ok(serde_json::from_slice(&config_contents)?) } /// Fetch list of accounts stored at `NSSA_WALLET_HOME_DIR/curr_accounts.json` /// /// If file not present, it is considered as empty list of persistent accounts -pub fn fetch_persistent_accounts() -> Result> { +pub async fn fetch_persistent_accounts() -> Result> { let home = get_home()?; let accs_path = home.join("curr_accounts.json"); + let mut persistent_accounts_content = vec![]; - match File::open(accs_path) { - Ok(file) => { - let reader = BufReader::new(file); - Ok(serde_json::from_reader(reader)?) + match tokio::fs::File::open(accs_path).await { + Ok(mut file) => { + file.read_to_end(&mut persistent_accounts_content).await?; + Ok(serde_json::from_slice(&persistent_accounts_content)?) } Err(err) => match err.kind() { std::io::ErrorKind::NotFound => Ok(vec![]), diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index ff17a10..ae96064 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::Write, path::PathBuf, sync::Arc}; +use std::{path::PathBuf, sync::Arc}; use base64::{Engine, engine::general_purpose::STANDARD as BASE64}; use common::{ @@ -15,6 +15,7 @@ use nssa::{Account, Address, privacy_preserving_transaction::message::EncryptedA use clap::{Parser, Subcommand}; use nssa_core::{Commitment, MembershipProof}; +use tokio::io::AsyncWriteExt; use crate::cli::{ WalletSubcommand, chain::ChainSubcommand, @@ -47,13 +48,13 @@ pub struct WalletCore { } impl WalletCore { - pub fn start_from_config_update_chain(config: WalletConfig) -> Result { + pub async fn start_from_config_update_chain(config: WalletConfig) -> Result { let client = Arc::new(SequencerClient::new(config.sequencer_addr.clone())?); let tx_poller = TxPoller::new(config.clone(), client.clone()); let mut storage = WalletChainStore::new(config)?; - let persistent_accounts = fetch_persistent_accounts()?; + let persistent_accounts = fetch_persistent_accounts().await?; for pers_acc_data in persistent_accounts { storage.insert_account_data(pers_acc_data); } @@ -66,15 +67,15 @@ impl WalletCore { } ///Store persistent accounts at home - pub fn store_persistent_accounts(&self) -> Result { + pub async fn store_persistent_accounts(&self) -> Result { let home = get_home()?; let accs_path = home.join("curr_accounts.json"); let data = produce_data_for_storage(&self.storage.user_data); let accs = serde_json::to_vec_pretty(&data)?; - let mut accs_file = File::create(accs_path.as_path())?; - accs_file.write_all(&accs)?; + let mut accs_file = tokio::fs::File::create(accs_path.as_path()).await?; + accs_file.write_all(&accs).await?; info!("Stored accounts data at {accs_path:#?}"); @@ -221,8 +222,8 @@ pub enum SubcommandReturnValue { } pub async fn execute_subcommand(command: Command) -> Result { - let wallet_config = fetch_config()?; - let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config)?; + let wallet_config = fetch_config().await?; + let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config).await?; let subcommand_ret = match command { Command::Transfer(transfer_subcommand) => { @@ -247,9 +248,9 @@ pub async fn execute_subcommand(command: Command) -> Result Result<()> { - let config = fetch_config()?; + let config = fetch_config().await?; let seq_client = Arc::new(SequencerClient::new(config.sequencer_addr.clone())?); - let mut wallet_core = WalletCore::start_from_config_update_chain(config.clone())?; + let mut wallet_core = WalletCore::start_from_config_update_chain(config.clone()).await?; let mut latest_block_num = seq_client.get_last_block().await?.last_block; let mut curr_last_block = latest_block_num; @@ -312,7 +313,7 @@ pub async fn execute_continious_run() -> Result<()> { } } - wallet_core.store_persistent_accounts()?; + wallet_core.store_persistent_accounts().await?; println!( "Block at id {block_id} with timestamp {} parsed",