|
| 1 | +package io.libp2p.pubsub.gossip.extensions |
| 2 | + |
| 3 | +import io.libp2p.pubsub.PubsubProtocol |
| 4 | +import io.libp2p.pubsub.gossip.GossipExtension |
| 5 | +import io.libp2p.pubsub.gossip.GossipTestsBase |
| 6 | +import org.assertj.core.api.Assertions.assertThat |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | +import pubsub.pb.Rpc |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests for Step 6 — full-message suppression (§5.1). |
| 12 | + * |
| 13 | + * When a peer supports partial messages (ControlExtensions handshake) AND has |
| 14 | + * requested partial delivery for a topic (SubOpts), the gossip router MUST NOT |
| 15 | + * send the full message to that peer in either [broadcastOutbound] or |
| 16 | + * [broadcastInbound]. The client is responsible for pushing parts via |
| 17 | + * [Gossip.publishPartial]. |
| 18 | + */ |
| 19 | +class PartialMessagesFullMsgSuppressionTest : GossipTestsBase() { |
| 20 | + |
| 21 | + private val topicId = "test-topic" |
| 22 | + |
| 23 | + private fun newTest() = TwoRoutersTest( |
| 24 | + protocol = PubsubProtocol.Gossip_V_1_3, |
| 25 | + enabledGossipExtensions = listOf(GossipExtension.PARTIAL_MESSAGES), |
| 26 | + partialMessagesHandler = nopPartialMessagesHandler, |
| 27 | + ) |
| 28 | + |
| 29 | + private fun controlExtensionsWithPartial(): Rpc.RPC = |
| 30 | + Rpc.RPC.newBuilder().setControl( |
| 31 | + Rpc.ControlMessage.newBuilder().setExtensions( |
| 32 | + Rpc.ControlExtensions.newBuilder().setPartialMessages(true) |
| 33 | + ) |
| 34 | + ).build() |
| 35 | + |
| 36 | + private fun subscribeRpc( |
| 37 | + topic: String = topicId, |
| 38 | + requestsPartial: Boolean, |
| 39 | + supportsSendingPartial: Boolean, |
| 40 | + ): Rpc.RPC = |
| 41 | + Rpc.RPC.newBuilder().addSubscriptions( |
| 42 | + Rpc.RPC.SubOpts.newBuilder() |
| 43 | + .setTopicid(topic) |
| 44 | + .setSubscribe(true) |
| 45 | + .setRequestsPartial(requestsPartial) |
| 46 | + .setSupportsSendingPartial(supportsSendingPartial) |
| 47 | + ).build() |
| 48 | + |
| 49 | + private fun TwoRoutersTest.flushRouter() = |
| 50 | + gossipRouter.submitOnEventThread {}.join() |
| 51 | + |
| 52 | + // ── broadcastOutbound ──────────────────────────────────────────────────── |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `broadcastOutbound - full message NOT sent to peer that requested partial`() { |
| 56 | + val test = newTest() |
| 57 | + |
| 58 | + test.mockRouter.subscribe(topicId) |
| 59 | + test.mockRouter.sendToSingle(controlExtensionsWithPartial()) |
| 60 | + test.mockRouter.sendToSingle(subscribeRpc(requestsPartial = true, supportsSendingPartial = true)) |
| 61 | + test.flushRouter() |
| 62 | + |
| 63 | + val msg = newMessage(topicId, 0L, "Hello".toByteArray()) |
| 64 | + test.gossipRouter.publish(msg) |
| 65 | + test.flushRouter() |
| 66 | + |
| 67 | + assertThat(test.mockRouter.inboundMessages.none { it.publishCount > 0 }).isTrue() |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `broadcastOutbound - full message still sent when partial extension disabled`() { |
| 72 | + val test = TwoRoutersTest(protocol = PubsubProtocol.Gossip_V_1_3) |
| 73 | + |
| 74 | + test.mockRouter.subscribe(topicId) |
| 75 | + test.flushRouter() |
| 76 | + |
| 77 | + val msg = newMessage(topicId, 0L, "Hello".toByteArray()) |
| 78 | + test.gossipRouter.publish(msg) |
| 79 | + test.mockRouter.waitForMessage { it.publishCount > 0 } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun `broadcastOutbound - full message still sent when peer did not request partial`() { |
| 84 | + val test = newTest() |
| 85 | + |
| 86 | + test.mockRouter.subscribe(topicId) |
| 87 | + test.mockRouter.sendToSingle(controlExtensionsWithPartial()) |
| 88 | + // No requestsPartial flag — peer supports sending but did not request |
| 89 | + test.mockRouter.sendToSingle(subscribeRpc(requestsPartial = false, supportsSendingPartial = true)) |
| 90 | + test.flushRouter() |
| 91 | + |
| 92 | + val msg = newMessage(topicId, 0L, "Hello".toByteArray()) |
| 93 | + test.gossipRouter.publish(msg) |
| 94 | + test.mockRouter.waitForMessage { it.publishCount > 0 } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `broadcastOutbound - full message still sent when peer supports partial at node level but no topic sub flag`() { |
| 99 | + val test = newTest() |
| 100 | + |
| 101 | + test.mockRouter.subscribe(topicId) |
| 102 | + // ControlExtensions: peer supports partial, but no SubOpts requestsPartial |
| 103 | + test.mockRouter.sendToSingle(controlExtensionsWithPartial()) |
| 104 | + test.flushRouter() |
| 105 | + |
| 106 | + val msg = newMessage(topicId, 0L, "Hello".toByteArray()) |
| 107 | + test.gossipRouter.publish(msg) |
| 108 | + test.mockRouter.waitForMessage { it.publishCount > 0 } |
| 109 | + } |
| 110 | + |
| 111 | + // ── broadcastInbound ───────────────────────────────────────────────────── |
| 112 | + |
| 113 | + @Test |
| 114 | + fun `broadcastInbound - forwarded full message NOT sent to peer that requested partial`() { |
| 115 | + val test = ManyRoutersTest( |
| 116 | + mockRouterCount = 2, |
| 117 | + protocol = PubsubProtocol.Gossip_V_1_3, |
| 118 | + enabledGossipExtensions = listOf(GossipExtension.PARTIAL_MESSAGES), |
| 119 | + partialMessagesHandler = nopPartialMessagesHandler, |
| 120 | + ) |
| 121 | + test.connectAll() |
| 122 | + |
| 123 | + // Subscribe mock routers first so gossipRouter.subscribe grafts them immediately. |
| 124 | + test.routers.forEach { it.router.subscribe(topicId) } |
| 125 | + test.gossipRouter.subscribe(topicId) |
| 126 | + |
| 127 | + // mockRouters[1] announces partial support and requests partial for the topic. |
| 128 | + test.mockRouters[1].sendToSingle(controlExtensionsWithPartial()) |
| 129 | + test.mockRouters[1].sendToSingle(subscribeRpc(requestsPartial = true, supportsSendingPartial = true)) |
| 130 | + |
| 131 | + // mockRouters[0] sends a full message that gossipRouter would normally forward. |
| 132 | + test.mockRouters[0].sendToSingle( |
| 133 | + Rpc.RPC.newBuilder().addPublish(newProtoMessage(topicId, 0L, "Hello".toByteArray())).build() |
| 134 | + ) |
| 135 | + test.fuzz.timeController.addTime(100) |
| 136 | + |
| 137 | + assertThat(test.mockRouters[1].inboundMessages.none { it.publishCount > 0 }).isTrue() |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + fun `broadcastInbound - forwarded full message IS sent to non-partial peer (sanity)`() { |
| 142 | + val test = ManyRoutersTest( |
| 143 | + mockRouterCount = 2, |
| 144 | + protocol = PubsubProtocol.Gossip_V_1_3, |
| 145 | + enabledGossipExtensions = listOf(GossipExtension.PARTIAL_MESSAGES), |
| 146 | + partialMessagesHandler = nopPartialMessagesHandler, |
| 147 | + ) |
| 148 | + test.connectAll() |
| 149 | + |
| 150 | + test.routers.forEach { it.router.subscribe(topicId) } |
| 151 | + test.gossipRouter.subscribe(topicId) |
| 152 | + |
| 153 | + // mockRouters[1] does NOT request partial — should receive the forwarded message. |
| 154 | + test.mockRouters[0].sendToSingle( |
| 155 | + Rpc.RPC.newBuilder().addPublish(newProtoMessage(topicId, 0L, "Hello".toByteArray())).build() |
| 156 | + ) |
| 157 | + |
| 158 | + test.mockRouters[1].waitForMessage { it.publishCount > 0 } |
| 159 | + } |
| 160 | +} |
0 commit comments