Skip to content

Commit

Permalink
chore(rln-relay): add chain-id flag to wakunode and restrict usage if…
Browse files Browse the repository at this point in the history
… mismatches rpc provider (#2858)
  • Loading branch information
rymnc authored Jun 28, 2024
1 parent dad054d commit 05356ab
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
19 changes: 18 additions & 1 deletion tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import
../testlib/common,
./utils

const CHAIN_ID = 1337

proc generateCredentials(rlnInstance: ptr RLN): IdentityCredential =
let credRes = membershipKeyGen(rlnInstance)
return credRes.get()
Expand Down Expand Up @@ -140,7 +142,7 @@ proc runAnvil(): Process =
anvilPath,
args = [
"--port", "8540", "--gas-limit", "300000000000000", "--balance", "1000000000",
"--chain-id", "1337",
"--chain-id", $CHAIN_ID,
],
options = {poUsePath},
)
Expand Down Expand Up @@ -195,6 +197,7 @@ proc setup(): Future[OnchainGroupManager] {.async.} =
let manager = OnchainGroupManager(
ethClientUrl: EthClient,
ethContractAddress: $contractAddress,
chainId: CHAIN_ID,
ethPrivateKey: pk,
rlnInstance: rlnInstance,
)
Expand All @@ -218,6 +221,20 @@ suite "Onchain group manager":

await manager.stop()

asyncTest "should error on initialization when chainId does not match":
let manager = await setup()
manager.chainId = CHAIN_ID + 1

(await manager.init()).isErrOr:
raiseAssert "Expected error when chainId does not match"

asyncTest "should initialize when chainId is set to 0":
let manager = await setup()
manager.chainId = 0

(await manager.init()).isOkOr:
raiseAssert $error

asyncTest "should error on initialization when loaded metadata does not match":
let manager = await setup()
(await manager.init()).isOkOr:
Expand Down
4 changes: 2 additions & 2 deletions tools/rln_keystore_generator/rln_keystore_generator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ proc doRlnKeystoreGenerator*(conf: WakuNodeConf) =
debug "Transaction hash", txHash = groupManager.registrationTxHash.get()

info "Your membership has been registered on-chain.",
chainId = $groupManager.chainId.get(),
chainId = $groupManager.chainId,
contractAddress = conf.rlnRelayEthContractAddress,
membershipIndex = groupManager.membershipIndex.get()
info "Your user message limit is", userMessageLimit = conf.rlnRelayUserMessageLimit

# 6. write to keystore
let keystoreCred = KeystoreMembership(
membershipContract: MembershipContract(
chainId: $groupManager.chainId.get(), address: conf.rlnRelayEthContractAddress
chainId: $groupManager.chainId, address: conf.rlnRelayEthContractAddress
),
treeIndex: groupManager.membershipIndex.get(),
identityCredential: credential,
Expand Down
6 changes: 6 additions & 0 deletions waku/factory/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ type WakuNodeConf* = object
name: "rln-relay-eth-contract-address"
.}: string

rlnRelayChainId* {.
desc: "Chain ID of the provided contract (optional, will fetch from RPC provider if not used)",
defaultValue: 0,
name: "rln-relay-chain-id"
.}: uint

rlnRelayCredPassword* {.
desc: "Password for encrypting RLN credentials",
defaultValue: "",
Expand Down
2 changes: 2 additions & 0 deletions waku/factory/networks_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ClusterConf* = object
clusterId*: uint16
rlnRelay*: bool
rlnRelayEthContractAddress*: string
rlnRelayChainId*: uint
rlnRelayDynamic*: bool
rlnRelayBandwidthThreshold*: int
rlnEpochSizeSec*: uint64
Expand Down Expand Up @@ -37,6 +38,7 @@ proc TheWakuNetworkConf*(T: type ClusterConf): ClusterConf =
rlnRelay: true,
rlnRelayEthContractAddress: "0x4976Df0f61135EF3E5720D92eadE2e5F47A68Ef9",
rlnRelayDynamic: true,
rlnRelayChainId: 2442, # https://chainlist.org/chain/2442
rlnRelayBandwidthThreshold: 0,
rlnEpochSizeSec: 600,
rlnRelayUserMessageLimit: 20,
Expand Down
1 change: 1 addition & 0 deletions waku/factory/node_factory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ proc setupProtocols(
rlnRelayDynamic: conf.rlnRelayDynamic,
rlnRelayCredIndex: conf.rlnRelayCredIndex,
rlnRelayEthContractAddress: conf.rlnRelayEthContractAddress,
rlnRelayChainId: conf.rlnRelayChainId,
rlnRelayEthClientAddress: string(conf.rlnRelayethClientAddress),
rlnRelayCredPath: conf.rlnRelayCredPath,
rlnRelayCredPassword: conf.rlnRelayCredPassword,
Expand Down
1 change: 1 addition & 0 deletions waku/factory/waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ proc init*(T: type Waku, conf: WakuNodeConf): Result[Waku, string] =
confCopy.clusterId = twnClusterConf.clusterId
confCopy.rlnRelay = twnClusterConf.rlnRelay
confCopy.rlnRelayEthContractAddress = twnClusterConf.rlnRelayEthContractAddress
confCopy.rlnRelayChainId = twnClusterConf.rlnRelayChainId
confCopy.rlnRelayDynamic = twnClusterConf.rlnRelayDynamic
confCopy.rlnRelayBandwidthThreshold = twnClusterConf.rlnRelayBandwidthThreshold
confCopy.discv5Discovery = twnClusterConf.discv5Discovery
Expand Down
23 changes: 15 additions & 8 deletions waku/waku_rln_relay/group_manager/on_chain/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type
wakuRlnContract*: Option[WakuRlnContractWithSender]
latestProcessedBlock*: BlockNumber
registrationTxHash*: Option[TxHash]
chainId*: Option[Quantity]
chainId*: uint
keystorePath*: Option[string]
keystorePassword*: Option[string]
registrationHandler*: Option[RegistrationHandler]
Expand Down Expand Up @@ -106,7 +106,7 @@ proc setMetadata*(
let metadataSetRes = g.rlnInstance.setMetadata(
RlnMetadata(
lastProcessedBlock: normalizedBlock,
chainId: uint64(g.chainId.get()),
chainId: g.chainId,
contractAddress: g.ethContractAddress,
validRoots: g.validRoots.toSeq(),
)
Expand Down Expand Up @@ -540,11 +540,18 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.}
g.retryWrapper(ethRpc, "Failed to connect to the Ethereum client"):
await newWeb3(g.ethClientUrl)

var fetchedChainId: uint
g.retryWrapper(fetchedChainId, "Failed to get the chain id"):
uint(await ethRpc.provider.eth_chainId())

# Set the chain id
var chainId: Quantity
g.retryWrapper(chainId, "Failed to get the chain id"):
await ethRpc.provider.eth_chainId()
g.chainId = some(chainId)
if g.chainId == 0:
warn "Chain ID not set in config, using RPC Provider's Chain ID", providerChainId = fetchedChainId

if g.chainId != 0 and g.chainId != fetchedChainId:
return err("The RPC Provided a Chain ID which is different than the provided Chain ID: provided = " & $g.chainId & ", actual = " & $fetchedChainId)

g.chainId = fetchedChainId

if g.ethPrivateKey.isSome():
let pk = g.ethPrivateKey.get()
Expand All @@ -567,7 +574,7 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.}

var keystoreQuery = KeystoreMembership(
membershipContract:
MembershipContract(chainId: $g.chainId.get(), address: g.ethContractAddress)
MembershipContract(chainId: $g.chainId, address: g.ethContractAddress)
)
if g.membershipIndex.isSome():
keystoreQuery.treeIndex = MembershipIndex(g.membershipIndex.get())
Expand Down Expand Up @@ -599,7 +606,7 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.}
warn "could not initialize with persisted rln metadata"
elif metadataGetOptRes.get().isSome():
let metadata = metadataGetOptRes.get().get()
if metadata.chainId != uint64(g.chainId.get()):
if metadata.chainId != uint(g.chainId):
return err("persisted data: chain id mismatch")

if metadata.contractAddress != g.ethContractAddress.toLower():
Expand Down
2 changes: 2 additions & 0 deletions waku/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type WakuRlnConfig* = object
rlnRelayCredIndex*: Option[uint]
rlnRelayEthContractAddress*: string
rlnRelayEthClientAddress*: string
rlnRelayChainId*: uint
rlnRelayCredPath*: string
rlnRelayCredPassword*: string
rlnRelayTreePath*: string
Expand Down Expand Up @@ -429,6 +430,7 @@ proc mount(
groupManager = OnchainGroupManager(
ethClientUrl: string(conf.rlnRelayethClientAddress),
ethContractAddress: $conf.rlnRelayEthContractAddress,
chainId: conf.rlnRelayChainId,
rlnInstance: rlnInstance,
registrationHandler: registrationHandler,
keystorePath: rlnRelayCredPath,
Expand Down

0 comments on commit 05356ab

Please sign in to comment.