From 4ec66d11c21b9ed256e664e7f3b9c56c080a3867 Mon Sep 17 00:00:00 2001 From: Joe C Date: Mon, 24 Jun 2024 14:17:22 -0500 Subject: [PATCH] Update program API (#12) --- harness/src/lib.rs | 8 +++--- harness/src/program.rs | 51 ++++++++++++++++-------------------- harness/tests/bpf_program.rs | 20 +++++++------- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/harness/src/lib.rs b/harness/src/lib.rs index e33016d..24de189 100644 --- a/harness/src/lib.rs +++ b/harness/src/lib.rs @@ -48,7 +48,6 @@ use { pubkey::Pubkey, rent::Rent, slot_hashes::SlotHashes, - system_program, transaction_context::{InstructionAccount, TransactionContext}, }, std::sync::Arc, @@ -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(), } } @@ -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() }; diff --git a/harness/src/program.rs b/harness/src/program.rs index cf24821..5f89ff9 100644 --- a/harness/src/program.rs +++ b/harness/src/program.rs @@ -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 { Arc::new(LoadedProgram::new_builtin( 0, @@ -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 { @@ -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(); @@ -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. diff --git a/harness/tests/bpf_program.rs b/harness/tests/bpf_program.rs index ded6988..cbfcc5d 100644 --- a/harness/tests/bpf_program.rs +++ b/harness/tests/bpf_program.rs @@ -1,6 +1,6 @@ use { mollusk_svm::{ - program::{create_program_account, system_program_account}, + program::{program_account, system_program}, result::Check, Mollusk, }, @@ -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), @@ -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( @@ -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(), @@ -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), @@ -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(), @@ -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), ), ], &[ @@ -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), ), ], &[ @@ -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), ), ], &[ @@ -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), ), ], &[