Limit ihave emission (#462)

* add some limits to ihave emission, go has them, rust does not actually

* restore shuffling of IDs

* add some context
This commit is contained in:
Giovanni Petrantoni 2020-11-28 16:27:39 +09:00 committed by GitHub
parent 12db9a4cf2
commit 02b20440f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -57,6 +57,10 @@ const
BackoffSlackTime = 2 # seconds
IWantPeerBudget = 25 # 25 messages per second ( reset every heartbeat )
IHavePeerBudget = 10
# the max amount of IHave to expose, not by spec, but go as example
# rust sigp: https://github.com/sigp/rust-libp2p/blob/f53d02bc873fef2bf52cd31e3d5ce366a41d8a8c/protocols/gossipsub/src/config.rs#L572
# go: https://github.com/libp2p/go-libp2p-pubsub/blob/08c17398fb11b2ab06ca141dddc8ec97272eb772/gossipsub.go#L155
IHaveMaxLength = 5000
type
TopicInfo* = object
@ -618,7 +622,14 @@ proc getGossipPeers(g: GossipSub): Table[PubSubPeer, ControlMessage] {.gcsafe.}
if not mids.len > 0:
continue
let ihave = ControlIHave(topicID: topic, messageIDs: toSeq(mids))
var midsSeq = toSeq(mids)
# not in spec
# similar to rust: https://github.com/sigp/rust-libp2p/blob/f53d02bc873fef2bf52cd31e3d5ce366a41d8a8c/protocols/gossipsub/src/behaviour.rs#L2101
# and go https://github.com/libp2p/go-libp2p-pubsub/blob/08c17398fb11b2ab06ca141dddc8ec97272eb772/gossipsub.go#L582
if midsSeq.len > IHaveMaxLength:
shuffle(midsSeq)
midsSeq.setLen(IHaveMaxLength)
let ihave = ControlIHave(topicID: topic, messageIDs: midsSeq)
let mesh = g.mesh.getOrDefault(topic)
let fanout = g.fanout.getOrDefault(topic)