Skip to content

Commit

Permalink
Merge pull request #2320 from subspace/pot-gossip-priority
Browse files Browse the repository at this point in the history
Prefer peer with the highest reputation if multiple PoT proofs were gossiped
  • Loading branch information
nazar-pc authored Dec 13, 2023
2 parents 5ad43be + 759262c commit bc81cca
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions crates/sc-proof-of-time/src/source/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ where
Block: BlockT,
{
engine: Arc<Mutex<GossipEngine<Block>>>,
network: Arc<dyn NetworkPeers + Send + Sync>,
topic: Block::Hash,
state: Arc<PotState>,
pot_verifier: PotVerifier,
Expand Down Expand Up @@ -149,10 +150,11 @@ where
sync_oracle,
network.clone(),
));
let engine = GossipEngine::new(network, sync, GOSSIP_PROTOCOL, validator, None);
let engine = GossipEngine::new(network.clone(), sync, GOSSIP_PROTOCOL, validator, None);

Self {
engine: Arc::new(Mutex::new(engine)),
network: Arc::new(network),
topic,
state,
pot_verifier,
Expand Down Expand Up @@ -384,6 +386,7 @@ where
// Avoid blocking gossip for too long
rayon::spawn({
let engine = Arc::clone(&self.engine);
let network = Arc::clone(&self.network);
let pot_verifier = self.pot_verifier.clone();
let from_gossip_sender = self.from_gossip_sender.clone();
let topic = self.topic;
Expand All @@ -393,6 +396,7 @@ where
next_slot_input,
potentially_matching_proofs,
engine,
network.as_ref(),
&pot_verifier,
from_gossip_sender,
topic,
Expand All @@ -403,8 +407,9 @@ where

fn handle_potentially_matching_proofs(
next_slot_input: PotNextSlotInput,
potentially_matching_proofs: Vec<(GossipProof, Vec<PeerId>)>,
mut potentially_matching_proofs: Vec<(GossipProof, Vec<PeerId>)>,
engine: Arc<Mutex<GossipEngine<Block>>>,
network: &dyn NetworkPeers,
pot_verifier: &PotVerifier,
mut from_gossip_sender: mpsc::Sender<(PeerId, GossipProof)>,
topic: Block::Hash,
Expand Down Expand Up @@ -433,23 +438,43 @@ where

correct_proof
} else {
match subspace_proof_of_time::prove(
next_slot_input.seed,
next_slot_input.slot_iterations,
// 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()
.expect("Guaranteed to be non-empty; qed");

if pot_verifier.verify_checkpoints(
proof.seed,
proof.slot_iterations,
&proof.checkpoints,
) {
Ok(checkpoints) => Some(GossipProof {
slot: next_slot_input.slot,
seed: next_slot_input.seed,
slot_iterations: next_slot_input.slot_iterations,
checkpoints,
}),
Err(error) => {
error!(
%error,
slot = %next_slot_input.slot,
"Failed to run proof of time, this is an implementation bug",
);
return;
Some(*proof)
} else {
match subspace_proof_of_time::prove(
next_slot_input.seed,
next_slot_input.slot_iterations,
) {
Ok(checkpoints) => Some(GossipProof {
slot: next_slot_input.slot,
seed: next_slot_input.seed,
slot_iterations: next_slot_input.slot_iterations,
checkpoints,
}),
Err(error) => {
error!(
%error,
slot = %next_slot_input.slot,
"Failed to run proof of time, this is an implementation bug",
);
return;
}
}
}
};
Expand Down

0 comments on commit bc81cca

Please sign in to comment.