Skip to content

Commit

Permalink
cli: Use simulated compute units in nonce interactions (anza-xyz#2695)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joncinque authored Aug 26, 2024
1 parent a1f1e9c commit ae6285f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 49 deletions.
26 changes: 16 additions & 10 deletions cli/src/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
52 changes: 13 additions & 39 deletions cli/tests/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, use_nonce_authority: bool, compute_unit_price: Option<u64>) {
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<String>,
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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand All @@ -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!(
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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!(
Expand Down

0 comments on commit ae6285f

Please sign in to comment.