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
This commit is contained in:
Aaryamann Challani 2023-06-16 11:33:41 +05:30 committed by GitHub
parent fce845bb04
commit 045f07c616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 1 deletions

View File

@ -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()

2
vendor/zerokit vendored

@ -1 +1 @@
Subproject commit 9cc86e526ee0b34b20e4110dc3e4fded03a046d2
Subproject commit 2793fe0e24f7b48813fe3b6e9a0e6e5ee4a4c8ce

View File

@ -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

View File

@ -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))