2019-02-28 13:15:25 -08:00
|
|
|
#[cfg(test)]
|
2019-05-16 08:23:31 -07:00
|
|
|
pub(crate) mod tests {
|
2021-08-10 07:39:50 -07:00
|
|
|
use {
|
|
|
|
rand::Rng,
|
2021-08-12 19:42:41 -07:00
|
|
|
solana_runtime::{
|
|
|
|
bank::Bank,
|
|
|
|
vote_account::{VoteAccount, VoteAccounts},
|
|
|
|
},
|
2021-08-10 07:39:50 -07:00
|
|
|
solana_sdk::{
|
2021-08-12 19:42:41 -07:00
|
|
|
account::AccountSharedData,
|
2021-08-10 07:39:50 -07:00
|
|
|
clock::Clock,
|
|
|
|
instruction::Instruction,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
signature::{Keypair, Signer},
|
|
|
|
signers::Signers,
|
|
|
|
stake::{
|
|
|
|
instruction as stake_instruction,
|
2021-08-12 19:42:41 -07:00
|
|
|
state::{Authorized, Lockup},
|
2021-08-10 07:39:50 -07:00
|
|
|
},
|
|
|
|
transaction::Transaction,
|
|
|
|
},
|
|
|
|
solana_vote_program::{
|
|
|
|
vote_instruction,
|
|
|
|
vote_state::{VoteInit, VoteState, VoteStateVersions},
|
|
|
|
},
|
2020-11-30 09:18:33 -08:00
|
|
|
};
|
2019-02-28 13:15:25 -08:00
|
|
|
|
2019-05-16 08:23:31 -07:00
|
|
|
pub(crate) fn setup_vote_and_stake_accounts(
|
|
|
|
bank: &Bank,
|
|
|
|
from_account: &Keypair,
|
2019-11-08 02:27:35 -08:00
|
|
|
vote_account: &Keypair,
|
2020-03-19 01:58:52 -07:00
|
|
|
validator_identity_account: &Keypair,
|
2019-05-16 08:23:31 -07:00
|
|
|
amount: u64,
|
|
|
|
) {
|
2019-11-08 02:27:35 -08:00
|
|
|
let vote_pubkey = vote_account.pubkey();
|
2020-04-24 12:03:46 -07:00
|
|
|
fn process_instructions<T: Signers>(bank: &Bank, keypairs: &T, ixs: &[Instruction]) {
|
2020-02-20 12:13:23 -08:00
|
|
|
let tx = Transaction::new_signed_with_payer(
|
2019-05-16 08:23:31 -07:00
|
|
|
ixs,
|
2020-02-20 12:13:23 -08:00
|
|
|
Some(&keypairs.pubkeys()[0]),
|
2019-06-03 09:04:51 -07:00
|
|
|
keypairs,
|
2019-05-16 08:23:31 -07:00
|
|
|
bank.last_blockhash(),
|
2020-02-20 12:13:23 -08:00
|
|
|
);
|
|
|
|
bank.process_transaction(&tx).unwrap();
|
2019-05-16 08:23:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
process_instructions(
|
|
|
|
bank,
|
2020-03-19 01:58:52 -07:00
|
|
|
&[from_account, vote_account, validator_identity_account],
|
2020-04-24 12:03:46 -07:00
|
|
|
&vote_instruction::create_account(
|
2019-05-23 23:20:04 -07:00
|
|
|
&from_account.pubkey(),
|
2019-11-08 02:27:35 -08:00
|
|
|
&vote_pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
&VoteInit {
|
2020-03-19 01:58:52 -07:00
|
|
|
node_pubkey: validator_identity_account.pubkey(),
|
2019-11-08 02:27:35 -08:00
|
|
|
authorized_voter: vote_pubkey,
|
|
|
|
authorized_withdrawer: vote_pubkey,
|
2019-09-25 13:53:49 -07:00
|
|
|
commission: 0,
|
|
|
|
},
|
2019-05-23 23:20:04 -07:00
|
|
|
amount,
|
|
|
|
),
|
2019-05-16 08:23:31 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
let stake_account_keypair = Keypair::new();
|
|
|
|
let stake_account_pubkey = stake_account_keypair.pubkey();
|
|
|
|
|
|
|
|
process_instructions(
|
|
|
|
bank,
|
2019-06-10 12:17:29 -07:00
|
|
|
&[from_account, &stake_account_keypair],
|
2020-04-24 12:03:46 -07:00
|
|
|
&stake_instruction::create_account_and_delegate_stake(
|
2019-05-16 08:23:31 -07:00
|
|
|
&from_account.pubkey(),
|
|
|
|
&stake_account_pubkey,
|
2019-11-08 02:27:35 -08:00
|
|
|
&vote_pubkey,
|
2019-09-26 13:29:29 -07:00
|
|
|
&Authorized::auto(&stake_account_pubkey),
|
2019-12-14 04:38:24 -08:00
|
|
|
&Lockup::default(),
|
2019-09-29 21:18:15 -07:00
|
|
|
amount,
|
2019-05-21 07:32:38 -07:00
|
|
|
),
|
2019-05-16 08:23:31 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:31:59 -08:00
|
|
|
#[test]
|
2019-05-14 16:15:51 -07:00
|
|
|
fn test_to_staked_nodes() {
|
2019-03-03 18:04:13 -08:00
|
|
|
let mut stakes = Vec::new();
|
2020-10-19 12:12:08 -07:00
|
|
|
let node1 = solana_sdk::pubkey::new_rand();
|
2019-03-03 18:04:13 -08:00
|
|
|
|
2019-05-14 16:15:51 -07:00
|
|
|
// Node 1 has stake of 3
|
2019-03-03 18:04:13 -08:00
|
|
|
for i in 0..3 {
|
2019-09-25 13:53:49 -07:00
|
|
|
stakes.push((
|
|
|
|
i,
|
2019-12-30 19:57:53 -08:00
|
|
|
VoteState::new(
|
|
|
|
&VoteInit {
|
|
|
|
node_pubkey: node1,
|
|
|
|
..VoteInit::default()
|
|
|
|
},
|
|
|
|
&Clock::default(),
|
|
|
|
),
|
2019-09-25 13:53:49 -07:00
|
|
|
));
|
2019-03-03 18:04:13 -08:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:15:51 -07:00
|
|
|
// Node 1 has stake of 5
|
2020-10-19 12:12:08 -07:00
|
|
|
let node2 = solana_sdk::pubkey::new_rand();
|
2019-09-25 13:53:49 -07:00
|
|
|
|
|
|
|
stakes.push((
|
|
|
|
5,
|
2019-12-30 19:57:53 -08:00
|
|
|
VoteState::new(
|
|
|
|
&VoteInit {
|
|
|
|
node_pubkey: node2,
|
|
|
|
..VoteInit::default()
|
|
|
|
},
|
|
|
|
&Clock::default(),
|
|
|
|
),
|
2019-09-25 13:53:49 -07:00
|
|
|
));
|
2020-11-30 09:18:33 -08:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let vote_accounts = stakes.into_iter().map(|(stake, vote_state)| {
|
2021-03-09 13:06:07 -08:00
|
|
|
let account = AccountSharedData::new_data(
|
2020-11-30 09:18:33 -08:00
|
|
|
rng.gen(), // lamports
|
2020-12-21 15:19:04 -08:00
|
|
|
&VoteStateVersions::new_current(vote_state),
|
2020-11-30 09:18:33 -08:00
|
|
|
&Pubkey::new_unique(), // owner
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let vote_pubkey = Pubkey::new_unique();
|
2021-08-10 07:39:50 -07:00
|
|
|
(vote_pubkey, (stake, VoteAccount::from(account)))
|
2020-11-30 09:18:33 -08:00
|
|
|
});
|
2020-12-17 13:22:50 -08:00
|
|
|
let result = vote_accounts.collect::<VoteAccounts>().staked_nodes();
|
2019-03-03 18:04:13 -08:00
|
|
|
assert_eq!(result.len(), 2);
|
2019-05-14 16:15:51 -07:00
|
|
|
assert_eq!(result[&node1], 3);
|
|
|
|
assert_eq!(result[&node2], 5);
|
2019-03-01 17:31:59 -08:00
|
|
|
}
|
2019-02-28 13:15:25 -08:00
|
|
|
}
|