Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rln-relay): metadata ffi api #1803

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/v2/waku_rln_relay/test_waku_rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions waku/v2/waku_rln_relay/rln/rln_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions waku/v2/waku_rln_relay/rln/wrappers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))