Explorar o código

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
Jon C hai 1 ano
pai
achega
ae6285ffbf
Modificáronse 2 ficheiros con 29 adicións e 49 borrados
  1. 16 10
      cli/src/nonce.rs
  2. 13 39
      cli/tests/nonce.rs

+ 16 - 10
cli/src/nonce.rs

@@ -5,7 +5,9 @@ use {
             log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
             log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
             ProcessResult,
             ProcessResult,
         },
         },
-        compute_budget::{ComputeUnitConfig, WithComputeUnitConfig},
+        compute_budget::{
+            simulate_and_update_compute_unit_limit, ComputeUnitConfig, WithComputeUnitConfig,
+        },
         memo::WithMemo,
         memo::WithMemo,
         spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
         spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
     },
     },
@@ -421,9 +423,10 @@ pub fn process_authorize_nonce_account(
     .with_memo(memo)
     .with_memo(memo)
     .with_compute_unit_config(&ComputeUnitConfig {
     .with_compute_unit_config(&ComputeUnitConfig {
         compute_unit_price,
         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);
     let mut tx = Transaction::new_unsigned(message);
     tx.try_sign(&config.signers, latest_blockhash)?;
     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 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 build_message = |lamports| {
         let ixs = if let Some(seed) = seed.clone() {
         let ixs = if let Some(seed) = seed.clone() {
             create_nonce_account_with_seed(
             create_nonce_account_with_seed(
@@ -579,10 +582,11 @@ pub fn process_new_nonce(
     .with_memo(memo)
     .with_memo(memo)
     .with_compute_unit_config(&ComputeUnitConfig {
     .with_compute_unit_config(&ComputeUnitConfig {
         compute_unit_price,
         compute_unit_price,
-        compute_unit_limit: ComputeUnitLimit::Default,
+        compute_unit_limit: ComputeUnitLimit::Simulated,
     });
     });
     let latest_blockhash = rpc_client.get_latest_blockhash()?;
     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);
     let mut tx = Transaction::new_unsigned(message);
     tx.try_sign(&config.signers, latest_blockhash)?;
     tx.try_sign(&config.signers, latest_blockhash)?;
     check_account_for_fee_with_commitment(
     check_account_for_fee_with_commitment(
@@ -648,9 +652,10 @@ pub fn process_withdraw_from_nonce_account(
     .with_memo(memo)
     .with_memo(memo)
     .with_compute_unit_config(&ComputeUnitConfig {
     .with_compute_unit_config(&ComputeUnitConfig {
         compute_unit_price,
         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);
     let mut tx = Transaction::new_unsigned(message);
     tx.try_sign(&config.signers, latest_blockhash)?;
     tx.try_sign(&config.signers, latest_blockhash)?;
     check_account_for_fee_with_commitment(
     check_account_for_fee_with_commitment(
@@ -676,9 +681,10 @@ pub(crate) fn process_upgrade_nonce_account(
         .with_memo(memo)
         .with_memo(memo)
         .with_compute_unit_config(&ComputeUnitConfig {
         .with_compute_unit_config(&ComputeUnitConfig {
             compute_unit_price,
             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);
     let mut tx = Transaction::new_unsigned(message);
     tx.try_sign(&config.signers, latest_blockhash)?;
     tx.try_sign(&config.signers, latest_blockhash)?;
     check_account_for_fee_with_commitment(
     check_account_for_fee_with_commitment(

+ 13 - 39
cli/tests/nonce.rs

@@ -20,46 +20,20 @@ use {
     },
     },
     solana_streamer::socket::SocketAddrSpace,
     solana_streamer::socket::SocketAddrSpace,
     solana_test_validator::TestValidator,
     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_keypair = Keypair::new();
     let mint_pubkey = mint_keypair.pubkey();
     let mint_pubkey = mint_keypair.pubkey();
     let faucet_addr = run_local_faucet(mint_keypair, None);
     let faucet_addr = run_local_faucet(mint_keypair, None);
     let test_validator =
     let test_validator =
         TestValidator::with_no_fees(mint_pubkey, Some(faucet_addr), SocketAddrSpace::Unspecified);
         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 =
     let rpc_client =
         RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
         RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
     let json_rpc_url = test_validator.rpc_url();
     let json_rpc_url = test_validator.rpc_url();
@@ -113,7 +87,7 @@ fn full_battery_tests(
         nonce_authority: optional_authority,
         nonce_authority: optional_authority,
         memo: None,
         memo: None,
         amount: SpendAmount::Some(sol_to_lamports(1000.0)),
         amount: SpendAmount::Some(sol_to_lamports(1000.0)),
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
 
 
     process_command(&config_payer).unwrap();
     process_command(&config_payer).unwrap();
@@ -151,7 +125,7 @@ fn full_battery_tests(
         nonce_account,
         nonce_account,
         nonce_authority: index,
         nonce_authority: index,
         memo: None,
         memo: None,
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
     process_command(&config_payer).unwrap();
     process_command(&config_payer).unwrap();
 
 
@@ -172,7 +146,7 @@ fn full_battery_tests(
         memo: None,
         memo: None,
         destination_account_pubkey: payee_pubkey,
         destination_account_pubkey: payee_pubkey,
         lamports: sol_to_lamports(100.0),
         lamports: sol_to_lamports(100.0),
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
     process_command(&config_payer).unwrap();
     process_command(&config_payer).unwrap();
     check_balance!(
     check_balance!(
@@ -197,7 +171,7 @@ fn full_battery_tests(
         nonce_authority: index,
         nonce_authority: index,
         memo: None,
         memo: None,
         new_authority: new_authority.pubkey(),
         new_authority: new_authority.pubkey(),
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
     process_command(&config_payer).unwrap();
     process_command(&config_payer).unwrap();
 
 
@@ -206,7 +180,7 @@ fn full_battery_tests(
         nonce_account,
         nonce_account,
         nonce_authority: index,
         nonce_authority: index,
         memo: None,
         memo: None,
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
     process_command(&config_payer).unwrap_err();
     process_command(&config_payer).unwrap_err();
 
 
@@ -216,7 +190,7 @@ fn full_battery_tests(
         nonce_account,
         nonce_account,
         nonce_authority: 1,
         nonce_authority: 1,
         memo: None,
         memo: None,
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
     process_command(&config_payer).unwrap();
     process_command(&config_payer).unwrap();
 
 
@@ -227,7 +201,7 @@ fn full_battery_tests(
         memo: None,
         memo: None,
         destination_account_pubkey: payee_pubkey,
         destination_account_pubkey: payee_pubkey,
         lamports: sol_to_lamports(100.0),
         lamports: sol_to_lamports(100.0),
-        compute_unit_price: None,
+        compute_unit_price,
     };
     };
     process_command(&config_payer).unwrap();
     process_command(&config_payer).unwrap();
     check_balance!(
     check_balance!(