peer stats fixes (#511)

Gossipsub fix, required by nimbus, merging into master as low impact
This commit is contained in:
Giovanni Petrantoni 2021-01-29 12:41:51 +09:00 committed by GitHub
parent fb493d1a4a
commit 5aebf0990e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 18 deletions

View File

@ -340,8 +340,10 @@ method init*(g: GossipSub) =
g.codecs &= GossipSubCodec g.codecs &= GossipSubCodec
g.codecs &= GossipSubCodec_10 g.codecs &= GossipSubCodec_10
proc initPeerStats(g: GossipSub, peer: PubSubPeer) = proc initPeerStats(g: GossipSub, peer: PubSubPeer, stats: PeerStats = PeerStats()) =
g.peerStats[peer.peerId] = PeerStats() var initialStats = stats
initialStats.expire = Moment.now() + g.parameters.retainScore
g.peerStats[peer.peerId] = initialStats
peer.iWantBudget = IWantPeerBudget peer.iWantBudget = IWantPeerBudget
peer.iHaveBudget = IHavePeerBudget peer.iHaveBudget = IHavePeerBudget
@ -793,7 +795,7 @@ proc updateScores(g: GossipSub) = # avoid async
if isNil(peer) or not(peer.connected): if isNil(peer) or not(peer.connected):
if now > stats.expire: if now > stats.expire:
evicting.add(peerId) evicting.add(peerId)
trace "evicted peer from memory", peer trace "evicted peer from memory", peer = peerId
continue continue
trace "updating peer score", peer trace "updating peer score", peer
@ -933,6 +935,7 @@ proc updateScores(g: GossipSub) = # avoid async
stats.score = peer.score stats.score = peer.score
stats.appScore = peer.appScore stats.appScore = peer.appScore
stats.behaviourPenalty = peer.behaviourPenalty stats.behaviourPenalty = peer.behaviourPenalty
stats.expire = Moment.now() + g.parameters.retainScore # refresh expiration
assert(g.peerStats[peer.peerId].score == peer.score) # nim sanity check assert(g.peerStats[peer.peerId].score == peer.score) # nim sanity check
trace "updated peer's score", peer, score = peer.score, n_topics, is_grafted 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! do: # if we have no stats populate!
stats[].topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1) stats[].topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1)
do: # if we have no stats populate! do: # if we have no stats populate!
g.peerStats[peer.peerId] = g.initPeerStats(peer) do:
block: var stats = PeerStats()
var stats = PeerStats() stats.topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1)
stats.topicInfos[t] = TopicInfo(invalidMessageDeliveries: 1) stats
stats
proc handleGraft(g: GossipSub, proc handleGraft(g: GossipSub,
@ -1314,11 +1316,10 @@ method rpcHandler*(g: GossipSub,
do: # make sure we don't loose this information do: # make sure we don't loose this information
pstats[].topicInfos[t] = TopicInfo(meshMessageDeliveries: 1) pstats[].topicInfos[t] = TopicInfo(meshMessageDeliveries: 1)
do: # make sure we don't loose this information do: # make sure we don't loose this information
g.peerStats[peer.peerId] = g.initPeerStats(peer) do:
block: var stats = PeerStats()
var stats = PeerStats() stats.topicInfos[t] = TopicInfo(meshMessageDeliveries: 1)
stats.topicInfos[t] = TopicInfo(meshMessageDeliveries: 1) stats
stats
# onto the next message # onto the next message
continue continue
@ -1384,11 +1385,10 @@ method rpcHandler*(g: GossipSub,
do: # make sure we don't loose this information do: # make sure we don't loose this information
pstats[].topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1) pstats[].topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1)
do: # make sure we don't loose this information do: # make sure we don't loose this information
g.peerStats[peer.peerId] = g.initPeerStats(peer) do:
block: var stats = PeerStats()
var stats = PeerStats() stats.topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1)
stats.topicInfos[t] = TopicInfo(firstMessageDeliveries: 1, meshMessageDeliveries: 1) stats
stats
g.floodsub.withValue(t, peers): toSendPeers.incl(peers[]) g.floodsub.withValue(t, peers): toSendPeers.incl(peers[])
g.mesh.withValue(t, peers): toSendPeers.incl(peers[]) g.mesh.withValue(t, peers): toSendPeers.incl(peers[])