Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
bank: Add errored compute units during simulation (#34450)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored Dec 14, 2023
1 parent 88b2585 commit d36b211
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
16 changes: 9 additions & 7 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4356,13 +4356,15 @@ impl Bank {
})
.unwrap_or_default();

let units_consumed = timings
.details
.per_program_timings
.iter()
.fold(0, |acc: u64, (_, program_timing)| {
acc.saturating_add(program_timing.accumulated_units)
});
let units_consumed =
timings
.details
.per_program_timings
.iter()
.fold(0, |acc: u64, (_, program_timing)| {
acc.saturating_add(program_timing.accumulated_units)
.saturating_add(program_timing.total_errored_units)
});

debug!("simulate_transaction: {:?}", timings);

Expand Down
26 changes: 26 additions & 0 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14114,3 +14114,29 @@ fn test_rebuild_skipped_rewrites() {
let snapshot_skipped_rewrites = snapshot_bank.calculate_skipped_rewrites();
assert_eq!(snapshot_skipped_rewrites, actual_skipped_rewrites);
}

/// Test that simulations report the compute units of failed transactions
#[test]
fn test_failed_simulation_compute_units() {
let (genesis_config, mint_keypair) = create_genesis_config(LAMPORTS_PER_SOL);
let program_id = Pubkey::new_unique();
let bank =
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| {
invoke_context.consume_checked(TEST_UNITS).unwrap();
Err(InstructionError::InvalidInstructionData)
});

let message = Message::new(
&[Instruction::new_with_bincode(program_id, &0, vec![])],
Some(&mint_keypair.pubkey()),
);
let transaction = Transaction::new(&[&mint_keypair], message, bank.last_blockhash());

bank.freeze();
let sanitized = SanitizedTransaction::from_transaction_for_tests(transaction);
let simulation = bank.simulate_transaction(sanitized);
assert_eq!(TEST_UNITS, simulation.units_consumed);
}

0 comments on commit d36b211

Please sign in to comment.