minip2p-platform
Portable clock, deadline, and entropy contracts for minip2p. no_std + alloc compatible.
Everything in minip2p is caller-driven: protocol agents, transports, and runtimes never read a system clock and never draw randomness themselves. The host samples time once per drive iteration and passes that one value to everything it drives; components that need randomness are handed an entropy source. This crate defines those contracts, so the rest of the workspace stays deterministic and testable and all platform access lives in adapters.
Features
Now: one time sample, carrying monotonic milliseconds and optional wall-clock seconds.Clock: the trait adapters implement to produceNow.Deadline: a point on a clock's monotonic timeline, used by caller-driven components to report when they next need attention.EntropySourceandEntropyError: cryptographically secure random bytes, with an explicit "this platform has none" error.StdClockandStdEntropybehind the defaultstdfeature.
Usage
Sample once, then drive everything with that sample:
use ;
let mut clock = new;
// The only sample of this drive iteration; everything below is derived from it.
let now = clock.now;
let mut heartbeat = Heartbeat ;
assert!;
assert!;
// The host can idle until the earliest deadline across all its subsystems.
let idle_ms = heartbeat
.next_deadline
.map;
assert_eq!;
Wall-clock time is optional
Now::unix_seconds is None whenever the platform has no usable wall-clock reading: an embedded board without an RTC or NTP sync has none at all, and a host whose clock is set before the Unix epoch has none that can be reported. Components that need real time — signed beacon freshness, certificate validity — must handle its absence explicitly. Substituting monotonic time is never correct: monotonic epochs are arbitrary and not comparable between peers.
no_std
Build without the std feature and supply your own implementations. A Clock must never let monotonic_ms decrease, and must saturate at Now::MAX_MONOTONIC_MS — u64::MAX is reserved for Deadline::NEVER, so it is not a usable instant:
use ;
;
;
License
MIT