rings-node 1.0.0-pre.5

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
//! The data plane's shell: it owns the admission state, applies the pure hop step to it, and
//! performs the outcome (#834 L9, #843 item 3).
//!
//! ```text
//! Cell(from, cell): now ← clock
//!                   now < clock_A − X₀ ?  ⇒ renew(fresh e′, key, A's live links); the epoch
//!                                            cell ← e′, published at the next heartbeat
//!                   outcome ← hop(A, d, relays, T.expects(from), from, now, cell)     (pure)
//!                     Relayed(next, c)         ⇒ link sender ← (next, c)       (next is live)
//!                     Consumed(f, ā, v, υ)     ⇒ ⟦f⟧(from, ā, v, υ) in the algebra
//!                     Returned(t_⋄, c)         ⇒ T.deliver(t_⋄, c)
//!                     Refused | Dropped        ⇒ nothing (the cell was paid for, or not decrypted)
//! Link(Opened(p))   p live ⇒ nothing;  else A.link_opened((p, fresh g)): a full table leaves it
//!                   not live, else the emitter opens p's lane
//! Link(Closed(p))   A.link_closed(p's live link);  the emitter closes p's lane;  fate(p)
//! Link(Dropped(p))  fate(p): a message lost from p's live link may be theirs (#966, #969)
//! Link(Reconcile)   L ← core's admitted peers, read now;  A.reconcile(L);  lanes ← A's live peers
//! fate(p)           T.lose(p);  ⟦−⟧.lose(p)
//! ```
//!
//! Every effect runs under the protocol's transition gate, so cells and link facts reach the
//! admission state in one linear order. Core's facts arrive in its order and are never dropped
//! (`extension::dispatch`); the one `Reconcile`, injected at install, reads core's admitted
//! peers inside its effect, at its place in that order: the linearisation obligation of
//! admission (#844). A renewal keeps the table's own live links, which are linearised by
//! construction, and draws the new epoch independently and uniformly, retrying on the negligible
//! collision with the current one. Lanes change only after the table, and only toward the links
//! the table holds live.
//!
//! Law (Circuit fate, #963, #966, #969). Relays keep no session state, so a link fact reaches
//! only the sessions of this node's own endpoints that ride the link: a client session with a
//! loop out through `p` and back (`T.lose`, `tags`), and an exit session whose loops arrive from
//! `p` or whose replies leave through `p` (`⟦−⟧.lose`, `session::serve`). On `Closed(p)`, and on
//! `Dropped(p)`, where a lost cell may have been one of theirs, each fails closed at this node
//! and sends `abort` toward its other endpoint, which therefore observes the failure too, at the
//! latest at its next loop. A cell lost at a relay is observed by both endpoints as a sequence
//! gap, which fails the session closed the same way (`session`). A session with no reply in
//! flight sees no gap; its liveness rule observes the death of a link that is neither endpoint's
//! own instead: `h` echoes the client's keep-alive, the client fails a session silent for
//! `Δ = 2V` and aborts it toward `h`, and `h` drops a session without a forward loop for `V`
//! (`session`, D2′ Liveness, #975).

use std::collections::BTreeMap;
use std::num::NonZeroUsize;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;

use bytes::Bytes;
use rings_core::dht::Did;
use rings_core::message::SlotRoster;
use rings_core::utils::SharedClock;
use rings_core::utils::SharedRng;
use rings_core::utils::SystemClock;
use rings_runtime::MaybeSendSync;

use super::admission::OnionAdmissionLink;
use super::admission::OnionAdmissionState;
use super::admission::OnionReplayFilterKey;
use super::codec::OnionLinkFact;
use super::hop::hop;
use super::hop::OnionHopOutcome;
use super::invoke::OnionInvokeRegistry;
use super::OnionCircuitInput;
use super::OnionClientTags;
use super::OnionLink;
use super::OnionLinkSender;
use crate::error::Error;
use crate::error::Result;
use crate::extension::ext::EffectScope;
use crate::extension::ext::Interpret;
use crate::extension::ext::Scope;
use crate::onion::signature::OnionShape;
use crate::onion::sphinx::carry::OnionCarryValue;
use crate::onion::sphinx::cell::OnionStep;
use crate::onion::sphinx::cell::OnionSurb;
use crate::onion::sphinx::layer::OnionArguments;
use crate::onion::OnionProcessEpoch;
use crate::onion::OnionProcessEpochCell;
use crate::onion::OnionServiceName;
use crate::sync_lock::lock;

/// The input of one symbol's interpretation: what `Hop_i` consumed at this hop.
pub(crate) struct OnionApplicationInput {
    /// The authenticated previous hop.
    pub(crate) from: Did,
    /// `ā`.
    pub(crate) arguments: OnionArguments,
    /// `v`.
    pub(crate) value: OnionCarryValue,
    /// `υ`, the reply block of the output.
    pub(crate) surb: Box<OnionSurb>,
    /// When the cell arrived.
    pub(crate) received_at_ms: u128,
}

/// Interpretation `⟦f⟧` of one world-facing symbol `f` at this hop.
///
/// `⟦f⟧(ā) : In_f → M Out_f` is a Kleisli arrow of the hop effect monad `M` (#834 D2): here the
/// input is the consumed value with its reply block, and the effects of `M` (sockets, fetch,
/// reply cells) run in the interpretation's own tasks. `evaluate` runs under the data plane's
/// transition gate, so it hands its input on and returns at once.
#[cfg_attr(rings_browser, async_trait::async_trait(?Send))]
#[cfg_attr(rings_native, async_trait::async_trait)]
pub(crate) trait OnionInterpretation: MaybeSendSync {
    /// Evaluate one application of the symbol.
    async fn evaluate(&self, scope: &Scope, input: OnionApplicationInput) -> Result<()>;

    /// The link to `peer` retired, or a message from it was dropped here: fail closed every
    /// session of this symbol that rides it (`Law (Circuit fate)` of the module). Required, so
    /// no interpretation can leave such a session waiting; it must not block.
    fn lose(&self, peer: Did);
}

/// The partial Σ-algebra of one node: `⟦−⟧ : Reg_n ⇀ Kl(M)` (#834 D2).
///
/// Maps each registered world-facing symbol `f ∈ Σ_W` to its session interpretation, and
/// delegates intermediate `(shape, backend)` pairs in `Σ_I × Ω` to [`OnionInvokeRegistry`].
#[derive(Default)]
pub(crate) struct OnionAlgebra {
    interpretations:
        BTreeMap<OnionServiceName, Box<rings_runtime::maybe_send_sync!(dyn OnionInterpretation)>>,
    invokes: OnionInvokeRegistry,
}

impl OnionAlgebra {
    /// Attach the shared intermediate `invoke` registry to this algebra.
    pub(crate) fn with_invokes(mut self, invokes: OnionInvokeRegistry) -> Self {
        self.invokes = invokes;
        self
    }

    /// `⟦−⟧.lose(peer)`: every world-facing interpretation fails closed its sessions that ride
    /// the link to `peer` (intermediate `invoke` symbols are stateless one-shot evaluations).
    fn lose(&self, peer: Did) {
        self.interpretations
            .values()
            .for_each(|interpretation| interpretation.lose(peer));
    }

    /// Register `interpretation` as `⟦symbol⟧` for a world-facing symbol.
    pub(crate) fn register(
        mut self,
        symbol: OnionServiceName,
        interpretation: impl OnionInterpretation + 'static,
    ) -> Self {
        self.interpretations
            .insert(symbol, Box::new(interpretation));
        self
    }

    /// Return `Σ_W,n`, the world-facing symbols this algebra interprets, in name order.
    #[cfg(all(test, rings_native))]
    pub(crate) fn symbols(&self) -> impl Iterator<Item = &OnionServiceName> {
        self.interpretations.keys()
    }

    /// Evaluate one application of `symbol` through its interpretation.
    async fn evaluate(
        &self,
        scope: &Scope,
        symbol: &OnionShape,
        input: OnionApplicationInput,
    ) -> Result<()> {
        match symbol {
            OnionShape::WorldFacing(service) => match self.interpretations.get(service) {
                Some(interpretation) => interpretation.evaluate(scope, input).await,
                None => {
                    tracing::debug!(
                        symbol = service.as_str(),
                        "drop an onion application of an unregistered symbol"
                    );
                    Ok(())
                }
            },
            OnionShape::Invoke(shape) => self.invokes.evaluate(scope, *shape, input),
        }
    }
}

/// The interpreter of the data plane's effects; see the module documentation.
pub(crate) struct OnionCircuitShell {
    /// Whether this node registers `relay`.
    relays: bool,
    /// The admission state `A`: ledgers, the link table and the replay store.
    admission: Arc<Mutex<OnionAdmissionState>>,
    /// `e_n`, published by the registrations and renewed with `A`.
    epoch: OnionProcessEpochCell,
    /// The client's tag table `T`.
    tags: OnionClientTags,
    /// The node's constant-rate link emitter.
    link_sender: OnionLinkSender,
    /// `⟦−⟧`.
    algebra: OnionAlgebra,
    /// The generation of the next admission the table opens (`OnionAdmissionLink`).
    generations: AtomicU64,
    /// Injected epoch clock capability.
    clock: SharedClock,
    /// Injected cryptographic RNG capability.
    rng: SharedRng,
    /// The waiters of [`OnionLinkWitness::live`], woken after every link fact.
    #[cfg(all(test, rings_native))]
    link_waiters: OnionLinkWaiters,
}

/// The waiters of a link-table change.
#[cfg(all(test, rings_native))]
type OnionLinkWaiters = Arc<Mutex<Vec<futures::channel::oneshot::Sender<()>>>>;

/// A test's view of a shell's link table, for waiting on the event that a link is live instead
/// of on a duration.
#[cfg(all(test, rings_native))]
#[derive(Clone)]
pub(crate) struct OnionLinkWitness {
    /// The shell's admission state.
    admission: Arc<Mutex<OnionAdmissionState>>,
    /// The shell's waiters.
    waiters: OnionLinkWaiters,
}

#[cfg(all(test, rings_native))]
impl OnionLinkWitness {
    /// Wait until the link table holds a live link of `did`.
    ///
    /// No lost wakeup: the check and the waiter's registration happen under the admission lock,
    /// and a link fact wakes the waiters only after it released that lock.
    pub(crate) async fn live(&self, did: Did) {
        loop {
            let woken = {
                let admission = lock(&self.admission).expect("admission lock");
                if admission.live_link(did).is_some() {
                    return;
                }
                let (waiter, woken) = futures::channel::oneshot::channel();
                lock(&self.waiters).expect("waiter lock").push(waiter);
                woken
            };
            let _ = woken.await;
        }
    }
}

impl OnionCircuitShell {
    /// A shell over the node's epoch, with a link table of `2·𝓡` for the connection
    /// registry capacity `registry_capacity`, sharing `slot_roster` with the node's `PeerRing`
    /// and `tags` and `link_sender` with the node's sessions.
    #[allow(dead_code)]
    pub(crate) fn new(
        relays: bool,
        epoch: OnionProcessEpochCell,
        registry_capacity: NonZeroUsize,
        slot_roster: Arc<SlotRoster>,
        tags: OnionClientTags,
        link_sender: OnionLinkSender,
        algebra: OnionAlgebra,
    ) -> Self {
        Self::new_with_capabilities(
            relays,
            epoch,
            registry_capacity,
            slot_roster,
            tags,
            link_sender,
            algebra,
            Arc::new(SystemClock),
            SharedRng::system(),
        )
    }

    /// [`Self::new`] with explicit `clock` and `rng` capabilities for deterministic replay.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new_with_capabilities(
        relays: bool,
        epoch: OnionProcessEpochCell,
        registry_capacity: NonZeroUsize,
        slot_roster: Arc<SlotRoster>,
        tags: OnionClientTags,
        link_sender: OnionLinkSender,
        algebra: OnionAlgebra,
        clock: SharedClock,
        rng: SharedRng,
    ) -> Self {
        let filter_key = OnionReplayFilterKey::from_rng(&mut rng.lock_rng());
        let admission = OnionAdmissionState::new(epoch.get(), filter_key, registry_capacity)
            .with_slot_roster(slot_roster);
        Self {
            relays,
            admission: Arc::new(Mutex::new(admission)),
            epoch,
            tags,
            link_sender,
            algebra,
            generations: AtomicU64::new(0),
            clock,
            rng,
            #[cfg(all(test, rings_native))]
            link_waiters: OnionLinkWaiters::default(),
        }
    }

    /// The witness of this shell's link table.
    #[cfg(all(test, rings_native))]
    pub(crate) fn link_witness(&self) -> OnionLinkWitness {
        OnionLinkWitness {
            admission: Arc::clone(&self.admission),
            waiters: Arc::clone(&self.link_waiters),
        }
    }

    /// The symbols registered in this shell's exit algebra, for tests of [`OnionRuntime::install`].
    #[cfg(all(test, rings_native))]
    pub(crate) fn registered_services(
        &self,
    ) -> std::collections::BTreeSet<crate::onion::OnionServiceName> {
        self.algebra.symbols().cloned().collect()
    }

    /// `Cell(from, cell)` of the module diagram.
    async fn hop(
        &self,
        scope: &EffectScope,
        from: Did,
        cell: crate::onion::sphinx::cell::OnionCell,
    ) -> Result<()> {
        let now_ms = self.clock.now_ms();
        self.renew_if_rolled_back(now_ms)?;
        let keys = scope.lifecycle().decryption_keys_at(now_ms);
        let outcome = {
            let mut admission = lock(&self.admission)?;
            hop(
                &mut admission,
                &keys,
                self.relays,
                |tag| self.tags.expects(from, tag),
                from,
                now_ms,
                cell,
            )
        };
        match outcome {
            OnionHopOutcome::Stepped(OnionStep::Relayed { next, cell }) => {
                // A full lane drops the cell: the loop's session sees a gap and fails closed.
                if let Err(error) =
                    self.link_sender
                        .enqueue(scope.lifecycle(), OnionLink::new(next), cell)
                {
                    tracing::debug!(%next, %error, "drop a relayed onion cell");
                }
                Ok(())
            }
            OnionHopOutcome::Stepped(OnionStep::Consumed {
                symbol,
                arguments,
                value,
                surb,
            }) => {
                let input = OnionApplicationInput {
                    from,
                    arguments,
                    value,
                    surb,
                    received_at_ms: now_ms,
                };
                self.algebra
                    .evaluate(&scope.lifecycle(), &symbol, input)
                    .await
            }
            OnionHopOutcome::Returned { tag, cell } => {
                if let Err(dropped) = self.tags.deliver(now_ms, from, &tag, cell) {
                    tracing::debug!(%from, %dropped, "drop a returning onion cell");
                }
                Ok(())
            }
            OnionHopOutcome::Refused(rejection) => {
                tracing::debug!(%from, ?rejection, "refuse an onion cell uncharged");
                Ok(())
            }
            OnionHopOutcome::Dropped(drop) => {
                tracing::debug!(%from, %drop, "drop a charged onion cell");
                Ok(())
            }
        }
    }

    /// Apply one link fact (see the module diagram): first to the admission table, then to the
    /// link emitter, whose lanes follow the links the table holds live, then to the sessions
    /// that ride the link (`Law (Circuit fate)`).
    async fn link(&self, scope: &EffectScope, fact: OnionLinkFact) -> Result<()> {
        let now_ms = self.clock.now_ms();
        let lifecycle = scope.lifecycle();
        let (refused, lanes) = match fact {
            OnionLinkFact::Opened(peer) => self.open_link(now_ms, peer)?,
            OnionLinkFact::Closed(peer) => {
                let mut admission = lock(&self.admission)?;
                if let Some(link) = admission.live_link(peer) {
                    admission.link_closed(now_ms, link);
                }
                (Vec::new(), OnionLaneChange::Close(peer))
            }
            OnionLinkFact::Dropped(_) => (Vec::new(), OnionLaneChange::Keep),
            OnionLinkFact::Reconcile => self.reconcile(now_ms, lifecycle.admitted_peers())?,
        };
        let applied = match lanes {
            OnionLaneChange::Keep => Ok(()),
            OnionLaneChange::Open(peer) => self.link_sender.open(lifecycle, OnionLink::new(peer)),
            OnionLaneChange::Close(peer) => self.link_sender.close(OnionLink::new(peer)),
            OnionLaneChange::Follow(up) => self.link_sender.reconcile(&lifecycle, &up),
        };
        if let Err(error) = applied {
            tracing::debug!(%error, "onion link lanes did not follow a link fact");
        }
        if let OnionLinkFact::Closed(peer) | OnionLinkFact::Dropped(peer) = fact {
            self.tags.lose(peer);
            self.algebra.lose(peer);
        }
        #[cfg(all(test, rings_native))]
        if let Ok(mut waiters) = lock(&self.link_waiters) {
            waiters.drain(..).for_each(|waiter| {
                let _ = waiter.send(());
            });
        }
        for link in refused {
            tracing::debug!(
                did = %link.peer(),
                "onion link table is full; the link carries no onion cell"
            );
        }
        Ok(())
    }

    /// `Opened(peer)`: a fresh generation of `peer`'s link, unless one is live already.
    fn open_link(
        &self,
        now_ms: u128,
        peer: Did,
    ) -> Result<(Vec<OnionAdmissionLink>, OnionLaneChange)> {
        let mut admission = lock(&self.admission)?;
        if admission.live_link(peer).is_some() {
            return Ok((Vec::new(), OnionLaneChange::Keep));
        }
        let link = OnionAdmissionLink::new(peer, self.next_generation());
        Ok(match admission.link_opened(now_ms, link) {
            Ok(()) => (Vec::new(), OnionLaneChange::Open(peer)),
            Err(_) => (vec![link], OnionLaneChange::Keep),
        })
    }

    /// `Reconcile`: make the live links those of `admitted`, core's admitted peers read now,
    /// keeping the generation of every peer already live.
    fn reconcile(
        &self,
        now_ms: u128,
        admitted: Vec<Did>,
    ) -> Result<(Vec<OnionAdmissionLink>, OnionLaneChange)> {
        let mut admission = lock(&self.admission)?;
        let snapshot = admitted
            .into_iter()
            .map(|peer| {
                admission
                    .live_link(peer)
                    .unwrap_or_else(|| OnionAdmissionLink::new(peer, self.next_generation()))
            })
            .collect::<Vec<_>>();
        let refused = admission.reconcile(now_ms, snapshot).links().to_vec();
        Ok((refused, OnionLaneChange::Follow(live_peers(&admission))))
    }

    /// A generation no admission of this table has had.
    fn next_generation(&self) -> u64 {
        self.generations.fetch_add(1, Ordering::Relaxed)
    }

    /// Renew `A` into a fresh epoch if the clock has rolled back beyond `X₀`, and set the
    /// node's epoch cell to it, which the registrations publish at their next heartbeat. The
    /// renewed table keeps its own live links, which are linearised with the facts it has
    /// applied, so it refuses none.
    fn renew_if_rolled_back(&self, now_ms: u128) -> Result<()> {
        let mut admission = lock(&self.admission)?;
        if !admission.is_rolled_back_at(now_ms) {
            return Ok(());
        }
        loop {
            let mut rng = self.rng.lock_rng();
            let epoch = OnionProcessEpoch::from_rng(&mut rng);
            let key = OnionReplayFilterKey::from_rng(&mut rng);
            drop(rng);
            if admission.renew(epoch, key).is_ok() {
                self.epoch.renew(epoch);
                tracing::warn!("onion admission renewed its epoch after a clock rollback");
                return Ok(());
            }
            // The fresh draw met the current epoch (probability 2⁻¹²⁸): draw again.
        }
    }
}

impl Drop for OnionCircuitShell {
    /// Law (lifetime): the data plane lives as long as its registration, which owns the shell,
    /// not as long as any handle to it: a relay whose runtime handle was dropped still relays.
    /// When the registry drops the shell, the link sender shuts down, which closes every lane,
    /// stops its emitter, and refuses every later open, so no onion task outlives the protocol.
    fn drop(&mut self) {
        if let Err(error) = self.link_sender.shutdown() {
            tracing::debug!(%error, "onion data plane could not close its link lanes");
        }
    }
}

/// What one link fact does to the emitter's lanes.
enum OnionLaneChange {
    /// Nothing.
    Keep,
    /// The peer's link is up.
    Open(Did),
    /// No generation of the peer's link is live.
    Close(Did),
    /// Exactly these peers' links are up.
    Follow(Vec<Did>),
}

/// The peers with a live link in `admission`, each once.
fn live_peers(admission: &OnionAdmissionState) -> Vec<Did> {
    let mut peers = admission
        .live_links()
        .into_iter()
        .map(OnionAdmissionLink::peer)
        .collect::<Vec<_>>();
    peers.dedup();
    peers
}

#[cfg_attr(rings_browser, async_trait::async_trait(?Send))]
#[cfg_attr(rings_native, async_trait::async_trait)]
impl Interpret for OnionCircuitShell {
    type Effect = OnionCircuitInput;

    async fn run(&self, scope: &EffectScope, effect: OnionCircuitInput) -> Result<Vec<Bytes>> {
        match effect {
            OnionCircuitInput::Cell { from, cell } => self.hop(scope, from, cell).await,
            OnionCircuitInput::Link(fact) => self.link(scope, fact).await,
        }
        .map(|()| Vec::new())
        .or_else(|error: Error| {
            tracing::debug!(%error, "onion circuit effect failed");
            Ok(Vec::new())
        })
    }
}