Skip to content

Commit

Permalink
GS: improve handleIHave (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
Menduist authored Jul 11, 2023
1 parent fab1340 commit 440461b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
18 changes: 6 additions & 12 deletions libp2p/protocols/pubsub/gossipsub/behavior.nim
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,18 @@ proc handleIHave*(g: GossipSub,
elif peer.iHaveBudget <= 0:
trace "ihave: ignoring out of budget peer", peer, score = peer.score
else:
# TODO review deduplicate algorithm
# * https://github.com/nim-lang/Nim/blob/5f46474555ee93306cce55342e81130c1da79a42/lib/pure/collections/sequtils.nim#L184
# * it's probably not efficient and might give preference to the first dupe
let deIhaves = ihaves.deduplicate()
for ihave in deIhaves:
for ihave in ihaves:
trace "peer sent ihave",
peer, topic = ihave.topicId, msgs = ihave.messageIds
if ihave.topicId in g.mesh:
# also avoid duplicates here!
let deIhavesMsgs = ihave.messageIds.deduplicate()
for msgId in deIhavesMsgs:
if ihave.topicId in g.topics:
for msgId in ihave.messageIds:
if not g.hasSeen(msgId):
if peer.iHaveBudget > 0:
if peer.iHaveBudget <= 0:
break
elif msgId notin res.messageIds:
res.messageIds.add(msgId)
dec peer.iHaveBudget
trace "requested message via ihave", messageID=msgId
else:
break
# shuffling res.messageIDs before sending it out to increase the likelihood
# of getting an answer if the peer truncates the list due to internal size restrictions.
g.rng.shuffle(res.messageIds)
Expand Down
3 changes: 3 additions & 0 deletions tests/pubsub/testgossipinternal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -658,11 +658,14 @@ suite "GossipSub internal":

proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
check false
proc handler2(topic: string, data: seq[byte]) {.async.} = discard

let topic = "foobar"
var conns = newSeq[Connection]()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.subscribe(topic, handler2)

for i in 0..<30:
let conn = TestBufferStream.new(noop)
conns &= conn
Expand Down

0 comments on commit 440461b

Please sign in to comment.