Skip to content

Commit

Permalink
program-test consumes some units as native (solana-labs#34714)
Browse files Browse the repository at this point in the history
program-test consumes some units as native;
harden a unit test wrt if builtin consumes units;
  • Loading branch information
tao-stones authored Jan 10, 2024
1 parent c5d51ac commit 88f8d11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ pub fn invoke_builtin_function(
let instruction_data = instruction_context.get_instruction_data();
let instruction_account_indices = 0..instruction_context.get_number_of_instruction_accounts();

// mock builtin program must consume units
invoke_context.consume_checked(1)?;

let log_collector = invoke_context.get_log_collector();
let program_id = instruction_context.get_last_program_key(transaction_context)?;
stable_log::program_invoke(
Expand Down
13 changes: 11 additions & 2 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14015,7 +14015,16 @@ fn test_failed_simulation_compute_units() {
Bank::new_with_mockup_builtin_for_tests(&genesis_config, program_id, MockBuiltin::vm).0;

const TEST_UNITS: u64 = 10_000;
declare_process_instruction!(MockBuiltin, 1, |invoke_context| {
const MOCK_BUILTIN_UNITS: u64 = 1;
let expected_consumed_units = if bank
.feature_set
.is_active(&solana_sdk::feature_set::native_programs_consume_cu::id())
{
TEST_UNITS + MOCK_BUILTIN_UNITS
} else {
TEST_UNITS
};
declare_process_instruction!(MockBuiltin, MOCK_BUILTIN_UNITS, |invoke_context| {
invoke_context.consume_checked(TEST_UNITS).unwrap();
Err(InstructionError::InvalidInstructionData)
});
Expand All @@ -14029,5 +14038,5 @@ fn test_failed_simulation_compute_units() {
bank.freeze();
let sanitized = SanitizedTransaction::from_transaction_for_tests(transaction);
let simulation = bank.simulate_transaction(&sanitized, false);
assert_eq!(TEST_UNITS, simulation.units_consumed);
assert_eq!(expected_consumed_units, simulation.units_consumed);
}

0 comments on commit 88f8d11

Please sign in to comment.