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

Update program API #12

Merged
merged 1 commit into from
Jun 24, 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
8 changes: 4 additions & 4 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ use {
pubkey::Pubkey,
rent::Rent,
slot_hashes::SlotHashes,
system_program,
transaction_context::{InstructionAccount, TransactionContext},
},
std::sync::Arc,
Expand Down Expand Up @@ -78,12 +77,13 @@ impl Default for Mollusk {
solana_runtime::message_processor=debug,\
solana_runtime::system_instruction_processor=trace",
);
let (program_id, program_account) = program::system_program();
Self {
compute_budget: ComputeBudget::default(),
feature_set: FeatureSet::all_enabled(),
program_account: program::system_program_account(),
program_account,
program_cache: program::default_program_cache(),
program_id: system_program::id(),
program_id,
sysvar_cache: sysvar::default_sysvar_cache(),
}
}
Expand All @@ -100,7 +100,7 @@ impl Mollusk {

let mut mollusk = Self {
program_id: *program_id,
program_account: program::create_program_account(program_id),
program_account: program::program_account(program_id),
..Default::default()
};

Expand Down
51 changes: 23 additions & 28 deletions harness/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ struct Builtin {
}

impl Builtin {
fn program_account(&self) -> AccountSharedData {
let data = self.name.as_bytes().to_vec();
let lamports = Rent::default().minimum_balance(data.len());
AccountSharedData::from(Account {
lamports,
data,
owner: native_loader::id(),
executable: true,
rent_epoch: 0,
})
}

fn program_cache_entry(&self) -> Arc<LoadedProgram> {
Arc::new(LoadedProgram::new_builtin(
0,
Expand All @@ -60,20 +48,33 @@ static BUILTINS: &[Builtin] = &[
/* ... */
];

/// Get the account for the system program.
pub fn system_program_account() -> AccountSharedData {
BUILTINS[0].program_account()
fn builtin_program_account(program_id: &Pubkey, name: &str) -> (Pubkey, AccountSharedData) {
let data = name.as_bytes().to_vec();
let lamports = Rent::default().minimum_balance(data.len());
let account = AccountSharedData::from(Account {
lamports,
data,
owner: native_loader::id(),
executable: true,
rent_epoch: 0,
});
(*program_id, account)
}

/// Get the key and account for the system program.
pub fn system_program() -> (Pubkey, AccountSharedData) {
builtin_program_account(&BUILTINS[0].program_id, BUILTINS[0].name)
}

/// Get the account for the BPF Loader Upgradeable program.
pub fn bpf_loader_upgradeable_program_account() -> AccountSharedData {
BUILTINS[1].program_account()
/// Get the key and account for the BPF Loader Upgradeable program.
pub fn bpf_loader_upgradeable_program() -> (Pubkey, AccountSharedData) {
builtin_program_account(&BUILTINS[1].program_id, BUILTINS[1].name)
}

/* ... */

/// Create a BPF Loader Upgradeable program account.
pub fn create_program_account(program_id: &Pubkey) -> AccountSharedData {
pub fn program_account(program_id: &Pubkey) -> AccountSharedData {
let programdata_address =
Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::id()).0;
let data = bincode::serialize(&UpgradeableLoaderState::Program {
Expand All @@ -91,7 +92,7 @@ pub fn create_program_account(program_id: &Pubkey) -> AccountSharedData {
}

/// Create a BPF Loader Upgradeable program data account.
pub fn create_program_data_account(elf: &[u8]) -> AccountSharedData {
pub fn program_data_account(elf: &[u8]) -> AccountSharedData {
let data = {
let elf_offset = UpgradeableLoaderState::size_of_programdata_metadata();
let data_len = elf_offset + elf.len();
Expand Down Expand Up @@ -121,14 +122,8 @@ pub fn create_program_data_account(elf: &[u8]) -> AccountSharedData {
///
/// Returns a tuple, where the first element is the program account and the
/// second element is the program data account.
pub fn create_program_accounts(
program_id: &Pubkey,
elf: &[u8],
) -> (AccountSharedData, AccountSharedData) {
(
create_program_account(program_id),
create_program_data_account(elf),
)
pub fn program_accounts(program_id: &Pubkey, elf: &[u8]) -> (AccountSharedData, AccountSharedData) {
(program_account(program_id), program_data_account(elf))
}

/// Create a default program cache instance.
Expand Down
20 changes: 10 additions & 10 deletions harness/tests/bpf_program.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
mollusk_svm::{
program::{create_program_account, system_program_account},
program::{program_account, system_program},
result::Check,
Mollusk,
},
Expand Down Expand Up @@ -128,7 +128,7 @@ fn test_transfer() {
&[
(payer, payer_account.clone()),
(recipient, recipient_account.clone()),
(system_program::id(), system_program_account()),
system_program(),
],
&[
Check::err(ProgramError::MissingRequiredSignature),
Expand All @@ -144,7 +144,7 @@ fn test_transfer() {
&[
(payer, AccountSharedData::default()),
(recipient, recipient_account.clone()),
(system_program::id(), system_program_account()),
system_program(),
],
&[
Check::err(ProgramError::Custom(
Expand All @@ -161,7 +161,7 @@ fn test_transfer() {
&[
(payer, payer_account.clone()),
(recipient, recipient_account.clone()),
(system_program::id(), system_program_account()),
system_program(),
],
&[
Check::success(),
Expand Down Expand Up @@ -207,7 +207,7 @@ fn test_close_account() {
&[
(key, account.clone()),
(incinerator::id(), AccountSharedData::default()),
(system_program::id(), system_program_account()),
system_program(),
],
&[
Check::err(ProgramError::MissingRequiredSignature),
Expand All @@ -222,7 +222,7 @@ fn test_close_account() {
&[
(key, account.clone()),
(incinerator::id(), AccountSharedData::default()),
(system_program::id(), system_program_account()),
system_program(),
],
&[
Check::success(),
Expand Down Expand Up @@ -287,7 +287,7 @@ fn test_cpi() {
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
program_account(&cpi_target_program_id),
),
],
&[
Expand All @@ -312,7 +312,7 @@ fn test_cpi() {
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
program_account(&cpi_target_program_id),
),
],
&[
Expand All @@ -336,7 +336,7 @@ fn test_cpi() {
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
program_account(&cpi_target_program_id),
),
],
&[
Expand All @@ -353,7 +353,7 @@ fn test_cpi() {
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
program_account(&cpi_target_program_id),
),
],
&[
Expand Down
Loading