Skip to main content

qualia_core_db/net/peer/fabric/
evidence.rs

1//! Path evidence. Successful validation is not a public constructor.
2
3use super::carrier::PathClass;
4
5#[repr(u8)]
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum ObserverKind {
8    /// Produced by this node's transport integration.
9    LocalTransport = 1,
10    /// A peer or provider assertion. Not local validation.
11    RemoteAssertion = 2,
12}
13
14/// Opaque witness minted only by a carrier after a real check.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct TransportWitness {
17    class: PathClass,
18    generation: u32,
19    max_payload: u16,
20    rtt_ms: u32,
21    at_ms: u64,
22}
23
24impl TransportWitness {
25    /// Builds `TransportWitness` from `local`.
26    #[cfg(any(not(target_arch = "wasm32"), test))]
27    pub(crate) const fn from_local(
28        class: PathClass,
29        generation: u32,
30        max_payload: u16,
31        rtt_ms: u32,
32        at_ms: u64,
33    ) -> Self {
34        Self {
35            class,
36            generation,
37            max_payload,
38            rtt_ms,
39            at_ms,
40        }
41    }
42
43    /// `TransportWitness::class`.
44    pub const fn class(self) -> PathClass {
45        self.class
46    }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub struct PathEvidence {
51    pub class: PathClass,
52    pub generation: u32,
53    validated: bool,
54    pub observed_at_ms: u64,
55    pub max_payload: u16,
56    pub rtt_ms: u32,
57    pub observer: ObserverKind,
58}
59
60impl PathEvidence {
61    /// `PathEvidence::remote_assertion`.
62    pub const fn remote_assertion(class: PathClass, generation: u32, at_ms: u64) -> Self {
63        Self {
64            class,
65            generation,
66            validated: false,
67            observed_at_ms: at_ms,
68            max_payload: 0,
69            rtt_ms: u32::MAX,
70            observer: ObserverKind::RemoteAssertion,
71        }
72    }
73
74    /// Builds `PathEvidence` from `witness`.
75    pub const fn from_witness(w: TransportWitness) -> Self {
76        Self {
77            class: w.class,
78            generation: w.generation,
79            validated: true,
80            observed_at_ms: w.at_ms,
81            max_payload: w.max_payload,
82            rtt_ms: w.rtt_ms,
83            observer: ObserverKind::LocalTransport,
84        }
85    }
86
87    /// `PathEvidence::validated`.
88    pub const fn validated(&self) -> bool {
89        self.validated && matches!(self.observer, ObserverKind::LocalTransport)
90    }
91
92    /// `PathEvidence::stale_generation`.
93    pub const fn stale_generation(&self, live: u32) -> bool {
94        self.generation != live
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn remote_assertion_is_not_validated() {
104        let e = PathEvidence::remote_assertion(PathClass::DirectV6, 1, 10);
105        assert!(!e.validated());
106        let w = TransportWitness::from_local(PathClass::Relayed, 1, 1152, 12, 10);
107        assert!(PathEvidence::from_witness(w).validated());
108    }
109}