From 5aebf0990e5315a0349e2414e7bc11da96f703bc Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Fri, 29 Jan 2021 12:41:51 +0900 Subject: [PATCH] peer stats fixes (#511) Gossipsub fix, required by nimbus, merging into master as low impact --- libp2p/protocols/pubsub/gossipsub.nim | 36 +++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 7ad421eac..31c515cf7 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -340,8 +340,10 @@ method init*(g: GossipSub) = g.codecs &= GossipSubCodec g.codecs &= GossipSubCodec_10 -proc initPeerStats(g: GossipSub, peer: PubSubPeer) = - g.peerStats[peer.peerId] = PeerStats() +proc initPeerStats(g: GossipSub, peer: PubSubPeer, stats: PeerStats = PeerStats()) = + var initialStats = stats + initialStats.expire = Moment.now() + g.parameters.retainScore + g.peerStats[peer.peerId] = initialStats peer.iWantBudget = IWantPeerBudget peer.iHaveBudget = IHavePeerBudget @@ -793,7 +795,7 @@ proc updateScores(g: GossipSub) = # avoid async if isNil(peer) or not(peer.connected): if now > stats.expire: evicting.add(peerId) - trace "evicted peer from memory", peer + trace "evicted peer from memory", peer = peerId continue trace "updating peer score", peer @@ -933,6 +935,7 @@ proc updateScores(g: GossipSub) = # avoid async stats.score = peer.score stats.appScore = peer.appScore stats.behaviourPenalty = peer.behaviourPenalty + stats.expire = Moment.now() + g.parameters.retainScore # refresh expiration assert(g.peerStats[peer.peerId].score == peer.score) # nim sanity check trace "updated peer's score", peer, score = peer.score, n_topics, is_grafted @@ -1123,11 +1126,10 @@ proc punishInvalidMessage(g: GossipSub, peer: PubSubPeer, topics: seq[string]) = do: # if we have no stats populate! stats[].topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1) do: # if we have no stats populate! - g.peerStats[peer.peerId] = - block: - var stats = PeerStats() - stats.topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1) - stats + g.initPeerStats(peer) do: + var stats = PeerStats() + stats.topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1) + stats proc handleGraft(g: GossipSub, @@ -1314,11 +1316,10 @@ method rpcHandler*(g: GossipSub, do: # make sure we don't loose this information pstats[].topicInfos[t] = TopicInfo(meshMessageDeliveries: 1) do: # make sure we don't loose this information - g.peerStats[peer.peerId] = - block: - var stats = PeerStats() - stats.topicInfos[t] = TopicInfo(meshMessageDeliveries: 1) - stats + g.initPeerStats(peer) do: + var stats = PeerStats() + stats.topicInfos[t] = TopicInfo(meshMessageDeliveries: 1) + stats # onto the next message continue @@ -1384,11 +1385,10 @@ method rpcHandler*(g: GossipSub, do: # make sure we don't loose this information pstats[].topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1) do: # make sure we don't loose this information - g.peerStats[peer.peerId] = - block: - var stats = PeerStats() - stats.topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1) - stats + g.initPeerStats(peer) do: + var stats = PeerStats() + stats.topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1) + stats g.floodsub.withValue(t, peers): toSendPeers.incl(peers[]) g.mesh.withValue(t, peers): toSendPeers.incl(peers[])