GS: improve handleIHave (#922)

This commit is contained in:
Tanguy 2023-07-11 12:17:50 +02:00 committed by GitHub
parent fab1340020
commit 440461b24b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 12 deletions

View File

@ -245,24 +245,18 @@ proc handleIHave*(g: GossipSub,
elif peer.iHaveBudget <= 0: elif peer.iHaveBudget <= 0:
trace "ihave: ignoring out of budget peer", peer, score = peer.score trace "ihave: ignoring out of budget peer", peer, score = peer.score
else: else:
# TODO review deduplicate algorithm for ihave in ihaves:
# * 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:
trace "peer sent ihave", trace "peer sent ihave",
peer, topic = ihave.topicId, msgs = ihave.messageIds peer, topic = ihave.topicId, msgs = ihave.messageIds
if ihave.topicId in g.mesh: if ihave.topicId in g.topics:
# also avoid duplicates here! for msgId in ihave.messageIds:
let deIhavesMsgs = ihave.messageIds.deduplicate()
for msgId in deIhavesMsgs:
if not g.hasSeen(msgId): if not g.hasSeen(msgId):
if peer.iHaveBudget > 0: if peer.iHaveBudget <= 0:
break
elif msgId notin res.messageIds:
res.messageIds.add(msgId) res.messageIds.add(msgId)
dec peer.iHaveBudget dec peer.iHaveBudget
trace "requested message via ihave", messageID=msgId trace "requested message via ihave", messageID=msgId
else:
break
# shuffling res.messageIDs before sending it out to increase the likelihood # 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. # of getting an answer if the peer truncates the list due to internal size restrictions.
g.rng.shuffle(res.messageIds) g.rng.shuffle(res.messageIds)

View File

@ -658,11 +658,14 @@ suite "GossipSub internal":
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} = proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
check false check false
proc handler2(topic: string, data: seq[byte]) {.async.} = discard
let topic = "foobar" let topic = "foobar"
var conns = newSeq[Connection]() var conns = newSeq[Connection]()
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
gossipSub.mesh[topic] = initHashSet[PubSubPeer]() gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
gossipSub.subscribe(topic, handler2)
for i in 0..<30: for i in 0..<30:
let conn = TestBufferStream.new(noop) let conn = TestBufferStream.new(noop)
conns &= conn conns &= conn