Skip to main content

chio_kernel_core/
clock.rs

1//! Abstract clock for capability time-bound enforcement.
2//!
3//! The kernel core never calls `std::time::SystemTime::now()`. All time
4//! enters the pure evaluation surface through a `&dyn Clock` so that
5//! browser, WASM, and embedded adapters can inject `Date.now()`,
6//! `instant::now()`, or a fuzzed/mock clock for deterministic testing.
7
8/// Abstract monotonic wall-clock exposing Unix seconds.
9///
10/// Implementations MUST return a value consistent with the signed
11/// `issued_at` / `expires_at` fields on capabilities. The verdict path is
12/// fail-closed against clock errors: if `now_unix_secs` returns a value in
13/// the past of `issued_at` or past `expires_at`, the capability is rejected.
14pub trait Clock {
15    /// Current Unix timestamp in seconds.
16    fn now_unix_secs(&self) -> u64;
17}
18
19/// Test-only clock that returns a fixed value.
20///
21/// Useful for deterministic evaluation harnesses (e.g. the wasm
22/// platform adapter's `evaluate_at_time()` helper and the
23/// `portable_build.rs` integration test).
24#[derive(Debug, Clone, Copy)]
25pub struct FixedClock {
26    now_unix_secs: u64,
27}
28
29impl FixedClock {
30    /// Build a fixed clock pinned to `now_unix_secs`.
31    #[must_use]
32    pub const fn new(now_unix_secs: u64) -> Self {
33        Self { now_unix_secs }
34    }
35}
36
37impl Clock for FixedClock {
38    fn now_unix_secs(&self) -> u64 {
39        self.now_unix_secs
40    }
41}
42
43impl<T: Clock + ?Sized> Clock for &T {
44    fn now_unix_secs(&self) -> u64 {
45        (**self).now_unix_secs()
46    }
47}