Factor out creating genesis with vote accounts into a utility function (#8315)

automerge
This commit is contained in:
carllin 2020-02-18 02:39:47 -08:00 committed by GitHub
parent a042ee609a
commit 73a278dc64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 50 deletions

View File

@ -473,7 +473,9 @@ pub mod test {
use solana_ledger::bank_forks::BankForks; use solana_ledger::bank_forks::BankForks;
use solana_runtime::{ use solana_runtime::{
bank::Bank, bank::Bank,
genesis_utils::{create_genesis_config, GenesisConfigInfo}, genesis_utils::{
create_genesis_config_with_vote_accounts, GenesisConfigInfo, ValidatorVoteKeypairs,
},
}; };
use solana_sdk::{ use solana_sdk::{
clock::Slot, clock::Slot,
@ -482,28 +484,12 @@ pub mod test {
signature::{Keypair, KeypairUtil}, signature::{Keypair, KeypairUtil},
transaction::Transaction, transaction::Transaction,
}; };
use solana_stake_program::stake_state;
use solana_vote_program::vote_state;
use solana_vote_program::{vote_instruction, vote_state::Vote}; use solana_vote_program::{vote_instruction, vote_state::Vote};
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::sync::RwLock; use std::sync::RwLock;
use std::{thread::sleep, time::Duration}; use std::{thread::sleep, time::Duration};
use trees::{tr, Node, Tree}; use trees::{tr, Node, Tree};
pub(crate) struct ValidatorKeypairs {
node_keypair: Keypair,
vote_keypair: Keypair,
}
impl ValidatorKeypairs {
pub(crate) fn new(node_keypair: Keypair, vote_keypair: Keypair) -> Self {
Self {
node_keypair,
vote_keypair,
}
}
}
pub(crate) struct VoteSimulator<'a> { pub(crate) struct VoteSimulator<'a> {
searchable_nodes: HashMap<u64, &'a Node<u64>>, searchable_nodes: HashMap<u64, &'a Node<u64>>,
} }
@ -521,8 +507,8 @@ pub mod test {
vote_slot: Slot, vote_slot: Slot,
bank_forks: &RwLock<BankForks>, bank_forks: &RwLock<BankForks>,
cluster_votes: &mut HashMap<Pubkey, Vec<u64>>, cluster_votes: &mut HashMap<Pubkey, Vec<u64>>,
validator_keypairs: &HashMap<Pubkey, ValidatorKeypairs>, validator_keypairs: &HashMap<Pubkey, ValidatorVoteKeypairs>,
my_keypairs: &ValidatorKeypairs, my_keypairs: &ValidatorVoteKeypairs,
progress: &mut HashMap<u64, ForkProgress>, progress: &mut HashMap<u64, ForkProgress>,
tower: &mut Tower, tower: &mut Tower,
) -> VoteResult { ) -> VoteResult {
@ -690,38 +676,18 @@ pub mod test {
// Setup BankForks with bank 0 and all the validator accounts // Setup BankForks with bank 0 and all the validator accounts
pub(crate) fn initialize_state( pub(crate) fn initialize_state(
validator_keypairs: &HashMap<Pubkey, ValidatorKeypairs>, validator_keypairs_map: &HashMap<Pubkey, ValidatorVoteKeypairs>,
) -> (BankForks, HashMap<u64, ForkProgress>) { ) -> (BankForks, HashMap<u64, ForkProgress>) {
let validator_keypairs: Vec<_> = validator_keypairs_map.values().collect();
let GenesisConfigInfo { let GenesisConfigInfo {
mut genesis_config, genesis_config,
mint_keypair, mint_keypair,
voting_keypair: _, voting_keypair: _,
} = create_genesis_config(1_000_000_000); } = create_genesis_config_with_vote_accounts(1_000_000_000, &validator_keypairs);
// Initialize BankForks
for keypairs in validator_keypairs.values() {
let node_pubkey = keypairs.node_keypair.pubkey();
let vote_pubkey = keypairs.vote_keypair.pubkey();
let stake_key = Pubkey::new_rand();
let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, 100);
let stake_account = stake_state::create_account(
&Pubkey::new_rand(),
&vote_pubkey,
&vote_account,
&genesis_config.rent,
100,
);
genesis_config.accounts.extend(vec![
(vote_pubkey, vote_account.clone()),
(stake_key, stake_account),
]);
}
let bank0 = Bank::new(&genesis_config); let bank0 = Bank::new(&genesis_config);
for pubkey in validator_keypairs.keys() { for pubkey in validator_keypairs_map.keys() {
bank0.transfer(10_000, &mint_keypair, pubkey).unwrap(); bank0.transfer(10_000, &mint_keypair, pubkey).unwrap();
} }
@ -756,7 +722,7 @@ pub mod test {
num_slots: u64, num_slots: u64,
bank_forks: &RwLock<BankForks>, bank_forks: &RwLock<BankForks>,
cluster_votes: &mut HashMap<Pubkey, Vec<u64>>, cluster_votes: &mut HashMap<Pubkey, Vec<u64>>,
keypairs: &HashMap<Pubkey, ValidatorKeypairs>, keypairs: &HashMap<Pubkey, ValidatorVoteKeypairs>,
progress: &mut HashMap<u64, ForkProgress>, progress: &mut HashMap<u64, ForkProgress>,
) -> bool { ) -> bool {
// Check that within some reasonable time, validator can make a new // Check that within some reasonable time, validator can make a new
@ -792,12 +758,13 @@ pub mod test {
fn test_simple_votes() { fn test_simple_votes() {
let node_keypair = Keypair::new(); let node_keypair = Keypair::new();
let vote_keypair = Keypair::new(); let vote_keypair = Keypair::new();
let stake_keypair = Keypair::new();
let node_pubkey = node_keypair.pubkey(); let node_pubkey = node_keypair.pubkey();
let mut keypairs = HashMap::new(); let mut keypairs = HashMap::new();
keypairs.insert( keypairs.insert(
node_pubkey, node_pubkey,
ValidatorKeypairs::new(node_keypair, vote_keypair), ValidatorVoteKeypairs::new(node_keypair, vote_keypair, stake_keypair),
); );
// Initialize BankForks // Initialize BankForks
@ -841,6 +808,7 @@ pub mod test {
solana_logger::setup(); solana_logger::setup();
let node_keypair = Keypair::new(); let node_keypair = Keypair::new();
let vote_keypair = Keypair::new(); let vote_keypair = Keypair::new();
let stake_keypair = Keypair::new();
let node_pubkey = node_keypair.pubkey(); let node_pubkey = node_keypair.pubkey();
let vote_pubkey = vote_keypair.pubkey(); let vote_pubkey = vote_keypair.pubkey();
@ -848,7 +816,7 @@ pub mod test {
info!("my_pubkey: {}", node_pubkey); info!("my_pubkey: {}", node_pubkey);
keypairs.insert( keypairs.insert(
node_pubkey, node_pubkey,
ValidatorKeypairs::new(node_keypair, vote_keypair), ValidatorVoteKeypairs::new(node_keypair, vote_keypair, stake_keypair),
); );
// Create the tree of banks in a BankForks object // Create the tree of banks in a BankForks object

View File

@ -1055,7 +1055,7 @@ pub(crate) mod tests {
use super::*; use super::*;
use crate::{ use crate::{
commitment::BlockCommitment, commitment::BlockCommitment,
consensus::test::{initialize_state, ValidatorKeypairs, VoteResult, VoteSimulator}, consensus::test::{initialize_state, VoteResult, VoteSimulator},
consensus::Tower, consensus::Tower,
genesis_utils::{create_genesis_config, create_genesis_config_with_leader}, genesis_utils::{create_genesis_config, create_genesis_config_with_leader},
replay_stage::ReplayStage, replay_stage::ReplayStage,
@ -1075,7 +1075,7 @@ pub(crate) mod tests {
SIZE_OF_COMMON_SHRED_HEADER, SIZE_OF_DATA_SHRED_HEADER, SIZE_OF_DATA_SHRED_PAYLOAD, SIZE_OF_COMMON_SHRED_HEADER, SIZE_OF_DATA_SHRED_HEADER, SIZE_OF_DATA_SHRED_PAYLOAD,
}, },
}; };
use solana_runtime::genesis_utils::GenesisConfigInfo; use solana_runtime::genesis_utils::{GenesisConfigInfo, ValidatorVoteKeypairs};
use solana_sdk::{ use solana_sdk::{
account::Account, account::Account,
hash::{hash, Hash}, hash::{hash, Hash},
@ -1918,11 +1918,12 @@ pub(crate) mod tests {
fn test_child_bank_heavier() { fn test_child_bank_heavier() {
let node_keypair = Keypair::new(); let node_keypair = Keypair::new();
let vote_keypair = Keypair::new(); let vote_keypair = Keypair::new();
let stake_keypair = Keypair::new();
let node_pubkey = node_keypair.pubkey(); let node_pubkey = node_keypair.pubkey();
let mut keypairs = HashMap::new(); let mut keypairs = HashMap::new();
keypairs.insert( keypairs.insert(
node_pubkey, node_pubkey,
ValidatorKeypairs::new(node_keypair, vote_keypair), ValidatorVoteKeypairs::new(node_keypair, vote_keypair, stake_keypair),
); );
let (bank_forks, mut progress) = initialize_state(&keypairs); let (bank_forks, mut progress) = initialize_state(&keypairs);

View File

@ -9,10 +9,27 @@ use solana_sdk::{
}; };
use solana_stake_program::stake_state; use solana_stake_program::stake_state;
use solana_vote_program::vote_state; use solana_vote_program::vote_state;
use std::borrow::Borrow;
// The default stake placed with the bootstrap validator // The default stake placed with the bootstrap validator
pub const BOOTSTRAP_VALIDATOR_LAMPORTS: u64 = 42; pub const BOOTSTRAP_VALIDATOR_LAMPORTS: u64 = 42;
pub struct ValidatorVoteKeypairs {
pub node_keypair: Keypair,
pub vote_keypair: Keypair,
pub stake_keypair: Keypair,
}
impl ValidatorVoteKeypairs {
pub fn new(node_keypair: Keypair, vote_keypair: Keypair, stake_keypair: Keypair) -> Self {
Self {
node_keypair,
vote_keypair,
stake_keypair,
}
}
}
pub struct GenesisConfigInfo { pub struct GenesisConfigInfo {
pub genesis_config: GenesisConfig, pub genesis_config: GenesisConfig,
pub mint_keypair: Keypair, pub mint_keypair: Keypair,
@ -23,6 +40,36 @@ pub fn create_genesis_config(mint_lamports: u64) -> GenesisConfigInfo {
create_genesis_config_with_leader(mint_lamports, &Pubkey::new_rand(), 0) create_genesis_config_with_leader(mint_lamports, &Pubkey::new_rand(), 0)
} }
pub fn create_genesis_config_with_vote_accounts(
mint_lamports: u64,
voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
) -> GenesisConfigInfo {
let mut genesis_config_info = create_genesis_config(mint_lamports);
for validator_voting_keypairs in voting_keypairs {
let node_pubkey = validator_voting_keypairs.borrow().node_keypair.pubkey();
let vote_pubkey = validator_voting_keypairs.borrow().vote_keypair.pubkey();
let stake_pubkey = validator_voting_keypairs.borrow().stake_keypair.pubkey();
// Create accounts
let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, 100);
let stake_account = stake_state::create_account(
&stake_pubkey,
&vote_pubkey,
&vote_account,
&genesis_config_info.genesis_config.rent,
100,
);
// Put newly created accounts into genesis
genesis_config_info.genesis_config.accounts.extend(vec![
(vote_pubkey, vote_account.clone()),
(stake_pubkey, stake_account),
]);
}
genesis_config_info
}
pub fn create_genesis_config_with_leader( pub fn create_genesis_config_with_leader(
mint_lamports: u64, mint_lamports: u64,
bootstrap_validator_pubkey: &Pubkey, bootstrap_validator_pubkey: &Pubkey,