Skip to main content

df_hyperliquid_core/utils/
nonce.rs

1/// One account's nonces: milliseconds, strictly increasing. The venue keeps
2/// the highest nonces it has seen per signer and refuses a repeat, so each
3/// account holds its own clock. The time is passed in, since the browser and
4/// Workers have no std clock.
5#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
6pub struct NonceClock {
7    last: u64,
8}
9
10impl NonceClock {
11    #[must_use]
12    pub const fn new() -> Self {
13        NonceClock { last: 0 }
14    }
15
16    /// A clock that hands out nothing at or below `last`.
17    #[must_use]
18    pub const fn after(last: u64) -> Self {
19        NonceClock { last }
20    }
21
22    /// `max(now_ms, last + 1)`, remembered as the new last.
23    pub fn next(&mut self, now_ms: u64) -> u64 {
24        let nonce = now_ms.max(self.last.saturating_add(1));
25        self.last = nonce;
26        nonce
27    }
28
29    #[must_use]
30    pub const fn last(&self) -> u64 {
31        self.last
32    }
33}
34
35#[cfg(test)]
36#[path = "tests/nonce.rs"]
37mod tests;