Skip to content

Commit

Permalink
Make stake integration tests aware of stake minimum delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo committed Apr 27, 2022
1 parent 02cc056 commit b55196c
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions runtime/tests/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,20 @@ fn test_stake_create_and_split_single_signature() {

let staker_pubkey = staker_keypair.pubkey();

let bank_client = BankClient::new_shared(&Arc::new(Bank::new_for_tests(&genesis_config)));
let bank = Arc::new(Bank::new_for_tests(&genesis_config));
let bank_client = BankClient::new_shared(&bank);

let stake_address =
Pubkey::create_with_seed(&staker_pubkey, "stake", &stake::program::id()).unwrap();

let authorized = Authorized::auto(&staker_pubkey);

let lamports = 1_000_000;
let lamports = {
let rent = &bank.rent_collector().rent;
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
let minimum_delegation = solana_stake_program::get_minimum_delegation(&bank.feature_set);
2 * (rent_exempt_reserve + minimum_delegation)
};

// Create stake account with seed
let message = Message::new(
Expand Down Expand Up @@ -186,14 +192,20 @@ fn test_stake_create_and_split_to_existing_system_account() {

let staker_pubkey = staker_keypair.pubkey();

let bank_client = BankClient::new_shared(&Arc::new(Bank::new_for_tests(&genesis_config)));
let bank = Arc::new(Bank::new_for_tests(&genesis_config));
let bank_client = BankClient::new_shared(&bank);

let stake_address =
Pubkey::create_with_seed(&staker_pubkey, "stake", &stake::program::id()).unwrap();

let authorized = Authorized::auto(&staker_pubkey);

let lamports = 1_000_000;
let lamports = {
let rent = &bank.rent_collector().rent;
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
let minimum_delegation = solana_stake_program::get_minimum_delegation(&bank.feature_set);
2 * (rent_exempt_reserve + minimum_delegation)
};

// Create stake account with seed
let message = Message::new(
Expand Down Expand Up @@ -263,7 +275,7 @@ fn test_stake_account_lifetime() {
} = create_genesis_config_with_leader(
100_000_000_000,
&solana_sdk::pubkey::new_rand(),
1_000_000,
2_000_000_000,
);
let bank = Bank::new_for_tests(&genesis_config);
let mint_pubkey = mint_keypair.pubkey();
Expand All @@ -290,6 +302,14 @@ fn test_stake_account_lifetime() {
.expect("failed to create vote account");

let authorized = Authorized::auto(&stake_pubkey);
let minimum_delegation = {
let rent = &bank.rent_collector().rent;
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
assert_eq!(rent_exempt_reserve, 0, "this test assumes rent is free");
solana_stake_program::get_minimum_delegation(&bank.feature_set)
};
let starting_balance = 2 * minimum_delegation;

// Create stake account and delegate to vote account
let message = Message::new(
&stake_instruction::create_account_and_delegate_stake(
Expand All @@ -298,7 +318,7 @@ fn test_stake_account_lifetime() {
&vote_pubkey,
&authorized,
&Lockup::default(),
1_000_000,
starting_balance,
),
Some(&mint_pubkey),
);
Expand All @@ -310,7 +330,7 @@ fn test_stake_account_lifetime() {
let account = bank.get_account(&stake_pubkey).expect("account not found");
let stake_state = account.state().expect("couldn't unpack account data");
if let StakeState::Stake(_meta, stake) = stake_state {
assert_eq!(stake.delegation.stake, 1_000_000);
assert_eq!(stake.delegation.stake, starting_balance);
} else {
panic!("wrong account type found")
}
Expand All @@ -334,7 +354,7 @@ fn test_stake_account_lifetime() {
let account = bank.get_account(&stake_pubkey).expect("account not found");
let stake_state = account.state().expect("couldn't unpack account data");
if let StakeState::Stake(_meta, stake) = stake_state {
assert_eq!(stake.delegation.stake, 1_000_000);
assert_eq!(stake.delegation.stake, starting_balance);
} else {
panic!("wrong account type found")
}
Expand Down Expand Up @@ -372,7 +392,7 @@ fn test_stake_account_lifetime() {
let staked = get_staked(&bank, &stake_pubkey);
let lamports = bank.get_balance(&stake_pubkey);
assert!(staked > pre_staked);
assert!(lamports > 1_000_000);
assert!(lamports > starting_balance);

// split the stake
let split_stake_keypair = Keypair::new();
Expand Down Expand Up @@ -537,6 +557,13 @@ fn test_create_stake_account_from_seed() {
.expect("failed to create vote account");

let authorized = Authorized::auto(&mint_pubkey);
let (balance, delegation) = {
let rent = &bank.rent_collector().rent;
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
let minimum_delegation = solana_stake_program::get_minimum_delegation(&bank.feature_set);
(rent_exempt_reserve + minimum_delegation, minimum_delegation)
};

// Create stake account and delegate to vote account
let message = Message::new(
&stake_instruction::create_account_with_seed_and_delegate_stake(
Expand All @@ -547,7 +574,7 @@ fn test_create_stake_account_from_seed() {
&vote_pubkey,
&authorized,
&Lockup::default(),
1_000_000,
balance,
),
Some(&mint_pubkey),
);
Expand All @@ -559,7 +586,7 @@ fn test_create_stake_account_from_seed() {
let account = bank.get_account(&stake_pubkey).expect("account not found");
let stake_state = account.state().expect("couldn't unpack account data");
if let StakeState::Stake(_meta, stake) = stake_state {
assert_eq!(stake.delegation.stake, 1_000_000);
assert_eq!(stake.delegation.stake, delegation);
} else {
panic!("wrong account type found")
}
Expand Down

0 comments on commit b55196c

Please sign in to comment.