rings-node 1.0.0-pre.8

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.
Documentation
pub mod test_browser;
pub mod test_evidence;
/// Browser mail descriptors, durable reception, notifications, and shared-tab records.
pub mod test_mail;
pub mod test_processor;
use std::sync::Arc;
use std::time::Duration;

use futures::channel::mpsc;
use futures::lock::Mutex;
use futures::StreamExt;
use rings_core::delegation::DelegateeKey;
use rings_core::dht::Did;
use rings_core::ecc::SecretKey;
use rings_core::storage::idb::IdbStorage;
use rings_core::swarm::application::SwarmEvent;
use rings_rpc::protos::rings_node::AcceptAnswerRequest;
use rings_rpc::protos::rings_node::AnswerOfferRequest;
use rings_rpc::protos::rings_node::AnswerOfferResponse;
use rings_rpc::protos::rings_node::CreateOfferRequest;
use rings_rpc::protos::rings_node::CreateOfferResponse;
use rings_rpc::protos::rings_node::ListPeersRequest;
use rings_rpc::protos::rings_node::ListPeersResponse;
use rings_rpc::protos::rings_node::PeerInfo;
use rings_transport::core::transport::TransportConnectionState;
use uuid;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::wasm_bindgen_test_configure;

use crate::extension::ext::LinkFact;
use crate::extension::BackendObserver;
use crate::logging::browser::init_logging;
use crate::onion::OnionExitOffer;
use crate::onion::OnionRole;
use crate::prelude::rings_core::utils::js_value;
use crate::processor::Processor;
use crate::processor::ProcessorConfig;
use crate::provider::Provider;
use crate::tests::test_processor_builder;
use crate::tests::TEST_TRAVERSAL_SERVERS;

/// Per-test hang guard of the connection tests; see [`with_hang_guard`].
///
/// Budget arithmetic (measured unloaded in headless Chrome): wasm-bindgen-test gives the whole
/// binary 120 s, and the suite passes in about 7 s. Five tests carry this guard, so even a
/// common-cause hang of all five (browser WebRTC broken for every test) costs at most
/// 5 × 20 s = 100 s, plus the suite's normal runtime, and stays inside the runner budget.
/// Every guard therefore fails by name before the runner's timeout.
pub const TEST_HANG_GUARD: Duration = Duration::from_secs(20);

/// A logical peer transition: a link fact read off the application inbox.
type Transition = LinkFact;

/// Write side of a peer-transition log: forwards every admission and retirement of one swarm.
///
/// Install it when the swarm is created, before any connection exists. Every transition is
/// then buffered in the log, so a test that awaits one later cannot miss it.
#[derive(Clone)]
pub struct TransitionRecorder(mpsc::UnboundedSender<Transition>);

impl TransitionRecorder {
    /// Record the transition `event` is, if it is about a link.
    pub fn record(&self, event: &SwarmEvent) {
        if let Some(fact) = LinkFact::of(event) {
            self.send(fact);
        }
    }

    /// Append `transition` to the log; a log whose reader is gone has nobody left to wake.
    fn send(&self, transition: Transition) {
        let _ = self.0.unbounded_send(transition);
    }
}

impl BackendObserver for TransitionRecorder {
    fn link_fact(&self, fact: LinkFact) {
        self.send(fact);
    }
}

/// Read side of a peer-transition log.
pub struct PeerTransitions(Mutex<TransitionLog>);

/// Transitions received so far that no wait has consumed, and the channel of later ones.
struct TransitionLog {
    unconsumed: Vec<Transition>,
    receiver: mpsc::UnboundedReceiver<Transition>,
}

/// A peer-transition log: the recorder to install on a swarm and the reader a test awaits.
pub fn peer_transitions() -> (TransitionRecorder, PeerTransitions) {
    let (sender, receiver) = mpsc::unbounded();
    (
        TransitionRecorder(sender),
        PeerTransitions(Mutex::new(TransitionLog {
            unconsumed: Vec::new(),
            receiver,
        })),
    )
}

impl PeerTransitions {
    /// Await `peer`'s next admission: its transport is ready and it joined the local DHT.
    pub async fn admitted(&self, peer: Did) {
        self.consume(LinkFact::Admitted(peer)).await
    }

    /// Await `peer`'s next retirement: its record left the local DHT.
    pub async fn retired(&self, peer: Did) {
        self.consume(LinkFact::Retired(peer)).await
    }

    /// Consume the oldest unconsumed occurrence of `wanted`, waiting for it if necessary.
    ///
    /// ```text
    /// log = unconsumed ++ channel        (arrival order)
    /// consume(t): remove the first t in log, awaiting the channel until one arrives
    /// ```
    ///
    /// Law (no lost wake-up): the recorder was installed before any connection existed, and
    /// the channel is unbounded, so every transition that occurred is in `log`, whether it
    /// happened before or after this call. Transitions skipped on the way stay in
    /// `unconsumed` for later waits, so the order in which a test waits is free.
    async fn consume(&self, wanted: Transition) {
        let mut log = self.0.lock().await;
        if let Some(position) = log.unconsumed.iter().position(|seen| *seen == wanted) {
            log.unconsumed.remove(position);
            return;
        }
        let arrival = async {
            while let Some(transition) = log.receiver.next().await {
                if transition == wanted {
                    return true;
                }
                log.unconsumed.push(transition);
            }
            false
        };
        if !arrival.await {
            panic!("transition recorder dropped before {wanted:?}");
        }
    }
}

/// Await `a`'s admission of `b` and `b`'s admission of `a`.
pub async fn await_mutual_admission(
    (a, a_transitions): (Did, &PeerTransitions),
    (b, b_transitions): (Did, &PeerTransitions),
) {
    futures::join!(a_transitions.admitted(b), b_transitions.admitted(a));
}

/// Per-test hang guard shared with core's browser tests; see
/// [`rings_test_support::with_hang_guard`].
pub use rings_test_support::with_hang_guard;

/// Whether `promise` is settled at this moment, decided without waiting on a timer.
///
/// The microtask queue is flushed first, so work that is ready without a timer or I/O has run.
/// `Promise.race` then takes the first *already settled* promise in array order: `promise`
/// itself if it has settled, otherwise the resolved sentinel.
pub async fn promise_settled_now(promise: &js_sys::Promise) -> bool {
    let flushed = js_sys::Promise::resolve(&wasm_bindgen::JsValue::UNDEFINED);
    JsFuture::from(flushed).await.unwrap();
    let sentinel = js_sys::Object::new();
    let raced = js_sys::Promise::race(&js_sys::Array::of2(
        promise,
        &js_sys::Promise::resolve(&sentinel),
    ));
    let winner = JsFuture::from(raced).await;
    !matches!(winner, Ok(value) if value == wasm_bindgen::JsValue::from(sentinel))
}

wasm_bindgen_test_configure!(run_in_browser);

pub fn setup_log() {
    init_logging(crate::logging::LogLevel::Info);
    tracing::debug!("test")
}

pub async fn prepare_processor() -> Processor {
    prepare_processor_with_onion_role(OnionRole::Client).await
}

/// Prepare a browser test processor registering the onion symbols of `role`.
pub async fn prepare_processor_with_onion_role(role: OnionRole<OnionExitOffer>) -> Processor {
    let delegatee_key = DelegateeKey::new_with_seckey(&SecretKey::random()).unwrap();
    let config = ProcessorConfig::new(0, TEST_TRAVERSAL_SERVERS.to_string(), delegatee_key, 200);

    let storage_name = uuid::Uuid::new_v4().to_simple().to_string();
    let storage = Box::new(
        IdbStorage::new_with_cap_and_name(50000, &storage_name)
            .await
            .unwrap(),
    );

    test_processor_builder(&config)
        .storage(storage)
        .onion_role(role)
        .build()
        .unwrap()
}

/// A provider together with the log of its peer transitions.
pub struct ObservedProvider {
    /// The provider under test.
    pub provider: Provider,
    /// Admissions and retirements its extension backend observed.
    pub transitions: PeerTransitions,
}

/// A provider whose extension backend records its peer transitions from creation on.
pub async fn new_provider() -> ObservedProvider {
    let processor = prepare_processor().await;
    let provider = Provider::from_processor(Arc::new(processor));
    let (recorder, transitions) = peer_transitions();
    provider
        .serve_application(Some(Arc::new(recorder)))
        .unwrap();
    ObservedProvider {
        provider,
        transitions,
    }
}

/// The DID `provider` signs as.
pub fn provider_did(provider: &Provider) -> Did {
    provider.address().parse().unwrap()
}

pub async fn get_peers(provider: &Provider) -> Vec<PeerInfo> {
    let resp = JsFuture::from(provider.request(
        "listPeers".to_string(),
        js_value::serialize(&ListPeersRequest {}).unwrap(),
    ))
    .await
    .unwrap();

    js_value::deserialize::<ListPeersResponse>(resp)
        .unwrap()
        .peers
}

/// Whether `state`, a listed peer's WebRTC state as `peers()` and `listPeers` render it, is one
/// in which an admitted connection is routable.
///
/// ```text
/// Ready(c) ⟺ webrtc(c) ∈ {Connecting, Connected} ∧ channel_open(c)   (core readiness)
/// Listed(c) ⟹ Ready(c) ⟹ webrtc(c) ∈ {Connecting, Connected}
/// ```
///
/// The listing reports the raw WebRTC state, and the browser may fire the data channel's
/// `open` before the `connectionstatechange` to `connected`. An admitted, listed peer may
/// therefore still read `Connecting`; only `Connected` is not guaranteed (#962).
pub fn is_routable_transport_state(state: &str) -> bool {
    [
        TransportConnectionState::Connecting,
        TransportConnectionState::Connected,
    ]
    .iter()
    .any(|routable| format!("{routable:?}") == state)
}

/// Connect two providers and await both admissions.
///
/// The offer/answer exchange returns before ICE, DTLS, the data-channel open and core
/// admission complete, so the admission events on both ends are the completion signal.
pub async fn create_connection(node1: &ObservedProvider, node2: &ObservedProvider) {
    let req0 = CreateOfferRequest {
        did: node2.provider.address(),
    };
    let resp0 = JsFuture::from(node1.provider.request(
        "createOffer".to_string(),
        js_value::serialize(&req0).unwrap(),
    ))
    .await
    .unwrap();

    let offer = js_value::deserialize::<CreateOfferResponse>(resp0)
        .unwrap()
        .offer;

    let req1 = AnswerOfferRequest { offer };
    let resp1 = JsFuture::from(node2.provider.request(
        "answerOffer".to_string(),
        js_value::serialize(&req1).unwrap(),
    ))
    .await
    .unwrap();

    let answer = js_value::deserialize::<AnswerOfferResponse>(resp1)
        .unwrap()
        .answer;

    let req2 = AcceptAnswerRequest { answer };
    let _resp2 = JsFuture::from(node1.provider.request(
        "acceptAnswer".to_string(),
        js_value::serialize(&req2).unwrap(),
    ))
    .await
    .unwrap();

    await_mutual_admission(
        (provider_did(&node1.provider), &node1.transitions),
        (provider_did(&node2.provider), &node2.transitions),
    )
    .await;
}