Skip to content

Commit a00728e

Browse files
Send IDONTWANT prior to publish (#386)
1 parent cfdff90 commit a00728e

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

libp2p/src/main/kotlin/io/libp2p/pubsub/gossip/GossipParams.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ data class GossipParams(
242242
* [iDontWantMinMessageSizeThreshold] controls the minimum size (in bytes) that an incoming message needs to be so that an IDONTWANT message is sent to mesh peers.
243243
* The default is 16 KB.
244244
*/
245-
val iDontWantMinMessageSizeThreshold: Int = 16000,
245+
val iDontWantMinMessageSizeThreshold: Int = 16384,
246246

247247
/**
248248
* [iDontWantTTL] Expiry time for cache of received IDONTWANT messages for peers

libp2p/src/main/kotlin/io/libp2p/pubsub/gossip/GossipRouter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ open class GossipRouter(
420420
mCache += msg
421421

422422
return if (peers.isNotEmpty()) {
423+
iDontWant(msg)
423424
val publishedMessages = peers
424425
.filterNot { peerDoesNotWantMessage(it, msg.messageId) }
425426
.map { submitPublishMessage(it, msg) }
@@ -610,15 +611,15 @@ open class GossipRouter(
610611
enqueueIwant(peer, messageIds)
611612
}
612613

613-
private fun iDontWant(msg: PubsubMessage, receivedFrom: PeerHandler) {
614+
private fun iDontWant(msg: PubsubMessage, receivedFrom: PeerHandler? = null) {
614615
if (!this.protocol.supportsIDontWant()) return
615616
if (msg.protobufMessage.data.size() < params.iDontWantMinMessageSizeThreshold) return
616617
// we need to send IDONTWANT messages to mesh peers immediately in order for them to have an effect
617618
msg.topics
618619
.mapNotNull { mesh[it] }
619620
.flatten()
620621
.distinct()
621-
.minus(receivedFrom)
622+
.minus(setOfNotNull(receivedFrom))
622623
.forEach { sendIdontwant(it, msg.messageId) }
623624
}
624625

libp2p/src/test/kotlin/io/libp2p/pubsub/gossip/GossipV1_2Tests.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ class GossipV1_2Tests : GossipTestsBase() {
150150
assertThat(receivedMessages).containsExactly(msg.protobufMessage)
151151
}
152152

153+
@Test
154+
fun iDontWantIsSentOnPublishing() {
155+
val test = startSingleTopicNetwork(
156+
params = GossipParams(iDontWantMinMessageSizeThreshold = 5),
157+
mockRouterCount = 3
158+
)
159+
160+
test.mockRouters.forEach { it.subscribe("topic1") }
161+
val msgToPublish = newMessage("topic1", 0L, "Hello".toByteArray())
162+
test.gossipRouter.publish(msgToPublish)
163+
test.mockRouters.forEach {
164+
// IDONTWANT is received
165+
it.waitForMessage { msg ->
166+
msg.control.idontwantCount == 1 &&
167+
msg.control.idontwantList.first().messageIDsList.map { mIds -> mIds.toWBytes() }.contains(msgToPublish.messageId)
168+
}
169+
// msg is received
170+
it.waitForMessage { msg -> msg.publishCount > 0 }
171+
}
172+
}
173+
153174
private fun startSingleTopicNetwork(params: GossipParams, mockRouterCount: Int): ManyRoutersTest {
154175
val test = ManyRoutersTest(
155176
protocol = PubsubProtocol.Gossip_V_1_2,

0 commit comments

Comments
 (0)