apply default values

This commit is contained in:
fryorcraken 2025-09-10 15:17:19 +10:00
parent 637e8407f7
commit d3073b9f8c
3 changed files with 195 additions and 153 deletions

View File

@ -16,7 +16,6 @@ type RlnConfig* = object
contractAddress*: string
chainId*: uint
epochSizeSec*: uint64
rpcApiUrls*: seq[string]
type MessageValidation* = object
maxMessageSizeBytes*: uint64
@ -26,7 +25,7 @@ type NetworkConf* = object
bootstrapNodes*: seq[string]
staticStoreNodes*: seq[string]
clusterId*: uint16
shardingMode*: ShardingMode
shardingMode*: Option[ShardingMode]
autoShardingConf*: Option[AutoShardingConf]
messageValidation*: Option[MessageValidation]
@ -36,8 +35,42 @@ type WakuMode* = enum
type LibWakuConf* = object
mode*: WakuMode
networkConf*: NetworkConf
networkConf*: Option[NetworkConf]
storeConfirmation*: bool
ethRpcEndpoints*: seq[string]
proc DefaultShardingMode(): ShardingMode =
return ShardingMode.Autosharding
proc DefaultAutoShardingConf(): AutoShardingConf =
return AutoShardingConf(numShardsInCluster: 1)
proc DefaultNetworkConf(): NetworkConf =
return NetworkConf(
bootstrapNodes:
@[
"enrtree://AIRVQ5DDA4FFWLRBCHJWUWOO6X6S4ZTZ5B667LQ6AJU6PEYDLRD5O@sandbox.waku.nodes.status.im"
],
staticStoreNodes: @[], # TODO
clusterId: 1,
shardingMode: some(ShardingMode.AutoSharding),
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 8)),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 153600,
rlnConfig: some (
RlnConfig(
contractAddress: "0xB9cd878C90E49F797B4431fBF4fb333108CB90e6",
chain_id: 59141,
epoch_size_sec: 600, # 10 minutes
)
),
)
),
)
proc DefaultMessageValidation(): MessageValidation =
return MessageValidation(maxMessageSizeBytes: 153600, rlnConfig: none(RlnConfig))
proc toWakuConf*(libConf: LibWakuConf): Result[WakuConf, string] =
var b = WakuConfBuilder.init()
@ -61,18 +94,17 @@ proc toWakuConf*(libConf: LibWakuConf): Result[WakuConf, string] =
#TODO: store confirmation
## Network Conf
let networkConf = libConf.networkConf
let networkConf = libConf.networkConf.get(DefaultNetworkConf())
# Set cluster ID
b.withClusterId(networkConf.clusterId)
# Set sharding configuration
case networkConf.shardingMode
case networkConf.shardingMode.get(DefaultShardingMode())
of AutoSharding:
b.withShardingConf(ShardingConfKind.AutoSharding)
if networkConf.autoShardingConf.isSome():
let autoConf = networkConf.autoShardingConf.get()
b.withNumShardsInCluster(autoConf.numShardsInCluster)
let autoShardingConf = networkConf.autoShardingConf.get(DefaultAutoShardingConf())
b.withNumShardsInCluster(autoShardingConf.numShardsInCluster)
of StaticSharding:
b.withShardingConf(ShardingConfKind.StaticSharding)
@ -86,23 +118,23 @@ proc toWakuConf*(libConf: LibWakuConf): Result[WakuConf, string] =
b.withStaticNodes(networkConf.staticStoreNodes)
# Set message validation
if networkConf.messageValidation.isSome():
let msgValidation = networkConf.messageValidation.get()
b.withMaxMessageSize(msgValidation.maxMessageSizeBytes)
# Set RLN config if provided
if msgValidation.rlnConfig.isSome():
let rlnConfig = msgValidation.rlnConfig.get()
b.rlnRelayConf.withEnabled(true)
b.rlnRelayConf.withEthContractAddress(rlnConfig.contractAddress)
b.rlnRelayConf.withChainId(rlnConfig.chainId)
b.rlnRelayConf.withEpochSizeSec(rlnConfig.epochSizeSec)
b.rlnRelayConf.withDynamic(true)
b.rlnRelayConf.withEthClientUrls(rlnConfig.rpcApiUrls)
let msgValidation = networkConf.messageValidation.get(DefaultMessageValidation())
b.withMaxMessageSize(msgValidation.maxMessageSizeBytes)
# TODO: we should get rid of those two
b.rlnRelayconf.withUserMessageLimit(100)
b.rlnRelayConf.withTreePath("./rln_tree")
# Set RLN config if provided
if msgValidation.rlnConfig.isSome():
let rlnConfig = msgValidation.rlnConfig.get()
b.rlnRelayConf.withEnabled(true)
b.rlnRelayConf.withEthContractAddress(rlnConfig.contractAddress)
b.rlnRelayConf.withChainId(rlnConfig.chainId)
b.rlnRelayConf.withEpochSizeSec(rlnConfig.epochSizeSec)
b.rlnRelayConf.withDynamic(true)
b.rlnRelayConf.withEthClientUrls(libConf.ethRpcEndpoints)
# TODO: we should get rid of those two
b.rlnRelayconf.withUserMessageLimit(100)
b.rlnRelayConf.withTreePath("./rln_tree")
## Various configurations
b.withNatStrategy("any")

View File

@ -8,13 +8,15 @@ suite "LibWaku - createNode":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: some(StaticSharding),
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
)
),
storeConfirmation: false,
)
@ -33,13 +35,15 @@ suite "LibWaku - createNode":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 42,
shardingMode: AutoSharding,
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 8)),
messageValidation: none(MessageValidation),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 42,
shardingMode: some(AutoSharding),
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 8)),
messageValidation: none(MessageValidation),
)
),
storeConfirmation: false,
)
@ -58,24 +62,26 @@ suite "LibWaku - createNode":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes:
@[
"enr:-QESuEC1p_s3xJzAC_XlOuuNrhVUETmfhbm1wxRGis0f7DlqGSw2FM-p2Vn7gmfkTTnAe8Ys2cgGBN8ufJnvzKQFZqFMBgmlkgnY0iXNlY3AyNTZrMaEDS8-D878DrdbNwcuY-3p1qdDp5MOoCurhdsNPJTXZ3c5g3RjcIJ2X4N1ZHCCd2g"
],
staticStoreNodes:
@[
"/ip4/127.0.0.1/tcp/60000/p2p/16Uuu2HBmAcHvhLqQKwSSbX6BG5JLWUDRcaLVrehUVqpw7fz1hbYc"
],
clusterId: 99,
shardingMode: AutoSharding,
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 16)),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 1024'u64 * 1024'u64, # 1MB
rlnConfig: none(RlnConfig),
)
),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes:
@[
"enr:-QESuEC1p_s3xJzAC_XlOuuNrhVUETmfhbm1wxRGis0f7DlqGSw2FM-p2Vn7gmfkTTnAe8Ys2cgGBN8ufJnvzKQFZqFMBgmlkgnY0iXNlY3AyNTZrMaEDS8-D878DrdbNwcuY-3p1qdDp5MOoCurhdsNPJTXZ3c5g3RjcIJ2X4N1ZHCCd2g"
],
staticStoreNodes:
@[
"/ip4/127.0.0.1/tcp/60000/p2p/16Uuu2HBmAcHvhLqQKwSSbX6BG5JLWUDRcaLVrehUVqpw7fz1hbYc"
],
clusterId: 99,
shardingMode: some(AutoSharding),
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 16)),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 1024'u64 * 1024'u64, # 1MB
rlnConfig: none(RlnConfig),
)
),
)
),
storeConfirmation: true,
)

View File

@ -8,13 +8,15 @@ suite "LibWaku Conf - toWakuConf":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: some(ShardingMode.StaticSharding),
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
)
),
storeConfirmation: false,
)
@ -37,13 +39,15 @@ suite "LibWaku Conf - toWakuConf":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 42,
shardingMode: AutoSharding,
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 16)),
messageValidation: none(MessageValidation),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 42,
shardingMode: some(ShardingMode.AutoSharding),
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 16)),
messageValidation: none(MessageValidation),
)
),
storeConfirmation: false,
)
@ -69,13 +73,15 @@ suite "LibWaku Conf - toWakuConf":
]
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: bootstrapNodes,
staticStoreNodes: @[],
clusterId: 1,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: bootstrapNodes,
staticStoreNodes: @[],
clusterId: 1,
shardingMode: some(ShardingMode.StaticSharding),
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
)
),
storeConfirmation: false,
)
@ -100,13 +106,15 @@ suite "LibWaku Conf - toWakuConf":
]
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: staticStoreNodes,
clusterId: 1,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: staticStoreNodes,
clusterId: 1,
shardingMode: some(ShardingMode.StaticSharding),
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
)
),
storeConfirmation: false,
)
@ -125,18 +133,20 @@ suite "LibWaku Conf - toWakuConf":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 100'u64 * 1024'u64, # 100kB
rlnConfig: none(RlnConfig),
)
),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: some(ShardingMode.StaticSharding),
autoShardingConf: none(AutoShardingConf),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 100'u64 * 1024'u64, # 100kB
rlnConfig: none(RlnConfig),
)
),
)
),
storeConfirmation: false,
)
@ -155,27 +165,29 @@ suite "LibWaku Conf - toWakuConf":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 150'u64 * 1024'u64, # 150KB
rlnConfig: some(
RlnConfig(
contractAddress: "0x1234567890123456789012345678901234567890",
chainId: 1'u,
epochSizeSec: 600'u64,
rpcApiUrls: @["http://127.0.0.1:1111"],
)
),
)
),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 1,
shardingMode: some(ShardingMode.StaticSharding),
autoShardingConf: none(AutoShardingConf),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 150'u64 * 1024'u64, # 150KB
rlnConfig: some(
RlnConfig(
contractAddress: "0x1234567890123456789012345678901234567890",
chainId: 1'u,
epochSizeSec: 600'u64,
)
),
)
),
)
),
storeConfirmation: false,
ethRpcEndpoints: @["http://127.0.0.1:1111"],
)
## When
@ -200,33 +212,35 @@ suite "LibWaku Conf - toWakuConf":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes:
@[
"enr:-QESuEC1p_s3xJzAC_XlOuuNrhVUETmfhbm1wxRGis0f7DlqGSw2FM-p2Vn7gmfkTTnAe8Ys2cgGBN8ufJnvzKQFZqFMBgmlkgnY0iXNlY3AyNTZrMaEDS8-D878DrdbNwcuY-3p1qdDp5MOoCurhdsNPJTXZ3c5g3RjcIJ2X4N1ZHCCd2g"
],
staticStoreNodes:
@[
"/ip4/127.0.0.1/tcp/60000/p2p/16Uuu2HBmAcHvhLqQKwSSbX6BG5JLWUDRcaLVrehUVqpw7fz1hbYc"
],
clusterId: 99,
shardingMode: AutoSharding,
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 8)),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 512'u64 * 1024'u64, # 512KB
rlnConfig: some(
RlnConfig(
contractAddress: "0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
chainId: 5'u, # Goerli
epochSizeSec: 300'u64,
rpcApiUrls: @["https://127.0.0.1:8333"],
)
),
)
),
networkConf: some(
libwaku_conf.NetworkConf(
bootstrapNodes:
@[
"enr:-QESuEC1p_s3xJzAC_XlOuuNrhVUETmfhbm1wxRGis0f7DlqGSw2FM-p2Vn7gmfkTTnAe8Ys2cgGBN8ufJnvzKQFZqFMBgmlkgnY0iXNlY3AyNTZrMaEDS8-D878DrdbNwcuY-3p1qdDp5MOoCurhdsNPJTXZ3c5g3RjcIJ2X4N1ZHCCd2g"
],
staticStoreNodes:
@[
"/ip4/127.0.0.1/tcp/60000/p2p/16Uuu2HBmAcHvhLqQKwSSbX6BG5JLWUDRcaLVrehUVqpw7fz1hbYc"
],
clusterId: 99,
shardingMode: some(ShardingMode.AutoSharding),
autoShardingConf: some(AutoShardingConf(numShardsInCluster: 8)),
messageValidation: some(
MessageValidation(
maxMessageSizeBytes: 512'u64 * 1024'u64, # 512KB
rlnConfig: some(
RlnConfig(
contractAddress: "0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
chainId: 5'u, # Goerli
epochSizeSec: 300'u64,
)
),
)
),
)
),
storeConfirmation: true,
ethRpcEndpoints: @["https://127.0.0.1:8333"],
)
## When
@ -276,18 +290,7 @@ suite "LibWaku Conf - toWakuConf":
test "Minimal configuration":
## Given
let libConf = LibWakuConf(
mode: Relay,
networkConf: libwaku_conf.NetworkConf(
bootstrapNodes: @[],
staticStoreNodes: @[],
clusterId: 0,
shardingMode: StaticSharding,
autoShardingConf: none(AutoShardingConf),
messageValidation: none(MessageValidation),
),
storeConfirmation: false,
)
let libConf = LibWakuConf(mode: Relay, ethRpcEndpoints: @["http://someaddress"])
## When
let wakuConfRes = toWakuConf(libConf)
@ -298,6 +301,7 @@ suite "LibWaku Conf - toWakuConf":
wakuConf.validate().isOkOr:
raiseAssert error
check:
wakuConf.clusterId == 0
wakuConf.shardingConf.kind == ShardingConfKind.StaticSharding
wakuConf.clusterId == 1
wakuConf.shardingConf.kind == ShardingConfKind.AutoSharding
wakuConf.shardingConf.numShardsInCluster == 8
wakuConf.staticNodes.len == 0