bootstrap: Refactor retaining the highest snapshot hashes (#31254)

This commit is contained in:
Brooks 2023-04-19 16:00:00 -04:00 committed by GitHub
parent 10d637d2e6
commit b4ea10413f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 20 deletions

View File

@ -1140,16 +1140,16 @@ fn retain_peer_snapshot_hashes_that_match_known_snapshot_hashes(
fn retain_peer_snapshot_hashes_with_highest_full_snapshot_slot( fn retain_peer_snapshot_hashes_with_highest_full_snapshot_slot(
peer_snapshot_hashes: &mut Vec<PeerSnapshotHash>, peer_snapshot_hashes: &mut Vec<PeerSnapshotHash>,
) { ) {
// retain the hashes with the highest full snapshot slot let highest_full_snapshot_hash = peer_snapshot_hashes
// do a two-pass algorithm .iter()
// 1. find the full snapshot hash with the highest full snapshot slot .map(|peer_snapshot_hash| peer_snapshot_hash.snapshot_hash.full)
// 2. retain elems with that full snapshot hash .max_by_key(|(slot, _hash)| *slot);
let mut highest_full_snapshot_hash = (Slot::MIN, Hash::default()); let Some(highest_full_snapshot_hash) = highest_full_snapshot_hash else {
peer_snapshot_hashes.iter().for_each(|peer_snapshot_hash| { // `max_by_key` will only be `None` IFF the input `peer_snapshot_hashes` is empty.
if peer_snapshot_hash.snapshot_hash.full.0 > highest_full_snapshot_hash.0 { // In that case there's nothing to do (additionally, without a valid 'max' value, there
highest_full_snapshot_hash = peer_snapshot_hash.snapshot_hash.full; // will be nothing to compare against within the `retain()` predicate).
} return;
}); };
peer_snapshot_hashes.retain(|peer_snapshot_hash| { peer_snapshot_hashes.retain(|peer_snapshot_hash| {
peer_snapshot_hash.snapshot_hash.full == highest_full_snapshot_hash peer_snapshot_hash.snapshot_hash.full == highest_full_snapshot_hash
@ -1162,16 +1162,10 @@ fn retain_peer_snapshot_hashes_with_highest_full_snapshot_slot(
fn retain_peer_snapshot_hashes_with_highest_incremental_snapshot_slot( fn retain_peer_snapshot_hashes_with_highest_incremental_snapshot_slot(
peer_snapshot_hashes: &mut Vec<PeerSnapshotHash>, peer_snapshot_hashes: &mut Vec<PeerSnapshotHash>,
) { ) {
let mut highest_incremental_snapshot_hash: Option<(Slot, Hash)> = None; let highest_incremental_snapshot_hash = peer_snapshot_hashes
peer_snapshot_hashes.iter().for_each(|peer_snapshot_hash| { .iter()
if let Some(incremental_snapshot_hash) = peer_snapshot_hash.snapshot_hash.incr.as_ref() { .flat_map(|peer_snapshot_hash| peer_snapshot_hash.snapshot_hash.incr)
if highest_incremental_snapshot_hash.is_none() .max_by_key(|(slot, _hash)| *slot);
|| incremental_snapshot_hash.0 > highest_incremental_snapshot_hash.unwrap().0
{
highest_incremental_snapshot_hash = Some(*incremental_snapshot_hash);
}
};
});
peer_snapshot_hashes.retain(|peer_snapshot_hash| { peer_snapshot_hashes.retain(|peer_snapshot_hash| {
peer_snapshot_hash.snapshot_hash.incr == highest_incremental_snapshot_hash peer_snapshot_hash.snapshot_hash.incr == highest_incremental_snapshot_hash