From ae6285ffbfbd737c29ddf2464796b0d90adf3119 Mon Sep 17 00:00:00 2001 From: Jon C Date: Mon, 26 Aug 2024 13:19:30 +0200 Subject: [PATCH] cli: Use simulated compute units in nonce interactions (#2695) * cli: Use simulated compute unit limit for nonces #### Problem The CLI can simulate to get the compute budget used by a transaction, but nonce interactions are still using the default compute unit limit. #### Summary of changes Refactor the tests into `test_case`s, add tests for setting a compute unit price, and then change compute unit limit to `Simulated`. * Add compute unit price test case * Change to using simulated compute units everywhere * Run simulations where it isn't done normally * Fix clippy issues --- cli/src/nonce.rs | 26 ++++++++++++++--------- cli/tests/nonce.rs | 52 ++++++++++++---------------------------------- 2 files changed, 29 insertions(+), 49 deletions(-) diff --git a/cli/src/nonce.rs b/cli/src/nonce.rs index 36ee8cc89e80fd..708ce2b677afdc 100644 --- a/cli/src/nonce.rs +++ b/cli/src/nonce.rs @@ -5,7 +5,9 @@ use { log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult, }, - compute_budget::{ComputeUnitConfig, WithComputeUnitConfig}, + compute_budget::{ + simulate_and_update_compute_unit_limit, ComputeUnitConfig, WithComputeUnitConfig, + }, memo::WithMemo, spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount}, }, @@ -421,9 +423,10 @@ pub fn process_authorize_nonce_account( .with_memo(memo) .with_compute_unit_config(&ComputeUnitConfig { compute_unit_price, - compute_unit_limit: ComputeUnitLimit::Default, + compute_unit_limit: ComputeUnitLimit::Simulated, }); - let message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + let mut message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + simulate_and_update_compute_unit_limit(rpc_client, &mut message)?; let mut tx = Transaction::new_unsigned(message); tx.try_sign(&config.signers, latest_blockhash)?; @@ -469,7 +472,7 @@ pub fn process_create_nonce_account( let nonce_authority = nonce_authority.unwrap_or_else(|| config.signers[0].pubkey()); - let compute_unit_limit = ComputeUnitLimit::Default; + let compute_unit_limit = ComputeUnitLimit::Simulated; let build_message = |lamports| { let ixs = if let Some(seed) = seed.clone() { create_nonce_account_with_seed( @@ -579,10 +582,11 @@ pub fn process_new_nonce( .with_memo(memo) .with_compute_unit_config(&ComputeUnitConfig { compute_unit_price, - compute_unit_limit: ComputeUnitLimit::Default, + compute_unit_limit: ComputeUnitLimit::Simulated, }); let latest_blockhash = rpc_client.get_latest_blockhash()?; - let message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + let mut message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + simulate_and_update_compute_unit_limit(rpc_client, &mut message)?; let mut tx = Transaction::new_unsigned(message); tx.try_sign(&config.signers, latest_blockhash)?; check_account_for_fee_with_commitment( @@ -648,9 +652,10 @@ pub fn process_withdraw_from_nonce_account( .with_memo(memo) .with_compute_unit_config(&ComputeUnitConfig { compute_unit_price, - compute_unit_limit: ComputeUnitLimit::Default, + compute_unit_limit: ComputeUnitLimit::Simulated, }); - let message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + let mut message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + simulate_and_update_compute_unit_limit(rpc_client, &mut message)?; let mut tx = Transaction::new_unsigned(message); tx.try_sign(&config.signers, latest_blockhash)?; check_account_for_fee_with_commitment( @@ -676,9 +681,10 @@ pub(crate) fn process_upgrade_nonce_account( .with_memo(memo) .with_compute_unit_config(&ComputeUnitConfig { compute_unit_price, - compute_unit_limit: ComputeUnitLimit::Default, + compute_unit_limit: ComputeUnitLimit::Simulated, }); - let message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + let mut message = Message::new(&ixs, Some(&config.signers[0].pubkey())); + simulate_and_update_compute_unit_limit(rpc_client, &mut message)?; let mut tx = Transaction::new_unsigned(message); tx.try_sign(&config.signers, latest_blockhash)?; check_account_for_fee_with_commitment( diff --git a/cli/tests/nonce.rs b/cli/tests/nonce.rs index ff305e3954e46f..a9edd06656efd6 100644 --- a/cli/tests/nonce.rs +++ b/cli/tests/nonce.rs @@ -20,46 +20,20 @@ use { }, solana_streamer::socket::SocketAddrSpace, solana_test_validator::TestValidator, + test_case::test_case, }; -#[test] -fn test_nonce() { - let mint_keypair = Keypair::new(); - let mint_pubkey = mint_keypair.pubkey(); - let faucet_addr = run_local_faucet(mint_keypair, None); - let test_validator = - TestValidator::with_no_fees(mint_pubkey, Some(faucet_addr), SocketAddrSpace::Unspecified); - - full_battery_tests(test_validator, None, false); -} - -#[test] -fn test_nonce_with_seed() { - let mint_keypair = Keypair::new(); - let mint_pubkey = mint_keypair.pubkey(); - let faucet_addr = run_local_faucet(mint_keypair, None); - let test_validator = - TestValidator::with_no_fees(mint_pubkey, Some(faucet_addr), SocketAddrSpace::Unspecified); - - full_battery_tests(test_validator, Some(String::from("seed")), false); -} - -#[test] -fn test_nonce_with_authority() { +#[test_case(None, false, None; "base")] +#[test_case(Some(String::from("seed")), false, None; "with_seed")] +#[test_case(None, true, None; "with_authority")] +#[test_case(None, false, Some(1_000_000); "with_compute_unit_price")] +fn test_nonce(seed: Option, use_nonce_authority: bool, compute_unit_price: Option) { let mint_keypair = Keypair::new(); let mint_pubkey = mint_keypair.pubkey(); let faucet_addr = run_local_faucet(mint_keypair, None); let test_validator = TestValidator::with_no_fees(mint_pubkey, Some(faucet_addr), SocketAddrSpace::Unspecified); - full_battery_tests(test_validator, None, true); -} - -fn full_battery_tests( - test_validator: TestValidator, - seed: Option, - use_nonce_authority: bool, -) { let rpc_client = RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed()); let json_rpc_url = test_validator.rpc_url(); @@ -113,7 +87,7 @@ fn full_battery_tests( nonce_authority: optional_authority, memo: None, amount: SpendAmount::Some(sol_to_lamports(1000.0)), - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap(); @@ -151,7 +125,7 @@ fn full_battery_tests( nonce_account, nonce_authority: index, memo: None, - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap(); @@ -172,7 +146,7 @@ fn full_battery_tests( memo: None, destination_account_pubkey: payee_pubkey, lamports: sol_to_lamports(100.0), - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap(); check_balance!( @@ -197,7 +171,7 @@ fn full_battery_tests( nonce_authority: index, memo: None, new_authority: new_authority.pubkey(), - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap(); @@ -206,7 +180,7 @@ fn full_battery_tests( nonce_account, nonce_authority: index, memo: None, - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap_err(); @@ -216,7 +190,7 @@ fn full_battery_tests( nonce_account, nonce_authority: 1, memo: None, - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap(); @@ -227,7 +201,7 @@ fn full_battery_tests( memo: None, destination_account_pubkey: payee_pubkey, lamports: sol_to_lamports(100.0), - compute_unit_price: None, + compute_unit_price, }; process_command(&config_payer).unwrap(); check_balance!(