Cleanup test utilities (#11723)

* Add voting utility

* Add blockstore utility

Co-authored-by: Carl <carl@solana.com>
This commit is contained in:
carllin
2020-08-19 22:04:38 -07:00
committed by GitHub
parent cf941b0eda
commit b7ed06b17a
9 changed files with 142 additions and 87 deletions

View File

@ -246,10 +246,7 @@ mod tests {
bank_forks::BankForks, bank_forks::BankForks,
genesis_utils::{create_genesis_config_with_vote_accounts, ValidatorVoteKeypairs}, genesis_utils::{create_genesis_config_with_vote_accounts, ValidatorVoteKeypairs},
}; };
use solana_sdk::{ use solana_sdk::{pubkey::Pubkey, signature::Signer};
pubkey::Pubkey,
signature::{Keypair, Signer},
};
use solana_stake_program::stake_state; use solana_stake_program::stake_state;
use solana_vote_program::{ use solana_vote_program::{
vote_state::{self, VoteStateVersions}, vote_state::{self, VoteStateVersions},
@ -488,14 +485,8 @@ mod tests {
let block_commitment_cache = RwLock::new(BlockCommitmentCache::new_for_tests()); let block_commitment_cache = RwLock::new(BlockCommitmentCache::new_for_tests());
let node_keypair = Arc::new(Keypair::new()); let validator_vote_keypairs = ValidatorVoteKeypairs::new_rand();
let vote_keypair = Arc::new(Keypair::new()); let validator_keypairs = vec![&validator_vote_keypairs];
let stake_keypair = Arc::new(Keypair::new());
let validator_keypairs = vec![ValidatorVoteKeypairs {
node_keypair: node_keypair.clone(),
vote_keypair: vote_keypair.clone(),
stake_keypair,
}];
let GenesisConfigInfo { let GenesisConfigInfo {
genesis_config, genesis_config,
mint_keypair: _, mint_keypair: _,
@ -518,9 +509,9 @@ mod tests {
vec![x], vec![x],
previous_bank.hash(), previous_bank.hash(),
previous_bank.last_blockhash(), previous_bank.last_blockhash(),
&node_keypair, &validator_vote_keypairs.node_keypair,
&vote_keypair, &validator_vote_keypairs.vote_keypair,
&vote_keypair, &validator_vote_keypairs.vote_keypair,
None, None,
); );
bank.process_transaction(&vote).unwrap(); bank.process_transaction(&vote).unwrap();
@ -528,7 +519,10 @@ mod tests {
} }
let working_bank = bank_forks.working_bank(); let working_bank = bank_forks.working_bank();
let root = get_vote_account_root_slot(vote_keypair.pubkey(), &working_bank); let root = get_vote_account_root_slot(
validator_vote_keypairs.vote_keypair.pubkey(),
&working_bank,
);
for x in 0..root { for x in 0..root {
bank_forks.set_root(x, &None, None); bank_forks.set_root(x, &None, None);
} }
@ -540,16 +534,19 @@ mod tests {
vec![33], vec![33],
bank33.hash(), bank33.hash(),
bank33.last_blockhash(), bank33.last_blockhash(),
&node_keypair, &validator_vote_keypairs.node_keypair,
&vote_keypair, &validator_vote_keypairs.vote_keypair,
&vote_keypair, &validator_vote_keypairs.vote_keypair,
None, None,
); );
bank34.process_transaction(&vote33).unwrap(); bank34.process_transaction(&vote33).unwrap();
bank_forks.insert(bank34); bank_forks.insert(bank34);
let working_bank = bank_forks.working_bank(); let working_bank = bank_forks.working_bank();
let root = get_vote_account_root_slot(vote_keypair.pubkey(), &working_bank); let root = get_vote_account_root_slot(
validator_vote_keypairs.vote_keypair.pubkey(),
&working_bank,
);
let ancestors = working_bank.status_cache_ancestors(); let ancestors = working_bank.status_cache_ancestors();
let _ = AggregateCommitmentService::update_commitment_cache( let _ = AggregateCommitmentService::update_commitment_cache(
&block_commitment_cache, &block_commitment_cache,
@ -601,9 +598,9 @@ mod tests {
vec![x], vec![x],
previous_bank.hash(), previous_bank.hash(),
previous_bank.last_blockhash(), previous_bank.last_blockhash(),
&node_keypair, &validator_vote_keypairs.node_keypair,
&vote_keypair, &validator_vote_keypairs.vote_keypair,
&vote_keypair, &validator_vote_keypairs.vote_keypair,
None, None,
); );
bank.process_transaction(&vote).unwrap(); bank.process_transaction(&vote).unwrap();
@ -611,7 +608,10 @@ mod tests {
} }
let working_bank = bank_forks.working_bank(); let working_bank = bank_forks.working_bank();
let root = get_vote_account_root_slot(vote_keypair.pubkey(), &working_bank); let root = get_vote_account_root_slot(
validator_vote_keypairs.vote_keypair.pubkey(),
&working_bank,
);
let ancestors = working_bank.status_cache_ancestors(); let ancestors = working_bank.status_cache_ancestors();
let _ = AggregateCommitmentService::update_commitment_cache( let _ = AggregateCommitmentService::update_commitment_cache(
&block_commitment_cache, &block_commitment_cache,

View File

@ -511,6 +511,7 @@ mod test {
use super::*; use super::*;
use solana_ledger::{blockstore::Blockstore, get_tmp_ledger_path}; use solana_ledger::{blockstore::Blockstore, get_tmp_ledger_path};
use solana_runtime::{bank::Bank, bank_utils}; use solana_runtime::{bank::Bank, bank_utils};
use solana_sdk::hash::Hash;
use trees::tr; use trees::tr;
#[test] #[test]
@ -658,7 +659,7 @@ mod test {
assert_eq!(repair_weight.trees.get(&8).unwrap().ancestors(10), vec![8]); assert_eq!(repair_weight.trees.get(&8).unwrap().ancestors(10), vec![8]);
// Connect orphan back to main fork // Connect orphan back to main fork
blockstore.add_tree(tr(6) / (tr(8)), true, true); blockstore.add_tree(tr(6) / (tr(8)), true, true, 2, Hash::default());
assert_eq!( assert_eq!(
AncestorIterator::new(8, &blockstore).collect::<Vec<_>>(), AncestorIterator::new(8, &blockstore).collect::<Vec<_>>(),
vec![6, 5, 3, 1, 0] vec![6, 5, 3, 1, 0]
@ -742,8 +743,8 @@ mod test {
assert!(repair_weight.trees.contains_key(&20)); assert!(repair_weight.trees.contains_key(&20));
// Resolve orphans in blockstore // Resolve orphans in blockstore
blockstore.add_tree(tr(6) / (tr(8)), true, true); blockstore.add_tree(tr(6) / (tr(8)), true, true, 2, Hash::default());
blockstore.add_tree(tr(11) / (tr(20)), true, true); blockstore.add_tree(tr(11) / (tr(20)), true, true, 2, Hash::default());
// Call `update_orphan_ancestors` to resolve orphan // Call `update_orphan_ancestors` to resolve orphan
repair_weight.update_orphan_ancestors( repair_weight.update_orphan_ancestors(
&blockstore, &blockstore,
@ -853,8 +854,8 @@ mod test {
// Resolve the orphans, should now return no // Resolve the orphans, should now return no
// orphans // orphans
repairs = vec![]; repairs = vec![];
blockstore.add_tree(tr(6) / (tr(8)), true, true); blockstore.add_tree(tr(6) / (tr(8)), true, true, 2, Hash::default());
blockstore.add_tree(tr(11) / (tr(20)), true, true); blockstore.add_tree(tr(11) / (tr(20)), true, true, 2, Hash::default());
repair_weight.get_best_orphans( repair_weight.get_best_orphans(
&blockstore, &blockstore,
&mut repairs, &mut repairs,
@ -889,7 +890,7 @@ mod test {
// orphan in the `trees` map, we should search for // orphan in the `trees` map, we should search for
// exactly one more of the remaining two // exactly one more of the remaining two
let mut repairs = vec![]; let mut repairs = vec![];
blockstore.add_tree(tr(100) / (tr(101)), true, true); blockstore.add_tree(tr(100) / (tr(101)), true, true, 2, Hash::default());
repair_weight.get_best_orphans( repair_weight.get_best_orphans(
&blockstore, &blockstore,
&mut repairs, &mut repairs,
@ -991,7 +992,7 @@ mod test {
// Chain orphan 8 back to the main fork, but don't // Chain orphan 8 back to the main fork, but don't
// touch orphan 20 // touch orphan 20
blockstore.add_tree(tr(4) / (tr(8)), true, true); blockstore.add_tree(tr(4) / (tr(8)), true, true, 2, Hash::default());
// Call `update_orphan_ancestors` to resolve orphan // Call `update_orphan_ancestors` to resolve orphan
repair_weight.update_orphan_ancestors( repair_weight.update_orphan_ancestors(
@ -1061,10 +1062,10 @@ mod test {
} }
// Chain orphan 8 back to slot `old_parent` // Chain orphan 8 back to slot `old_parent`
blockstore.add_tree(tr(*old_parent) / (tr(8)), true, true); blockstore.add_tree(tr(*old_parent) / (tr(8)), true, true, 2, Hash::default());
// Chain orphan 20 back to orphan 8 // Chain orphan 20 back to orphan 8
blockstore.add_tree(tr(8) / (tr(20)), true, true); blockstore.add_tree(tr(8) / (tr(20)), true, true, 2, Hash::default());
// Call `update_orphan_ancestors` to resolve orphan // Call `update_orphan_ancestors` to resolve orphan
repair_weight.update_orphan_ancestors( repair_weight.update_orphan_ancestors(
@ -1089,7 +1090,13 @@ mod test {
// Add a vote that chains back to `old_parent`, should be purged // Add a vote that chains back to `old_parent`, should be purged
let new_vote_slot = 100; let new_vote_slot = 100;
blockstore.add_tree(tr(*old_parent) / tr(new_vote_slot), true, true); blockstore.add_tree(
tr(*old_parent) / tr(new_vote_slot),
true,
true,
2,
Hash::default(),
);
repair_weight.add_votes( repair_weight.add_votes(
&blockstore, &blockstore,
vec![(new_vote_slot, vec![Pubkey::default()])].into_iter(), vec![(new_vote_slot, vec![Pubkey::default()])].into_iter(),
@ -1137,7 +1144,7 @@ mod test {
); );
// Ancestors of slot 31 are [30], with no existing subtree // Ancestors of slot 31 are [30], with no existing subtree
blockstore.add_tree(tr(30) / (tr(31)), true, true); blockstore.add_tree(tr(30) / (tr(31)), true, true, 2, Hash::default());
assert_eq!( assert_eq!(
repair_weight.find_ancestor_subtree_of_slot(&blockstore, 31), repair_weight.find_ancestor_subtree_of_slot(&blockstore, 31),
(vec![30].into_iter().collect::<VecDeque<_>>(), None) (vec![30].into_iter().collect::<VecDeque<_>>(), None)
@ -1155,7 +1162,7 @@ mod test {
// Chain orphan 8 back to slot 4 on a different fork, ancestor search // Chain orphan 8 back to slot 4 on a different fork, ancestor search
// should not return ancestors earlier than the root // should not return ancestors earlier than the root
blockstore.add_tree(tr(4) / (tr(8)), true, true); blockstore.add_tree(tr(4) / (tr(8)), true, true, 2, Hash::default());
assert_eq!( assert_eq!(
repair_weight.find_ancestor_subtree_of_slot(&blockstore, 8), repair_weight.find_ancestor_subtree_of_slot(&blockstore, 8),
(vec![4].into_iter().collect::<VecDeque<_>>(), None) (vec![4].into_iter().collect::<VecDeque<_>>(), None)
@ -1242,8 +1249,8 @@ mod test {
*/ */
let blockstore = setup_forks(); let blockstore = setup_forks();
blockstore.add_tree(tr(8) / (tr(10) / (tr(11))), true, true); blockstore.add_tree(tr(8) / (tr(10) / (tr(11))), true, true, 2, Hash::default());
blockstore.add_tree(tr(20) / (tr(22) / (tr(23))), true, true); blockstore.add_tree(tr(20) / (tr(22) / (tr(23))), true, true, 2, Hash::default());
assert!(blockstore.orphan(8).unwrap().is_some()); assert!(blockstore.orphan(8).unwrap().is_some());
blockstore blockstore
} }
@ -1265,7 +1272,7 @@ mod test {
let forks = tr(0) / (tr(1) / (tr(2) / (tr(4))) / (tr(3) / (tr(5) / (tr(6))))); let forks = tr(0) / (tr(1) / (tr(2) / (tr(4))) / (tr(3) / (tr(5) / (tr(6)))));
let ledger_path = get_tmp_ledger_path!(); let ledger_path = get_tmp_ledger_path!();
let blockstore = Blockstore::open(&ledger_path).unwrap(); let blockstore = Blockstore::open(&ledger_path).unwrap();
blockstore.add_tree(forks, false, true); blockstore.add_tree(forks, false, true, 2, Hash::default());
blockstore blockstore
} }
} }

View File

@ -150,6 +150,7 @@ pub mod test {
use super::*; use super::*;
use solana_ledger::{get_tmp_ledger_path, shred::Shred}; use solana_ledger::{get_tmp_ledger_path, shred::Shred};
use solana_runtime::bank_utils; use solana_runtime::bank_utils;
use solana_sdk::hash::Hash;
use trees::tr; use trees::tr;
#[test] #[test]
@ -246,7 +247,13 @@ pub mod test {
repairs = vec![]; repairs = vec![];
let best_overall_slot = heaviest_subtree_fork_choice.best_overall_slot(); let best_overall_slot = heaviest_subtree_fork_choice.best_overall_slot();
assert_eq!(heaviest_subtree_fork_choice.best_overall_slot(), 4); assert_eq!(heaviest_subtree_fork_choice.best_overall_slot(), 4);
blockstore.add_tree(tr(best_overall_slot) / (tr(6) / tr(7)), true, false); blockstore.add_tree(
tr(best_overall_slot) / (tr(6) / tr(7)),
true,
false,
2,
Hash::default(),
);
get_best_repair_shreds( get_best_repair_shreds(
&heaviest_subtree_fork_choice, &heaviest_subtree_fork_choice,
&blockstore, &blockstore,
@ -300,7 +307,7 @@ pub mod test {
// Adding incomplete children with higher weighted parents, even if // Adding incomplete children with higher weighted parents, even if
// the parents are complete should still be repaired // the parents are complete should still be repaired
repairs = vec![]; repairs = vec![];
blockstore.add_tree(tr(2) / (tr(8)), true, false); blockstore.add_tree(tr(2) / (tr(8)), true, false, 2, Hash::default());
get_best_repair_shreds( get_best_repair_shreds(
&heaviest_subtree_fork_choice, &heaviest_subtree_fork_choice,
&blockstore, &blockstore,
@ -322,7 +329,7 @@ pub mod test {
let (blockstore, heaviest_subtree_fork_choice) = setup_forks(); let (blockstore, heaviest_subtree_fork_choice) = setup_forks();
// Add a branch to slot 2, make sure it doesn't repair child // Add a branch to slot 2, make sure it doesn't repair child
// 4 again when the Unvisited(2) event happens // 4 again when the Unvisited(2) event happens
blockstore.add_tree(tr(2) / (tr(6) / tr(7)), true, false); blockstore.add_tree(tr(2) / (tr(6) / tr(7)), true, false, 2, Hash::default());
let mut repairs = vec![]; let mut repairs = vec![];
get_best_repair_shreds( get_best_repair_shreds(
&heaviest_subtree_fork_choice, &heaviest_subtree_fork_choice,
@ -368,7 +375,7 @@ pub mod test {
// Adding slot 2 to ignore should not remove its unexplored children from // Adding slot 2 to ignore should not remove its unexplored children from
// the repair set // the repair set
repairs = vec![]; repairs = vec![];
blockstore.add_tree(tr(2) / (tr(6) / tr(7)), true, false); blockstore.add_tree(tr(2) / (tr(6) / tr(7)), true, false, 2, Hash::default());
ignore_set.insert(2); ignore_set.insert(2);
get_best_repair_shreds( get_best_repair_shreds(
&heaviest_subtree_fork_choice, &heaviest_subtree_fork_choice,
@ -420,7 +427,7 @@ pub mod test {
let forks = tr(0) / (tr(1) / (tr(2) / (tr(4))) / (tr(3) / (tr(5)))); let forks = tr(0) / (tr(1) / (tr(2) / (tr(4))) / (tr(3) / (tr(5))));
let ledger_path = get_tmp_ledger_path!(); let ledger_path = get_tmp_ledger_path!();
let blockstore = Blockstore::open(&ledger_path).unwrap(); let blockstore = Blockstore::open(&ledger_path).unwrap();
blockstore.add_tree(forks.clone(), false, false); blockstore.add_tree(forks.clone(), false, false, 2, Hash::default());
(blockstore, HeaviestSubtreeForkChoice::new_from_tree(forks)) (blockstore, HeaviestSubtreeForkChoice::new_from_tree(forks))
} }

View File

@ -4035,7 +4035,7 @@ pub mod tests {
( (
Arc::new(RwLock::new(BankForks::new(bank))), Arc::new(RwLock::new(BankForks::new(bank))),
mint_keypair, mint_keypair,
voting_keypair, Arc::new(voting_keypair),
) )
} }

View File

@ -863,8 +863,8 @@ impl TestValidator {
} = create_genesis_config_with_leader_ex( } = create_genesis_config_with_leader_ex(
mint_lamports, mint_lamports,
&contact_info.id, &contact_info.id,
Arc::new(Keypair::new()), &Keypair::new(),
Arc::new(Keypair::new()), &Pubkey::new_rand(),
42, 42,
bootstrap_validator_lamports, bootstrap_validator_lamports,
); );
@ -882,12 +882,13 @@ impl TestValidator {
)), )),
..ValidatorConfig::default() ..ValidatorConfig::default()
}; };
let vote_pubkey = voting_keypair.pubkey();
let node = Validator::new( let node = Validator::new(
node, node,
&node_keypair, &node_keypair,
&ledger_path, &ledger_path,
&voting_keypair.pubkey(), &voting_keypair.pubkey(),
vec![voting_keypair.clone()], vec![Arc::new(voting_keypair)],
None, None,
true, true,
&config, &config,
@ -899,7 +900,7 @@ impl TestValidator {
alice: mint_keypair, alice: mint_keypair,
ledger_path, ledger_path,
genesis_hash: blockhash, genesis_hash: blockhash,
vote_pubkey: voting_keypair.pubkey(), vote_pubkey,
} }
} }
} }
@ -1117,7 +1118,7 @@ mod tests {
.genesis_config; .genesis_config;
let (validator_ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_config); let (validator_ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_config);
ledger_paths.push(validator_ledger_path.clone()); ledger_paths.push(validator_ledger_path.clone());
let vote_account_keypair = Arc::new(Keypair::new()); let vote_account_keypair = Keypair::new();
let config = ValidatorConfig { let config = ValidatorConfig {
rpc_ports: Some(( rpc_ports: Some((
validator_node.info.rpc.port(), validator_node.info.rpc.port(),
@ -1131,7 +1132,7 @@ mod tests {
&Arc::new(validator_keypair), &Arc::new(validator_keypair),
&validator_ledger_path, &validator_ledger_path,
&vote_account_keypair.pubkey(), &vote_account_keypair.pubkey(),
vec![vote_account_keypair.clone()], vec![Arc::new(vote_account_keypair)],
Some(&leader_node.info), Some(&leader_node.info),
true, true,
&config, &config,

View File

@ -233,7 +233,9 @@ pub fn cluster_info_scale() {
let nodes: Vec<_> = vote_keypairs let nodes: Vec<_> = vote_keypairs
.into_iter() .into_iter()
.map(|keypairs| test_node_with_bank(keypairs.node_keypair, &exit, bank_forks.clone())) .map(|keypairs| {
test_node_with_bank(Arc::new(keypairs.node_keypair), &exit, bank_forks.clone())
})
.collect(); .collect();
let ci0 = nodes[0].0.my_contact_info(); let ci0 = nodes[0].0.my_contact_info();
for node in &nodes[1..] { for node in &nodes[1..] {

View File

@ -350,18 +350,39 @@ impl Blockstore {
Ok((blockstore, signal_receiver, completed_slots_receiver)) Ok((blockstore, signal_receiver, completed_slots_receiver))
} }
pub fn add_tree(&self, forks: Tree<Slot>, is_orphan: bool, is_slot_complete: bool) { pub fn add_tree(
&self,
forks: Tree<Slot>,
is_orphan: bool,
is_slot_complete: bool,
num_ticks: u64,
starting_hash: Hash,
) {
let mut walk = TreeWalk::from(forks); let mut walk = TreeWalk::from(forks);
let mut blockhashes = HashMap::new();
while let Some(visit) = walk.get() { while let Some(visit) = walk.get() {
let slot = visit.node().data; let slot = visit.node().data;
if self.meta(slot).unwrap().is_some() && self.orphan(slot).unwrap().is_none() { if self.meta(slot).unwrap().is_some() && self.orphan(slot).unwrap().is_none() {
// If slot exists and is not an orphan, then skip it // If slot exists in blockstore and is not an orphan, then skip it
walk.forward(); walk.forward();
continue; continue;
} }
let parent = walk.get_parent().map(|n| n.data); let parent = walk.get_parent().map(|n| n.data);
if parent.is_some() || !is_orphan { if parent.is_some() || !is_orphan {
let entries = create_ticks(2, 0, Hash::default()); let parent_hash = parent
// parent won't exist for first node in a tree where
// `is_orphan == true`
.and_then(|parent| blockhashes.get(&parent))
.unwrap_or(&starting_hash);
let mut entries = create_ticks(
num_ticks * (std::cmp::max(1, slot - parent.unwrap_or(slot))),
0,
*parent_hash,
);
blockhashes.insert(slot, entries.last().unwrap().hash);
if !is_slot_complete {
entries.pop().unwrap();
}
let shreds = entries_to_test_shreds( let shreds = entries_to_test_shreds(
entries.clone(), entries.clone(),
slot, slot,
@ -407,6 +428,16 @@ impl Blockstore {
self.orphans_cf.get(slot) self.orphans_cf.get(slot)
} }
// Get max root or 0 if it doesn't exist
pub fn max_root(&self) -> Slot {
self.db
.iter::<cf::Root>(IteratorMode::End)
.expect("Couldn't get rooted iterator for max_root()")
.next()
.map(|(slot, _)| slot)
.unwrap_or(0)
}
pub fn slot_meta_iterator<'a>( pub fn slot_meta_iterator<'a>(
&'a self, &'a self,
slot: Slot, slot: Slot,

View File

@ -135,9 +135,10 @@ impl LocalCluster {
if *in_genesis { if *in_genesis {
Some(( Some((
ValidatorVoteKeypairs { ValidatorVoteKeypairs {
node_keypair: node_keypair.clone(), node_keypair: Keypair::from_bytes(&node_keypair.to_bytes())
vote_keypair: Arc::new(Keypair::new()), .unwrap(),
stake_keypair: Arc::new(Keypair::new()), vote_keypair: Keypair::new(),
stake_keypair: Keypair::new(),
}, },
stake, stake,
)) ))
@ -205,9 +206,12 @@ impl LocalCluster {
leader_node.info.rpc_banks.port(), leader_node.info.rpc_banks.port(),
)); ));
leader_config.account_paths = vec![leader_ledger_path.join("accounts")]; leader_config.account_paths = vec![leader_ledger_path.join("accounts")];
let leader_keypair = Arc::new(Keypair::from_bytes(&leader_keypair.to_bytes()).unwrap());
let leader_vote_keypair =
Arc::new(Keypair::from_bytes(&leader_vote_keypair.to_bytes()).unwrap());
let leader_server = Validator::new( let leader_server = Validator::new(
leader_node, leader_node,
leader_keypair, &leader_keypair,
&leader_ledger_path, &leader_ledger_path,
&leader_vote_keypair.pubkey(), &leader_vote_keypair.pubkey(),
vec![leader_vote_keypair.clone()], vec![leader_vote_keypair.clone()],
@ -218,8 +222,8 @@ impl LocalCluster {
let mut validators = HashMap::new(); let mut validators = HashMap::new();
let leader_info = ValidatorInfo { let leader_info = ValidatorInfo {
keypair: leader_keypair.clone(), keypair: leader_keypair,
voting_keypair: leader_vote_keypair.clone(), voting_keypair: leader_vote_keypair,
ledger_path: leader_ledger_path, ledger_path: leader_ledger_path,
contact_info: leader_contact_info.clone(), contact_info: leader_contact_info.clone(),
}; };
@ -241,7 +245,12 @@ impl LocalCluster {
let node_pubkey_to_vote_key: HashMap<Pubkey, Arc<Keypair>> = keys_in_genesis let node_pubkey_to_vote_key: HashMap<Pubkey, Arc<Keypair>> = keys_in_genesis
.into_iter() .into_iter()
.map(|keypairs| (keypairs.node_keypair.pubkey(), keypairs.vote_keypair)) .map(|keypairs| {
(
keypairs.node_keypair.pubkey(),
Arc::new(Keypair::from_bytes(&keypairs.vote_keypair.to_bytes()).unwrap()),
)
})
.collect(); .collect();
for (stake, validator_config, (key, _)) in izip!( for (stake, validator_config, (key, _)) in izip!(
(&config.node_stakes[1..]).iter(), (&config.node_stakes[1..]).iter(),

View File

@ -9,23 +9,19 @@ 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, sync::Arc}; 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 struct ValidatorVoteKeypairs {
pub node_keypair: Arc<Keypair>, pub node_keypair: Keypair,
pub vote_keypair: Arc<Keypair>, pub vote_keypair: Keypair,
pub stake_keypair: Arc<Keypair>, pub stake_keypair: Keypair,
} }
impl ValidatorVoteKeypairs { impl ValidatorVoteKeypairs {
pub fn new( pub fn new(node_keypair: Keypair, vote_keypair: Keypair, stake_keypair: Keypair) -> Self {
node_keypair: Arc<Keypair>,
vote_keypair: Arc<Keypair>,
stake_keypair: Arc<Keypair>,
) -> Self {
Self { Self {
node_keypair, node_keypair,
vote_keypair, vote_keypair,
@ -35,9 +31,9 @@ impl ValidatorVoteKeypairs {
pub fn new_rand() -> Self { pub fn new_rand() -> Self {
Self { Self {
node_keypair: Arc::new(Keypair::new()), node_keypair: Keypair::new(),
vote_keypair: Arc::new(Keypair::new()), vote_keypair: Keypair::new(),
stake_keypair: Arc::new(Keypair::new()), stake_keypair: Keypair::new(),
} }
} }
} }
@ -45,7 +41,7 @@ impl ValidatorVoteKeypairs {
pub struct GenesisConfigInfo { pub struct GenesisConfigInfo {
pub genesis_config: GenesisConfig, pub genesis_config: GenesisConfig,
pub mint_keypair: Keypair, pub mint_keypair: Keypair,
pub voting_keypair: Arc<Keypair>, pub voting_keypair: Keypair,
} }
pub fn create_genesis_config(mint_lamports: u64) -> GenesisConfigInfo { pub fn create_genesis_config(mint_lamports: u64) -> GenesisConfigInfo {
@ -57,31 +53,32 @@ pub fn create_genesis_config_with_vote_accounts(
voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>], voting_keypairs: &[impl Borrow<ValidatorVoteKeypairs>],
stakes: Vec<u64>, stakes: Vec<u64>,
) -> GenesisConfigInfo { ) -> GenesisConfigInfo {
assert!(!voting_keypairs.is_empty());
assert_eq!(voting_keypairs.len(), stakes.len()); assert_eq!(voting_keypairs.len(), stakes.len());
let mut genesis_config_info = create_genesis_config_with_leader_ex( let mut genesis_config_info = create_genesis_config_with_leader_ex(
mint_lamports, mint_lamports,
&voting_keypairs[0].borrow().node_keypair.pubkey(), &voting_keypairs[0].borrow().node_keypair.pubkey(),
voting_keypairs[0].borrow().vote_keypair.clone(), &voting_keypairs[0].borrow().vote_keypair,
voting_keypairs[0].borrow().stake_keypair.clone(), &voting_keypairs[0].borrow().stake_keypair.pubkey(),
stakes[0], stakes[0],
BOOTSTRAP_VALIDATOR_LAMPORTS, BOOTSTRAP_VALIDATOR_LAMPORTS,
); );
for (validator_voting_keypairs, stake) in voting_keypairs.iter().zip(stakes) { for (validator_voting_keypairs, stake) in voting_keypairs[1..].iter().zip(&stakes[1..]) {
let node_pubkey = validator_voting_keypairs.borrow().node_keypair.pubkey(); let node_pubkey = validator_voting_keypairs.borrow().node_keypair.pubkey();
let vote_pubkey = validator_voting_keypairs.borrow().vote_keypair.pubkey(); let vote_pubkey = validator_voting_keypairs.borrow().vote_keypair.pubkey();
let stake_pubkey = validator_voting_keypairs.borrow().stake_keypair.pubkey(); let stake_pubkey = validator_voting_keypairs.borrow().stake_keypair.pubkey();
// Create accounts // Create accounts
let node_account = Account::new(BOOTSTRAP_VALIDATOR_LAMPORTS, 0, &system_program::id()); let node_account = Account::new(BOOTSTRAP_VALIDATOR_LAMPORTS, 0, &system_program::id());
let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, stake); let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, *stake);
let stake_account = stake_state::create_account( let stake_account = stake_state::create_account(
&stake_pubkey, &stake_pubkey,
&vote_pubkey, &vote_pubkey,
&vote_account, &vote_account,
&genesis_config_info.genesis_config.rent, &genesis_config_info.genesis_config.rent,
stake, *stake,
); );
// Put newly created accounts into genesis // Put newly created accounts into genesis
@ -103,8 +100,8 @@ pub fn create_genesis_config_with_leader(
create_genesis_config_with_leader_ex( create_genesis_config_with_leader_ex(
mint_lamports, mint_lamports,
bootstrap_validator_pubkey, bootstrap_validator_pubkey,
Arc::new(Keypair::new()), &Keypair::new(),
Arc::new(Keypair::new()), &Pubkey::new_rand(),
bootstrap_validator_stake_lamports, bootstrap_validator_stake_lamports,
BOOTSTRAP_VALIDATOR_LAMPORTS, BOOTSTRAP_VALIDATOR_LAMPORTS,
) )
@ -113,8 +110,8 @@ pub fn create_genesis_config_with_leader(
pub fn create_genesis_config_with_leader_ex( pub fn create_genesis_config_with_leader_ex(
mint_lamports: u64, mint_lamports: u64,
bootstrap_validator_pubkey: &Pubkey, bootstrap_validator_pubkey: &Pubkey,
bootstrap_validator_voting_keypair: Arc<Keypair>, bootstrap_validator_voting_keypair: &Keypair,
bootstrap_validator_staking_keypair: Arc<Keypair>, bootstrap_validator_staking_pubkey: &Pubkey,
bootstrap_validator_stake_lamports: u64, bootstrap_validator_stake_lamports: u64,
bootstrap_validator_lamports: u64, bootstrap_validator_lamports: u64,
) -> GenesisConfigInfo { ) -> GenesisConfigInfo {
@ -129,7 +126,7 @@ pub fn create_genesis_config_with_leader_ex(
let rent = Rent::free(); let rent = Rent::free();
let bootstrap_validator_stake_account = stake_state::create_account( let bootstrap_validator_stake_account = stake_state::create_account(
&bootstrap_validator_staking_keypair.pubkey(), bootstrap_validator_staking_pubkey,
&bootstrap_validator_voting_keypair.pubkey(), &bootstrap_validator_voting_keypair.pubkey(),
&bootstrap_validator_vote_account, &bootstrap_validator_vote_account,
&rent, &rent,
@ -150,7 +147,7 @@ pub fn create_genesis_config_with_leader_ex(
bootstrap_validator_vote_account, bootstrap_validator_vote_account,
), ),
( (
bootstrap_validator_staking_keypair.pubkey(), *bootstrap_validator_staking_pubkey,
bootstrap_validator_stake_account, bootstrap_validator_stake_account,
), ),
] ]
@ -171,6 +168,7 @@ pub fn create_genesis_config_with_leader_ex(
GenesisConfigInfo { GenesisConfigInfo {
genesis_config, genesis_config,
mint_keypair, mint_keypair,
voting_keypair: bootstrap_validator_voting_keypair, voting_keypair: Keypair::from_bytes(&bootstrap_validator_voting_keypair.to_bytes())
.unwrap(),
} }
} }