rings-node 0.39.0

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
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
//! Onion loops: the closed hop sequence a pipeline is evaluated on (#834 D4c, D5, L7).
//!
//! A pipeline `P = (f₁, ā₁) ⋙ … ⋙ (fₙ, āₙ)` is evaluated on the client-sealed loop
//!
//! ```text
//! LoopOf(P):  cl → r₀,₁ … r₀,ₛ → h₁ → r₁,₁ … r₁,ₛ → ⋯ → hₙ → rₙ,₁ … rₙ,ₛ → cl
//! ```
//!
//! of `n + 1` segments of `s = ONION_SEGMENT_RELAYS` relays, closed by the client's entry guard
//! `g = r₀,₁ = rₙ,ₛ`: the guard is relay `1` of the first segment and relay `s` of the last, and
//! the symbol hop `hₖ` stands at position `(s + 1)·k` of `[1, H]`.
//!
//! Laws, for every shape `n ∈ [1, n_max]`:
//!
//! - **Size.** `H(n, s) = (s + 1)·n + s` positions and `H − 1` distinct hops, bounded by
//!   `MAX_ONION_LOOP_HOPS = H(n_max, s) = 14` with `n_max = MAX_ONION_LOOP_SYMBOLS = 4`.
//! - **Segment** (D5). Exactly `s` relay positions, the guard counted, precede `h₁`, separate
//!   `hₖ` from `hₖ₊₁` and follow `hₙ`; the only constructor emits no other segment length.
//! - **Guard closure** (L7). An [`OnionLoop`] stores the guard once and places it at positions `1`
//!   and `H` and nowhere else, so the guard is the only hop adjacent to the client. The remaining
//!   `H − 2` positions are pairwise distinct and distinct from `g` for every loop a route draws
//!   (`select_loop` in the route module).
//!
//! The only constructor is [`OnionLoop::try_unfold`], an unfold of the non-empty, already
//! labelled pipeline `f₁ … fₙ₋₁ ⋙ fₙ` in the failure monad: it places each symbol and labels the
//! relay positions around them,
//!
//! ```text
//! unfold [f₁ … fₙ] = Guard · Relay^{s−1} · Π_{k=1..n} (Symbol fₖ · Relay^{s − [k = n]})
//! ```
//!
//! and closes the loop with the guard's label. Relay positions are labelled `P` and symbol
//! positions `T`; the terminal `hₙ` is a field, not an index, so a caller labelling it with the
//! drawn registrant reads it back totally. The unfold fixes the position order only: route
//! selection draws the symbols first, then the guard, then the relays (#834 L7).

use std::iter;

use super::OnionRouteError;
use crate::error::Error;
use crate::error::Result;

/// Number `s` of relays in every segment of a loop, the guard counted in both end segments (D5).
///
/// `s = 2` is the least value at which no single node is adjacent to both the client and a symbol
/// hop: at `s = 1` the guard alone would neighbour the client, `h₁` and `hₙ`.
pub const ONION_SEGMENT_RELAYS: usize = 2;

/// Largest number `n_max` of symbol applications in one pipeline (#834 D4a).
pub const MAX_ONION_LOOP_SYMBOLS: usize = 4;

/// Largest number of positions of one loop, `H(n_max, s) = (s + 1)·n_max + s = 14`, derived from
/// [`ONION_SEGMENT_RELAYS`] and [`MAX_ONION_LOOP_SYMBOLS`].
pub const MAX_ONION_LOOP_HOPS: usize = loop_hops(MAX_ONION_LOOP_SYMBOLS);

/// Period `s + 1` of the loop: each symbol hop `hₖ` together with the `s` relay positions before
/// it, so `hₖ` stands at position `(s + 1)·k`.
const SYMBOL_PERIOD: usize = ONION_SEGMENT_RELAYS + 1;

/// Number of positions `H(n, s) = n + (n + 1)·s = (s + 1)·n + s` of a loop over `n` symbols.
const fn loop_hops(symbols: usize) -> usize {
    SYMBOL_PERIOD * symbols + ONION_SEGMENT_RELAYS
}

/// The shape of a loop over `n ∈ [1, n_max]` symbol applications.
///
/// Invariant: `1 ≤ symbols ≤ MAX_ONION_LOOP_SYMBOLS`, so `hop_count() ≤ MAX_ONION_LOOP_HOPS`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OnionLoopShape {
    symbols: usize,
}

impl OnionLoopShape {
    /// Shape of a pipeline ending in a world-facing session symbol, which stands alone (`n = 1`,
    /// #834 D4a): `g, r₀,₂, h₁, r₁,₁, g`.
    pub const SESSION: Self = Self { symbols: 1 };

    /// Build the shape of a pipeline of `symbols` applications, rejecting `n = 0` and `n > n_max`.
    pub fn new(symbols: usize) -> Result<Self> {
        if symbols == 0 || symbols > MAX_ONION_LOOP_SYMBOLS {
            return Err(Error::OnionRouteError(
                OnionRouteError::LoopSymbolsOutOfBounds {
                    symbols,
                    max_symbols: MAX_ONION_LOOP_SYMBOLS,
                },
            ));
        }
        Ok(Self { symbols })
    }

    /// Return the number `n` of symbol applications.
    pub const fn symbols(self) -> usize {
        self.symbols
    }

    /// Return the number of positions `H(n, s) = (s + 1)·n + s`.
    pub const fn hop_count(self) -> usize {
        loop_hops(self.symbols)
    }

    /// Return the number `H − 1` of pairwise distinct hops: every position once, the guard once.
    pub const fn distinct_hops(self) -> usize {
        self.hop_count() - 1
    }
}

/// A non-empty pipeline of symbols `f₁ … fₙ` (#834 D4a): every constructor proves `n ≥ 1`, so
/// the terminal symbol `fₙ` is total.
#[derive(Debug)]
pub(crate) struct OnionPipelineSymbols<'s, S> {
    all: &'s [S],
    intermediate: &'s [S],
    terminal: &'s S,
}

impl<'s, S> OnionPipelineSymbols<'s, S> {
    /// The pipeline of one symbol.
    pub(crate) const fn single(symbol: &'s S) -> Self {
        Self {
            all: std::slice::from_ref(symbol),
            intermediate: &[],
            terminal: symbol,
        }
    }

    /// The pipeline `symbols`, or `None` when it is empty. Multi-symbol pipelines are drawn only in
    /// tests until Phase 2b registers intermediate symbols.
    #[cfg(test)]
    pub(crate) fn new(symbols: &'s [S]) -> Option<Self> {
        symbols.split_last().map(|(terminal, intermediate)| Self {
            all: symbols,
            intermediate,
            terminal,
        })
    }

    /// Return the number `n ≥ 1` of symbols.
    pub(crate) const fn symbol_count(&self) -> usize {
        self.all.len()
    }

    /// Return the symbols in pipeline order.
    pub(crate) const fn as_slice(&self) -> &'s [S] {
        self.all
    }

    /// Label every symbol in pipeline order, handing each the symbols after it, and stop at the
    /// first failure: `fₖ ↦ label(fₖ, [fₖ₊₁ … fₙ])`, split into the intermediate labels and the
    /// terminal's.
    pub(crate) fn try_map_with_later<T>(
        &self,
        mut label: impl FnMut(&'s S, &'s [S]) -> Result<T>,
    ) -> Result<(Vec<T>, T)> {
        let laters = iter::successors(self.all.split_first().map(|(_, later)| later), |later| {
            later.split_first().map(|(_, rest)| rest)
        });
        let intermediate = self
            .intermediate
            .iter()
            .zip(laters)
            .map(|(symbol, later)| label(symbol, later))
            .collect::<Result<Vec<_>>>()?;
        Ok((intermediate, label(self.terminal, &[])?))
    }
}

/// The kind of a relay position of the open path: the entry guard at position `1`, or another
/// relay. Both evaluate `relay`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum OnionLoopRelay {
    /// Position `1`, the entry guard `g`.
    Guard,
    /// A relay position other than the guard.
    Relay,
}

/// The role of one position of a loop: a relay position (the guard included), or the symbol hop
/// `hₖ` with its index `k ∈ [1, n]`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum OnionLoopRole {
    /// A position evaluating `relay`.
    Relay,
    /// The symbol hop `hₖ`.
    Symbol(usize),
}

/// A loop whose relay positions carry a label `P` and whose symbol positions carry a label `T`,
/// the guard's label stored once.
///
/// The fields follow the unfold grammar of the module documentation: `lead` holds the relays
/// after the guard, `stages` each intermediate symbol with the relays after it, `terminal` the
/// last symbol `hₙ`, and `tail` the relays before the closing guard. The guard and the terminal
/// are total, and the shape is derived from `stages`. The segment lengths `s − 1`, `s`, `s − 1`
/// are established by `OnionLoop::try_unfold`, the only constructor.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OnionLoop<P, T = P> {
    guard: P,
    lead: Vec<P>,
    stages: Vec<(T, Vec<P>)>,
    terminal: T,
    tail: Vec<P>,
}

impl<P, T> OnionLoop<P, T> {
    /// Unfold the loop of the labelled symbols `intermediate ⋙ terminal`, placing each symbol at
    /// its position and labelling every relay position of the open path `1 … H − 1` in order,
    /// stopping at the first failure.
    ///
    /// The guard is labelled exactly once, and position `H` repeats its label (L7). A pipeline
    /// of more than `n_max` symbols is rejected before any relay is labelled.
    pub(crate) fn try_unfold(
        intermediate: Vec<T>,
        terminal: T,
        mut label_relay: impl FnMut(OnionLoopRelay) -> Result<P>,
    ) -> Result<Self> {
        OnionLoopShape::new(intermediate.len() + 1)?;
        let guard = label_relay(OnionLoopRelay::Guard)?;
        let lead = (1..ONION_SEGMENT_RELAYS)
            .map(|_| label_relay(OnionLoopRelay::Relay))
            .collect::<Result<Vec<_>>>()?;
        let stages = intermediate
            .into_iter()
            .map(|symbol| {
                let relays = (0..ONION_SEGMENT_RELAYS)
                    .map(|_| label_relay(OnionLoopRelay::Relay))
                    .collect::<Result<Vec<_>>>()?;
                Ok((symbol, relays))
            })
            .collect::<Result<Vec<_>>>()?;
        let tail = (1..ONION_SEGMENT_RELAYS)
            .map(|_| label_relay(OnionLoopRelay::Relay))
            .collect::<Result<Vec<_>>>()?;
        Ok(Self {
            guard,
            lead,
            stages,
            terminal,
            tail,
        })
    }

    /// Return the shape of this loop: `n = |stages| + 1` symbols, bounded by construction.
    pub fn shape(&self) -> OnionLoopShape {
        OnionLoopShape {
            symbols: self.stages.len() + 1,
        }
    }

    /// Return the guard's label, the label of positions `1` and `H`.
    pub const fn guard(&self) -> &P {
        &self.guard
    }

    /// Return the label of the terminal symbol hop `hₙ`.
    pub const fn terminal(&self) -> &T {
        &self.terminal
    }

    /// Return the labels of the relay positions before `h₁`: the guard, then `r₀,₂ … r₀,ₛ`.
    pub fn forward_prefix(&self) -> impl Iterator<Item = &P> {
        iter::once(&self.guard).chain(self.lead.iter())
    }

    /// Relabel every symbol position by `project` and return the terminal's label beside the
    /// relabelled loop: `OnionLoop<P, T> → OnionLoop<P> × T`.
    pub(crate) fn project_symbols(self, project: impl Fn(&T) -> P) -> (OnionLoop<P>, T) {
        let stages = self
            .stages
            .into_iter()
            .map(|(symbol, relays)| (project(&symbol), relays))
            .collect();
        (
            OnionLoop {
                guard: self.guard,
                lead: self.lead,
                stages,
                terminal: project(&self.terminal),
                tail: self.tail,
            },
            self.terminal,
        )
    }
}

impl<P> OnionLoop<P> {
    /// Return the labels of the open path `1 … H − 1`, the guard once: the `H − 1` hops a route
    /// requires pairwise distinct.
    pub fn open_path(&self) -> impl Iterator<Item = &P> {
        self.forward_prefix()
            .chain(
                self.stages
                    .iter()
                    .flat_map(|(symbol, relays)| iter::once(symbol).chain(relays.iter())),
            )
            .chain(iter::once(&self.terminal))
            .chain(self.tail.iter())
    }

    /// Return the labels of all `H` positions in order, the guard at `1` and `H`.
    pub fn positions(&self) -> impl Iterator<Item = &P> {
        self.open_path().chain(iter::once(&self.guard))
    }

    /// Return every position `1 … H` in order with its role: the relay positions, the guard at
    /// `1` and `H` included, and each symbol hop `hₖ` with its index `k` (`1 ≤ k ≤ n`).
    ///
    /// Law: the projection to labels is [`Self::positions`], and the roles read
    /// `Relay^s · Π_{k=1..n} (Symbol k · Relay^s)`, the unfold grammar with the guard counted in
    /// both end segments.
    pub fn roles(&self) -> impl Iterator<Item = (OnionLoopRole, &P)> {
        let symbols = self.shape().symbols();
        iter::once(&self.guard)
            .chain(self.lead.iter())
            .map(|relay| (OnionLoopRole::Relay, relay))
            .chain(
                self.stages
                    .iter()
                    .zip(1..)
                    .flat_map(|((symbol, relays), k)| {
                        iter::once((OnionLoopRole::Symbol(k), symbol))
                            .chain(relays.iter().map(|relay| (OnionLoopRole::Relay, relay)))
                    }),
            )
            .chain(iter::once((OnionLoopRole::Symbol(symbols), &self.terminal)))
            .chain(
                self.tail
                    .iter()
                    .chain(iter::once(&self.guard))
                    .map(|relay| (OnionLoopRole::Relay, relay)),
            )
    }

    /// Return the return path: the positions after the terminal `hₙ`, `rₙ,₁ … rₙ,ₛ = g`, which a
    /// reply block of `hₙ` travels.
    pub fn return_path(&self) -> impl Iterator<Item = &P> {
        self.tail.iter().chain(iter::once(&self.guard))
    }

    /// Return the label of the symbol hop `hₖ`, `1 ≤ k ≤ n`, or `None` for any other `k`.
    pub fn symbol(&self, k: usize) -> Option<&P> {
        if k == self.shape().symbols() {
            return Some(&self.terminal);
        }
        self.stages.get(k.checked_sub(1)?).map(|(symbol, _)| symbol)
    }
}

#[cfg(test)]
mod tests {
    use super::OnionLoop;
    use super::OnionLoopRelay;
    use super::OnionLoopRole;
    use super::OnionLoopShape;
    use super::OnionPipelineSymbols;
    use super::MAX_ONION_LOOP_HOPS;
    use super::MAX_ONION_LOOP_SYMBOLS;
    use super::ONION_SEGMENT_RELAYS;
    use crate::error::Error;
    use crate::error::Result;
    use crate::onion::OnionRouteError;

    /// The kind of one position, read off the unfolded loop: the guard, a relay, or the symbol
    /// `k` of the pipeline `[1 … n]`.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    enum Kind {
        Guard,
        Relay,
        Symbol(usize),
    }

    /// Unfold the pipeline `[1 … n]` with each position labelled by its kind, and count the relay
    /// labels asked for.
    fn unfold_kinds(symbols: usize) -> Result<(OnionLoop<Kind>, usize)> {
        let mut asked = 0;
        let unfolded = OnionLoop::try_unfold(
            (1..symbols).map(Kind::Symbol).collect(),
            Kind::Symbol(symbols),
            |relay| {
                asked += 1;
                Ok(match relay {
                    OnionLoopRelay::Guard => Kind::Guard,
                    OnionLoopRelay::Relay => Kind::Relay,
                })
            },
        )?;
        Ok((unfolded, asked))
    }

    /// Size (D5): `H = (s + 1)·n + s` for every admitted `n`, `H − 1` distinct hops asked for once
    /// each, and the derived bound `MAX_ONION_LOOP_HOPS = 14`.
    #[test]
    fn test_loop_size_is_derived_from_segments() -> Result<()> {
        assert_eq!(ONION_SEGMENT_RELAYS, 2);
        assert_eq!(MAX_ONION_LOOP_SYMBOLS, 4);
        assert_eq!(MAX_ONION_LOOP_HOPS, 14);
        for (symbols, hops) in [(1, 5), (2, 8), (3, 11), (4, 14)] {
            let shape = OnionLoopShape::new(symbols)?;
            let (unfolded, asked) = unfold_kinds(symbols)?;
            assert_eq!(shape.hop_count(), hops);
            assert_eq!(
                shape.hop_count(),
                symbols + (symbols + 1) * ONION_SEGMENT_RELAYS
            );
            assert_eq!(shape.distinct_hops(), hops - 1);
            assert!(shape.hop_count() <= MAX_ONION_LOOP_HOPS);
            assert_eq!(unfolded.shape(), shape);
            assert_eq!(unfolded.positions().count(), hops);
            assert_eq!(unfolded.open_path().count(), hops - 1);
            assert_eq!(asked + symbols, hops - 1);
        }
        assert_eq!(OnionLoopShape::SESSION, OnionLoopShape::new(1)?);
        Ok(())
    }

    /// `n = 0` and `n > n_max` are rejected, the latter before any label is asked.
    #[test]
    fn test_loop_symbols_are_bounded() {
        let mut asked = 0;
        let unfolded =
            OnionLoop::<(), ()>::try_unfold(vec![(); MAX_ONION_LOOP_SYMBOLS], (), |_| {
                asked += 1;
                Ok(())
            });

        assert!(matches!(
            unfolded,
            Err(Error::OnionRouteError(
                OnionRouteError::LoopSymbolsOutOfBounds {
                    symbols: 5,
                    max_symbols: MAX_ONION_LOOP_SYMBOLS,
                }
            ))
        ));
        assert_eq!(asked, 0);
        for symbols in [0, usize::MAX] {
            assert!(OnionLoopShape::new(symbols).is_err());
        }
    }

    /// Segment (D5) and guard closure (L7): the guard is at `1` and `H` only, the `k`-th symbol at
    /// `(s + 1)·k`, exactly `s` relay positions, the guard counted, lie before `h₁`, between
    /// consecutive symbols and after `hₙ`.
    #[test]
    fn test_unfold_interleaves_segments_of_s_relays() -> Result<()> {
        for symbols in 1..=MAX_ONION_LOOP_SYMBOLS {
            let (unfolded, _) = unfold_kinds(symbols)?;
            let kinds = unfolded.positions().copied().collect::<Vec<_>>();
            let guards = (1..=kinds.len())
                .filter(|position| kinds.get(position - 1) == Some(&Kind::Guard))
                .collect::<Vec<_>>();
            let segments = kinds
                .split(|kind| matches!(kind, Kind::Symbol(_)))
                .map(<[Kind]>::len)
                .collect::<Vec<_>>();

            assert_eq!(guards, vec![1, kinds.len()]);
            for k in 1..=symbols {
                assert_eq!(
                    kinds.get((ONION_SEGMENT_RELAYS + 1) * k - 1),
                    Some(&Kind::Symbol(k))
                );
                assert_eq!(
                    unfolded.symbol(k),
                    kinds.get((ONION_SEGMENT_RELAYS + 1) * k - 1)
                );
            }
            assert_eq!(segments, vec![ONION_SEGMENT_RELAYS; symbols + 1]);
            assert_eq!(unfolded.guard(), &Kind::Guard);
            assert_eq!(unfolded.terminal(), &Kind::Symbol(symbols));
            assert_eq!(unfolded.symbol(0), None);
            assert_eq!(unfolded.symbol(symbols + 1), None);
            assert_eq!(
                unfolded.forward_prefix().copied().collect::<Vec<_>>(),
                kinds
                    .get(..ONION_SEGMENT_RELAYS)
                    .map(<[Kind]>::to_vec)
                    .unwrap_or_default()
            );
        }
        Ok(())
    }

    /// `roles` labels exactly the positions `positions` lists, the `k`-th symbol at `(s + 1)·k`
    /// and every other position a relay, and the return path is the last `s` positions.
    #[test]
    fn test_roles_label_every_position_by_the_unfold_grammar() -> Result<()> {
        for symbols in 1..=MAX_ONION_LOOP_SYMBOLS {
            let (unfolded, _) = unfold_kinds(symbols)?;
            let roles = unfolded.roles().collect::<Vec<_>>();

            assert_eq!(
                roles.iter().map(|(_, kind)| **kind).collect::<Vec<_>>(),
                unfolded.positions().copied().collect::<Vec<_>>()
            );
            for (index, (role, kind)) in roles.iter().enumerate() {
                match role {
                    OnionLoopRole::Symbol(k) => {
                        assert_eq!(**kind, Kind::Symbol(*k));
                        assert_eq!(index + 1, (ONION_SEGMENT_RELAYS + 1) * k);
                    }
                    OnionLoopRole::Relay => assert!(!matches!(kind, Kind::Symbol(_))),
                }
            }
            let tail = roles
                .get(roles.len() - ONION_SEGMENT_RELAYS..)
                .map(|tail| tail.iter().map(|(_, kind)| **kind).collect::<Vec<_>>());
            assert_eq!(
                tail,
                Some(unfolded.return_path().copied().collect::<Vec<_>>())
            );
        }
        Ok(())
    }

    /// Projecting the symbol labels keeps every position and hands back the terminal's label.
    #[test]
    fn test_project_symbols_returns_the_terminal_label() -> Result<()> {
        let unfolded = OnionLoop::<u64, u64>::try_unfold(vec![1000], 2000, |relay| {
            Ok(match relay {
                OnionLoopRelay::Guard => 1,
                OnionLoopRelay::Relay => 2,
            })
        })?;

        let (projected, terminal) = unfolded.project_symbols(|label| label / 100 + 5);

        assert_eq!(terminal, 2000);
        assert_eq!(projected.positions().copied().collect::<Vec<_>>(), vec![
            1, 2, 15, 2, 2, 25, 2, 1
        ]);
        Ok(())
    }

    /// Unfolding stops at the first failure.
    #[test]
    fn test_unfold_propagates_the_first_failure() {
        let mut asked = 0;
        let unfolded = OnionLoop::<(), ()>::try_unfold(Vec::new(), (), |_| {
            asked += 1;
            if asked > ONION_SEGMENT_RELAYS {
                Err(Error::InvalidData)
            } else {
                Ok(())
            }
        });

        assert!(matches!(unfolded, Err(Error::InvalidData)));
        assert_eq!(asked, ONION_SEGMENT_RELAYS + 1);
    }

    /// Each symbol is labelled with the symbols after it, in pipeline order, the terminal with
    /// none.
    #[test]
    fn test_symbols_are_labelled_with_the_symbols_after_them() -> Result<()> {
        let pipeline = [1, 2, 3];
        let symbols = OnionPipelineSymbols::new(&pipeline).ok_or(Error::InvalidData)?;

        let labelled = symbols.try_map_with_later(|symbol, later| Ok((*symbol, later.to_vec())))?;

        assert_eq!(
            labelled,
            (vec![(1, vec![2, 3]), (2, vec![3])], (3, Vec::new()))
        );
        assert_eq!(symbols.symbol_count(), 3);
        assert!(OnionPipelineSymbols::<u8>::new(&[]).is_none());
        assert_eq!(OnionPipelineSymbols::single(&7).symbol_count(), 1);
        Ok(())
    }
}