diff --git a/libp2p/src/test/kotlin/io/libp2p/pubsub/gossip/GossipV1_1Tests.kt b/libp2p/src/test/kotlin/io/libp2p/pubsub/gossip/GossipV1_1Tests.kt index e69f1357..3750803e 100644 --- a/libp2p/src/test/kotlin/io/libp2p/pubsub/gossip/GossipV1_1Tests.kt +++ b/libp2p/src/test/kotlin/io/libp2p/pubsub/gossip/GossipV1_1Tests.kt @@ -3,6 +3,7 @@ package io.libp2p.pubsub.gossip import com.google.common.util.concurrent.AtomicDouble +import com.google.protobuf.ByteString import io.libp2p.core.PeerId import io.libp2p.core.pubsub.MessageApi import io.libp2p.core.pubsub.RESULT_IGNORE @@ -1169,4 +1170,46 @@ class GossipV1_1Tests { } ) } + + @Test + fun testMaxPeersAcceptedInPruneMsg() { + val test = TwoRoutersTest() + val topic = "topic1" + + test.mockRouter.subscribe(topic) + test.gossipRouter.subscribe(topic) + + // 2 heartbeats - the topic should be GRAFTed + test.fuzz.timeController.addTime(2.seconds) + + fun createPruneMessage(peersCount: Int): Rpc.RPC { + val peerInfos = List(peersCount) { + Rpc.PeerInfo.newBuilder() + .setPeerID(PeerId.random().bytes.toProtobuf()) + .setSignedPeerRecord(ByteString.EMPTY) + .build() + } + return Rpc.RPC.newBuilder().setControl( + Rpc.ControlMessage.newBuilder().addPrune( + Rpc.ControlPrune.newBuilder() + .setTopicID(topic) + .addAllPeers(peerInfos) + ) + ).build() + } + + test.mockRouter.sendToSingle( + createPruneMessage(test.gossipRouter.params.maxPeersAcceptedInPruneMsg + 1) + ) + + // prune message should be dropped because too many peers + assertEquals(1, test.gossipRouter.mesh[topic]!!.size) + + test.mockRouter.sendToSingle( + createPruneMessage(test.gossipRouter.params.maxPeersAcceptedInPruneMsg) + ) + + // prune message should now be processed + assertEquals(0, test.gossipRouter.mesh[topic]!!.size) + } }