Skip to content

Commit

Permalink
feat(rln-relay): close db connection appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jul 25, 2023
1 parent 08f3bba commit ee3a505
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vendor/zerokit
3 changes: 3 additions & 0 deletions waku/v2/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -828,4 +828,7 @@ proc stop*(node: WakuNode) {.async.} =
await node.switch.stop()
node.peerManager.stop()

if not node.wakuRlnRelay.isNil():
await node.wakuRlnRelay.stop()

node.started = false
3 changes: 3 additions & 0 deletions waku/v2/waku_rln_relay/group_manager/group_manager_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ method withdrawBatch*(g: GroupManager, identitySecretHashes: seq[IdentitySecretH
method atomicBatch*(g: GroupManager, idCommitments: seq[IDCommitment], toRemoveIndices: seq[MembershipIndex]): Future[void] {.base,gcsafe.} =
raise newException(CatchableError, "atomicBatch proc for " & $g.type & " is not implemented yet")

method close*(g: GroupManager): Future[void] {.base,gcsafe.} =
raise newException(CatchableError, "close proc for " & $g.type & " is not implemented yet")

# This proc is used to set a callback that will be called when an identity commitment is withdrawn
# The callback may be called multiple times, and should be used to for any post processing
method onWithdraw*(g: GroupManager, cb: OnWithdrawCallback) {.base,gcsafe.} =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,10 @@ method init*(g: OnchainGroupManager): Future[void] {.async.} =
error "failed to restart group sync", error = getCurrentExceptionMsg()

g.initialized = true

method close*(g: OnchainGroupManager): Future[void] {.async.} =
if g.ethRpc.isSome():
await g.ethRpc.get().close()
let connClosed = g.rlnInstance.closeDbConnection()
if not connClosed:
error "failed to close the tree db connection"
7 changes: 7 additions & 0 deletions waku/v2/waku_rln_relay/group_manager/static/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ method onRegister*(g: StaticGroupManager, cb: OnRegisterCallback) {.gcsafe.} =

method onWithdraw*(g: StaticGroupManager, cb: OnWithdrawCallback) {.gcsafe.} =
g.withdrawCb = some(cb)

method close*(g: StaticGroupManager): Future[void] =
initializedGuard(g)
# No-op
var retFut = newFuture[void]("StaticGroupManager.close")
retFut.complete()
return retFut
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 @@ -188,3 +188,8 @@ proc get_metadata*(ctx: ptr RLN, output_buffer: ptr Buffer): bool {.importc: "ge
## 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

proc close_db_connection*(ctx: ptr RLN): bool {.importc: "close_db_connection".}
## closes the connection to the database
## the return bool value indicates the success or failure of the operation
## This allows more robust and graceful handling of the database connection
7 changes: 7 additions & 0 deletions waku/v2/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ type WakuRLNRelay* = ref object of RootObj
groupManager*: GroupManager
messageBucket*: Option[TokenBucket]

method stop*(rlnPeer: WakuRLNRelay) {.async.} =
## stops the rln-relay protocol
## Throws an error if it cannot stop the rln-relay protocol

# stop the group sync, and flush data to tree db
await rlnPeer.groupManager.close()

proc hasDuplicate*(rlnPeer: WakuRLNRelay,
proofMetadata: ProofMetadata): RlnRelayResult[bool] =
## returns true if there is another message in the `nullifierLog` of the `rlnPeer` with the same
Expand Down

0 comments on commit ee3a505

Please sign in to comment.