Skip to content

Commit

Permalink
Record instructions which are precompiles (#24743) (#25004)
Browse files Browse the repository at this point in the history
* Record instructions which are "is_precompile".

(cherry picked from commit eae9a66)

Co-authored-by: Alexander Meißner <[email protected]>
  • Loading branch information
mergify[bot] and Lichtso authored May 5, 2022
1 parent d7c4e26 commit 3a3f740
Showing 1 changed file with 54 additions and 27 deletions.
81 changes: 54 additions & 27 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use {
},
solana_sdk::{
account::WritableAccount,
feature_set::{prevent_calling_precompiles_as_programs, FeatureSet},
feature_set::{
prevent_calling_precompiles_as_programs,
record_instruction_in_transaction_context_push, FeatureSet,
},
hash::Hash,
message::SanitizedMessage,
precompiles::is_precompile,
Expand Down Expand Up @@ -86,10 +89,14 @@ impl MessageProcessor {
.zip(program_indices.iter())
.enumerate()
{
if invoke_context
let is_precompile = invoke_context
.feature_set
.is_active(&prevent_calling_precompiles_as_programs::id())
&& is_precompile(program_id, |id| invoke_context.feature_set.is_active(id))
&& is_precompile(program_id, |id| invoke_context.feature_set.is_active(id));
if is_precompile
&& !invoke_context
.feature_set
.is_active(&record_instruction_in_transaction_context_push::id())
{
// Precompiled programs don't have an instruction processor
continue;
Expand Down Expand Up @@ -125,29 +132,48 @@ impl MessageProcessor {
}
})
.collect::<Vec<_>>();
let mut time = Measure::start("execute_instruction");
let mut compute_units_consumed = 0;
let result = invoke_context.process_instruction(
&instruction.data,
&instruction_accounts,
program_indices,
&mut compute_units_consumed,
timings,
);
time.stop();
*accumulated_consumed_units =
accumulated_consumed_units.saturating_add(compute_units_consumed);
timings.details.accumulate_program(
program_id,
time.as_us(),
compute_units_consumed,
result.is_err(),
);
timings.details.accumulate(&invoke_context.timings);
saturating_add_assign!(
timings.execute_accessories.process_instructions.total_us,
time.as_us()
);

let result = if is_precompile
&& invoke_context
.feature_set
.is_active(&record_instruction_in_transaction_context_push::id())
{
invoke_context
.transaction_context
.push(
program_indices,
&instruction_accounts,
&instruction.data,
true,
)
.and_then(|_| invoke_context.transaction_context.pop())
} else {
let mut time = Measure::start("execute_instruction");
let mut compute_units_consumed = 0;
let result = invoke_context.process_instruction(
&instruction.data,
&instruction_accounts,
program_indices,
&mut compute_units_consumed,
timings,
);
time.stop();
*accumulated_consumed_units =
accumulated_consumed_units.saturating_add(compute_units_consumed);
timings.details.accumulate_program(
program_id,
time.as_us(),
compute_units_consumed,
result.is_err(),
);
timings.details.accumulate(&invoke_context.timings);
saturating_add_assign!(
timings.execute_accessories.process_instructions.total_us,
time.as_us()
);
result
};

result
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?;
}
Expand Down Expand Up @@ -626,7 +652,7 @@ mod tests {
(secp256k1_program::id(), secp256k1_account),
(mock_program_id, mock_program_account),
];
let mut transaction_context = TransactionContext::new(accounts, 1, 1);
let mut transaction_context = TransactionContext::new(accounts, 1, 2);

let message = SanitizedMessage::Legacy(Message::new(
&[
Expand Down Expand Up @@ -664,5 +690,6 @@ mod tests {
InstructionError::Custom(0xbabb1e)
))
);
assert_eq!(transaction_context.get_instruction_trace().len(), 2);
}
}

0 comments on commit 3a3f740

Please sign in to comment.