Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AshwinSekar committed Jul 9, 2024
1 parent d28c84b commit 8fd8390
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3243,6 +3243,10 @@ pub mod test {
assert!(!blockstore.is_root(1));
assert!(!blockstore.is_root(3));
assert!(!blockstore.is_root(4));
for i in 0..4 {
blockstore.store_duplicate_slot(i, vec![], vec![]).unwrap();
assert!(blockstore.has_duplicate_shreds_in_slot(i));
}

let mut tower = Tower::default();
tower.vote_state.root_slot = Some(4);
Expand All @@ -3254,9 +3258,13 @@ pub mod test {
.unwrap();

assert!(!blockstore.is_root(0));
assert!(blockstore.has_duplicate_shreds_in_slot(0));
assert!(blockstore.is_root(1));
assert!(!blockstore.has_duplicate_shreds_in_slot(1));
assert!(!blockstore.is_root(3));
assert!(blockstore.has_duplicate_shreds_in_slot(3));
assert!(blockstore.is_root(4));
assert!(!blockstore.has_duplicate_shreds_in_slot(4));
}

#[test]
Expand Down
77 changes: 77 additions & 0 deletions core/src/replay_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4521,6 +4521,7 @@ pub(crate) mod tests {
replay_stage::ReplayStage,
vote_simulator::{self, VoteSimulator},
},
blockstore_processor::{fill_blockstore_slot_with_ticks, ProcessOptions},
crossbeam_channel::unbounded,
itertools::Itertools,
solana_entry::entry::{self, Entry},
Expand Down Expand Up @@ -9032,4 +9033,80 @@ pub(crate) mod tests {
assert_eq!(tower.vote_state, expected_tower.vote_state);
assert_eq!(tower.node_pubkey, expected_tower.node_pubkey);
}

#[test]
fn test_initialize_progress_and_fork_choice_with_duplicates() {
let GenesisConfigInfo {
mut genesis_config, ..
} = create_genesis_config(123);

let ticks_per_slot = 1;
genesis_config.ticks_per_slot = ticks_per_slot;
let (ledger_path, blockhash) =
solana_ledger::create_new_tmp_ledger_auto_delete!(&genesis_config);
let blockstore = Blockstore::open(ledger_path.path()).unwrap();

/*
Bank forks with:
slot 0
|
slot 1 -> Duplicate before restart
*/

let mut last_hash = blockhash;
for i in 0..1 {
last_hash =
fill_blockstore_slot_with_ticks(&blockstore, ticks_per_slot, i + 1, i, last_hash);
}
blockstore.set_roots([0, 1].iter()).unwrap();

// Set up bank0
let bank_forks = BankForks::new_rw_arc(Bank::new_for_tests(&genesis_config));
let bank0 = bank_forks.read().unwrap().get_with_scheduler(0).unwrap();

// Mark block 1 as duplicate
blockstore.store_duplicate_slot(1, vec![], vec![]).unwrap();

let bank1 = bank_forks.write().unwrap().insert(Bank::new_from_parent(
bank0.clone_without_scheduler(),
&Pubkey::default(),
1,
));
bank_forks
.write()
.unwrap()
.set_root(
1,
&solana_runtime::accounts_background_service::AbsRequestSender::default(),
None,
)
.unwrap();

let leader_schedule_cache = LeaderScheduleCache::new_from_bank(&bank1);

// process_blockstore_from_root() from slot 1 onwards, should remove the duplicate proof from before the restart
blockstore_processor::process_blockstore_from_root(
&blockstore,
&bank_forks,
&leader_schedule_cache,
&ProcessOptions::default(),
None,
None,
None,
&AbsRequestSender::default(),
)
.unwrap();

// Verify that the duplicate information has been cleared and that fork choice can be initialized
let (_progress, fork_choice) =
ReplayStage::initialize_progress_and_fork_choice_with_locked_bank_forks(
&bank_forks,
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&blockstore,
);

assert_eq!(fork_choice.tree_root().0, 1);
assert_eq!(fork_choice.best_overall_slot().0, 1);
}
}
13 changes: 13 additions & 0 deletions ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3992,6 +3992,14 @@ pub mod tests {
accounts_db_test_hash_calculation: true,
..ProcessOptions::default()
};

// Mark all blocks 1 - 6 as duplicate
for slot in 1..6 {
blockstore
.store_duplicate_slot(slot, vec![], vec![])
.unwrap();
}

let recyclers = VerifyRecyclers::default();
let replay_tx_thread_pool = create_thread_pool(1);
process_bank_0(
Expand Down Expand Up @@ -4065,6 +4073,11 @@ pub mod tests {

// Check that bank forks has the correct banks
verify_fork_infos(&bank_forks);

// Verify that the duplicate information has been cleared and that fork choice can be initialized
for slot in 1..6 {
assert!(!blockstore.has_duplicate_shreds_in_slot(slot));
}
}

#[test]
Expand Down

0 comments on commit 8fd8390

Please sign in to comment.