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.
//! Run-owned bootstrap reachability supervision for `rings run`.
//!
//! `rings connect node` and `rings connect seed` establish a connection once. This module gives
//! the native daemon a set of *managed* bootstrap targets that it keeps reachable for the life
//! of the process: whenever a target stops being reachable through the overlay, the supervisor
//! redials it through its HTTP endpoint, first in a bounded rapid burst and then on a slow
//! jittered cadence, and resets once the target is reachable again. Ordinary neighbour and
//! finger edges stay under Chord stabilization; only the configured targets are supervised, and
//! a target reachable through other peers is never forced into a direct edge.
//!
//! The design splits into a pure core and an effect shell:
//!
//! - `schedule::BootstrapSchedule` (private) — the retry state machine, a function of explicit
//!   time; its module docs carry the phase diagram and laws.
//! - `BootstrapPort` (crate-private) — the two effects a turn needs: assess reachability and
//!   dial; `ProcessorPort` is the production port over a live processor.
//! - [`BootstrapSupervisor`] — the shell: wakes on deadlines, target losses and finished turns;
//!   runs at most one turn per target; exits on the run's stop token.
//! - `evidence::ReachabilityEvidence` (crate-private) — what the swarm reports about the
//!   targets: admissions the dial waits for and retirements the supervisor reads as losses;
//!   written by the [`Backend`] through [`BackendObserver`], which the supervisor hands out as
//!   [`BootstrapSupervisor::observer`].
//!
//! ```text
//!                    ┌────────────────────────── run loop ──────────────────────────┐
//!  StopToken ──stop─▶│ drain losses ─▶ notice_loss ─▶ ∀ due target: begin ─▶ push turn│
//!  Losses ─────wake─▶│ wait { stop | wake | turn finished | next deadline } ─▶ settle │
//!                    └──────────────────────────────────────────────────────────────┘
//!
//!  turn(t) := reachable(t) ? Reachable : dial(t) = Ok ? Reachable : Unreachable
//! ```
//!
//! Lost-wakeup freedom: every wait is preceded by a drain of the loss record, and a loss
//! recorded between the drain and the wait leaves a stored permit, so no loss is missed. A turn
//! is bounded: the port bounds its probe, the endpoint's resolution, each of its two HTTP
//! requests and the admission wait with a timeout of its own, and the core bounds the offer
//! (ICE gathering) and the routed send it performs on the port's behalf; the one await without
//! a bound of its own is the transport close a cancelled handshake performs.
//!
//! Shutdown: the loop returns on the first stop observation and its in-flight turns are
//! dropped with it, cancelling any probe or handshake in progress. A pending connection attempt
//! the core already reserved expires under the core's own pending timeout. Transient failures
//! stay inside the loop; only configuration is validated up front and fails fast.
//!
//! [`Backend`]: crate::extension::Backend
//! [`BackendObserver`]: crate::extension::BackendObserver

use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use futures::future::BoxFuture;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use rings_core::dht::Did;
use tokio::time::Instant;

use self::evidence::PeerLosses;
use self::schedule::BootstrapSchedule;
use self::schedule::TurnOutcome;
use crate::error::Error;
use crate::error::Result;
use crate::extension::BackendObserver;
use crate::native::config::BootstrapConfig;
use crate::prelude::StopToken;
use crate::processor::Processor;
use crate::seed::validate_seed_peers;
use crate::seed::ValidatedSeedPeer;

mod evidence;
mod probe;
mod schedule;
#[cfg(test)]
mod tests;

pub(crate) use self::evidence::ReachabilityEvidence;
pub(crate) use self::probe::ProcessorPort;

/// Validated managed targets: one per DID, one per endpoint, none of them the local node.
#[derive(Debug)]
pub struct BootstrapTargets(Vec<ValidatedSeedPeer>);

impl BootstrapTargets {
    /// Validate `config` for the node `local`: the entries validate as a seed set (every entry
    /// validates, a verbatim repeat is merged, a DID with conflicting entries or an endpoint
    /// under two DIDs is refused). An entry naming `local` itself is skipped with a log line,
    /// as `connect seed` skips it: the shared seed document a seed node supervises the other
    /// seeds from lists that node too.
    pub fn from_config(config: BootstrapConfig, local: Did) -> Result<Self> {
        let mut targets = validate_seed_peers(config.peers)?;
        if targets.iter().any(|target| target.did() == local) {
            tracing::info!(peer = %local, "bootstrap target is this node; skipped");
            targets.retain(|target| target.did() != local);
        }
        Ok(Self(targets))
    }

    /// Whether there is nothing to supervise.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Number of managed targets.
    #[cfg(test)]
    pub(crate) fn len(&self) -> usize {
        self.0.len()
    }
}

/// Why a dial did not connect the target.
#[derive(Debug)]
pub(crate) enum DialFailure {
    /// The target's slot is owned by another attempt (a handshake in flight, whichever side
    /// started it, or an admission being committed): refused before any request when the
    /// local core already holds an unadmitted handshake, or by the local core during the
    /// exchange. A refusal by the remote core arrives as an RPC error and is a failure.
    InFlight,
    /// The handshake or its admission failed.
    Failed(Error),
}

/// Effects one supervisor turn performs on a target.
#[async_trait]
pub(crate) trait BootstrapPort: Send + Sync {
    /// Whether `target` is reachable through the overlay right now.
    async fn reachable(&self, target: Did) -> bool;

    /// Redial `target` through its HTTP endpoint; `Ok` once the peer is admitted.
    async fn dial(&self, target: &ValidatedSeedPeer) -> std::result::Result<(), DialFailure>;
}

/// The run-owned supervisor; see the module diagram.
pub struct BootstrapSupervisor {
    targets: BTreeMap<Did, Arc<ValidatedSeedPeer>>,
    port: Arc<dyn BootstrapPort>,
    evidence: Arc<ReachabilityEvidence>,
    schedule: BootstrapSchedule,
    origin: Instant,
}

/// In-flight turns, each resolving to the target it served and its outcome.
type Turns = FuturesUnordered<BoxFuture<'static, (Did, TurnOutcome)>>;

/// What woke the run loop.
enum Wake {
    /// The run's stop token was observed.
    Stop,
    /// A target loss was recorded.
    Loss,
    /// A turn finished for the target.
    Turn(Did, TurnOutcome),
    /// The next scheduled deadline passed.
    Deadline,
}

impl BootstrapSupervisor {
    /// Supervision of `targets` over a live processor, or `None` when there is nothing to
    /// supervise, in which case no observer is needed either.
    pub fn over_processor(targets: BootstrapTargets, processor: Arc<Processor>) -> Option<Self> {
        if targets.is_empty() {
            return None;
        }
        let evidence = Arc::new(ReachabilityEvidence::default());
        let port = Arc::new(ProcessorPort::new(processor, evidence.clone()));
        Some(Self::new(targets, port, evidence, rand::random()))
    }

    /// Supervisor over `port` reading `evidence`, with every target due immediately and jitter
    /// seeded by `jitter_seed`.
    pub(crate) fn new(
        targets: BootstrapTargets,
        port: Arc<dyn BootstrapPort>,
        evidence: Arc<ReachabilityEvidence>,
        jitter_seed: u64,
    ) -> Self {
        let targets: BTreeMap<Did, Arc<ValidatedSeedPeer>> = targets
            .0
            .into_iter()
            .map(|target| (target.did(), Arc::new(target)))
            .collect();
        let schedule = BootstrapSchedule::new(targets.keys().copied(), jitter_seed);
        Self {
            targets,
            port,
            evidence,
            schedule,
            origin: Instant::now(),
        }
    }

    /// The observer the daemon installs on its backend so the swarm's reports reach this
    /// supervisor.
    pub fn observer(&self) -> Arc<dyn BackendObserver> {
        self.evidence.clone()
    }

    /// Run until `stop` is observed; never returns otherwise.
    pub async fn run(mut self, stop: StopToken) {
        let mut turns = Turns::new();
        loop {
            self.drain_losses();
            self.start_due_turns(&mut turns);
            let deadline = self.schedule.next_deadline_ms().and_then(|deadline_ms| {
                self.origin.checked_add(Duration::from_millis(deadline_ms))
            });
            match wait_for_wake(&stop, self.evidence.losses(), &mut turns, deadline).await {
                Wake::Stop => return,
                Wake::Loss | Wake::Deadline => {}
                Wake::Turn(target, outcome) => {
                    self.schedule.settle(target, outcome, self.now_ms());
                }
            }
        }
    }

    /// Milliseconds since the supervisor started, saturating.
    fn now_ms(&self) -> u64 {
        schedule::duration_ms(self.origin.elapsed())
    }

    /// Fold every recorded target loss into the schedule.
    fn drain_losses(&mut self) {
        let lost = match self.evidence.losses().take() {
            Ok(lost) => lost,
            Err(error) => {
                tracing::error!(%error, "bootstrap loss record unavailable");
                return;
            }
        };
        let now_ms = self.now_ms();
        for target in lost {
            if self.schedule.notice_loss(target, now_ms) {
                tracing::info!(peer = %target, "bootstrap target left the local DHT; reassessing");
            }
        }
    }

    /// Push one turn for every target that is due.
    fn start_due_turns(&mut self, turns: &mut Turns) {
        let now_ms = self.now_ms();
        for (did, target) in &self.targets {
            if self.schedule.begin_if_due(*did, now_ms) {
                turns.push(Box::pin(turn(self.port.clone(), target.clone())));
            }
        }
    }
}

/// Block until the run should act again.
///
/// An empty turn set yields `None` at once, which disables its branch rather than completing
/// it, so an idle supervisor sleeps until its deadline, a loss, or the stop token. Every future
/// here is cancel-safe: the stop wait registers before re-checking, the loss wait consumes a
/// permit `notify_one` stores when nobody waits, the turn stream hands back completed turns one
/// at a time, and the sleep holds no state.
async fn wait_for_wake(
    stop: &StopToken,
    losses: &PeerLosses,
    turns: &mut Turns,
    deadline: Option<Instant>,
) -> Wake {
    tokio::select! {
        _ = stop.stopped() => Wake::Stop,
        _ = losses.woken() => Wake::Loss,
        Some((target, outcome)) = turns.next() => Wake::Turn(target, outcome),
        _ = sleep_until_or_forever(deadline) => Wake::Deadline,
    }
}

/// Sleep until `deadline`, or forever when no target has one.
async fn sleep_until_or_forever(deadline: Option<Instant>) {
    match deadline {
        Some(deadline) => tokio::time::sleep_until(deadline).await,
        None => std::future::pending().await,
    }
}

/// One turn: assess reachability and, only when unreachable, redial. A refused dial is logged
/// as such and, like a failed one, is a miss for the schedule.
async fn turn(port: Arc<dyn BootstrapPort>, target: Arc<ValidatedSeedPeer>) -> (Did, TurnOutcome) {
    let did = target.did();
    if port.reachable(did).await {
        tracing::debug!(peer = %did, "bootstrap target reachable");
        return (did, TurnOutcome::Reachable);
    }
    match port.dial(target.as_ref()).await {
        Ok(()) => {
            tracing::info!(peer = %did, endpoint = %target.endpoint(), "bootstrap target redialed");
            (did, TurnOutcome::Reachable)
        }
        Err(DialFailure::InFlight) => {
            tracing::debug!(peer = %did, "bootstrap redial refused: the target's slot is owned by another attempt");
            (did, TurnOutcome::Unreachable)
        }
        Err(DialFailure::Failed(error)) => {
            tracing::warn!(peer = %did, endpoint = %target.endpoint(), %error, "bootstrap redial failed");
            (did, TurnOutcome::Unreachable)
        }
    }
}