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

[Consensus] avoid subtracting from current Instant #18939

Merged
merged 2 commits into from
Aug 13, 2024
Merged

Conversation

mwtian
Copy link
Member

@mwtian mwtian commented Aug 9, 2024

Description

On Windows, Instant::now() can be close to 0 and we cannot subtract from that.

Test plan

CI. Verified on a Windows machine.


Release notes

Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required.

For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates.

  • Protocol:
  • Nodes (Validators and Full nodes):
  • Indexer:
  • JSON-RPC:
  • GraphQL:
  • CLI:
  • Rust SDK:
  • REST API:

Copy link

vercel bot commented Aug 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 11, 2024 10:00am
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
multisig-toolkit ⬜️ Ignored (Inspect) Visit Preview Aug 11, 2024 10:00am
sui-kiosk ⬜️ Ignored (Inspect) Visit Preview Aug 11, 2024 10:00am
sui-typescript-docs ⬜️ Ignored (Inspect) Visit Preview Aug 11, 2024 10:00am

@stefan-mysten
Copy link
Contributor

stefan-mysten commented Aug 9, 2024

Let me add a bit of context. On Windows, the Instant::now seems to yield very different values than on Linux (as the underlying calls are OS system calls specific), thus having a value much smaller than duration_since_unix_epoch. What happens is that the checked_sub overflows, so the unwrap on that function call panics.

So when spinning up a local network on Windows with sui start, it panics.

@stefan-mysten stefan-mysten requested a review from a team August 9, 2024 14:12
@mwtian mwtian changed the title [Consensus] use system time for clock [Consensus] avoid assuming current Instant can move back in time Aug 9, 2024
@mwtian mwtian changed the title [Consensus] avoid assuming current Instant can move back in time [Consensus] avoid subtracting from current Instant Aug 9, 2024
@mwtian mwtian marked this pull request as ready for review August 9, 2024 23:03
)
}),
)
.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this is safer than before, but what do you think about handling this unwrap better? In my mind it might entail that we need to either change the return type, or have a default?

Copy link
Member Author

Choose a reason for hiding this comment

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

When this happens, this is an invariant violation (system time overflow) which I don't think there is a reasonable way to fallback or recover. There is nothing the client can do to handle this either, when current time is unavailable. So crash is necessary here. Added a comment to turn the unwrap() into expect().

Copy link
Contributor

@stefan-mysten stefan-mysten left a comment

Choose a reason for hiding this comment

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

This looks good to me, but would love if @akichidis and @arun-koshy can have a look.

Copy link
Contributor

@arun-koshy arun-koshy left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @mwtian!

@mwtian mwtian merged commit 1913ce6 into main Aug 13, 2024
48 checks passed
@mwtian mwtian deleted the consensus-clock branch August 13, 2024 06:29
suiwombat pushed a commit that referenced this pull request Sep 16, 2024
## Description 

On Windows, `Instant::now()` can be close to 0 and we cannot subtract
from that.

## Test plan 

CI. Verified on a Windows machine.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants