Skip to content

Commit 9658391

Browse files
authored
harness: rework sysvar caching (#64) (#67)
1 parent a7d8998 commit 9658391

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

harness/src/fuzz/firedancer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use {
88
crate::{
99
accounts::{compile_accounts, CompiledAccounts},
1010
result::{Check, InstructionResult},
11-
sysvar::Sysvars,
1211
Mollusk, DEFAULT_LOADER_KEY,
1312
},
1413
mollusk_svm_fuzz_fixture_firedancer::{
@@ -136,7 +135,6 @@ fn parse_fixture_context(
136135
let mollusk = Mollusk {
137136
compute_budget,
138137
feature_set: epoch_context.feature_set.clone(),
139-
sysvars: Sysvars::fill_from_accounts(&accounts),
140138
..Default::default()
141139
};
142140

harness/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use {
4646
solana_compute_budget::compute_budget::ComputeBudget,
4747
solana_program_runtime::{
4848
invoke_context::{EnvironmentConfig, InvokeContext},
49-
sysvar_cache::SysvarCache,
5049
timings::ExecuteTimings,
5150
},
5251
solana_sdk::{
@@ -163,17 +162,18 @@ impl Mollusk {
163162
);
164163

165164
let invoke_result = {
166-
let mut cache = self.program_cache.cache().write().unwrap();
165+
let mut program_cache = self.program_cache.cache().write().unwrap();
166+
let sysvar_cache = self.sysvars.setup_sysvar_cache(accounts);
167167
InvokeContext::new(
168168
&mut transaction_context,
169-
&mut cache,
169+
&mut program_cache,
170170
EnvironmentConfig::new(
171171
Hash::default(),
172172
None,
173173
None,
174174
Arc::new(self.feature_set.clone()),
175175
self.fee_structure.lamports_per_signature,
176-
&SysvarCache::from(&self.sysvars),
176+
&sysvar_cache,
177177
),
178178
None,
179179
self.compute_budget,

harness/src/sysvar.rs

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,6 @@ impl Default for Sysvars {
5959
}
6060

6161
impl Sysvars {
62-
/// Create a `Sysvars` instance from a list of accounts. Any missing
63-
/// sysvars will be filled with default values.
64-
pub fn fill_from_accounts(accounts: &[(Pubkey, AccountSharedData)]) -> Self {
65-
let mut me = Self::default();
66-
for (key, account) in accounts {
67-
if key == &Clock::id() {
68-
me.clock = bincode::deserialize(account.data()).unwrap();
69-
}
70-
if key == &EpochRewards::id() {
71-
me.epoch_rewards = bincode::deserialize(account.data()).unwrap();
72-
}
73-
if key == &EpochSchedule::id() {
74-
me.epoch_schedule = bincode::deserialize(account.data()).unwrap();
75-
}
76-
if key == &LastRestartSlot::id() {
77-
me.last_restart_slot = bincode::deserialize(account.data()).unwrap();
78-
}
79-
if key == &Rent::id() {
80-
me.rent = bincode::deserialize(account.data()).unwrap();
81-
}
82-
if key == &SlotHashes::id() {
83-
me.slot_hashes = bincode::deserialize(account.data()).unwrap();
84-
}
85-
if key == &StakeHistory::id() {
86-
me.stake_history = bincode::deserialize(account.data()).unwrap();
87-
}
88-
}
89-
me
90-
}
91-
9262
fn sysvar_account<T: SysvarId + Sysvar>(&self, sysvar: &T) -> (Pubkey, AccountSharedData) {
9363
let data = bincode::serialize::<T>(sysvar).unwrap();
9464
let space = data.len();
@@ -172,6 +142,47 @@ impl Sysvars {
172142
}
173143
}
174144
}
145+
146+
pub(crate) fn setup_sysvar_cache(
147+
&self,
148+
accounts: &[(Pubkey, AccountSharedData)],
149+
) -> SysvarCache {
150+
let mut sysvar_cache = SysvarCache::default();
151+
152+
// First fill any sysvar cache entries from the provided accounts.
153+
sysvar_cache.fill_missing_entries(|pubkey, set_sysvar| {
154+
if let Some((_, account)) = accounts.iter().find(|(key, _)| key == pubkey) {
155+
set_sysvar(account.data())
156+
}
157+
});
158+
159+
// Then fill the rest with the entries from `self`.
160+
sysvar_cache.fill_missing_entries(|pubkey, set_sysvar| {
161+
if pubkey.eq(&Clock::id()) {
162+
set_sysvar(&bincode::serialize(&self.clock).unwrap());
163+
}
164+
if pubkey.eq(&EpochRewards::id()) {
165+
set_sysvar(&bincode::serialize(&self.epoch_rewards).unwrap());
166+
}
167+
if pubkey.eq(&EpochSchedule::id()) {
168+
set_sysvar(&bincode::serialize(&self.epoch_schedule).unwrap());
169+
}
170+
if pubkey.eq(&LastRestartSlot::id()) {
171+
set_sysvar(&bincode::serialize(&self.last_restart_slot).unwrap());
172+
}
173+
if pubkey.eq(&Rent::id()) {
174+
set_sysvar(&bincode::serialize(&self.rent).unwrap());
175+
}
176+
if pubkey.eq(&SlotHashes::id()) {
177+
set_sysvar(&bincode::serialize(&self.slot_hashes).unwrap());
178+
}
179+
if pubkey.eq(&StakeHistory::id()) {
180+
set_sysvar(&bincode::serialize(&self.stake_history).unwrap());
181+
}
182+
});
183+
184+
sysvar_cache
185+
}
175186
}
176187

177188
impl From<&Sysvars> for SysvarCache {

0 commit comments

Comments
 (0)