-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rln-relay-v2): nonce/messageId manager #2413
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the if/else you set the value of
retNonce
in both cases. So this assigment will be overridden no matter what? Meaning its not needed?or well, you can avoid the else, something like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 699a5bc