From 02b20440f268da7ffd69a528544c1df5543caeb3 Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Sat, 28 Nov 2020 16:27:39 +0900 Subject: [PATCH] Limit ihave emission (#462) * add some limits to ihave emission, go has them, rust does not actually * restore shuffling of IDs * add some context --- libp2p/protocols/pubsub/gossipsub.nim | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index fd094d0..faa4570 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -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)