Skip to main content

clawft_kernel/licence/
floor.rs

1//! The verifier's clock floor (ADR-106 section 4, "Clock").
2//!
3//! Per grant key: `floor = max(highest accepted issued_at, persisted local
4//! now high-water mark)`. Expiry uses `max(now, floor)`, so a clock set back
5//! cannot revive a grant. The mark never goes down. Its growth is capped at
6//! 30 days past the newest accepted `issued_at`, so a forward clock jump
7//! cannot push the floor arbitrarily far; undoing a jump that did land is the
8//! Admin `reset_floor`.
9
10use serde::{Deserialize, Serialize};
11
12use super::FAR_FUTURE_CLAMP_SECS;
13
14/// Floor state for one grant key.
15#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(deny_unknown_fields)]
17pub struct FloorState {
18    /// Highest `issued_at` among accepted grants (0 = none yet).
19    pub max_issued: u64,
20    /// Highest capped local `now` seen (the persisted high-water mark).
21    pub hw: u64,
22}
23
24impl FloorState {
25    /// The current floor.
26    pub fn floor(&self) -> u64 {
27        self.max_issued.max(self.hw)
28    }
29
30    /// The time to judge expiry by.
31    pub fn effective_now(&self, now: u64) -> u64 {
32        now.max(self.floor())
33    }
34
35    /// Record the local clock: `hw = max(hw, min(now, max_issued + 30 d))`.
36    /// Nothing is recorded while no grant has been accepted. Never lowers `hw`.
37    pub fn observe(&mut self, now: u64) {
38        if self.max_issued == 0 {
39            return;
40        }
41        let cap = self.max_issued.saturating_add(FAR_FUTURE_CLAMP_SECS);
42        self.hw = self.hw.max(now.min(cap));
43    }
44
45    /// Record an accepted grant's `issued_at`.
46    pub fn note_issued(&mut self, issued_at: u64) {
47        self.max_issued = self.max_issued.max(issued_at);
48    }
49
50    /// Operator reset: forget the high-water mark and restart it from `now`.
51    pub fn reset(&mut self, now: u64) {
52        self.hw = 0;
53        self.observe(now);
54    }
55}