From 6cc22e62d42ff186645118410e916539ca628346 Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Fri, 19 Mar 2021 14:37:13 +0100 Subject: [PATCH] program-test: Fix warp and staking issue (#16002) Since program-test creates a test genesis and then adds fees and rent, some of the genesis accounts get rent-collected after warping. Most notably, `StakeConfig` gets rent-collected, causing any stake operations to fail after warp. This fix creates genesis with the `Rent` and `FeeRateGovernor` actually used by the bank. --- program-test/src/lib.rs | 53 ++++++++++++++++++++++++++++---------- program-test/tests/warp.rs | 2 ++ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/program-test/src/lib.rs b/program-test/src/lib.rs index 9a88a11bb..029b41218 100644 --- a/program-test/src/lib.rs +++ b/program-test/src/lib.rs @@ -8,21 +8,29 @@ use { solana_banks_client::start_client, solana_banks_server::banks_server::start_local_server, solana_program::{ - account_info::AccountInfo, entrypoint::ProgramResult, fee_calculator::FeeCalculator, - hash::Hash, instruction::Instruction, instruction::InstructionError, message::Message, - native_token::sol_to_lamports, program_error::ProgramError, program_stubs, pubkey::Pubkey, + account_info::AccountInfo, + entrypoint::ProgramResult, + fee_calculator::{FeeCalculator, FeeRateGovernor}, + hash::Hash, + instruction::Instruction, + instruction::InstructionError, + message::Message, + native_token::sol_to_lamports, + program_error::ProgramError, + program_stubs, + pubkey::Pubkey, rent::Rent, }, solana_runtime::{ bank::{Bank, Builtin, ExecuteTimings}, bank_forks::BankForks, commitment::BlockCommitmentCache, - genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo}, + genesis_utils::{create_genesis_config_with_leader_ex, GenesisConfigInfo}, }, solana_sdk::{ account::{Account, AccountSharedData, ReadableAccount}, clock::Slot, - genesis_config::GenesisConfig, + genesis_config::{ClusterType, GenesisConfig}, keyed_account::KeyedAccount, process_instruction::{ stable_log, BpfComputeBudget, InvokeContext, ProcessInstructionWithContext, @@ -623,19 +631,27 @@ impl ProgramTest { } let rent = Rent::default(); + let fee_rate_governor = FeeRateGovernor::default(); let bootstrap_validator_pubkey = Pubkey::new_unique(); - let bootstrap_validator_lamports = rent.minimum_balance(VoteState::size_of()); + let bootstrap_validator_stake_lamports = rent.minimum_balance(VoteState::size_of()); - let mut gci = create_genesis_config_with_leader( + let mint_keypair = Keypair::new(); + let voting_keypair = Keypair::new(); + + let genesis_config = create_genesis_config_with_leader_ex( sol_to_lamports(1_000_000.0), + &mint_keypair.pubkey(), &bootstrap_validator_pubkey, - bootstrap_validator_lamports, + &voting_keypair.pubkey(), + &Pubkey::new_unique(), + bootstrap_validator_stake_lamports, + 42, + fee_rate_governor, + rent, + ClusterType::Development, + vec![], ); - let genesis_config = &mut gci.genesis_config; - genesis_config.rent = rent; - genesis_config.fee_rate_governor = - solana_program::fee_calculator::FeeRateGovernor::default(); - debug!("Payer address: {}", gci.mint_keypair.pubkey()); + debug!("Payer address: {}", mint_keypair.pubkey()); debug!("Genesis config: {}", genesis_config); let mut bank = Bank::new(&genesis_config); @@ -682,7 +698,16 @@ impl ProgramTest { BlockCommitmentCache::new_for_tests_with_slots(slot, slot), )); - (bank_forks, block_commitment_cache, last_blockhash, gci) + ( + bank_forks, + block_commitment_cache, + last_blockhash, + GenesisConfigInfo { + mint_keypair, + voting_keypair, + genesis_config, + }, + ) } pub async fn start(self) -> (BanksClient, Keypair, Hash) { diff --git a/program-test/tests/warp.rs b/program-test/tests/warp.rs index 47807ac62..8baf36067 100644 --- a/program-test/tests/warp.rs +++ b/program-test/tests/warp.rs @@ -164,6 +164,8 @@ async fn stake_rewards_from_warp() { let program_test = ProgramTest::default(); let mut context = program_test.start_with_context().await; + // warp once to make sure stake config doesn't get rent-collected + context.warp_to_slot(100).unwrap(); let mut instructions = vec![]; let validator_keypair = Keypair::new(); instructions.push(system_instruction::create_account(