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

* Refactor: move sysvar cache to new module

(cherry picked from commit 7171c95bdd5738b835150098cd3af3bcc3d0ada7)

# 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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 153 additions and 90 deletions

2
Cargo.lock generated
View File

@ -5460,6 +5460,8 @@ dependencies = [
"num-traits",
"rustc_version 0.4.0",
"serde",
"solana-frozen-abi 1.9.5",
"solana-frozen-abi-macro 1.9.5",
"solana-logger 1.9.5",
"solana-measure",
"solana-sdk",

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

View File

@ -3282,6 +3282,8 @@ dependencies = [
"num-traits",
"rustc_version 0.4.0",
"serde",
"solana-frozen-abi 1.9.5",
"solana-frozen-abi-macro 1.9.5",
"solana-logger 1.9.5",
"solana-measure",
"solana-sdk",

View File

@ -2647,12 +2647,12 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallLogData<'a, 'b> {
mod tests {
use {
super::*,
solana_program_runtime::invoke_context::InvokeContext,
solana_program_runtime::{invoke_context::InvokeContext, sysvar_cache::SysvarCache},
solana_rbpf::{
ebpf::HOST_ALIGN, memory_region::MemoryRegion, user_error::UserError, vm::Config,
},
solana_sdk::{bpf_loader, fee_calculator::FeeCalculator, hash::hashv},
std::str::FromStr,
std::{borrow::Cow, str::FromStr},
};
macro_rules! assert_access_violation {
@ -3559,11 +3559,10 @@ mod tests {
leader_schedule_epoch: 4,
unix_timestamp: 5,
};
let mut data = vec![];
bincode::serialize_into(&mut data, &src_clock).unwrap();
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&src_clock).unwrap());
let mut invoke_context = InvokeContext::new_mock(&accounts, &[]);
let sysvars = [(sysvar::clock::id(), data)];
invoke_context.sysvars = &sysvars;
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context
.push(&message, &message.instructions()[0], &[0], &[])
.unwrap();
@ -3604,11 +3603,13 @@ mod tests {
first_normal_epoch: 3,
first_normal_slot: 4,
};
let mut data = vec![];
bincode::serialize_into(&mut data, &src_epochschedule).unwrap();
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(
sysvar::epoch_schedule::id(),
bincode::serialize(&src_epochschedule).unwrap(),
);
let mut invoke_context = InvokeContext::new_mock(&accounts, &[]);
let sysvars = [(sysvar::epoch_schedule::id(), data)];
invoke_context.sysvars = &sysvars;
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context
.push(&message, &message.instructions()[0], &[0], &[])
.unwrap();
@ -3656,11 +3657,10 @@ mod tests {
lamports_per_signature: 1,
},
};
let mut data = vec![];
bincode::serialize_into(&mut data, &src_fees).unwrap();
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(sysvar::fees::id(), bincode::serialize(&src_fees).unwrap());
let mut invoke_context = InvokeContext::new_mock(&accounts, &[]);
let sysvars = [(sysvar::fees::id(), data)];
invoke_context.sysvars = &sysvars;
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context
.push(&message, &message.instructions()[0], &[0], &[])
.unwrap();
@ -3699,11 +3699,10 @@ mod tests {
exemption_threshold: 2.0,
burn_percent: 3,
};
let mut data = vec![];
bincode::serialize_into(&mut data, &src_rent).unwrap();
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&src_rent).unwrap());
let mut invoke_context = InvokeContext::new_mock(&accounts, &[]);
let sysvars = [(sysvar::rent::id(), data)];
invoke_context.sysvars = &sysvars;
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context
.push(&message, &message.instructions()[0], &[0], &[])
.unwrap();

View File

@ -341,8 +341,11 @@ mod tests {
super::*,
crate::stake_state::{Meta, StakeState},
bincode::serialize,
solana_program_runtime::invoke_context::{
mock_process_instruction, prepare_mock_invoke_context, InvokeContext,
solana_program_runtime::{
invoke_context::{
mock_process_instruction, prepare_mock_invoke_context, InvokeContext,
},
sysvar_cache::SysvarCache,
},
solana_sdk::{
account::{self, AccountSharedData},
@ -354,9 +357,9 @@ mod tests {
instruction::{self, LockupArgs},
state::{Authorized, Lockup, StakeAuthorize},
},
sysvar::{stake_history::StakeHistory, Sysvar},
sysvar::stake_history::StakeHistory,
},
std::{cell::RefCell, rc::Rc, str::FromStr},
std::{borrow::Cow, cell::RefCell, rc::Rc, str::FromStr},
};
fn create_default_account() -> Rc<RefCell<AccountSharedData>> {
@ -428,11 +431,13 @@ mod tests {
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
let program_indices = vec![preparation.accounts.len()];
preparation.accounts.push((id(), processor_account));
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
let sysvars = [(sysvar::clock::id(), data)];
invoke_context.sysvars = &sysvars;
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(
sysvar::clock::id(),
bincode::serialize(&Clock::default()).unwrap(),
);
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context.push(
&preparation.message,
&preparation.message.instructions()[0],
@ -1074,11 +1079,13 @@ mod tests {
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
let program_indices = vec![preparation.accounts.len()];
preparation.accounts.push((id(), processor_account));
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
let sysvars = [(sysvar::clock::id(), data)];
invoke_context.sysvars = &sysvars;
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(
sysvar::clock::id(),
bincode::serialize(&Clock::default()).unwrap(),
);
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context
.push(
&preparation.message,

View File

@ -449,7 +449,9 @@ mod tests {
use {
super::*,
bincode::serialize,
solana_program_runtime::invoke_context::mock_process_instruction,
solana_program_runtime::{
invoke_context::mock_process_instruction, sysvar_cache::SysvarCache,
},
solana_sdk::{
account::{self, Account, AccountSharedData},
rent::Rent,
@ -509,14 +511,15 @@ mod tests {
.map(|(meta, account)| (meta.is_signer, meta.is_writable, meta.pubkey, account))
.collect();
let mut sysvar_cache = SysvarCache::default();
let rent = Rent::default();
let rent_sysvar = (sysvar::rent::id(), bincode::serialize(&rent).unwrap());
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&rent).unwrap());
solana_program_runtime::invoke_context::mock_process_instruction_with_sysvars(
&id(),
Vec::new(),
&instruction.data,
&keyed_accounts,
&[rent_sysvar],
&sysvar_cache,
super::process_instruction,
)
}

View File

@ -83,6 +83,7 @@ use {
TransactionAccountRefCells, TransactionExecutor,
},
log_collector::LogCollector,
sysvar_cache::SysvarCache,
timings::ExecuteTimings,
},
solana_sdk::{
@ -163,6 +164,7 @@ use {
},
};
mod sysvar_cache;
mod transaction_account_state_info;
pub const SECONDS_PER_YEAR: f64 = 365.25 * 24.0 * 60.0 * 60.0;
@ -1189,7 +1191,7 @@ pub struct Bank {
pub cost_tracker: RwLock<CostTracker>,
sysvar_cache: RwLock<Vec<(Pubkey, Vec<u8>)>>,
sysvar_cache: RwLock<SysvarCache>,
/// Current size of the accounts data. Used when processing messages to enforce a limit on its
/// maximum size.
@ -1334,7 +1336,7 @@ impl Bank {
freeze_started: AtomicBool::default(),
vote_only_bank: false,
cost_tracker: RwLock::<CostTracker>::default(),
sysvar_cache: RwLock::new(Vec::new()),
sysvar_cache: RwLock::<SysvarCache>::default(),
accounts_data_len: AtomicU64::default(),
};
@ -1587,7 +1589,7 @@ impl Bank {
)),
freeze_started: AtomicBool::new(false),
cost_tracker: RwLock::new(CostTracker::default()),
sysvar_cache: RwLock::new(Vec::new()),
sysvar_cache: RwLock::new(SysvarCache::default()),
accounts_data_len: AtomicU64::new(parent.load_accounts_data_len()),
};
@ -1775,7 +1777,7 @@ impl Bank {
freeze_started: AtomicBool::new(fields.hash != Hash::default()),
vote_only_bank: false,
cost_tracker: RwLock::new(CostTracker::default()),
sysvar_cache: RwLock::new(Vec::new()),
sysvar_cache: RwLock::new(SysvarCache::default()),
accounts_data_len: AtomicU64::new(accounts_data_len),
};
bank.finish_init(
@ -1958,11 +1960,7 @@ impl Bank {
// Update the entry in the cache
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
if let Some(position) = sysvar_cache.iter().position(|(id, _data)| id == pubkey) {
sysvar_cache[position].1 = new_account.data().to_vec();
} else {
sysvar_cache.push((*pubkey, new_account.data().to_vec()));
}
sysvar_cache.update_entry(pubkey, &new_account);
}
fn inherit_specially_retained_account_fields(
@ -2112,17 +2110,6 @@ impl Bank {
});
}
fn fill_sysvar_cache(&mut self) {
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
for id in sysvar::ALL_IDS.iter() {
if !sysvar_cache.iter().any(|(key, _data)| key == id) {
if let Some(account) = self.get_account_with_fixed_root(id) {
sysvar_cache.push((*id, account.data().to_vec()));
}
}
}
}
pub fn get_slot_history(&self) -> SlotHistory {
from_account(&self.get_account(&sysvar::slot_history::id()).unwrap()).unwrap()
}
@ -3675,21 +3662,6 @@ impl Bank {
Arc::make_mut(&mut cache).remove(pubkey);
}
/// Get the value of a cached sysvar by its id
pub fn get_cached_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Option<T> {
self.sysvar_cache
.read()
.unwrap()
.iter()
.find_map(|(key, data)| {
if id == key {
bincode::deserialize(data).ok()
} else {
None
}
})
}
pub fn load_lookup_table_addresses(
&self,
address_table_lookups: &[MessageAddressTableLookup],

View File

@ -0,0 +1,33 @@
use {
super::Bank,
solana_sdk::{
account::ReadableAccount,
pubkey::Pubkey,
sysvar::{self, Sysvar},
},
};
impl Bank {
pub(crate) fn fill_sysvar_cache(&mut self) {
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
for id in sysvar::ALL_IDS.iter() {
if !sysvar_cache.iter().any(|(key, _data)| key == id) {
if let Some(account) = self.get_account_with_fixed_root(id) {
sysvar_cache.push_entry(*id, account.data().to_vec());
}
}
}
}
/// Get the value of a cached sysvar by its id
pub fn get_cached_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Option<T> {
let sysvar_cache = self.sysvar_cache.read().unwrap();
sysvar_cache.iter().find_map(|(key, data)| {
if id == key {
bincode::deserialize(data).ok()
} else {
None
}
})
}
}

View File

@ -8,6 +8,7 @@ use {
TransactionAccountRefCell,
},
log_collector::LogCollector,
sysvar_cache::SysvarCache,
timings::ExecuteTimings,
},
solana_sdk::{
@ -17,13 +18,12 @@ use {
hash::Hash,
message::SanitizedMessage,
precompiles::is_precompile,
pubkey::Pubkey,
rent::Rent,
saturating_add_assign,
sysvar::instructions,
transaction::TransactionError,
},
std::{cell::RefCell, rc::Rc, sync::Arc},
std::{borrow::Cow, cell::RefCell, rc::Rc, sync::Arc},
};
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
@ -64,7 +64,7 @@ impl MessageProcessor {
feature_set: Arc<FeatureSet>,
compute_budget: ComputeBudget,
timings: &mut ExecuteTimings,
sysvars: &[(Pubkey, Vec<u8>)],
sysvar_cache: &SysvarCache,
blockhash: Hash,
lamports_per_signature: u64,
current_accounts_data_len: u64,
@ -73,7 +73,7 @@ impl MessageProcessor {
rent,
accounts,
builtin_programs,
sysvars,
Cow::Borrowed(sysvar_cache),
log_collector,
compute_budget,
executors,
@ -159,6 +159,7 @@ mod tests {
keyed_account::keyed_account_at_index,
message::Message,
native_loader::{self, create_loadable_account_for_test},
pubkey::Pubkey,
secp256k1_instruction::new_secp256k1_instruction,
secp256k1_program,
},
@ -253,7 +254,7 @@ mod tests {
)],
Some(&accounts[0].0),
));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&message,
@ -266,7 +267,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -296,7 +297,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -330,7 +331,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -463,6 +464,7 @@ mod tests {
)],
Some(&accounts[0].0),
));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&message,
@ -475,7 +477,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -509,7 +511,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -540,7 +542,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -585,7 +587,7 @@ mod tests {
],
None,
));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&message,
@ -598,7 +600,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,