rings-node 1.0.0-pre.6

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
//! End-to-end loops over real links (#843): `relay^k ⋙ tcp` and `relay^k ⋙ https`, with `k = 4`
//! relay positions around one symbol hop.
//!
//! The data path is bounded by the client's own timeouts, not by a test duration (#985). Once a
//! loop is open, every link of its route emits at the constant rate `r` (#880), real cells
//! replacing cover, and a link's cell rate is `min(r / u(b), ρ_rel / f(b))`. Where the receiver
//! releases frames slower than `r`, the lane's window stays full of cover, so a real cell waits
//! at every hop for the window ahead of it to drain at `ρ_rel`. That rate is the transport's on
//! the test host, not a protocol period: a debug build takes seconds per hop, and a fixed
//! wall-clock guard on a multi-cell phase measures it. What the protocol bounds is progress, and
//! the client reports a stall itself, as an error or a failed stream:
//!
//! ```text
//! open     an answer within ONION_SESSION_OPEN_TIMEOUT              else timed out
//! https    a response within ONION_HTTPS_RESPONSE_TIMEOUT of the open   else timed out
//! stream   a frame within Δ = 2V of the last one heard              else Failed (D2′ Liveness)
//! ```
//!
//! Each guard below is that bound plus one [`EVENT_BOUND`] for the report to arrive, so it fires
//! only if the client's own bound fails to.

use std::time::Duration;
use std::time::Instant;

use bytes::Bytes;
use rings_core::ecc::PublicKey;
use rings_test_support::with_hang_guard;

use super::loop_network::open_policy;
use super::loop_network::EchoWorld;
use super::loop_network::LoopNetwork;
use super::loop_network::SourceWorld;
use super::loop_network::EVENT_BOUND;
use crate::error::Error;
use crate::error::Result;
use crate::onion::circuit::OnionAlgebra;
use crate::onion::https::OnionHttpsCall;
use crate::onion::https::OnionHttpsClient;
use crate::onion::https::OnionHttpsEgress;
use crate::onion::https::OnionHttpsRequest;
use crate::onion::https::OnionHttpsResponse;
use crate::onion::https::OnionHttpsWorld;
use crate::onion::https::ONION_HTTPS_RESPONSE_TIMEOUT;
use crate::onion::native::NativeOnionCircuitHandle;
use crate::onion::proxy::OnionProxyRoute;
use crate::onion::session::client::OnionCreditWindow;
use crate::onion::session::client::ONION_SESSION_DEADLINE_MS;
use crate::onion::session::dial::OnionSessionRequest;
use crate::onion::session::dial::OnionStreamEvent;
use crate::onion::session::dial::OnionStreamReceiver;
use crate::onion::session::dial::ONION_SESSION_OPEN_TIMEOUT;
use crate::onion::session::frame::OnionFrame;
use crate::onion::session::serve::OnionExitSessions;
use crate::onion::session::serve::OnionWorld;
use crate::onion::OnionExitPolicy;
use crate::onion::OnionLoopClass;
use crate::onion::OnionProxyTarget;
use crate::onion::OnionRouteError;
use crate::onion::OnionServiceName;

/// The fixture target.
fn target() -> OnionProxyTarget {
    OnionProxyTarget::parse_authority("example.com:443").expect("authority")
}

/// A network whose `h` interprets `symbol` over `world` under `policy`.
async fn network<W: OnionWorld>(
    symbol: OnionServiceName,
    world: W,
    policy: OnionExitPolicy,
) -> Result<LoopNetwork> {
    LoopNetwork::new(move |accounting, link_sender| {
        OnionAlgebra::default().register(
            symbol,
            OnionExitSessions::new(world, policy, accounting.clone(), link_sender.clone()),
        )
    })
    .await
}

/// The session request of `symbol` to [`target`] over `network`'s route.
fn request(network: &LoopNetwork, symbol: OnionServiceName) -> Result<OnionSessionRequest> {
    Ok(OnionSessionRequest {
        route: network.route(symbol.clone())?,
        symbol,
        target: target(),
        class: OnionLoopClass::DEFAULT,
        window: OnionCreditWindow::DEFAULT,
    })
}

/// The hang guard of one awaited stream event: the session deadline `Δ = 2V`, within which the
/// client fails a session that hears no frame, plus one [`EVENT_BOUND`] for the failure to arrive.
fn stream_event_bound() -> Duration {
    let deadline = u64::try_from(ONION_SESSION_DEADLINE_MS).expect("Δ fits in u64 milliseconds");
    Duration::from_millis(deadline).saturating_add(EVENT_BOUND)
}

/// The hang guard of one HTTPS request: the client's open and response timeouts, each of which
/// fails the request with an error, plus one [`EVENT_BOUND`] for the request's own sends and the
/// failure to arrive.
fn https_request_bound() -> Duration {
    ONION_SESSION_OPEN_TIMEOUT
        .saturating_add(ONION_HTTPS_RESPONSE_TIMEOUT)
        .saturating_add(EVENT_BOUND)
}

/// Collect the world-to-client stream until `fin`, failing on a failed session.
///
/// Each event is awaited under [`stream_event_bound`], so the guard scales with the frames of
/// the stream: it bounds a stall, never the transport's rate.
async fn collect(receiver: &mut OnionStreamReceiver) -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    let bound = stream_event_bound();
    loop {
        match with_hang_guard("the loop stream's next event", bound, receiver.next()).await {
            Some(OnionStreamEvent::Data(data)) => bytes.extend_from_slice(&data),
            Some(OnionStreamEvent::Fin) => return Ok(bytes),
            Some(OnionStreamEvent::Failed) | None => {
                return Err(Error::OnionRouteError(OnionRouteError::SessionFailed))
            }
        }
    }
}

/// `relay^4 ⋙ tcp` over an echo: the stream returns byte for byte, across several frames, and
/// the client's `fin` ends the echo's stream.
#[tokio::test]
async fn test_tcp_loop_echoes_a_multi_frame_stream() -> Result<()> {
    let network = network(OnionServiceName::tcp(), EchoWorld, open_policy()).await?;
    let stream = network
        .client
        .runtime
        .open(request(&network, OnionServiceName::tcp())?)
        .await?;
    let (mut sender, mut receiver) = stream.split();
    let sent = (0..40_000_u32)
        .map(|index| u8::try_from(index % 251).expect("small"))
        .collect::<Vec<_>>();

    sender.send(Bytes::from(sent.clone())).await?;
    sender.fin().await?;
    let echoed = collect(&mut receiver).await?;

    assert_eq!(echoed, sent);
    Ok(())
}

/// A target the exit policy denies is refused at the open: `abort` before any data, and no
/// reason on the wire (#843 Q5).
#[tokio::test]
async fn test_a_denied_target_is_refused_at_the_open() -> Result<()> {
    let deny = OnionExitPolicy::from_target_strings(vec!["other.example:443".to_string()], vec![])?;
    let network = network(OnionServiceName::tcp(), EchoWorld, deny).await?;

    let opened = network
        .client
        .runtime
        .open(request(&network, OnionServiceName::tcp())?)
        .await;

    assert!(matches!(
        opened,
        Err(Error::OnionRouteError(OnionRouteError::ExitRefused))
    ));
    Ok(())
}

/// A session whose driver ends before `h` answers its open reports the driver's own error,
/// never `ExitRefused` (#1118): with `r₀₂`'s session key off the curve, the client cannot build
/// the open's loop, so the driver ends at its first send and `h` hears nothing.
#[tokio::test]
async fn test_a_session_ended_before_the_answer_is_not_refused() -> Result<()> {
    let network = network(OnionServiceName::tcp(), EchoWorld, open_policy()).await?;
    let [g, mut r02, h, r11] = network.route_hops();
    r02.delegatee_public_key = PublicKey([0xff; 33]);
    let route = network.route_over(OnionServiceName::tcp(), [g, r02, h, r11])?;

    let opened = network
        .client
        .runtime
        .open(OnionSessionRequest {
            route,
            ..request(&network, OnionServiceName::tcp())?
        })
        .await;

    assert!(matches!(
        opened,
        Err(Error::OnionRouteError(OnionRouteError::LoopBuild(_)))
    ));
    Ok(())
}

/// The test egress: a response naming the target and echoing the request.
fn respond(target: &OnionProxyTarget, request: &OnionHttpsRequest) -> OnionHttpsResponse {
    OnionHttpsResponse {
        status: 200,
        headers: vec![("x-target".to_string(), target.authority())],
        body: [
            request.method.as_bytes(),
            b" ",
            request.path.as_bytes(),
            b" ",
        ]
        .concat()
        .into_iter()
        .chain(request.body.iter().copied())
        .collect(),
    }
}

/// `relay^4 ⋙ https` through the fetch world over a test egress: the request's authority is
/// the session's target, and the response returns whole.
#[tokio::test]
async fn test_https_loop_fetches_through_the_session_target() -> Result<()> {
    let network = LoopNetwork::new(|accounting, link_sender| {
        OnionAlgebra::default().register(
            OnionServiceName::https(),
            OnionExitSessions::new(
                OnionHttpsWorld::with_egress(
                    open_policy(),
                    accounting.clone(),
                    OnionHttpsEgress::Test(respond),
                ),
                open_policy(),
                accounting.clone(),
                link_sender.clone(),
            ),
        )
    })
    .await?;
    let (target, call) = OnionHttpsCall::from_url(
        "https://example.com/search?q=loop",
        crate::onion::https::OnionHttpsClientRequest {
            method: "POST".to_string(),
            body: vec![0x42; 20_000],
            ..Default::default()
        },
    )?;
    let route = OnionProxyRoute {
        target,
        route: network.route(OnionServiceName::https())?,
    };

    let response = with_hang_guard(
        "test_https_loop_fetches_through_the_session_target",
        https_request_bound(),
        OnionHttpsClient::new(network.client.runtime.clone()).request(&route, call),
    )
    .await?;

    assert_eq!(response.status, 200);
    assert_eq!(response.headers, vec![(
        "x-target".to_string(),
        "example.com:443".to_string()
    )]);
    assert_eq!(
        response.body,
        [
            b"POST /search?q=loop ".as_slice(),
            [0x42; 20_000].as_slice()
        ]
        .concat()
    );
    Ok(())
}

/// Caller-selected loop class `b = 64 KiB` on the native handle: a `tcp` stream opened via
/// [`NativeOnionCircuitHandle::open_tcp_stream_with_class`] and an `https` fetch sent via
/// [`NativeOnionCircuitHandle::request_https`] (with [`OnionHttpsClientRequest::class`]) carry a
/// 20 KiB payload (> `OnionFrame::data_capacity(KiB16) = 13,442`, ≤
/// `OnionFrame::data_capacity(KiB64) = 62,594`) end to end over a `KiB64` loop.
#[tokio::test]
async fn test_caller_apis_open_tcp_and_https_sessions_at_chosen_loop_class() -> Result<()> {
    let class = OnionLoopClass::KiB64;
    let payload_len = 20_000;
    assert!(payload_len > OnionFrame::data_capacity(OnionLoopClass::DEFAULT));
    assert!(payload_len <= OnionFrame::data_capacity(class));

    let network = LoopNetwork::new(|accounting, link_sender| {
        OnionAlgebra::default()
            .register(
                OnionServiceName::tcp(),
                OnionExitSessions::new(
                    EchoWorld,
                    open_policy(),
                    accounting.clone(),
                    link_sender.clone(),
                ),
            )
            .register(
                OnionServiceName::https(),
                OnionExitSessions::new(
                    OnionHttpsWorld::with_egress(
                        open_policy(),
                        accounting.clone(),
                        OnionHttpsEgress::Test(respond),
                    ),
                    open_policy(),
                    accounting.clone(),
                    link_sender.clone(),
                ),
            )
    })
    .await?;
    let handle = NativeOnionCircuitHandle::new(network.client.runtime.clone());

    // 1. Native TCP stream opened with `OnionLoopClass::KiB64`.
    let opened = with_hang_guard(
        "open_tcp_stream_with_class(KiB64)",
        ONION_SESSION_OPEN_TIMEOUT.saturating_add(EVENT_BOUND),
        handle.open_tcp_stream_with_class(network.route(OnionServiceName::tcp())?, target(), class),
    )
    .await?;
    let (local, mut peer) = tokio::io::duplex(64 * 1024);
    let local: rings_gateway::BoxGatewayDuplex = Box::new(local);
    opened.relay(local);
    let sent = (0..payload_len)
        .map(|index| u8::try_from(index % 251).expect("small"))
        .collect::<Vec<_>>();
    tokio::io::AsyncWriteExt::write_all(&mut peer, &sent)
        .await
        .expect("write local stream");
    tokio::io::AsyncWriteExt::shutdown(&mut peer)
        .await
        .expect("shutdown local write half");
    let mut echoed = Vec::new();
    with_hang_guard(
        "tcp KiB64 echo read_to_end",
        stream_event_bound(),
        tokio::io::AsyncReadExt::read_to_end(&mut peer, &mut echoed),
    )
    .await
    .expect("read echoed stream");
    assert_eq!(echoed, sent);

    // 2. HTTPS request with `class: Some(OnionLoopClass::KiB64)` through `NativeOnionCircuitHandle`.
    let (https_target, call) = OnionHttpsCall::from_url(
        "https://example.com/class?b=64kib",
        crate::onion::https::OnionHttpsClientRequest {
            method: "POST".to_string(),
            body: sent.clone(),
            class: Some(class),
            ..Default::default()
        },
    )?;
    assert_eq!(call.class(), class);
    let https_route = OnionProxyRoute {
        target: https_target,
        route: network.route(OnionServiceName::https())?,
    };
    let response = with_hang_guard(
        "request_https with KiB64",
        https_request_bound(),
        handle.request_https(&https_route, call),
    )
    .await?;
    assert_eq!(response.status, 200);
    assert_eq!(
        response.body,
        [b"POST /class?b=64kib ".as_slice(), sent.as_slice()].concat()
    );
    Ok(())
}

/// The throughput bench (#843): a `tcp` download of 8 MiB at `b = 16 KiB` over loopback links,
/// at least 1 MB/s. Run with `--ignored`; it reports the rate it measured.
///
/// It runs on a multi-thread runtime, as a native node does by default (`--runtime
/// multi-thread`). All five nodes share this process, so a current-thread runtime would put
/// every node's work on one core and measure that core rather than the loop (#1087): the five
/// nodes' WebRTC transports, signatures and onion crypto saturate it near 0.5 MB/s, below the
/// paced rate `r = ρ·B/V ≈ 98` cells of 16 KiB per second per link.
#[tokio::test(flavor = "multi_thread")]
#[ignore = "benchmark"]
async fn bench_tcp_download_rate() -> Result<()> {
    const DOWNLOAD: usize = 8 << 20;
    let network = network(
        OnionServiceName::tcp(),
        SourceWorld(DOWNLOAD),
        open_policy(),
    )
    .await?;
    let stream = network
        .client
        .runtime
        .open(request(&network, OnionServiceName::tcp())?)
        .await?;
    let (_sender, mut receiver) = stream.split();

    let started = Instant::now();
    let received = collect(&mut receiver).await?;
    let seconds = started.elapsed().as_secs_f64();
    let rate = f64::from(u32::try_from(received.len()).expect("small")) / seconds;
    println!(
        "tcp download: {} bytes in {seconds:.2} s = {rate:.0} B/s",
        received.len()
    );

    assert_eq!(received.len(), DOWNLOAD);
    assert!(rate >= 1_000_000.0, "{rate:.0} B/s is under 1 MB/s");
    Ok(())
}