Skip to content

Commit db3c98f

Browse files
authored
fix: context: hydrate programs & sysvars every time (#153)
1 parent 405e968 commit db3c98f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

harness/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ impl Mollusk {
11551155
MolluskContext {
11561156
mollusk: self,
11571157
account_store: Rc::new(RefCell::new(account_store)),
1158+
hydrate_store: true, // <-- Default
11581159
}
11591160
}
11601161
}
@@ -1179,22 +1180,42 @@ impl Mollusk {
11791180
pub struct MolluskContext<AS: AccountStore> {
11801181
pub mollusk: Mollusk,
11811182
pub account_store: Rc<RefCell<AS>>,
1183+
pub hydrate_store: bool,
11821184
}
11831185

11841186
impl<AS: AccountStore> MolluskContext<AS> {
11851187
fn load_accounts_for_instructions<'a>(
11861188
&self,
11871189
instructions: impl Iterator<Item = &'a Instruction>,
11881190
) -> Vec<(Pubkey, Account)> {
1189-
let mut seen = HashSet::new();
11901191
let mut accounts = Vec::new();
1192+
1193+
// If hydration is enabled, add sysvars and program accounts regardless
1194+
// of whether or not they exist already.
1195+
if self.hydrate_store {
1196+
self.mollusk
1197+
.program_cache
1198+
.get_all_keyed_program_accounts()
1199+
.into_iter()
1200+
.chain(self.mollusk.sysvars.get_all_keyed_sysvar_accounts())
1201+
.for_each(|(pubkey, account)| {
1202+
accounts.push((pubkey, account));
1203+
});
1204+
}
1205+
1206+
// Regardless of hydration, only add an account if the caller hasn't
1207+
// already loaded it into the store.
1208+
let mut seen = HashSet::new();
11911209
let store = self.account_store.borrow();
11921210
instructions.for_each(|instruction| {
11931211
instruction
11941212
.accounts
11951213
.iter()
11961214
.for_each(|AccountMeta { pubkey, .. }| {
11971215
if seen.insert(*pubkey) {
1216+
// First try to load theirs, then see if it's a sysvar,
1217+
// then see if it's a cached program, then apply the
1218+
// default.
11981219
let account = store.get_account(pubkey).unwrap_or_else(|| {
11991220
self.mollusk
12001221
.sysvars

harness/src/sysvar.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ impl Sysvars {
127127
self.sysvar_account(&self.stake_history)
128128
}
129129

130+
pub(crate) fn get_all_keyed_sysvar_accounts(&self) -> Vec<(Pubkey, Account)> {
131+
vec![
132+
self.keyed_account_for_clock_sysvar(),
133+
self.keyed_account_for_epoch_rewards_sysvar(),
134+
self.keyed_account_for_epoch_schedule_sysvar(),
135+
self.keyed_account_for_last_restart_slot_sysvar(),
136+
self.keyed_account_for_rent_sysvar(),
137+
self.keyed_account_for_slot_hashes_sysvar(),
138+
self.keyed_account_for_stake_history_sysvar(),
139+
]
140+
}
141+
130142
/// Warp the test environment to a slot by updating sysvars.
131143
pub fn warp_to_slot(&mut self, slot: Slot) {
132144
let slot_delta = slot.saturating_sub(self.clock.slot);

0 commit comments

Comments
 (0)