Refactor: move sysvar cache to new module (backport #22448) (#22461)

* Refactor: move sysvar cache to new module

(cherry picked from commit 7171c95bdd)

# Conflicts:
#	Cargo.lock
#	program-runtime/Cargo.toml
#	program-runtime/src/invoke_context.rs
#	programs/bpf/Cargo.lock
#	programs/bpf_loader/src/syscalls.rs
#	programs/stake/src/stake_instruction.rs
#	programs/vote/src/vote_instruction.rs
#	runtime/src/message_processor.rs

* resolve conflicts

Co-authored-by: Justin Starry <justin@solana.com>
This commit is contained in:
mergify[bot]
2022-01-14 08:43:26 +00:00
committed by GitHub
parent 9d69f2b324
commit a6b7a3b7ff
12 changed files with 153 additions and 90 deletions

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()));
}
}
}