| 
									
										
										
										
											2021-02-16 14:48:20 -07:00
										 |  |  | #![allow(clippy::integer_arithmetic)]
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  | use solana_runtime::{
 | 
					
						
							|  |  |  |     bank::Bank,
 | 
					
						
							|  |  |  |     bank_client::BankClient,
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |     genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo},
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  | };
 | 
					
						
							|  |  |  | use solana_sdk::{
 | 
					
						
							| 
									
										
										
										
											2020-10-28 22:01:07 -07:00
										 |  |  |     account::from_account,
 | 
					
						
							| 
									
										
										
										
											2020-01-22 17:54:06 -08:00
										 |  |  |     account_utils::StateMut,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  |     client::SyncClient,
 | 
					
						
							|  |  |  |     message::Message,
 | 
					
						
							|  |  |  |     pubkey::Pubkey,
 | 
					
						
							| 
									
										
										
										
											2020-02-20 14:28:55 -07:00
										 |  |  |     signature::{Keypair, Signer},
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  |     system_instruction::SystemError,
 | 
					
						
							| 
									
										
										
										
											2020-10-28 22:01:07 -07:00
										 |  |  |     sysvar::{self, stake_history::StakeHistory},
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  |     transaction::TransactionError,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:12:43 -08:00
										 |  |  | use solana_stake_program::{
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     stake_instruction::{self},
 | 
					
						
							|  |  |  |     stake_state::{self, StakeState},
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:12:43 -08:00
										 |  |  | use solana_vote_program::{
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  |     vote_instruction,
 | 
					
						
							| 
									
										
										
										
											2020-02-25 17:12:01 -08:00
										 |  |  |     vote_state::{Vote, VoteInit, VoteState, VoteStateVersions},
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:29:29 -07:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | use std::sync::Arc;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  | fn next_epoch(bank: &Arc<Bank>) -> Arc<Bank> {
 | 
					
						
							|  |  |  |     bank.squash();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Arc::new(Bank::new_from_parent(
 | 
					
						
							|  |  |  |         &bank,
 | 
					
						
							|  |  |  |         &Pubkey::default(),
 | 
					
						
							|  |  |  |         bank.get_slots_in_epoch(bank.epoch()) + bank.slot(),
 | 
					
						
							|  |  |  |     ))
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | fn fill_epoch_with_votes(
 | 
					
						
							|  |  |  |     bank: &Arc<Bank>,
 | 
					
						
							|  |  |  |     vote_keypair: &Keypair,
 | 
					
						
							|  |  |  |     mint_keypair: &Keypair,
 | 
					
						
							|  |  |  | ) -> Arc<Bank> {
 | 
					
						
							|  |  |  |     let mint_pubkey = mint_keypair.pubkey();
 | 
					
						
							|  |  |  |     let vote_pubkey = vote_keypair.pubkey();
 | 
					
						
							|  |  |  |     let old_epoch = bank.epoch();
 | 
					
						
							|  |  |  |     let mut bank = bank.clone();
 | 
					
						
							|  |  |  |     while bank.epoch() != old_epoch + 1 {
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |         bank.squash();
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         bank = Arc::new(Bank::new_from_parent(
 | 
					
						
							|  |  |  |             &bank,
 | 
					
						
							|  |  |  |             &Pubkey::default(),
 | 
					
						
							|  |  |  |             1 + bank.slot(),
 | 
					
						
							|  |  |  |         ));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							|  |  |  |         let parent = bank.parent().unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |         let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |             &[vote_instruction::vote(
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |                 &vote_pubkey,
 | 
					
						
							|  |  |  |                 &vote_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-09-02 12:01:09 -07:00
										 |  |  |                 Vote::new(vec![parent.slot() as u64], parent.hash()),
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |             )],
 | 
					
						
							|  |  |  |             Some(&mint_pubkey),
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  |         assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |             .send_and_confirm_message(&[mint_keypair, vote_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |             .is_ok());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     bank
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  | fn warmed_up(bank: &Bank, stake_pubkey: &Pubkey) -> bool {
 | 
					
						
							|  |  |  |     let stake = StakeState::stake_from(&bank.get_account(stake_pubkey).unwrap()).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 13:14:32 -08:00
										 |  |  |     stake.delegation.stake
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |         == stake.stake(
 | 
					
						
							|  |  |  |             bank.epoch(),
 | 
					
						
							|  |  |  |             Some(
 | 
					
						
							| 
									
										
										
										
											2021-03-09 15:06:07 -06:00
										 |  |  |                 &from_account::<StakeHistory, _>(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |                     &bank.get_account(&sysvar::stake_history::id()).unwrap(),
 | 
					
						
							|  |  |  |                 )
 | 
					
						
							|  |  |  |                 .unwrap(),
 | 
					
						
							|  |  |  |             ),
 | 
					
						
							| 
									
										
										
										
											2020-11-12 06:11:57 +09:00
										 |  |  |             true,
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |         )
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fn get_staked(bank: &Bank, stake_pubkey: &Pubkey) -> u64 {
 | 
					
						
							|  |  |  |     StakeState::stake_from(&bank.get_account(stake_pubkey).unwrap())
 | 
					
						
							|  |  |  |         .unwrap()
 | 
					
						
							|  |  |  |         .stake(
 | 
					
						
							|  |  |  |             bank.epoch(),
 | 
					
						
							|  |  |  |             Some(
 | 
					
						
							| 
									
										
										
										
											2021-03-09 15:06:07 -06:00
										 |  |  |                 &from_account::<StakeHistory, _>(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |                     &bank.get_account(&sysvar::stake_history::id()).unwrap(),
 | 
					
						
							|  |  |  |                 )
 | 
					
						
							|  |  |  |                 .unwrap(),
 | 
					
						
							|  |  |  |             ),
 | 
					
						
							| 
									
										
										
										
											2020-11-12 06:11:57 +09:00
										 |  |  |             true,
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |         )
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  | #[test]
 | 
					
						
							|  |  |  | fn test_stake_create_and_split_single_signature() {
 | 
					
						
							|  |  |  |     solana_logger::setup();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let GenesisConfigInfo {
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:05:12 -07:00
										 |  |  |         genesis_config,
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  |         mint_keypair: staker_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:23:14 -07:00
										 |  |  |     } = create_genesis_config_with_leader(
 | 
					
						
							|  |  |  |         100_000_000_000,
 | 
					
						
							|  |  |  |         &solana_sdk::pubkey::new_rand(),
 | 
					
						
							|  |  |  |         1_000_000,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let staker_pubkey = staker_keypair.pubkey();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&Arc::new(Bank::new(&genesis_config)));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let stake_address =
 | 
					
						
							| 
									
										
										
										
											2020-03-20 15:20:48 -07:00
										 |  |  |         Pubkey::create_with_seed(&staker_pubkey, "stake", &solana_stake_program::id()).unwrap();
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let authorized = stake_state::Authorized::auto(&staker_pubkey);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let lamports = 1_000_000;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Create stake account with seed
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							|  |  |  |         &stake_instruction::create_account_with_seed(
 | 
					
						
							|  |  |  |             &staker_pubkey, // from
 | 
					
						
							|  |  |  |             &stake_address, // to
 | 
					
						
							|  |  |  |             &staker_pubkey, // base
 | 
					
						
							|  |  |  |             "stake",        // seed
 | 
					
						
							|  |  |  |             &authorized,
 | 
					
						
							|  |  |  |             &stake_state::Lockup::default(),
 | 
					
						
							|  |  |  |             lamports,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&staker_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // only one signature required
 | 
					
						
							|  |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&staker_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  |         .expect("failed to create and delegate stake account");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // split the stake
 | 
					
						
							|  |  |  |     let split_stake_address =
 | 
					
						
							| 
									
										
										
										
											2020-03-20 15:20:48 -07:00
										 |  |  |         Pubkey::create_with_seed(&staker_pubkey, "split_stake", &solana_stake_program::id())
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  |             .unwrap();
 | 
					
						
							|  |  |  |     // Test split
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-05-01 17:23:33 -07:00
										 |  |  |         &stake_instruction::split_with_seed(
 | 
					
						
							|  |  |  |             &stake_address, // original
 | 
					
						
							|  |  |  |             &staker_pubkey, // authorized
 | 
					
						
							|  |  |  |             lamports / 2,
 | 
					
						
							|  |  |  |             &split_stake_address, // new address
 | 
					
						
							|  |  |  |             &staker_pubkey,       // base
 | 
					
						
							|  |  |  |             "split_stake",        // seed
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&staker_keypair.pubkey()),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&staker_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2020-01-20 12:33:27 -08:00
										 |  |  |         .is_ok());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // w00t!
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  | #[test]
 | 
					
						
							|  |  |  | fn test_stake_create_and_split_to_existing_system_account() {
 | 
					
						
							|  |  |  |     // Ensure stake-split does not allow the user to promote an existing system account into
 | 
					
						
							|  |  |  |     // a stake account.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     solana_logger::setup();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let GenesisConfigInfo {
 | 
					
						
							|  |  |  |         genesis_config,
 | 
					
						
							|  |  |  |         mint_keypair: staker_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:23:14 -07:00
										 |  |  |     } = create_genesis_config_with_leader(
 | 
					
						
							|  |  |  |         100_000_000_000,
 | 
					
						
							|  |  |  |         &solana_sdk::pubkey::new_rand(),
 | 
					
						
							|  |  |  |         1_000_000,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let staker_pubkey = staker_keypair.pubkey();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&Arc::new(Bank::new(&genesis_config)));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let stake_address =
 | 
					
						
							|  |  |  |         Pubkey::create_with_seed(&staker_pubkey, "stake", &solana_stake_program::id()).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let authorized = stake_state::Authorized::auto(&staker_pubkey);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let lamports = 1_000_000;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Create stake account with seed
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							|  |  |  |         &stake_instruction::create_account_with_seed(
 | 
					
						
							|  |  |  |             &staker_pubkey, // from
 | 
					
						
							|  |  |  |             &stake_address, // to
 | 
					
						
							|  |  |  |             &staker_pubkey, // base
 | 
					
						
							|  |  |  |             "stake",        // seed
 | 
					
						
							|  |  |  |             &authorized,
 | 
					
						
							|  |  |  |             &stake_state::Lockup::default(),
 | 
					
						
							|  |  |  |             lamports,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&staker_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&staker_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  |         .expect("failed to create and delegate stake account");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let split_stake_address =
 | 
					
						
							|  |  |  |         Pubkey::create_with_seed(&staker_pubkey, "split_stake", &solana_stake_program::id())
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // First, put a system account where we want the new stake account
 | 
					
						
							|  |  |  |     let existing_lamports = 42;
 | 
					
						
							|  |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .transfer_and_confirm(existing_lamports, &staker_keypair, &split_stake_address)
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  |         .unwrap();
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         bank_client.get_balance(&split_stake_address).unwrap(),
 | 
					
						
							|  |  |  |         existing_lamports
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Verify the split fails because the account is already in use
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  |         &stake_instruction::split_with_seed(
 | 
					
						
							|  |  |  |             &stake_address, // original
 | 
					
						
							|  |  |  |             &staker_pubkey, // authorized
 | 
					
						
							|  |  |  |             lamports / 2,
 | 
					
						
							|  |  |  |             &split_stake_address, // new address
 | 
					
						
							|  |  |  |             &staker_pubkey,       // base
 | 
					
						
							|  |  |  |             "split_stake",        // seed
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&staker_keypair.pubkey()),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |             .send_and_confirm_message(&[&staker_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2020-05-22 17:39:01 -06:00
										 |  |  |             .unwrap_err()
 | 
					
						
							|  |  |  |             .unwrap(),
 | 
					
						
							|  |  |  |         TransactionError::InstructionError(0, SystemError::AccountAlreadyInUse.into())
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         bank_client.get_balance(&split_stake_address).unwrap(),
 | 
					
						
							|  |  |  |         existing_lamports
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | #[test]
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  | fn test_stake_account_lifetime() {
 | 
					
						
							|  |  |  |     let stake_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     let stake_pubkey = stake_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     let vote_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     let vote_pubkey = vote_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2020-03-19 01:58:52 -07:00
										 |  |  |     let identity_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     let identity_pubkey = identity_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |     let GenesisConfigInfo {
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:05:12 -07:00
										 |  |  |         genesis_config,
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         mint_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:23:14 -07:00
										 |  |  |     } = create_genesis_config_with_leader(
 | 
					
						
							|  |  |  |         100_000_000_000,
 | 
					
						
							|  |  |  |         &solana_sdk::pubkey::new_rand(),
 | 
					
						
							|  |  |  |         1_000_000,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:56:57 -05:00
										 |  |  |     let bank = Bank::new(&genesis_config);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     let mint_pubkey = mint_keypair.pubkey();
 | 
					
						
							|  |  |  |     let mut bank = Arc::new(bank);
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Create Vote Account
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							|  |  |  |         &vote_instruction::create_account(
 | 
					
						
							|  |  |  |             &mint_pubkey,
 | 
					
						
							|  |  |  |             &vote_pubkey,
 | 
					
						
							|  |  |  |             &VoteInit {
 | 
					
						
							|  |  |  |                 node_pubkey: identity_pubkey,
 | 
					
						
							|  |  |  |                 authorized_voter: vote_pubkey,
 | 
					
						
							|  |  |  |                 authorized_withdrawer: vote_pubkey,
 | 
					
						
							|  |  |  |                 commission: 50,
 | 
					
						
							|  |  |  |             },
 | 
					
						
							|  |  |  |             10,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         .expect("failed to create vote account");
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     let authorized = stake_state::Authorized::auto(&stake_pubkey);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     // Create stake account and delegate to vote account
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							|  |  |  |         &stake_instruction::create_account_and_delegate_stake(
 | 
					
						
							|  |  |  |             &mint_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							|  |  |  |             &vote_pubkey,
 | 
					
						
							|  |  |  |             &authorized,
 | 
					
						
							|  |  |  |             &stake_state::Lockup::default(),
 | 
					
						
							|  |  |  |             1_000_000,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         .expect("failed to create and delegate stake account");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test that correct lamports are staked
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     let account = bank.get_account(&stake_pubkey).expect("account not found");
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     let stake_state = account.state().expect("couldn't unpack account data");
 | 
					
						
							| 
									
										
										
										
											2019-10-31 11:07:27 -07:00
										 |  |  |     if let StakeState::Stake(_meta, stake) = stake_state {
 | 
					
						
							| 
									
										
										
										
											2019-11-25 13:14:32 -08:00
										 |  |  |         assert_eq!(stake.delegation.stake, 1_000_000);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     } else {
 | 
					
						
							| 
									
										
										
										
											2020-05-15 17:35:43 +01:00
										 |  |  |         panic!("wrong account type found")
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // Test that we cannot withdraw anything until deactivation
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &[stake_instruction::withdraw(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:12:08 -07:00
										 |  |  |             &solana_sdk::pubkey::new_rand(),
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             1,
 | 
					
						
							| 
									
										
										
										
											2020-04-20 18:16:50 -06:00
										 |  |  |             None,
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         .is_err());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test that lamports are still staked
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     let account = bank.get_account(&stake_pubkey).expect("account not found");
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     let stake_state = account.state().expect("couldn't unpack account data");
 | 
					
						
							| 
									
										
										
										
											2019-10-31 11:07:27 -07:00
										 |  |  |     if let StakeState::Stake(_meta, stake) = stake_state {
 | 
					
						
							| 
									
										
										
										
											2019-11-25 13:14:32 -08:00
										 |  |  |         assert_eq!(stake.delegation.stake, 1_000_000);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     } else {
 | 
					
						
							| 
									
										
										
										
											2020-05-15 17:35:43 +01:00
										 |  |  |         panic!("wrong account type found")
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 12:21:31 -08:00
										 |  |  |     loop {
 | 
					
						
							|  |  |  |         if warmed_up(&bank, &stake_pubkey) {
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         // Cycle thru banks until we're fully warmed up
 | 
					
						
							|  |  |  |         bank = next_epoch(&bank);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     // Reward redemption
 | 
					
						
							|  |  |  |     // Submit enough votes to generate rewards
 | 
					
						
							|  |  |  |     bank = fill_epoch_with_votes(&bank, &vote_keypair, &mint_keypair);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test that votes and credits are there
 | 
					
						
							|  |  |  |     let account = bank.get_account(&vote_pubkey).expect("account not found");
 | 
					
						
							| 
									
										
										
										
											2020-02-25 17:12:01 -08:00
										 |  |  |     let vote_state: VoteState = StateMut::<VoteStateVersions>::state(&account)
 | 
					
						
							|  |  |  |         .expect("couldn't unpack account data")
 | 
					
						
							|  |  |  |         .convert_to_current();
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 1 less vote, as the first vote should have cleared the lockout
 | 
					
						
							|  |  |  |     assert_eq!(vote_state.votes.len(), 31);
 | 
					
						
							| 
									
										
										
										
											2020-01-22 12:21:31 -08:00
										 |  |  |     // one vote per slot, might be more slots than 32 in the epoch
 | 
					
						
							|  |  |  |     assert!(vote_state.credits() >= 1);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     bank = fill_epoch_with_votes(&bank, &vote_keypair, &mint_keypair);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     let pre_staked = get_staked(&bank, &stake_pubkey);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 12:21:31 -08:00
										 |  |  |     // next epoch bank should pay rewards
 | 
					
						
							|  |  |  |     bank = next_epoch(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // Test that balance increased, and that the balance got staked
 | 
					
						
							|  |  |  |     let staked = get_staked(&bank, &stake_pubkey);
 | 
					
						
							|  |  |  |     let lamports = bank.get_balance(&stake_pubkey);
 | 
					
						
							|  |  |  |     assert!(staked > pre_staked);
 | 
					
						
							|  |  |  |     assert!(lamports > 1_000_000);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // split the stake
 | 
					
						
							|  |  |  |     let split_stake_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     let split_stake_pubkey = split_stake_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2020-01-22 12:21:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // Test split
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &stake_instruction::split(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							|  |  |  |             lamports / 2,
 | 
					
						
							|  |  |  |             &split_stake_pubkey,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &[&mint_keypair, &stake_keypair, &split_stake_keypair],
 | 
					
						
							|  |  |  |             message
 | 
					
						
							|  |  |  |         )
 | 
					
						
							|  |  |  |         .is_ok());
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // Deactivate the split
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &[stake_instruction::deactivate_stake(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &split_stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-08-09 12:55:21 -07:00
										 |  |  |         )],
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         .is_ok());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     let split_staked = get_staked(&bank, &split_stake_pubkey);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test that we cannot withdraw above what's staked
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &[stake_instruction::withdraw(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &split_stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:12:08 -07:00
										 |  |  |             &solana_sdk::pubkey::new_rand(),
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             lamports / 2 - split_staked + 1,
 | 
					
						
							| 
									
										
										
										
											2020-04-20 18:16:50 -06:00
										 |  |  |             None,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |         .is_err());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     let mut bank = next_epoch(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:59:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // assert we're still cooling down
 | 
					
						
							|  |  |  |     let split_staked = get_staked(&bank, &split_stake_pubkey);
 | 
					
						
							|  |  |  |     assert!(split_staked > 0);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // withdrawal in cooldown
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &[stake_instruction::withdraw(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &split_stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:12:08 -07:00
										 |  |  |             &solana_sdk::pubkey::new_rand(),
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             lamports / 2,
 | 
					
						
							| 
									
										
										
										
											2020-04-20 18:16:50 -06:00
										 |  |  |             None,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |         .is_err());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // but we can withdraw unstaked
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &[stake_instruction::withdraw(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &split_stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:12:08 -07:00
										 |  |  |             &solana_sdk::pubkey::new_rand(),
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             lamports / 2 - split_staked,
 | 
					
						
							| 
									
										
										
										
											2020-04-20 18:16:50 -06:00
										 |  |  |             None,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // assert we can withdraw unstaked tokens
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:57:18 -07:00
										 |  |  |         .is_ok());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // finish cooldown
 | 
					
						
							|  |  |  |     loop {
 | 
					
						
							|  |  |  |         if get_staked(&bank, &split_stake_pubkey) == 0 {
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         bank = next_epoch(&bank);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // Test that we can withdraw everything else out of the split
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &[stake_instruction::withdraw(
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             &split_stake_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:12:08 -07:00
										 |  |  |             &solana_sdk::pubkey::new_rand(),
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |             split_staked,
 | 
					
						
							| 
									
										
										
										
											2020-04-20 18:16:50 -06:00
										 |  |  |             None,
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert!(bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  |         .is_ok());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 15:31:17 -08:00
										 |  |  |     // verify all the math sums to zero
 | 
					
						
							|  |  |  |     assert_eq!(bank.get_balance(&split_stake_pubkey), 0);
 | 
					
						
							|  |  |  |     assert_eq!(bank.get_balance(&stake_pubkey), lamports - lamports / 2);
 | 
					
						
							| 
									
										
										
										
											2019-06-25 23:00:35 -07:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							|  |  |  | fn test_create_stake_account_from_seed() {
 | 
					
						
							|  |  |  |     let vote_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     let vote_pubkey = vote_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2020-03-19 01:58:52 -07:00
										 |  |  |     let identity_keypair = Keypair::new();
 | 
					
						
							|  |  |  |     let identity_pubkey = identity_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let GenesisConfigInfo {
 | 
					
						
							| 
									
										
										
										
											2020-04-27 21:05:12 -07:00
										 |  |  |         genesis_config,
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |         mint_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							| 
									
										
										
										
											2020-10-19 12:23:14 -07:00
										 |  |  |     } = create_genesis_config_with_leader(
 | 
					
						
							|  |  |  |         100_000_000_000,
 | 
					
						
							|  |  |  |         &solana_sdk::pubkey::new_rand(),
 | 
					
						
							|  |  |  |         1_000_000,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |     let bank = Bank::new(&genesis_config);
 | 
					
						
							|  |  |  |     let mint_pubkey = mint_keypair.pubkey();
 | 
					
						
							|  |  |  |     let bank = Arc::new(bank);
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let seed = "test-string";
 | 
					
						
							|  |  |  |     let stake_pubkey =
 | 
					
						
							| 
									
										
										
										
											2020-03-20 15:20:48 -07:00
										 |  |  |         Pubkey::create_with_seed(&mint_pubkey, seed, &solana_stake_program::id()).unwrap();
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Create Vote Account
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |     let message = Message::new(
 | 
					
						
							|  |  |  |         &vote_instruction::create_account(
 | 
					
						
							|  |  |  |             &mint_pubkey,
 | 
					
						
							|  |  |  |             &vote_pubkey,
 | 
					
						
							|  |  |  |             &VoteInit {
 | 
					
						
							|  |  |  |                 node_pubkey: identity_pubkey,
 | 
					
						
							|  |  |  |                 authorized_voter: vote_pubkey,
 | 
					
						
							|  |  |  |                 authorized_withdrawer: vote_pubkey,
 | 
					
						
							|  |  |  |                 commission: 50,
 | 
					
						
							|  |  |  |             },
 | 
					
						
							|  |  |  |             10,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |         .expect("failed to create vote account");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let authorized = stake_state::Authorized::auto(&mint_pubkey);
 | 
					
						
							|  |  |  |     // Create stake account and delegate to vote account
 | 
					
						
							|  |  |  |     let message = Message::new(
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:37:23 -07:00
										 |  |  |         &stake_instruction::create_account_with_seed_and_delegate_stake(
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |             &mint_pubkey,
 | 
					
						
							|  |  |  |             &stake_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-12-29 16:42:24 -08:00
										 |  |  |             &mint_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |             seed,
 | 
					
						
							|  |  |  |             &vote_pubkey,
 | 
					
						
							|  |  |  |             &authorized,
 | 
					
						
							|  |  |  |             &stake_state::Lockup::default(),
 | 
					
						
							|  |  |  |             1_000_000,
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							| 
									
										
										
										
											2020-06-24 14:52:38 -06:00
										 |  |  |         Some(&mint_pubkey),
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |     );
 | 
					
						
							|  |  |  |     bank_client
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:35:38 -06:00
										 |  |  |         .send_and_confirm_message(&[&mint_keypair], message)
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |         .expect("failed to create and delegate stake account");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test that correct lamports are staked
 | 
					
						
							|  |  |  |     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);
 | 
					
						
							|  |  |  |     } else {
 | 
					
						
							| 
									
										
										
										
											2020-05-15 17:35:43 +01:00
										 |  |  |         panic!("wrong account type found")
 | 
					
						
							| 
									
										
										
										
											2019-12-16 17:02:40 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 |