Skip to content

Commit

Permalink
chore(rln-relay): pass in the path to the tree db (#1782)
Browse files Browse the repository at this point in the history
* chore(rln-relay): pass in the path to the tree db

* fix(rln-relay): address visibility

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>

* fix(rln-relay): make db used more explicit

* fix(rln-relay): reduce visibility

---------

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>
  • Loading branch information
rymnc and Ivansete-status authored Jun 8, 2023
1 parent 93e09b8 commit dba8424
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
5 changes: 5 additions & 0 deletions apps/wakunode2/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ type
defaultValue: ""
name: "rln-relay-cred-password" }: string

rlnRelayTreePath* {.
desc: "Path to the RLN merkle tree sled db (https://github.com/spacejam/sled)",
defaultValue: ""
name: "rln-relay-tree-path" }: string

staticnodes* {.
desc: "Peer multiaddr to directly connect with. Argument may be repeated."
name: "staticnode" }: seq[string]
Expand Down
12 changes: 1 addition & 11 deletions waku/v2/waku_rln_relay/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ const
HashHexSize* = int(HashBitSize/4)

const
# The relative folder where the circuit, proving and verification key for RLN can be found
# Note that resources has to be compiled with respect to the above MerkleTreeDepth
RlnConfig* = $(%* {
"resources_folder": "tree_height_" & $MerkleTreeDepth & "/",
"tree_config": {
"cache_capacity": 15_000,
"mode": "high_throughput",
"compression": false,
"flush_interval": 12_000
}
})
DefaultRlnTreePath* = "rln_tree.db"

# temporary variables to test waku-rln-relay performance in the static group mode
const
Expand Down
51 changes: 46 additions & 5 deletions waku/v2/waku_rln_relay/rln/wrappers.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import
std/json
import
chronicles,
options,
Expand Down Expand Up @@ -51,30 +53,69 @@ proc membershipKeyGen*(ctxPtr: ptr RLN): RlnRelayResult[IdentityCredential] =

return ok(identityCredential)

proc createRLNInstanceLocal*(d: int = MerkleTreeDepth): RLNResult =
type RlnTreeConfig = ref object of RootObj
cache_capacity: int
mode: string
compression: bool
flush_interval: int
path: string

type RlnConfig = ref object of RootObj
resources_folder: string
tree_config: RlnTreeConfig

proc `%`(c: RlnConfig): JsonNode =
## wrapper around the generic JObject constructor.
## We don't need to have a separate proc for the tree_config field
let tree_config = %{ "cache_capacity": %c.tree_config.cache_capacity,
"mode": %c.tree_config.mode,
"compression": %c.tree_config.compression,
"flush_interval": %c.tree_config.flush_interval,
"path": %c.tree_config.path }
return %[("resources_folder", %c.resources_folder),
("tree_config", %tree_config)]

proc createRLNInstanceLocal(d = MerkleTreeDepth,
tree_path = DefaultRlnTreePath): RLNResult =
## generates an instance of RLN
## An RLN instance supports both zkSNARKs logics and Merkle tree data structure and operations
## d indicates the depth of Merkle tree
## tree_path indicates the path of the Merkle tree
## Returns an error if the instance creation fails

let rln_config = RlnConfig(
resources_folder: "tree_height_" & $d & "/",
tree_config: RlnTreeConfig(
cache_capacity: 15_000,
mode: "high_throughput",
compression: false,
flush_interval: 12_000,
path: if tree_path != "": tree_path else: DefaultRlnTreePath
)
)

var serialized_rln_config = $(%rln_config)

var
rlnInstance: ptr RLN
merkleDepth: csize_t = uint(d)
resourcesPathBuffer = RlnConfig.toOpenArrayByte(0, RlnConfig.high).toBuffer()
configBuffer = serialized_rln_config.toOpenArrayByte(0, serialized_rln_config.high).toBuffer()

# create an instance of RLN
let res = new_circuit(merkleDepth, addr resourcesPathBuffer, addr rlnInstance)
let res = new_circuit(merkleDepth, addr configBuffer, addr rlnInstance)
# check whether the circuit parameters are generated successfully
if (res == false):
debug "error in parameters generation"
return err("error in parameters generation")
return ok(rlnInstance)

proc createRLNInstance*(d: int = MerkleTreeDepth): RLNResult =
proc createRLNInstance*(d = MerkleTreeDepth,
tree_path = DefaultRlnTreePath): RLNResult =
## Wraps the rln instance creation for metrics
## Returns an error if the instance creation fails
var res: RLNResult
waku_rln_instance_creation_duration_seconds.nanosecondTime:
res = createRLNInstanceLocal(d)
res = createRLNInstanceLocal(d, tree_path)
return res

proc sha256*(data: openArray[byte]): RlnRelayResult[MerkleNode] =
Expand Down
3 changes: 2 additions & 1 deletion waku/v2/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type WakuRlnConfig* = object
rlnRelayEthAccountAddress*: string
rlnRelayCredPath*: string
rlnRelayCredentialsPassword*: string
rlnRelayTreePath*: string

proc createMembershipList*(rln: ptr RLN, n: int): RlnRelayResult[(
seq[RawMembershipCredentials], string
Expand Down Expand Up @@ -338,7 +339,7 @@ proc mount(conf: WakuRlnConfig,
credentials: MembershipCredentials
persistCredentials = false
# create an RLN instance
let rlnInstanceRes = createRLNInstance()
let rlnInstanceRes = createRLNInstance(tree_path = conf.rlnRelayTreePath)
if rlnInstanceRes.isErr():
raise newException(CatchableError, "RLN instance creation failed")
let rlnInstance = rlnInstanceRes.get()
Expand Down

0 comments on commit dba8424

Please sign in to comment.