Skip to content

Commit

Permalink
add set_sysvar_for_tests to SysvarCache (#1852)
Browse files Browse the repository at this point in the history
* make SysvarCache fields pub

* add set_sysvar_for_tests to SysvarCache

* make the SysvarCache fields private again

* shorter .expect messages
  • Loading branch information
kevinheavey authored Jul 17, 2024
1 parent ca0855a commit 9fd061e
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion program-runtime/src/sysvar_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
instruction::InstructionError,
pubkey::Pubkey,
sysvar::{
clock::Clock, epoch_rewards::EpochRewards, epoch_schedule::EpochSchedule,
self, clock::Clock, epoch_rewards::EpochRewards, epoch_schedule::EpochSchedule,
last_restart_slot::LastRestartSlot, rent::Rent, slot_hashes::SlotHashes,
stake_history::StakeHistory, Sysvar, SysvarId,
},
Expand Down Expand Up @@ -48,7 +48,60 @@ pub struct SysvarCache {
recent_blockhashes: Option<RecentBlockhashes>,
}

// declare_deprecated_sysvar_id doesn't support const.
// These sysvars are going away anyway.
const FEES_ID: Pubkey = solana_sdk::pubkey!("SysvarFees111111111111111111111111111111111");
const RECENT_BLOCKHASHES_ID: Pubkey =
solana_sdk::pubkey!("SysvarRecentB1ockHashes11111111111111111111");

impl SysvarCache {
/// Overwrite a sysvar. For testing purposes only.
#[allow(deprecated)]
pub fn set_sysvar_for_tests<T: Sysvar + SysvarId>(&mut self, sysvar: &T) {
let data = bincode::serialize(sysvar).expect("Failed to serialize sysvar.");
let sysvar_id = T::id();
match sysvar_id {
sysvar::clock::ID => {
self.clock = Some(data);
}
sysvar::epoch_rewards::ID => {
self.epoch_rewards = Some(data);
}
sysvar::epoch_schedule::ID => {
self.epoch_schedule = Some(data);
}
FEES_ID => {
let fees: Fees =
bincode::deserialize(&data).expect("Failed to deserialize Fees sysvar.");
self.fees = Some(fees);
}
sysvar::last_restart_slot::ID => {
self.last_restart_slot = Some(data);
}
RECENT_BLOCKHASHES_ID => {
let recent_blockhashes: RecentBlockhashes = bincode::deserialize(&data)
.expect("Failed to deserialize RecentBlockhashes sysvar.");
self.recent_blockhashes = Some(recent_blockhashes);
}
sysvar::rent::ID => {
self.rent = Some(data);
}
sysvar::slot_hashes::ID => {
let slot_hashes: SlotHashes =
bincode::deserialize(&data).expect("Failed to deserialize SlotHashes sysvar.");
self.slot_hashes = Some(data);
self.slot_hashes_obj = Some(Arc::new(slot_hashes));
}
sysvar::stake_history::ID => {
let stake_history: StakeHistory = bincode::deserialize(&data)
.expect("Failed to deserialize StakeHistory sysvar.");
self.stake_history = Some(data);
self.stake_history_obj = Some(Arc::new(stake_history));
}
_ => panic!("Unrecognized Sysvar ID: {sysvar_id}"),
}
}

// this is exposed for SyscallGetSysvar and should not otherwise be used
pub fn sysvar_id_to_buffer(&self, sysvar_id: &Pubkey) -> &Option<Vec<u8>> {
if Clock::check_id(sysvar_id) {
Expand Down

0 comments on commit 9fd061e

Please sign in to comment.