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
//! The unit budgets: charge once on receipt, `B` per DID and `G` per hop over the sliding window.

use rand::rngs::StdRng;
use rand::SeedableRng;

use super::expiry;
use super::latest_expiry;
use super::layer;
use super::link;
use super::open;
use super::send;
use super::sender_load;
use super::state;
use super::state_with;
use super::units;
use super::Verdict;
use super::EPOCH;
use super::ORIGIN_MS;
use super::Q;
use crate::onion::circuit::admission::OnionAdmissionRejection;
use crate::onion::circuit::admission::OnionChargeRejection;
use crate::onion::circuit::admission::ADMISSION_WINDOW_QUANTA_WIDE;
use crate::onion::circuit::admission::ONION_ADMISSION_GLOBAL_UNITS;
use crate::onion::circuit::admission::ONION_ADMISSION_SENDER_UNITS;
use crate::onion::circuit::admission::ONION_ADMISSION_SLOT_FLOOR_UNITS;
use crate::onion::circuit::ONION_ADMISSION_WINDOW_MS;

/// Law (charging): a cell is charged exactly once, before its key is computed. A `γ`-failing cell
/// drops its token and stays charged without touching the replay store. Replayed and expired
/// cells stay charged. A refused charge charges nothing. Pipelined cells cannot overdraw the
/// budget, because each ECDH needs a token that has already been paid for.
#[test]
fn test_every_computed_cell_is_charged_exactly_once() {
    let mut rng = StdRng::seed_from_u64(0x0841_000f);
    let mut admission = state(&mut rng);
    let quantum = ORIGIN_MS / Q;
    let x = latest_expiry(ORIGIN_MS);
    let sender = link(1);

    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 2, layer(x, 1)),
        Verdict::Admitted
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 3, layer(x, 1)),
        Verdict::Rejected(OnionAdmissionRejection::Replayed)
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1, 5, layer(expiry(quantum), 2)),
        Verdict::Rejected(OnionAdmissionRejection::OutsideWindow)
    );
    let invalid = admission.charge_units(ORIGIN_MS, sender, units(7));
    assert!(invalid.is_ok());
    drop(invalid);
    assert_eq!(sender_load(&admission, 1, ORIGIN_MS), Some(2 + 3 + 5 + 7));
    assert_eq!(admission.global.load(quantum), 2 + 3 + 5 + 7);
    assert_eq!(
        admission
            .replay
            .filters()
            .get(&x)
            .map(|filter| filter.blocks().count()),
        Some(1)
    );

    let spent = ONION_ADMISSION_SENDER_UNITS - 17 - 1;
    assert!(admission
        .charge_units(ORIGIN_MS, sender, units(spent))
        .is_ok());
    let pipelined = (0..8)
        .map(|_| admission.charge_units(ORIGIN_MS, sender, units(1)))
        .collect::<Vec<_>>();
    assert!(pipelined.first().is_some_and(Result::is_ok));
    assert!(pipelined
        .iter()
        .skip(1)
        .all(|charge| matches!(charge, Err(OnionChargeRejection::SenderBudget))));
    assert_eq!(
        sender_load(&admission, 1, ORIGIN_MS),
        Some(ONION_ADMISSION_SENDER_UNITS)
    );
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 1_000, 1, layer(x, 4)),
        Verdict::Unpaid(OnionChargeRejection::LinkNotLive)
    );
    assert_eq!(admission.global.load(quantum), ONION_ADMISSION_SENDER_UNITS);
}

/// Law: one sender is charged at most `B` units over five aligned quanta. The budget returns
/// exactly when the charging quantum leaves the window, and other senders are unaffected.
#[test]
fn test_sender_budget_is_bounded_over_the_sliding_window() {
    let mut rng = StdRng::seed_from_u64(0x0841_0005);
    let mut admission = state(&mut rng);
    let x = latest_expiry(ORIGIN_MS);
    let over = Verdict::Unpaid(OnionChargeRejection::SenderBudget);
    for tag in 0..u128::from(ONION_ADMISSION_SENDER_UNITS / 768) {
        assert_eq!(
            send(&mut admission, ORIGIN_MS, 1, 768, layer(x, tag)),
            Verdict::Admitted
        );
    }
    assert_eq!(send(&mut admission, ORIGIN_MS, 1, 768, layer(x, 100)), over);
    assert_eq!(
        send(
            &mut admission,
            ORIGIN_MS,
            1,
            ONION_ADMISSION_SENDER_UNITS % 768,
            layer(x, 101)
        ),
        Verdict::Admitted
    );
    assert_eq!(send(&mut admission, ORIGIN_MS, 1, 1, layer(x, 102)), over);
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 2, 1, layer(x, 103)),
        Verdict::Admitted
    );

    let last_in_window = ORIGIN_MS + ADMISSION_WINDOW_QUANTA_WIDE * Q - 1;
    assert_eq!(
        send(
            &mut admission,
            last_in_window,
            1,
            1,
            layer(latest_expiry(last_in_window), 104)
        ),
        over
    );
    let refilled = last_in_window + 1;
    assert_eq!(
        send(
            &mut admission,
            refilled,
            1,
            1,
            layer(latest_expiry(refilled), 105)
        ),
        Verdict::Admitted
    );
}

/// Law: the hop admits at most `G = 64·B` units per window across all slots (floors plus shared
/// pool). Once both the shared pool `P` and every slot's floor `f` are spent, a further charge is
/// refused with [`OnionChargeRejection::GlobalBudget`] and leaves every ledger unchanged.
#[test]
fn test_global_budget_bounds_every_sender_together() {
    let mut rng = StdRng::seed_from_u64(0x0841_0006);
    let mut admission = state_with(&mut rng, EPOCH, 64, 0..128);
    let x = latest_expiry(ORIGIN_MS);
    for sender in 64..128_u32 {
        assert_eq!(
            send(
                &mut admission,
                ORIGIN_MS,
                sender,
                ONION_ADMISSION_SLOT_FLOOR_UNITS,
                layer(x, u128::from(sender))
            ),
            Verdict::Admitted
        );
    }
    let per_noisy_slot = ONION_ADMISSION_SENDER_UNITS - ONION_ADMISSION_SLOT_FLOOR_UNITS;
    for sender in 0..64_u32 {
        assert_eq!(
            send(
                &mut admission,
                ORIGIN_MS,
                sender,
                per_noisy_slot,
                layer(x, u128::from(sender))
            ),
            Verdict::Admitted
        );
    }
    assert_eq!(
        admission.global.load(ORIGIN_MS / Q),
        ONION_ADMISSION_GLOBAL_UNITS
    );
    let before = admission.slots.clone();
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 64, 1, layer(x, 200)),
        Verdict::Unpaid(OnionChargeRejection::GlobalBudget)
    );
    assert_eq!(admission.slots, before);
    let next_window = ORIGIN_MS + ONION_ADMISSION_WINDOW_MS;
    assert_eq!(
        send(
            &mut admission,
            next_window,
            64,
            1,
            layer(latest_expiry(next_window), 200)
        ),
        Verdict::Admitted
    );
}

/// Law (#1183): `ONION_ADMISSION_GLOBAL_UNITS` reserves a non-zero per-slot floor `f > 0`
/// separate from the shared pool `P`. Even when 64 noisy Sybil slots exhaust the entire shared
/// pool `P`, they cannot starve an honest connection slot (either already open or newly admitted)
/// below its guaranteed floor `f`.
#[test]
fn test_per_slot_floor_protects_honest_slot_when_64_noisy_slots_exhaust_pool() {
    let mut rng = StdRng::seed_from_u64(0x0841_0020);
    let mut admission = state(&mut rng);
    let (floor_units, pool_units) = admission.shares();
    assert_eq!(floor_units, ONION_ADMISSION_SLOT_FLOOR_UNITS);
    assert!(floor_units >= 768);
    assert_eq!(128 * floor_units + pool_units, ONION_ADMISSION_GLOBAL_UNITS);

    let x = latest_expiry(ORIGIN_MS);
    let mut tag = 0_u128;
    // 64 noisy slots each spend their floor plus as much of the shared pool `P` as fits in `B`.
    for sender in 0..64_u32 {
        let remaining_pool = pool_units.saturating_sub(admission.pool_load);
        let over_floor =
            remaining_pool.min(ONION_ADMISSION_SENDER_UNITS - ONION_ADMISSION_SLOT_FLOOR_UNITS);
        let spend = ONION_ADMISSION_SLOT_FLOOR_UNITS + over_floor;
        tag += 1;
        assert_eq!(
            send(&mut admission, ORIGIN_MS, sender, spend, layer(x, tag)),
            Verdict::Admitted
        );
    }
    assert_eq!(admission.pool_load, pool_units);
    // The 64th noisy slot (`sender = 63`) still has personal sender-budget headroom, but the
    // shared pool `P` is exhausted, so its next unit is rejected with `GlobalBudget`.
    assert!(
        sender_load(&admission, 63, ORIGIN_MS).unwrap_or(0) < ONION_ADMISSION_SENDER_UNITS,
        "noisy slot 63 still has personal sender-budget headroom"
    );
    tag += 1;
    assert_eq!(
        send(&mut admission, ORIGIN_MS, 63, 1, layer(x, tag)),
        Verdict::Unpaid(OnionChargeRejection::GlobalBudget)
    );

    // Honest live slot 64 (and a newly opened honest slot 65) can still spend their full
    // guaranteed floor `f`, including a maximum-size 768-unit (`MiB12`) cell, without starvation.
    open(&mut admission, ORIGIN_MS, link(65));
    for honest in [64_u32, 65_u32] {
        tag += 1;
        assert_eq!(
            send(&mut admission, ORIGIN_MS, honest, 768, layer(x, tag)),
            Verdict::Admitted
        );
        tag += 1;
        assert_eq!(
            send(
                &mut admission,
                ORIGIN_MS,
                honest,
                floor_units - 768,
                layer(x, tag)
            ),
            Verdict::Admitted
        );
        // One unit beyond its floor `f` requires the exhausted pool `P` and is refused.
        tag += 1;
        assert_eq!(
            send(&mut admission, ORIGIN_MS, honest, 1, layer(x, tag)),
            Verdict::Unpaid(OnionChargeRejection::GlobalBudget)
        );
    }
}