From 96e8db1052c4ea9b667883967f7375939111c5bd Mon Sep 17 00:00:00 2001 From: Marvin Jones Date: Tue, 16 Jun 2026 09:47:56 -0400 Subject: [PATCH] refactor integration_tests: extract new_account/send/restored_private_account helpers Move repeated account-creation and transfer boilerplate into four public helpers in integration_tests/src/lib.rs (new_account, send, restored_private_account, assert_public_account_restored), then apply them across keys.rs, ata.rs, auth_transfer/public.rs, auth_transfer/private.rs, token.rs, and amm.rs. Net result: -816 lines of boilerplate. --- integration_tests/src/lib.rs | 75 ++++ integration_tests/tests/amm.rs | 156 +------- integration_tests/tests/ata.rs | 77 ++-- .../tests/auth_transfer/private.rs | 93 +---- .../tests/auth_transfer/public.rs | 140 ++----- integration_tests/tests/keys.rs | 118 +----- integration_tests/tests/token.rs | 362 ++---------------- 7 files changed, 204 insertions(+), 817 deletions(-) diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index 07212251..f176ca04 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -6,8 +6,83 @@ use std::time::Duration; use anyhow::{Context as _, Result}; +use key_protocol::key_management::key_tree::chain_index::ChainIndex; +use lee::AccountId; use log::info; pub use test_fixtures::*; +use wallet::{ + cli::{ + CliAccountMention, Command, SubcommandReturnValue, + account::{AccountSubcommand, NewSubcommand}, + programs::native_token_transfer::AuthTransferSubcommand, + }, + storage::key_chain::FoundPrivateAccount, +}; + +/// Create a private or public account at the given chain index and return its ID. +/// Pass `cci: None` to use the wallet's next available chain index. +pub async fn new_account( + ctx: &mut TestContext, + private: bool, + cci: Option, +) -> Result { + let subcommand = if private { + NewSubcommand::Private { cci, label: None } + } else { + NewSubcommand::Public { cci, label: None } + }; + let result = wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Account(AccountSubcommand::New(subcommand)), + ) + .await?; + let SubcommandReturnValue::RegisterAccount { account_id } = result else { + anyhow::bail!("Expected RegisterAccount return value"); + }; + Ok(account_id) +} + +/// Send `amount` from `from` to `to` via an authenticated transfer (identifier 0). +pub async fn send( + ctx: &mut TestContext, + from: CliAccountMention, + to: CliAccountMention, + amount: u128, +) -> Result<()> { + let command = Command::AuthTransfer(AuthTransferSubcommand::Send { + from, + to: Some(to), + to_npk: None, + to_vpk: None, + to_keys: None, + to_identifier: Some(0), + amount, + }); + wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + Ok(()) +} + +/// Look up a restored private account for `account_id`, panicking with `label` if absent. +pub fn restored_private_account<'a>( + ctx: &'a TestContext, + account_id: AccountId, + label: &str, +) -> FoundPrivateAccount<'a> { + ctx.wallet() + .storage() + .key_chain() + .private_account(account_id) + .unwrap_or_else(|| panic!("{label} should be restored")) +} + +/// Assert that a restored public account's signing key exists, panicking with `label` if absent. +pub fn assert_public_account_restored(ctx: &TestContext, account_id: AccountId, label: &str) { + ctx.wallet() + .storage() + .key_chain() + .pub_account_signing_key(account_id) + .unwrap_or_else(|| panic!("{label} should be restored")); +} /// Maximum time to wait for the indexer to catch up to the sequencer. pub const L2_TO_L1_TIMEOUT: Duration = Duration::from_mins(7); diff --git a/integration_tests/tests/amm.rs b/integration_tests/tests/amm.rs index 9f953001..748e393f 100644 --- a/integration_tests/tests/amm.rs +++ b/integration_tests/tests/amm.rs @@ -7,7 +7,7 @@ use std::time::Duration; use anyhow::Result; -use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, public_mention}; +use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, new_account, public_mention}; use log::info; use sequencer_service_rpc::RpcClient as _; use tokio::test; @@ -25,94 +25,22 @@ async fn amm_public() -> Result<()> { let mut ctx = TestContext::new().await?; // Create new account for the token definition - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id_1, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id_1 = new_account(&mut ctx, false, None).await?; // Create new account for the token supply holder - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id_1, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id_1 = new_account(&mut ctx, false, None).await?; // Create new account for receiving a token transaction - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id_1, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id_1 = new_account(&mut ctx, false, None).await?; // Create new account for the token definition - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id_2, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id_2 = new_account(&mut ctx, false, None).await?; // Create new account for the token supply holder - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id_2, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id_2 = new_account(&mut ctx, false, None).await?; // Create new account for receiving a token transaction - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id_2, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id_2 = new_account(&mut ctx, false, None).await?; // Create new token let subcommand = TokenProgramAgnosticSubcommand::New { @@ -174,19 +102,7 @@ async fn amm_public() -> Result<()> { // Setup accounts // Create new account for the user holding lp - let SubcommandReturnValue::RegisterAccount { - account_id: user_holding_lp, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let user_holding_lp = new_account(&mut ctx, false, None).await?; // Send creation tx let subcommand = AmmProgramAgnosticSubcommand::New { @@ -412,33 +328,9 @@ async fn amm_new_pool_using_labels() -> Result<()> { let mut ctx = TestContext::new().await?; // Create token 1 accounts - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id_1, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id_1 = new_account(&mut ctx, false, None).await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id_1, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id_1 = new_account(&mut ctx, false, None).await?; // Create holding_a with a label let holding_a_label = Label::new("amm-holding-a-label"); @@ -457,33 +349,9 @@ async fn amm_new_pool_using_labels() -> Result<()> { }; // Create token 2 accounts - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id_2, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id_2 = new_account(&mut ctx, false, None).await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id_2, - } = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await? - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id_2 = new_account(&mut ctx, false, None).await?; // Create holding_b with a label let holding_b_label = Label::new("amm-holding-b-label"); diff --git a/integration_tests/tests/ata.rs b/integration_tests/tests/ata.rs index 7faac67e..3c51fd4f 100644 --- a/integration_tests/tests/ata.rs +++ b/integration_tests/tests/ata.rs @@ -9,7 +9,7 @@ use std::time::Duration; use anyhow::{Context as _, Result}; use associated_token_account_core::{compute_ata_seed, get_associated_token_account_id}; use integration_tests::{ - TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, private_mention, public_mention, + TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, new_account, private_mention, public_mention, verify_commitment_is_in_state, }; use log::info; @@ -17,50 +17,17 @@ use sequencer_service_rpc::RpcClient as _; use token_core::{TokenDefinition, TokenHolding}; use tokio::test; use wallet::cli::{ - Command, SubcommandReturnValue, - account::{AccountSubcommand, NewSubcommand}, + Command, programs::{ata::AtaSubcommand, token::TokenProgramAgnosticSubcommand}, }; -/// Create a public account and return its ID. -async fn new_public_account(ctx: &mut TestContext) -> Result { - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { account_id } = result else { - anyhow::bail!("Expected RegisterAccount return value"); - }; - Ok(account_id) -} - -/// Create a private account and return its ID. -async fn new_private_account(ctx: &mut TestContext) -> Result { - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { account_id } = result else { - anyhow::bail!("Expected RegisterAccount return value"); - }; - Ok(account_id) -} - #[test] async fn create_ata_initializes_holding_account() -> Result<()> { let mut ctx = TestContext::new().await?; - let definition_account_id = new_public_account(&mut ctx).await?; - let supply_account_id = new_public_account(&mut ctx).await?; - let owner_account_id = new_public_account(&mut ctx).await?; + let definition_account_id = new_account(&mut ctx, false, None).await?; + let supply_account_id = new_account(&mut ctx, false, None).await?; + let owner_account_id = new_account(&mut ctx, false, None).await?; // Create a fungible token let total_supply = 100_u128; @@ -121,9 +88,9 @@ async fn create_ata_initializes_holding_account() -> Result<()> { async fn create_ata_is_idempotent() -> Result<()> { let mut ctx = TestContext::new().await?; - let definition_account_id = new_public_account(&mut ctx).await?; - let supply_account_id = new_public_account(&mut ctx).await?; - let owner_account_id = new_public_account(&mut ctx).await?; + let definition_account_id = new_account(&mut ctx, false, None).await?; + let supply_account_id = new_account(&mut ctx, false, None).await?; + let owner_account_id = new_account(&mut ctx, false, None).await?; // Create a fungible token wallet::cli::execute_subcommand( @@ -196,10 +163,10 @@ async fn create_ata_is_idempotent() -> Result<()> { async fn transfer_and_burn_via_ata() -> Result<()> { let mut ctx = TestContext::new().await?; - let definition_account_id = new_public_account(&mut ctx).await?; - let supply_account_id = new_public_account(&mut ctx).await?; - let sender_account_id = new_public_account(&mut ctx).await?; - let recipient_account_id = new_public_account(&mut ctx).await?; + let definition_account_id = new_account(&mut ctx, false, None).await?; + let supply_account_id = new_account(&mut ctx, false, None).await?; + let sender_account_id = new_account(&mut ctx, false, None).await?; + let recipient_account_id = new_account(&mut ctx, false, None).await?; let total_supply = 1000_u128; @@ -355,9 +322,9 @@ async fn transfer_and_burn_via_ata() -> Result<()> { async fn create_ata_with_private_owner() -> Result<()> { let mut ctx = TestContext::new().await?; - let definition_account_id = new_public_account(&mut ctx).await?; - let supply_account_id = new_public_account(&mut ctx).await?; - let owner_account_id = new_private_account(&mut ctx).await?; + let definition_account_id = new_account(&mut ctx, false, None).await?; + let supply_account_id = new_account(&mut ctx, false, None).await?; + let owner_account_id = new_account(&mut ctx, true, None).await?; // Create a fungible token wallet::cli::execute_subcommand( @@ -424,10 +391,10 @@ async fn create_ata_with_private_owner() -> Result<()> { async fn transfer_via_ata_private_owner() -> Result<()> { let mut ctx = TestContext::new().await?; - let definition_account_id = new_public_account(&mut ctx).await?; - let supply_account_id = new_public_account(&mut ctx).await?; - let sender_account_id = new_private_account(&mut ctx).await?; - let recipient_account_id = new_public_account(&mut ctx).await?; + let definition_account_id = new_account(&mut ctx, false, None).await?; + let supply_account_id = new_account(&mut ctx, false, None).await?; + let sender_account_id = new_account(&mut ctx, true, None).await?; + let recipient_account_id = new_account(&mut ctx, false, None).await?; let total_supply = 1000_u128; @@ -549,9 +516,9 @@ async fn transfer_via_ata_private_owner() -> Result<()> { async fn burn_via_ata_private_owner() -> Result<()> { let mut ctx = TestContext::new().await?; - let definition_account_id = new_public_account(&mut ctx).await?; - let supply_account_id = new_public_account(&mut ctx).await?; - let holder_account_id = new_private_account(&mut ctx).await?; + let definition_account_id = new_account(&mut ctx, false, None).await?; + let supply_account_id = new_account(&mut ctx, false, None).await?; + let holder_account_id = new_account(&mut ctx, true, None).await?; let total_supply = 500_u128; diff --git a/integration_tests/tests/auth_transfer/private.rs b/integration_tests/tests/auth_transfer/private.rs index 30f0cfdd..23fb3170 100644 --- a/integration_tests/tests/auth_transfer/private.rs +++ b/integration_tests/tests/auth_transfer/private.rs @@ -3,8 +3,8 @@ use std::time::Duration; use anyhow::{Context as _, Result}; use common::transaction::LeeTransaction; use integration_tests::{ - TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, fetch_privacy_preserving_tx, private_mention, - public_mention, verify_commitment_is_in_state, + TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, fetch_privacy_preserving_tx, new_account, + private_mention, public_mention, send, verify_commitment_is_in_state, }; use lee::{ AccountId, SharedSecretKey, execute_and_prove, @@ -34,17 +34,7 @@ async fn private_transfer_to_owned_account() -> Result<()> { let from: AccountId = ctx.existing_private_accounts()[0]; let to: AccountId = ctx.existing_private_accounts()[1]; - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: private_mention(from), - to: Some(private_mention(to)), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send(&mut ctx, private_mention(from), private_mention(to), 100).await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -125,17 +115,7 @@ async fn deshielded_transfer_to_public_account() -> Result<()> { .context("Failed to get sender's private account")?; assert_eq!(from_acc.balance, 10000); - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: private_mention(from), - to: Some(public_mention(to)), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send(&mut ctx, private_mention(from), public_mention(to), 100).await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -167,18 +147,7 @@ async fn private_transfer_to_owned_account_using_claiming_path() -> Result<()> { let from: AccountId = ctx.existing_private_accounts()[0]; // Create a new private account - let command = Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })); - - let sub_ret = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::RegisterAccount { - account_id: to_account_id, - } = sub_ret - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let to_account_id = new_account(&mut ctx, true, None).await?; // Get the keys for the newly created account let to = ctx @@ -239,17 +208,7 @@ async fn shielded_transfer_to_owned_private_account() -> Result<()> { let from: AccountId = ctx.existing_public_accounts()[0]; let to: AccountId = ctx.existing_private_accounts()[1]; - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: public_mention(from), - to: Some(private_mention(to)), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send(&mut ctx, public_mention(from), private_mention(to), 100).await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -332,18 +291,7 @@ async fn private_transfer_to_owned_account_continuous_run_path() -> Result<()> { let from: AccountId = ctx.existing_private_accounts()[0]; // Create a new private account - let command = Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })); - let sub_ret = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - - let SubcommandReturnValue::RegisterAccount { - account_id: to_account_id, - } = sub_ret - else { - anyhow::bail!("Failed to register account"); - }; + let to_account_id = new_account(&mut ctx, true, None).await?; // Get the newly created account's keys let to = ctx @@ -396,14 +344,7 @@ async fn private_transfer_to_owned_account_continuous_run_path() -> Result<()> { async fn initialize_private_account() -> Result<()> { let mut ctx = TestContext::new().await?; - let command = Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })); - let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::RegisterAccount { account_id } = result else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let account_id = new_account(&mut ctx, true, None).await?; let command = Command::AuthTransfer(AuthTransferSubcommand::Init { account_id: private_mention(account_id), @@ -455,17 +396,13 @@ async fn private_transfer_using_from_label() -> Result<()> { wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; // Send using the label instead of account ID - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: CliAccountMention::Label(label), - to: Some(private_mention(to)), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send( + &mut ctx, + CliAccountMention::Label(label), + private_mention(to), + 100, + ) + .await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; diff --git a/integration_tests/tests/auth_transfer/public.rs b/integration_tests/tests/auth_transfer/public.rs index 5bbf0954..f103e4b8 100644 --- a/integration_tests/tests/auth_transfer/public.rs +++ b/integration_tests/tests/auth_transfer/public.rs @@ -2,16 +2,17 @@ use std::time::Duration; use anyhow::Result; use common::transaction::LeeTransaction; -use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, public_mention}; -use lee::public_transaction; +use integration_tests::{ + TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, new_account, public_mention, send, +}; +use lee::{program::Program, public_transaction, system_faucet_account_id}; use log::info; use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::{ account::Label, cli::{ - CliAccountMention, Command, SubcommandReturnValue, - account::{AccountSubcommand, NewSubcommand}, + CliAccountMention, Command, account::AccountSubcommand, programs::native_token_transfer::AuthTransferSubcommand, }, }; @@ -19,18 +20,12 @@ use wallet::{ #[test] async fn successful_transfer_to_existing_account() -> Result<()> { let mut ctx = TestContext::new().await?; + let (acc0, acc1) = ( + ctx.existing_public_accounts()[0], + ctx.existing_public_accounts()[1], + ); - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: public_mention(ctx.existing_public_accounts()[0]), - to: Some(public_mention(ctx.existing_public_accounts()[1])), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send(&mut ctx, public_mention(acc0), public_mention(acc1), 100).await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -58,38 +53,16 @@ async fn successful_transfer_to_existing_account() -> Result<()> { pub async fn successful_transfer_to_new_account() -> Result<()> { let mut ctx = TestContext::new().await?; - let command = Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })); + let new_persistent_account_id = new_account(&mut ctx, false, None).await?; + let acc0 = ctx.existing_public_accounts()[0]; - wallet::cli::execute_subcommand(ctx.wallet_mut(), command) - .await - .unwrap(); - - let new_persistent_account_id = ctx - .wallet() - .storage() - .key_chain() - .public_account_ids() - .map(|(account_id, _)| account_id) - .find(|acc_id| { - *acc_id != ctx.existing_public_accounts()[0] - && *acc_id != ctx.existing_public_accounts()[1] - }) - .expect("Failed to find newly created account in the wallet storage"); - - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: public_mention(ctx.existing_public_accounts()[0]), - to: Some(public_mention(new_persistent_account_id)), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send( + &mut ctx, + public_mention(acc0), + public_mention(new_persistent_account_id), + 100, + ) + .await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -155,19 +128,13 @@ async fn failed_transfer_with_insufficient_balance() -> Result<()> { #[test] async fn two_consecutive_successful_transfers() -> Result<()> { let mut ctx = TestContext::new().await?; + let (acc0, acc1) = ( + ctx.existing_public_accounts()[0], + ctx.existing_public_accounts()[1], + ); // First transfer - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: public_mention(ctx.existing_public_accounts()[0]), - to: Some(public_mention(ctx.existing_public_accounts()[1])), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send(&mut ctx, public_mention(acc0), public_mention(acc1), 100).await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -191,17 +158,7 @@ async fn two_consecutive_successful_transfers() -> Result<()> { info!("First TX Success!"); // Second transfer - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: public_mention(ctx.existing_public_accounts()[0]), - to: Some(public_mention(ctx.existing_public_accounts()[1])), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + send(&mut ctx, public_mention(acc0), public_mention(acc1), 100).await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -231,14 +188,7 @@ async fn two_consecutive_successful_transfers() -> Result<()> { async fn initialize_public_account() -> Result<()> { let mut ctx = TestContext::new().await?; - let command = Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })); - let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::RegisterAccount { account_id } = result else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let account_id = new_account(&mut ctx, false, None).await?; let command = Command::AuthTransfer(AuthTransferSubcommand::Init { account_id: public_mention(account_id), @@ -274,17 +224,14 @@ async fn successful_transfer_using_from_label() -> Result<()> { wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; // Send using the label instead of account ID - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: CliAccountMention::Label(label), - to: Some(public_mention(ctx.existing_public_accounts()[1])), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + let acc1 = ctx.existing_public_accounts()[1]; + send( + &mut ctx, + CliAccountMention::Label(label), + public_mention(acc1), + 100, + ) + .await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; @@ -320,17 +267,14 @@ async fn successful_transfer_using_to_label() -> Result<()> { wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; // Send using the label for the recipient - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: public_mention(ctx.existing_public_accounts()[0]), - to: Some(CliAccountMention::Label(label)), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; + let acc0 = ctx.existing_public_accounts()[0]; + send( + &mut ctx, + public_mention(acc0), + CliAccountMention::Label(label), + 100, + ) + .await?; info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; diff --git a/integration_tests/tests/keys.rs b/integration_tests/tests/keys.rs index 5f203b15..7fc73c62 100644 --- a/integration_tests/tests/keys.rs +++ b/integration_tests/tests/keys.rs @@ -8,124 +8,32 @@ use std::{str::FromStr as _, time::Duration}; use anyhow::{Context as _, Result}; use integration_tests::{ - TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, fetch_privacy_preserving_tx, private_mention, - public_mention, verify_commitment_is_in_state, + TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, assert_public_account_restored, + fetch_privacy_preserving_tx, new_account, private_mention, public_mention, + restored_private_account, send, verify_commitment_is_in_state, }; use key_protocol::key_management::key_tree::chain_index::ChainIndex; use lee::AccountId; use log::info; use sequencer_service_rpc::RpcClient as _; use tokio::test; -use wallet::{ - cli::{ - CliAccountMention, Command, SubcommandReturnValue, - account::{AccountSubcommand, NewSubcommand}, - programs::native_token_transfer::AuthTransferSubcommand, - }, - storage::key_chain::FoundPrivateAccount, +use wallet::cli::{ + Command, SubcommandReturnValue, account::AccountSubcommand, + programs::native_token_transfer::AuthTransferSubcommand, }; -/// Create a private or public account at the given chain index and return its ID. -async fn new_account(ctx: &mut TestContext, private: bool, cci: ChainIndex) -> Result { - let subcommand = if private { - NewSubcommand::Private { - cci: Some(cci), - label: None, - } - } else { - NewSubcommand::Public { - cci: Some(cci), - label: None, - } - }; - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(subcommand)), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { account_id } = result else { - anyhow::bail!("Expected RegisterAccount return value"); - }; - Ok(account_id) -} - -/// Send `amount` from `from` to `to` via an authenticated transfer (identifier 0). -async fn send( - ctx: &mut TestContext, - from: CliAccountMention, - to: CliAccountMention, - amount: u128, -) -> Result<()> { - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from, - to: Some(to), - to_npk: None, - to_vpk: None, - to_keys: None, - to_identifier: Some(0), - amount, - }); - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - Ok(()) -} - -/// Look up the restored private account for `account_id`, asserting it exists. -fn restored_private_account<'a>( - ctx: &'a TestContext, - account_id: AccountId, - label: &str, -) -> FoundPrivateAccount<'a> { - ctx.wallet() - .storage() - .key_chain() - .private_account(account_id) - .unwrap_or_else(|| panic!("{label} should be restored")) -} - -/// Assert that a restored public account's signing key exists. -fn assert_public_account_restored(ctx: &TestContext, account_id: AccountId, label: &str) { - ctx.wallet() - .storage() - .key_chain() - .pub_account_signing_key(account_id) - .unwrap_or_else(|| panic!("{label} should be restored")); -} - #[test] async fn sync_private_account_with_non_zero_chain_index() -> Result<()> { let mut ctx = TestContext::new().await?; let from: AccountId = ctx.existing_private_accounts()[0]; - // Create a new private account - let command = Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })); - + // Key Tree shift — create 3 accounts to advance the key index for _ in 0..3 { - // Key Tree shift - // This way we have account with child index > 0. - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { account_id: _ } = result else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + new_account(&mut ctx, true, None).await?; } - let sub_ret = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::RegisterAccount { - account_id: to_account_id, - } = sub_ret - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let to_account_id = new_account(&mut ctx, true, None).await?; // Get the keys for the newly created account let to_account = ctx @@ -188,8 +96,8 @@ async fn restore_keys_from_seed() -> Result<()> { let from: AccountId = ctx.existing_private_accounts()[0]; // Create private accounts at root and /0 - let to_account_id1 = new_account(&mut ctx, true, ChainIndex::root()).await?; - let to_account_id2 = new_account(&mut ctx, true, ChainIndex::from_str("/0")?).await?; + let to_account_id1 = new_account(&mut ctx, true, Some(ChainIndex::root())).await?; + let to_account_id2 = new_account(&mut ctx, true, Some(ChainIndex::from_str("/0")?)).await?; // Send to both private accounts send( @@ -210,8 +118,8 @@ async fn restore_keys_from_seed() -> Result<()> { let from: AccountId = ctx.existing_public_accounts()[0]; // Create public accounts at root and /0 - let to_account_id3 = new_account(&mut ctx, false, ChainIndex::root()).await?; - let to_account_id4 = new_account(&mut ctx, false, ChainIndex::from_str("/0")?).await?; + let to_account_id3 = new_account(&mut ctx, false, Some(ChainIndex::root())).await?; + let to_account_id4 = new_account(&mut ctx, false, Some(ChainIndex::from_str("/0")?)).await?; // Send to both public accounts send( diff --git a/integration_tests/tests/token.rs b/integration_tests/tests/token.rs index 60bd3de8..b5ddd6c3 100644 --- a/integration_tests/tests/token.rs +++ b/integration_tests/tests/token.rs @@ -8,7 +8,7 @@ use std::time::Duration; use anyhow::{Context as _, Result}; use integration_tests::{ - TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, private_mention, public_mention, + TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, new_account, private_mention, public_mention, verify_commitment_is_in_state, }; use key_protocol::key_management::key_tree::chain_index::ChainIndex; @@ -30,52 +30,13 @@ async fn create_and_transfer_public_token() -> Result<()> { let mut ctx = TestContext::new().await?; // Create new account for the token definition - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, false, None).await?; // Create new account for the token supply holder - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, false, None).await?; // Create new account for receiving a token transaction - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, false, None).await?; // Create new token let name = "A NAME".to_owned(); @@ -274,52 +235,13 @@ async fn create_and_transfer_token_with_private_supply() -> Result<()> { let mut ctx = TestContext::new().await?; // Create new account for the token definition (public) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, false, None).await?; // Create new account for the token supply holder (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, true, None).await?; // Create new account for receiving a token transaction (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, true, None).await?; // Create new token let name = "A NAME".to_owned(); @@ -448,36 +370,10 @@ async fn create_token_with_private_definition() -> Result<()> { let mut ctx = TestContext::new().await?; // Create token definition account (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: Some(ChainIndex::root()), - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, true, Some(ChainIndex::root())).await?; // Create supply account (public) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: Some(ChainIndex::root()), - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, false, Some(ChainIndex::root())).await?; // Create token with private definition let name = "A NAME".to_owned(); @@ -518,36 +414,10 @@ async fn create_token_with_private_definition() -> Result<()> { ); // Create private recipient account - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id_private, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id_private = new_account(&mut ctx, true, None).await?; // Create public recipient account - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id_public, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id_public = new_account(&mut ctx, false, None).await?; // Mint to public account let mint_amount_public = 10; @@ -646,36 +516,10 @@ async fn create_token_with_private_definition_and_supply() -> Result<()> { let mut ctx = TestContext::new().await?; // Create token definition account (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, true, None).await?; // Create supply account (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, true, None).await?; // Create token with both private definition and supply let name = "A NAME".to_owned(); @@ -722,20 +566,7 @@ async fn create_token_with_private_definition_and_supply() -> Result<()> { ); // Create recipient account - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, true, None).await?; // Transfer tokens let transfer_amount = 7; @@ -804,52 +635,13 @@ async fn shielded_token_transfer() -> Result<()> { let mut ctx = TestContext::new().await?; // Create token definition account (public) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, false, None).await?; // Create supply account (public) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, false, None).await?; // Create recipient account (private) for shielded transfer - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, true, None).await?; // Create token let name = "A NAME".to_owned(); @@ -928,52 +720,13 @@ async fn deshielded_token_transfer() -> Result<()> { let mut ctx = TestContext::new().await?; // Create token definition account (public) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, false, None).await?; // Create supply account (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, true, None).await?; // Create recipient account (public) for deshielded transfer - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, false, None).await?; // Create token with private supply let name = "A NAME".to_owned(); @@ -1052,36 +805,10 @@ async fn token_claiming_path_with_private_accounts() -> Result<()> { let mut ctx = TestContext::new().await?; // Create token definition account (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, true, None).await?; // Create supply account (private) - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: supply_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let supply_account_id = new_account(&mut ctx, true, None).await?; // Create token let name = "A NAME".to_owned(); @@ -1099,20 +826,7 @@ async fn token_claiming_path_with_private_accounts() -> Result<()> { tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; // Create new private account for claiming path - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Private { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, true, None).await?; // Get keys for foreign mint (claiming path) let holder = ctx @@ -1263,20 +977,7 @@ async fn transfer_token_using_from_label() -> Result<()> { let mut ctx = TestContext::new().await?; // Create definition account - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: definition_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let definition_account_id = new_account(&mut ctx, false, None).await?; // Create supply account with a label let supply_label = Label::new("token-supply-sender"); @@ -1296,20 +997,7 @@ async fn transfer_token_using_from_label() -> Result<()> { }; // Create recipient account - let result = wallet::cli::execute_subcommand( - ctx.wallet_mut(), - Command::Account(AccountSubcommand::New(NewSubcommand::Public { - cci: None, - label: None, - })), - ) - .await?; - let SubcommandReturnValue::RegisterAccount { - account_id: recipient_account_id, - } = result - else { - anyhow::bail!("Expected RegisterAccount return value"); - }; + let recipient_account_id = new_account(&mut ctx, false, None).await?; // Create token let total_supply = 50;