From 9cbb4e7338c62abe9345fe70cc81694373be87e2 Mon Sep 17 00:00:00 2001 From: Fabiana Cecin Date: Mon, 20 Apr 2026 08:48:27 -0300 Subject: [PATCH] fix: prefer --num-shards-in-network over preset (#3816) * fill numShardsInCluster from preset when builder slot is none * add regression tests --- tests/factory/test_waku_conf.nim | 48 +++++++++++++++++++ .../conf_builder/waku_conf_builder.nim | 13 +++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/tests/factory/test_waku_conf.nim b/tests/factory/test_waku_conf.nim index 9d05f7fb5..eeacf791b 100644 --- a/tests/factory/test_waku_conf.nim +++ b/tests/factory/test_waku_conf.nim @@ -213,6 +213,54 @@ suite "Waku Conf - build with cluster conf": check rlnRelayConf.epochSizeSec == networkConf.rlnEpochSizeSec check rlnRelayConf.userMessageLimit == userMessageLimit.uint + test "num-shards-in-network > 0 overrides preset": + ## Setup + let networkConf = NetworkConf.LogosDevConf() + var builder = WakuConfBuilder.init() + + # Sanity check + check networkConf.shardingConf.kind == AutoSharding + check networkConf.shardingConf.numShardsInCluster > 1 + + ## Given: preset says >1 shards but user explicitly sets 1 + builder.withNetworkConf(networkConf) + builder.withNumShardsInCluster(1) + builder.withShardingConf(AutoSharding) + + ## When + let conf = builder.build().expect("build should succeed") + + ## Then: user value wins, not preset + conf.validate().expect("conf should validate") + check conf.shardingConf.kind == AutoSharding + check conf.shardingConf.numShardsInCluster == 1 + + test "num-shards-in-network == 0 does not override preset": + ## Passing an AutoSharding preset and trying to override with + ## --num-shards-in-network=0 (which is StaticSharding) doesn't work. + ## Note that --num-shards-in-network=0 and omitting the switch are + ## internally the same. Promoting the config to an Option[uint16] is + ## probably not worth it since overriding an AutoSharding preset with + ## StaticSharding shouldn't make any sense (that is, no use case). + + ## Given: emulate --preset=logos.dev --num-shards-in-network=0 + let networkConf = NetworkConf.LogosDevConf() + var builder = WakuConfBuilder.init() + builder.withNetworkConf(networkConf) + # Note: builder.withNumShardsInCluster() is not called when the + # value that comes from the CLI path is 0 (which means it was + # either set to 0 or was left unset). + builder.withShardingConf(StaticSharding) + + ## When + let conf = builder.build().expect("build should succeed") + + ## Then: preset wins and StaticSharding user intent is lost + conf.validate().expect("conf should validate") + check conf.shardingConf.kind == networkConf.shardingConf.kind + check conf.shardingConf.numShardsInCluster == + networkConf.shardingConf.numShardsInCluster + suite "Waku Conf - node key": test "Node key is generated": ## Setup diff --git a/waku/factory/conf_builder/waku_conf_builder.nim b/waku/factory/conf_builder/waku_conf_builder.nim index 956d733d3..78dbd9eb9 100644 --- a/waku/factory/conf_builder/waku_conf_builder.nim +++ b/waku/factory/conf_builder/waku_conf_builder.nim @@ -299,7 +299,6 @@ proc buildShardingConf( bNumShardsInCluster: Option[uint16], bSubscribeShards: Option[seq[uint16]], ): (ShardingConf, seq[uint16]) = - echo "bSubscribeShards: ", bSubscribeShards case bShardingConfKind.get(AutoSharding) of StaticSharding: (ShardingConf(kind: StaticSharding), bSubscribeShards.get(@[])) @@ -374,17 +373,17 @@ proc applyNetworkConf(builder: var WakuConfBuilder) = warn "Sharding Conf was provided alongside a network conf", used = networkConf.shardingConf.kind, discarded = builder.shardingConf - if builder.numShardsInCluster.isSome(): - warn "Num Shards In Cluster was provided alongside a network conf", - used = networkConf.shardingConf.numShardsInCluster, - discarded = builder.numShardsInCluster - case networkConf.shardingConf.kind of StaticSharding: builder.shardingConf = some(StaticSharding) of AutoSharding: builder.shardingConf = some(AutoSharding) - builder.numShardsInCluster = some(networkConf.shardingConf.numShardsInCluster) + if builder.numShardsInCluster.isSome(): + warn "Num Shards In Cluster overrides network conf preset", + used = builder.numShardsInCluster.get(), + ignored = networkConf.shardingConf.numShardsInCluster + else: + builder.numShardsInCluster = some(networkConf.shardingConf.numShardsInCluster) if networkConf.discv5Discovery: if builder.discv5Conf.enabled.isNone: