logos-messaging-nim/tests/factory/test_external_config.nim
fryorcraken 994d485b49 chore!: make sharding configuration explicit (#3468)
* Reserve `networkconfig` name to waku network related settings

* Rename cluster conf to network conf

 A `NetworkConf` is a Waku network configuration.

# Conflicts:
#	tests/factory/test_waku_conf.nim

# Conflicts:
#	tests/factory/test_waku_conf.nim

* Improve sharding configuration

A smarter data types simplifies the logic.

* Fixing tests

* fixup! rename to endpointConf

* wip: autosharding is a specific configuration state and treat it like
it

# Conflicts:
#	waku/factory/external_config.nim

* refactor lightpush handler

some metrics error reporting were missing

# Conflicts:
#	waku/waku_lightpush/protocol.nim

* test_node_factory tests pass

* remove warnings

* fix tests

* Revert eager previous replace-all command

* fix up build tools compilation

* metadata is used to store cluster id

* Mount relay routes in static sharding

* Rename activeRelayShards to subscribeShards

To make it clearer that these are the shards the node will subscribe to.

* Remove unused msg var

* Improve error handling

* Set autosharding as default, with 1 shard in network

Also makes shards to subscribe to all shards in auto sharding, none in
static sharding.
2025-07-04 17:10:53 +10:00

262 lines
7.7 KiB
Nim

{.used.}
import
std/options,
testutils/unittests,
chronos,
libp2p/crypto/[crypto, secp],
libp2p/multiaddress,
nimcrypto/utils,
secp256k1,
confutils,
stint
import
../../waku/factory/external_config,
../../waku/factory/networks_config,
../../waku/factory/waku_conf,
../../waku/common/logging,
../../waku/common/utils/parse_size_units
suite "Waku external config - default values":
test "Default sharding value":
## Setup
let defaultShardingMode = AutoSharding
let defaultNumShardsInCluster = 1.uint16
let defaultSubscribeShards = @[0.uint16]
## Given
let preConfig = defaultWakuNodeConf().get()
## When
let res = preConfig.toWakuConf()
assert res.isOk(), $res.error
## Then
let conf = res.get()
check conf.shardingConf.kind == defaultShardingMode
check conf.shardingConf.numShardsInCluster == defaultNumShardsInCluster
check conf.subscribeShards == defaultSubscribeShards
test "Default shards value in static sharding":
## Setup
let defaultSubscribeShards: seq[uint16] = @[]
## Given
var preConfig = defaultWakuNodeConf().get()
preConfig.numShardsInNetwork = 0.uint16
## When
let res = preConfig.toWakuConf()
assert res.isOk(), $res.error
## Then
let conf = res.get()
check conf.subscribeShards == defaultSubscribeShards
suite "Waku external config - apply preset":
test "Preset is TWN":
## Setup
let expectedConf = NetworkConf.TheWakuNetworkConf()
## Given
let preConfig = WakuNodeConf(
cmd: noCommand,
preset: "twn",
relay: true,
ethClientUrls: @["http://someaddress".EthRpcUrl],
rlnRelayTreePath: "/tmp/sometreepath",
)
## When
let res = preConfig.toWakuConf()
assert res.isOk(), $res.error
## Then
let conf = res.get()
check conf.maxMessageSizeBytes ==
uint64(parseCorrectMsgSize(expectedConf.maxMessageSize))
check conf.clusterId == expectedConf.clusterId
check conf.rlnRelayConf.isSome() == expectedConf.rlnRelay
if conf.rlnRelayConf.isSome():
let rlnRelayConf = conf.rlnRelayConf.get()
check rlnRelayConf.ethContractAddress == expectedConf.rlnRelayEthContractAddress
check rlnRelayConf.dynamic == expectedConf.rlnRelayDynamic
check rlnRelayConf.chainId == expectedConf.rlnRelayChainId
check rlnRelayConf.epochSizeSec == expectedConf.rlnEpochSizeSec
check rlnRelayConf.userMessageLimit == expectedConf.rlnRelayUserMessageLimit
check conf.shardingConf.kind == expectedConf.shardingConf.kind
check conf.shardingConf.numShardsInCluster ==
expectedConf.shardingConf.numShardsInCluster
check conf.discv5Conf.isSome() == expectedConf.discv5Discovery
if conf.discv5Conf.isSome():
let discv5Conf = conf.discv5Conf.get()
check discv5Conf.bootstrapNodes == expectedConf.discv5BootstrapNodes
test "Subscribes to all valid shards in twn":
## Setup
let expectedConf = NetworkConf.TheWakuNetworkConf()
## Given
let shards: seq[uint16] = @[0, 1, 2, 3, 4, 5, 6, 7]
let preConfig = WakuNodeConf(cmd: noCommand, preset: "twn", shards: shards)
## When
let res = preConfig.toWakuConf()
assert res.isOk(), $res.error
## Then
let conf = res.get()
check conf.subscribeShards.len == expectedConf.shardingConf.numShardsInCluster.int
test "Subscribes to some valid shards in twn":
## Setup
let expectedConf = NetworkConf.TheWakuNetworkConf()
## Given
let shards: seq[uint16] = @[0, 4, 7]
let preConfig = WakuNodeConf(cmd: noCommand, preset: "twn", shards: shards)
## When
let resConf = preConfig.toWakuConf()
assert resConf.isOk(), $resConf.error
## Then
let conf = resConf.get()
assert conf.subscribeShards.len() == shards.len()
for index, shard in shards:
assert shard in conf.subscribeShards
test "Subscribes to invalid shards in twn":
## Setup
## Given
let shards: seq[uint16] = @[0, 4, 7, 10]
let preConfig = WakuNodeConf(cmd: noCommand, preset: "twn", shards: shards)
## When
let res = preConfig.toWakuConf()
## Then
assert res.isErr(), "Invalid shard was accepted"
test "Apply TWN preset when cluster id = 1":
## Setup
let expectedConf = NetworkConf.TheWakuNetworkConf()
## Given
let preConfig = WakuNodeConf(
cmd: noCommand,
clusterId: 1.uint16,
relay: true,
ethClientUrls: @["http://someaddress".EthRpcUrl],
rlnRelayTreePath: "/tmp/sometreepath",
)
## When
let res = preConfig.toWakuConf()
assert res.isOk(), $res.error
## Then
let conf = res.get()
check conf.maxMessageSizeBytes ==
uint64(parseCorrectMsgSize(expectedConf.maxMessageSize))
check conf.clusterId == expectedConf.clusterId
check conf.rlnRelayConf.isSome() == expectedConf.rlnRelay
if conf.rlnRelayConf.isSome():
let rlnRelayConf = conf.rlnRelayConf.get()
check rlnRelayConf.ethContractAddress == expectedConf.rlnRelayEthContractAddress
check rlnRelayConf.dynamic == expectedConf.rlnRelayDynamic
check rlnRelayConf.chainId == expectedConf.rlnRelayChainId
check rlnRelayConf.epochSizeSec == expectedConf.rlnEpochSizeSec
check rlnRelayConf.userMessageLimit == expectedConf.rlnRelayUserMessageLimit
check conf.shardingConf.kind == expectedConf.shardingConf.kind
check conf.shardingConf.numShardsInCluster ==
expectedConf.shardingConf.numShardsInCluster
check conf.discv5Conf.isSome() == expectedConf.discv5Discovery
if conf.discv5Conf.isSome():
let discv5Conf = conf.discv5Conf.get()
check discv5Conf.bootstrapNodes == expectedConf.discv5BootstrapNodes
suite "Waku external config - node key":
test "Passed node key is used":
## Setup
let nodeKeyStr =
"0011223344556677889900aabbccddeeff0011223344556677889900aabbccddeeff"
let nodekey = block:
let key = SkPrivateKey.init(utils.fromHex(nodeKeyStr)).tryGet()
crypto.PrivateKey(scheme: Secp256k1, skkey: key)
## Given
let config = WakuNodeConf.load(version = "", cmdLine = @["--nodekey=" & nodeKeyStr])
## When
let res = config.toWakuConf()
assert res.isOk(), $res.error
## Then
let resKey = res.get().nodeKey
assert utils.toHex(resKey.getRawBytes().get()) ==
utils.toHex(nodekey.getRawBytes().get())
suite "Waku external config - Shards":
test "Shards are valid":
## Setup
## Given
let shards: seq[uint16] = @[0, 2, 4]
let numShardsInNetwork = 5.uint16
let wakuNodeConf = WakuNodeConf(
cmd: noCommand, shards: shards, numShardsInNetwork: numShardsInNetwork
)
## When
let res = wakuNodeConf.toWakuConf()
assert res.isOk(), $res.error
## Then
let wakuConf = res.get()
let vRes = wakuConf.validate()
assert vRes.isOk(), $vRes.error
test "Shards are not in range":
## Setup
## Given
let shards: seq[uint16] = @[0, 2, 5]
let numShardsInNetwork = 5.uint16
let wakuNodeConf = WakuNodeConf(
cmd: noCommand, shards: shards, numShardsInNetwork: numShardsInNetwork
)
## When
let res = wakuNodeConf.toWakuConf()
## Then
assert res.isErr(), "Invalid shard was accepted"
test "Shard is passed without num shards":
## Setup
## Given
let wakuNodeConf = WakuNodeConf.load(version = "", cmdLine = @["--shard=0"])
## When
let res = wakuNodeConf.toWakuConf()
## Then
let wakuConf = res.get()
let vRes = wakuConf.validate()
assert vRes.isOk(), $vRes.error
test "Imvalid shard is passed without num shards":
## Setup
## Given
let wakuNodeConf = WakuNodeConf.load(version = "", cmdLine = @["--shard=32"])
## When
let res = wakuNodeConf.toWakuConf()
## Then
assert res.isErr(), "Invalid shard was accepted"