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

@ -121,12 +121,19 @@ pub trait InvokeContext {
execute_us: u64,
deserialize_us: u64,
);
/// Get sysvars
#[allow(clippy::type_complexity)]
fn get_sysvars(&self) -> &RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>>;
/// Get sysvar data
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>>;
/// Get this invocation's compute budget
fn get_compute_budget(&self) -> &ComputeBudget;
/// Set this invocation's blockhash
fn set_blockhash(&mut self, hash: Hash);
/// Get this invocation's blockhash
fn get_blockhash(&self) -> &Hash;
/// Set this invocation's `FeeCalculator`
fn set_fee_calculator(&mut self, fee_calculator: FeeCalculator);
/// Get this invocation's `FeeCalculator`
fn get_fee_calculator(&self) -> &FeeCalculator;
/// Set the return data
@ -350,7 +357,8 @@ pub struct MockInvokeContext<'a> {
pub compute_meter: MockComputeMeter,
pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>,
pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
pub sysvars: Vec<(Pubkey, Option<Rc<Vec<u8>>>)>,
#[allow(clippy::type_complexity)]
pub sysvars: RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>>,
pub disabled_features: HashSet<Pubkey>,
pub blockhash: Hash,
pub fee_calculator: FeeCalculator,
@ -369,7 +377,7 @@ impl<'a> MockInvokeContext<'a> {
},
programs: vec![],
accounts: vec![],
sysvars: vec![],
sysvars: RefCell::new(Vec::new()),
disabled_features: HashSet::default(),
blockhash: Hash::default(),
fee_calculator: FeeCalculator::default(),
@ -390,21 +398,6 @@ impl<'a> MockInvokeContext<'a> {
}
}
pub fn mock_set_sysvar<T: Sysvar>(
mock_invoke_context: &mut MockInvokeContext,
id: Pubkey,
sysvar: T,
) -> Result<(), InstructionError> {
let mut data = Vec::with_capacity(T::size_of());
bincode::serialize_into(&mut data, &sysvar).map_err(|err| {
ic_msg!(mock_invoke_context, "Unable to serialize sysvar: {:?}", err);
InstructionError::GenericError
})?;
mock_invoke_context.sysvars.push((id, Some(Rc::new(data))));
Ok(())
}
impl<'a> InvokeContext for MockInvokeContext<'a> {
fn push(
&mut self,
@ -498,17 +491,28 @@ impl<'a> InvokeContext for MockInvokeContext<'a> {
_deserialize_us: u64,
) {
}
#[allow(clippy::type_complexity)]
fn get_sysvars(&self) -> &RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>> {
&self.sysvars
}
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
self.sysvars
.borrow()
.iter()
.find_map(|(key, sysvar)| if id == key { sysvar.clone() } else { None })
}
fn get_compute_budget(&self) -> &ComputeBudget {
&self.compute_budget
}
fn set_blockhash(&mut self, hash: Hash) {
self.blockhash = hash;
}
fn get_blockhash(&self) -> &Hash {
&self.blockhash
}
fn set_fee_calculator(&mut self, fee_calculator: FeeCalculator) {
self.fee_calculator = fee_calculator;
}
fn get_fee_calculator(&self) -> &FeeCalculator {
&self.fee_calculator
}