routers_realtime 0.5.0

A Demonstration for Real-Time Map Matching
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Orchestrator: the pure result reader and validator.
//!
//! Classifies each solve result against the vehicle's current state as commit,
//! park (early — replay has not re-created its job), reject (obsolete), or
//! quarantine (a same-id redelivery whose bytes cannot be compared here). Pure
//! and I/O-free: the worker performs the side effect the verdict implies.

use core::cmp::Ordering;

use routers_network::Entry;

use crate::bus::adapter::AckHandle;
use crate::event::MatchedDiff;
use crate::orchestrator::scheduler::{ActiveJob, VehicleState};
use crate::protocol::ids::SCHEMA_VERSION;
use crate::protocol::job::BaseState;
use crate::protocol::result::SolveResult;
use crate::store::checkpoint::VehicleCheckpoint;

/// The validator's decision for one solve result against one vehicle's state.
#[derive(Debug)]
pub enum Verdict<'a> {
    /// The expected result for the vehicle's active job: commit it.
    Accept {
        /// The in-flight job this result answers.
        job: &'a ActiveJob,
    },
    /// No active job yet for this observation, but it is newer than the last
    /// committed input: replay has not reached it. Park and revisit when the job is dispatched.
    Park,
    /// Definitively obsolete: ack and drop.
    Reject(RejectReason),
    /// A same-identity result that must never reach the commit path; kept for diagnosis.
    Quarantine(QuarantineReason),
}

impl Verdict<'_> {
    /// A bounded, low-cardinality label for the verdict class, suitable as a metric dimension.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            Verdict::Accept { .. } => "accept",
            Verdict::Park => "park",
            Verdict::Reject(_) => "reject",
            Verdict::Quarantine(_) => "quarantine",
        }
    }
}

/// Why a result is definitively obsolete and may be acknowledged and dropped.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RejectReason {
    /// The result's observation is at or below the vehicle's last committed input.
    Committed,
    /// The result resumes from a base the vehicle no longer holds.
    StaleBase {
        /// The base the vehicle expects (its in-flight job's base), if any.
        expected: Option<BaseState>,
        /// The base the result was solved against, if any.
        got: Option<BaseState>,
    },
    /// The result belongs to a continuity segment the vehicle has since closed.
    SegmentClosed,
    /// The result's vehicle does not map to the partition this worker owns.
    WrongVehicle,
    /// The result was produced against a wire schema this build does not serve.
    Schema,
    /// The echoed job id does not match the id its identity hashes to.
    IdMismatch,
}

impl RejectReason {
    /// A bounded, low-cardinality metric label; one stable string per variant.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            RejectReason::Committed => "committed",
            RejectReason::StaleBase { .. } => "stale_base",
            RejectReason::SegmentClosed => "segment_closed",
            RejectReason::WrongVehicle => "wrong_vehicle",
            RejectReason::Schema => "schema",
            RejectReason::IdMismatch => "id_mismatch",
        }
    }
}

/// Why a result is quarantined: kept out of the commit path but retained for diagnosis.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QuarantineReason {
    /// Same logical job identity as one already accepted, but different semantic content. Raised by the worker, never by the pure validator.
    ConflictingContent,
    /// A second result for a job whose commit is already in flight; its bytes cannot be compared here.
    DuplicateAfterCommit,
}

impl QuarantineReason {
    /// A bounded, low-cardinality metric label; one stable string per variant.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            QuarantineReason::ConflictingContent => "conflicting_content",
            QuarantineReason::DuplicateAfterCommit => "duplicate_after_commit",
        }
    }
}

/// Classify one solve `result` against a `vehicle`'s current state on the
/// `partition` this worker owns.
///
/// Checks run in strict order — envelope integrity, addressing, segment
/// continuity, then job matching — so the most authoritative disqualifier wins.
/// Pure: the worker performs the commit / park / ack / retain the verdict implies.
pub fn validate<'a, E, H>(
    vehicle: &'a VehicleState<E, H>,
    result: &SolveResult<E>,
    partition: u16,
) -> Verdict<'a>
where
    E: Entry,
    H: AckHandle,
{
    if result.verify().is_err() {
        return Verdict::Reject(RejectReason::IdMismatch);
    }
    if result.identity.schema != SCHEMA_VERSION {
        return Verdict::Reject(RejectReason::Schema);
    }

    if result.partition() != partition {
        return Verdict::Reject(RejectReason::WrongVehicle);
    }

    // A base-less result names no segment, so it is left to the observation checks below.
    if let (Some(cp), Some(base)) = (vehicle.checkpoint.present(), result.identity.base)
        && base.segment != cp.segment
    {
        return Verdict::Reject(RejectReason::SegmentClosed);
    }

    match &vehicle.active {
        Some(active) if active.id == result.job => {
            if vehicle.committing {
                Verdict::Quarantine(QuarantineReason::DuplicateAfterCommit)
            } else {
                Verdict::Accept { job: active }
            }
        }
        Some(active) => match result.identity.observation.cmp(&active.observation) {
            Ordering::Less => Verdict::Reject(older_reject(vehicle, active, result)),
            // Newer than the in-flight job: this owner dispatched it before a crash; park until replay re-creates it.
            Ordering::Greater => Verdict::Park,
            Ordering::Equal => Verdict::Reject(RejectReason::StaleBase {
                expected: active.identity.base,
                got: result.identity.base,
            }),
        },
        None => match vehicle.checkpoint.present() {
            Some(cp) => match result.identity.observation.cmp(&cp.last_input) {
                Ordering::Less => Verdict::Reject(RejectReason::Committed),
                Ordering::Equal => Verdict::Reject(RejectReason::Committed),
                Ordering::Greater => Verdict::Park,
            },
            None => Verdict::Park,
        },
    }
}

/// The reject reason for a result older than the vehicle's in-flight job:
/// [`RejectReason::Committed`] at or below the last committed input, else
/// [`RejectReason::StaleBase`].
fn older_reject<E, H>(
    vehicle: &VehicleState<E, H>,
    active: &ActiveJob,
    result: &SolveResult<E>,
) -> RejectReason
where
    E: Entry,
    H: AckHandle,
{
    if let Some(cp) = vehicle.checkpoint.present()
        && result.identity.observation <= cp.last_input
    {
        return RejectReason::Committed;
    }
    RejectReason::StaleBase {
        expected: active.identity.base,
        got: result.identity.base,
    }
}

/// Report the earliest layer a solve would rewrite: the smallest layer timestamp
/// in `diff` at or below the checkpoint's finality watermark, or `None` when the
/// checkpoint is absent, has no watermark, or the diff touches only unfinalized history.
#[must_use]
pub fn finality_conflict<E: Entry>(
    checkpoint: Option<&VehicleCheckpoint<E>>,
    diff: &MatchedDiff<E>,
) -> Option<i64> {
    let cutoff = checkpoint?.finalized_through?;
    diff.layers
        .iter()
        .map(|layer| layer.timestamp)
        .filter(|&ts| ts <= cutoff)
        .min()
}

#[cfg(test)]
mod tests {
    use alloc::collections::VecDeque;
    use core::time::Duration;

    use geo::Point;
    use routers_network::mock::MockEntryId;
    use routers_network::{DirectionAwareEdgeId, Edge};
    use routers_transition::matcher::{Continuation, Trip};
    use tokio::time::Instant;

    use super::*;
    use crate::event::{MatchedLayer, VehicleId};
    use crate::orchestrator::admission::{Admission, AdmissionConfig};
    use crate::orchestrator::scheduler::{CheckpointState, JobReservation};
    use crate::partition::partition_of;
    use crate::protocol::ids::{
        GraphVersion, JobId, Lane, ObservationId, RegionId, Revision, SCHEMA_VERSION, SegmentId,
    };
    use crate::protocol::job::{JobIdentity, SolveJob};
    use crate::protocol::result::{SolveOutcome, SolveResult};

    #[derive(Clone)]
    struct TestAck;

    impl AckHandle for TestAck {
        async fn ack(self) -> anyhow::Result<()> {
            Ok(())
        }
        async fn nak(self, _delay: Option<Duration>) -> anyhow::Result<()> {
            Ok(())
        }
        fn sequence(&self) -> u64 {
            0
        }
        fn deliveries(&self) -> u32 {
            1
        }
    }

    struct Fixture {
        admission: Admission,
        region: RegionId,
        base: Instant,
    }

    impl Fixture {
        fn new() -> Self {
            let region = RegionId::new("r1").unwrap();
            let admission = Admission::new(AdmissionConfig::default(), core::iter::once(&region));
            Self {
                admission,
                region,
                base: Instant::now(),
            }
        }

        fn obs(&self, seq: u64) -> ObservationId {
            ObservationId {
                partition: 7,
                sequence: seq,
            }
        }

        fn identity(&self, vehicle: u64, seq: u64, base: Option<BaseState>) -> JobIdentity {
            JobIdentity {
                schema: SCHEMA_VERSION,
                vehicle_id: VehicleId(vehicle),
                observation: self.obs(seq),
                base,
                graph: GraphVersion::new("g1").unwrap(),
                region: self.region.clone(),
            }
        }

        fn result(
            &self,
            vehicle: u64,
            seq: u64,
            base: Option<BaseState>,
        ) -> SolveResult<MockEntryId> {
            let job = self.solve_job(vehicle, seq, base);
            SolveResult::new(&job, SolveOutcome::Unanchored, 0)
        }

        fn solve_job(
            &self,
            vehicle: u64,
            seq: u64,
            base: Option<BaseState>,
        ) -> SolveJob<MockEntryId> {
            SolveJob::new(
                self.identity(vehicle, seq, base),
                Lane::DEFAULT,
                1_000,
                Continuation::Restart { fresh: Vec::new() },
            )
        }

        fn job(&self, vehicle: u64, seq: u64, base: Option<BaseState>) -> ActiveJob {
            let solve_job = self.solve_job(vehicle, seq, base);
            ActiveJob {
                id: solve_job.id,
                identity: solve_job.identity,
                observation: self.obs(seq),
                bytes: 100,
                reservation: JobReservation::Admitted(
                    self.admission.try_admit(&self.region, 100).unwrap(),
                ),
                dispatched: self.base,
            }
        }

        fn checkpoint(
            &self,
            last_seq: u64,
            segment: u64,
            finalized: Option<i64>,
        ) -> VehicleCheckpoint<MockEntryId> {
            VehicleCheckpoint {
                trip: Trip::new(),
                last_input: self.obs(last_seq),
                revision: Revision(last_seq),
                segment: SegmentId(segment),
                finalized_through: finalized,
                graph: GraphVersion::new("g1").unwrap(),
                schema: SCHEMA_VERSION,
                region: self.region.clone(),
                routing_version: 1,
            }
        }

        fn vehicle(
            &self,
            checkpoint: CheckpointState<MockEntryId>,
            active: Option<ActiveJob>,
            committing: bool,
        ) -> VehicleState<MockEntryId, TestAck> {
            VehicleState {
                checkpoint,
                pending: VecDeque::new(),
                active,
                committing,
                last_touch: self.base,
            }
        }
    }

    fn part(vehicle: u64) -> u16 {
        partition_of(VehicleId(vehicle)) as u16
    }

    fn base(revision: u64, segment: u64) -> BaseState {
        BaseState {
            revision: Revision(revision),
            segment: SegmentId(segment),
        }
    }

    #[test]
    fn accepts_the_answer_to_the_active_job() {
        let fx = Fixture::new();
        let job = fx.job(1, 10, None);
        let want_id = job.id;
        let vehicle = fx.vehicle(CheckpointState::Unloaded, Some(job), false);
        let result = fx.result(1, 10, None);

        match validate(&vehicle, &result, part(1)) {
            Verdict::Accept { job } => assert_eq!(job.id, want_id),
            other => panic!("expected Accept, got {other:?}"),
        }
    }

    #[test]
    fn accepts_a_resumed_job_when_segments_agree() {
        let fx = Fixture::new();
        let checkpoint = CheckpointState::Present(fx.checkpoint(9, 5, None));
        let job = fx.job(1, 10, Some(base(9, 5)));
        let vehicle = fx.vehicle(checkpoint, Some(job), false);
        let result = fx.result(1, 10, Some(base(9, 5)));

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Accept { .. }
        ));
    }

    #[test]
    fn quarantines_a_duplicate_once_a_commit_is_in_flight() {
        let fx = Fixture::new();
        let job = fx.job(1, 10, None);
        let vehicle = fx.vehicle(CheckpointState::Unloaded, Some(job), true);
        let result = fx.result(1, 10, None);

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Quarantine(QuarantineReason::DuplicateAfterCommit)
        ));
    }

    #[test]
    fn rejects_a_crossed_envelope_before_touching_state() {
        let fx = Fixture::new();
        let job = fx.solve_job(1, 10, None);
        // A forged id that the identity does not hash to.
        let mut forged = SolveResult::new(&job, SolveOutcome::Unanchored, 0);
        forged.job = JobId(0);
        let vehicle = fx.vehicle(CheckpointState::Unloaded, None, false);

        assert!(matches!(
            validate(&vehicle, &forged, part(1)),
            Verdict::Reject(RejectReason::IdMismatch)
        ));
    }

    #[test]
    fn rejects_a_foreign_schema() {
        let fx = Fixture::new();
        // Its id still hashes correctly, so validation reaches the schema gate.
        let mut identity = fx.identity(1, 10, None);
        identity.schema = crate::protocol::ids::SchemaVersion(SCHEMA_VERSION.0 + 1);
        let job = SolveJob::new(
            identity,
            Lane::DEFAULT,
            1_000,
            Continuation::Restart { fresh: Vec::new() },
        );
        let result = SolveResult::new(&job, SolveOutcome::Unanchored, 0);
        let vehicle = fx.vehicle(CheckpointState::Unloaded, None, false);

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Reject(RejectReason::Schema)
        ));
    }

    #[test]
    fn rejects_a_result_addressed_to_the_wrong_partition() {
        let fx = Fixture::new();
        let vehicle = fx.vehicle(CheckpointState::Unloaded, None, false);
        let result = fx.result(1, 10, None);
        let wrong = part(1) ^ 1;

        assert!(matches!(
            validate(&vehicle, &result, wrong),
            Verdict::Reject(RejectReason::WrongVehicle)
        ));
    }

    #[test]
    fn rejects_a_result_from_a_closed_segment() {
        let fx = Fixture::new();
        // The checkpoint is in segment 5; the result resumes from the closed segment 4.
        let checkpoint = CheckpointState::Present(fx.checkpoint(9, 5, None));
        let vehicle = fx.vehicle(checkpoint, None, false);
        let result = fx.result(1, 10, Some(base(9, 4)));

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Reject(RejectReason::SegmentClosed)
        ));
    }

    #[test]
    fn parks_an_early_result_with_no_active_job() {
        let fx = Fixture::new();
        // A newer observation whose job replay has not re-dispatched yet.
        let checkpoint = CheckpointState::Present(fx.checkpoint(9, 5, None));
        let vehicle = fx.vehicle(checkpoint, None, false);
        let result = fx.result(1, 20, Some(base(9, 5)));

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Park
        ));
    }

    #[test]
    fn parks_when_the_checkpoint_is_not_loaded_yet() {
        let fx = Fixture::new();
        let vehicle = fx.vehicle(CheckpointState::Unloaded, None, false);
        let result = fx.result(1, 20, None);

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Park
        ));
    }

    #[test]
    fn parks_a_future_result_while_an_older_job_is_in_flight() {
        let fx = Fixture::new();
        // A result newer than the in-flight job: dispatched before a crash, so park it.
        let job = fx.job(1, 10, None);
        let vehicle = fx.vehicle(CheckpointState::Unloaded, Some(job), false);
        let result = fx.result(1, 20, None);

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Park
        ));
    }

    #[test]
    fn rejects_a_committed_result_with_no_active_job() {
        let fx = Fixture::new();
        let checkpoint = CheckpointState::Present(fx.checkpoint(9, 5, None));
        let vehicle = fx.vehicle(checkpoint, None, false);
        let result = fx.result(1, 8, Some(base(9, 5)));

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Reject(RejectReason::Committed)
        ));
    }

    #[test]
    fn rejects_the_already_resolved_committed_head() {
        let fx = Fixture::new();
        // The exact committed observation with no job in flight: that job already resolved.
        let checkpoint = CheckpointState::Present(fx.checkpoint(9, 5, None));
        let vehicle = fx.vehicle(checkpoint, None, false);
        let result = fx.result(1, 9, Some(base(9, 5)));

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Reject(RejectReason::Committed)
        ));
    }

    #[test]
    fn rejects_a_stale_base_for_an_older_uncommitted_observation() {
        let fx = Fixture::new();
        // A result older than the in-flight job but still above the committed input: stale, not committed.
        let checkpoint = CheckpointState::Present(fx.checkpoint(5, 3, None));
        let job = fx.job(1, 10, Some(base(5, 3)));
        let vehicle = fx.vehicle(checkpoint, Some(job), false);
        let result = fx.result(1, 8, Some(base(5, 3)));

        match validate(&vehicle, &result, part(1)) {
            Verdict::Reject(RejectReason::StaleBase { expected, got }) => {
                assert_eq!(expected, Some(base(5, 3)));
                assert_eq!(got, Some(base(5, 3)));
            }
            other => panic!("expected StaleBase, got {other:?}"),
        }
    }

    #[test]
    fn rejects_committed_when_an_older_result_is_at_or_below_the_input() {
        let fx = Fixture::new();
        // A result at the committed input is committed even though a different job is in flight.
        let checkpoint = CheckpointState::Present(fx.checkpoint(8, 3, None));
        let job = fx.job(1, 10, Some(base(8, 3)));
        let vehicle = fx.vehicle(checkpoint, Some(job), false);
        let result = fx.result(1, 8, Some(base(8, 3)));

        assert!(matches!(
            validate(&vehicle, &result, part(1)),
            Verdict::Reject(RejectReason::Committed)
        ));
    }

    #[test]
    fn rejects_a_diverged_base_at_the_same_observation() {
        let fx = Fixture::new();
        // Same observation as the active job, but a different base makes it a different id.
        let job = fx.job(1, 10, Some(base(9, 5)));
        let vehicle = fx.vehicle(CheckpointState::Unloaded, Some(job), false);
        let result = fx.result(1, 10, Some(base(9, 6)));

        match validate(&vehicle, &result, part(1)) {
            Verdict::Reject(RejectReason::StaleBase { expected, got }) => {
                assert_eq!(expected, Some(base(9, 5)));
                assert_eq!(got, Some(base(9, 6)));
            }
            other => panic!("expected StaleBase, got {other:?}"),
        }
    }

    #[test]
    fn labels_are_distinct_and_stable() {
        let rejects = [
            (RejectReason::Committed, "committed"),
            (
                RejectReason::StaleBase {
                    expected: None,
                    got: None,
                },
                "stale_base",
            ),
            (RejectReason::SegmentClosed, "segment_closed"),
            (RejectReason::WrongVehicle, "wrong_vehicle"),
            (RejectReason::Schema, "schema"),
            (RejectReason::IdMismatch, "id_mismatch"),
        ];
        let mut seen = std::collections::HashSet::new();
        for (reason, label) in rejects {
            assert_eq!(reason.label(), label);
            assert!(seen.insert(label), "duplicate reject label {label}");
        }
        assert_eq!(seen.len(), 6);

        for (reason, label) in [
            (QuarantineReason::ConflictingContent, "conflicting_content"),
            (
                QuarantineReason::DuplicateAfterCommit,
                "duplicate_after_commit",
            ),
        ] {
            assert_eq!(reason.label(), label);
        }

        assert_eq!(Verdict::Park.label(), "park");
        assert_eq!(Verdict::Reject(RejectReason::Schema).label(), "reject");
        assert_eq!(
            Verdict::Quarantine(QuarantineReason::ConflictingContent).label(),
            "quarantine"
        );
    }

    fn layer(timestamp: i64) -> MatchedLayer<MockEntryId> {
        MatchedLayer {
            timestamp,
            edge: Edge {
                source: MockEntryId(0),
                target: MockEntryId(0),
                weight: 0,
                id: DirectionAwareEdgeId::new(MockEntryId(0)),
            },
            position: Point::new(0.0, 0.0),
            path: Vec::new(),
        }
    }

    fn diff(timestamps: &[i64]) -> MatchedDiff<MockEntryId> {
        MatchedDiff {
            revision: 1,
            downgraded: false,
            layers: timestamps.iter().copied().map(layer).collect(),
        }
    }

    #[test]
    fn finality_conflict_finds_the_earliest_offender() {
        let fx = Fixture::new();
        let checkpoint = fx.checkpoint(9, 5, Some(100));
        let d = diff(&[80, 200, 40, 300]);
        assert_eq!(finality_conflict(Some(&checkpoint), &d), Some(40));
    }

    #[test]
    fn finality_conflict_none_when_all_layers_are_unfinalized() {
        let fx = Fixture::new();
        let checkpoint = fx.checkpoint(9, 5, Some(100));
        let d = diff(&[101, 200, 300]);
        assert_eq!(finality_conflict(Some(&checkpoint), &d), None);
    }

    #[test]
    fn finality_conflict_none_without_a_watermark_or_checkpoint() {
        let fx = Fixture::new();
        let d = diff(&[1, 2, 3]);
        let checkpoint = fx.checkpoint(9, 5, None);
        assert_eq!(finality_conflict(Some(&checkpoint), &d), None);
        assert_eq!(finality_conflict(None, &d), None);
    }
}