Revert "Stop caching sysvars, instead load them ahead of time (backport #21108) (#22466)"

This reverts commit 5d3f3bc9b1.
This commit is contained in:
Justin Starry
2022-01-14 11:13:04 +08:00
parent 5d3f3bc9b1
commit 304afd42c6
8 changed files with 167 additions and 112 deletions

View File

@@ -1,7 +1,6 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
use lazy_static::lazy_static;
pub mod clock;
pub mod epoch_schedule;
@@ -14,25 +13,17 @@ pub mod slot_hashes;
pub mod slot_history;
pub mod stake_history;
lazy_static! {
pub static ref ALL_IDS: Vec<Pubkey> = vec![
clock::id(),
epoch_schedule::id(),
#[allow(deprecated)]
fees::id(),
#[allow(deprecated)]
recent_blockhashes::id(),
rent::id(),
rewards::id(),
slot_hashes::id(),
slot_history::id(),
stake_history::id(),
instructions::id(),
];
}
pub fn is_sysvar_id(id: &Pubkey) -> bool {
ALL_IDS.iter().any(|key| key == id)
clock::check_id(id)
|| epoch_schedule::check_id(id)
|| fees::check_id(id)
|| recent_blockhashes::check_id(id)
|| rent::check_id(id)
|| rewards::check_id(id)
|| slot_hashes::check_id(id)
|| slot_history::check_id(id)
|| stake_history::check_id(id)
|| instructions::check_id(id)
}
#[macro_export]

View File

@@ -103,8 +103,8 @@ pub trait InvokeContext {
execute_us: u64,
deserialize_us: u64,
);
/// Get sysvars
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)];
/// Get sysvar data
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>>;
/// Set the return data
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>);
/// Get the return data
@@ -145,20 +145,15 @@ pub fn get_sysvar<T: Sysvar>(
invoke_context: &dyn InvokeContext,
id: &Pubkey,
) -> Result<T, InstructionError> {
invoke_context
.get_sysvars()
.iter()
.find_map(|(key, data)| {
if id == key {
bincode::deserialize(data).ok()
} else {
None
}
})
.ok_or_else(|| {
ic_msg!(invoke_context, "Unable to get sysvar {}", id);
InstructionError::UnsupportedSysvar
})
let sysvar_data = invoke_context.get_sysvar_data(id).ok_or_else(|| {
ic_msg!(invoke_context, "Unable to get sysvar {}", id);
InstructionError::UnsupportedSysvar
})?;
bincode::deserialize(&sysvar_data).map_err(|err| {
ic_msg!(invoke_context, "Unable to get sysvar {}: {:?}", id, err);
InstructionError::UnsupportedSysvar
})
}
#[derive(Clone, Copy, Debug, AbiExample, PartialEq)]
@@ -391,7 +386,7 @@ pub struct MockInvokeContext<'a> {
pub compute_meter: MockComputeMeter,
pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>,
pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
pub sysvars: Vec<(Pubkey, Vec<u8>)>,
pub sysvars: Vec<(Pubkey, Option<Rc<Vec<u8>>>)>,
pub disabled_features: HashSet<Pubkey>,
pub return_data: Option<(Pubkey, Vec<u8>)>,
}
@@ -422,6 +417,21 @@ 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,
@@ -509,8 +519,10 @@ impl<'a> InvokeContext for MockInvokeContext<'a> {
_deserialize_us: u64,
) {
}
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)] {
&self.sysvars
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
self.sysvars
.iter()
.find_map(|(key, sysvar)| if id == key { sysvar.clone() } else { None })
}
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>) {
self.return_data = return_data;