fix(rln-relay): RangeDefect test (#1318)

* fix(rln-relay): RangeDefect test

* fix(rln-relay): any overflow

* fix(rln-relay): dont use int64 when its absolute value is being used anyway

* chore(rln): rename proc to be more meaningful about operation

* fix(rln): remove unused imports
This commit is contained in:
Aaryamann Challani 2022-11-01 05:55:39 +05:30 committed by GitHub
parent bd516788cb
commit 24d288ccb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 10 deletions

View File

@ -820,7 +820,7 @@ suite "Waku rln relay":
debug "encoded and decode time", epoch = epoch, epochBytes = epochBytes,
decodedEpoch = decodedEpoch
test "Epoch comparison":
test "Epoch comparison, epoch1 > epoch2":
# check edge cases
let
time1 = uint64.high
@ -828,8 +828,8 @@ suite "Waku rln relay":
epoch1 = time1.toEpoch()
epoch2 = time2.toEpoch()
check:
diff(epoch1, epoch2) == int64(1)
diff(epoch2, epoch1) == int64(-1)
absDiff(epoch1, epoch2) == uint64(1)
absDiff(epoch2, epoch1) == uint64(1)
test "updateLog and hasDuplicate tests":
let

View File

@ -458,4 +458,4 @@ const EpochUnitSeconds* = float64(10) # the rln-relay epoch length in seconds
const MaxClockGapSeconds* = 20.0 # the maximum clock difference between peers in seconds
# maximum allowed gap between the epochs of messages' RateLimitProofs
const MaxEpochGap* = int64(MaxClockGapSeconds/EpochUnitSeconds)
const MaxEpochGap* = uint64(MaxClockGapSeconds/EpochUnitSeconds)

View File

@ -1,7 +1,7 @@
{.push raises: [Defect].}
import
std/[sequtils, tables, times, streams, os, deques],
std/[sequtils, tables, times, os, deques],
chronicles, options, chronos, stint,
confutils,
web3, json,
@ -785,15 +785,21 @@ proc getCurrentEpoch*(): Epoch =
## gets the current rln Epoch time
return calcEpoch(epochTime())
proc diff*(e1, e2: Epoch): int64 =
## returns the difference between the two rln `Epoch`s `e1` and `e2`
proc absDiff*(e1, e2: Epoch): uint64 =
## returns the absolute difference between the two rln `Epoch`s `e1` and `e2`
## i.e., e1 - e2
# convert epochs to their corresponding unsigned numerical values
let
epoch1 = fromEpoch(e1)
epoch2 = fromEpoch(e2)
return int64(epoch1) - int64(epoch2)
# Manually perform an `abs` calculation
if epoch1 > epoch2:
return epoch1 - epoch2
else:
return epoch2 - epoch1
proc validateMessage*(rlnPeer: WakuRLNRelay, msg: WakuMessage,
timeOption: Option[float64] = none(float64)): MessageValidationResult =
@ -820,12 +826,12 @@ proc validateMessage*(rlnPeer: WakuRLNRelay, msg: WakuMessage,
let
msgEpoch = msg.proof.epoch
# calculate the gaps
gap = diff(epoch, msgEpoch)
gap = absDiff(epoch, msgEpoch)
debug "message epoch", msgEpoch = fromEpoch(msgEpoch)
# validate the epoch
if abs(gap) > MaxEpochGap:
if gap > MaxEpochGap:
# message's epoch is too old or too ahead
# accept messages whose epoch is within +-MaxEpochGap from the current epoch
debug "invalid message: epoch gap exceeds a threshold", gap = gap,