Skip to content

Commit

Permalink
Program-Test: Spport adding genesis accounts (anza-xyz#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored May 18, 2024
1 parent 1a37631 commit 6631e5f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
10 changes: 9 additions & 1 deletion program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ pub fn read_file<P: AsRef<Path>>(path: P) -> Vec<u8> {

pub struct ProgramTest {
accounts: Vec<(Pubkey, AccountSharedData)>,
genesis_accounts: Vec<(Pubkey, AccountSharedData)>,
builtin_programs: Vec<(Pubkey, &'static str, ProgramCacheEntry)>,
compute_max_units: Option<u64>,
prefer_bpf: bool,
Expand Down Expand Up @@ -496,6 +497,7 @@ impl Default for ProgramTest {

Self {
accounts: vec![],
genesis_accounts: vec![],
builtin_programs: vec![],
compute_max_units: None,
prefer_bpf,
Expand Down Expand Up @@ -549,6 +551,12 @@ impl ProgramTest {
self.set_compute_max_units(bpf_compute_max_units);
}

/// Add an account to the test environment's genesis config.
pub fn add_genesis_account(&mut self, address: Pubkey, account: Account) {
self.genesis_accounts
.push((address, AccountSharedData::from(account)));
}

/// Add an account to the test environment
pub fn add_account(&mut self, address: Pubkey, account: Account) {
self.accounts
Expand Down Expand Up @@ -781,7 +789,7 @@ impl ProgramTest {
fee_rate_governor,
rent,
ClusterType::Development,
vec![],
std::mem::take(&mut self.genesis_accounts),
);

// Remove features tagged to deactivate
Expand Down
45 changes: 45 additions & 0 deletions program-test/tests/genesis_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use {
solana_program_test::ProgramTest,
solana_sdk::{account::Account, pubkey::Pubkey},
};

#[tokio::test]
async fn genesis_accounts() {
let my_genesis_accounts = [
(
Pubkey::new_unique(),
Account::new(1, 0, &solana_sdk::system_program::id()),
),
(
Pubkey::new_unique(),
Account::new(1, 0, &solana_sdk::config::program::id()),
),
(
Pubkey::new_unique(),
Account::new(1, 0, &solana_sdk::feature::id()),
),
(
Pubkey::new_unique(),
Account::new(1, 0, &solana_sdk::stake::program::id()),
),
];

let mut program_test = ProgramTest::default();

for (pubkey, account) in my_genesis_accounts.iter() {
program_test.add_genesis_account(*pubkey, account.clone());
}

let mut context = program_test.start_with_context().await;

// Verify the accounts are present.
for (pubkey, account) in my_genesis_accounts.iter() {
let fetched_account = context
.banks_client
.get_account(*pubkey)
.await
.unwrap()
.unwrap();
assert_eq!(fetched_account, *account);
}
}

0 comments on commit 6631e5f

Please sign in to comment.