compute_units.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use {
  2. solana_instruction::{AccountMeta, Instruction},
  3. solana_keypair::Keypair,
  4. solana_program_test::ProgramTest,
  5. solana_signer::Signer,
  6. solana_system_interface::instruction as system_instruction,
  7. solana_sysvar::rent,
  8. solana_transaction::Transaction,
  9. };
  10. #[should_panic]
  11. #[test]
  12. fn overflow_compute_units() {
  13. let mut program_test = ProgramTest::default();
  14. program_test.set_compute_max_units(i64::MAX as u64 + 1);
  15. }
  16. #[tokio::test]
  17. async fn max_compute_units() {
  18. let mut program_test = ProgramTest::default();
  19. program_test.set_compute_max_units(i64::MAX as u64);
  20. let context = program_test.start_with_context().await;
  21. // Invalid compute unit maximums are only triggered by BPF programs, so send
  22. // a valid instruction into a BPF program to make sure the issue doesn't
  23. // manifest.
  24. let token_2022_id = spl_generic_token::token_2022::id();
  25. let mint = Keypair::new();
  26. let rent = context.banks_client.get_rent().await.unwrap();
  27. let space = 82;
  28. let transaction = Transaction::new_signed_with_payer(
  29. &[
  30. system_instruction::create_account(
  31. &context.payer.pubkey(),
  32. &mint.pubkey(),
  33. rent.minimum_balance(space),
  34. space as u64,
  35. &token_2022_id,
  36. ),
  37. Instruction::new_with_bytes(
  38. token_2022_id,
  39. &[0; 35], // initialize mint
  40. vec![
  41. AccountMeta::new(mint.pubkey(), false),
  42. AccountMeta::new_readonly(rent::id(), false),
  43. ],
  44. ),
  45. ],
  46. Some(&context.payer.pubkey()),
  47. &[&context.payer, &mint],
  48. context.last_blockhash,
  49. );
  50. context
  51. .banks_client
  52. .process_transaction(transaction)
  53. .await
  54. .unwrap();
  55. }