Refactor: move sysvar cache to new module (#22586)
This commit is contained in:
@@ -116,6 +116,7 @@ use {
|
||||
slot_history::SlotHistory,
|
||||
system_transaction,
|
||||
sysvar::{self},
|
||||
sysvar_cache::SysvarCache,
|
||||
timing::years_as_slots,
|
||||
transaction::{self, Result, Transaction, TransactionError},
|
||||
},
|
||||
@@ -144,6 +145,8 @@ use {
|
||||
},
|
||||
};
|
||||
|
||||
mod sysvar_cache;
|
||||
|
||||
pub const SECONDS_PER_YEAR: f64 = 365.25 * 24.0 * 60.0 * 60.0;
|
||||
|
||||
pub const MAX_LEADER_SCHEDULE_STAKES: Epoch = 5;
|
||||
@@ -1159,7 +1162,7 @@ pub struct Bank {
|
||||
|
||||
pub cost_tracker: RwLock<CostTracker>,
|
||||
|
||||
sysvar_cache: RwLock<Vec<(Pubkey, Vec<u8>)>>,
|
||||
sysvar_cache: RwLock<SysvarCache>,
|
||||
}
|
||||
|
||||
impl Default for BlockhashQueue {
|
||||
@@ -1433,7 +1436,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()),
|
||||
};
|
||||
|
||||
let mut ancestors = Vec::with_capacity(1 + new.parents().len());
|
||||
@@ -1610,7 +1613,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()),
|
||||
};
|
||||
bank.finish_init(
|
||||
genesis_config,
|
||||
@@ -1789,11 +1792,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(
|
||||
@@ -1931,17 +1930,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()
|
||||
}
|
||||
|
17
runtime/src/bank/sysvar_cache.rs
Normal file
17
runtime/src/bank/sysvar_cache.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use {
|
||||
super::Bank,
|
||||
solana_sdk::{account::ReadableAccount, 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -29,9 +29,11 @@ use {
|
||||
rent::Rent,
|
||||
saturating_add_assign, system_program,
|
||||
sysvar::instructions,
|
||||
sysvar_cache::SysvarCache,
|
||||
transaction::TransactionError,
|
||||
},
|
||||
std::{
|
||||
borrow::Cow,
|
||||
cell::{Ref, RefCell},
|
||||
collections::HashMap,
|
||||
rc::Rc,
|
||||
@@ -277,7 +279,7 @@ pub struct ThisInvokeContext<'a> {
|
||||
pre_accounts: Vec<PreAccount>,
|
||||
accounts: &'a [TransactionAccountRefCell],
|
||||
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
||||
sysvars: &'a [(Pubkey, Vec<u8>)],
|
||||
sysvar_cache: Cow<'a, SysvarCache>,
|
||||
logger: Rc<RefCell<dyn Logger>>,
|
||||
bpf_compute_budget: BpfComputeBudget,
|
||||
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
||||
@@ -298,7 +300,7 @@ impl<'a> ThisInvokeContext<'a> {
|
||||
executable_accounts: &'a [TransactionAccountRefCell],
|
||||
accounts: &'a [TransactionAccountRefCell],
|
||||
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
||||
sysvars: &'a [(Pubkey, Vec<u8>)],
|
||||
sysvar_cache: Cow<'a, SysvarCache>,
|
||||
log_collector: Option<Rc<LogCollector>>,
|
||||
bpf_compute_budget: BpfComputeBudget,
|
||||
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
||||
@@ -327,7 +329,7 @@ impl<'a> ThisInvokeContext<'a> {
|
||||
pre_accounts,
|
||||
accounts,
|
||||
programs,
|
||||
sysvars,
|
||||
sysvar_cache,
|
||||
logger: Rc::new(RefCell::new(ThisLogger { log_collector })),
|
||||
bpf_compute_budget,
|
||||
compute_meter,
|
||||
@@ -507,8 +509,8 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
|
||||
self.timings.execute_us += execute_us;
|
||||
self.timings.deserialize_us += deserialize_us;
|
||||
}
|
||||
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;
|
||||
@@ -1184,7 +1186,7 @@ impl MessageProcessor {
|
||||
executable_accounts: &[TransactionAccountRefCell],
|
||||
accounts: &[TransactionAccountRefCell],
|
||||
rent_collector: &RentCollector,
|
||||
sysvars: &[(Pubkey, Vec<u8>)],
|
||||
sysvar_cache: &SysvarCache,
|
||||
log_collector: Option<Rc<LogCollector>>,
|
||||
executors: Rc<RefCell<Executors>>,
|
||||
instruction_recorder: Option<InstructionRecorder>,
|
||||
@@ -1234,7 +1236,7 @@ impl MessageProcessor {
|
||||
executable_accounts,
|
||||
accounts,
|
||||
&self.programs,
|
||||
sysvars,
|
||||
Cow::Borrowed(sysvar_cache),
|
||||
log_collector,
|
||||
bpf_compute_budget,
|
||||
compute_meter,
|
||||
@@ -1299,7 +1301,7 @@ impl MessageProcessor {
|
||||
bpf_compute_budget: BpfComputeBudget,
|
||||
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
||||
timings: &mut ExecuteTimings,
|
||||
sysvars: &[(Pubkey, Vec<u8>)],
|
||||
sysvar_cache: &SysvarCache,
|
||||
) -> Result<(), TransactionError> {
|
||||
for (instruction_index, instruction) in message.instructions.iter().enumerate() {
|
||||
let instruction_recorder = instruction_recorders
|
||||
@@ -1312,7 +1314,7 @@ impl MessageProcessor {
|
||||
&loaders[instruction_index],
|
||||
accounts,
|
||||
rent_collector,
|
||||
sysvars,
|
||||
sysvar_cache,
|
||||
log_collector.clone(),
|
||||
executors.clone(),
|
||||
instruction_recorder,
|
||||
@@ -1386,7 +1388,7 @@ mod tests {
|
||||
&[],
|
||||
&accounts,
|
||||
&[],
|
||||
&[],
|
||||
Cow::Owned(SysvarCache::default()),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
@@ -1990,6 +1992,7 @@ mod tests {
|
||||
Some(&accounts[0].0),
|
||||
);
|
||||
|
||||
let sysvar_cache = SysvarCache::default();
|
||||
let result = message_processor.process_message(
|
||||
&message,
|
||||
&loaders,
|
||||
@@ -2002,7 +2005,7 @@ mod tests {
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteTimings::default(),
|
||||
&[],
|
||||
&sysvar_cache,
|
||||
);
|
||||
assert_eq!(result, Ok(()));
|
||||
assert_eq!(accounts[0].1.borrow().lamports(), 100);
|
||||
@@ -2029,7 +2032,7 @@ mod tests {
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteTimings::default(),
|
||||
&[],
|
||||
&sysvar_cache,
|
||||
);
|
||||
assert_eq!(
|
||||
result,
|
||||
@@ -2060,7 +2063,7 @@ mod tests {
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteTimings::default(),
|
||||
&[],
|
||||
&sysvar_cache,
|
||||
);
|
||||
assert_eq!(
|
||||
result,
|
||||
@@ -2170,6 +2173,7 @@ mod tests {
|
||||
)],
|
||||
Some(&accounts[0].0),
|
||||
);
|
||||
let sysvar_cache = SysvarCache::default();
|
||||
let result = message_processor.process_message(
|
||||
&message,
|
||||
&loaders,
|
||||
@@ -2182,7 +2186,7 @@ mod tests {
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteTimings::default(),
|
||||
&[],
|
||||
&sysvar_cache,
|
||||
);
|
||||
assert_eq!(
|
||||
result,
|
||||
@@ -2213,7 +2217,7 @@ mod tests {
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteTimings::default(),
|
||||
&[],
|
||||
&sysvar_cache,
|
||||
);
|
||||
assert_eq!(result, Ok(()));
|
||||
|
||||
@@ -2241,7 +2245,7 @@ mod tests {
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteTimings::default(),
|
||||
&[],
|
||||
&sysvar_cache,
|
||||
);
|
||||
assert_eq!(result, Ok(()));
|
||||
assert_eq!(accounts[0].1.borrow().lamports(), 80);
|
||||
@@ -2342,6 +2346,7 @@ mod tests {
|
||||
let feature_set = FeatureSet::all_enabled();
|
||||
let demote_program_write_locks = feature_set.is_active(&demote_program_write_locks::id());
|
||||
|
||||
let sysvar_cache = SysvarCache::default();
|
||||
let mut invoke_context = ThisInvokeContext::new(
|
||||
&caller_program_id,
|
||||
Rent::default(),
|
||||
@@ -2350,7 +2355,7 @@ mod tests {
|
||||
&executable_accounts,
|
||||
&accounts,
|
||||
programs.as_slice(),
|
||||
&[],
|
||||
Cow::Borrowed(&sysvar_cache),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
@@ -2419,7 +2424,7 @@ mod tests {
|
||||
&executable_accounts,
|
||||
&accounts,
|
||||
programs.as_slice(),
|
||||
&[],
|
||||
Cow::Borrowed(&sysvar_cache),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
@@ -2562,6 +2567,7 @@ mod tests {
|
||||
);
|
||||
let message = Message::new(&[callee_instruction.clone()], None);
|
||||
|
||||
let sysvar_cache = SysvarCache::default();
|
||||
let mut invoke_context = ThisInvokeContext::new(
|
||||
&caller_program_id,
|
||||
Rent::default(),
|
||||
@@ -2570,7 +2576,7 @@ mod tests {
|
||||
&executable_accounts,
|
||||
&accounts,
|
||||
programs.as_slice(),
|
||||
&[],
|
||||
Cow::Borrowed(&sysvar_cache),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
@@ -2635,7 +2641,7 @@ mod tests {
|
||||
&executable_accounts,
|
||||
&accounts,
|
||||
programs.as_slice(),
|
||||
&[],
|
||||
Cow::Borrowed(&sysvar_cache),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
|
Reference in New Issue
Block a user