diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 8bfcf3e140..b65210e340 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -102,6 +102,7 @@ proc init*( overheadRateLimit = Opt.none(tuple[bytes: int, interval: Duration]), disconnectPeerAboveRateLimit = false, maxNumElementsInNonPriorityQueue = DefaultMaxNumElementsInNonPriorityQueue, + sendIDontWantOnPublish = false, ): GossipSubParams = GossipSubParams( explicit: true, @@ -139,6 +140,7 @@ proc init*( overheadRateLimit: overheadRateLimit, disconnectPeerAboveRateLimit: disconnectPeerAboveRateLimit, maxNumElementsInNonPriorityQueue: maxNumElementsInNonPriorityQueue, + sendIDontWantOnPublish: sendIDontWantOnPublish, ) proc validateParameters*(parameters: GossipSubParams): Result[void, cstring] = @@ -412,8 +414,10 @@ proc sendIDontWant( isHighPriority = true, ) +const iDontWantMessageSizeThreshold* = 512 + proc isLargeMessage(msg: Message, msgId: MessageId): bool = - msg.data.len > max(512, msgId.len * 10) + msg.data.len > max(iDontWantMessageSizeThreshold, msgId.len * 10) proc validateAndRelay( g: GossipSub, msg: Message, msgId: MessageId, saltedId: SaltedId, peer: PubSubPeer @@ -795,11 +799,11 @@ method publish*(g: GossipSub, topic: string, data: seq[byte]): Future[int] {.asy g.mcache.put(msgId, msg) - g.broadcast(peers, RPCMsg(messages: @[msg]), isHighPriority = true) - - if isLargeMessage(msg, msgId): + if g.parameters.sendIDontWantOnPublish and isLargeMessage(msg, msgId): g.sendIDontWant(msg, msgId, peers) + g.broadcast(peers, RPCMsg(messages: @[msg]), isHighPriority = true) + if g.knownTopics.contains(topic): libp2p_pubsub_messages_published.inc(peers.len.int64, labelValues = [topic]) else: diff --git a/libp2p/protocols/pubsub/gossipsub/types.nim b/libp2p/protocols/pubsub/gossipsub/types.nim index d50e098240..0044be9d50 100644 --- a/libp2p/protocols/pubsub/gossipsub/types.nim +++ b/libp2p/protocols/pubsub/gossipsub/types.nim @@ -154,6 +154,9 @@ type # Max number of elements allowed in the non-priority queue. When this limit has been reached, the peer will be disconnected. maxNumElementsInNonPriorityQueue*: int + # Broadcast an IDONTWANT message automatically when the message exceeds the IDONTWANT message size threshold + sendIDontWantOnPublish*: bool + BackoffTable* = Table[string, Table[PeerId, Moment]] ValidationSeenTable* = Table[SaltedId, HashSet[PubSubPeer]]