Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program loader helpers #18

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use {
},
solana_sdk::{
account::AccountSharedData,
bpf_loader_upgradeable,
feature_set::FeatureSet,
hash::Hash,
instruction::Instruction,
Expand Down Expand Up @@ -112,8 +113,26 @@ impl Mollusk {
/// If you intend to CPI to a program, this is likely what you want to use.
pub fn add_program(&mut self, program_id: &Pubkey, program_name: &'static str) {
let elf = file::load_program_elf(program_name);
self.program_cache
.add_program(program_id, &elf, &self.compute_budget, &self.feature_set);
self.program_cache.add_program(
program_id,
&bpf_loader_upgradeable::id(),
&elf,
&self.compute_budget,
&self.feature_set,
);
}

/// Add a program to the test environment using a provided ELF.
///
/// If you intend to CPI to a program, this is likely what you want to use.
pub fn add_program_with_elf(&mut self, program_id: &Pubkey, loader_key: &Pubkey, elf: &[u8]) {
self.program_cache.add_program(
program_id,
loader_key,
elf,
&self.compute_budget,
&self.feature_set,
);
}

/// Warp the test environment to a slot by updating sysvars.
Expand Down
16 changes: 15 additions & 1 deletion harness/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
},
solana_sdk::{
account::{Account, AccountSharedData},
bpf_loader,
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
feature_set::FeatureSet,
native_loader,
Expand Down Expand Up @@ -43,6 +44,7 @@ impl ProgramCache {
pub fn add_program(
&mut self,
program_id: &Pubkey,
loader_key: &Pubkey,
elf: &[u8],
compute_budget: &ComputeBudget,
feature_set: &FeatureSet,
Expand All @@ -55,7 +57,7 @@ impl ProgramCache {
*program_id,
Arc::new(
LoadedProgram::new(
&bpf_loader_upgradeable::id(),
loader_key,
environment,
0,
0,
Expand Down Expand Up @@ -125,6 +127,18 @@ pub fn bpf_loader_upgradeable_program() -> (Pubkey, AccountSharedData) {

/* ... */

/// Create a BPF Loader 2 program account.
pub fn program_account_loader_2(elf: &[u8]) -> AccountSharedData {
let lamports = Rent::default().minimum_balance(elf.len());
AccountSharedData::from(Account {
lamports,
data: elf.to_vec(),
owner: bpf_loader::id(),
executable: true,
rent_epoch: 0,
})
}

/// Create a BPF Loader Upgradeable program account.
pub fn program_account(program_id: &Pubkey) -> AccountSharedData {
let programdata_address =
Expand Down
Loading