fix(rln-relay): handle empty metadata returned by getMetadata proc (#2516)

* fix(rln-relay): silence error on startup when metadata is not found

* chore: fix fetching value from option

* fix: clarity of returned opt
This commit is contained in:
Aaryamann Challani 2024-03-08 19:36:22 +05:30 committed by GitHub
parent 8341864d30
commit 1274b15d75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 45 additions and 25 deletions

View File

@ -137,7 +137,7 @@ LIBRLN_BUILDDIR := $(CURDIR)/vendor/zerokit
ifeq ($(RLN_V2),true) ifeq ($(RLN_V2),true)
LIBRLN_VERSION := v0.4.1 LIBRLN_VERSION := v0.4.1
else else
LIBRLN_VERSION := v0.3.4 LIBRLN_VERSION := v0.3.6
endif endif
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)

View File

@ -226,12 +226,13 @@ suite "Onchain group manager":
let metadataSetRes = manager.setMetadata() let metadataSetRes = manager.setMetadata()
assert metadataSetRes.isOk(), metadataSetRes.error assert metadataSetRes.isOk(), metadataSetRes.error
let metadataRes = manager.rlnInstance.getMetadata() let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
assert metadataRes.isOk(), metadataRes.error raiseAssert $error
let metadata = metadataRes.get() assert metadataOpt.isSome(), "metadata is not set"
require: let metadata = metadataOpt.get()
metadata.chainId == 1337
metadata.contractAddress == manager.ethContractAddress assert metadata.chainId == 1337, "chainId is not equal to 1337"
assert metadata.contractAddress == manager.ethContractAddress, "contractAddress is not equal to " & manager.ethContractAddress
await manager.stop() await manager.stop()
@ -458,8 +459,11 @@ suite "Onchain group manager":
await fut await fut
let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
raiseAssert $error
assert metadataOpt.isSome(), "metadata is not set"
check: check:
manager.rlnInstance.getMetadata().get().validRoots == manager.validRoots.toSeq() metadataOpt.get().validRoots == manager.validRoots.toSeq()
await manager.stop() await manager.stop()
asyncTest "withdraw: should guard against uninitialized state": asyncTest "withdraw: should guard against uninitialized state":

View File

@ -260,18 +260,26 @@ suite "Waku rln relay":
chainId: 1155511, chainId: 1155511,
contractAddress: "0x9c09146844c1326c2dbc41c451766c7138f88155")).isOk() contractAddress: "0x9c09146844c1326c2dbc41c451766c7138f88155")).isOk()
let metadataRes = rln.getMetadata() let metadataOpt = rln.getMetadata().valueOr:
raiseAssert $error
require:
metadataRes.isOk()
let metadata = metadataRes.get()
assert metadataOpt.isSome(), "metadata is not set"
let metadata = metadataOpt.get()
check: check:
metadata.lastProcessedBlock == 128 metadata.lastProcessedBlock == 128
metadata.chainId == 1155511 metadata.chainId == 1155511
metadata.contractAddress == "0x9c09146844c1326c2dbc41c451766c7138f88155" metadata.contractAddress == "0x9c09146844c1326c2dbc41c451766c7138f88155"
test "getMetadata: empty rln metadata":
# create an RLN instance which also includes an empty Merkle tree
let rln = createRLNInstanceWrapper().valueOr:
raiseAssert $error
let metadata = rln.getMetadata().valueOr:
raiseAssert $error
check:
metadata.isNone()
test "Merkle tree consistency check between deletion and insertion": test "Merkle tree consistency check between deletion and insertion":
# create an RLN instance # create an RLN instance
let rlnInstance = createRLNInstanceWrapper() let rlnInstance = createRLNInstanceWrapper()

View File

@ -27,9 +27,14 @@ proc doInspectRlnDb*(conf: WakuNodeConf) =
quit(1) quit(1)
# 3. get metadata # 3. get metadata
let metadata = rlnInstance.getMetadata().valueOr: let metadataOpt = rlnInstance.getMetadata().valueOr:
error "failure while getting RLN metadata", error error "failure while getting RLN metadata", error
quit(1) quit(1)
if metadataOpt.isNone():
error "RLN metadata does not exist"
quit(1)
let metadata = metadataOpt.get()
info "RLN metadata", lastProcessedBlock = metadata.lastProcessedBlock, info "RLN metadata", lastProcessedBlock = metadata.lastProcessedBlock,
chainId = metadata.chainId, chainId = metadata.chainId,

2
vendor/zerokit vendored

@ -1 +1 @@
Subproject commit b903d8d740e0b8b82057bcc5377ddce05ae5676b Subproject commit 7dd019b0a5716e52169f396cecce961021fcac51

View File

@ -666,11 +666,11 @@ method init*(g: OnchainGroupManager): Future[void] {.async.} =
g.idCredentials = some(keystoreCred.identityCredential) g.idCredentials = some(keystoreCred.identityCredential)
let metadataGetRes = g.rlnInstance.getMetadata() let metadataGetOptRes = g.rlnInstance.getMetadata()
if metadataGetRes.isErr(): if metadataGetOptRes.isErr():
warn "could not initialize with persisted rln metadata" warn "could not initialize with persisted rln metadata"
else: elif metadataGetOptRes.get().isSome():
let metadata = metadataGetRes.get() let metadata = metadataGetOptRes.get().get()
if metadata.chainId != uint64(g.chainId.get()): if metadata.chainId != uint64(g.chainId.get()):
raise newException(ValueError, "persisted data: chain id mismatch") raise newException(ValueError, "persisted data: chain id mismatch")

View File

@ -500,7 +500,7 @@ proc setMetadata*(rlnInstance: ptr RLN, metadata: RlnMetadata): RlnRelayResult[v
return err("could not set the metadata") return err("could not set the metadata")
return ok() return ok()
proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[RlnMetadata] = proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[Option[RlnMetadata]] =
## gets the metadata of the RLN instance ## gets the metadata of the RLN instance
## returns an error if the metadata could not be retrieved ## returns an error if the metadata could not be retrieved
## returns the metadata if the metadata is retrieved successfully ## returns the metadata if the metadata is retrieved successfully
@ -514,6 +514,9 @@ proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[RlnMetadata] =
return err("could not get the metadata") return err("could not get the metadata")
trace "metadata length", metadataLen = metadata.len trace "metadata length", metadataLen = metadata.len
if metadata.len == 0:
return ok(none(RlnMetadata))
let let
lastProcessedBlockOffset = 0 lastProcessedBlockOffset = 0
chainIdOffset = lastProcessedBlockOffset + 8 chainIdOffset = lastProcessedBlockOffset + 8
@ -536,7 +539,7 @@ proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[RlnMetadata] =
let validRootsBytes = metadataBytes[validRootsOffset..metadataBytes.high] let validRootsBytes = metadataBytes[validRootsOffset..metadataBytes.high]
validRoots = MerkleNodeSeq.deserialize(validRootsBytes) validRoots = MerkleNodeSeq.deserialize(validRootsBytes)
return ok(RlnMetadata(lastProcessedBlock: lastProcessedBlock, return ok(some(RlnMetadata(lastProcessedBlock: lastProcessedBlock,
chainId: chainId, chainId: chainId,
contractAddress: "0x" & contractAddress, contractAddress: "0x" & contractAddress,
validRoots: validRoots)) validRoots: validRoots)))