Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer peer with the highest reputation if multiple PoT proofs were gossiped #2320

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading