From 045f07c616886273c4acce5cc0fa4797269a54aa Mon Sep 17 00:00:00 2001 From: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:33:41 +0530 Subject: [PATCH] feat(rln-relay): metadata ffi api (#1803) * feat(rln-relay): metadata ffi api * chore(rln-relay): bump to latest master after merge * fix(rln-relay): naming, visibility, tests --- .../v2/waku_rln_relay/test_waku_rln_relay.nim | 30 +++++++++++++ vendor/zerokit | 2 +- waku/v2/waku_rln_relay/rln/rln_interface.nim | 13 ++++++ waku/v2/waku_rln_relay/rln/wrappers.nim | 43 +++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/tests/v2/waku_rln_relay/test_waku_rln_relay.nim b/tests/v2/waku_rln_relay/test_waku_rln_relay.nim index 12444bfa8..60c4496f7 100644 --- a/tests/v2/waku_rln_relay/test_waku_rln_relay.nim +++ b/tests/v2/waku_rln_relay/test_waku_rln_relay.nim @@ -238,6 +238,36 @@ suite "Waku rln relay": check: rln.removeMember(MembershipIndex(0)) + test "setMetadata rln utils": + # create an RLN instance which also includes an empty Merkle tree + let rlnInstance = createRLNInstance() + require: + rlnInstance.isOk() + let rln = rlnInstance.get() + check: + rln.setMetadata(RlnMetadata(lastProcessedBlock: 128)).isOk() + + test "getMetadata rln utils": + # create an RLN instance which also includes an empty Merkle tree + let rlnInstance = createRLNInstance() + require: + rlnInstance.isOk() + let rln = rlnInstance.get() + + require: + rln.setMetadata(RlnMetadata(lastProcessedBlock: 128)).isOk() + + let metadataRes = rln.getMetadata() + + require: + metadataRes.isOk() + + let metadata = metadataRes.get() + + check: + metadata.lastProcessedBlock == 128 + + test "Merkle tree consistency check between deletion and insertion": # create an RLN instance let rlnInstance = createRLNInstance() diff --git a/vendor/zerokit b/vendor/zerokit index 9cc86e526..2793fe0e2 160000 --- a/vendor/zerokit +++ b/vendor/zerokit @@ -1 +1 @@ -Subproject commit 9cc86e526ee0b34b20e4110dc3e4fded03a046d2 +Subproject commit 2793fe0e24f7b48813fe3b6e9a0e6e5ee4a4c8ce diff --git a/waku/v2/waku_rln_relay/rln/rln_interface.nim b/waku/v2/waku_rln_relay/rln/rln_interface.nim index 728927036..34def0f28 100644 --- a/waku/v2/waku_rln_relay/rln/rln_interface.nim +++ b/waku/v2/waku_rln_relay/rln/rln_interface.nim @@ -175,3 +175,16 @@ proc poseidon*(input_buffer: ptr Buffer, ## inputs_buffer holds the hash input as a byte seq ## the hash output is generated and populated inside output_buffer ## the output_buffer contains 32 bytes hash output + +#-------------------------------- Persistent Metadata utils ------------------------------------------- + +proc set_metadata*(ctx: ptr RLN, input_buffer: ptr Buffer): bool {.importc: "set_metadata".} +## sets the metadata stored by ctx to the value passed by input_buffer +## the input_buffer holds a serialized representation of the metadata (format to be defined) +## input_buffer holds the metadata as a byte seq +## the return bool value indicates the success or failure of the operation + +proc get_metadata*(ctx: ptr RLN, output_buffer: ptr Buffer): bool {.importc: "get_metadata".} +## gets the metadata stored by ctx and populates the passed pointer output_buffer with it +## the output_buffer holds the metadata as a byte seq +## the return bool value indicates the success or failure of the operation diff --git a/waku/v2/waku_rln_relay/rln/wrappers.nim b/waku/v2/waku_rln_relay/rln/wrappers.nim index b23e095a7..9fbd24ba4 100644 --- a/waku/v2/waku_rln_relay/rln/wrappers.nim +++ b/waku/v2/waku_rln_relay/rln/wrappers.nim @@ -346,3 +346,46 @@ proc getMerkleRoot*(rlnInstance: ptr RLN): MerkleNodeResult = var rootValue = cast[ptr MerkleNode] (root.`ptr`)[] return ok(rootValue) + +type + RlnMetadata* = object + lastProcessedBlock*: uint64 + +proc serialize(metadata: RlnMetadata): seq[byte] = + ## serializes the metadata + ## returns the serialized metadata + return @(metadata.lastProcessedBlock.toBytes()) + +proc setMetadata*(rlnInstance: ptr RLN, metadata: RlnMetadata): RlnRelayResult[void] = + ## sets the metadata of the RLN instance + ## returns an error if the metadata could not be set + ## returns void if the metadata is set successfully + + # serialize the metadata + let metadataBytes = serialize(metadata) + var metadataBuffer = metadataBytes.toBuffer() + let metadataBufferPtr = addr metadataBuffer + + # set the metadata + let metadataSet = set_metadata(rlnInstance, metadataBufferPtr) + if not metadataSet: + return err("could not set the metadata") + return ok() + +proc getMetadata*(rlnInstance: ptr RLN): RlnRelayResult[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 + + # read the metadata + var + metadata {.noinit.}: Buffer = Buffer() + metadataPtr = addr(metadata) + getMetadataSuccessful = get_metadata(rlnInstance, metadataPtr) + if not getMetadataSuccessful: + return err("could not get the metadata") + if not metadata.len == 8: + return err("wrong output size") + + var metadataValue = cast[ptr uint64] (metadata.`ptr`)[] + return ok(RlnMetadata(lastProcessedBlock: metadataValue))