Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

network-gossip: Do not report peer on duplicate message if its the first time #13508

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
36 changes: 35 additions & 1 deletion client/network-gossip/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,15 @@ impl<B: BlockT> ConsensusGossip<B> {
protocol = %self.protocol,
"Ignored already known message",
);
network.report_peer(who, rep::DUPLICATE_GOSSIP);

// If the peer already send us the message once, let's report them.
if self
.peers
.get_mut(&who)
.map_or(false, |p| !p.known_messages.insert(message_hash))
{
network.report_peer(who, rep::DUPLICATE_GOSSIP);
}
continue
}

Expand Down Expand Up @@ -814,4 +822,30 @@ mod tests {
to_forward,
);
}

// Two peers can send us the same gossip message. We should not report the second peer
// sending the gossip message as long as its the first time the peer send us this message.
#[test]
fn do_not_report_peer_for_first_time_duplicate_gossip_message() {
let mut consensus = ConsensusGossip::<Block>::new(Arc::new(AllowAll), "/foo".into(), None);

let mut network = NoOpNetwork::default();

let peer_id = PeerId::random();
consensus.new_peer(&mut network, peer_id, ObservedRole::Full);
assert!(consensus.peers.contains_key(&peer_id));

let peer_id2 = PeerId::random();
consensus.new_peer(&mut network, peer_id2, ObservedRole::Full);
assert!(consensus.peers.contains_key(&peer_id2));

let message = vec![vec![1, 2, 3]];
consensus.on_incoming(&mut network, peer_id, message.clone());
consensus.on_incoming(&mut network, peer_id2, message.clone());

assert_eq!(
vec![(peer_id, rep::GOSSIP_SUCCESS)],
network.inner.lock().unwrap().peer_reports
);
}
}