Skip to main content

liminal_protocol/wire/
primitives.rs

1/// Stable conversation identifier.
2pub type ConversationId = u64;
3
4/// Permanent participant identifier and base identity index.
5pub type ParticipantId = u64;
6
7/// Permanent participant index used by participant-scoped progress accounting.
8pub type ParticipantIndex = u64;
9
10/// Nonzero participant capability or retired generation.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct Generation(core::num::NonZeroU64);
13
14impl Generation {
15    /// Canonical first participant generation.
16    pub const ONE: Self = Self(core::num::NonZeroU64::MIN);
17
18    /// Creates a generation, returning `None` for the forbidden zero value.
19    #[must_use]
20    pub const fn new(value: u64) -> Option<Self> {
21        match core::num::NonZeroU64::new(value) {
22            Some(value) => Some(Self(value)),
23            None => None,
24        }
25    }
26
27    /// Returns the nonzero wire value.
28    #[must_use]
29    pub const fn get(self) -> u64 {
30        self.0.get()
31    }
32}
33
34/// Conversation delivery sequence.
35pub type DeliverySeq = u64;
36
37/// Serialized conversation transaction order.
38pub type TransactionOrder = u64;
39
40/// Observer refusal/progress epoch.
41pub type ObserverEpoch = u64;
42
43/// Participant protocol version carried in every inner frame prefix.
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub struct ProtocolVersion {
46    /// Major version.
47    pub major: u16,
48    /// Minor version.
49    pub minor: u16,
50}
51
52impl ProtocolVersion {
53    /// Participant protocol v1.0.
54    pub const V1: Self = Self { major: 1, minor: 0 };
55
56    /// Creates a protocol version.
57    #[must_use]
58    pub const fn new(major: u16, minor: u16) -> Self {
59        Self { major, minor }
60    }
61}
62
63/// Server and connection identity for one accepted connection incarnation.
64#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
65pub struct ConnectionIncarnation {
66    /// Durable server incarnation.
67    pub server_incarnation: u64,
68    /// Connection ordinal within that server incarnation.
69    pub connection_ordinal: u64,
70}
71
72impl ConnectionIncarnation {
73    /// Creates a connection incarnation.
74    #[must_use]
75    pub const fn new(server_incarnation: u64, connection_ordinal: u64) -> Self {
76        Self {
77            server_incarnation,
78            connection_ordinal,
79        }
80    }
81}
82
83/// Immutable participant binding epoch.
84#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
85pub struct BindingEpoch {
86    /// Connection that owns the binding.
87    pub connection_incarnation: ConnectionIncarnation,
88    /// Capability generation captured by the binding commit.
89    pub capability_generation: Generation,
90}
91
92impl BindingEpoch {
93    /// Creates a binding epoch.
94    #[must_use]
95    pub const fn new(
96        connection_incarnation: ConnectionIncarnation,
97        capability_generation: Generation,
98    ) -> Self {
99        Self {
100            connection_incarnation,
101            capability_generation,
102        }
103    }
104}
105
106macro_rules! fixed_credential {
107    ($(#[$meta:meta])* $name:ident, $length:expr) => {
108        $(#[$meta])*
109        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
110        pub struct $name([u8; $length]);
111
112        impl $name {
113            /// Creates the fixed-width value from its canonical bytes.
114            #[must_use]
115            pub const fn new(bytes: [u8; $length]) -> Self {
116                Self(bytes)
117            }
118
119            /// Returns the canonical fixed-width bytes.
120            #[must_use]
121            pub const fn into_bytes(self) -> [u8; $length] {
122                self.0
123            }
124
125            /// Borrows the canonical fixed-width bytes.
126            #[must_use]
127            pub const fn as_bytes(&self) -> &[u8; $length] {
128                &self.0
129            }
130        }
131    };
132}
133
134fixed_credential!(
135    /// Single-purpose enrollment attempt token.
136    EnrollmentToken,
137    16
138);
139fixed_credential!(
140    /// Single-purpose credential-attach attempt token.
141    AttachAttemptToken,
142    16
143);
144fixed_credential!(
145    /// Single-purpose explicit-detach attempt token.
146    DetachAttemptToken,
147    16
148);
149fixed_credential!(
150    /// Single-purpose terminal Leave attempt token.
151    LeaveAttemptToken,
152    16
153);
154fixed_credential!(
155    /// Single-purpose ordinary record-admission attempt token.
156    RecordAdmissionAttemptToken,
157    16
158);
159fixed_credential!(
160    /// Participant attach secret.
161    AttachSecret,
162    32
163);