1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Portable identities and authority contracts.
use bytes::Bytes;
use serde::{Deserialize, Serialize};
/// Widens one native collection size into the durable 64-bit accounting domain.
///
/// Rust supports pointer widths no larger than 64 bits, so this conversion is
/// an invariant rather than a recoverable filesystem outcome on every native
/// and WebAssembly target supported by this crate.
#[inline]
pub(crate) fn usize_to_u64(value: usize) -> u64 {
value as u64
}
use std::fmt;
use thiserror::Error;
use uuid::Uuid;
/// Stable identity of one independently fenced authority stream.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AuthorityId(Uuid);
impl AuthorityId {
/// Creates a fresh time-ordered authority identity.
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
/// Constructs an identity from its canonical 16 bytes.
#[must_use]
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
Self(Uuid::from_bytes(bytes))
}
/// Returns the canonical bytes.
#[must_use]
pub const fn into_bytes(self) -> [u8; 16] {
self.0.into_bytes()
}
}
impl Default for AuthorityId {
fn default() -> Self {
Self::new()
}
}
/// Stable idempotency identity for one authority operation.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OperationId(Uuid);
impl OperationId {
/// Creates a fresh time-ordered operation identity.
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
/// Constructs an identity from its canonical 16 bytes.
#[must_use]
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
Self(Uuid::from_bytes(bytes))
}
/// Returns the canonical bytes.
#[must_use]
pub const fn into_bytes(self) -> [u8; 16] {
self.0.into_bytes()
}
}
impl Default for OperationId {
fn default() -> Self {
Self::new()
}
}
/// Monotonic authority incarnation used to fence stale writers.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Epoch(u64);
impl Epoch {
/// Genesis authority epoch.
pub const GENESIS: Self = Self(1);
/// Constructs a non-zero epoch.
///
/// # Errors
///
/// Returns [`IdentityError::ZeroEpoch`] when `value` is zero.
pub const fn new(value: u64) -> Result<Self, IdentityError> {
if value == 0 {
Err(IdentityError::ZeroEpoch)
} else {
Ok(Self(value))
}
}
/// Returns the wire integer.
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
/// Dense one-based sequence within an authority history; zero denotes genesis.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Sequence(u64);
impl Sequence {
/// Empty stream position.
pub const GENESIS: Self = Self(0);
/// Constructs a sequence from its wire integer.
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
/// Returns the wire integer.
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
/// Returns the next sequence, failing closed at exhaustion.
///
/// # Errors
///
/// Returns [`IdentityError::SequenceExhausted`] at `u64::MAX`.
pub const fn checked_next(self) -> Result<Self, IdentityError> {
match self.0.checked_add(1) {
Some(value) => Ok(Self(value)),
None => Err(IdentityError::SequenceExhausted),
}
}
}
/// Canonical 256-bit digest.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Digest([u8; 32]);
impl Digest {
/// Digest used only for an empty/genesis predecessor.
pub const ZERO: Self = Self([0; 32]);
/// Constructs a digest from canonical bytes.
#[must_use]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
/// Borrows canonical bytes.
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
/// Returns canonical bytes.
#[must_use]
pub const fn into_bytes(self) -> [u8; 32] {
self.0
}
}
impl fmt::Debug for Digest {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_tuple("Digest")
.field(&hex::encode(self.0))
.finish()
}
}
macro_rules! uuid_identity {
($name:ident, $description:literal) => {
#[doc = $description]
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct $name(Uuid);
impl $name {
/// Creates a fresh time-ordered identity.
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
/// Constructs an identity from its canonical 16 bytes.
#[must_use]
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
Self(Uuid::from_bytes(bytes))
}
/// Returns the canonical bytes.
#[must_use]
pub const fn into_bytes(self) -> [u8; 16] {
self.0.into_bytes()
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
};
}
uuid_identity!(
VolumeId,
"Stable identity of one independently versioned volume."
);
uuid_identity!(
CheckoutId,
"Process-independent identity of one volume checkout."
);
uuid_identity!(MountId, "Stable identity of one mounted-volume binding.");
uuid_identity!(
WatchId,
"Process-local identity of one native watcher handle."
);
uuid_identity!(
FileId,
"Stable path-independent identity of one file record."
);
/// Content-addressed identity of one immutable volume generation.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct GenerationId(Digest);
impl GenerationId {
/// Constructs a generation identity from its authenticated root digest.
#[must_use]
pub const fn new(digest: Digest) -> Self {
Self(digest)
}
/// Returns the authenticated root digest.
#[must_use]
pub const fn digest(self) -> Digest {
self.0
}
}
/// Durable authority head.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Head {
/// Current fencing epoch.
pub epoch: Epoch,
/// Last committed sequence.
pub sequence: Sequence,
/// Hash-chain head, or [`Digest::ZERO`] at genesis.
pub digest: Digest,
}
impl Head {
/// Empty head for a new authority.
#[must_use]
pub const fn genesis(epoch: Epoch) -> Self {
Self {
epoch,
sequence: Sequence::GENESIS,
digest: Digest::ZERO,
}
}
}
/// One canonical opaque operation submitted to authority.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProposedCommit {
/// Stable retry identity.
pub operation_id: OperationId,
/// Digest of the complete semantic command and caller-visible preconditions.
pub fingerprint: Digest,
/// Canonical schema-versioned operation bytes.
pub payload: Bytes,
}
/// One recovered or newly durable authority commit.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DurableCommit {
/// Writer-fencing epoch that admitted this commit.
pub epoch: Epoch,
/// Assigned dense sequence.
pub sequence: Sequence,
/// Stable retry identity.
pub operation_id: OperationId,
/// Original command fingerprint.
pub fingerprint: Digest,
/// Previous hash-chain head.
pub previous_digest: Digest,
/// Commit hash-chain head.
pub digest: Digest,
/// Canonical schema-versioned operation bytes.
pub payload: Bytes,
}
/// Canonical non-payload bytes hashed into one authority commit identity.
pub const AUTHORITY_COMMIT_DIGEST_ENVELOPE_BYTES: u64 = 151;
/// Computes the canonical hash-chain digest for one durable authority commit.
///
/// This function is the backend conformance boundary: every authority store
/// must produce this exact digest for the same canonical operation.
#[must_use]
pub fn authority_commit_digest(
authority_id: AuthorityId,
epoch: Epoch,
sequence: Sequence,
operation_id: OperationId,
fingerprint: Digest,
previous_digest: Digest,
payload: &[u8],
) -> Digest {
const DOMAIN: &[u8] = b"acyclic-fs-authority-commit-v1\0";
let mut hasher = blake3::Hasher::new();
hasher.update(DOMAIN);
hasher.update(&authority_id.into_bytes());
hasher.update(&epoch.get().to_le_bytes());
hasher.update(&sequence.get().to_le_bytes());
hasher.update(&operation_id.into_bytes());
hasher.update(fingerprint.as_bytes());
hasher.update(previous_digest.as_bytes());
hasher.update(
&u64::try_from(payload.len())
.unwrap_or(u64::MAX)
.to_le_bytes(),
);
hasher.update(payload);
Digest::from_bytes(*hasher.finalize().as_bytes())
}
/// Errors constructing portable identities.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
pub enum IdentityError {
/// Epoch zero is reserved and never valid.
#[error("authority epoch must be non-zero")]
ZeroEpoch,
/// No further sequence can be represented.
#[error("authority sequence exhausted")]
SequenceExhausted,
}
#[cfg(test)]
#[path = "tests/foundation.rs"]
mod tests;