From cd1aecb577b92003a7005b87855912db539239f1 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Thu, 14 Dec 2023 15:07:37 +0200 Subject: [PATCH] PoT gossip sort optimization --- crates/sc-proof-of-time/src/source/gossip.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/sc-proof-of-time/src/source/gossip.rs b/crates/sc-proof-of-time/src/source/gossip.rs index e2922ab593..6df5084f52 100644 --- a/crates/sc-proof-of-time/src/source/gossip.rs +++ b/crates/sc-proof-of-time/src/source/gossip.rs @@ -419,13 +419,21 @@ where return; } + // This sorts from lowest reputation to highest + potentially_matching_proofs.sort_by_cached_key(|(_proof, peer_ids)| { + peer_ids + .iter() + .map(|peer_id| network.peer_reputation(peer_id)) + .max() + }); + // If we have too many unique proofs to verify it might be cheaper to prove it ourselves let correct_proof = if potentially_matching_proofs.len() < EXPECTED_POT_VERIFICATION_SPEEDUP { let mut correct_proof = None; - // Verify all proofs - for (proof, _senders) in &potentially_matching_proofs { + // Verify all proofs, starting with those sent by most reputable peers + for (proof, _senders) in potentially_matching_proofs.iter().rev() { if pot_verifier.verify_checkpoints( proof.seed, proof.slot_iterations, @@ -438,13 +446,6 @@ where correct_proof } else { - // This sorts from lowest reputation to highest - potentially_matching_proofs.sort_by_cached_key(|(_proof, peer_ids)| { - peer_ids - .iter() - .map(|peer_id| network.peer_reputation(peer_id)) - .max() - }); // Last proof includes peer with the highest reputation let (proof, _senders) = potentially_matching_proofs .last()