mirror of https://github.com/waku-org/nwaku.git
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:
parent
8341864d30
commit
1274b15d75
2
Makefile
2
Makefile
|
@ -137,7 +137,7 @@ LIBRLN_BUILDDIR := $(CURDIR)/vendor/zerokit
|
|||
ifeq ($(RLN_V2),true)
|
||||
LIBRLN_VERSION := v0.4.1
|
||||
else
|
||||
LIBRLN_VERSION := v0.3.4
|
||||
LIBRLN_VERSION := v0.3.6
|
||||
endif
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
|
|
|
@ -226,12 +226,13 @@ suite "Onchain group manager":
|
|||
|
||||
let metadataSetRes = manager.setMetadata()
|
||||
assert metadataSetRes.isOk(), metadataSetRes.error
|
||||
let metadataRes = manager.rlnInstance.getMetadata()
|
||||
assert metadataRes.isOk(), metadataRes.error
|
||||
let metadata = metadataRes.get()
|
||||
require:
|
||||
metadata.chainId == 1337
|
||||
metadata.contractAddress == manager.ethContractAddress
|
||||
let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
|
||||
raiseAssert $error
|
||||
assert metadataOpt.isSome(), "metadata is not set"
|
||||
let metadata = metadataOpt.get()
|
||||
|
||||
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()
|
||||
|
||||
|
@ -458,8 +459,11 @@ suite "Onchain group manager":
|
|||
|
||||
await fut
|
||||
|
||||
let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
|
||||
raiseAssert $error
|
||||
assert metadataOpt.isSome(), "metadata is not set"
|
||||
check:
|
||||
manager.rlnInstance.getMetadata().get().validRoots == manager.validRoots.toSeq()
|
||||
metadataOpt.get().validRoots == manager.validRoots.toSeq()
|
||||
await manager.stop()
|
||||
|
||||
asyncTest "withdraw: should guard against uninitialized state":
|
||||
|
|
|
@ -260,18 +260,26 @@ suite "Waku rln relay":
|
|||
chainId: 1155511,
|
||||
contractAddress: "0x9c09146844c1326c2dbc41c451766c7138f88155")).isOk()
|
||||
|
||||
let metadataRes = rln.getMetadata()
|
||||
|
||||
require:
|
||||
metadataRes.isOk()
|
||||
|
||||
let metadata = metadataRes.get()
|
||||
let metadataOpt = rln.getMetadata().valueOr:
|
||||
raiseAssert $error
|
||||
|
||||
assert metadataOpt.isSome(), "metadata is not set"
|
||||
let metadata = metadataOpt.get()
|
||||
check:
|
||||
metadata.lastProcessedBlock == 128
|
||||
metadata.chainId == 1155511
|
||||
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":
|
||||
# create an RLN instance
|
||||
let rlnInstance = createRLNInstanceWrapper()
|
||||
|
|
|
@ -27,9 +27,14 @@ proc doInspectRlnDb*(conf: WakuNodeConf) =
|
|||
quit(1)
|
||||
|
||||
# 3. get metadata
|
||||
let metadata = rlnInstance.getMetadata().valueOr:
|
||||
let metadataOpt = rlnInstance.getMetadata().valueOr:
|
||||
error "failure while getting RLN metadata", error
|
||||
quit(1)
|
||||
|
||||
if metadataOpt.isNone():
|
||||
error "RLN metadata does not exist"
|
||||
quit(1)
|
||||
let metadata = metadataOpt.get()
|
||||
|
||||
info "RLN metadata", lastProcessedBlock = metadata.lastProcessedBlock,
|
||||
chainId = metadata.chainId,
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit b903d8d740e0b8b82057bcc5377ddce05ae5676b
|
||||
Subproject commit 7dd019b0a5716e52169f396cecce961021fcac51
|
|
@ -666,11 +666,11 @@ method init*(g: OnchainGroupManager): Future[void] {.async.} =
|
|||
|
||||
g.idCredentials = some(keystoreCred.identityCredential)
|
||||
|
||||
let metadataGetRes = g.rlnInstance.getMetadata()
|
||||
if metadataGetRes.isErr():
|
||||
let metadataGetOptRes = g.rlnInstance.getMetadata()
|
||||
if metadataGetOptRes.isErr():
|
||||
warn "could not initialize with persisted rln metadata"
|
||||
else:
|
||||
let metadata = metadataGetRes.get()
|
||||
elif metadataGetOptRes.get().isSome():
|
||||
let metadata = metadataGetOptRes.get().get()
|
||||
if metadata.chainId != uint64(g.chainId.get()):
|
||||
raise newException(ValueError, "persisted data: chain id mismatch")
|
||||
|
||||
|
|
|
@ -500,7 +500,7 @@ proc setMetadata*(rlnInstance: ptr RLN, metadata: RlnMetadata): RlnRelayResult[v
|
|||
return err("could not set the metadata")
|
||||
return ok()
|
||||
|
||||
proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[RlnMetadata] =
|
||||
proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[Option[RlnMetadata]] =
|
||||
## gets the metadata of the RLN instance
|
||||
## returns an error if the metadata could not be retrieved
|
||||
## 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")
|
||||
trace "metadata length", metadataLen = metadata.len
|
||||
|
||||
if metadata.len == 0:
|
||||
return ok(none(RlnMetadata))
|
||||
|
||||
let
|
||||
lastProcessedBlockOffset = 0
|
||||
chainIdOffset = lastProcessedBlockOffset + 8
|
||||
|
@ -536,7 +539,7 @@ proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[RlnMetadata] =
|
|||
let validRootsBytes = metadataBytes[validRootsOffset..metadataBytes.high]
|
||||
validRoots = MerkleNodeSeq.deserialize(validRootsBytes)
|
||||
|
||||
return ok(RlnMetadata(lastProcessedBlock: lastProcessedBlock,
|
||||
chainId: chainId,
|
||||
contractAddress: "0x" & contractAddress,
|
||||
validRoots: validRoots))
|
||||
return ok(some(RlnMetadata(lastProcessedBlock: lastProcessedBlock,
|
||||
chainId: chainId,
|
||||
contractAddress: "0x" & contractAddress,
|
||||
validRoots: validRoots)))
|
||||
|
|
Loading…
Reference in New Issue