Skip to content

Commit be0350a

Browse files
authored
Update account helpers (#21)
1 parent 858a7cd commit be0350a

File tree

4 files changed

+88
-27
lines changed

4 files changed

+88
-27
lines changed

harness/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Default for Mollusk {
8383
solana_runtime::message_processor=debug,\
8484
solana_runtime::system_instruction_processor=trace",
8585
);
86-
let (program_id, program_account) = program::system_program();
86+
let (program_id, program_account) = program::keyed_account_for_system_program();
8787
Self {
8888
compute_budget: ComputeBudget::default(),
8989
feature_set: FeatureSet::all_enabled(),
@@ -105,7 +105,7 @@ impl Mollusk {
105105
pub fn new(program_id: &Pubkey, program_name: &'static str) -> Self {
106106
let mut mollusk = Self {
107107
program_id: *program_id,
108-
program_account: program::program_account(program_id),
108+
program_account: program::create_program_account_loader_v3(program_id),
109109
..Default::default()
110110
};
111111
mollusk.add_program(program_id, program_name);

harness/src/program.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ static BUILTINS: &[Builtin] = &[
115115
/* ... */
116116
];
117117

118-
fn builtin_program_account(program_id: &Pubkey, name: &str) -> (Pubkey, AccountSharedData) {
118+
/// Create a key and account for a builtin program.
119+
pub fn create_keyed_account_for_builtin_program(
120+
program_id: &Pubkey,
121+
name: &str,
122+
) -> (Pubkey, AccountSharedData) {
119123
let data = name.as_bytes().to_vec();
120124
let lamports = Rent::default().minimum_balance(data.len());
121125
let account = AccountSharedData::from(Account {
@@ -129,19 +133,24 @@ fn builtin_program_account(program_id: &Pubkey, name: &str) -> (Pubkey, AccountS
129133
}
130134

131135
/// Get the key and account for the system program.
132-
pub fn system_program() -> (Pubkey, AccountSharedData) {
133-
builtin_program_account(&BUILTINS[0].program_id, BUILTINS[0].name)
136+
pub fn keyed_account_for_system_program() -> (Pubkey, AccountSharedData) {
137+
create_keyed_account_for_builtin_program(&BUILTINS[0].program_id, BUILTINS[0].name)
134138
}
135139

136-
/// Get the key and account for the BPF Loader Upgradeable program.
137-
pub fn bpf_loader_upgradeable_program() -> (Pubkey, AccountSharedData) {
138-
builtin_program_account(&BUILTINS[1].program_id, BUILTINS[1].name)
140+
/// Get the key and account for the BPF Loader v2 program.
141+
pub fn keyed_account_for_bpf_loader_v2_program() -> (Pubkey, AccountSharedData) {
142+
create_keyed_account_for_builtin_program(&BUILTINS[1].program_id, BUILTINS[1].name)
143+
}
144+
145+
/// Get the key and account for the BPF Loader v3 (Upgradeable) program.
146+
pub fn keyed_account_for_bpf_loader_v3_program() -> (Pubkey, AccountSharedData) {
147+
create_keyed_account_for_builtin_program(&BUILTINS[1].program_id, BUILTINS[1].name)
139148
}
140149

141150
/* ... */
142151

143152
/// Create a BPF Loader 2 program account.
144-
pub fn program_account_loader_2(elf: &[u8]) -> AccountSharedData {
153+
pub fn create_program_account_loader_v2(elf: &[u8]) -> AccountSharedData {
145154
let lamports = Rent::default().minimum_balance(elf.len());
146155
AccountSharedData::from(Account {
147156
lamports,
@@ -152,8 +161,8 @@ pub fn program_account_loader_2(elf: &[u8]) -> AccountSharedData {
152161
})
153162
}
154163

155-
/// Create a BPF Loader Upgradeable program account.
156-
pub fn program_account(program_id: &Pubkey) -> AccountSharedData {
164+
/// Create a BPF Loader v3 (Upgradeable) program account.
165+
pub fn create_program_account_loader_v3(program_id: &Pubkey) -> AccountSharedData {
157166
let programdata_address =
158167
Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::id()).0;
159168
let data = bincode::serialize(&UpgradeableLoaderState::Program {
@@ -170,8 +179,8 @@ pub fn program_account(program_id: &Pubkey) -> AccountSharedData {
170179
})
171180
}
172181

173-
/// Create a BPF Loader Upgradeable program data account.
174-
pub fn program_data_account(elf: &[u8]) -> AccountSharedData {
182+
/// Create a BPF Loader v3 (Upgradeable) program data account.
183+
pub fn create_program_data_account_loader_v3(elf: &[u8]) -> AccountSharedData {
175184
let data = {
176185
let elf_offset = UpgradeableLoaderState::size_of_programdata_metadata();
177186
let data_len = elf_offset + elf.len();
@@ -197,10 +206,16 @@ pub fn program_data_account(elf: &[u8]) -> AccountSharedData {
197206
})
198207
}
199208

200-
/// Create a BPF Loader Upgradeable program and program data account.
209+
/// Create a BPF Loader v3 (Upgradeable) program and program data account.
201210
///
202211
/// Returns a tuple, where the first element is the program account and the
203212
/// second element is the program data account.
204-
pub fn program_accounts(program_id: &Pubkey, elf: &[u8]) -> (AccountSharedData, AccountSharedData) {
205-
(program_account(program_id), program_data_account(elf))
213+
pub fn create_program_account_pair_loader_v3(
214+
program_id: &Pubkey,
215+
elf: &[u8],
216+
) -> (AccountSharedData, AccountSharedData) {
217+
(
218+
create_program_account_loader_v3(program_id),
219+
create_program_data_account_loader_v3(elf),
220+
)
206221
}

harness/src/sysvar.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
use {
44
solana_program_runtime::sysvar_cache::SysvarCache,
55
solana_sdk::{
6+
account::AccountSharedData,
67
clock::{Clock, Slot},
78
epoch_rewards::EpochRewards,
89
epoch_schedule::EpochSchedule,
910
hash::Hash,
11+
pubkey::Pubkey,
1012
rent::Rent,
1113
slot_hashes::SlotHashes,
1214
stake_history::StakeHistory,
13-
sysvar::{last_restart_slot::LastRestartSlot, SysvarId},
15+
sysvar::{self, last_restart_slot::LastRestartSlot, Sysvar, SysvarId},
1416
},
1517
};
1618

@@ -29,6 +31,50 @@ pub struct Sysvars {
2931
}
3032

3133
impl Sysvars {
34+
fn sysvar_account<T: SysvarId + Sysvar>(&self, sysvar: &T) -> (Pubkey, AccountSharedData) {
35+
let data = bincode::serialize::<T>(sysvar).unwrap();
36+
let space = data.len();
37+
let lamports = self.rent.minimum_balance(space);
38+
let mut account = AccountSharedData::new(lamports, space, &sysvar::id());
39+
account.set_data_from_slice(&data);
40+
(T::id(), account)
41+
}
42+
43+
/// Get the key and account for the clock sysvar.
44+
pub fn keyed_account_for_clock_sysvar(&self) -> (Pubkey, AccountSharedData) {
45+
self.sysvar_account(&self.clock)
46+
}
47+
48+
/// Get the key and account for the epoch rewards sysvar.
49+
pub fn keyed_account_for_epoch_rewards_sysvar(&self) -> (Pubkey, AccountSharedData) {
50+
self.sysvar_account(&self.epoch_rewards)
51+
}
52+
53+
/// Get the key and account for the epoch schedule sysvar.
54+
pub fn keyed_account_for_epoch_schedule_sysvar(&self) -> (Pubkey, AccountSharedData) {
55+
self.sysvar_account(&self.epoch_schedule)
56+
}
57+
58+
/// Get the key and account for the last restart slot sysvar.
59+
pub fn keyed_account_for_last_restart_slot_sysvar(&self) -> (Pubkey, AccountSharedData) {
60+
self.sysvar_account(&self.last_restart_slot)
61+
}
62+
63+
/// Get the key and account for the rent sysvar.
64+
pub fn keyed_account_for_rent_sysvar(&self) -> (Pubkey, AccountSharedData) {
65+
self.sysvar_account(&self.rent)
66+
}
67+
68+
/// Get the key and account for the slot hashes sysvar.
69+
pub fn keyed_account_for_slot_hashes_sysvar(&self) -> (Pubkey, AccountSharedData) {
70+
self.sysvar_account(&self.slot_hashes)
71+
}
72+
73+
/// Get the key and account for the stake history sysvar.
74+
pub fn keyed_account_for_stake_history_sysvar(&self) -> (Pubkey, AccountSharedData) {
75+
self.sysvar_account(&self.stake_history)
76+
}
77+
3278
/// Warp the test environment to a slot by updating sysvars.
3379
pub fn warp_to_slot(&mut self, slot: Slot) {
3480
// First update `Clock`.

harness/tests/bpf_program.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use {
22
mollusk_svm::{
3-
program::{program_account, system_program},
3+
program::{create_program_account_loader_v3, keyed_account_for_system_program},
44
result::Check,
55
Mollusk,
66
},
@@ -128,7 +128,7 @@ fn test_transfer() {
128128
&[
129129
(payer, payer_account.clone()),
130130
(recipient, recipient_account.clone()),
131-
system_program(),
131+
keyed_account_for_system_program(),
132132
],
133133
&[
134134
Check::err(ProgramError::MissingRequiredSignature),
@@ -144,7 +144,7 @@ fn test_transfer() {
144144
&[
145145
(payer, AccountSharedData::default()),
146146
(recipient, recipient_account.clone()),
147-
system_program(),
147+
keyed_account_for_system_program(),
148148
],
149149
&[
150150
Check::err(ProgramError::Custom(
@@ -161,7 +161,7 @@ fn test_transfer() {
161161
&[
162162
(payer, payer_account.clone()),
163163
(recipient, recipient_account.clone()),
164-
system_program(),
164+
keyed_account_for_system_program(),
165165
],
166166
&[
167167
Check::success(),
@@ -207,7 +207,7 @@ fn test_close_account() {
207207
&[
208208
(key, account.clone()),
209209
(incinerator::id(), AccountSharedData::default()),
210-
system_program(),
210+
keyed_account_for_system_program(),
211211
],
212212
&[
213213
Check::err(ProgramError::MissingRequiredSignature),
@@ -222,7 +222,7 @@ fn test_close_account() {
222222
&[
223223
(key, account.clone()),
224224
(incinerator::id(), AccountSharedData::default()),
225-
system_program(),
225+
keyed_account_for_system_program(),
226226
],
227227
&[
228228
Check::success(),
@@ -287,7 +287,7 @@ fn test_cpi() {
287287
(key, account.clone()),
288288
(
289289
cpi_target_program_id,
290-
program_account(&cpi_target_program_id),
290+
create_program_account_loader_v3(&cpi_target_program_id),
291291
),
292292
],
293293
&[
@@ -312,7 +312,7 @@ fn test_cpi() {
312312
(key, account.clone()),
313313
(
314314
cpi_target_program_id,
315-
program_account(&cpi_target_program_id),
315+
create_program_account_loader_v3(&cpi_target_program_id),
316316
),
317317
],
318318
&[
@@ -336,7 +336,7 @@ fn test_cpi() {
336336
(key, account.clone()),
337337
(
338338
cpi_target_program_id,
339-
program_account(&cpi_target_program_id),
339+
create_program_account_loader_v3(&cpi_target_program_id),
340340
),
341341
],
342342
&[
@@ -353,7 +353,7 @@ fn test_cpi() {
353353
(key, account.clone()),
354354
(
355355
cpi_target_program_id,
356-
program_account(&cpi_target_program_id),
356+
create_program_account_loader_v3(&cpi_target_program_id),
357357
),
358358
],
359359
&[

0 commit comments

Comments
 (0)