Skip to content
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

chore: add timestamp and ephemeral for opt-in dos validator #1713

Merged
merged 4 commits into from
May 5, 2023

Conversation

alrevuelta
Copy link
Contributor

@alrevuelta alrevuelta commented May 4, 2023

let now = getNanosecondTime(getTime().toUnixFloat())
let window = getNanosecondTime(MessageWindowInSec)

if now > ts:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open for other ways.

cant do abs(now-ts) since im working with uint64 and if I cast to signed, I will lose precision. So its the way I found.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, mabe it is a completely stupid question, but would losing precision matter? Is the concern that someone could make the difference between TS and now overflow and thus the result of abs() be < window?

Can we check for overflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ou between is indeed what I needed, but since in nwaku we generally use our own Timestamp (and not te Duration) I think this may be simpler. Less castings, etc.

Sorry, mabe it is a completely stupid question, but would losing precision matter? Is the concern that someone could make the difference between TS and now overflow and thus the result of abs() be < window?

mm havent done the maths if matters or not. Difference between ts and now will never overflow, since it will be always a smaller value. But since both values are uint64 I need to do ts-now or now-ts to ensure the result is not negative. Hence why i compare.

Mathematically is just abs(now-ts) but in practice thats not possible afaik without converting to int64 with a possible lose in precission.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, but Timestamp is an alias for int64 not uint64, unless I'm missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch indeed. Totally assumed it was uint64, since otherwise it would be a waste of bytes (assuming negative timestamps doesnt make sense). my bad.

fixed, way easier da7814a

some quick napkin math @vpavlin int64 overflow with 9223372036854775807, since its nano seconds thats 9223372036 . 854775807 and 9223372036 in unix timestamp its Fri Apr 11 2262 23:47:16 GMT+0000 so we are covered.

thanks all!

@alrevuelta alrevuelta marked this pull request as ready for review May 4, 2023 15:14
Copy link
Member

@vpavlin vpavlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@jm-clius jm-clius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We need overflow-proof arithmetic for waku Timestamps. In general, though, we want to stop people from directly having to choose between std/times and chronos/timer and just use waku_core/time which should eventually wrap what is needed from both the above. This is beyond the scope of this PR, of course.

# Returns true if the message timestamp is:
# abs(now - msg.timestamp) < MessageWindowInSec
let ts = msg.timestamp
let now = getNanosecondTime(getTime().toUnixFloat())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waku_core (exporting time) already provides getNowInNanosecondTime() so you don't have to import std/times

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes right, i keep forgeting.

fixed 3092a51

@@ -4,16 +4,20 @@ else:
{.push raises: [].}

import
std/[math,times],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to avoid importing std/times (especially when we use chronos.timer constructs in the same module). In this case we need it for absolute system time, but waku_core/time provides the wrappers for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 3092a51

@alrevuelta
Copy link
Contributor Author

Merging, failing experimental test is a known issue.

@alrevuelta alrevuelta merged commit 3e0a693 into master May 5, 2023
@alrevuelta alrevuelta deleted the add-timestamp-dos branch May 5, 2023 08:12
@fryorcraken fryorcraken added the E:Opt-in message signing See https://github.com/waku-org/pm/issues/20 for details label Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E:Opt-in message signing See https://github.com/waku-org/pm/issues/20 for details
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants