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

@@ -19,6 +19,8 @@ log = "0.4.14"
num-derive = { version = "0.3" }
num-traits = { version = "0.2" }
serde = { version = "1.0.129", features = ["derive", "rc"] }
solana-frozen-abi = { path = "../frozen-abi", version = "=1.9.5" }
solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.9.5" }
solana-logger = { path = "../logger", version = "=1.9.5" }
solana-measure = { path = "../measure", version = "=1.9.5" }
solana-sdk = { path = "../sdk", version = "=1.9.5" }

View File

@@ -6,6 +6,7 @@ use {
log_collector::LogCollector,
native_loader::NativeLoader,
pre_account::PreAccount,
sysvar_cache::SysvarCache,
timings::{ExecuteDetailsTimings, ExecuteTimings},
},
solana_measure::measure::Measure,
@@ -28,7 +29,7 @@ use {
saturating_add_assign,
sysvar::Sysvar,
},
std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc},
std::{borrow::Cow, cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc},
};
pub type TransactionAccountRefCell = (Pubkey, Rc<RefCell<AccountSharedData>>);
@@ -194,7 +195,7 @@ pub struct InvokeContext<'a> {
pre_accounts: Vec<PreAccount>,
accounts: &'a [TransactionAccountRefCell],
builtin_programs: &'a [BuiltinProgram],
pub sysvars: &'a [(Pubkey, Vec<u8>)],
pub sysvar_cache: Cow<'a, SysvarCache>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
current_compute_budget: ComputeBudget,
@@ -215,7 +216,7 @@ impl<'a> InvokeContext<'a> {
rent: Rent,
accounts: &'a [TransactionAccountRefCell],
builtin_programs: &'a [BuiltinProgram],
sysvars: &'a [(Pubkey, Vec<u8>)],
sysvar_cache: Cow<'a, SysvarCache>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
executors: Rc<RefCell<Executors>>,
@@ -230,7 +231,7 @@ impl<'a> InvokeContext<'a> {
pre_accounts: Vec::new(),
accounts,
builtin_programs,
sysvars,
sysvar_cache,
log_collector,
current_compute_budget: compute_budget,
compute_budget,
@@ -254,7 +255,7 @@ impl<'a> InvokeContext<'a> {
Rent::default(),
accounts,
builtin_programs,
&[],
Cow::Owned(SysvarCache::default()),
Some(LogCollector::new_ref()),
ComputeBudget::default(),
Rc::new(RefCell::new(Executors::default())),
@@ -952,7 +953,7 @@ impl<'a> InvokeContext<'a> {
/// Get the value of a sysvar by its id
pub fn get_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Result<T, InstructionError> {
self.sysvars
self.sysvar_cache
.iter()
.find_map(|(key, data)| {
if id == key {
@@ -1069,7 +1070,7 @@ pub fn mock_process_instruction_with_sysvars(
mut program_indices: Vec<usize>,
instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
sysvars: &[(Pubkey, Vec<u8>)],
sysvar_cache: &SysvarCache,
process_instruction: ProcessInstructionWithContext,
) -> Result<(), InstructionError> {
let mut preparation =
@@ -1078,7 +1079,7 @@ pub fn mock_process_instruction_with_sysvars(
program_indices.insert(0, preparation.accounts.len());
preparation.accounts.push((*loader_id, processor_account));
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context.sysvars = sysvars;
invoke_context.sysvar_cache = Cow::Borrowed(sysvar_cache);
invoke_context.push(
&preparation.message,
&preparation.message.instructions()[0],
@@ -1100,7 +1101,7 @@ pub fn mock_process_instruction(
program_indices,
instruction_data,
keyed_accounts,
&[],
&SysvarCache::default(),
process_instruction,
)
}

View File

@@ -8,4 +8,5 @@ pub mod native_loader;
pub mod neon_evm_program;
pub mod pre_account;
pub mod stable_log;
pub mod sysvar_cache;
pub mod timings;

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