Skip to content

Commit

Permalink
program-test: Prohibit setting the compute unit limit past i64::MAX (
Browse files Browse the repository at this point in the history
…solana-labs#32807)

program-test: Prohibit setting the compute unit limit past the max
  • Loading branch information
joncinque authored Aug 11, 2023
1 parent 4e6fb8e commit 194c959
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ impl ProgramTest {

/// Override the default maximum compute units
pub fn set_compute_max_units(&mut self, compute_max_units: u64) {
debug_assert!(
compute_max_units <= i64::MAX as u64,
"Compute unit limit must fit in `i64::MAX`"
);
self.compute_max_units = Some(compute_max_units);
}

Expand All @@ -533,7 +537,7 @@ impl ProgramTest {
#[allow(deprecated)]
#[deprecated(since = "1.8.0", note = "please use `set_compute_max_units` instead")]
pub fn set_bpf_compute_max_units(&mut self, bpf_compute_max_units: u64) {
self.compute_max_units = Some(bpf_compute_max_units);
self.set_compute_max_units(bpf_compute_max_units);
}

/// Add an account to the test environment
Expand Down
61 changes: 61 additions & 0 deletions program-test/tests/compute_units.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use {
solana_program_test::ProgramTest,
solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signature::{Keypair, Signer},
system_instruction,
sysvar::rent,
transaction::Transaction,
},
};

#[should_panic]
#[test]
fn overflow_compute_units() {
let mut program_test = ProgramTest::default();
program_test.set_compute_max_units(i64::MAX as u64 + 1);
}

#[tokio::test]
async fn max_compute_units() {
let mut program_test = ProgramTest::default();
program_test.set_compute_max_units(i64::MAX as u64);
let mut context = program_test.start_with_context().await;

// Invalid compute unit maximums are only triggered by BPF programs, so send
// a valid instruction into a BPF program to make sure the issue doesn't
// manifest.
let token_2022_id = Pubkey::try_from("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb").unwrap();
let mint = Keypair::new();
let rent = context.banks_client.get_rent().await.unwrap();
let space = 82;
let transaction = Transaction::new_signed_with_payer(
&[
system_instruction::create_account(
&context.payer.pubkey(),
&mint.pubkey(),
rent.minimum_balance(space),
space as u64,
&token_2022_id,
),
Instruction::new_with_bytes(
token_2022_id,
&[0; 35], // initialize mint
vec![
AccountMeta::new(mint.pubkey(), false),
AccountMeta::new_readonly(rent::id(), false),
],
),
],
Some(&context.payer.pubkey()),
&[&context.payer, &mint],
context.last_blockhash,
);

context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
}

0 comments on commit 194c959

Please sign in to comment.