Cleanup ReplayStage tests (#18241)

This commit is contained in:
carllin 2021-06-28 20:19:42 -07:00 committed by GitHub
parent 9d6f1ebef4
commit 68c87469c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 84 deletions

View File

@ -1439,9 +1439,6 @@ pub mod test {
while let Some(visit) = walk.get() { while let Some(visit) = walk.get() {
let slot = *visit.node().data(); let slot = *visit.node().data();
self.progress
.entry(slot)
.or_insert_with(|| ForkProgress::new(Hash::default(), None, None, 0, 0));
if self.bank_forks.read().unwrap().get(slot).is_some() { if self.bank_forks.read().unwrap().get(slot).is_some() {
walk.forward(); walk.forward();
continue; continue;
@ -1449,6 +1446,9 @@ pub mod test {
let parent = *walk.get_parent().unwrap().data(); let parent = *walk.get_parent().unwrap().data();
let parent_bank = self.bank_forks.read().unwrap().get(parent).unwrap().clone(); let parent_bank = self.bank_forks.read().unwrap().get(parent).unwrap().clone();
let new_bank = Bank::new_from_parent(&parent_bank, &Pubkey::default(), slot); let new_bank = Bank::new_from_parent(&parent_bank, &Pubkey::default(), slot);
self.progress
.entry(slot)
.or_insert_with(|| ForkProgress::new(Hash::default(), None, None, 0, 0));
for (pubkey, vote) in cluster_votes.iter() { for (pubkey, vote) in cluster_votes.iter() {
if vote.contains(&parent) { if vote.contains(&parent) {
let keypairs = self.validator_keypairs.get(pubkey).unwrap(); let keypairs = self.validator_keypairs.get(pubkey).unwrap();
@ -1701,7 +1701,14 @@ pub mod test {
let mut progress = ProgressMap::default(); let mut progress = ProgressMap::default();
progress.insert( progress.insert(
0, 0,
ForkProgress::new(bank0.last_blockhash(), None, None, 0, 0), ForkProgress::new_from_bank(
&bank0,
bank0.collector_id(),
&Pubkey::default(),
None,
0,
0,
),
); );
let bank_forks = BankForks::new(bank0); let bank_forks = BankForks::new(bank0);
let heaviest_subtree_fork_choice = let heaviest_subtree_fork_choice =

View File

@ -2570,7 +2570,7 @@ mod tests {
use solana_runtime::{ use solana_runtime::{
accounts_background_service::AbsRequestSender, accounts_background_service::AbsRequestSender,
commitment::BlockCommitment, commitment::BlockCommitment,
genesis_utils::{self, GenesisConfigInfo, ValidatorVoteKeypairs}, genesis_utils::{GenesisConfigInfo, ValidatorVoteKeypairs},
}; };
use solana_sdk::{ use solana_sdk::{
clock::NUM_CONSECUTIVE_LEADER_SLOTS, clock::NUM_CONSECUTIVE_LEADER_SLOTS,
@ -2593,11 +2593,11 @@ mod tests {
iter, iter,
sync::{atomic::AtomicU64, Arc, RwLock}, sync::{atomic::AtomicU64, Arc, RwLock},
}; };
use trees::tr; use trees::{tr, Tree};
#[test] #[test]
fn test_is_partition_detected() { fn test_is_partition_detected() {
let VoteSimulator { bank_forks, .. } = setup_forks(); let (VoteSimulator { bank_forks, .. }, _) = setup_default_forks(1);
let ancestors = bank_forks.read().unwrap().ancestors(); let ancestors = bank_forks.read().unwrap().ancestors();
// Last vote 1 is an ancestor of the heaviest slot 3, no partition // Last vote 1 is an ancestor of the heaviest slot 3, no partition
assert!(!ReplayStage::is_partition_detected(&ancestors, 1, 3)); assert!(!ReplayStage::is_partition_detected(&ancestors, 1, 3));
@ -2614,8 +2614,8 @@ mod tests {
struct ReplayBlockstoreComponents { struct ReplayBlockstoreComponents {
blockstore: Arc<Blockstore>, blockstore: Arc<Blockstore>,
validator_node_to_vote_keys: HashMap<Pubkey, Pubkey>, validator_node_to_vote_keys: HashMap<Pubkey, Pubkey>,
validator_authorized_voter_keypairs: HashMap<Pubkey, ValidatorVoteKeypairs>, validator_keypairs: HashMap<Pubkey, ValidatorVoteKeypairs>,
my_vote_pubkey: Pubkey, my_pubkey: Pubkey,
progress: ProgressMap, progress: ProgressMap,
cluster_info: ClusterInfo, cluster_info: ClusterInfo,
leader_schedule_cache: Arc<LeaderScheduleCache>, leader_schedule_cache: Arc<LeaderScheduleCache>,
@ -2625,45 +2625,32 @@ mod tests {
rpc_subscriptions: Arc<RpcSubscriptions>, rpc_subscriptions: Arc<RpcSubscriptions>,
} }
fn replay_blockstore_components() -> ReplayBlockstoreComponents { fn replay_blockstore_components(forks: Option<Tree<Slot>>) -> ReplayBlockstoreComponents {
// Setup blockstore // Setup blockstore
let ledger_path = get_tmp_ledger_path!(); let (vote_simulator, blockstore) =
let blockstore = Arc::new( setup_forks_from_tree(forks.unwrap_or_else(|| tr(0)), 20);
Blockstore::open(&ledger_path).expect("Expected to be able to open database ledger"),
);
let validator_authorized_voter_keypairs: Vec<_> =
(0..20).map(|_| ValidatorVoteKeypairs::new_rand()).collect();
let validator_node_to_vote_keys: HashMap<Pubkey, Pubkey> = let VoteSimulator {
validator_authorized_voter_keypairs validator_keypairs,
progress,
bank_forks,
..
} = vote_simulator;
let blockstore = Arc::new(blockstore);
let bank_forks = Arc::new(bank_forks);
let validator_node_to_vote_keys: HashMap<Pubkey, Pubkey> = validator_keypairs
.iter() .iter()
.map(|v| (v.node_keypair.pubkey(), v.vote_keypair.pubkey())) .map(|(_, keypairs)| {
(
keypairs.node_keypair.pubkey(),
keypairs.vote_keypair.pubkey(),
)
})
.collect(); .collect();
let GenesisConfigInfo { genesis_config, .. } =
genesis_utils::create_genesis_config_with_vote_accounts(
10_000,
&validator_authorized_voter_keypairs,
vec![100; validator_authorized_voter_keypairs.len()],
);
let bank0 = Bank::new(&genesis_config);
// ProgressMap
let mut progress = ProgressMap::default();
progress.insert(
0,
ForkProgress::new_from_bank(
&bank0,
bank0.collector_id(),
&Pubkey::default(),
None,
0,
0,
),
);
// ClusterInfo // ClusterInfo
let my_keypairs = &validator_authorized_voter_keypairs[0]; let my_keypairs = validator_keypairs.values().next().unwrap();
let my_pubkey = my_keypairs.node_keypair.pubkey(); let my_pubkey = my_keypairs.node_keypair.pubkey();
let cluster_info = ClusterInfo::new( let cluster_info = ClusterInfo::new(
Node::new_localhost_with_pubkey(&my_pubkey).info, Node::new_localhost_with_pubkey(&my_pubkey).info,
@ -2672,16 +2659,18 @@ mod tests {
assert_eq!(my_pubkey, cluster_info.id()); assert_eq!(my_pubkey, cluster_info.id());
// Leader schedule cache // Leader schedule cache
let leader_schedule_cache = Arc::new(LeaderScheduleCache::new_from_bank(&bank0)); let root_bank = bank_forks.read().unwrap().root_bank();
let leader_schedule_cache = Arc::new(LeaderScheduleCache::new_from_bank(&root_bank));
// PohRecorder // PohRecorder
let working_bank = bank_forks.read().unwrap().working_bank();
let poh_recorder = Mutex::new( let poh_recorder = Mutex::new(
PohRecorder::new( PohRecorder::new(
bank0.tick_height(), working_bank.tick_height(),
bank0.last_blockhash(), working_bank.last_blockhash(),
bank0.slot(), working_bank.slot(),
None, None,
bank0.ticks_per_slot(), working_bank.ticks_per_slot(),
&Pubkey::default(), &Pubkey::default(),
&blockstore, &blockstore,
&leader_schedule_cache, &leader_schedule_cache,
@ -2691,14 +2680,11 @@ mod tests {
.0, .0,
); );
// BankForks
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank0)));
// Tower // Tower
let my_vote_pubkey = my_keypairs.vote_keypair.pubkey(); let my_vote_pubkey = my_keypairs.vote_keypair.pubkey();
let tower = Tower::new_from_bankforks( let tower = Tower::new_from_bankforks(
&bank_forks.read().unwrap(), &bank_forks.read().unwrap(),
&ledger_path, blockstore.ledger_path(),
&cluster_info.id(), &cluster_info.id(),
&my_vote_pubkey, &my_vote_pubkey,
); );
@ -2714,17 +2700,11 @@ mod tests {
optimistically_confirmed_bank, optimistically_confirmed_bank,
)); ));
let validator_authorized_voter_keypairs: HashMap<Pubkey, ValidatorVoteKeypairs> =
validator_authorized_voter_keypairs
.into_iter()
.map(|keys| (keys.vote_keypair.pubkey(), keys))
.collect();
ReplayBlockstoreComponents { ReplayBlockstoreComponents {
blockstore, blockstore,
validator_node_to_vote_keys, validator_node_to_vote_keys,
validator_authorized_voter_keypairs, validator_keypairs,
my_vote_pubkey, my_pubkey,
progress, progress,
cluster_info, cluster_info,
leader_schedule_cache, leader_schedule_cache,
@ -2745,7 +2725,7 @@ mod tests {
leader_schedule_cache, leader_schedule_cache,
rpc_subscriptions, rpc_subscriptions,
.. ..
} = replay_blockstore_components(); } = replay_blockstore_components(None);
// Insert a non-root bank so that the propagation logic will update this // Insert a non-root bank so that the propagation logic will update this
// bank // bank
@ -4344,11 +4324,12 @@ mod tests {
#[test] #[test]
fn test_purge_unconfirmed_duplicate_slot() { fn test_purge_unconfirmed_duplicate_slot() {
let (vote_simulator, _) = setup_default_forks(2);
let VoteSimulator { let VoteSimulator {
bank_forks, bank_forks,
mut progress, mut progress,
.. ..
} = setup_forks(); } = vote_simulator;
let mut descendants = bank_forks.read().unwrap().descendants().clone(); let mut descendants = bank_forks.read().unwrap().descendants().clone();
let mut ancestors = bank_forks.read().unwrap().ancestors(); let mut ancestors = bank_forks.read().unwrap().ancestors();
@ -4408,7 +4389,7 @@ mod tests {
#[test] #[test]
fn test_purge_ancestors_descendants() { fn test_purge_ancestors_descendants() {
let VoteSimulator { bank_forks, .. } = setup_forks(); let (VoteSimulator { bank_forks, .. }, _) = setup_default_forks(1);
// Purge branch rooted at slot 2 // Purge branch rooted at slot 2
let mut descendants = bank_forks.read().unwrap().descendants().clone(); let mut descendants = bank_forks.read().unwrap().descendants().clone();
@ -4466,7 +4447,7 @@ mod tests {
bank_forks, bank_forks,
leader_schedule_cache, leader_schedule_cache,
.. ..
} = replay_blockstore_components(); } = replay_blockstore_components(None);
let root_bank = bank_forks.read().unwrap().root_bank(); let root_bank = bank_forks.read().unwrap().root_bank();
let my_pubkey = leader_schedule_cache let my_pubkey = leader_schedule_cache
@ -4660,13 +4641,16 @@ mod tests {
#[test] #[test]
fn test_gossip_vote_doesnt_affect_fork_choice() { fn test_gossip_vote_doesnt_affect_fork_choice() {
let VoteSimulator { let (
VoteSimulator {
bank_forks, bank_forks,
mut heaviest_subtree_fork_choice, mut heaviest_subtree_fork_choice,
mut latest_validator_votes_for_frozen_banks, mut latest_validator_votes_for_frozen_banks,
vote_pubkeys, vote_pubkeys,
.. ..
} = setup_forks(); },
_,
) = setup_default_forks(1);
let vote_pubkey = vote_pubkeys[0]; let vote_pubkey = vote_pubkeys[0];
let mut unfrozen_gossip_verified_vote_hashes = UnfrozenGossipVerifiedVoteHashes::default(); let mut unfrozen_gossip_verified_vote_hashes = UnfrozenGossipVerifiedVoteHashes::default();
@ -4702,14 +4686,14 @@ mod tests {
#[test] #[test]
fn test_replay_stage_refresh_last_vote() { fn test_replay_stage_refresh_last_vote() {
let ReplayBlockstoreComponents { let ReplayBlockstoreComponents {
mut validator_authorized_voter_keypairs, mut validator_keypairs,
cluster_info, cluster_info,
poh_recorder, poh_recorder,
bank_forks, bank_forks,
mut tower, mut tower,
my_vote_pubkey, my_pubkey,
.. ..
} = replay_blockstore_components(); } = replay_blockstore_components(None);
let mut last_vote_refresh_time = LastVoteRefreshTime { let mut last_vote_refresh_time = LastVoteRefreshTime {
last_refresh_time: Instant::now(), last_refresh_time: Instant::now(),
@ -4719,11 +4703,9 @@ mod tests {
let mut voted_signatures = vec![]; let mut voted_signatures = vec![];
let my_vote_keypair = vec![Arc::new( let my_vote_keypair = vec![Arc::new(
validator_authorized_voter_keypairs validator_keypairs.remove(&my_pubkey).unwrap().vote_keypair,
.remove(&my_vote_pubkey)
.unwrap()
.vote_keypair,
)]; )];
let my_vote_pubkey = my_vote_keypair[0].pubkey();
let bank0 = bank_forks.read().unwrap().get(0).unwrap().clone(); let bank0 = bank_forks.read().unwrap().get(0).unwrap().clone();
fn fill_bank_with_ticks(bank: &Bank) { fn fill_bank_with_ticks(bank: &Bank) {
@ -5065,7 +5047,16 @@ mod tests {
) )
} }
fn setup_forks() -> VoteSimulator { fn setup_forks_from_tree(tree: Tree<Slot>, num_keys: usize) -> (VoteSimulator, Blockstore) {
let mut vote_simulator = VoteSimulator::new(num_keys);
vote_simulator.fill_bank_forks(tree.clone(), &HashMap::new());
let ledger_path = get_tmp_ledger_path!();
let blockstore = Blockstore::open(&ledger_path).unwrap();
blockstore.add_tree(tree, false, true, 2, Hash::default());
(vote_simulator, blockstore)
}
fn setup_default_forks(num_keys: usize) -> (VoteSimulator, Blockstore) {
/* /*
Build fork structure: Build fork structure:
@ -5080,12 +5071,9 @@ mod tests {
| |
slot 6 slot 6
*/ */
let forks = tr(0) / (tr(1) / (tr(2) / (tr(4))) / (tr(3) / (tr(5) / (tr(6)))));
let mut vote_simulator = VoteSimulator::new(1); let tree = tr(0) / (tr(1) / (tr(2) / (tr(4))) / (tr(3) / (tr(5) / (tr(6)))));
vote_simulator.fill_bank_forks(forks, &HashMap::new()); setup_forks_from_tree(tree, num_keys)
vote_simulator
} }
fn check_map_eq<K: Eq + std::hash::Hash + std::fmt::Debug, T: PartialEq + std::fmt::Debug>( fn check_map_eq<K: Eq + std::hash::Hash + std::fmt::Debug, T: PartialEq + std::fmt::Debug>(