Skip to content

Commit 2b973db

Browse files
authored
feat: remove program targeting (#30)
1 parent dfad1af commit 2b973db

File tree

3 files changed

+26
-70
lines changed

3 files changed

+26
-70
lines changed

harness/src/lib.rs

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ pub struct Mollusk {
6464
pub compute_budget: ComputeBudget,
6565
pub feature_set: FeatureSet,
6666
pub fee_structure: FeeStructure,
67-
pub program_account: AccountSharedData,
6867
pub program_cache: ProgramCache,
69-
pub program_id: Pubkey,
7068
pub sysvars: Sysvars,
7169
}
7270

@@ -78,63 +76,36 @@ impl Default for Mollusk {
7876
solana_runtime::message_processor=debug,\
7977
solana_runtime::system_instruction_processor=trace",
8078
);
81-
let (program_id, program_account) = program::keyed_account_for_system_program();
8279
Self {
8380
compute_budget: ComputeBudget::default(),
8481
feature_set: FeatureSet::all_enabled(),
8582
fee_structure: FeeStructure::default(),
86-
program_account,
8783
program_cache: ProgramCache::default(),
88-
program_id,
8984
sysvars: Sysvars::default(),
9085
}
9186
}
9287
}
9388

9489
impl Mollusk {
95-
/// Create a new Mollusk instance for the provided program.
90+
/// Create a new Mollusk instance containing the provided program.
9691
///
9792
/// Attempts the load the program's ELF file from the default search paths.
98-
/// Once loaded, adds the program to the program cache and updates the
99-
/// Mollusk instance with the program's ID and account.
93+
/// Once loaded, adds the program to the program cache and returns the
94+
/// newly created Mollusk instance.
10095
pub fn new(program_id: &Pubkey, program_name: &str) -> Self {
101-
let mut mollusk = Self {
102-
program_id: *program_id,
103-
program_account: program::create_program_account_loader_v3(program_id),
104-
..Default::default()
105-
};
106-
mollusk.add_program(program_id, program_name);
96+
let mut mollusk = Self::default();
97+
mollusk.add_program(program_id, program_name, &DEFAULT_LOADER_KEY);
10798
mollusk
10899
}
109100

110101
/// Add a program to the test environment.
111102
///
112103
/// If you intend to CPI to a program, this is likely what you want to use.
113-
pub fn add_program(&mut self, program_id: &Pubkey, program_name: &str) {
114-
let elf = file::load_program_elf(program_name);
115-
self.add_program_with_elf(program_id, &elf);
116-
}
117-
118-
/// Add a program to the test environment under a specific loader.
119-
///
120-
/// If you intend to CPI to a program, this is likely what you want to use.
121-
pub fn add_program_with_loader(
122-
&mut self,
123-
program_id: &Pubkey,
124-
program_name: &str,
125-
loader_key: &Pubkey,
126-
) {
104+
pub fn add_program(&mut self, program_id: &Pubkey, program_name: &str, loader_key: &Pubkey) {
127105
let elf = file::load_program_elf(program_name);
128106
self.add_program_with_elf_and_loader(program_id, &elf, loader_key);
129107
}
130108

131-
/// Add a program to the test environment using a provided ELF.
132-
///
133-
/// If you intend to CPI to a program, this is likely what you want to use.
134-
pub fn add_program_with_elf(&mut self, program_id: &Pubkey, elf: &[u8]) {
135-
self.add_program_with_elf_and_loader(program_id, elf, &DEFAULT_LOADER_KEY);
136-
}
137-
138109
/// Add a program to the test environment using a provided ELF under a
139110
/// specific loader.
140111
///
@@ -154,32 +125,6 @@ impl Mollusk {
154125
);
155126
}
156127

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

harness/src/program.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use {
99
},
1010
solana_sdk::{
1111
account::{Account, AccountSharedData},
12-
bpf_loader,
13-
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
12+
bpf_loader_upgradeable::UpgradeableLoaderState,
1413
feature_set::FeatureSet,
1514
native_loader,
1615
pubkey::Pubkey,
@@ -19,6 +18,14 @@ use {
1918
std::sync::{Arc, RwLock},
2019
};
2120

21+
/// Loader keys, re-exported from `solana_sdk` for convenience.
22+
pub mod loader_keys {
23+
pub use solana_sdk::{
24+
bpf_loader::ID as LOADER_V2, bpf_loader_upgradeable::ID as LOADER_V3,
25+
loader_v4::ID as LOADER_V4, native_loader::ID as NATIVE_LOADER,
26+
};
27+
}
28+
2229
pub struct ProgramCache {
2330
cache: RwLock<ProgramCacheForTxBatch>,
2431
}
@@ -108,12 +115,12 @@ static BUILTINS: &[Builtin] = &[
108115
entrypoint: solana_system_program::system_processor::Entrypoint::vm,
109116
},
110117
Builtin {
111-
program_id: bpf_loader::id(),
118+
program_id: loader_keys::LOADER_V2,
112119
name: "solana_bpf_loader_program",
113120
entrypoint: solana_bpf_loader_program::Entrypoint::vm,
114121
},
115122
Builtin {
116-
program_id: bpf_loader_upgradeable::id(),
123+
program_id: loader_keys::LOADER_V3,
117124
name: "solana_bpf_loader_upgradeable_program",
118125
entrypoint: solana_bpf_loader_program::Entrypoint::vm,
119126
},
@@ -160,7 +167,7 @@ pub fn create_program_account_loader_v2(elf: &[u8]) -> AccountSharedData {
160167
AccountSharedData::from(Account {
161168
lamports,
162169
data: elf.to_vec(),
163-
owner: bpf_loader::id(),
170+
owner: loader_keys::LOADER_V2,
164171
executable: true,
165172
rent_epoch: 0,
166173
})
@@ -169,7 +176,7 @@ pub fn create_program_account_loader_v2(elf: &[u8]) -> AccountSharedData {
169176
/// Create a BPF Loader v3 (Upgradeable) program account.
170177
pub fn create_program_account_loader_v3(program_id: &Pubkey) -> AccountSharedData {
171178
let programdata_address =
172-
Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::id()).0;
179+
Pubkey::find_program_address(&[program_id.as_ref()], &loader_keys::LOADER_V3).0;
173180
let data = bincode::serialize(&UpgradeableLoaderState::Program {
174181
programdata_address,
175182
})
@@ -178,7 +185,7 @@ pub fn create_program_account_loader_v3(program_id: &Pubkey) -> AccountSharedDat
178185
AccountSharedData::from(Account {
179186
lamports,
180187
data,
181-
owner: bpf_loader_upgradeable::id(),
188+
owner: loader_keys::LOADER_V3,
182189
executable: true,
183190
rent_epoch: 0,
184191
})
@@ -205,7 +212,7 @@ pub fn create_program_data_account_loader_v3(elf: &[u8]) -> AccountSharedData {
205212
AccountSharedData::from(Account {
206213
lamports,
207214
data,
208-
owner: bpf_loader_upgradeable::id(),
215+
owner: loader_keys::LOADER_V3,
209216
executable: false,
210217
rent_epoch: 0,
211218
})

harness/tests/bpf_program.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,11 @@ fn test_cpi() {
273273
);
274274
}
275275

276-
mollusk.add_program(&cpi_target_program_id, "test_program_cpi_target");
276+
mollusk.add_program(
277+
&cpi_target_program_id,
278+
"test_program_cpi_target",
279+
&mollusk_svm::program::loader_keys::LOADER_V3,
280+
);
277281

278282
// Fail account not signer.
279283
{

0 commit comments

Comments
 (0)