-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rln-relay-v2): nonce/messageId manager
- Loading branch information
Showing
9 changed files
with
148 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
options, | ||
chronos, | ||
stew/results, | ||
times | ||
import | ||
./constants | ||
|
||
# This module contains the NonceManager interface | ||
# The NonceManager is responsible for managing the messageId used to generate RLN proofs | ||
# It should be used to fetch a new messageId every time a proof is generated | ||
# It refreshes the messageId every `epoch` seconds | ||
|
||
type | ||
Nonce* = uint64 | ||
NonceManager* = ref object of RootObj | ||
epoch: float64 | ||
lastNonce: Nonce | ||
lastNonceTime: float64 | ||
nonceLimit: Nonce | ||
|
||
NonceManagerError* = enum | ||
NonceLimitReached | ||
|
||
NonceManagerResult[T] = Result[T, NonceManagerError] | ||
|
||
proc `$`*(ne: NonceManagerError): string = | ||
case ne | ||
of NonceManagerError.NonceLimitReached: return "Nonce limit reached" | ||
|
||
proc init*(T: type NonceManager, nonceLimit: Nonce, epoch = EpochUnitSeconds): T = | ||
return NonceManager( | ||
epoch: epoch, | ||
lastNonce: 0, | ||
lastNonceTime: 0, | ||
nonceLimit: nonceLimit | ||
) | ||
|
||
|
||
proc get*(n: NonceManager): NonceManagerResult[Nonce] = | ||
let now = getTime().toUnixFloat() | ||
|
||
if now - n.lastNonceTime > n.epoch: | ||
n.lastNonce = n.lastNonce + 1 | ||
n.lastNonceTime = now | ||
else: | ||
# we are in a new epoch | ||
n.lastNonce = 0 | ||
n.lastNonceTime = now | ||
|
||
if n.lastNonce > n.nonceLimit: | ||
return err(NonceManagerError.NonceLimitReached) | ||
|
||
return ok(n.lastNonce) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters