Refactor: move sysvar cache to new module

This commit is contained in:
Justin Starry
2022-01-12 11:19:11 +08:00
committed by Trent Nelson
parent b27333e52d
commit 7171c95bdd
12 changed files with 144 additions and 87 deletions

View File

@ -79,6 +79,7 @@ use {
BuiltinProgram, Executor, Executors, ProcessInstructionWithContext, TransactionExecutor,
},
log_collector::LogCollector,
sysvar_cache::SysvarCache,
timings::ExecuteTimings,
},
solana_sdk::{
@ -160,6 +161,7 @@ use {
},
};
mod sysvar_cache;
mod transaction_account_state_info;
pub const SECONDS_PER_YEAR: f64 = 365.25 * 24.0 * 60.0 * 60.0;
@ -1186,7 +1188,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.
@ -1331,7 +1333,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(),
};
@ -1584,7 +1586,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()),
};
@ -1772,7 +1774,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(
@ -1955,11 +1957,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(
@ -2109,17 +2107,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()
}
@ -3629,21 +3616,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

@ -5,6 +5,7 @@ use {
instruction_recorder::InstructionRecorder,
invoke_context::{BuiltinProgram, Executors, InvokeContext},
log_collector::LogCollector,
sysvar_cache::SysvarCache,
timings::ExecuteTimings,
},
solana_sdk::{
@ -14,14 +15,13 @@ use {
hash::Hash,
message::SanitizedMessage,
precompiles::is_precompile,
pubkey::Pubkey,
rent::Rent,
saturating_add_assign,
sysvar::instructions,
transaction::TransactionError,
transaction_context::{InstructionAccount, TransactionContext},
},
std::{cell::RefCell, rc::Rc, sync::Arc},
std::{borrow::Cow, cell::RefCell, rc::Rc, sync::Arc},
};
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
@ -62,7 +62,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,
@ -71,7 +71,7 @@ impl MessageProcessor {
transaction_context,
rent,
builtin_programs,
sysvars,
Cow::Borrowed(sysvar_cache),
log_collector,
compute_budget,
executors,
@ -166,6 +166,7 @@ mod tests {
instruction::{AccountMeta, Instruction, InstructionError},
message::Message,
native_loader::{self, create_loadable_account_for_test},
pubkey::Pubkey,
secp256k1_instruction::new_secp256k1_instruction,
secp256k1_program,
},
@ -257,6 +258,7 @@ mod tests {
)],
Some(transaction_context.get_key_of_account_at_index(0)),
));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&message,
@ -269,7 +271,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -310,7 +312,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -343,7 +345,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -457,6 +459,7 @@ mod tests {
)],
Some(transaction_context.get_key_of_account_at_index(0)),
));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&message,
@ -469,7 +472,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -503,7 +506,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -534,7 +537,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,
@ -595,6 +598,7 @@ mod tests {
],
None,
));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&message,
@ -607,7 +611,7 @@ mod tests {
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
&mut ExecuteTimings::default(),
&[],
&sysvar_cache,
Hash::default(),
0,
0,