program-test: Add warp tests for rent and stake rewards (#15136)

* program-test Add rent collection and stake rewards

* Improve tests to initialize vote state

* Update comment

* Update program-test/src/lib.rs

Co-authored-by: Michael Vines <mvines@gmail.com>

* Review feedback

* cargo fmt

* Avoid using hard-coded slots in tests

* Make genesis_config private

Co-authored-by: Michael Vines <mvines@gmail.com>
This commit is contained in:
Jon Cinque 2021-02-05 22:08:00 +01:00 committed by GitHub
parent fabecdc86c
commit c5be226821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 217 additions and 37 deletions

2
Cargo.lock generated
View File

@ -4935,6 +4935,8 @@ dependencies = [
"solana-program 1.6.0",
"solana-runtime",
"solana-sdk",
"solana-stake-program",
"solana-vote-program",
"thiserror",
"tokio 1.1.1",
]

View File

@ -21,5 +21,9 @@ solana-logger = { path = "../logger", version = "1.6.0" }
solana-program = { path = "../sdk/program", version = "1.6.0" }
solana-runtime = { path = "../runtime", version = "1.6.0" }
solana-sdk = { path = "../sdk", version = "1.6.0" }
solana-vote-program = { path = "../programs/vote", version = "1.6.0" }
thiserror = "1.0"
tokio = { version = "1.1", features = ["full"] }
[dev-dependencies]
solana-stake-program = { path = "../programs/stake", version = "1.6.0" }

View File

@ -16,7 +16,7 @@ use {
bank::{Bank, Builtin, ExecuteTimings},
bank_forks::BankForks,
commitment::BlockCommitmentCache,
genesis_utils::create_genesis_config_with_leader,
genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo},
},
solana_sdk::{
account::Account,
@ -28,6 +28,7 @@ use {
},
signature::{Keypair, Signer},
},
solana_vote_program::vote_state::{VoteState, VoteStateVersions},
std::{
cell::RefCell,
collections::HashMap,
@ -593,9 +594,8 @@ impl ProgramTest {
) -> (
Arc<RwLock<BankForks>>,
Arc<RwLock<BlockCommitmentCache>>,
Keypair,
Hash,
GenesisConfig,
GenesisConfigInfo,
) {
{
use std::sync::Once;
@ -606,20 +606,20 @@ impl ProgramTest {
});
}
let rent = Rent::default();
let bootstrap_validator_pubkey = Pubkey::new_unique();
let bootstrap_validator_stake_lamports = 42;
let bootstrap_validator_lamports = rent.minimum_balance(VoteState::size_of());
let gci = create_genesis_config_with_leader(
let mut gci = create_genesis_config_with_leader(
sol_to_lamports(1_000_000.0),
&bootstrap_validator_pubkey,
bootstrap_validator_stake_lamports,
bootstrap_validator_lamports,
);
let mut genesis_config = gci.genesis_config;
genesis_config.rent = Rent::default();
let genesis_config = &mut gci.genesis_config;
genesis_config.rent = rent;
genesis_config.fee_rate_governor =
solana_program::fee_calculator::FeeRateGovernor::default();
let payer = gci.mint_keypair;
debug!("Payer address: {}", payer.pubkey());
debug!("Payer address: {}", gci.mint_keypair.pubkey());
debug!("Genesis config: {}", genesis_config);
let mut bank = Bank::new(&genesis_config);
@ -666,18 +666,11 @@ impl ProgramTest {
BlockCommitmentCache::new_for_tests_with_slots(slot, slot),
));
(
bank_forks,
block_commitment_cache,
payer,
last_blockhash,
genesis_config,
)
(bank_forks, block_commitment_cache, last_blockhash, gci)
}
pub async fn start(self) -> (BanksClient, Keypair, Hash) {
let (bank_forks, block_commitment_cache, payer, last_blockhash, genesis_config) =
self.setup_bank();
let (bank_forks, block_commitment_cache, last_blockhash, gci) = self.setup_bank();
let transport =
start_local_server(bank_forks.clone(), block_commitment_cache.clone()).await;
let banks_client = start_client(transport)
@ -687,6 +680,7 @@ impl ProgramTest {
// Run a simulated PohService to provide the client with new blockhashes. New blockhashes
// are required when sending multiple otherwise identical transactions in series from a
// test
let target_tick_duration = gci.genesis_config.poh_config.target_tick_duration;
tokio::spawn(async move {
loop {
bank_forks
@ -694,11 +688,11 @@ impl ProgramTest {
.unwrap()
.working_bank()
.register_tick(&Hash::new_unique());
tokio::time::sleep(genesis_config.poh_config.target_tick_duration).await;
tokio::time::sleep(target_tick_duration).await;
}
});
(banks_client, payer, last_blockhash)
(banks_client, gci.mint_keypair, last_blockhash)
}
/// Start the test client
@ -706,8 +700,7 @@ impl ProgramTest {
/// Returns a `BanksClient` interface into the test environment as well as a payer `Keypair`
/// with SOL for sending transactions
pub async fn start_with_context(self) -> ProgramTestContext {
let (bank_forks, block_commitment_cache, payer, last_blockhash, genesis_config) =
self.setup_bank();
let (bank_forks, block_commitment_cache, last_blockhash, gci) = self.setup_bank();
let transport =
start_local_server(bank_forks.clone(), block_commitment_cache.clone()).await;
let banks_client = start_client(transport)
@ -718,9 +711,8 @@ impl ProgramTest {
bank_forks,
block_commitment_cache,
banks_client,
payer,
last_blockhash,
genesis_config,
gci,
)
}
}
@ -772,8 +764,9 @@ impl<T> Drop for DroppableTask<T> {
pub struct ProgramTestContext {
pub banks_client: BanksClient,
pub payer: Keypair,
pub last_blockhash: Hash,
pub payer: Keypair,
genesis_config: GenesisConfig,
bank_forks: Arc<RwLock<BankForks>>,
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
_bank_task: DroppableTask<()>,
@ -784,15 +777,17 @@ impl ProgramTestContext {
bank_forks: Arc<RwLock<BankForks>>,
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
banks_client: BanksClient,
payer: Keypair,
last_blockhash: Hash,
genesis_config: GenesisConfig,
genesis_config_info: GenesisConfigInfo,
) -> Self {
// Run a simulated PohService to provide the client with new blockhashes. New blockhashes
// are required when sending multiple otherwise identical transactions in series from a
// test
let running_bank_forks = bank_forks.clone();
let target_tick_duration = genesis_config.poh_config.target_tick_duration;
let target_tick_duration = genesis_config_info
.genesis_config
.poh_config
.target_tick_duration;
let exit = Arc::new(AtomicBool::new(false));
let bank_task = DroppableTask(
exit.clone(),
@ -813,14 +808,41 @@ impl ProgramTestContext {
Self {
banks_client,
block_commitment_cache,
payer,
last_blockhash,
payer: genesis_config_info.mint_keypair,
genesis_config: genesis_config_info.genesis_config,
bank_forks,
block_commitment_cache,
_bank_task: bank_task,
}
}
pub fn genesis_config(&self) -> &GenesisConfig {
&self.genesis_config
}
/// Manually increment vote credits for the current epoch in the specified vote account to simulate validator voting activity
pub fn increment_vote_account_credits(
&mut self,
vote_account_address: &Pubkey,
number_of_credits: u64,
) {
let bank_forks = self.bank_forks.read().unwrap();
let bank = bank_forks.working_bank();
// generate some vote activity for rewards
let mut vote_account = bank.get_account(vote_account_address).unwrap();
let mut vote_state = VoteState::from(&vote_account).unwrap();
let epoch = bank.epoch();
for _ in 0..number_of_credits {
vote_state.increment_credits(epoch);
}
let versioned = VoteStateVersions::new_current(vote_state);
VoteState::to(&versioned, &mut vote_account).unwrap();
bank.store_account(vote_account_address, &vote_account);
}
/// Force the working bank ahead to a new slot
pub fn warp_to_slot(&mut self, warp_slot: Slot) -> Result<(), ProgramTestError> {
let mut bank_forks = self.bank_forks.write().unwrap();
@ -839,6 +861,7 @@ impl ProgramTestContext {
if warp_slot <= working_slot {
return Err(ProgramTestError::InvalidWarpSlot);
}
let pre_warp_slot = warp_slot - 1;
let warp_bank = bank_forks.insert(Bank::warp_from_parent(
&bank,

View File

@ -2,12 +2,10 @@ use {
solana_banks_client::BanksClient,
solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, hash::Hash, instruction::Instruction,
msg, pubkey::Pubkey, rent::Rent,
msg, pubkey::Pubkey, rent::Rent, system_instruction,
},
solana_program_test::{processor, ProgramTest},
solana_sdk::{
signature::Keypair, signature::Signer, system_instruction, transaction::Transaction,
},
solana_sdk::{signature::Keypair, signature::Signer, transaction::Transaction},
};
#[allow(clippy::unnecessary_wraps)]

View File

@ -6,13 +6,23 @@ use {
instruction::{AccountMeta, Instruction, InstructionError},
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
system_instruction, system_program,
sysvar::{clock, Sysvar},
},
solana_program_test::{processor, ProgramTest, ProgramTestError},
solana_sdk::{
signature::Signer,
signature::{Keypair, Signer},
transaction::{Transaction, TransactionError},
},
solana_stake_program::{
stake_instruction,
stake_state::{Authorized, Lockup},
},
solana_vote_program::{
vote_instruction,
vote_state::{VoteInit, VoteState},
},
std::convert::TryInto,
};
@ -36,7 +46,7 @@ fn process_instruction(
}
#[tokio::test]
async fn custom_warp() {
async fn clock_sysvar_updated_from_warp() {
let program_id = Pubkey::new_unique();
// Initialize and start the test network
let program_test = ProgramTest::new(
@ -95,3 +105,146 @@ async fn custom_warp() {
ProgramTestError::InvalidWarpSlot,
);
}
#[tokio::test]
async fn rent_collected_from_warp() {
let program_id = Pubkey::new_unique();
// Initialize and start the test network
let program_test = ProgramTest::default();
let mut context = program_test.start_with_context().await;
let account_size = 100;
let keypair = Keypair::new();
let account_lamports = Rent::default().minimum_balance(account_size) - 100; // not rent exempt
let instruction = system_instruction::create_account(
&context.payer.pubkey(),
&keypair.pubkey(),
account_lamports,
account_size as u64,
&program_id,
);
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&context.payer.pubkey()),
&[&context.payer, &keypair],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
let account = context
.banks_client
.get_account(keypair.pubkey())
.await
.expect("account exists")
.unwrap();
assert_eq!(account.lamports, account_lamports);
// Warp forward and see that rent has been collected
// This test was a bit flaky with one warp, but two warps always works
let slots_per_epoch = context.genesis_config().epoch_schedule.slots_per_epoch;
context.warp_to_slot(slots_per_epoch).unwrap();
context.warp_to_slot(slots_per_epoch * 2).unwrap();
let account = context
.banks_client
.get_account(keypair.pubkey())
.await
.expect("account exists")
.unwrap();
assert!(account.lamports < account_lamports);
}
#[tokio::test]
async fn stake_rewards_from_warp() {
// Initialize and start the test network
let program_test = ProgramTest::default();
let mut context = program_test.start_with_context().await;
let mut instructions = vec![];
let validator_keypair = Keypair::new();
instructions.push(system_instruction::create_account(
&context.payer.pubkey(),
&validator_keypair.pubkey(),
42,
0,
&system_program::id(),
));
let vote_lamports = Rent::default().minimum_balance(VoteState::size_of());
let vote_keypair = Keypair::new();
let user_keypair = Keypair::new();
instructions.append(&mut vote_instruction::create_account(
&context.payer.pubkey(),
&vote_keypair.pubkey(),
&VoteInit {
node_pubkey: validator_keypair.pubkey(),
authorized_voter: user_keypair.pubkey(),
..VoteInit::default()
},
vote_lamports,
));
let stake_keypair = Keypair::new();
let stake_lamports = 1_000_000_000_000;
instructions.append(&mut stake_instruction::create_account_and_delegate_stake(
&context.payer.pubkey(),
&stake_keypair.pubkey(),
&vote_keypair.pubkey(),
&Authorized::auto(&user_keypair.pubkey()),
&Lockup::default(),
stake_lamports,
));
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&vec![
&context.payer,
&validator_keypair,
&vote_keypair,
&stake_keypair,
&user_keypair,
],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
let account = context
.banks_client
.get_account(stake_keypair.pubkey())
.await
.expect("account exists")
.unwrap();
assert_eq!(account.lamports, stake_lamports);
// warp one epoch forward for normal inflation, no rewards collected
let first_normal_slot = context.genesis_config().epoch_schedule.first_normal_slot;
context.warp_to_slot(first_normal_slot).unwrap();
let account = context
.banks_client
.get_account(stake_keypair.pubkey())
.await
.expect("account exists")
.unwrap();
assert_eq!(account.lamports, stake_lamports);
context.increment_vote_account_credits(&vote_keypair.pubkey(), 100);
// go forward and see that rewards have been distributed
let slots_per_epoch = context.genesis_config().epoch_schedule.slots_per_epoch;
context
.warp_to_slot(first_normal_slot + slots_per_epoch)
.unwrap();
let account = context
.banks_client
.get_account(stake_keypair.pubkey())
.await
.expect("account exists")
.unwrap();
assert!(account.lamports > stake_lamports);
}