Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

harness: rework sysvar caching #64

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions harness/src/fuzz/firedancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use {
crate::{
accounts::{compile_accounts, CompiledAccounts},
result::{Check, InstructionResult},
sysvar::Sysvars,
Mollusk, DEFAULT_LOADER_KEY,
},
mollusk_svm_fuzz_fixture_firedancer::{
Expand Down Expand Up @@ -136,7 +135,6 @@ fn parse_fixture_context(
let mollusk = Mollusk {
compute_budget,
feature_set: epoch_context.feature_set.clone(),
sysvars: Sysvars::fill_from_accounts(&accounts),
..Default::default()
};

Expand Down
12 changes: 5 additions & 7 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ use {
accounts::CompiledAccounts,
mollusk_svm_error::error::{MolluskError, MolluskPanic},
solana_compute_budget::compute_budget::ComputeBudget,
solana_program_runtime::{
invoke_context::{EnvironmentConfig, InvokeContext},
sysvar_cache::SysvarCache,
},
solana_program_runtime::invoke_context::{EnvironmentConfig, InvokeContext},
solana_sdk::{
account::AccountSharedData, bpf_loader_upgradeable, feature_set::FeatureSet,
fee::FeeStructure, hash::Hash, instruction::Instruction, pubkey::Pubkey,
Expand Down Expand Up @@ -175,17 +172,18 @@ impl Mollusk {
);

let invoke_result = {
let mut cache = self.program_cache.cache().write().unwrap();
let mut program_cache = self.program_cache.cache().write().unwrap();
let sysvar_cache = self.sysvars.setup_sysvar_cache(accounts);
InvokeContext::new(
&mut transaction_context,
&mut cache,
&mut program_cache,
EnvironmentConfig::new(
Hash::default(),
None,
None,
Arc::new(self.feature_set.clone()),
self.fee_structure.lamports_per_signature,
&SysvarCache::from(&self.sysvars),
&sysvar_cache,
),
None,
self.compute_budget,
Expand Down
71 changes: 41 additions & 30 deletions harness/src/sysvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,6 @@ impl Default for Sysvars {
}

impl Sysvars {
/// Create a `Sysvars` instance from a list of accounts. Any missing
/// sysvars will be filled with default values.
pub fn fill_from_accounts(accounts: &[(Pubkey, AccountSharedData)]) -> Self {
let mut me = Self::default();
for (key, account) in accounts {
if key == &Clock::id() {
me.clock = bincode::deserialize(account.data()).unwrap();
}
if key == &EpochRewards::id() {
me.epoch_rewards = bincode::deserialize(account.data()).unwrap();
}
if key == &EpochSchedule::id() {
me.epoch_schedule = bincode::deserialize(account.data()).unwrap();
}
if key == &LastRestartSlot::id() {
me.last_restart_slot = bincode::deserialize(account.data()).unwrap();
}
if key == &Rent::id() {
me.rent = bincode::deserialize(account.data()).unwrap();
}
if key == &SlotHashes::id() {
me.slot_hashes = bincode::deserialize(account.data()).unwrap();
}
if key == &StakeHistory::id() {
me.stake_history = bincode::deserialize(account.data()).unwrap();
}
}
me
}

fn sysvar_account<T: SysvarId + Sysvar>(&self, sysvar: &T) -> (Pubkey, AccountSharedData) {
let data = bincode::serialize::<T>(sysvar).unwrap();
let space = data.len();
Expand Down Expand Up @@ -172,6 +142,47 @@ impl Sysvars {
}
}
}

pub(crate) fn setup_sysvar_cache(
&self,
accounts: &[(Pubkey, AccountSharedData)],
) -> SysvarCache {
let mut sysvar_cache = SysvarCache::default();

// First fill any sysvar cache entries from the provided accounts.
sysvar_cache.fill_missing_entries(|pubkey, set_sysvar| {
if let Some((_, account)) = accounts.iter().find(|(key, _)| key == pubkey) {
set_sysvar(account.data())
}
});

// Then fill the rest with the entries from `self`.
sysvar_cache.fill_missing_entries(|pubkey, set_sysvar| {
if pubkey.eq(&Clock::id()) {
set_sysvar(&bincode::serialize(&self.clock).unwrap());
}
if pubkey.eq(&EpochRewards::id()) {
set_sysvar(&bincode::serialize(&self.epoch_rewards).unwrap());
}
if pubkey.eq(&EpochSchedule::id()) {
set_sysvar(&bincode::serialize(&self.epoch_schedule).unwrap());
}
if pubkey.eq(&LastRestartSlot::id()) {
set_sysvar(&bincode::serialize(&self.last_restart_slot).unwrap());
}
if pubkey.eq(&Rent::id()) {
set_sysvar(&bincode::serialize(&self.rent).unwrap());
}
if pubkey.eq(&SlotHashes::id()) {
set_sysvar(&bincode::serialize(&self.slot_hashes).unwrap());
}
if pubkey.eq(&StakeHistory::id()) {
set_sysvar(&bincode::serialize(&self.stake_history).unwrap());
}
});

sysvar_cache
}
}

impl From<&Sysvars> for SysvarCache {
Expand Down
Loading