Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send IDONTWANT prior to publish #386

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ data class GossipParams(
* [iDontWantMinMessageSizeThreshold] controls the minimum size (in bytes) that an incoming message needs to be so that an IDONTWANT message is sent to mesh peers.
* The default is 16 KB.
*/
val iDontWantMinMessageSizeThreshold: Int = 16000,
val iDontWantMinMessageSizeThreshold: Int = 16384,

/**
* [iDontWantTTL] Expiry time for cache of received IDONTWANT messages for peers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ open class GossipRouter(
mCache += msg

return if (peers.isNotEmpty()) {
iDontWant(msg)
val publishedMessages = peers
.filterNot { peerDoesNotWantMessage(it, msg.messageId) }
.map { submitPublishMessage(it, msg) }
Expand Down Expand Up @@ -610,15 +611,15 @@ open class GossipRouter(
enqueueIwant(peer, messageIds)
}

private fun iDontWant(msg: PubsubMessage, receivedFrom: PeerHandler) {
private fun iDontWant(msg: PubsubMessage, receivedFrom: PeerHandler? = null) {
if (!this.protocol.supportsIDontWant()) return
if (msg.protobufMessage.data.size() < params.iDontWantMinMessageSizeThreshold) return
// we need to send IDONTWANT messages to mesh peers immediately in order for them to have an effect
msg.topics
.mapNotNull { mesh[it] }
.flatten()
.distinct()
.minus(receivedFrom)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            .minus(setOfNotNull(receivedFrom))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was looking for a nice way to add it back. This one seems good. Implemented.

.minus(setOfNotNull(receivedFrom))
.forEach { sendIdontwant(it, msg.messageId) }
}

Expand Down
21 changes: 21 additions & 0 deletions libp2p/src/test/kotlin/io/libp2p/pubsub/gossip/GossipV1_2Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ class GossipV1_2Tests : GossipTestsBase() {
assertThat(receivedMessages).containsExactly(msg.protobufMessage)
}

@Test
fun iDontWantIsSentOnPublishing() {
val test = startSingleTopicNetwork(
params = GossipParams(iDontWantMinMessageSizeThreshold = 5),
mockRouterCount = 3
)

test.mockRouters.forEach { it.subscribe("topic1") }
val msgToPublish = newMessage("topic1", 0L, "Hello".toByteArray())
test.gossipRouter.publish(msgToPublish)
test.mockRouters.forEach {
// IDONTWANT is received
it.waitForMessage { msg ->
msg.control.idontwantCount == 1 &&
msg.control.idontwantList.first().messageIDsList.map { mIds -> mIds.toWBytes() }.contains(msgToPublish.messageId)
}
// msg is received
it.waitForMessage { msg -> msg.publishCount > 0 }
}
}

private fun startSingleTopicNetwork(params: GossipParams, mockRouterCount: Int): ManyRoutersTest {
val test = ManyRoutersTest(
protocol = PubsubProtocol.Gossip_V_1_2,
Expand Down
Loading