diff --git a/examples/program_deployment/src/bin/run_hello_world_private.rs b/examples/program_deployment/src/bin/run_hello_world_private.rs index 3b7ca404..64fc39c2 100644 --- a/examples/program_deployment/src/bin/run_hello_world_private.rs +++ b/examples/program_deployment/src/bin/run_hello_world_private.rs @@ -23,7 +23,7 @@ use wallet::{AccountIdentity, WalletCore}; #[tokio::main] async fn main() { // Initialize wallet - let mut wallet_core = WalletCore::from_env().await.unwrap(); + let wallet_core = WalletCore::from_env().await.unwrap(); // Parse arguments // First argument is the path to the program binary diff --git a/examples/program_deployment/src/bin/run_hello_world_through_tail_call_private.rs b/examples/program_deployment/src/bin/run_hello_world_through_tail_call_private.rs index 5429d6f3..c1282dae 100644 --- a/examples/program_deployment/src/bin/run_hello_world_through_tail_call_private.rs +++ b/examples/program_deployment/src/bin/run_hello_world_through_tail_call_private.rs @@ -26,7 +26,7 @@ use wallet::{AccountIdentity, WalletCore}; #[tokio::main] async fn main() { // Initialize wallet - let mut wallet_core = WalletCore::from_env().await.unwrap(); + let wallet_core = WalletCore::from_env().await.unwrap(); // Parse arguments // First argument is the path to the simple_tail_call program binary diff --git a/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs b/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs index e1f0d7d0..32aa0d4c 100644 --- a/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs +++ b/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs @@ -29,7 +29,7 @@ use wallet::WalletCore; #[tokio::main] async fn main() { // Initialize wallet - let mut wallet_core = WalletCore::from_env().await.unwrap(); + let wallet_core = WalletCore::from_env().await.unwrap(); // Parse arguments // First argument is the path to the program binary diff --git a/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs b/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs index 99961b30..b99f41f4 100644 --- a/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs +++ b/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs @@ -66,7 +66,7 @@ async fn main() { let program = Program::new(bytecode.into()).unwrap(); // Initialize wallet - let mut wallet_core = WalletCore::from_env().await.unwrap(); + let wallet_core = WalletCore::from_env().await.unwrap(); match cli.command { Command::WritePublic { diff --git a/integration_tests/tests/wallet_ffi.rs b/integration_tests/tests/wallet_ffi.rs index 16172e96..321c874d 100644 --- a/integration_tests/tests/wallet_ffi.rs +++ b/integration_tests/tests/wallet_ffi.rs @@ -1794,7 +1794,7 @@ fn test_wallet_ffi_transfer_generic_private() -> Result<()> { #[test] fn test_wallet_ffi_vault_balance_and_claim_public() -> Result<()> { - let mut ctx = BlockingTestContext::new()?; + let ctx = BlockingTestContext::new()?; let home = tempfile::tempdir()?; let FfiCreateWalletOutput { wallet: wallet_ffi_handle, @@ -1808,7 +1808,7 @@ fn test_wallet_ffi_vault_balance_and_claim_public() -> Result<()> { // Fund the owner's vault, simulating an L1 bridge deposit. ctx.block_on(|ctx| async move { - Vault(ctx.wallet_mut()) + Vault(ctx.wallet()) .send_transfer(sender, owner, amount) .await }) @@ -1894,7 +1894,7 @@ fn test_wallet_ffi_vault_balance_and_claim_private() -> Result<()> { // Fund the owner's vault. Real deposits always land via a public transfer (the bridge // program crediting the vault PDA), regardless of whether the owner is private. ctx.block_on(|ctx| async move { - Vault(ctx.wallet_mut()) + Vault(ctx.wallet()) .send_transfer(sender, owner, amount) .await }) diff --git a/lez/wallet-ffi/src/account.rs b/lez/wallet-ffi/src/account.rs index 61047a66..fdbfabc1 100644 --- a/lez/wallet-ffi/src/account.rs +++ b/lez/wallet-ffi/src/account.rs @@ -323,7 +323,7 @@ pub unsafe extern "C" fn wallet_ffi_get_balance( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -389,7 +389,7 @@ pub unsafe extern "C" fn wallet_ffi_get_account_public( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); diff --git a/lez/wallet-ffi/src/bridge.rs b/lez/wallet-ffi/src/bridge.rs index 70cc9383..fca71b1c 100644 --- a/lez/wallet-ffi/src/bridge.rs +++ b/lez/wallet-ffi/src/bridge.rs @@ -55,7 +55,7 @@ pub unsafe extern "C" fn wallet_ffi_bridge_withdraw( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -66,7 +66,7 @@ pub unsafe extern "C" fn wallet_ffi_bridge_withdraw( let from_id = AccountId::new(unsafe { (*from).data }); let bedrock_account_pk = unsafe { (*bedrock_account_pk).data }; - let mut bridge = Bridge(&mut wallet); + let bridge = Bridge(&wallet); match block_on(bridge.send_withdraw(from_id, amount, bedrock_account_pk)) { Ok(tx_hash) => { diff --git a/lez/wallet-ffi/src/generic_transaction.rs b/lez/wallet-ffi/src/generic_transaction.rs index 3e1873c2..7be6ddaf 100644 --- a/lez/wallet-ffi/src/generic_transaction.rs +++ b/lez/wallet-ffi/src/generic_transaction.rs @@ -237,7 +237,7 @@ pub unsafe extern "C" fn wallet_ffi_send_generic_public_transaction( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -329,7 +329,7 @@ pub unsafe extern "C" fn wallet_ffi_send_generic_private_transaction( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); diff --git a/lez/wallet-ffi/src/pinata.rs b/lez/wallet-ffi/src/pinata.rs index 37861657..b1fdb27a 100644 --- a/lez/wallet-ffi/src/pinata.rs +++ b/lez/wallet-ffi/src/pinata.rs @@ -60,7 +60,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -72,7 +72,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata( let winner_id = AccountId::new(unsafe { (*winner_account_id).data }); let solution = u128::from_le_bytes(unsafe { *solution }); - let mut pinata = Pinata(&mut wallet); + let pinata = Pinata(&wallet); match block_on(pinata.claim(pinata_id, winner_id, solution)) { Ok(tx_hash) => { @@ -155,7 +155,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_already_initializ return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -174,7 +174,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_already_initializ }; let proof: MembershipProof = (winner_proof_index, siblings); - let mut pinata = Pinata(&mut wallet); + let pinata = Pinata(&wallet); match block_on( pinata @@ -250,7 +250,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_not_initialized( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -262,7 +262,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_not_initialized( let winner_id = AccountId::new(unsafe { (*winner_account_id).data }); let solution = u128::from_le_bytes(unsafe { *solution }); - let mut pinata = Pinata(&mut wallet); + let pinata = Pinata(&wallet); match block_on(pinata.claim_private_owned_account(pinata_id, winner_id, solution)) { Ok((tx_hash, _shared_key)) => { diff --git a/lez/wallet-ffi/src/program_deployment.rs b/lez/wallet-ffi/src/program_deployment.rs index 8d924682..2e5550ae 100644 --- a/lez/wallet-ffi/src/program_deployment.rs +++ b/lez/wallet-ffi/src/program_deployment.rs @@ -46,7 +46,7 @@ pub unsafe extern "C" fn wallet_ffi_program_deployment( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); diff --git a/lez/wallet-ffi/src/sync.rs b/lez/wallet-ffi/src/sync.rs index 7682d1be..b65a0944 100644 --- a/lez/wallet-ffi/src/sync.rs +++ b/lez/wallet-ffi/src/sync.rs @@ -126,7 +126,7 @@ pub unsafe extern "C" fn wallet_ffi_get_current_block_height( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); diff --git a/lez/wallet-ffi/src/transfer.rs b/lez/wallet-ffi/src/transfer.rs index 33386752..1fcd3133 100644 --- a/lez/wallet-ffi/src/transfer.rs +++ b/lez/wallet-ffi/src/transfer.rs @@ -72,7 +72,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_public( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -84,7 +84,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_public( let to_id = AccountId::new(unsafe { (*to).data }); let amount = u128::from_le_bytes(unsafe { *amount }); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_public_transfer( AccountIdentity::Public(from_id), @@ -164,7 +164,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -188,7 +188,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded( CliAccountMention::KeyPath, ); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_shielded_transfer_to_outer_account( from_mention.into_public_identity(from_id), @@ -262,7 +262,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_deshielded( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -273,7 +273,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_deshielded( let from_id = AccountId::new(unsafe { (*from).data }); let to_id = AccountId::new(unsafe { (*to).data }); let amount = u128::from_le_bytes(unsafe { *amount }); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_deshielded_transfer(from_id, to_id, amount)) { Ok((tx_hash, _shared_key)) => { @@ -348,7 +348,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -367,7 +367,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private( }; let to_identifier = u128::from_le_bytes(unsafe { (*to_identifier).data }); let amount = u128::from_le_bytes(unsafe { *amount }); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_private_transfer_to_outer_account( from_id, @@ -445,7 +445,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded_owned( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -461,7 +461,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded_owned( CliAccountMention::KeyPath, ); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_shielded_transfer( from_mention.into_public_identity(from_id), @@ -536,7 +536,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private_owned( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -547,7 +547,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private_owned( let from_id = AccountId::new(unsafe { (*from).data }); let to_id = AccountId::new(unsafe { (*to).data }); let amount = u128::from_le_bytes(unsafe { *amount }); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_private_transfer_to_owned_account(from_id, to_id, amount)) { Ok((tx_hash, _shared_keys)) => { @@ -608,7 +608,7 @@ pub unsafe extern "C" fn wallet_ffi_register_public_account( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -618,7 +618,7 @@ pub unsafe extern "C" fn wallet_ffi_register_public_account( let account_id = AccountId::new(unsafe { (*account_id).data }); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.register_account(AccountIdentity::Public(account_id))) { Ok(tx_hash) => { @@ -679,7 +679,7 @@ pub unsafe extern "C" fn wallet_ffi_register_private_account( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); @@ -688,7 +688,7 @@ pub unsafe extern "C" fn wallet_ffi_register_private_account( }; let account_id = AccountId::new(unsafe { (*account_id).data }); - let mut transfer = NativeTokenTransfer(&mut wallet); + let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.register_account_private(account_id)) { Ok((tx_hash, _secret)) => { diff --git a/lez/wallet-ffi/src/vault.rs b/lez/wallet-ffi/src/vault.rs index 73a24599..e4d9dd6a 100644 --- a/lez/wallet-ffi/src/vault.rs +++ b/lez/wallet-ffi/src/vault.rs @@ -48,7 +48,7 @@ pub unsafe extern "C" fn wallet_ffi_get_vault_balance( return WalletFfiError::NullPointer; } - let mut wallet = match wrapper.core.lock() { + let wallet = match wrapper.core.lock() { Ok(w) => w, Err(e) => { print_error(format!("Failed to lock wallet: {e}")); diff --git a/lez/wallet/src/account_manager.rs b/lez/wallet/src/account_manager.rs index fc820d40..20d5586d 100644 --- a/lez/wallet/src/account_manager.rs +++ b/lez/wallet/src/account_manager.rs @@ -192,7 +192,7 @@ pub struct AccountManager { impl AccountManager { pub async fn new( - wallet: &mut WalletCore, + wallet: &WalletCore, accounts: Vec, ) -> Result { let mut states = Vec::with_capacity(accounts.len()); @@ -541,7 +541,7 @@ struct AccountPreparedData { } async fn private_key_tree_acc_preparation( - wallet: &mut WalletCore, + wallet: &WalletCore, account_id: AccountId, is_pda: bool, ) -> Result { @@ -581,7 +581,7 @@ async fn private_key_tree_acc_preparation( } async fn private_shared_acc_preparation( - wallet: &mut WalletCore, + wallet: &WalletCore, account_id: AccountId, nsk: NullifierSecretKey, npk: NullifierPublicKey, diff --git a/lez/wallet/src/lib.rs b/lez/wallet/src/lib.rs index d673af06..77b015d1 100644 --- a/lez/wallet/src/lib.rs +++ b/lez/wallet/src/lib.rs @@ -10,6 +10,7 @@ use std::{ collections::{BTreeMap, HashMap}, path::PathBuf, + sync::Arc, }; pub use account_manager::AccountIdentity; @@ -31,7 +32,7 @@ use lee_core::{ use log::info; use sequencer_service_rpc::{RpcClient as _, SequencerClient}; use storage::Storage; -use tokio::io::AsyncWriteExt as _; +use tokio::{io::AsyncWriteExt as _, sync::RwLock}; use url::Url; use crate::{ @@ -103,7 +104,10 @@ pub struct WalletCore { metrics_path: PathBuf, metrics: HashMap, - metric_updates: Vec, + /// Wrapping metric updates in Arc to not break intefaces too much. + /// + /// It is assumed, that wallet methods can be accesed via immutable reference + metric_updates: Arc>>, multi_sequencer_client: MultiSequencerClient, } @@ -188,7 +192,7 @@ impl WalletCore { storage, metrics_path, metrics, - metric_updates: vec![], + metric_updates: Arc::new(RwLock::new(vec![])), multi_sequencer_client, }) } @@ -251,14 +255,20 @@ impl WalletCore { Ok(()) } - pub fn store_metrics(&self) -> Result<()> { - save_metrics_at_path_with_updates( - self.metrics.clone(), - &self.multi_sequencer_client.leader_url, - &self.metric_updates, - &self.storage_path, - ) - .with_context(|| format!("Failed to store metrics at {}", self.storage_path.display()))?; + pub async fn store_metrics(&self) -> Result<()> { + { + let metric_updates = self.metric_updates.read().await; + save_metrics_at_path_with_updates( + self.metrics.clone(), + &self.multi_sequencer_client.leader_url, + &metric_updates, + &self.storage_path, + ) + .await + .with_context(|| { + format!("Failed to store metrics at {}", self.storage_path.display()) + })?; + } println!("Stored metrics at {}", self.metrics_path.display()); @@ -478,28 +488,34 @@ impl WalletCore { } /// Get account balance. - pub async fn get_account_balance(&mut self, acc: AccountId) -> Result { + pub async fn get_account_balance(&self, acc: AccountId) -> Result { let call_f = async |client: &SequencerClient| client.get_account_balance(acc).await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } /// Get accounts nonces. - pub async fn get_accounts_nonces(&mut self, accs: Vec) -> Result> { + pub async fn get_accounts_nonces(&self, accs: Vec) -> Result> { let call_f = async |client: &SequencerClient| client.get_accounts_nonces(accs).await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } - pub async fn get_account(&mut self, account_id: AccountIdWithPrivacy) -> Result { + pub async fn get_account(&self, account_id: AccountIdWithPrivacy) -> Result { match account_id { AccountIdWithPrivacy::Public(acc_id) => self.get_account_public(acc_id).await, AccountIdWithPrivacy::Private(acc_id) => { @@ -512,46 +528,58 @@ impl WalletCore { } } - pub async fn get_last_block_id(&mut self) -> Result { + pub async fn get_last_block_id(&self) -> Result { let call_f = async |client: &SequencerClient| client.get_last_block_id().await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } - pub async fn get_block(&mut self, block_id: u64) -> Result> { + pub async fn get_block(&self, block_id: u64) -> Result> { let call_f = async |client: &SequencerClient| client.get_block(block_id).await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } pub async fn get_transaction( - &mut self, + &self, hash: HashType, ) -> Result> { let call_f = async |client: &SequencerClient| client.get_transaction(hash).await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } /// Get public account. - pub async fn get_account_public(&mut self, account_id: AccountId) -> Result { + pub async fn get_account_public(&self, account_id: AccountId) -> Result { let call_f = async |client: &SequencerClient| client.get_account(account_id).await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } @@ -588,12 +616,15 @@ impl WalletCore { Some(Commitment::new(&account_id, account)) } - pub async fn get_program_ids(&mut self) -> Result> { + pub async fn get_program_ids(&self) -> Result> { let call_f = async |client: &SequencerClient| client.get_program_ids().await; let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } @@ -607,7 +638,7 @@ impl WalletCore { } pub async fn check_private_account_initialized( - &mut self, + &self, account_id: AccountId, ) -> Result> { if let Some(acc_comm) = self.get_private_account_commitment(account_id) { @@ -616,7 +647,10 @@ impl WalletCore { let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } call_res.map_err(Into::into) } else { @@ -624,14 +658,17 @@ impl WalletCore { } } - pub async fn get_commitment_root(&mut self) -> Result> { + pub async fn get_commitment_root(&self) -> Result> { let call_f = async |client: &SequencerClient| { client.get_proof_for_commitment(DUMMY_COMMITMENT).await }; let (proof, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(proof?.map(|p| compute_digest_for_path(&DUMMY_COMMITMENT, &p))) } @@ -703,7 +740,7 @@ impl WalletCore { } pub async fn send_privacy_preserving_tx( - &mut self, + &self, accounts: Vec, instruction_data: InstructionData, program: &ProgramWithDependencies, @@ -715,7 +752,7 @@ impl WalletCore { } pub async fn send_privacy_preserving_tx_with_pre_check( - &mut self, + &self, accounts: Vec, instruction_data: InstructionData, program: &ProgramWithDependencies, @@ -773,13 +810,16 @@ impl WalletCore { let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok((call_res?, shared_secrets)) } pub async fn send_pub_tx( - &mut self, + &self, accounts: Vec, instruction_data: InstructionData, program_id: ProgramId, @@ -789,7 +829,7 @@ impl WalletCore { } pub async fn send_pub_tx_with_pre_check( - &mut self, + &self, accounts: Vec, instruction_data: InstructionData, program_id: ProgramId, @@ -840,15 +880,15 @@ impl WalletCore { let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } - pub async fn send_program_deployment_transaction( - &mut self, - bytecode: Vec, - ) -> Result { + pub async fn send_program_deployment_transaction(&self, bytecode: Vec) -> Result { let message = lee::program_deployment_transaction::Message::new(bytecode); let transaction = ProgramDeploymentTransaction::new(message); @@ -860,7 +900,10 @@ impl WalletCore { let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await; - self.metric_updates.push(metrics_update); + { + let mut metric_updates_guard = self.metric_updates.write().await; + metric_updates_guard.push(metrics_update); + } Ok(call_res?) } diff --git a/lez/wallet/src/multi_client.rs b/lez/wallet/src/multi_client.rs index aa42c6e8..3eb53e06 100644 --- a/lez/wallet/src/multi_client.rs +++ b/lez/wallet/src/multi_client.rs @@ -1,8 +1,9 @@ -use std::{collections::HashMap, io::Write, path::Path}; +use std::{collections::HashMap, path::Path}; use anyhow::{Context, Result}; use sequencer_service_rpc::{RpcClient, SequencerClient, SequencerClientBuilder}; use serde::{Deserialize, Serialize}; +use tokio::io::AsyncWriteExt; use url::Url; use crate::config::SequencerConnectionData; @@ -250,15 +251,18 @@ pub fn choose_leader( )) } -pub fn save_metrics_at_path( +pub async fn save_metrics_at_path( metrics: &HashMap, path: &Path, ) -> Result<(), anyhow::Error> { let metrics_serialized = serde_json::to_vec_pretty(metrics)?; - let mut file = std::fs::File::create(path).context("Failed to create file")?; + let mut file = tokio::fs::File::create(path) + .await + .context("Failed to create file")?; file.write_all(&metrics_serialized) + .await .context("Failed to write to file")?; - file.sync_all().context("Failed to sync file")?; + file.sync_all().await.context("Failed to sync file")?; Ok(()) } @@ -378,7 +382,7 @@ pub fn update_metrics( Ok(()) } -pub fn save_metrics_at_path_with_updates( +pub async fn save_metrics_at_path_with_updates( mut metrics: HashMap, leader_url: &Url, metric_updates: &[MetricsUpdate], @@ -386,7 +390,7 @@ pub fn save_metrics_at_path_with_updates( ) -> Result<(), anyhow::Error> { update_metrics(&mut metrics, leader_url, metric_updates)?; - save_metrics_at_path(&metrics, path) + save_metrics_at_path(&metrics, path).await } #[cfg(test)] diff --git a/lez/wallet/src/program_facades/amm.rs b/lez/wallet/src/program_facades/amm.rs index 68802c3f..bc5c5d62 100644 --- a/lez/wallet/src/program_facades/amm.rs +++ b/lez/wallet/src/program_facades/amm.rs @@ -4,11 +4,11 @@ use lee::{AccountId, program::Program}; use token_core::TokenHolding; use crate::{AccountIdentity, ExecutionFailureKind, WalletCore}; -pub struct Amm<'wallet>(pub &'wallet mut WalletCore); +pub struct Amm<'wallet>(pub &'wallet WalletCore); impl Amm<'_> { pub async fn send_new_definition( - &mut self, + &self, user_holding_a: AccountIdentity, user_holding_b: AccountIdentity, user_holding_lp: AccountIdentity, @@ -72,7 +72,7 @@ impl Amm<'_> { } pub async fn send_swap_exact_input( - &mut self, + &self, user_holding_a: AccountIdentity, user_holding_b: AccountIdentity, swap_amount_in: u128, @@ -153,7 +153,7 @@ impl Amm<'_> { } pub async fn send_swap_exact_output( - &mut self, + &self, user_holding_a: AccountIdentity, user_holding_b: AccountIdentity, exact_amount_out: u128, @@ -234,7 +234,7 @@ impl Amm<'_> { } pub async fn send_add_liquidity( - &mut self, + &self, user_holding_a: AccountIdentity, user_holding_b: AccountIdentity, user_holding_lp: AccountIdentity, @@ -299,7 +299,7 @@ impl Amm<'_> { } pub async fn send_remove_liquidity( - &mut self, + &self, user_holding_a: AccountId, user_holding_b: AccountId, user_holding_lp: AccountIdentity, diff --git a/lez/wallet/src/program_facades/ata.rs b/lez/wallet/src/program_facades/ata.rs index 373469ba..6c9ec568 100644 --- a/lez/wallet/src/program_facades/ata.rs +++ b/lez/wallet/src/program_facades/ata.rs @@ -9,11 +9,11 @@ use lee_core::SharedSecretKey; use crate::{AccountIdentity, ExecutionFailureKind, WalletCore}; -pub struct Ata<'wallet>(pub &'wallet mut WalletCore); +pub struct Ata<'wallet>(pub &'wallet WalletCore); impl Ata<'_> { pub async fn send_create( - &mut self, + &self, owner: AccountIdentity, definition_id: AccountId, ) -> Result { @@ -44,7 +44,7 @@ impl Ata<'_> { } pub async fn send_transfer( - &mut self, + &self, owner: AccountIdentity, definition_id: AccountId, recipient_id: AccountId, @@ -80,7 +80,7 @@ impl Ata<'_> { } pub async fn send_burn( - &mut self, + &self, owner: AccountIdentity, definition_id: AccountId, amount: u128, @@ -115,7 +115,7 @@ impl Ata<'_> { } pub async fn send_create_private_owner( - &mut self, + &self, owner_id: AccountId, definition_id: AccountId, ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { @@ -147,7 +147,7 @@ impl Ata<'_> { } pub async fn send_transfer_private_owner( - &mut self, + &self, owner_id: AccountId, definition_id: AccountId, recipient_id: AccountId, @@ -184,7 +184,7 @@ impl Ata<'_> { } pub async fn send_burn_private_owner( - &mut self, + &self, owner_id: AccountId, definition_id: AccountId, amount: u128, diff --git a/lez/wallet/src/program_facades/bridge.rs b/lez/wallet/src/program_facades/bridge.rs index 4b8ad6f6..c9aa585e 100644 --- a/lez/wallet/src/program_facades/bridge.rs +++ b/lez/wallet/src/program_facades/bridge.rs @@ -3,11 +3,11 @@ use lee::{AccountId, program::Program}; use crate::{AccountIdentity, ExecutionFailureKind, WalletCore}; -pub struct Bridge<'wallet>(pub &'wallet mut WalletCore); +pub struct Bridge<'wallet>(pub &'wallet WalletCore); impl Bridge<'_> { pub async fn send_withdraw( - &mut self, + &self, sender_account_id: AccountId, amount: u64, bedrock_account_pk: [u8; 32], diff --git a/lez/wallet/src/program_facades/native_token_transfer/deshielded.rs b/lez/wallet/src/program_facades/native_token_transfer/deshielded.rs index 8354c5fc..f060e0fa 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/deshielded.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/deshielded.rs @@ -6,7 +6,7 @@ use crate::{AccountIdentity, ExecutionFailureKind}; impl NativeTokenTransfer<'_> { pub async fn send_deshielded_transfer( - &mut self, + &self, from: AccountId, to: AccountId, balance_to_move: u128, diff --git a/lez/wallet/src/program_facades/native_token_transfer/mod.rs b/lez/wallet/src/program_facades/native_token_transfer/mod.rs index c54d344a..c4e673fa 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/mod.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/mod.rs @@ -12,7 +12,7 @@ pub mod shielded; clippy::multiple_inherent_impl, reason = "impl blocks split across multiple files for organization" )] -pub struct NativeTokenTransfer<'wallet>(pub &'wallet mut WalletCore); +pub struct NativeTokenTransfer<'wallet>(pub &'wallet WalletCore); fn auth_transfer_preparation( balance_to_move: u128, diff --git a/lez/wallet/src/program_facades/native_token_transfer/private.rs b/lez/wallet/src/program_facades/native_token_transfer/private.rs index a650b78f..712d6774 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/private.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/private.rs @@ -9,7 +9,7 @@ use crate::{AccountIdentity, ExecutionFailureKind}; impl NativeTokenTransfer<'_> { pub async fn register_account_private( - &mut self, + &self, from: AccountId, ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = authenticated_transfer_core::Instruction::Initialize; @@ -34,7 +34,7 @@ impl NativeTokenTransfer<'_> { } pub async fn send_private_transfer_to_outer_account( - &mut self, + &self, from: AccountId, to_npk: NullifierPublicKey, to_vpk: ViewingPublicKey, @@ -69,7 +69,7 @@ impl NativeTokenTransfer<'_> { } pub async fn send_private_transfer_to_owned_account( - &mut self, + &self, from: AccountId, to: AccountId, balance_to_move: u128, diff --git a/lez/wallet/src/program_facades/native_token_transfer/public.rs b/lez/wallet/src/program_facades/native_token_transfer/public.rs index 81f62f8b..ee01d9c8 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/public.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/public.rs @@ -10,7 +10,7 @@ use crate::{ impl NativeTokenTransfer<'_> { pub async fn send_public_transfer( - &mut self, + &self, from: AccountIdentity, to: AccountIdentity, balance_to_move: u128, @@ -28,7 +28,7 @@ impl NativeTokenTransfer<'_> { } pub async fn register_account( - &mut self, + &self, account: AccountIdentity, ) -> Result { let instruction_data = Program::serialize_instruction(AuthTransferInstruction::Initialize)?; diff --git a/lez/wallet/src/program_facades/native_token_transfer/shielded.rs b/lez/wallet/src/program_facades/native_token_transfer/shielded.rs index 18c998c5..002b1176 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/shielded.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/shielded.rs @@ -7,7 +7,7 @@ use crate::{AccountIdentity, ExecutionFailureKind}; impl NativeTokenTransfer<'_> { pub async fn send_shielded_transfer( - &mut self, + &self, from: AccountIdentity, to: AccountId, balance_to_move: u128, @@ -36,7 +36,7 @@ impl NativeTokenTransfer<'_> { } pub async fn send_shielded_transfer_to_outer_account( - &mut self, + &self, from: AccountIdentity, to_npk: NullifierPublicKey, to_vpk: ViewingPublicKey, diff --git a/lez/wallet/src/program_facades/pinata.rs b/lez/wallet/src/program_facades/pinata.rs index d7d79844..3a1ff898 100644 --- a/lez/wallet/src/program_facades/pinata.rs +++ b/lez/wallet/src/program_facades/pinata.rs @@ -4,11 +4,11 @@ use lee_core::{MembershipProof, SharedSecretKey}; use crate::{AccountIdentity, ExecutionFailureKind, WalletCore}; -pub struct Pinata<'wallet>(pub &'wallet mut WalletCore); +pub struct Pinata<'wallet>(pub &'wallet WalletCore); impl Pinata<'_> { pub async fn claim( - &mut self, + &self, pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, @@ -35,7 +35,7 @@ impl Pinata<'_> { /// The `winner_proof` parameter is accepted for API completeness; the wallet currently fetches /// the membership proof automatically from the chain. pub async fn claim_private_owned_account_already_initialized( - &mut self, + &self, pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, @@ -46,7 +46,7 @@ impl Pinata<'_> { } pub async fn claim_private_owned_account( - &mut self, + &self, pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, diff --git a/lez/wallet/src/program_facades/token.rs b/lez/wallet/src/program_facades/token.rs index c9418ee4..d634976f 100644 --- a/lez/wallet/src/program_facades/token.rs +++ b/lez/wallet/src/program_facades/token.rs @@ -5,11 +5,11 @@ use token_core::Instruction; use crate::{AccountIdentity, ExecutionFailureKind, WalletCore}; -pub struct Token<'wallet>(pub &'wallet mut WalletCore); +pub struct Token<'wallet>(pub &'wallet WalletCore); impl Token<'_> { pub async fn send_new_definition( - &mut self, + &self, definition: AccountIdentity, supply: AccountIdentity, name: String, @@ -29,7 +29,7 @@ impl Token<'_> { } pub async fn send_new_definition_private_owned_supply( - &mut self, + &self, definition_account_id: AccountId, supply_account_id: AccountId, name: String, @@ -61,7 +61,7 @@ impl Token<'_> { } pub async fn send_new_definition_private_owned_definiton( - &mut self, + &self, definition_account_id: AccountId, supply_account_id: AccountId, name: String, @@ -93,7 +93,7 @@ impl Token<'_> { } pub async fn send_new_definition_private_owned_definiton_and_supply( - &mut self, + &self, definition_account_id: AccountId, supply_account_id: AccountId, name: String, @@ -126,7 +126,7 @@ impl Token<'_> { } pub async fn send_transfer_transaction( - &mut self, + &self, sender: AccountIdentity, recipient: AccountIdentity, amount: u128, @@ -147,7 +147,7 @@ impl Token<'_> { } pub async fn send_transfer_transaction_private_owned_account( - &mut self, + &self, sender_account_id: AccountId, recipient_account_id: AccountId, amount: u128, @@ -181,7 +181,7 @@ impl Token<'_> { } pub async fn send_transfer_transaction_private_foreign_account( - &mut self, + &self, sender_account_id: AccountId, recipient_npk: NullifierPublicKey, recipient_vpk: ViewingPublicKey, @@ -219,7 +219,7 @@ impl Token<'_> { } pub async fn send_transfer_transaction_deshielded( - &mut self, + &self, sender_account_id: AccountId, recipient_account_id: AccountId, amount: u128, @@ -252,7 +252,7 @@ impl Token<'_> { } pub async fn send_transfer_transaction_shielded_owned_account( - &mut self, + &self, sender: AccountIdentity, recipient_account_id: AccountId, amount: u128, @@ -284,7 +284,7 @@ impl Token<'_> { } pub async fn send_transfer_transaction_shielded_foreign_account( - &mut self, + &self, sender: AccountIdentity, recipient_npk: NullifierPublicKey, recipient_vpk: ViewingPublicKey, @@ -320,7 +320,7 @@ impl Token<'_> { } pub async fn send_burn_transaction( - &mut self, + &self, definition_account_id: AccountId, holder: AccountIdentity, amount: u128, @@ -341,7 +341,7 @@ impl Token<'_> { } pub async fn send_burn_transaction_private_owned_account( - &mut self, + &self, definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, @@ -375,7 +375,7 @@ impl Token<'_> { } pub async fn send_burn_transaction_deshielded_owned_account( - &mut self, + &self, definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, @@ -408,7 +408,7 @@ impl Token<'_> { } pub async fn send_burn_transaction_shielded( - &mut self, + &self, definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, @@ -441,7 +441,7 @@ impl Token<'_> { } pub async fn send_mint_transaction( - &mut self, + &self, definition: AccountIdentity, holder: AccountIdentity, amount: u128, @@ -462,7 +462,7 @@ impl Token<'_> { } pub async fn send_mint_transaction_private_owned_account( - &mut self, + &self, definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, @@ -496,7 +496,7 @@ impl Token<'_> { } pub async fn send_mint_transaction_private_foreign_account( - &mut self, + &self, definition_account_id: AccountId, holder_npk: NullifierPublicKey, holder_vpk: ViewingPublicKey, @@ -534,7 +534,7 @@ impl Token<'_> { } pub async fn send_mint_transaction_deshielded( - &mut self, + &self, definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, @@ -567,7 +567,7 @@ impl Token<'_> { } pub async fn send_mint_transaction_shielded_owned_account( - &mut self, + &self, definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, @@ -600,7 +600,7 @@ impl Token<'_> { } pub async fn send_mint_transaction_shielded_foreign_account( - &mut self, + &self, definition_account_id: AccountId, holder_npk: NullifierPublicKey, holder_vpk: ViewingPublicKey, diff --git a/lez/wallet/src/program_facades/vault.rs b/lez/wallet/src/program_facades/vault.rs index f080c2a0..dcb5c5e0 100644 --- a/lez/wallet/src/program_facades/vault.rs +++ b/lez/wallet/src/program_facades/vault.rs @@ -8,11 +8,11 @@ use lee_core::SharedSecretKey; use crate::{AccountIdentity, ExecutionFailureKind, WalletCore}; -pub struct Vault<'wallet>(pub &'wallet mut WalletCore); +pub struct Vault<'wallet>(pub &'wallet WalletCore); impl Vault<'_> { pub async fn send_transfer( - &mut self, + &self, sender_id: AccountId, recipient_id: AccountId, amount: u128, @@ -41,7 +41,7 @@ impl Vault<'_> { } pub async fn send_transfer_private_sender( - &mut self, + &self, sender_id: AccountId, recipient_id: AccountId, amount: u128, @@ -75,7 +75,7 @@ impl Vault<'_> { } pub async fn send_claim( - &mut self, + &self, owner_id: AccountId, amount: u128, ) -> Result { @@ -99,7 +99,7 @@ impl Vault<'_> { } pub async fn send_claim_private_owner( - &mut self, + &self, owner_id: AccountId, amount: u128, ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { diff --git a/test_fixtures/src/lib.rs b/test_fixtures/src/lib.rs index 5423e007..e885e357 100644 --- a/test_fixtures/src/lib.rs +++ b/test_fixtures/src/lib.rs @@ -429,11 +429,11 @@ impl BlockingTestContext { &self.runtime } - pub fn block_on<'ctx, F>(&'ctx mut self, f: impl FnOnce(&'ctx TestContext) -> F) -> F::Output + pub fn block_on<'ctx, F>(&'ctx self, f: impl FnOnce(&'ctx TestContext) -> F) -> F::Output where F: std::future::Future + 'ctx, { - let future = f(self.ctx_mut()); + let future = f(self.ctx()); self.runtime.block_on(future) }