Skip to content

Commit

Permalink
SVM: Reduce public visibility of sysvar_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed Jun 18, 2024
1 parent 2dc9508 commit 0bef442
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
6 changes: 2 additions & 4 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6821,10 +6821,8 @@ impl Bank {

pub fn is_in_slot_hashes_history(&self, slot: &Slot) -> bool {
if slot < &self.slot {
if let Ok(sysvar_cache) = self.transaction_processor.sysvar_cache.read() {
if let Ok(slot_hashes) = sysvar_cache.get_slot_hashes() {
return slot_hashes.get(slot).is_some();
}
if let Ok(slot_hashes) = self.transaction_processor.sysvar_cache().get_slot_hashes() {
return slot_hashes.get(slot).is_some();
}
}
false
Expand Down
4 changes: 1 addition & 3 deletions runtime/src/bank/address_lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ impl AddressLoader for &Bank {
) -> Result<LoadedAddresses, AddressLoaderError> {
let slot_hashes = self
.transaction_processor
.sysvar_cache
.read()
.unwrap()
.sysvar_cache()
.get_slot_hashes()
.map_err(|_| AddressLoaderError::SlotHashesSysvarNotFound)?;

Expand Down
12 changes: 6 additions & 6 deletions runtime/src/bank/sysvar_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod tests {
let (genesis_config, _mint_keypair) = create_genesis_config(100_000);
let bank0 = Arc::new(Bank::new_for_tests(&genesis_config));

let bank0_sysvar_cache = bank0.transaction_processor.sysvar_cache.read().unwrap();
let bank0_sysvar_cache = bank0.transaction_processor.sysvar_cache();
let bank0_cached_clock = bank0_sysvar_cache.get_clock();
let bank0_cached_epoch_schedule = bank0_sysvar_cache.get_epoch_schedule();
let bank0_cached_fees = bank0_sysvar_cache.get_fees();
Expand All @@ -38,7 +38,7 @@ mod tests {
bank1_slot,
));

let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache.read().unwrap();
let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache();
let bank1_cached_clock = bank1_sysvar_cache.get_clock();
let bank1_cached_epoch_schedule = bank1_sysvar_cache.get_epoch_schedule();
let bank1_cached_fees = bank1_sysvar_cache.get_fees();
Expand All @@ -59,7 +59,7 @@ mod tests {
let bank2_slot = bank1.slot() + 1;
let bank2 = Bank::new_from_parent(bank1.clone(), &Pubkey::default(), bank2_slot);

let bank2_sysvar_cache = bank2.transaction_processor.sysvar_cache.read().unwrap();
let bank2_sysvar_cache = bank2.transaction_processor.sysvar_cache();
let bank2_cached_clock = bank2_sysvar_cache.get_clock();
let bank2_cached_epoch_schedule = bank2_sysvar_cache.get_epoch_schedule();
let bank2_cached_fees = bank2_sysvar_cache.get_fees();
Expand Down Expand Up @@ -90,7 +90,7 @@ mod tests {
let bank1_slot = bank0.slot() + 1;
let mut bank1 = Bank::new_from_parent(bank0, &Pubkey::default(), bank1_slot);

let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache.read().unwrap();
let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache();
let bank1_cached_clock = bank1_sysvar_cache.get_clock();
let bank1_cached_epoch_schedule = bank1_sysvar_cache.get_epoch_schedule();
let bank1_cached_fees = bank1_sysvar_cache.get_fees();
Expand All @@ -108,7 +108,7 @@ mod tests {
drop(bank1_sysvar_cache);
bank1.transaction_processor.reset_sysvar_cache();

let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache.read().unwrap();
let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache();
assert!(bank1_sysvar_cache.get_clock().is_err());
assert!(bank1_sysvar_cache.get_epoch_schedule().is_err());
assert!(bank1_sysvar_cache.get_fees().is_err());
Expand Down Expand Up @@ -143,7 +143,7 @@ mod tests {
.transaction_processor
.fill_missing_sysvar_cache_entries(&bank1);

let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache.read().unwrap();
let bank1_sysvar_cache = bank1.transaction_processor.sysvar_cache();
assert_eq!(bank1_sysvar_cache.get_clock(), bank1_cached_clock);
assert_eq!(
bank1_sysvar_cache.get_epoch_schedule(),
Expand Down
7 changes: 6 additions & 1 deletion svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use {
collections::{hash_map::Entry, HashMap, HashSet},
fmt::{Debug, Formatter},
rc::Rc,
sync::RwLockReadGuard,
},
};

Expand Down Expand Up @@ -147,7 +148,7 @@ pub struct TransactionBatchProcessor<FG: ForkGraph> {
/// SysvarCache is a collection of system variables that are
/// accessible from on chain programs. It is passed to SVM from
/// client code (e.g. Bank) and forwarded to the MessageProcessor.
pub sysvar_cache: RwLock<SysvarCache>,
sysvar_cache: RwLock<SysvarCache>,

/// Programs required for transaction batch processing
pub program_cache: Arc<RwLock<ProgramCache<FG>>>,
Expand Down Expand Up @@ -213,6 +214,10 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
.map(|cache| cache.get_environments_for_epoch(epoch))
}

pub fn sysvar_cache(&self) -> RwLockReadGuard<SysvarCache> {
self.sysvar_cache.read().unwrap()
}

/// Main entrypoint to the SVM.
pub fn load_and_execute_sanitized_transactions<CB: TransactionProcessingCallback>(
&self,
Expand Down
8 changes: 3 additions & 5 deletions svm/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ fn run_fixture(fixture: InstrFixture, filename: OsString, execute_as_instr: bool

#[allow(deprecated)]
let (blockhash, lamports_per_signature) = batch_processor
.sysvar_cache
.read()
.unwrap()
.sysvar_cache()
.get_recent_blockhashes()
.ok()
.and_then(|x| (*x).last().cloned())
Expand Down Expand Up @@ -400,7 +398,7 @@ fn execute_fixture_as_instr(
filename: OsString,
cu_avail: u64,
) {
let rent = if let Ok(rent) = batch_processor.sysvar_cache.read().unwrap().get_rent() {
let rent = if let Ok(rent) = batch_processor.sysvar_cache().get_rent() {
(*rent).clone()
} else {
Rent::default()
Expand Down Expand Up @@ -455,7 +453,7 @@ fn execute_fixture_as_instr(

let log_collector = LogCollector::new_ref();

let sysvar_cache = &batch_processor.sysvar_cache.read().unwrap();
let sysvar_cache = &batch_processor.sysvar_cache();
let env_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand Down

0 comments on commit 0bef442

Please sign in to comment.