Skip to content

Commit

Permalink
feat(c-bindings): rln relay (#2544)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Mar 27, 2024
1 parent b5e4795 commit 2aa835e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
55 changes: 55 additions & 0 deletions library/waku_thread/config.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import std/[json, strformat, options]
import std/sequtils
import
libp2p/crypto/crypto,
libp2p/crypto/secp,
stew/shims/net,
../../waku/waku_enr/capabilities,
../../waku/common/utils/nat,
../../waku/factory/external_config,
../../waku/waku_core/message/default_values,
../../waku/node/waku_node,
../../waku/node/config,
../events/json_base_event
Expand Down Expand Up @@ -82,6 +84,20 @@ proc parseRelay(jsonNode: JsonNode, conf: var WakuNodeConf, errorResp: var strin

return true

proc parseClusterId(jsonNode: JsonNode, conf: var WakuNodeConf, errorResp: var string): bool =
if not jsonNode.contains("relay"):
errorResp = "relay attribute is required"
return false

if jsonNode.contains("clusterId"):
if jsonNode["clusterId"].kind != JsonNodeKind.JInt:
errorResp = "The clusterId config param should be an int"
return false
else:
conf.clusterId = uint32(jsonNode["clusterId"].getInt())

return true

proc parseStore(
jsonNode: JsonNode,
conf: var WakuNodeConf,
Expand Down Expand Up @@ -149,6 +165,29 @@ proc parseTopics(jsonNode: JsonNode, conf: var WakuNodeConf) =
else:
conf.pubsubTopics = @["/waku/2/default-waku/proto"]

proc parseRLNRelay(jsonNode: JsonNode, conf: var WakuNodeConf, errorResp: var string): bool =
if not jsonNode.contains("rln-relay"):
return true

let jsonNode = jsonNode["rln-relay"]
if not jsonNode.contains("enabled"):
errorResp = "rlnRelay.enabled attribute is required"
return false

conf.rlnRelay = jsonNode["enabled"].getBool()
conf.rlnRelayCredPath = jsonNode{"cred-password"}.getStr()
conf.rlnRelayEthClientAddress = EthRpcUrl.parseCmdArg(jsonNode{"eth-client-address"}.getStr("http://localhost:8540"))
conf.rlnRelayEthContractAddress = jsonNode{"eth-contract-address"}.getStr()
conf.rlnRelayCredPassword = jsonNode{"cred-password"}.getStr()
conf.rlnRelayUserMessageLimit = uint64(jsonNode{"user-message-limit"}.getInt(1))
conf.rlnEpochSizeSec = uint64(jsonNode{"epoch-sec"}.getInt(1))
conf.rlnRelayCredIndex = some(uint(jsonNode{"membership-index"}.getInt()))
conf.rlnRelayDynamic = jsonNode{"dynamic"}.getBool()
conf.rlnRelayTreePath = jsonNode{"tree-path"}.getStr()
conf.rlnRelayBandwidthThreshold = jsonNode{"bandwidth-threshold"}.getInt()

return true

proc parseConfig*(
configNodeJson: string,
conf: var WakuNodeConf,
Expand Down Expand Up @@ -199,6 +238,14 @@ proc parseConfig*(
errorResp = "Exception calling parseRelay: " & getCurrentExceptionMsg()
return false

# clusterId
try:
if not parseClusterId(jsonNode, conf, errorResp):
return false
except Exception, KeyError:
errorResp = "Exception calling parseClusterId: " & getCurrentExceptionMsg()
return false

# topics
try:
parseTopics(jsonNode, conf)
Expand All @@ -214,4 +261,12 @@ proc parseConfig*(
errorResp = "Exception calling parseStore: " & getCurrentExceptionMsg()
return false

# rln
try:
if not parseRLNRelay(jsonNode, conf, errorResp):
return false
except Exception, KeyError:
errorResp = "Exception calling parseRLNRelay: " & getCurrentExceptionMsg()
return false

return true
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,32 @@ proc createNode(configJson: cstring): Future[Result[WakuNode, string]] {.async.}
return err("exception calling parseConfig: " & getCurrentExceptionMsg())

# TODO: figure out how to extract default values from the config pragma
conf.clusterId = 0
conf.nat = "any"
conf.maxConnections = 50.uint16
conf.maxMessageSize = default_values.DefaultMaxWakuMessageSizeStr

# The Waku Network config (cluster-id=1)
if conf.clusterId == 1:
let twnClusterConf = ClusterConf.TheWakuNetworkConf()
if len(conf.shards) != 0:
conf.pubsubTopics = conf.shards.mapIt(twnClusterConf.pubsubTopics[it.uint16])
else:
conf.pubsubTopics = twnClusterConf.pubsubTopics

# Override configuration
conf.maxMessageSize = twnClusterConf.maxMessageSize
conf.clusterId = twnClusterConf.clusterId
conf.rlnRelay = twnClusterConf.rlnRelay
conf.rlnRelayEthContractAddress = twnClusterConf.rlnRelayEthContractAddress
conf.rlnRelayDynamic = twnClusterConf.rlnRelayDynamic
conf.rlnRelayBandwidthThreshold = twnClusterConf.rlnRelayBandwidthThreshold
conf.discv5Discovery = twnClusterConf.discv5Discovery
conf.discv5BootstrapNodes =
conf.discv5BootstrapNodes & twnClusterConf.discv5BootstrapNodes
conf.rlnEpochSizeSec = twnClusterConf.rlnEpochSizeSec
conf.rlnRelayUserMessageLimit = twnClusterConf.rlnRelayUserMessageLimit


let nodeRes = setupNode(conf).valueOr():
error "Failed setting up node", error = error
return err("Failed setting up node: " & $error)
Expand Down
2 changes: 1 addition & 1 deletion waku/factory/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ProtectedTopic* = object

type ShardIdx = distinct uint16

type EthRpcUrl = distinct string
type EthRpcUrl* = distinct string

type StartUpCommand* = enum
noCommand # default, runs waku
Expand Down

0 comments on commit 2aa835e

Please sign in to comment.