liminal-server 0.24.5

Standalone server for the liminal messaging bus
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Exact checkpoint framing and validator delegation.

use std::error::Error;

use super::restore_checkpoint::{
    CheckpointError, CheckpointLiveState, CheckpointPositions, CheckpointSchema, RestoreCheckpoint,
};

#[derive(Debug, PartialEq, Eq)]
struct LiveBytes(Vec<u8>);

impl CheckpointLiveState for LiveBytes {
    type RestoreContext = u8;

    fn encode_live(&self) -> Result<Vec<u8>, CheckpointError> {
        Ok(self.0.clone())
    }

    fn decode_live(
        bytes: &[u8],
        positions: &CheckpointPositions,
        history_root: &[u8],
        context: &Self::RestoreContext,
    ) -> Result<Self, CheckpointError> {
        let live = Self(bytes.to_vec());
        if live.0.first() != Some(context) {
            return Err(CheckpointError::LiveState {
                source: std::io::Error::other("configured live tag differs").into(),
            });
        }
        live.validate_live(positions, history_root)?;
        Ok(live)
    }

    fn validate_live(
        &self,
        positions: &CheckpointPositions,
        history_root: &[u8],
    ) -> Result<(), CheckpointError> {
        if history_root != [9; 32] {
            return Err(CheckpointError::LiveState {
                source: std::io::Error::other("fixture root differs").into(),
            });
        }
        if self.0.first().copied() != Some(7) || positions.conversation_id != 7 {
            return Err(CheckpointError::LiveState {
                source: std::io::Error::other("fixture identity differs from its live owner")
                    .into(),
            });
        }
        Ok(())
    }
}

fn positions(history: u64) -> CheckpointPositions {
    CheckpointPositions {
        conversation_id: 7,
        covered_log_sequence: history,
        next_event_ordinal: history + 1,
        next_log_sequence: history + 1,
        next_seq: history + 1,
        next_order: history,
        next_participant: 2,
        extension_sequence: history,
        highest_delivery_seq: history,
        observer_progress: history,
        merged_base_sequence: history + 1,
        merged_extension_sequence: history,
        schema: CheckpointSchema::V4Suffix,
    }
}

fn checkpoint(history: u64) -> Result<RestoreCheckpoint<LiveBytes, 32>, CheckpointError> {
    RestoreCheckpoint::new(positions(history), [9; 32], LiveBytes(vec![7, 1, 4]))
}

#[test]
fn checkpoint_round_trip_preserves_every_position_root_and_live_byte() -> Result<(), Box<dyn Error>>
{
    let original = checkpoint(1_000)?;
    let encoded = original.encode_canonical()?;
    let restored = RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&encoded, 7, &7)?;
    assert_eq!(original, restored);
    assert_eq!(restored.encode_canonical()?, encoded);
    let (saved, root, live) = restored.into_parts();
    assert_eq!(saved, positions(1_000));
    assert_eq!(root, [9; 32]);
    assert_eq!(live, LiveBytes(vec![7, 1, 4]));
    Ok(())
}

#[test]
fn live_bytes_and_fixed_position_length_do_not_grow_with_history() -> Result<(), Box<dyn Error>> {
    let mut lengths = Vec::new();
    for history in [1_000, 42_005, 420_000] {
        let saved = checkpoint(history)?;
        assert_eq!(saved.live().0, [7, 1, 4]);
        lengths.push(saved.encode_canonical()?.len());
    }
    assert!(lengths.windows(2).all(|pair| pair[0] == pair[1]));
    assert_eq!(
        checkpoint(42_005)?.encode_canonical()?,
        checkpoint(42_005)?.encode_canonical()?
    );
    let wrong_varint_lengths = [1_000_u64, 420_000].map(|value| {
        let mut value = value;
        let mut length = 1;
        while value >= 128 {
            value >>= 7;
            length += 1;
        }
        length
    });
    assert_ne!(wrong_varint_lengths[0], wrong_varint_lengths[1]);
    Ok(())
}

#[test]
fn every_truncation_and_trailing_byte_is_refused() -> Result<(), Box<dyn Error>> {
    let bytes = checkpoint(1_000)?.encode_canonical()?;
    for end in 0..bytes.len() {
        assert!(
            RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&bytes[..end], 7, &7).is_err()
        );
    }
    let mut trailing = bytes;
    trailing.push(0);
    assert!(RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&trailing, 7, &7).is_err());
    Ok(())
}

#[test]
fn every_changed_byte_fails_integrity_or_canonical_validation() -> Result<(), Box<dyn Error>> {
    let bytes = checkpoint(1_000)?.encode_canonical()?;
    for at in 0..bytes.len() {
        let mut changed = bytes.clone();
        changed[at] ^= 1;
        assert!(RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&changed, 7, &7).is_err());
    }
    Ok(())
}

#[test]
fn a_valid_checksum_does_not_bypass_the_live_owner_validator() -> Result<(), Box<dyn Error>> {
    let mut bytes = checkpoint(1_000)?.encode_canonical()?;
    let live_at = bytes.len() - 32 - 3;
    bytes[live_at] = 8;
    let digest_at = bytes.len() - 32;
    let digest = *blake3::hash(&bytes[..digest_at]).as_bytes();
    bytes[digest_at..].copy_from_slice(&digest);
    assert!(matches!(
        RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&bytes, 7, &7),
        Err(CheckpointError::LiveState { .. })
    ));
    Ok(())
}

#[test]
fn foreign_identity_and_root_schema_are_refused() -> Result<(), Box<dyn Error>> {
    let bytes = checkpoint(1_000)?.encode_canonical()?;
    assert!(matches!(
        RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&bytes, 8, &7),
        Err(CheckpointError::ForeignConversation {
            expected: 8,
            actual: 7
        })
    ));
    assert!(matches!(
        RestoreCheckpoint::<LiveBytes, 16>::decode_canonical(&bytes, 7, &7),
        Err(CheckpointError::RootWidth {
            expected: 16,
            actual: 32
        })
    ));
    Ok(())
}

#[test]
fn invalid_heads_and_zero_root_width_are_refused_before_encoding() {
    let mut invalid = positions(1_000);
    invalid.next_log_sequence = invalid.covered_log_sequence;
    assert!(matches!(
        RestoreCheckpoint::new(invalid, [9; 32], LiveBytes(vec![7])),
        Err(CheckpointError::Position { .. })
    ));
    assert!(matches!(
        RestoreCheckpoint::new(positions(1_000), [], LiveBytes(vec![7])),
        Err(CheckpointError::RootWidth {
            expected: 0,
            actual: 0
        })
    ));
}

fn reseal(bytes: &mut [u8]) -> Result<(), Box<dyn Error>> {
    let at = bytes.len().checked_sub(32).ok_or("fixture has no digest")?;
    let digest = *blake3::hash(&bytes[..at]).as_bytes();
    bytes[at..].copy_from_slice(&digest);
    Ok(())
}

#[test]
fn checksummed_invalid_positions_schema_and_sections_retain_named_refusals()
-> Result<(), Box<dyn Error>> {
    let original = checkpoint(1_000)?.encode_canonical()?;
    let mut bad_head = original.clone();
    bad_head[38..46].copy_from_slice(&1_000_u64.to_be_bytes());
    reseal(&mut bad_head)?;
    assert!(matches!(
        RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&bad_head, 7, &7),
        Err(CheckpointError::Position {
            field: "next log sequence"
        })
    ));
    let mut bad_schema = original.clone();
    bad_schema[110] = 5;
    reseal(&mut bad_schema)?;
    assert!(matches!(
        RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&bad_schema, 7, &7),
        Err(CheckpointError::Schema { tag: 5 })
    ));
    let mut bad_section = original;
    bad_section[145..147].copy_from_slice(&3_u16.to_be_bytes());
    reseal(&mut bad_section)?;
    assert!(matches!(
        RestoreCheckpoint::<LiveBytes, 32>::decode_canonical(&bad_section, 7, &7),
        Err(CheckpointError::Section { tag: 2 })
    ));
    Ok(())
}

#[derive(Debug)]
struct LooseLive(LiveBytes);

impl CheckpointLiveState for LooseLive {
    type RestoreContext = u8;

    fn encode_live(&self) -> Result<Vec<u8>, CheckpointError> {
        self.0.encode_live()
    }

    fn decode_live(
        bytes: &[u8],
        positions: &CheckpointPositions,
        history_root: &[u8],
        context: &Self::RestoreContext,
    ) -> Result<Self, CheckpointError> {
        let decoded = LiveBytes::decode_live(bytes, positions, history_root, context)?;
        let first = decoded
            .0
            .first()
            .copied()
            .ok_or(CheckpointError::Truncated {
                required: 1,
                available: decoded.0.len(),
            })?;
        Ok(Self(LiveBytes(vec![first])))
    }

    fn validate_live(
        &self,
        positions: &CheckpointPositions,
        history_root: &[u8],
    ) -> Result<(), CheckpointError> {
        self.0.validate_live(positions, history_root)
    }
}

#[test]
fn an_owner_decoder_that_discards_unknown_live_bytes_cannot_install_a_checkpoint()
-> Result<(), Box<dyn Error>> {
    let checkpoint =
        RestoreCheckpoint::new(positions(1_000), [9; 32], LooseLive(LiveBytes(vec![7])))?;
    let mut encoded = checkpoint.encode_canonical()?;
    let extra_at = encoded.len() - 32;
    encoded.insert(extra_at, 1);
    encoded[147..151].copy_from_slice(&2_u32.to_be_bytes());
    reseal(&mut encoded)?;
    assert!(matches!(
        RestoreCheckpoint::<LooseLive, 32>::decode_canonical(&encoded, 7, &7),
        Err(CheckpointError::LiveEncoding)
    ));
    Ok(())
}

#[derive(Debug)]
struct HistoryLookupLive(Vec<u8>);

impl CheckpointLiveState for HistoryLookupLive {
    type RestoreContext = std::cell::RefCell<Vec<&'static str>>;

    fn encode_live(&self) -> Result<Vec<u8>, CheckpointError> {
        Ok(self.0.clone())
    }

    fn decode_live(
        bytes: &[u8],
        positions: &CheckpointPositions,
        history_root: &[u8],
        context: &Self::RestoreContext,
    ) -> Result<Self, CheckpointError> {
        context.borrow_mut().push("history lookup");
        let owner = Self(bytes.to_vec());
        owner.validate_live(positions, history_root)?;
        Ok(owner)
    }

    fn validate_live(
        &self,
        positions: &CheckpointPositions,
        history_root: &[u8],
    ) -> Result<(), CheckpointError> {
        if positions.conversation_id != 7 || history_root.len() != 32 || self.0 != [7] {
            return Err(CheckpointError::LiveEncoding);
        }
        Ok(())
    }
}

#[test]
fn checkpoint_root_association_precedes_any_owner_history_lookup() -> Result<(), Box<dyn Error>> {
    let encoded = RestoreCheckpoint::new(positions(1_000), [9; 32], HistoryLookupLive(vec![7]))?
        .encode_canonical()?;
    let expected_root = [8; 32];
    let calls = std::cell::RefCell::new(Vec::new());
    let result = RestoreCheckpoint::<HistoryLookupLive, 32>::decode_canonical_with_association(
        &encoded,
        7,
        &calls,
        |_, root| {
            if root != &expected_root {
                return Err(CheckpointError::Association {
                    field: "history root",
                });
            }
            Ok(())
        },
    );
    assert!(
        calls.borrow().is_empty(),
        "unassociated root triggered a history lookup"
    );
    assert!(matches!(
        result,
        Err(CheckpointError::Association {
            field: "history root"
        })
    ));
    Ok(())
}

#[test]
fn associated_checkpoint_decodes_the_owner_once_and_in_the_required_order()
-> Result<(), Box<dyn Error>> {
    let encoded = RestoreCheckpoint::new(positions(1_000), [9; 32], HistoryLookupLive(vec![7]))?
        .encode_canonical()?;
    let calls = std::cell::RefCell::new(Vec::new());
    let restored = RestoreCheckpoint::<HistoryLookupLive, 32>::decode_canonical_with_association(
        &encoded,
        7,
        &calls,
        |saved, root| {
            assert_eq!(*saved, positions(1_000));
            assert_eq!(*root, [9; 32]);
            calls.borrow_mut().push("association");
            Ok(())
        },
    )?;
    assert_eq!(*calls.borrow(), ["association", "history lookup"]);
    assert_eq!(restored.live().0, [7]);
    Ok(())
}

#[test]
fn malformed_checkpoint_never_calls_association_or_the_owner() -> Result<(), Box<dyn Error>> {
    let mut encoded =
        RestoreCheckpoint::new(positions(1_000), [9; 32], HistoryLookupLive(vec![7]))?
            .encode_canonical()?;
    let last = encoded.len() - 1;
    encoded[last] ^= 1;
    let calls = std::cell::RefCell::new(Vec::new());
    let result = RestoreCheckpoint::<HistoryLookupLive, 32>::decode_canonical_with_association(
        &encoded,
        7,
        &calls,
        |_, _| {
            calls.borrow_mut().push("association");
            Ok(())
        },
    );
    assert!(matches!(result, Err(CheckpointError::Integrity)));
    assert!(calls.borrow().is_empty());
    Ok(())
}

#[test]
fn post_decode_association_is_a_positive_control_for_the_wrong_lookup_order()
-> Result<(), Box<dyn Error>> {
    let encoded = RestoreCheckpoint::new(positions(1_000), [9; 32], HistoryLookupLive(vec![7]))?
        .encode_canonical()?;
    let calls = std::cell::RefCell::new(Vec::new());
    let result = RestoreCheckpoint::<HistoryLookupLive, 32>::decode_canonical(&encoded, 7, &calls)
        .and_then(|saved| {
            if saved.history_root() != &[8; 32] {
                return Err(CheckpointError::Association {
                    field: "history root",
                });
            }
            Ok(saved)
        });
    assert!(matches!(
        result,
        Err(CheckpointError::Association {
            field: "history root"
        })
    ));
    assert_eq!(*calls.borrow(), ["history lookup"]);
    Ok(())
}