Skip to content

Commit bd50c9c

Browse files
committed
add constants and PubsubMessage size method
1 parent 3805ba7 commit bd50c9c

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

libp2p/src/main/kotlin/io/libp2p/pubsub/PubsubRouter.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ interface PubsubMessage {
2222
val topics: List<Topic>
2323
get() = protobufMessage.topicIDsList
2424

25+
val size: Int
26+
get() = protobufMessage.data.size()
27+
2528
fun messageSha256() = sha256(protobufMessage.toByteArray())
2629

2730
override fun equals(other: Any?): Boolean

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ fun defaultDLazy(D: Int) = D
2222
fun defaultDScore(D: Int) = D * 2 / 3
2323
fun defaultDOut(D: Int, DLow: Int) = min(D / 2, max(DLow - 1, 0))
2424

25+
// floodPublishMaxMessageSizeThreshold shortcuts
26+
const val NEVER_FLOOD_PUBLISH = 0
27+
const val ALWAYS_FLOOD_PUBLISH = Int.MAX_VALUE
28+
2529
/**
2630
* Parameters of Gossip 1.1 router
2731
*/
@@ -116,9 +120,12 @@ data class GossipParams(
116120
* published using flood publishing mode.
117121
* When a message size is <= [floodPublishMaxMessageSizeThreshold], published messages are forwarded
118122
* to all peers with score >= to [GossipScoreParams.publishThreshold]
119-
* The default is 0 KiB (never flood publish).
123+
*
124+
* [NEVER_FLOOD_PUBLISH] and [ALWAYS_FLOOD_PUBLISH] can be used as shortcuts.
125+
*
126+
* The default is [NEVER_FLOOD_PUBLISH] (0 KiB).
120127
*/
121-
val floodPublishMaxMessageSizeThreshold: Int = 0,
128+
val floodPublishMaxMessageSizeThreshold: Int = NEVER_FLOOD_PUBLISH,
122129

123130
/**
124131
* [gossipFactor] affects how many peers we will emit gossip to at each heartbeat.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ open class GossipRouter(
400400
override fun broadcastOutbound(msg: PubsubMessage): CompletableFuture<Unit> {
401401
msg.topics.forEach { lastPublished[it] = currentTimeSupplier() }
402402

403-
val floodPublish = msg.protobufMessage.data.size() <= params.floodPublishMaxMessageSizeThreshold
403+
val floodPublish = msg.size <= params.floodPublishMaxMessageSizeThreshold
404404

405405
val peers =
406406
if (floodPublish) {
@@ -615,7 +615,7 @@ open class GossipRouter(
615615

616616
private fun iDontWant(msg: PubsubMessage, receivedFrom: PeerHandler? = null) {
617617
if (!this.protocol.supportsIDontWant()) return
618-
if (msg.protobufMessage.data.size() < params.iDontWantMinMessageSizeThreshold) return
618+
if (msg.size < params.iDontWantMinMessageSizeThreshold) return
619619
// we need to send IDONTWANT messages to mesh peers immediately in order for them to have an effect
620620
msg.topics
621621
.mapNotNull { mesh[it] }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit
2222

2323
class GossipPubsubRouterTest : PubsubRouterTest(
2424
createGossipFuzzRouterFactory {
25-
GossipRouterBuilder(params = GossipParams(3, 3, 100, floodPublishMaxMessageSizeThreshold = 0))
25+
GossipRouterBuilder(params = GossipParams(3, 3, 100, floodPublishMaxMessageSizeThreshold = NEVER_FLOOD_PUBLISH))
2626
}
2727
) {
2828

@@ -59,7 +59,7 @@ class GossipPubsubRouterTest : PubsubRouterTest(
5959
// this is to test ihave/iwant
6060
fuzz.timeController.addTime(Duration.ofMillis(1))
6161

62-
val r = { GossipRouterBuilder(params = GossipParams(3, 3, 3, DOut = 0, DLazy = 1000, floodPublishMaxMessageSizeThreshold = 0)) }
62+
val r = { GossipRouterBuilder(params = GossipParams(3, 3, 3, DOut = 0, DLazy = 1000, floodPublishMaxMessageSizeThreshold = NEVER_FLOOD_PUBLISH)) }
6363
val routerCenter = fuzz.createTestGossipRouter(r)
6464
allRouters.add(0, routerCenter)
6565

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ class GossipV1_1Tests : GossipTestsBase() {
532532
fun testNotFloodPublish() {
533533
val message = newMessage("topic1", 0L, "Hello-0".toByteArray())
534534
val appScore = mutableMapOf<PeerId, Double>().withDefault { 0.0 }
535-
val coreParams = GossipParams(3, 3, 3, floodPublishMaxMessageSizeThreshold = message.protobufMessage.data.size() - 1)
535+
val coreParams = GossipParams(3, 3, 3, floodPublishMaxMessageSizeThreshold = message.size - 1)
536536
val peerScoreParams = GossipPeerScoreParams(appSpecificScore = { appScore.getValue(it) })
537537
val scoreParams = GossipScoreParams(peerScoreParams = peerScoreParams)
538538
val test = ManyRoutersTest(params = coreParams, scoreParams = scoreParams)
@@ -560,7 +560,7 @@ class GossipV1_1Tests : GossipTestsBase() {
560560
fun testFloodPublish() {
561561
val message = newMessage("topic1", 0L, "Hello-0".toByteArray())
562562
val appScore = mutableMapOf<PeerId, Double>().withDefault { 0.0 }
563-
val coreParams = GossipParams(3, 3, 3, floodPublishMaxMessageSizeThreshold = message.protobufMessage.data.size())
563+
val coreParams = GossipParams(3, 3, 3, floodPublishMaxMessageSizeThreshold = message.size)
564564
val peerScoreParams = GossipPeerScoreParams(
565565
appSpecificScore = { appScore.getValue(it) },
566566
appSpecificWeight = 1.0

0 commit comments

Comments
 (0)