Refactor: move sysvar cache to new module (#22586)

This commit is contained in:
Justin Starry
2022-01-20 11:27:03 +08:00
committed by GitHub
parent 99846eea12
commit 2d5957a4b4
10 changed files with 174 additions and 99 deletions

View File

@@ -48,6 +48,8 @@ pub mod shred_version;
pub mod signature;
pub mod signer;
pub mod system_transaction;
#[doc(hidden)]
pub mod sysvar_cache;
pub mod timing;
pub mod transaction;
pub mod transport;

View File

@@ -1,4 +1,5 @@
use {
crate::sysvar_cache::SysvarCache,
itertools::Itertools,
solana_sdk::{
account::AccountSharedData,
@@ -8,7 +9,7 @@ use {
pubkey::Pubkey,
sysvar::Sysvar,
},
std::{cell::RefCell, collections::HashSet, fmt::Debug, rc::Rc, sync::Arc},
std::{borrow::Cow, cell::RefCell, collections::HashSet, fmt::Debug, rc::Rc, sync::Arc},
};
/// Prototype of a native loader entry point
@@ -104,8 +105,8 @@ pub trait InvokeContext {
execute_us: u64,
deserialize_us: u64,
);
/// Get sysvars
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)];
/// Get sysvar cache
fn get_sysvar_cache(&self) -> &SysvarCache;
/// Set the return data
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>);
/// Get the return data
@@ -149,7 +150,7 @@ pub fn get_sysvar<T: Sysvar>(
id: &Pubkey,
) -> Result<T, InstructionError> {
invoke_context
.get_sysvars()
.get_sysvar_cache()
.iter()
.find_map(|(key, data)| {
if id == key {
@@ -394,7 +395,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 sysvar_cache: Cow<'a, SysvarCache>,
pub disabled_features: HashSet<Pubkey>,
pub return_data: Option<(Pubkey, Vec<u8>)>,
pub execute_timings: ExecuteDetailsTimings,
@@ -412,7 +413,7 @@ impl<'a> MockInvokeContext<'a> {
},
programs: vec![],
accounts: vec![],
sysvars: vec![],
sysvar_cache: Cow::Owned(SysvarCache::default()),
disabled_features: HashSet::default(),
return_data: None,
execute_timings: ExecuteDetailsTimings::default(),
@@ -514,8 +515,8 @@ impl<'a> InvokeContext for MockInvokeContext<'a> {
_deserialize_us: u64,
) {
}
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)] {
&self.sysvars
fn get_sysvar_cache(&self) -> &SysvarCache {
&self.sysvar_cache
}
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>) {
self.return_data = return_data;

39
sdk/src/sysvar_cache.rs Normal file
View File

@@ -0,0 +1,39 @@
use {
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
pubkey::Pubkey,
},
std::ops::Deref,
};
#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl ::solana_frozen_abi::abi_example::AbiExample for SysvarCache {
fn example() -> Self {
// SysvarCache is not Serialize so just rely on Default.
SysvarCache::default()
}
}
#[derive(Default, Clone, Debug)]
pub struct SysvarCache(Vec<(Pubkey, Vec<u8>)>);
impl Deref for SysvarCache {
type Target = Vec<(Pubkey, Vec<u8>)>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl SysvarCache {
pub fn push_entry(&mut self, pubkey: Pubkey, data: Vec<u8>) {
self.0.push((pubkey, data));
}
pub fn update_entry(&mut self, pubkey: &Pubkey, new_account: &AccountSharedData) {
if let Some(position) = self.iter().position(|(id, _data)| id == pubkey) {
self.0[position].1 = new_account.data().to_vec();
} else {
self.0.push((*pubkey, new_account.data().to_vec()));
}
}
}