-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
221 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{.used.} | ||
|
||
import | ||
testutils/unittests, | ||
chronos, | ||
os | ||
import | ||
../../../waku/waku_rln_relay/nonce_manager | ||
|
||
|
||
suite "Nonce manager": | ||
test "should initialize successfully": | ||
let nm = NonceManager.init(nonceLimit = 100.uint) | ||
|
||
check: | ||
nm.nonceLimit == 100.uint | ||
nm.nextNonce == 0.uint | ||
|
||
test "should generate a new nonce": | ||
let nm = NonceManager.init(nonceLimit = 100.uint) | ||
let nonceRes = nm.get() | ||
|
||
assert nonceRes.isOk(), $nonceRes.error | ||
|
||
check: | ||
nonceRes.get() == 0.uint | ||
nm.nextNonce == 1.uint | ||
|
||
test "should fail to generate a new nonce if limit is reached": | ||
let nm = NonceManager.init(nonceLimit = 1.uint) | ||
let nonceRes = nm.get() | ||
let nonceRes2 = nm.get() | ||
|
||
assert nonceRes.isOk(), $nonceRes.error | ||
assert nonceRes2.isErr(), "Expected error, got: " & $nonceRes2.value | ||
|
||
check: | ||
nonceRes2.error.kind == NonceManagerErrorKind.NonceLimitReached | ||
|
||
test "should generate a new nonce if epoch is crossed": | ||
let nm = NonceManager.init(nonceLimit = 1.uint, epoch = float(0.000001)) | ||
let nonceRes = nm.get() | ||
sleep(1) | ||
let nonceRes2 = nm.get() | ||
|
||
assert nonceRes.isOk(), $nonceRes.error | ||
assert nonceRes2.isOk(), $nonceRes2.error | ||
|
||
check: | ||
nonceRes.value == 0.uint | ||
nonceRes2.value == 0.uint |
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,67 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
chronos, | ||
stew/results, | ||
times | ||
import | ||
./constants | ||
|
||
export | ||
chronos, | ||
times, | ||
results, | ||
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 | ||
nextNonce*: Nonce | ||
lastNonceTime*: float64 | ||
nonceLimit*: Nonce | ||
|
||
NonceManagerErrorKind* = enum | ||
NonceLimitReached | ||
|
||
NonceManagerError* = object | ||
kind*: NonceManagerErrorKind | ||
error*: string | ||
|
||
NonceManagerResult*[T] = Result[T, NonceManagerError] | ||
|
||
proc `$`*(ne: NonceManagerError): string = | ||
case ne.kind | ||
of NonceLimitReached: | ||
return "NonceLimitReached: " & ne.error | ||
|
||
proc init*(T: type NonceManager, nonceLimit: Nonce, epoch = EpochUnitSeconds): T = | ||
return NonceManager( | ||
epoch: epoch, | ||
nextNonce: 0, | ||
lastNonceTime: 0, | ||
nonceLimit: nonceLimit | ||
) | ||
|
||
|
||
proc get*(n: NonceManager): NonceManagerResult[Nonce] = | ||
let now = getTime().toUnixFloat() | ||
var retNonce = n.nextNonce | ||
|
||
if now - n.lastNonceTime >= n.epoch: retNonce = 0 | ||
n.nextNonce = retNonce + 1 | ||
n.lastNonceTime = now | ||
|
||
if retNonce >= n.nonceLimit: | ||
return err(NonceManagerError(kind: NonceLimitReached, | ||
error: "Nonce limit reached. Please wait for the next epoch")) | ||
|
||
return ok(retNonce) |
Oops, something went wrong.