Skip to content

Commit 5bbc7bc

Browse files
authored
Add more program helpers (#23)
1 parent 083cfe7 commit 5bbc7bc

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

harness/src/lib.rs

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ use {
5858
std::sync::Arc,
5959
};
6060

61+
const DEFAULT_LOADER_KEY: Pubkey = bpf_loader_upgradeable::id();
62+
6163
const PROGRAM_ACCOUNTS_LEN: usize = 1;
6264
const PROGRAM_INDICES: &[u16] = &[0];
6365

@@ -102,7 +104,7 @@ impl Mollusk {
102104
/// Attempts the load the program's ELF file from the default search paths.
103105
/// Once loaded, adds the program to the program cache and updates the
104106
/// Mollusk instance with the program's ID and account.
105-
pub fn new(program_id: &Pubkey, program_name: &'static str) -> Self {
107+
pub fn new(program_id: &Pubkey, program_name: &str) -> Self {
106108
let mut mollusk = Self {
107109
program_id: *program_id,
108110
program_account: program::create_program_account_loader_v3(program_id),
@@ -115,21 +117,41 @@ impl Mollusk {
115117
/// Add a program to the test environment.
116118
///
117119
/// If you intend to CPI to a program, this is likely what you want to use.
118-
pub fn add_program(&mut self, program_id: &Pubkey, program_name: &'static str) {
120+
pub fn add_program(&mut self, program_id: &Pubkey, program_name: &str) {
119121
let elf = file::load_program_elf(program_name);
120-
self.program_cache.add_program(
121-
program_id,
122-
&bpf_loader_upgradeable::id(),
123-
&elf,
124-
&self.compute_budget,
125-
&self.feature_set,
126-
);
122+
self.add_program_with_elf(program_id, &elf);
123+
}
124+
125+
/// Add a program to the test environment under a specific loader.
126+
///
127+
/// If you intend to CPI to a program, this is likely what you want to use.
128+
pub fn add_program_with_loader(
129+
&mut self,
130+
program_id: &Pubkey,
131+
program_name: &str,
132+
loader_key: &Pubkey,
133+
) {
134+
let elf = file::load_program_elf(program_name);
135+
self.add_program_with_elf_and_loader(program_id, &elf, loader_key);
127136
}
128137

129138
/// Add a program to the test environment using a provided ELF.
130139
///
131140
/// If you intend to CPI to a program, this is likely what you want to use.
132-
pub fn add_program_with_elf(&mut self, program_id: &Pubkey, loader_key: &Pubkey, elf: &[u8]) {
141+
pub fn add_program_with_elf(&mut self, program_id: &Pubkey, elf: &[u8]) {
142+
self.add_program_with_elf_and_loader(program_id, elf, &DEFAULT_LOADER_KEY);
143+
}
144+
145+
/// Add a program to the test environment using a provided ELF under a
146+
/// specific loader.
147+
///
148+
/// If you intend to CPI to a program, this is likely what you want to use.
149+
pub fn add_program_with_elf_and_loader(
150+
&mut self,
151+
program_id: &Pubkey,
152+
elf: &[u8],
153+
loader_key: &Pubkey,
154+
) {
133155
self.program_cache.add_program(
134156
program_id,
135157
loader_key,
@@ -139,6 +161,32 @@ impl Mollusk {
139161
);
140162
}
141163

164+
/// Switch the target program to a different program.
165+
///
166+
/// Note: The program must already be contained in the program cache.
167+
pub fn switch_target_program(&mut self, program_id: &Pubkey) {
168+
let loader_key: Pubkey = self
169+
.program_cache
170+
.cache()
171+
.read()
172+
.unwrap()
173+
.find(program_id)
174+
.expect("Program not found in cache")
175+
.account_owner
176+
.into();
177+
if loader_key != DEFAULT_LOADER_KEY {
178+
panic!("Loader not supported for target program: {:?}", loader_key);
179+
}
180+
self.program_id = *program_id;
181+
self.program_account = program::create_program_account_loader_v3(program_id);
182+
}
183+
184+
/// Add a program to the cache and make it the target program.
185+
pub fn add_and_switch_target_program(&mut self, program_id: &Pubkey, program_name: &str) {
186+
self.add_program(program_id, program_name);
187+
self.switch_target_program(program_id);
188+
}
189+
142190
/// Warp the test environment to a slot by updating sysvars.
143191
pub fn warp_to_slot(&mut self, slot: u64) {
144192
self.sysvars.warp_to_slot(slot)

0 commit comments

Comments
 (0)