Refactor: Cleanup InvokeContext (#20785)

* Move blockhash and fee_calculator in ThisInvokeContext instead of using a reference.

* Moves tx_wide_compute_cap into InvokeContext::push().

* Adds ThisInvokeContext::new_mock() constructor.

* Adds missing loader account in uses of MockInvokeContext.

* Use keyed_account_at_index() when accessing keyed_accounts.

* Makes sysvar interface consistent between ThisInvokeContext and MockInvokeContext,
in order to add InvokeContext::get_sysvars().

* Adds InvokeContext::set_blockhash() and InvokeContext ::set_fee_calculator().

* Adds new_mock_with_features.

* Makes ancestors optional in ThisInvokeContext.

* Adds prepare_mock_invoke_context() and mock_process_instruction().
This commit is contained in:
Alexander Meißner
2021-10-21 20:57:42 +02:00
committed by GitHub
parent 0ac89841bf
commit 97c2732d02
11 changed files with 350 additions and 248 deletions

View File

@ -33,7 +33,7 @@ use {
message::Message,
native_token::sol_to_lamports,
poh_config::PohConfig,
process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext},
process_instruction::{self, stable_log, InvokeContext, ProcessInstructionWithContext},
program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR},
pubkey::Pubkey,
rent::Rent,
@ -204,24 +204,6 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned>(
var_addr: *mut u8,
) -> u64 {
let invoke_context = get_invoke_context();
let sysvar_data = match invoke_context.get_sysvar_data(id).ok_or_else(|| {
ic_msg!(invoke_context, "Unable to get Sysvar {}", id);
UNSUPPORTED_SYSVAR
}) {
Ok(sysvar_data) => sysvar_data,
Err(err) => return err,
};
let var: T = match bincode::deserialize(&sysvar_data) {
Ok(sysvar_data) => sysvar_data,
Err(_) => return UNSUPPORTED_SYSVAR,
};
unsafe {
*(var_addr as *mut _ as *mut T) = var;
}
if invoke_context
.get_compute_meter()
.try_borrow_mut()
@ -233,7 +215,13 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned>(
panic!("Exceeded compute budget");
}
SUCCESS
match process_instruction::get_sysvar::<T>(invoke_context, id) {
Ok(sysvar_data) => unsafe {
*(var_addr as *mut _ as *mut T) = sysvar_data;
SUCCESS
},
Err(_) => UNSUPPORTED_SYSVAR,
}
}
struct SyscallStubs {}