Skip to content

Commit

Permalink
Removes override parameters from mock_process_instruction().
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Apr 3, 2023
1 parent 6287879 commit 04d071c
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 343 deletions.
16 changes: 5 additions & 11 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub struct InvokeContext<'a> {
rent: Rent,
pre_accounts: Vec<PreAccount>,
builtin_programs: &'a [BuiltinProgram],
pub sysvar_cache: &'a SysvarCache,
sysvar_cache: &'a SysvarCache,
pub trace_log_stack: Vec<TraceLogStackFrame>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
Expand Down Expand Up @@ -900,18 +900,16 @@ macro_rules! with_mock_invoke_context {
};
}

pub fn mock_process_instruction(
pub fn mock_process_instruction<F: FnMut(&mut InvokeContext)>(
loader_id: &Pubkey,
mut program_indices: Vec<IndexOfAccount>,
instruction_data: &[u8],
mut transaction_accounts: Vec<TransactionAccount>,
instruction_account_metas: Vec<AccountMeta>,
sysvar_cache_override: Option<&SysvarCache>,
feature_set_override: Option<Arc<FeatureSet>>,
expected_result: Result<(), InstructionError>,
process_instruction: ProcessInstructionWithContext,
mut pre_adjustments: F,
) -> Vec<AccountSharedData> {
program_indices.insert(0, transaction_accounts.len() as IndexOfAccount);
let mut instruction_accounts: Vec<InstructionAccount> =
Vec::with_capacity(instruction_account_metas.len());
for (instruction_account_index, account_meta) in instruction_account_metas.iter().enumerate() {
Expand All @@ -936,20 +934,16 @@ pub fn mock_process_instruction(
is_writable: account_meta.is_writable,
});
}
program_indices.insert(0, transaction_accounts.len() as IndexOfAccount);
let processor_account = AccountSharedData::new(0, 0, &native_loader::id());
transaction_accounts.push((*loader_id, processor_account));
with_mock_invoke_context!(invoke_context, transaction_context, transaction_accounts);
if let Some(sysvar_cache) = sysvar_cache_override {
invoke_context.sysvar_cache = &sysvar_cache;
}
if let Some(feature_set) = feature_set_override {
invoke_context.feature_set = feature_set;
}
let builtin_programs = &[BuiltinProgram {
program_id: *loader_id,
process_instruction,
}];
invoke_context.builtin_programs = builtin_programs;
pre_adjustments(&mut invoke_context);
let result = invoke_context.process_instruction(
instruction_data,
&instruction_accounts,
Expand Down
12 changes: 4 additions & 8 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,10 +1725,9 @@ mod tests {
instruction_data,
transaction_accounts,
instruction_accounts,
None,
None,
expected_result,
super::process_instruction,
|_invoke_context| {},
)
}

Expand Down Expand Up @@ -2011,12 +2010,10 @@ mod tests {
&[],
vec![(program_id, program_account.clone())],
Vec::new(),
None,
None,
Err(InstructionError::ProgramFailedToComplete),
|invoke_context: &mut InvokeContext| {
super::process_instruction,
|invoke_context| {
invoke_context.mock_set_remaining(0);
super::process_instruction(invoke_context)
},
);

Expand Down Expand Up @@ -2557,10 +2554,9 @@ mod tests {
&instruction_data,
transaction_accounts,
instruction_accounts,
None,
None,
expected_result,
super::process_instruction,
|_invoke_context| {},
)
}

Expand Down
25 changes: 21 additions & 4 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1820,14 +1820,14 @@ mod tests {
vm::{BuiltInFunction, Config},
},
solana_sdk::{
account::AccountSharedData,
account::{create_account_shared_data_for_test, AccountSharedData},
bpf_loader,
fee_calculator::FeeCalculator,
hash::hashv,
instruction::Instruction,
program::check_type_assumptions,
stable_layout::stable_instruction::StableInstruction,
sysvar::{clock::Clock, epoch_schedule::EpochSchedule},
sysvar::{self, clock::Clock, epoch_schedule::EpochSchedule},
},
std::{mem, str::FromStr},
};
Expand Down Expand Up @@ -3299,8 +3299,25 @@ mod tests {
sysvar_cache.set_fees(src_fees.clone());
sysvar_cache.set_rent(src_rent);

prepare_mockup!(invoke_context, program_id, bpf_loader::id());
invoke_context.sysvar_cache = &sysvar_cache;
let transaction_accounts = vec![
(
sysvar::clock::id(),
create_account_shared_data_for_test(&src_clock),
),
(
sysvar::epoch_schedule::id(),
create_account_shared_data_for_test(&src_epochschedule),
),
(
sysvar::fees::id(),
create_account_shared_data_for_test(&src_fees),
),
(
sysvar::rent::id(),
create_account_shared_data_for_test(&src_rent),
),
];
with_mock_invoke_context!(invoke_context, transaction_context, transaction_accounts);

// Test clock sysvar
{
Expand Down
3 changes: 1 addition & 2 deletions programs/config/src/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,9 @@ mod tests {
instruction_data,
transaction_accounts,
instruction_accounts,
None,
None,
expected_result,
super::process_instruction,
|_invoke_context| {},
)
}

Expand Down
3 changes: 1 addition & 2 deletions programs/loader-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,9 @@ mod tests {
instruction_data,
transaction_accounts,
instruction_accounts,
None,
None,
expected_result,
super::process_instruction,
|_invoke_context| {},
)
}

Expand Down
3 changes: 1 addition & 2 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,8 +1433,6 @@ fn assert_instruction_count() {
&[],
transaction_accounts,
instruction_accounts,
None,
None,
Ok(()),
|invoke_context: &mut InvokeContext| {
let expected_consumption: u64 = invoke_context
Expand All @@ -1458,6 +1456,7 @@ fn assert_instruction_count() {
assert_eq!(consumption, expected_consumption);
Ok(())
},
|_invoke_context| {},
);
}
}
Expand Down
Loading

0 comments on commit 04d071c

Please sign in to comment.