liminal-protocol 0.3.2

Shared participant-lifecycle protocol types for liminal
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use alloc::boxed::Box;

use crate::{algebra::floor_transition, wire::DeliverySeq};

use super::{LiveFrontierError, LiveFrontierOwner, live_frontier::BindingFateOwnerPlan};
use crate::lifecycle::{
    CommittedDiedTerminal, Event, FrontierBinding, ObserverProgressProjection, OrdinaryBindingFate,
    RecoveredBindingFate, SealedBindingFateIntent, SealedBindingFateToken,
};

/// Closed terminal input accepted by protocol-owned binding-fate measurement.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BindingFateTerminal {
    /// Ordinary fate consumes the exact committed Died terminal.
    Ordinary(CommittedDiedTerminal),
    /// Recovered fate after committed Died receives no Died terminal.
    Recovered,
    /// Recovered fate after pending Died preserves exact finalizer authority.
    RecoveredAndReserveFinalizer,
}

/// Protocol-produced binding fate after measuring the post-release floor.
#[derive(Debug, PartialEq, Eq)]
pub enum MeasuredBindingFate {
    /// Ordinary no-marker fate with exact Died provenance.
    Ordinary(OrdinaryBindingFate),
    /// Fenced recovered fate with no Died terminal input.
    Recovered(RecoveredBindingFate),
}

impl MeasuredBindingFate {
    /// Returns the measured floor carried by either closed fate class.
    #[must_use]
    pub const fn resulting_floor(&self) -> DeliverySeq {
        match self {
            Self::Ordinary(fate) => fate.resulting_floor(),
            Self::Recovered(fate) => fate.resulting_floor(),
        }
    }

    /// Projects the protocol-measured floor for observer routing.
    #[must_use]
    pub const fn observer_progress_projection(&self) -> ObserverProgressProjection {
        match self {
            Self::Ordinary(fate) => fate.observer_progress_projection(),
            Self::Recovered(fate) => fate.observer_progress_projection(),
        }
    }
}

/// Successful protocol measurement retaining the coupled frontier owner.
#[derive(Debug, PartialEq, Eq)]
pub struct PreparedBindingFate {
    owner: LiveFrontierOwner,
    fate: MeasuredBindingFate,
    event: Event,
}

impl PreparedBindingFate {
    /// Borrows the measured fate.
    #[must_use]
    pub const fn fate(&self) -> &MeasuredBindingFate {
        &self.fate
    }

    /// Returns the internally minted binding-fate event.
    #[must_use]
    pub const fn event(&self) -> Event {
        self.event
    }

    /// Consumes the prepared transition into the measured next owner and fate.
    #[must_use]
    pub fn into_parts(self) -> (LiveFrontierOwner, MeasuredBindingFate, Event) {
        (self.owner, self.fate, self.event)
    }
}

/// Floor authority measured before a Pending-Died enclosing finalizer commits.
#[derive(Debug, PartialEq, Eq)]
pub struct PendingDiedOrdinaryFinalizer {
    resulting_floor: DeliverySeq,
    authority: FinalizerAuthority,
}

#[derive(Debug, PartialEq, Eq)]
struct FinalizerAuthority;

/// Ordinary fate and unchanged owner prepared for an enclosing finalizer.
#[derive(Debug, PartialEq, Eq)]
pub struct PreparedPendingDiedOrdinaryFinalizer {
    owner: LiveFrontierOwner,
    fate: OrdinaryBindingFate,
    finalizer: PendingDiedOrdinaryFinalizer,
}

impl PreparedPendingDiedOrdinaryFinalizer {
    /// Returns the unchanged owner, measured fate, and one-use floor authority.
    #[must_use]
    pub fn into_parts(
        self,
    ) -> (
        LiveFrontierOwner,
        OrdinaryBindingFate,
        PendingDiedOrdinaryFinalizer,
    ) {
        (self.owner, self.fate, self.finalizer)
    }
}

impl LiveFrontierOwner {
    /// Applies a measured Ordinary floor after its enclosing finalizer transition.
    ///
    /// # Errors
    ///
    /// Returns [`LiveFrontierError`] if retained charges, the measured floor,
    /// marker precedence, or closure accounting disagree with the post-finalizer owner.
    pub fn complete_pending_died_ordinary_finalizer(
        self,
        finalizer: PendingDiedOrdinaryFinalizer,
    ) -> Result<Self, LiveFrontierError> {
        let PendingDiedOrdinaryFinalizer {
            resulting_floor,
            authority,
        } = finalizer;
        let FinalizerAuthority = authority;
        self.install_finalized_binding_fate_floor(resulting_floor)
    }
}

/// Typed reason protocol-owned binding-fate measurement refused.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BindingFateMeasurementError {
    /// The sealed token has no unique ordinary or recovered authority.
    Token,
    /// The token names a different conversation.
    Conversation,
    /// The token's participant is absent from the coupled frontier.
    Participant,
    /// The token's binding epoch or cursor disagrees with the coupled frontier.
    Binding,
    /// Ordinary/recovered terminal input disagrees with the token class.
    Terminal,
    /// Hard observer progress exceeds the candidate high watermark.
    ObserverProgress,
    /// The measured checked floor is outside the delivery-sequence domain.
    ResultingFloor,
    /// The coupled frontier, retained charges, or closure baseline refused installation.
    OwnerTransition,
}

/// Refused measurement preserving every move-only input for serial retry.
#[derive(Debug, PartialEq, Eq)]
pub struct BindingFateMeasurementRefused {
    owner: LiveFrontierOwner,
    token: SealedBindingFateToken,
    terminal: BindingFateTerminal,
    error: BindingFateMeasurementError,
}

impl BindingFateMeasurementRefused {
    /// Returns the typed refusal reason.
    #[must_use]
    pub const fn error(&self) -> BindingFateMeasurementError {
        self.error
    }

    /// Recovers every unchanged input for a same-lock serial retry.
    #[must_use]
    pub fn into_parts(
        self,
    ) -> (
        LiveFrontierOwner,
        SealedBindingFateToken,
        BindingFateTerminal,
    ) {
        (self.owner, self.token, self.terminal)
    }
}

struct ValidatedBindingFateMeasurement {
    participant_id: crate::wire::ParticipantId,
    binding_epoch: crate::wire::BindingEpoch,
    resulting_floor: DeliverySeq,
    owner_plan: BindingFateOwnerPlan,
}

struct ValidatedBindingFateFloor {
    participant_id: crate::wire::ParticipantId,
    binding_epoch: crate::wire::BindingEpoch,
    resulting_floor: DeliverySeq,
}

impl LiveFrontierOwner {
    /// Consumes one sealed fate token after measuring its real post-release floor.
    ///
    /// The server supplies only hard observer progress and the closed terminal
    /// class. The participant, binding epoch, current retained floor, candidate
    /// high watermark, and remaining member cursors all come from protocol-owned
    /// state. Recovered internally mints its event and accepts no terminal.
    ///
    /// # Errors
    ///
    /// Returns every input unchanged when authority, terminal class, observer
    /// progress, or checked floor validation fails.
    pub fn prepare_binding_fate(
        self,
        token: SealedBindingFateToken,
        terminal: BindingFateTerminal,
        hard_observer_progress: DeliverySeq,
    ) -> Result<PreparedBindingFate, Box<BindingFateMeasurementRefused>> {
        let measurement = match validate_binding_fate_measurement(
            &self,
            &token,
            terminal,
            hard_observer_progress,
        ) {
            Ok(measurement) => measurement,
            Err(error) => return refusal(self, token, terminal, error),
        };
        let event = Event::binding_fate_observed(
            measurement.participant_id,
            measurement.binding_epoch,
            measurement.resulting_floor,
        );
        let fate = match terminal {
            BindingFateTerminal::Ordinary(terminal) => token
                .ordinary_binding_fate(terminal, measurement.resulting_floor)
                .map(MeasuredBindingFate::Ordinary),
            BindingFateTerminal::Recovered | BindingFateTerminal::RecoveredAndReserveFinalizer => {
                token
                    .recovered_binding_fate_measured(measurement.resulting_floor)
                    .map(MeasuredBindingFate::Recovered)
            }
        };
        match fate {
            Ok(fate) => {
                let owner = self.install_binding_fate_transition(
                    measurement.owner_plan,
                    measurement.resulting_floor,
                );
                Ok(PreparedBindingFate { owner, fate, event })
            }
            Err(token) => Err(Box::new(BindingFateMeasurementRefused {
                owner: self,
                token: *token,
                terminal,
                error: BindingFateMeasurementError::Terminal,
            })),
        }
    }

    /// Measures Ordinary without installing its owner transition before an enclosing finalizer.
    ///
    /// # Errors
    ///
    /// Returns every move-only input unchanged when the token, terminal,
    /// observer progress, measured floor, or pre-finalizer owner transition
    /// is inconsistent.
    pub fn prepare_pending_died_ordinary_finalizer(
        self,
        token: SealedBindingFateToken,
        terminal: CommittedDiedTerminal,
        hard_observer_progress: DeliverySeq,
    ) -> Result<PreparedPendingDiedOrdinaryFinalizer, Box<BindingFateMeasurementRefused>> {
        let terminal_input = BindingFateTerminal::Ordinary(terminal);
        let measurement = match validate_binding_fate_measurement(
            &self,
            &token,
            terminal_input,
            hard_observer_progress,
        ) {
            Ok(measurement) => measurement,
            Err(error) => {
                return Err(Box::new(BindingFateMeasurementRefused {
                    owner: self,
                    token,
                    terminal: terminal_input,
                    error,
                }));
            }
        };
        let resulting_floor = measurement.resulting_floor;
        match token.ordinary_binding_fate(terminal, resulting_floor) {
            Ok(fate) => Ok(PreparedPendingDiedOrdinaryFinalizer {
                owner: self,
                fate,
                finalizer: PendingDiedOrdinaryFinalizer {
                    resulting_floor,
                    authority: FinalizerAuthority,
                },
            }),
            Err(token) => Err(Box::new(BindingFateMeasurementRefused {
                owner: self,
                token: *token,
                terminal: terminal_input,
                error: BindingFateMeasurementError::Terminal,
            })),
        }
    }

    /// Measures Ordinary after fenced proof minting consumed marker authority.
    ///
    /// The fenced attach transition itself owns the pending identity change, so
    /// this preparation validates the token, terminal, cursor, and exact floor
    /// but defers floor installation to the post-attach owner.
    ///
    /// # Errors
    ///
    /// Returns every move-only input unchanged when token authority, terminal,
    /// observer progress, or checked floor measurement is inconsistent.
    pub fn prepare_pending_died_ordinary_after_fenced_proof(
        self,
        token: SealedBindingFateToken,
        terminal: CommittedDiedTerminal,
        hard_observer_progress: DeliverySeq,
    ) -> Result<PreparedPendingDiedOrdinaryFinalizer, Box<BindingFateMeasurementRefused>> {
        let terminal_input = BindingFateTerminal::Ordinary(terminal);
        let measurement = match validate_binding_fate_floor(
            &self,
            &token,
            terminal_input,
            hard_observer_progress,
        ) {
            Ok(measurement) => measurement,
            Err(error) => {
                return Err(Box::new(BindingFateMeasurementRefused {
                    owner: self,
                    token,
                    terminal: terminal_input,
                    error,
                }));
            }
        };
        let resulting_floor = measurement.resulting_floor;
        match token.ordinary_binding_fate(terminal, resulting_floor) {
            Ok(fate) => Ok(PreparedPendingDiedOrdinaryFinalizer {
                owner: self,
                fate,
                finalizer: PendingDiedOrdinaryFinalizer {
                    resulting_floor,
                    authority: FinalizerAuthority,
                },
            }),
            Err(token) => Err(Box::new(BindingFateMeasurementRefused {
                owner: self,
                token: *token,
                terminal: terminal_input,
                error: BindingFateMeasurementError::Terminal,
            })),
        }
    }
}

fn validate_binding_fate_measurement(
    owner: &LiveFrontierOwner,
    token: &SealedBindingFateToken,
    terminal: BindingFateTerminal,
    hard_observer_progress: DeliverySeq,
) -> Result<ValidatedBindingFateMeasurement, BindingFateMeasurementError> {
    let floor = validate_binding_fate_floor(owner, token, terminal, hard_observer_progress)?;
    let owner_plan = owner
        .prepare_binding_fate_transition(
            floor.participant_id,
            floor.binding_epoch,
            token
                .measurement_context()
                .ok_or(BindingFateMeasurementError::Token)?
                .cursor,
            floor.resulting_floor,
            terminal == BindingFateTerminal::RecoveredAndReserveFinalizer,
        )
        .map_err(|_| BindingFateMeasurementError::OwnerTransition)?;
    Ok(ValidatedBindingFateMeasurement {
        participant_id: floor.participant_id,
        binding_epoch: floor.binding_epoch,
        resulting_floor: floor.resulting_floor,
        owner_plan,
    })
}

fn validate_binding_fate_floor(
    owner: &LiveFrontierOwner,
    token: &SealedBindingFateToken,
    terminal: BindingFateTerminal,
    hard_observer_progress: DeliverySeq,
) -> Result<ValidatedBindingFateFloor, BindingFateMeasurementError> {
    let Some(context) = token.measurement_context() else {
        return Err(BindingFateMeasurementError::Token);
    };
    if context.conversation_id != owner.frontiers().conversation_id() {
        return Err(BindingFateMeasurementError::Conversation);
    }
    let Some(participant) = owner
        .frontiers()
        .active_identities()
        .participants()
        .iter()
        .find(|participant| participant.participant_index() == context.participant_id)
    else {
        return Err(BindingFateMeasurementError::Participant);
    };
    if participant.cursor() != context.cursor
        || participant.binding() != FrontierBinding::Bound(context.binding_epoch)
            && participant.binding() != FrontierBinding::Detached(context.binding_epoch)
    {
        return Err(BindingFateMeasurementError::Binding);
    }
    let terminal_matches = match (token.intent(), terminal) {
        (Some(SealedBindingFateIntent::Ordinary), BindingFateTerminal::Ordinary(died)) => {
            died.conversation_id() == context.conversation_id
                && died.participant_id() == context.participant_id
                && died.binding_epoch() == context.binding_epoch
        }
        (
            Some(SealedBindingFateIntent::Recovered { .. }),
            BindingFateTerminal::Recovered | BindingFateTerminal::RecoveredAndReserveFinalizer,
        ) => true,
        _ => false,
    };
    if !terminal_matches {
        return Err(BindingFateMeasurementError::Terminal);
    }
    let candidate_high_watermark = owner.frontiers().sequence().ledger().high_watermark();
    if hard_observer_progress > candidate_high_watermark {
        return Err(BindingFateMeasurementError::ObserverProgress);
    }
    let minimum_remaining_cursor = owner
        .frontiers()
        .active_identities()
        .participants()
        .iter()
        .filter(|participant| participant.participant_index() != context.participant_id)
        .map(|participant| participant.cursor())
        .min();
    let measured = floor_transition(
        owner.frontiers().retained_floor(),
        minimum_remaining_cursor,
        candidate_high_watermark,
        hard_observer_progress,
        owner.frontiers().retained_floor(),
    );
    let Ok(resulting_floor) = DeliverySeq::try_from(measured.resulting_floor) else {
        return Err(BindingFateMeasurementError::ResultingFloor);
    };
    Ok(ValidatedBindingFateFloor {
        participant_id: context.participant_id,
        binding_epoch: context.binding_epoch,
        resulting_floor,
    })
}

fn refusal(
    owner: LiveFrontierOwner,
    token: SealedBindingFateToken,
    terminal: BindingFateTerminal,
    error: BindingFateMeasurementError,
) -> Result<PreparedBindingFate, Box<BindingFateMeasurementRefused>> {
    Err(Box::new(BindingFateMeasurementRefused {
        owner,
        token,
        terminal,
        error,
    }))
}