From c45f9705ab292ac81d65feae03802d916c36e38d Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Tue, 4 Jul 2023 00:27:45 +0200 Subject: [PATCH] Gossipsub scoring improvements (#909) --- libp2p/protocols/pubsub/gossipsub.nim | 3 +++ libp2p/protocols/pubsub/gossipsub/scoring.nim | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 997fdd9f6..445a9e56f 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -155,6 +155,9 @@ method onNewPeer(g: GossipSub, peer: PubSubPeer) = peer.appScore = stats.appScore peer.behaviourPenalty = stats.behaviourPenalty + # Check if the score is below the threshold and disconnect the peer if necessary + g.disconnectBadPeerCheck(peer, stats.score) + peer.iHaveBudget = IHavePeerBudget peer.pingBudget = PingsPeerBudget diff --git a/libp2p/protocols/pubsub/gossipsub/scoring.nim b/libp2p/protocols/pubsub/gossipsub/scoring.nim index 9601452d3..3785e37d1 100644 --- a/libp2p/protocols/pubsub/gossipsub/scoring.nim +++ b/libp2p/protocols/pubsub/gossipsub/scoring.nim @@ -101,6 +101,11 @@ proc disconnectPeer(g: GossipSub, peer: PubSubPeer) {.async.} = except CatchableError as exc: # Never cancelled trace "Failed to close connection", peer, error = exc.name, msg = exc.msg +proc disconnectBadPeerCheck*(g: GossipSub, peer: PubSubPeer, score: float64) = + if g.parameters.disconnectBadPeers and score < g.parameters.graylistThreshold and + peer.peerId notin g.parameters.directPeers: + debug "disconnecting bad score peer", peer, score = peer.score + asyncSpawn(g.disconnectPeer(peer)) proc updateScores*(g: GossipSub) = # avoid async ## https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#the-score-function @@ -241,10 +246,7 @@ proc updateScores*(g: GossipSub) = # avoid async trace "updated peer's score", peer, score = peer.score, n_topics, is_grafted - if g.parameters.disconnectBadPeers and stats.score < g.parameters.graylistThreshold and - peer.peerId notin g.parameters.directPeers: - debug "disconnecting bad score peer", peer, score = peer.score - asyncSpawn(g.disconnectPeer(peer)) + g.disconnectBadPeerCheck(peer, stats.score) libp2p_gossipsub_peers_scores.inc(peer.score, labelValues = [agent])