Skip to content

Commit

Permalink
feat(rln-relay): integrate get_leaf ffi api (#1790)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc authored Jun 12, 2023
1 parent 5fc5770 commit 940206b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
33 changes: 33 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 @@ -147,6 +147,39 @@ suite "Waku rln relay":
check:
memberAdded

test "getMember Nim wrapper":
# create an RLN instance which also includes an empty Merkle tree
let rlnInstance = createRLNInstance()
require:
rlnInstance.isOk()
let rln = rlnInstance.get()
# generate an identity credential
let idCredentialRes = membershipKeyGen(rln)
require:
idCredentialRes.isOk()

let idCredential = idCredentialRes.get()
let pkBuffer = toBuffer(idCredential.idCommitment)
let pkBufferPtr = unsafeAddr(pkBuffer)

let
root1 {.noinit.}: Buffer = Buffer()
rootPtr1 = unsafeAddr(root1)
getRootSuccessful1 = getRoot(rlnInstance.get(), rootPtr1)

# add the member to the tree
let memberAdded = updateNextMember(rln, pkBufferPtr)
require:
memberAdded

let leafRes = getMember(rln, 0)
require:
leafRes.isOk()
let leaf = leafRes.get()
let leafHex = leaf.inHex()
check:
leafHex == idCredential.idCommitment.inHex()

test "delete_member Nim wrapper":
# create an RLN instance which also includes an empty Merkle tree
let rlnInstance = createRLNInstance()
Expand Down
1 change: 0 additions & 1 deletion waku/v2/waku_rln_relay/constants.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import
std/json,
stint

import
Expand Down
5 changes: 5 additions & 0 deletions waku/v2/waku_rln_relay/rln/rln_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ proc set_leaf*(ctx: ptr RLN, index: uint, input_buffer: ptr Buffer): bool {.impo
## the input_buffer holds a serialized leaf of 32 bytes
## the return bool value indicates the success or failure of the operation

proc get_leaf*(ctx: ptr RLN, index: uint, output_buffer: ptr Buffer): bool {.importc: "get_leaf".}
## gets the leaf at position index in the tree stored by ctx
## the output_buffer holds a serialized leaf of 32 bytes
## the return bool value indicates the success or failure of the operation

proc init_tree_with_leaves*(ctx: ptr RLN, input_buffer: ptr Buffer): bool {.importc: "init_tree_with_leaves".}
## sets multiple leaves in the tree stored by ctx to the value passed by input_buffer
## the input_buffer holds a serialized vector of leaves (32 bytes each)
Expand Down
19 changes: 19 additions & 0 deletions waku/v2/waku_rln_relay/rln/wrappers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ proc insertMember*(rlnInstance: ptr RLN, idComm: IDCommitment): bool =
let memberAdded = update_next_member(rlnInstance, pkBufferPtr)
return memberAdded

proc getMember*(rlnInstance: ptr RLN, index: MembershipIndex): RlnRelayResult[IDCommitment] =
## returns the member at the given index
## returns an error if the index is out of bounds
## returns the member if the index is valid
var
idCommitment {.noinit.}: Buffer = Buffer()
idCommitmentPtr = addr(idCommitment)
memberRetrieved = get_leaf(rlnInstance, index, idCommitmentPtr)

if not memberRetrieved:
return err("could not get the member")

if not idCommitment.len == 32:
return err("wrong output size")

let idCommitmentValue = (cast[ptr array[32, byte]](idCommitment.`ptr`))[]

return ok(@idCommitmentValue)

proc atomicWrite*(rlnInstance: ptr RLN,
index = none(MembershipIndex),
idComms = newSeq[IDCommitment](),
Expand Down

0 comments on commit 940206b

Please sign in to comment.