fix internal gossip tests

This commit is contained in:
Giovanni Petrantoni 2020-07-18 21:00:44 +09:00
parent 13c87a76c0
commit 6af7909378
2 changed files with 27 additions and 7 deletions

View File

@ -103,6 +103,9 @@ type
heartbeatLock: AsyncLock # heartbeat lock to prevent two consecutive concurrent heartbeats heartbeatLock: AsyncLock # heartbeat lock to prevent two consecutive concurrent heartbeats
peerStats: Table[PubSubPeer, PeerStats] peerStats: Table[PubSubPeer, PeerStats]
when not defined(release):
prunedPeers: HashSet[PubSubPeer]
declareGauge(libp2p_gossipsub_peers_per_topic_mesh, declareGauge(libp2p_gossipsub_peers_per_topic_mesh,
"gossipsub peers per topic in mesh", "gossipsub peers per topic in mesh",
labels = ["topic"]) labels = ["topic"])
@ -191,14 +194,27 @@ method handleConnect*(g: GossipSub, peer: PubSubPeer) =
discard discard
proc grafted(g: GossipSub, p: PubSubPeer, topic: string) = proc grafted(g: GossipSub, p: PubSubPeer, topic: string) =
var stats = g.peerStats[p] g.peerStats.withValue(p, stats) do:
var info = stats.topicInfos.mgetOrPut(topic, TopicInfo()) var info = stats.topicInfos.mgetOrPut(topic, TopicInfo())
info.graftTime = Moment.now() info.graftTime = Moment.now()
info.meshTime = 0.seconds info.meshTime = 0.seconds
do:
raise newException(CatchableError, "TopicInfo key not found for " & $p)
proc pruned(g: GossipSub, p: PubSubPeer, topic: string) = proc pruned(g: GossipSub, p: PubSubPeer, topic: string) {.gcsafe.} =
var stats = g.peerStats[p] g.peerStats.withValue(p, stats) do:
var _ = stats.topicInfos[topic] when not defined(release):
g.prunedPeers.incl(p)
var _ = stats.topicInfos[topic]
do:
when not defined(release):
if p in g.prunedPeers:
raise newException(CatchableError, "Dupe prune " & $p)
else:
raise newException(CatchableError, "TopicInfo key not found for " & $p)
else:
raise newException(CatchableError, "TopicInfo key not found for " & $p)
proc replenishFanout(g: GossipSub, topic: string) = proc replenishFanout(g: GossipSub, topic: string) =
## get fanout peers for a topic ## get fanout peers for a topic

View File

@ -53,6 +53,8 @@ suite "GossipSub internal":
let peer = newPubSubPeer(peerInfo, GossipSubCodec) let peer = newPubSubPeer(peerInfo, GossipSubCodec)
peer.conn = conn peer.conn = conn
gossipSub.peers[peerInfo.id] = peer gossipSub.peers[peerInfo.id] = peer
gossipSub.handleConnect(peer)
gossipSub.grafted(peer, topic)
gossipSub.mesh[topic].incl(peer) gossipSub.mesh[topic].incl(peer)
check gossipSub.peers.len == 15 check gossipSub.peers.len == 15
@ -84,6 +86,8 @@ suite "GossipSub internal":
let peer = newPubSubPeer(peerInfo, GossipSubCodec) let peer = newPubSubPeer(peerInfo, GossipSubCodec)
peer.conn = conn peer.conn = conn
gossipSub.peers[peerInfo.id] = peer gossipSub.peers[peerInfo.id] = peer
gossipSub.handleConnect(peer)
gossipSub.grafted(peer, topic)
gossipSub.mesh[topic].incl(peer) gossipSub.mesh[topic].incl(peer)
check gossipSub.mesh[topic].len == 15 check gossipSub.mesh[topic].len == 15