offline_protocol/protocol/mesh_relay.rs
1//! Admission control and pacing for mesh forwarding.
2//!
3//! Forwarding is what turns a set of nearby devices into a network: a frame
4//! addressed to someone out of radio range is carried by the devices in
5//! between. The hazard is that the same property which gives coverage —
6//! every node repeating what it hears — also multiplies traffic, and in a
7//! dense room the multiplication can outrun the radio. This module is the
8//! governor on that: it decides whether a frame is forwarded at all, when,
9//! and how fast frames may leave.
10//!
11//! Four mechanisms, each covering something the others cannot:
12//!
13//! 1. **Suppression** ([`RelaySeenCache`]) — an id is forwarded once. Without
14//! it, copies circulate until their hop limit runs out, and every node
15//! repeats every copy.
16//! 2. **Jitter with narrowing** — a forward waits a short randomized delay
17//! before going out, and every copy that arrives while it waits takes the
18//! neighbor that sent it off the fan-out, because that neighbor holds the
19//! frame. A forward whose every neighbor has handed it a copy is dropped
20//! as covered. In a dense cluster the first neighbors to fire leave the
21//! rest less to do, and it adapts on its own as the room fills up, with no
22//! threshold to tune. A copy does *not* cancel the forward outright:
23//! carriers forward by unicast to a few chosen neighbors, so a copy proves
24//! only that its sender holds the frame, never that the region is covered.
25//! Cancelling on it once stood down the only device beside the way out of
26//! a dense cluster, and the frame died there (#510).
27//! 3. **Hop limits** — an arriving frame's remaining hop budget is clamped
28//! before use, so a frame that claims an implausible budget (nothing
29//! authenticates it at this layer) cannot circulate longer than one our own
30//! policy would have issued.
31//! 4. **Budgets** — a hard ceiling on frames per second, in total and per
32//! neighbor. The first three assume frames are what they appear to be;
33//! budgets hold even when they are not. Whatever arrives, and however it is
34//! shaped, a node cannot be made to transmit faster than its budget: that
35//! invariant is what bounds the worst case, so the failure mode under attack
36//! or overload is added delay rather than a radio that never goes quiet.
37//!
38//! Ordering matters. Suppression runs before anything expensive, admission
39//! before queueing (so a flood cannot grow the queue), and pacing at the moment
40//! of transmission (so a forward cancelled while waiting costs no budget).
41//!
42//! On top of those four sits **capability bias**: how willing this particular
43//! device is, right now, to spend itself on other people's traffic. A charging
44//! laptop and a phone at 20% are both allowed to forward, and the mechanisms
45//! above treat them identically — so the phone pays the same share as the
46//! laptop. Bias tilts three of the existing dials by battery and charging
47//! state, continuously: a weaker device waits longer before transmitting (so a
48//! stronger neighbor holding the same frame usually transmits first, and its
49//! copy takes that neighbor off the weaker device's fan-out before anything is
50//! spent), forwards to fewer neighbors, and refills its send budget more
51//! slowly.
52//!
53//! This is deliberately a *bias* and not a role. A threshold that switches
54//! forwarding on and off makes the network's shape depend on a state machine,
55//! and the failure mode of getting that wrong is a partition — devices that
56//! could carry a frame declining to. Scaling instead means every device
57//! forwards, capable ones simply forward first and more, and the worst a
58//! misjudged scale costs is some redundancy. The relay *role* reported to apps
59//! is derived the other way round, from what this device has actually carried
60//! ([`MeshRelayGovernor::observe_activity`]), so it describes behaviour rather
61//! than predicting it.
62
63use offline_protocol_core::{Message, MessagePriority};
64use offline_protocol_reliability::{RelaySeenCache, RelaySeenConfig};
65use std::collections::hash_map::DefaultHasher;
66use std::collections::HashMap;
67use std::hash::{Hash, Hasher};
68use std::time::{Duration, Instant};
69
70use super::custody::CUSTODY_META_KEY;
71
72/// Hop budget a forwarded frame is clamped to under normal density.
73pub const DEFAULT_RELAY_MAX_TTL: u8 = 8;
74
75/// Hop budget used once the local neighborhood is dense.
76///
77/// With many neighbors, coverage is reached in fewer hops — the same message
78/// travels further per hop — so a lower ceiling reaches the same devices for
79/// less traffic.
80pub const DEFAULT_RELAY_DENSE_MAX_TTL: u8 = 5;
81
82/// Neighbor count at or above which the dense hop budget applies.
83pub const DEFAULT_RELAY_DENSE_DEGREE: usize = 6;
84
85/// Absolute ceiling on how many times a frame may be forwarded, regardless of
86/// the hop budget it carries. A backstop against a frame whose hop fields have
87/// been shaped to evade the clamp.
88pub const RELAY_MAX_HOP_COUNT: u8 = 16;
89
90/// Neighbors a single frame is forwarded to.
91pub const DEFAULT_RELAY_FANOUT: usize = 3;
92
93/// Shortest delay before a queued forward is transmitted.
94pub const DEFAULT_RELAY_JITTER_MIN_MS: u64 = 20;
95
96/// Longest delay before a queued forward is transmitted, at low density.
97pub const DEFAULT_RELAY_JITTER_MAX_MS: u64 = 200;
98
99/// Sustained ceiling on forwarded frames leaving this node, per second.
100pub const DEFAULT_RELAY_RATE_PER_SEC: f32 = 10.0;
101
102/// Burst allowance on top of the sustained forwarding rate.
103pub const DEFAULT_RELAY_BURST: f32 = 30.0;
104
105/// Transmissions held back from carrying other people's traffic so this device
106/// can always get its own frames out.
107///
108/// Own traffic and forwarded traffic share one budget, because they share one
109/// radio. Without a reserve they also share one queue discipline, and a device
110/// forwarding at its ceiling would starve its *own* sends behind strangers'
111/// — including the delivery acknowledgement for a message it just received,
112/// whose absence makes a delivered message look lost. Carving the reserve out
113/// of the burst rather than adding a second bucket keeps the ceiling exactly
114/// where it was: this changes who may spend the last few tokens, not how many
115/// there are.
116///
117/// It costs forwarding no throughput at saturation. Tokens refill continuously,
118/// so a forward simply waits for the level to climb back above the reserve —
119/// which at the sustained rate is a fraction of a second.
120pub const RELAY_OWN_TRAFFIC_RESERVE: f32 = 5.0;
121
122/// Sustained ceiling on frames accepted for forwarding from any one neighbor.
123///
124/// Keeps a single noisy or hostile link from consuming the whole node's
125/// forwarding budget: its frames are refused at the door, leaving the budget
126/// for everyone else's traffic.
127pub const DEFAULT_RELAY_PEER_RATE_PER_SEC: f32 = 5.0;
128
129/// Burst allowance for a single neighbor.
130pub const DEFAULT_RELAY_PEER_BURST: f32 = 15.0;
131
132/// Maximum forwards waiting for their delay to elapse.
133pub const DEFAULT_RELAY_QUEUE_CAPACITY: usize = 256;
134
135/// Maximum neighbors tracked for per-neighbor rate limiting.
136pub const MAX_RELAY_RATE_PEERS: usize = 256;
137
138/// How long a queued forward may sit past its due time before being abandoned.
139///
140/// If a node is so far over budget that a frame waits this long, the frame's
141/// usefulness has passed — other paths have carried it or the sender has
142/// retransmitted — and holding it only displaces newer traffic.
143///
144/// Public because it bounds what the delay tunables may be set to: a jitter
145/// window plus a bias handicap that could exceed it would turn every biased
146/// forward into an abandoned one, which `ProtocolConfig::validate` refuses.
147pub const RELAY_QUEUE_MAX_OVERDUE: Duration = Duration::from_secs(5);
148
149/// Smallest share of the full forwarding effort a device is scaled down to by
150/// capability bias.
151///
152/// A floor rather than zero, because bias must never become an off switch:
153/// a device at 1% battery that is still allowed to forward at all (the
154/// [`RelayConfig::min_battery_for_relay`] gate is what decides that, not this)
155/// keeps a usable share, so a network of uniformly low devices still carries
156/// traffic instead of quietly ceasing to be a network.
157///
158/// [`RelayConfig::min_battery_for_relay`]: offline_protocol_router::RelayConfig::min_battery_for_relay
159pub const DEFAULT_RELAY_BIAS_MIN_SCALE: f32 = 0.25;
160
161/// Longest extra delay capability bias adds before a weaker device transmits.
162///
163/// This is the whole mechanism by which a stronger neighbor gets to carry a
164/// frame first: the weaker device's delay window opens later, so in the common
165/// case it is still holding the frame when neighbors' copies arrive, each one
166/// narrows its fan-out, and once every neighbor holds a copy it transmits
167/// nothing at all.
168///
169/// A fixed ceiling rather than a multiple of the jitter span, and that matters:
170/// the span already widens with density, and compounding the two would push a
171/// weak device in a crowded room past [`RELAY_QUEUE_MAX_OVERDUE`], where its
172/// forwards are abandoned rather than merely late. Bias must cost redundancy,
173/// never delivery.
174pub const DEFAULT_RELAY_BIAS_MAX_HANDICAP_MS: u64 = 400;
175
176/// How long a stretch of forwarding activity is measured over.
177pub const DEFAULT_RELAY_ACTIVITY_WINDOW_SECS: u64 = 60;
178
179/// Frames carried for other people within one window at or above which this
180/// device reads as an active relay.
181pub const DEFAULT_RELAY_ACTIVITY_MIN_FORWARDS: u64 = 3;
182
183/// Consecutive quiet windows before an active relay reads as inactive again.
184///
185/// Asymmetric with promotion on purpose: becoming a relay takes one busy
186/// window, ceasing to be one takes several quiet ones. A relay in a mesh that
187/// simply has nothing to say for a minute has not stopped being a relay, and
188/// reporting that it has would produce churn no app can act on.
189pub const DEFAULT_RELAY_ACTIVITY_IDLE_WINDOWS: u32 = 2;
190
191/// Tunables for mesh forwarding.
192#[derive(Debug, Clone)]
193pub struct MeshRelayConfig {
194 /// Hop budget a forwarded frame is clamped to.
195 pub max_ttl: u8,
196 /// Hop budget applied once the neighborhood is dense.
197 pub dense_max_ttl: u8,
198 /// Neighbor count at which the dense budget applies.
199 pub dense_degree: usize,
200 /// Neighbors a frame is forwarded to.
201 pub fanout: usize,
202 /// Shortest pre-transmit delay.
203 pub jitter_min: Duration,
204 /// Longest pre-transmit delay at low density.
205 pub jitter_max: Duration,
206 /// Sustained forwarding rate, frames per second.
207 pub rate_per_sec: f32,
208 /// Burst allowance above the sustained rate.
209 pub burst: f32,
210 /// Sustained per-neighbor acceptance rate.
211 pub peer_rate_per_sec: f32,
212 /// Per-neighbor burst allowance.
213 pub peer_burst: f32,
214 /// Maximum forwards awaiting transmission.
215 pub queue_capacity: usize,
216 /// Suppression cache sizing.
217 pub seen: RelaySeenConfig,
218 /// Smallest share of the full forwarding effort capability bias scales a
219 /// device down to. `1.0` disables bias: every device forwards as eagerly
220 /// as every other, whatever its battery.
221 pub bias_min_scale: f32,
222 /// Longest extra pre-transmit delay bias adds to a weaker device.
223 pub bias_max_handicap: Duration,
224 /// How long a stretch of forwarding activity is measured over.
225 pub activity_window: Duration,
226 /// Frames carried within one window at or above which this device reads as
227 /// an active relay.
228 pub activity_min_forwards: u64,
229 /// Consecutive quiet windows before an active relay reads as inactive.
230 pub activity_idle_windows: u32,
231}
232
233impl Default for MeshRelayConfig {
234 fn default() -> Self {
235 Self {
236 max_ttl: DEFAULT_RELAY_MAX_TTL,
237 dense_max_ttl: DEFAULT_RELAY_DENSE_MAX_TTL,
238 dense_degree: DEFAULT_RELAY_DENSE_DEGREE,
239 fanout: DEFAULT_RELAY_FANOUT,
240 jitter_min: Duration::from_millis(DEFAULT_RELAY_JITTER_MIN_MS),
241 jitter_max: Duration::from_millis(DEFAULT_RELAY_JITTER_MAX_MS),
242 rate_per_sec: DEFAULT_RELAY_RATE_PER_SEC,
243 burst: DEFAULT_RELAY_BURST,
244 peer_rate_per_sec: DEFAULT_RELAY_PEER_RATE_PER_SEC,
245 peer_burst: DEFAULT_RELAY_PEER_BURST,
246 queue_capacity: DEFAULT_RELAY_QUEUE_CAPACITY,
247 seen: RelaySeenConfig::default(),
248 bias_min_scale: DEFAULT_RELAY_BIAS_MIN_SCALE,
249 bias_max_handicap: Duration::from_millis(DEFAULT_RELAY_BIAS_MAX_HANDICAP_MS),
250 activity_window: Duration::from_secs(DEFAULT_RELAY_ACTIVITY_WINDOW_SECS),
251 activity_min_forwards: DEFAULT_RELAY_ACTIVITY_MIN_FORWARDS,
252 activity_idle_windows: DEFAULT_RELAY_ACTIVITY_IDLE_WINDOWS,
253 }
254 }
255}
256
257/// This device's own condition, as far as forwarding is concerned.
258///
259/// Pushed in from the engine each tick rather than read here, because the
260/// battery reading belongs to the host and reaches the SDK through
261/// `set_battery_state` — the governor holds no transports and asks nothing.
262#[derive(Debug, Clone, Copy, Default)]
263struct RelayConditions {
264 /// Host-reported battery percentage, or `None` while nothing has reported
265 /// one.
266 battery: Option<u8>,
267 /// Whether the device is plugged in.
268 is_charging: bool,
269 /// Whether the configuration asks this device to relay eagerly
270 /// (`RelayPriority::Always`).
271 eager: bool,
272 /// The soft battery floor forwarding is gated on, which is also where the
273 /// bias ramp starts.
274 soft_floor: u8,
275}
276
277/// A change in whether this device is carrying traffic for other people.
278///
279/// Returned by [`MeshRelayGovernor::observe_activity`] only at the tick the
280/// answer changes, so a caller can announce it exactly once.
281#[derive(Debug, Clone, Copy, PartialEq, Eq)]
282pub enum RelayActivity {
283 /// This device started carrying third-party traffic.
284 Began {
285 /// Frames carried in the window that triggered this.
286 forwarded: u64,
287 },
288 /// This device stopped carrying third-party traffic.
289 Ceased,
290}
291
292/// Why a frame was not queued for forwarding.
293#[derive(Debug, Clone, Copy, PartialEq, Eq)]
294pub enum RelayRejection {
295 /// Already handled; this is another copy of a frame we have dealt with.
296 AlreadySeen,
297 /// Another copy arrived while this one was waiting. The pending forward
298 /// stays, minus the neighbor that sent the copy, which now holds it.
299 NarrowedByDuplicate,
300 /// The frame has no hop budget left.
301 HopLimitReached,
302 /// The neighbor that sent it is over its acceptance rate.
303 PeerRateLimited,
304 /// No room in the pending queue.
305 QueueFull,
306}
307
308/// The outcome of offering an inbound frame to the governor.
309#[derive(Debug, Clone, Copy, PartialEq, Eq)]
310pub enum RelayAdmission {
311 /// Queued; it will be transmitted once its delay elapses.
312 Queued,
313 /// Not queued.
314 Rejected(RelayRejection),
315}
316
317/// A forward waiting for its delay to elapse.
318#[derive(Debug, Clone)]
319pub struct PendingRelay {
320 /// The frame to forward, with hop fields already adjusted.
321 pub message: Message,
322 /// The neighbor it arrived from, which must not receive it back.
323 pub arrival_peer: Option<String>,
324 /// Neighbors that handed us a further copy while this forward waited.
325 /// Each holds the frame, so the flush leaves them out of the fan-out,
326 /// and a forward with no neighbor left outside this set is dropped as
327 /// covered. Kept apart from [`Self::arrival_peer`], which custody reads
328 /// as the link the frame first came in on.
329 pub heard_from: Vec<String>,
330 /// When it becomes eligible to transmit.
331 pub due_at: Instant,
332 /// The class token the frame carried as a deposit request on arrival
333 /// (`docs/spec/custody.md`). Kept here because the forwarded copy has
334 /// the key stripped, and the drop point needs it to judge the frame for
335 /// custody. `None` on a frame that asked for nothing.
336 pub custody_request: Option<String>,
337 /// For a held frame being redelivered: the one neighbor it is
338 /// transmitted to, never through target selection. `None` on an
339 /// ordinary forward. A forward carrying this is a *marked* forward: the
340 /// drop point returns it to the custody store instead of judging it, and
341 /// neither the overdue cut-off nor a refused requeue touches the
342 /// suppression cache for it, because the held intake never recorded its
343 /// id there.
344 pub custody_target: Option<String>,
345}
346
347/// Running totals, for telemetry and for tests that assert a flood stayed
348/// bounded.
349#[derive(Debug, Clone, Default, PartialEq, Eq)]
350pub struct MeshRelayCounters {
351 /// Frames queued for forwarding.
352 pub queued: u64,
353 /// Times a frame was put on a link, counting each link separately.
354 ///
355 /// This is the airtime meter — what the send budget actually bounds — so it
356 /// counts this device handing over its *own* messages too. For "how much
357 /// have I carried for other people", see [`Self::forwarded`].
358 pub transmissions: u64,
359 /// Third-party frames that crossed at least one link on someone's behalf.
360 ///
361 /// Counted once per frame carried, not once per link, and never for this
362 /// device's own traffic.
363 pub forwarded: u64,
364 /// Frames put back on the queue because they reached no neighbor at all —
365 /// the budget ran out, the links chosen for them went away, or there was
366 /// no usable link to choose.
367 pub requeued: u64,
368 /// Queued forwards displaced to make room for a higher-priority frame.
369 pub queue_evicted: u64,
370 /// Arrivals suppressed as already handled.
371 pub duplicates_suppressed: u64,
372 /// Pending forwards dropped because every neighbor they could go to had
373 /// already handed this device a copy.
374 pub cancelled_by_duplicate: u64,
375 /// Frames refused because their sender was over its rate.
376 pub peer_rate_limited: u64,
377 /// Frames refused because the pending queue was full.
378 pub queue_full: u64,
379 /// Frames dropped for having no hop budget left.
380 pub hop_limit_reached: u64,
381 /// Transmissions deferred because the node was at its forwarding rate.
382 pub rate_deferred: u64,
383 /// Queued forwards abandoned after waiting too long past their due time.
384 pub abandoned_overdue: u64,
385 /// Hop budgets reduced because the frame claimed more than policy allows.
386 pub ttl_clamped: u64,
387}
388
389/// What this device has carried for other people.
390///
391/// A snapshot, safe to poll. Every field is cumulative since start-up except
392/// [`Self::awaiting_transmission`], which is a gauge: it is the queue depth
393/// right now, and it goes down as well as up.
394#[derive(Debug, Clone, Default, PartialEq, Eq)]
395pub struct MeshRelayStats {
396 /// Frames carried onward on someone else's behalf, counted once each.
397 pub forwarded: u64,
398 /// Times this device put a frame on a link, counting each link separately
399 /// and including hand-offs of its own messages.
400 ///
401 /// This is what the per-second forwarding budget bounds, so it is the
402 /// number to compare against that ceiling — [`Self::forwarded`] is the
403 /// contribution figure to show a user.
404 pub transmissions: u64,
405 /// Frames accepted for forwarding.
406 pub queued: u64,
407 /// Frames waiting to go out right now.
408 pub awaiting_transmission: usize,
409 /// Arrivals ignored because the frame had already been handled.
410 pub duplicates_suppressed: u64,
411 /// Forwards dropped because every neighbor they could go to had already
412 /// handed this device the same frame, the saving that keeps a crowded
413 /// room from repeating itself.
414 pub covered_by_a_neighbor: u64,
415 /// Frames refused because the neighbor sending them was over its share.
416 pub peer_rate_limited: u64,
417 /// Frames refused admission, or refused room on their way back to the
418 /// queue, because the pending queue was full.
419 ///
420 /// This is a real loss: the frame reached nobody, and only a copy behind
421 /// it or the sender's own retransmission will carry it now. A rising value
422 /// means forwards are arriving faster than this device can transmit them,
423 /// so the queue is the thing to raise, or the rate ceiling that is filling
424 /// it — see [`Self::rate_deferred`].
425 pub refused_queue_full: u64,
426 /// Transmissions held back because this device was at its forwarding rate.
427 ///
428 /// A frame counted here is delayed, not dropped: it goes back on the queue
429 /// and is tried again on the next tick, until it is either sent or has
430 /// waited so long past its turn that carrying it no longer helps.
431 pub rate_deferred: u64,
432 /// Queued forwards given up on after waiting too long past their due time.
433 ///
434 /// The other end of [`Self::rate_deferred`]: this is where a frame that
435 /// kept being delayed finally stops being carried. Deferral is free to
436 /// look healthy while this climbs, so it is the pair that says whether
437 /// back-pressure is costing anything.
438 pub abandoned_overdue: u64,
439 /// Frames that had travelled as far as they were allowed to.
440 pub hop_limit_reached: u64,
441 /// Frames whose claimed reach was cut down to local policy.
442 pub reach_clamped: u64,
443 /// Ids forgotten for lack of room rather than age.
444 ///
445 /// Expected to stay at zero. A non-zero value means this device is seeing
446 /// more traffic than it can remember having handled, and a frame it forgets
447 /// can be forwarded a second time.
448 pub dropped_for_capacity: u64,
449}
450
451/// A refilling allowance, in frames.
452#[derive(Debug, Clone)]
453struct TokenBucket {
454 tokens: f32,
455 capacity: f32,
456 refill_per_sec: f32,
457 last_refill: Instant,
458}
459
460impl TokenBucket {
461 fn new(capacity: f32, refill_per_sec: f32, now: Instant) -> Self {
462 Self {
463 tokens: capacity.max(0.0),
464 capacity: capacity.max(0.0),
465 refill_per_sec: refill_per_sec.max(0.0),
466 last_refill: now,
467 }
468 }
469
470 fn refill(&mut self, now: Instant) {
471 let elapsed = now
472 .saturating_duration_since(self.last_refill)
473 .as_secs_f32();
474 if elapsed > 0.0 {
475 self.tokens = (self.tokens + elapsed * self.refill_per_sec).min(self.capacity);
476 self.last_refill = now;
477 }
478 }
479
480 /// Re-sizes the allowance in place, keeping what has been spent spent.
481 ///
482 /// Refills against the *old* rate first, so the time already elapsed is
483 /// credited at the terms that applied to it, then clamps the level to the
484 /// new capacity. Clamping is what makes this safe to call every tick: a
485 /// device whose bias improves gets a larger bucket to refill into, and one
486 /// whose bias worsens gets a smaller one immediately, but neither is handed
487 /// tokens it did not earn — otherwise a value oscillating around a
488 /// threshold would mint a fresh burst on every crossing.
489 fn resize(&mut self, capacity: f32, refill_per_sec: f32, now: Instant) {
490 self.refill(now);
491 self.capacity = capacity.max(0.0);
492 self.refill_per_sec = refill_per_sec.max(0.0);
493 self.tokens = self.tokens.min(self.capacity);
494 }
495
496 /// Spends a token, but only while more than `floor` would remain
497 /// untouched. A `floor` of zero spends down to empty.
498 fn try_take_above(&mut self, floor: f32, now: Instant) -> bool {
499 self.refill(now);
500 if self.tokens >= 1.0 + floor {
501 self.tokens -= 1.0;
502 true
503 } else {
504 false
505 }
506 }
507
508 /// Whether a token could be spent right now leaving `floor` untouched,
509 /// without spending it.
510 ///
511 /// For a caller that must satisfy two meters at once and may spend neither
512 /// unless both allow it.
513 fn has_above(&mut self, floor: f32, now: Instant) -> bool {
514 self.refill(now);
515 self.tokens >= 1.0 + floor
516 }
517
518 /// Spends a token already confirmed available by [`Self::has_above`].
519 fn spend(&mut self) {
520 self.tokens = (self.tokens - 1.0).max(0.0);
521 }
522}
523
524/// Decides what gets forwarded, when, and how fast.
525///
526/// Holds no transports and sends nothing itself: it answers questions and hands
527/// back due work, so its behavior is testable without a radio and its policy
528/// stays in one place rather than spread through the receive path.
529#[derive(Debug)]
530pub struct MeshRelayGovernor {
531 config: MeshRelayConfig,
532 seen: RelaySeenCache,
533 /// Forwards awaiting their delay, in the order they were queued.
534 pending: Vec<PendingRelay>,
535 /// This device's whole airtime ceiling — every frame it transmits, its own
536 /// included. Never scaled by capability bias: it is what the device is
537 /// allowed to say, and bias is about how much of that it spends on other
538 /// people.
539 send_budget: TokenBucket,
540 /// The share of [`Self::send_budget`] capability bias leaves for forwarding.
541 ///
542 /// A second meter rather than a smaller shared bucket, and the distinction
543 /// is the whole point: scaling the shared bucket would throttle this
544 /// device's *own* messages and acknowledgements in step with its battery,
545 /// which is delivery rather than redundancy. Forwarding must satisfy both
546 /// meters; own traffic answers only to the shared one. At full capability
547 /// this one matches the shared bucket and never binds first, so a capable
548 /// device behaves exactly as it did before bias existed.
549 forward_budget: TokenBucket,
550 /// Tokens of [`Self::send_budget`] that only this device's own frames may
551 /// spend. Clamped to half the burst so a small configured burst cannot
552 /// reserve the whole bucket and stop forwarding outright.
553 own_reserve: f32,
554 peer_budgets: HashMap<String, TokenBucket>,
555 counters: MeshRelayCounters,
556 /// Seeds the per-frame delay so two nodes holding the same frame pick
557 /// different delays.
558 local_id: String,
559 /// This device's condition, as last reported by the host.
560 conditions: RelayConditions,
561 /// Whether this device currently reads as carrying traffic for others.
562 active_relay: bool,
563 /// Start of the activity window in progress.
564 window_started_at: Instant,
565 /// Frames carried for other people within the window in progress.
566 window_forwards: u64,
567 /// Consecutive completed windows in which nothing was carried.
568 idle_windows: u32,
569}
570
571impl MeshRelayGovernor {
572 /// Creates a governor with explicit tunables.
573 pub fn with_config(local_id: impl Into<String>, config: MeshRelayConfig) -> Self {
574 let now = Instant::now();
575 let seen = RelaySeenCache::with_config(config.seen.clone());
576 Self {
577 send_budget: TokenBucket::new(config.burst, config.rate_per_sec, now),
578 forward_budget: TokenBucket::new(config.burst, config.rate_per_sec, now),
579 own_reserve: RELAY_OWN_TRAFFIC_RESERVE.min(config.burst.max(0.0) / 2.0),
580 seen,
581 pending: Vec::new(),
582 peer_budgets: HashMap::new(),
583 counters: MeshRelayCounters::default(),
584 local_id: local_id.into(),
585 conditions: RelayConditions::default(),
586 active_relay: false,
587 window_started_at: now,
588 window_forwards: 0,
589 idle_windows: 0,
590 config,
591 }
592 }
593
594 /// Re-points the governor at this device's real identity once it is known.
595 ///
596 /// The governor is built before `initialize_mls` derives the address, so it
597 /// starts out seeded with the profile. Both uses are hashes — fan-out
598 /// spread and send jitter — so the change is only a reseed: it shifts which
599 /// frames this node prefers, never whether a frame is relayed.
600 pub fn set_local_id(&mut self, local_id: impl Into<String>) {
601 self.local_id = local_id.into();
602 }
603
604 /// The tunables actually in force.
605 ///
606 /// This is the governor's own snapshot, taken at construction, and it is
607 /// what every forwarding decision reads. Reporting it rather than
608 /// `ProtocolConfig::mesh_relay` keeps the answer honest: the two agree
609 /// today only because nothing can update the section after construction,
610 /// and a reader that trusted the config copy would start lying the moment
611 /// that changed.
612 pub fn config(&self) -> &MeshRelayConfig {
613 &self.config
614 }
615
616 /// Reports this device's condition, which scales how eagerly it forwards.
617 ///
618 /// Called from the process tick with the host's battery feed. Everything it
619 /// changes is continuous — delay, fan-out, refill rate — so there is no
620 /// value of any argument that stops this device forwarding. Whether it may
621 /// forward at all is the caller's gate (`RelayConfig::allow_relay` and the
622 /// battery floor), deliberately kept out of here so the two decisions
623 /// cannot half-apply.
624 ///
625 /// `battery` of `None` means the host has reported nothing, which is
626 /// treated as fully capable for the same reason the forwarding gate treats
627 /// it as willing: most platforms report a level, and penalising the ones
628 /// that do not would quietly thin the network on those devices.
629 ///
630 /// Applied to the forwarding budget immediately rather than on next use, so
631 /// a device that has just been unplugged stops refilling at the plugged-in
632 /// rate this tick.
633 ///
634 /// Only the *forwarding* share is scaled. This device's own airtime ceiling
635 /// is left alone, so its messages and acknowledgements go out at the same
636 /// rate on a dying phone as on a charging laptop — bias decides how much of
637 /// the room's traffic it carries, never how well it can speak for itself.
638 pub fn set_conditions(
639 &mut self,
640 battery: Option<u8>,
641 is_charging: bool,
642 eager: bool,
643 soft_floor: u8,
644 ) {
645 self.conditions = RelayConditions {
646 battery,
647 is_charging,
648 eager,
649 soft_floor,
650 };
651 let scale = self.bias_scale();
652 self.forward_budget.resize(
653 self.config.burst * scale,
654 self.config.rate_per_sec * scale,
655 Instant::now(),
656 );
657 }
658
659 /// How much of the full forwarding effort this device is currently willing
660 /// to spend, in `[bias_min_scale, 1.0]`.
661 ///
662 /// Fully capable when charging, when configured to relay eagerly, or when
663 /// no battery reading exists. Otherwise it ramps linearly from the soft
664 /// floor — the level at which forwarding would stop altogether — up to a
665 /// full battery, so the scale is a measure of headroom above the floor
666 /// rather than of the raw percentage.
667 fn bias_scale(&self) -> f32 {
668 let min_scale = self.config.bias_min_scale.clamp(0.0, 1.0);
669 if self.conditions.is_charging || self.conditions.eager {
670 return 1.0;
671 }
672 let Some(level) = self.conditions.battery else {
673 return 1.0;
674 };
675 let floor = self.conditions.soft_floor.min(100);
676 if level >= 100 {
677 return 1.0;
678 }
679 if level <= floor {
680 return min_scale;
681 }
682 let headroom = (level - floor) as f32 / (100 - floor).max(1) as f32;
683 min_scale + headroom * (1.0 - min_scale)
684 }
685
686 /// Whether this device currently reads as carrying traffic for other
687 /// people.
688 ///
689 /// Derived from what it has actually forwarded, not from what its battery
690 /// and neighbor count suggest it could — see [`Self::observe_activity`].
691 pub fn is_active_relay(&self) -> bool {
692 self.active_relay
693 }
694
695 /// Rolls the activity window and reports a change in relay activity.
696 ///
697 /// Answers "is this device carrying other people's traffic" from the only
698 /// evidence that cannot be wrong about it: whether it has. A device becomes
699 /// a relay by having carried [`MeshRelayConfig::activity_min_forwards`]
700 /// frames within one window, and stops being one after
701 /// [`MeshRelayConfig::activity_idle_windows`] consecutive windows carrying
702 /// none.
703 ///
704 /// Returns `Some` only at the tick the answer changes, so the caller emits
705 /// one event per transition. Cheap enough for every tick: it does nothing
706 /// at all until a window has elapsed.
707 pub fn observe_activity(&mut self, now: Instant) -> Option<RelayActivity> {
708 if now.saturating_duration_since(self.window_started_at) < self.config.activity_window {
709 return None;
710 }
711
712 let forwarded = std::mem::take(&mut self.window_forwards);
713 self.window_started_at = now;
714
715 if forwarded >= self.config.activity_min_forwards.max(1) {
716 self.idle_windows = 0;
717 if !self.active_relay {
718 self.active_relay = true;
719 return Some(RelayActivity::Began { forwarded });
720 }
721 return None;
722 }
723
724 if !self.active_relay {
725 return None;
726 }
727
728 self.idle_windows = self.idle_windows.saturating_add(1);
729 if self.idle_windows >= self.config.activity_idle_windows.max(1) {
730 self.active_relay = false;
731 return Some(RelayActivity::Ceased);
732 }
733 None
734 }
735
736 /// Drops the relay standing immediately, reporting whether it was held.
737 ///
738 /// For the caller's own gates closing — relaying switched off in
739 /// configuration, or the battery falling through the forwarding floor.
740 /// Those stop forwarding outright, so waiting out the idle windows would
741 /// leave this device reported as a relay for a minute after it had stopped
742 /// being one; the activity window is a measure of quiet, not of refusal.
743 pub fn force_inactive(&mut self) -> bool {
744 self.window_forwards = 0;
745 self.idle_windows = 0;
746 std::mem::replace(&mut self.active_relay, false)
747 }
748
749 /// Running totals.
750 pub fn counters(&self) -> &MeshRelayCounters {
751 &self.counters
752 }
753
754 /// Forwards awaiting transmission.
755 pub fn pending_len(&self) -> usize {
756 self.pending.len()
757 }
758
759 /// Ids dropped from the suppression cache for capacity rather than age.
760 /// Expected to stay zero; see [`RelaySeenCache::capacity_evictions`].
761 pub fn seen_capacity_evictions(&self) -> u64 {
762 self.seen.capacity_evictions()
763 }
764
765 /// Offers an inbound third-party frame for forwarding.
766 ///
767 /// Runs the full admission sequence and, when the frame is accepted,
768 /// queues an adjusted copy for later transmission. `degree` is the current
769 /// neighbor count, which sets both the hop budget and the delay spread.
770 pub fn admit(
771 &mut self,
772 message: &Message,
773 arrival_peer: Option<&str>,
774 degree: usize,
775 ) -> RelayAdmission {
776 let now = Instant::now();
777 let message_id = message.id.as_str();
778
779 // Has this id been dealt with? Checked without recording, because a
780 // frame that is about to be *refused* must not be remembered as
781 // handled: another copy of it arriving over a healthy link a moment
782 // later is the alternate path that should still carry it. Recording on
783 // rejection would leave the id suppressed for the whole retention
784 // window, so a single link running hot — or one frame with its hop
785 // budget rewritten to nothing, which nothing here can authenticate —
786 // would blank every path through this device.
787 if self.seen.contains(&message_id) {
788 self.counters.duplicates_suppressed =
789 self.counters.duplicates_suppressed.saturating_add(1);
790
791 // Our own copy is still waiting, so a neighbor has transmitted
792 // this frame already. It tells us one thing for certain: the
793 // neighbor that handed it to us holds it. So that neighbor comes
794 // off our fan-out, and nothing else changes.
795 //
796 // It does not tell us that the region is covered. Every carrier
797 // here forwards by unicast to a few chosen neighbors, not by
798 // broadcast, so a neighbor's transmission reached only the links
799 // it picked. Cancelling on a duplicate assumed otherwise, and in a
800 // cluster where everyone hears everyone it could stand down the
801 // one device adjacent to the only way out, after two copies
802 // reached it from neighbors that never picked that way. The frame
803 // then died inside the cluster with its id suppressed on every
804 // member, so the sender's retries could not rescue it (#510). A
805 // forward is dropped only once every neighbor is known to hold
806 // the frame, which the flush decides against the live neighbor
807 // set.
808 if let Some(pending) = self
809 .pending
810 .iter_mut()
811 .find(|p| p.message.id.as_str() == message_id)
812 {
813 if let Some(peer) = arrival_peer {
814 if pending.arrival_peer.as_deref() != Some(peer)
815 && !pending.heard_from.iter().any(|p| p == peer)
816 {
817 pending.heard_from.push(peer.to_string());
818 }
819 }
820 return RelayAdmission::Rejected(RelayRejection::NarrowedByDuplicate);
821 }
822
823 return RelayAdmission::Rejected(RelayRejection::AlreadySeen);
824 }
825
826 // Per-neighbor admission, before the frame can occupy queue space.
827 //
828 // A frame whose arrival link the carrier did not identify skips this
829 // step: there is no link to charge it to, and inventing a shared bucket
830 // for "unknown" would let one such carrier throttle every other. Those
831 // frames are still bounded by the queue capacity and the per-second
832 // send budget below, which is what makes skipping it acceptable — the
833 // per-neighbor limit shapes *fairness between links*, and a frame with
834 // no known link has no share to exceed.
835 if let Some(peer) = arrival_peer {
836 if !self.take_peer_token(peer, now) {
837 self.counters.peer_rate_limited = self.counters.peer_rate_limited.saturating_add(1);
838 return RelayAdmission::Rejected(RelayRejection::PeerRateLimited);
839 }
840 }
841
842 // The deposit request, read before the hop rewrite strips it from the
843 // copy that travels on. Only the drop point acts on it.
844 let custody_request = message.metadata.get(CUSTODY_META_KEY).cloned();
845
846 // Hop accounting. The arriving budget is clamped to what our own policy
847 // would have issued before it is spent, so an inflated claim buys at
848 // most this one hop.
849 let Some(forwarded) = self.prepare_hop(message, degree) else {
850 self.counters.hop_limit_reached = self.counters.hop_limit_reached.saturating_add(1);
851 return RelayAdmission::Rejected(RelayRejection::HopLimitReached);
852 };
853
854 if self.pending.len() >= self.config.queue_capacity && !self.evict_for(&forwarded, now) {
855 self.counters.queue_full = self.counters.queue_full.saturating_add(1);
856 return RelayAdmission::Rejected(RelayRejection::QueueFull);
857 }
858
859 // Accepted: now it is genuinely handled, and further copies can be
860 // suppressed. Recording here rather than at the top also means the
861 // cache fills at the rate frames are *taken on*, which the per-neighbor
862 // and per-device limits bound — not at the rate they arrive, which
863 // nothing bounds.
864 self.seen.observe(&message_id);
865
866 let due_at = now + self.jitter_for(&message_id, degree);
867 self.pending.push(PendingRelay {
868 message: forwarded,
869 arrival_peer: arrival_peer.map(str::to_string),
870 heard_from: Vec::new(),
871 due_at,
872 custody_request,
873 custody_target: None,
874 });
875 self.counters.queued = self.counters.queued.saturating_add(1);
876
877 RelayAdmission::Queued
878 }
879
880 /// Queues a held frame for redelivery toward one neighbor: the dedicated
881 /// intake custody uses instead of [`Self::admit`]
882 /// (`docs/spec/custody.md`, "Redelivery").
883 ///
884 /// The ordinary intake would refuse or mangle a held frame in three
885 /// ways, and this one skips exactly those: it does not consult the
886 /// suppression cache, which would refuse a second re-origination toward
887 /// another neighbor within the cache's window; it does not spend a hop,
888 /// because the custodian is the same forwarder it was when the frame
889 /// arrived and the frame goes out with the hop fields it was stored
890 /// with; and it does not charge the arrival peer's rate hours after that
891 /// peer sent anything. It still takes the queue capacity (evicting a
892 /// lower-priority forward as `admit` would) and the per-neighbor rate
893 /// toward the target, and the send budget is checked at release like
894 /// every other forward.
895 ///
896 /// Nothing is recorded in the suppression cache, so a refusal here and
897 /// an abandonment later both leave the cache exactly as they found it.
898 /// Refusals are not counted: the frame is still held, and the caller
899 /// tries again when the neighbor next appears.
900 pub fn admit_held(
901 &mut self,
902 message: Message,
903 target: &str,
904 now: Instant,
905 ) -> Result<(), RelayRejection> {
906 if !self.take_peer_token(target, now) {
907 return Err(RelayRejection::PeerRateLimited);
908 }
909 if self.pending.len() >= self.config.queue_capacity && !self.evict_for(&message, now) {
910 return Err(RelayRejection::QueueFull);
911 }
912 self.pending.push(PendingRelay {
913 message,
914 arrival_peer: None,
915 heard_from: Vec::new(),
916 due_at: now,
917 custody_request: None,
918 custody_target: Some(target.to_string()),
919 });
920 Ok(())
921 }
922
923 /// Returns the forwards whose delay has elapsed, removing them from the
924 /// queue.
925 ///
926 /// Frames whose delay has not elapsed stay queued, as do frames the device
927 /// has no budget left to transmit. A frame that has waited far past its
928 /// turn is abandoned instead: by then other paths have carried it or the
929 /// sender has retransmitted, and holding it only displaces newer traffic.
930 ///
931 /// Budget is *checked* here and *spent* in [`Self::take_send_token`], once
932 /// per link a frame actually crosses. Spending it here instead would
933 /// undercount by the fan-out — one release from this queue can put the
934 /// frame on several links — and the whole point of the ceiling is that it
935 /// bounds what reaches the radio.
936 ///
937 /// The check is therefore not a reservation, and cannot be: a released
938 /// frame may still find the budget gone by the time it reaches the radio,
939 /// because the frames released alongside it spend from the same bucket. A
940 /// caller that gets nothing onto a link for that reason must hand the frame
941 /// back with [`Self::requeue`] rather than drop it — the id is already
942 /// recorded as handled here, so dropping it would lose this copy *and*
943 /// refuse the copies and retransmissions that follow.
944 #[cfg(test)]
945 pub fn take_due(&mut self, now: Instant) -> Vec<PendingRelay> {
946 self.release_due(now).0
947 }
948
949 /// [`Self::take_due`], also handing back the forwards it abandoned.
950 ///
951 /// The second list is the drop point (`docs/spec/custody.md`, "What a
952 /// custodian does on arrival"). An ordinary forward in it waited past
953 /// [`RELAY_QUEUE_MAX_OVERDUE`] without reaching a link, its id has
954 /// already been released from the suppression cache here, and custody
955 /// may take it on. A marked held forward (`custody_target` set) in it is
956 /// returned to the custody store by the caller; this never touches the
957 /// suppression cache for one, because the held intake never recorded its
958 /// id there, and releasing it would at best be a no-op and at worst
959 /// release an entry the depositor's own retransmission legitimately holds.
960 pub fn release_due(&mut self, now: Instant) -> (Vec<PendingRelay>, Vec<PendingRelay>) {
961 self.seen.expire(now);
962
963 let mut due = Vec::new();
964 let mut abandoned = Vec::new();
965 let mut keep: Vec<PendingRelay> = Vec::with_capacity(self.pending.len());
966
967 for relay in std::mem::take(&mut self.pending) {
968 if relay.due_at > now {
969 keep.push(relay);
970 continue;
971 }
972
973 if now.saturating_duration_since(relay.due_at) > RELAY_QUEUE_MAX_OVERDUE {
974 if relay.custody_target.is_none() {
975 // Never made it onto a link, so its id must not stay
976 // suppressed: the sender's retransmissions carry the same
977 // id, and refusing them would close this route for the
978 // whole retention window over a few seconds of
979 // congestion.
980 self.seen.forget(&relay.message.id.as_str());
981 self.counters.abandoned_overdue =
982 self.counters.abandoned_overdue.saturating_add(1);
983 }
984 abandoned.push(relay);
985 continue;
986 }
987
988 if self.has_send_budget(now) {
989 due.push(relay);
990 } else {
991 self.counters.rate_deferred = self.counters.rate_deferred.saturating_add(1);
992 keep.push(relay);
993 }
994 }
995
996 self.pending = keep;
997 (due, abandoned)
998 }
999
1000 /// Whether any budget remains to forward right now.
1001 ///
1002 /// Measured against both meters [`Self::take_send_token`] must satisfy, so
1003 /// `take_due` does not release a batch the flush is about to refuse.
1004 fn has_send_budget(&mut self, now: Instant) -> bool {
1005 self.send_budget.has_above(self.own_reserve, now) && self.forward_budget.has_above(0.0, now)
1006 }
1007
1008 /// Claims budget for putting one **forwarded** frame on one link.
1009 ///
1010 /// Satisfies two meters. The shared ceiling, stopping short of the
1011 /// own-traffic reserve so this device can still send its own frames while
1012 /// forwarding at its limit (see [`RELAY_OWN_TRAFFIC_RESERVE`] and
1013 /// [`Self::take_own_send_token`]); and the capability-scaled forwarding
1014 /// share, which is how bias reduces what a weak device carries without
1015 /// touching what it can say for itself.
1016 ///
1017 /// Neither is spent unless both allow it, so a refusal by one cannot leak
1018 /// a token out of the other.
1019 pub fn take_send_token(&mut self) -> bool {
1020 let now = Instant::now();
1021 if !self.has_send_budget(now) {
1022 self.counters.rate_deferred = self.counters.rate_deferred.saturating_add(1);
1023 return false;
1024 }
1025 self.send_budget.spend();
1026 self.forward_budget.spend();
1027 self.counters.transmissions = self.counters.transmissions.saturating_add(1);
1028 true
1029 }
1030
1031 /// Claims budget for putting one of **this device's own** frames on one
1032 /// link.
1033 ///
1034 /// Metered against the same ceiling — it is the same radio — but allowed
1035 /// into the reserve, so a device forwarding at its limit can still get its
1036 /// own messages and acknowledgements out.
1037 ///
1038 /// Untouched by capability bias, which lives on the forwarding meter alone:
1039 /// a device at 10% battery hands its own messages to the mesh exactly as
1040 /// fast as a charging one, and only its share of everyone else's traffic
1041 /// shrinks.
1042 pub fn take_own_send_token(&mut self) -> bool {
1043 self.claim_transmission(0.0)
1044 }
1045
1046 fn claim_transmission(&mut self, floor: f32) -> bool {
1047 if self.send_budget.try_take_above(floor, Instant::now()) {
1048 self.counters.transmissions = self.counters.transmissions.saturating_add(1);
1049 true
1050 } else {
1051 self.counters.rate_deferred = self.counters.rate_deferred.saturating_add(1);
1052 false
1053 }
1054 }
1055
1056 /// Records that a third-party frame was carried onward.
1057 ///
1058 /// Separate from [`Self::take_send_token`], which meters airtime for every
1059 /// frame this device transmits including its own. Counted once per frame
1060 /// carried rather than once per link, so it reads as "messages I moved for
1061 /// other people".
1062 pub fn record_forwarded(&mut self) {
1063 self.counters.forwarded = self.counters.forwarded.saturating_add(1);
1064 self.window_forwards = self.window_forwards.saturating_add(1);
1065 }
1066
1067 /// Puts a released forward back on the queue, keeping its original due
1068 /// time.
1069 ///
1070 /// For the frame that reached no neighbor at all between [`Self::take_due`]
1071 /// releasing it and the radio being asked — because the budget ran out,
1072 /// because every link picked for it had gone away, or because there was no
1073 /// usable link to pick. All three end the same way: the frame has travelled
1074 /// nowhere, its id is already recorded as handled, and dropping it would
1075 /// refuse the copies and retransmissions behind it for the whole retention
1076 /// window.
1077 ///
1078 /// It keeps its due time deliberately: the overdue cut-off in `take_due` is
1079 /// measured from that, so a frame that stays stuck is eventually abandoned
1080 /// instead of being retried forever.
1081 ///
1082 /// Refused only if the queue has filled meanwhile, which is the same
1083 /// bound every other queued frame is subject to. A marked held forward
1084 /// refused room is handed back rather than dropped: the custody store
1085 /// still holds the frame, so nothing is lost, and its id was never
1086 /// recorded in the suppression cache, so nothing is released. The
1087 /// caller returns it to the store; `None` means the frame was requeued
1088 /// or, for an ordinary forward, dropped.
1089 pub fn requeue(&mut self, relay: PendingRelay) -> Option<PendingRelay> {
1090 if self.pending.len() >= self.config.queue_capacity {
1091 if relay.custody_target.is_some() {
1092 return Some(relay);
1093 }
1094 // Refused room on the way back, so this frame is being dropped
1095 // having reached nobody. Release its id for the same reason the
1096 // overdue cut-off does — a copy behind it, or the sender's own
1097 // retransmission, is now the only way it travels.
1098 self.seen.forget(&relay.message.id.as_str());
1099 self.counters.queue_full = self.counters.queue_full.saturating_add(1);
1100 return None;
1101 }
1102 self.counters.requeued = self.counters.requeued.saturating_add(1);
1103 self.pending.push(relay);
1104 None
1105 }
1106
1107 /// Drops a released forward because every neighbor it could go to has
1108 /// already handed us a copy.
1109 ///
1110 /// The only way a duplicate ends a forward. Its id stays recorded as
1111 /// handled: the frame is not lost, every neighbor holds it, and the
1112 /// copies still arriving should keep being absorbed rather than taken on
1113 /// again.
1114 pub fn drop_covered(&mut self, relay: PendingRelay) {
1115 debug_assert!(relay.custody_target.is_none());
1116 self.counters.cancelled_by_duplicate =
1117 self.counters.cancelled_by_duplicate.saturating_add(1);
1118 }
1119
1120 /// Drops every marked held forward from the queue, leaving the ordinary
1121 /// ones. For the custody erase: a frame queued for redelivery seconds
1122 /// before the erase must not go out after it.
1123 pub fn drop_held(&mut self) {
1124 self.pending.retain(|relay| relay.custody_target.is_none());
1125 }
1126
1127 /// Whether `message_id` is recorded as handled in the suppression cache.
1128 ///
1129 /// For the tests that pin custody and the cache disjoint: a custodian
1130 /// that both held a frame and suppressed its own forwarding of the
1131 /// depositor's retransmissions would be a black hole on exactly the
1132 /// route now known to be slow.
1133 #[cfg(test)]
1134 pub fn is_suppressed(&self, message_id: &str) -> bool {
1135 self.seen.contains(message_id)
1136 }
1137
1138 /// Records an id as handled without queueing a forward for it.
1139 ///
1140 /// Used for frames this device **originates** and hands to its neighbors:
1141 /// a copy circling back to us through the mesh is then recognized rather
1142 /// than carried onward as though it were someone else's.
1143 ///
1144 /// Deliberately not called for frames addressed to us. A later copy of one
1145 /// of those is still addressed to us, so it never reaches the forwarding
1146 /// path at all — recording it would only fill the cache at delivery rate,
1147 /// which the capacity is not sized for and which would make
1148 /// [`Self::seen_capacity_evictions`] climb for reasons unrelated to relay
1149 /// load.
1150 pub fn mark_handled(&mut self, message_id: &str) {
1151 self.seen.observe(message_id);
1152 }
1153
1154 /// Absorbs an arrival that is a copy of a frame already dealt with and
1155 /// needs no further decision, reporting whether it did.
1156 ///
1157 /// This is [`Self::admit`]'s suppression check, split out so a caller can
1158 /// reach it without first assembling the arguments `admit` needs. In a
1159 /// dense neighborhood most third-party arrivals are copies, and the caller's
1160 /// `degree` costs a status snapshot across every transport, work the
1161 /// suppression check would only throw away.
1162 ///
1163 /// Deliberately answers `false` while we still hold a pending copy of the
1164 /// id, even though that is also a duplicate: the copy names a neighbor
1165 /// that now holds the frame, which narrows our pending forward, so it has
1166 /// to go through [`Self::admit`]. Those are rare, since the pending window
1167 /// is one jitter delay wide.
1168 pub fn absorb_settled_duplicate(&mut self, message_id: &str) -> bool {
1169 if !self.seen.contains(message_id) {
1170 return false;
1171 }
1172 if self
1173 .pending
1174 .iter()
1175 .any(|p| p.message.id.as_str() == message_id)
1176 {
1177 return false;
1178 }
1179 self.counters.duplicates_suppressed = self.counters.duplicates_suppressed.saturating_add(1);
1180 true
1181 }
1182
1183 /// Neighbors a **forwarded** frame should be carried to, given the current
1184 /// neighbor set and the peers it must not go back to.
1185 ///
1186 /// Excludes the neighbor it arrived from and the peer that wrote it —
1187 /// both already have it, and returning it is how a frame ends up bouncing
1188 /// between two nodes.
1189 ///
1190 /// Capability-biased: a weaker device spends fewer of its neighbors on
1191 /// other people's traffic. That is safe here precisely because the frame is
1192 /// someone else's — neighbors hold copies of it and its sender is still
1193 /// retrying, so a narrower fan-out costs redundancy. Frames this device
1194 /// originated have neither, and go through
1195 /// [`Self::select_origination_targets`] instead.
1196 pub fn select_targets<'a>(
1197 &self,
1198 neighbors: impl IntoIterator<Item = (&'a str, u8)>,
1199 exclude: &[&str],
1200 message_id: &str,
1201 ) -> Vec<String> {
1202 self.select_within_fanout(neighbors, exclude, message_id, self.biased_fanout())
1203 }
1204
1205 /// Neighbors one of **this device's own** frames should be handed to when
1206 /// no transport reaches the recipient directly.
1207 ///
1208 /// Deliberately *not* capability-biased, for the same reason the jitter
1209 /// handicap skips this path (see [`Self::jitter_for`]): nobody else is
1210 /// holding this frame. For a forward, a narrower fan-out drops paths that
1211 /// neighbors' copies duplicate; here the fan-out *is* the delivery attempt,
1212 /// so narrowing it would cost delivery rather than redundancy — and because
1213 /// target choice is stable per message id, every retransmission would funnel
1214 /// into the same reduced set rather than eventually trying another.
1215 ///
1216 /// Bias still reaches this device's own traffic through the send budget's
1217 /// forwarding share, which is the one dial that cannot mistake the two: see
1218 /// [`Self::take_own_send_token`].
1219 pub fn select_origination_targets<'a>(
1220 &self,
1221 neighbors: impl IntoIterator<Item = (&'a str, u8)>,
1222 exclude: &[&str],
1223 message_id: &str,
1224 ) -> Vec<String> {
1225 self.select_within_fanout(neighbors, exclude, message_id, self.config.fanout.max(1))
1226 }
1227
1228 /// The fan-out width for other people's traffic.
1229 ///
1230 /// Never fewer than one: a fan-out of zero is not a cheaper forward, it is
1231 /// a silent drop, and this device has already taken the frame on.
1232 fn biased_fanout(&self) -> usize {
1233 ((self.config.fanout as f32 * self.bias_scale()).round() as usize).max(1)
1234 }
1235
1236 fn select_within_fanout<'a>(
1237 &self,
1238 neighbors: impl IntoIterator<Item = (&'a str, u8)>,
1239 exclude: &[&str],
1240 message_id: &str,
1241 fanout: usize,
1242 ) -> Vec<String> {
1243 let mut candidates: Vec<(&str, u8)> = neighbors
1244 .into_iter()
1245 .filter(|(peer, _)| !exclude.contains(peer))
1246 .collect();
1247
1248 if candidates.len() <= fanout {
1249 return candidates
1250 .into_iter()
1251 .map(|(peer, _)| peer.to_string())
1252 .collect();
1253 }
1254
1255 // Strongest links first: a fan-out slot spent on a barely-connected
1256 // neighbor is one the frame probably does not survive, and there are
1257 // only a few slots.
1258 //
1259 // Ties break on a hash of (id, peer, self), which does two things.
1260 // Within a group of equally good links it spreads different messages
1261 // across different neighbors rather than pinning every frame to the
1262 // same one. And it is stable per frame: a second copy of the same
1263 // message picks the same neighbors instead of a fresh set, so a
1264 // retransmission does not widen its own footprint.
1265 candidates.sort_by(|(a_peer, a_quality), (b_peer, b_quality)| {
1266 b_quality.cmp(a_quality).then_with(|| {
1267 Self::hash_of(&[message_id, a_peer, &self.local_id]).cmp(&Self::hash_of(&[
1268 message_id,
1269 b_peer,
1270 &self.local_id,
1271 ]))
1272 })
1273 });
1274 candidates
1275 .into_iter()
1276 .take(fanout)
1277 .map(|(peer, _)| peer.to_string())
1278 .collect()
1279 }
1280
1281 /// Drops per-neighbor rate state for a peer that has gone away.
1282 pub fn forget_peer(&mut self, peer_id: &str) {
1283 self.peer_budgets.remove(peer_id);
1284 }
1285
1286 /// Releases expired suppression entries. Safe to call from a periodic tick.
1287 pub fn maintain(&mut self, now: Instant) {
1288 self.seen.expire(now);
1289 }
1290
1291 /// Builds the copy to forward, or `None` when the frame has no hop budget
1292 /// left.
1293 fn prepare_hop(&mut self, message: &Message, degree: usize) -> Option<Message> {
1294 if message.hop_count.value() >= RELAY_MAX_HOP_COUNT {
1295 return None;
1296 }
1297
1298 let ceiling = if degree >= self.config.dense_degree {
1299 self.config.dense_max_ttl
1300 } else {
1301 self.config.max_ttl
1302 };
1303
1304 let claimed = message.ttl.value();
1305 let effective = claimed.min(ceiling);
1306 if effective < claimed {
1307 self.counters.ttl_clamped = self.counters.ttl_clamped.saturating_add(1);
1308 }
1309
1310 // A budget of 1 or less is spent: this node is the last that may hold
1311 // the frame, matching `TTL::is_exhausted`.
1312 let remaining = offline_protocol_core::TTL::from_value(effective).decrement()?;
1313
1314 let mut forwarded = message.clone();
1315 // Rewrite rather than decrement in place: the frame leaves with a
1316 // budget this node vouches for, so an inflated claim cannot survive
1317 // past this hop.
1318 forwarded.ttl = remaining;
1319 let _ = forwarded.increment_hop();
1320 // Every device that transmits a third-party frame strips the deposit
1321 // request from the copy it transmits, custody-enabled or not
1322 // (`docs/spec/custody.md`, "What a forwarder does"). The key is
1323 // unsigned metadata outside the sealed body, so nothing else stops
1324 // it travelling on; left in place, a two-hop network deposits a
1325 // frame at a custodian that never saw its sender, and a zone holds
1326 // every frame everywhere. Only the outer metadata is touched, never
1327 // `content`.
1328 forwarded.metadata.remove(CUSTODY_META_KEY);
1329 Some(forwarded)
1330 }
1331
1332 /// Makes room for a higher-priority forward by displacing the
1333 /// lowest-priority one that is not yet due.
1334 ///
1335 /// Frames already past their due time are left alone: they are on their way
1336 /// to the radio this tick, so taking one costs a transmission that was
1337 /// about to happen while the frame replacing it still has to wait out its
1338 /// hold. Only when every lower-priority frame is already due does it
1339 /// displace one of those instead of refusing outright.
1340 fn evict_for(&mut self, candidate: &Message, now: Instant) -> bool {
1341 let candidate_rank = priority_rank(candidate.priority);
1342 let lower_priority = |p: &PendingRelay| priority_rank(p.message.priority) < candidate_rank;
1343
1344 let victim = self
1345 .pending
1346 .iter()
1347 .enumerate()
1348 .filter(|(_, p)| lower_priority(p) && p.due_at > now)
1349 .min_by_key(|(_, p)| priority_rank(p.message.priority))
1350 .map(|(index, _)| index)
1351 .or_else(|| {
1352 self.pending
1353 .iter()
1354 .enumerate()
1355 .filter(|(_, p)| lower_priority(p))
1356 .min_by_key(|(_, p)| priority_rank(p.message.priority))
1357 .map(|(index, _)| index)
1358 });
1359
1360 match victim {
1361 Some(index) => {
1362 let displaced = self.pending.remove(index);
1363 // Displaced before it ever reached a link. Its id goes back to
1364 // being unknown, so the copy behind it — or the sender's
1365 // retransmission — can still be carried by this device once
1366 // there is room again.
1367 self.seen.forget(&displaced.message.id.as_str());
1368 self.counters.queue_evicted = self.counters.queue_evicted.saturating_add(1);
1369 true
1370 }
1371 None => false,
1372 }
1373 }
1374
1375 fn take_peer_token(&mut self, peer_id: &str, now: Instant) -> bool {
1376 if !self.peer_budgets.contains_key(peer_id) {
1377 // Bound the map: an attacker cannot grow it with invented peer ids
1378 // because entries are only created for peers we hold a link to,
1379 // but churn over a long session can still accumulate.
1380 if self.peer_budgets.len() >= MAX_RELAY_RATE_PEERS {
1381 // Make room by forgetting the link that is spending least,
1382 // rather than by clearing the table. A wholesale reset hands
1383 // every neighbor a fresh burst — including the noisy one the
1384 // per-link limit exists to hold back, which is the entry it can
1385 // least afford to lose. The bucket that has recovered the most
1386 // tokens is the one whose absence changes the least: at full
1387 // capacity it is indistinguishable from a link never tracked.
1388 //
1389 // Costs a scan of the table, but only once the ceiling is
1390 // reached and only when a genuinely new link appears.
1391 for bucket in self.peer_budgets.values_mut() {
1392 bucket.refill(now);
1393 }
1394 let most_recovered = self
1395 .peer_budgets
1396 .iter()
1397 .max_by(|(_, a), (_, b)| a.tokens.total_cmp(&b.tokens))
1398 .map(|(peer, _)| peer.clone());
1399 if let Some(peer) = most_recovered {
1400 self.peer_budgets.remove(&peer);
1401 }
1402 }
1403 self.peer_budgets.insert(
1404 peer_id.to_string(),
1405 TokenBucket::new(self.config.peer_burst, self.config.peer_rate_per_sec, now),
1406 );
1407 }
1408
1409 self.peer_budgets
1410 .get_mut(peer_id)
1411 .map(|bucket| bucket.try_take_above(0.0, now))
1412 .unwrap_or(true)
1413 }
1414
1415 /// The delay before a queued forward is transmitted.
1416 ///
1417 /// Derived from the frame and this node's identity, so two nodes holding
1418 /// the same frame wait different amounts and the later one's fan-out is
1419 /// narrowed by the earlier one's copy.
1420 /// The spread widens with density, because that is where the contention is.
1421 ///
1422 /// Capability shifts the whole window later rather than widening it, so
1423 /// between two neighbors holding the same frame the capable one usually
1424 /// transmits first and the weaker one's fan-out loses it before anything is
1425 /// spent. That is the point: the saving is the link that is never crossed,
1426 /// not a cheaper crossing.
1427 ///
1428 /// How reliably it wins depends on density, and the handicap is a fixed
1429 /// constant precisely so it cannot chase it. Below [`dense_degree`] the
1430 /// span is narrower than the handicap, so the weak device's earliest delay
1431 /// is later than the capable one's latest and the ordering is absolute.
1432 /// Past that the span grows and the two windows overlap, leaving a
1433 /// probabilistic bias — the capable device still wins most frames, and the
1434 /// weak one's wait stays bounded by one handicap rather than growing with
1435 /// the room. Widening the handicap to restore the absolute ordering is
1436 /// exactly the shape that would push a weak device in a crowded room past
1437 /// [`RELAY_QUEUE_MAX_OVERDUE`], where forwards are abandoned rather than
1438 /// merely late.
1439 ///
1440 /// Frames this device originated are not delayed at all — see
1441 /// [`Self::select_origination_targets`] and the caller in `send.rs`.
1442 ///
1443 /// [`dense_degree`]: MeshRelayConfig::dense_degree
1444 fn jitter_for(&self, message_id: &str, degree: usize) -> Duration {
1445 let min_ms = self.config.jitter_min.as_millis() as u64;
1446 let max_ms = (self.config.jitter_max.as_millis() as u64).max(min_ms);
1447
1448 // More neighbors means more nodes racing to forward the same frame, so
1449 // give them more room to separate.
1450 let density_scale = 1 + (degree / self.config.dense_degree.max(1)) as u64;
1451 let span = (max_ms - min_ms).saturating_mul(density_scale).max(1);
1452
1453 // Bounded by a constant rather than by the span, which already grows
1454 // with density: compounding the two would push a weak device in a
1455 // crowded room past the overdue cut-off, turning late forwards into
1456 // abandoned ones.
1457 let handicap_ms = ((1.0 - self.bias_scale()).clamp(0.0, 1.0)
1458 * self.config.bias_max_handicap.as_millis() as f32) as u64;
1459
1460 let hash = Self::hash_of(&[message_id, &self.local_id]);
1461 Duration::from_millis(min_ms + handicap_ms + (hash % span))
1462 }
1463
1464 /// Stable hash used to spread delays and fan-out choices.
1465 ///
1466 /// `DefaultHasher` is unseeded, so this is predictable to anyone who knows
1467 /// the inputs — both of which (a message id and a user id) are visible on
1468 /// the wire. That is accepted: knowing it buys only the ability to guess
1469 /// which neighbor fires first or which three links a frame takes, and every
1470 /// path it could steer a frame down is already bounded by the fan-out cap
1471 /// and the send budget. It must stay *stable*, which a randomly-seeded
1472 /// hasher would not be — a second copy of a frame has to pick the same
1473 /// neighbors, or a retransmission widens its own footprint.
1474 fn hash_of(parts: &[&str]) -> u64 {
1475 let mut hasher = DefaultHasher::new();
1476 for part in parts {
1477 part.hash(&mut hasher);
1478 }
1479 hasher.finish()
1480 }
1481}
1482
1483fn priority_rank(priority: MessagePriority) -> u8 {
1484 match priority {
1485 MessagePriority::Low => 0,
1486 MessagePriority::Medium => 1,
1487 MessagePriority::High => 2,
1488 MessagePriority::Critical => 3,
1489 }
1490}
1491
1492#[cfg(test)]
1493mod tests {
1494 use super::*;
1495 use offline_protocol_core::{AppId, UserId, TTL};
1496
1497 /// Zero jitter so `admit` queues a forward that is immediately due, which
1498 /// keeps the tests deterministic without a clock abstraction.
1499 fn immediate_config() -> MeshRelayConfig {
1500 MeshRelayConfig {
1501 jitter_min: Duration::from_millis(0),
1502 jitter_max: Duration::from_millis(0),
1503 ..Default::default()
1504 }
1505 }
1506
1507 fn governor() -> MeshRelayGovernor {
1508 MeshRelayGovernor::with_config("relay-node", immediate_config())
1509 }
1510
1511 fn frame_with(ttl: u8, priority: MessagePriority) -> Message {
1512 Message::builder(
1513 UserId::new("alice").unwrap(),
1514 UserId::new("bob").unwrap(),
1515 AppId::new("app").unwrap(),
1516 )
1517 .content("hello")
1518 .priority(priority)
1519 .ttl(TTL::new(ttl).unwrap())
1520 .build()
1521 }
1522
1523 fn frame() -> Message {
1524 frame_with(8, MessagePriority::Medium)
1525 }
1526
1527 #[test]
1528 fn a_frame_is_forwarded_once_however_many_copies_arrive() {
1529 let mut gov = governor();
1530 let msg = frame();
1531
1532 assert_eq!(gov.admit(&msg, Some("b"), 3), RelayAdmission::Queued);
1533
1534 // Copies arriving after ours has gone out are suppressed, not forwarded.
1535 let due = gov.take_due(Instant::now());
1536 assert_eq!(due.len(), 1);
1537
1538 for _ in 0..5 {
1539 assert_eq!(
1540 gov.admit(&msg, Some("c"), 3),
1541 RelayAdmission::Rejected(RelayRejection::AlreadySeen)
1542 );
1543 }
1544 assert_eq!(gov.counters().queued, 1);
1545 assert_eq!(gov.counters().duplicates_suppressed, 5);
1546 }
1547
1548 #[test]
1549 fn a_copy_arriving_while_we_wait_narrows_our_forward_rather_than_cancelling_it() {
1550 // A copy proves only that the neighbor who sent it holds the frame.
1551 // Carriers forward by unicast to a few chosen neighbors, so it says
1552 // nothing about the rest of ours: the forward stays, minus that
1553 // neighbor.
1554 let mut gov = MeshRelayGovernor::with_config(
1555 "relay-node",
1556 MeshRelayConfig {
1557 jitter_min: Duration::from_millis(50),
1558 jitter_max: Duration::from_millis(200),
1559 ..Default::default()
1560 },
1561 );
1562 let msg = frame();
1563
1564 assert_eq!(gov.admit(&msg, Some("b"), 6), RelayAdmission::Queued);
1565 assert_eq!(
1566 gov.admit(&msg, Some("c"), 6),
1567 RelayAdmission::Rejected(RelayRejection::NarrowedByDuplicate)
1568 );
1569 // A second copy from the same neighbor, and one from the neighbor it
1570 // first came from, teach nothing new.
1571 gov.admit(&msg, Some("c"), 6);
1572 gov.admit(&msg, Some("b"), 6);
1573
1574 assert_eq!(gov.pending_len(), 1, "the forward must survive a copy");
1575 assert_eq!(gov.counters().cancelled_by_duplicate, 0);
1576 let due = gov.take_due(Instant::now() + Duration::from_secs(1));
1577 assert_eq!(due.len(), 1);
1578 assert_eq!(due[0].arrival_peer.as_deref(), Some("b"));
1579 assert_eq!(due[0].heard_from, vec!["c".to_string()]);
1580 }
1581
1582 #[test]
1583 fn a_forward_every_neighbor_already_holds_is_dropped_as_covered() {
1584 let mut gov = governor();
1585 let msg = frame();
1586 gov.admit(&msg, Some("b"), 3);
1587 gov.admit(&msg, Some("c"), 3);
1588 let relay = gov
1589 .take_due(Instant::now() + Duration::from_secs(1))
1590 .pop()
1591 .expect("due");
1592
1593 gov.drop_covered(relay);
1594
1595 assert_eq!(gov.counters().cancelled_by_duplicate, 1);
1596 assert!(
1597 gov.is_suppressed(&msg.id.as_str()),
1598 "a covered frame is held all around, so later copies stay absorbed"
1599 );
1600 }
1601
1602 #[test]
1603 fn an_inflated_hop_budget_is_cut_to_local_policy() {
1604 // Nothing authenticates the hop budget at this layer, so a frame can
1605 // claim any value. It must not buy more than one hop past our own
1606 // ceiling.
1607 let mut gov = governor();
1608 let msg = frame_with(255, MessagePriority::Medium);
1609
1610 assert_eq!(gov.admit(&msg, Some("b"), 2), RelayAdmission::Queued);
1611 let due = gov.take_due(Instant::now());
1612
1613 assert_eq!(due.len(), 1);
1614 assert_eq!(due[0].message.ttl.value(), DEFAULT_RELAY_MAX_TTL - 1);
1615 assert_eq!(gov.counters().ttl_clamped, 1);
1616 }
1617
1618 #[test]
1619 fn a_dense_neighborhood_uses_the_lower_hop_ceiling() {
1620 let mut gov = governor();
1621 let msg = frame_with(255, MessagePriority::Medium);
1622
1623 assert_eq!(
1624 gov.admit(&msg, Some("b"), DEFAULT_RELAY_DENSE_DEGREE),
1625 RelayAdmission::Queued
1626 );
1627 let due = gov.take_due(Instant::now());
1628 assert_eq!(due[0].message.ttl.value(), DEFAULT_RELAY_DENSE_MAX_TTL - 1);
1629 }
1630
1631 #[test]
1632 fn a_frame_out_of_hops_is_not_forwarded() {
1633 let mut gov = governor();
1634 // A budget of 1 is spent: this node is the last that may hold it.
1635 let msg = frame_with(1, MessagePriority::Medium);
1636
1637 assert_eq!(
1638 gov.admit(&msg, Some("b"), 3),
1639 RelayAdmission::Rejected(RelayRejection::HopLimitReached)
1640 );
1641 assert_eq!(gov.counters().hop_limit_reached, 1);
1642 }
1643
1644 #[test]
1645 fn the_last_hop_is_forwarded_and_the_one_after_it_is_not() {
1646 let mut gov = governor();
1647
1648 // Two hops left: goes out with one, which the next node will refuse to
1649 // forward. Verifies the budget lands exactly on `TTL::is_exhausted`
1650 // rather than a hop early or late.
1651 assert_eq!(
1652 gov.admit(&frame_with(2, MessagePriority::Medium), Some("b"), 3),
1653 RelayAdmission::Queued
1654 );
1655 let due = gov.take_due(Instant::now());
1656 assert_eq!(due[0].message.ttl.value(), 1);
1657 assert!(due[0].message.is_ttl_exhausted());
1658 }
1659
1660 #[test]
1661 fn the_hop_ceiling_stops_a_frame_that_evaded_the_budget() {
1662 let mut gov = governor();
1663 let mut msg = frame_with(255, MessagePriority::Medium);
1664 for _ in 0..RELAY_MAX_HOP_COUNT {
1665 let _ = msg.increment_hop();
1666 }
1667
1668 assert_eq!(
1669 gov.admit(&msg, Some("b"), 3),
1670 RelayAdmission::Rejected(RelayRejection::HopLimitReached)
1671 );
1672 }
1673
1674 #[test]
1675 fn one_noisy_neighbor_cannot_consume_the_whole_node() {
1676 let mut gov = governor();
1677
1678 // Spend the noisy neighbor's allowance on unique frames — the shape
1679 // suppression cannot help with, since every id is new.
1680 let mut accepted_from_noisy = 0;
1681 for _ in 0..200 {
1682 if gov.admit(&frame(), Some("noisy"), 3) == RelayAdmission::Queued {
1683 accepted_from_noisy += 1;
1684 }
1685 }
1686
1687 assert!(
1688 accepted_from_noisy <= DEFAULT_RELAY_PEER_BURST as usize + 2,
1689 "accepted {accepted_from_noisy} frames from one neighbor"
1690 );
1691 assert!(gov.counters().peer_rate_limited > 0);
1692
1693 // A different neighbor still gets through: the limit is per link, so
1694 // one bad actor degrades only itself.
1695 assert_eq!(
1696 gov.admit(&frame(), Some("quiet"), 3),
1697 RelayAdmission::Queued
1698 );
1699 }
1700
1701 #[test]
1702 fn transmission_never_exceeds_the_node_budget() {
1703 // The invariant the whole design rests on: whatever arrives, what the
1704 // radio is asked to send is capped. Asserted on transmissions rather
1705 // than on queue releases, because one release can put a frame on
1706 // several links and it is the links that cost airtime.
1707 let mut gov = MeshRelayGovernor::with_config(
1708 "relay-node",
1709 MeshRelayConfig {
1710 queue_capacity: 512,
1711 // Admission is not what is under test here.
1712 peer_rate_per_sec: 10_000.0,
1713 peer_burst: 10_000.0,
1714 ..immediate_config()
1715 },
1716 );
1717
1718 for _ in 0..200 {
1719 gov.admit(&frame(), Some("b"), 3);
1720 }
1721 let due = gov.take_due(Instant::now());
1722 assert!(!due.is_empty());
1723
1724 // Ask for far more transmissions than the budget allows.
1725 let granted = (0..1000).filter(|_| gov.take_send_token()).count();
1726
1727 assert!(
1728 granted <= DEFAULT_RELAY_BURST as usize + 1,
1729 "granted {granted} transmissions against a burst of {DEFAULT_RELAY_BURST}"
1730 );
1731 assert_eq!(gov.counters().transmissions as usize, granted);
1732 assert!(gov.counters().rate_deferred > 0);
1733 }
1734
1735 #[test]
1736 fn a_frame_that_reached_no_neighbor_for_budget_goes_back_on_the_queue() {
1737 // `take_due` checks the budget without reserving it, so a batch of due
1738 // frames can be released against a single token: the first spends it
1739 // and the rest reach the radio with nothing left. Those frames have
1740 // been forwarded nowhere, and their ids are already recorded as
1741 // handled — dropping one would lose this copy and refuse both the
1742 // copies behind it and the sender's retransmissions for the whole
1743 // retention window. It has to go back on the queue.
1744 let mut gov = MeshRelayGovernor::with_config(
1745 "relay-node",
1746 MeshRelayConfig {
1747 peer_rate_per_sec: 10_000.0,
1748 peer_burst: 10_000.0,
1749 ..immediate_config()
1750 },
1751 );
1752
1753 for _ in 0..40 {
1754 gov.admit(&frame(), Some("b"), 3);
1755 }
1756 let due = gov.take_due(Instant::now());
1757 assert!(
1758 due.len() > DEFAULT_RELAY_BURST as usize,
1759 "the batch must outrun the budget for this to be the case under test"
1760 );
1761
1762 // Drain the budget the way a fan-out does, then hand back everything
1763 // that got nowhere.
1764 let mut requeued = 0;
1765 for relay in due {
1766 if gov.take_send_token() {
1767 continue;
1768 }
1769 gov.requeue(relay);
1770 requeued += 1;
1771 }
1772
1773 assert!(requeued > 0, "the budget must have run out mid-batch");
1774 assert_eq!(gov.pending_len(), requeued, "every starved frame is kept");
1775 assert_eq!(gov.counters().requeued as usize, requeued);
1776 }
1777
1778 #[test]
1779 fn a_requeued_frame_is_still_abandoned_once_it_is_far_past_its_turn() {
1780 // Re-queueing must not become an unbounded retry: a frame kept for
1781 // budget keeps its original due time, so the overdue cut-off still
1782 // reaches it.
1783 let mut gov = governor();
1784 gov.admit(&frame(), Some("b"), 3);
1785 let mut due = gov.take_due(Instant::now());
1786 assert_eq!(due.len(), 1);
1787
1788 gov.requeue(due.remove(0));
1789 assert_eq!(gov.pending_len(), 1);
1790
1791 let later = gov.take_due(Instant::now() + RELAY_QUEUE_MAX_OVERDUE + Duration::from_secs(1));
1792 assert!(later.is_empty());
1793 assert_eq!(gov.counters().abandoned_overdue, 1);
1794 assert_eq!(gov.pending_len(), 0);
1795 }
1796
1797 #[test]
1798 fn a_frame_abandoned_for_waiting_too_long_releases_its_id() {
1799 // A frame dropped for waiting past its turn never reached a link, so
1800 // nothing is circulating that a second forward could duplicate. Its id
1801 // must go back to being unknown, or the sender's retransmissions —
1802 // which carry the same id — are refused for the whole retention window
1803 // because of a few seconds of congestion.
1804 let mut gov = governor();
1805 let msg = frame();
1806 gov.admit(&msg, Some("b"), 3);
1807
1808 let due = gov.take_due(Instant::now() + RELAY_QUEUE_MAX_OVERDUE + Duration::from_secs(1));
1809 assert!(due.is_empty());
1810 assert_eq!(gov.counters().abandoned_overdue, 1);
1811
1812 assert_eq!(
1813 gov.admit(&msg, Some("c"), 3),
1814 RelayAdmission::Queued,
1815 "the sender's retransmission must still be carried"
1816 );
1817 }
1818
1819 #[test]
1820 fn a_frame_displaced_from_the_queue_releases_its_id() {
1821 // Same rule for a frame evicted to make room for an urgent one: it was
1822 // never transmitted, so the route through this device must stay open.
1823 let mut gov = MeshRelayGovernor::with_config(
1824 "relay-node",
1825 MeshRelayConfig {
1826 queue_capacity: 1,
1827 peer_rate_per_sec: 10_000.0,
1828 peer_burst: 10_000.0,
1829 ..immediate_config()
1830 },
1831 );
1832
1833 let routine = frame_with(8, MessagePriority::Low);
1834 gov.admit(&routine, Some("b"), 3);
1835 gov.admit(&frame_with(8, MessagePriority::Critical), Some("b"), 3);
1836 assert_eq!(gov.counters().queue_evicted, 1);
1837
1838 // The urgent frame goes out, leaving room again. A later copy of the
1839 // displaced one — or the sender's retransmission — must be carryable.
1840 gov.take_due(Instant::now());
1841 assert_eq!(
1842 gov.admit(&routine, Some("c"), 3),
1843 RelayAdmission::Queued,
1844 "the displaced frame's id must not be blackholed"
1845 );
1846 }
1847
1848 #[test]
1849 fn a_frame_refused_room_on_the_way_back_releases_its_id() {
1850 // The third drop that happens after the id was recorded: a frame that
1851 // reached no neighbor and finds the queue full when handed back.
1852 let mut gov = MeshRelayGovernor::with_config(
1853 "relay-node",
1854 MeshRelayConfig {
1855 queue_capacity: 1,
1856 peer_rate_per_sec: 10_000.0,
1857 peer_burst: 10_000.0,
1858 ..immediate_config()
1859 },
1860 );
1861
1862 let starved = frame();
1863 gov.admit(&starved, Some("b"), 3);
1864 let mut due = gov.take_due(Instant::now());
1865 assert_eq!(due.len(), 1);
1866
1867 // The queue fills while the frame is out being transmitted, so there is
1868 // no room for it on the way back.
1869 gov.admit(&frame(), Some("b"), 3);
1870 gov.requeue(due.remove(0));
1871 assert_eq!(gov.counters().queue_full, 1);
1872 assert_eq!(gov.pending_len(), 1, "the dropped frame is not held");
1873
1874 // Once the queue drains, the copy behind it must still be carryable.
1875 gov.take_due(Instant::now());
1876 assert_eq!(
1877 gov.admit(&starved, Some("c"), 3),
1878 RelayAdmission::Queued,
1879 "a frame that got nowhere and could not be kept must stay carryable"
1880 );
1881 }
1882
1883 #[test]
1884 fn forwarding_leaves_room_for_this_devices_own_traffic() {
1885 // One radio, one budget — but a device saturated with other people's
1886 // traffic must still be able to send its own, above all the delivery
1887 // acknowledgement whose absence makes a delivered message look lost.
1888 let mut gov = MeshRelayGovernor::with_config(
1889 "relay-node",
1890 MeshRelayConfig {
1891 queue_capacity: 512,
1892 peer_rate_per_sec: 10_000.0,
1893 peer_burst: 10_000.0,
1894 ..immediate_config()
1895 },
1896 );
1897
1898 for _ in 0..200 {
1899 gov.admit(&frame(), Some("b"), 3);
1900 }
1901 gov.take_due(Instant::now());
1902
1903 // Drain the budget the way a saturated forwarder does.
1904 let forwarded = (0..1000).filter(|_| gov.take_send_token()).count();
1905 assert!(forwarded > 0, "forwarding must get its share first");
1906 assert!(
1907 !gov.take_send_token(),
1908 "forwarding must stop at the reserve, not at empty"
1909 );
1910
1911 // The reserve is still there for us.
1912 let own = (0..1000).filter(|_| gov.take_own_send_token()).count();
1913 assert!(
1914 own >= RELAY_OWN_TRAFFIC_RESERVE as usize,
1915 "own traffic got {own} transmissions against a reserve of {RELAY_OWN_TRAFFIC_RESERVE}"
1916 );
1917
1918 // And the ceiling still holds across both: the reserve is carved out of
1919 // the burst, not added to it.
1920 assert!(
1921 (forwarded + own) <= DEFAULT_RELAY_BURST as usize + 1,
1922 "{forwarded} forwarded + {own} own exceeds the burst of {DEFAULT_RELAY_BURST}"
1923 );
1924 }
1925
1926 #[test]
1927 fn a_small_configured_burst_still_forwards() {
1928 // The reserve is clamped to half the burst, so a device configured with
1929 // a burst smaller than the reserve does not silently stop forwarding
1930 // altogether.
1931 let mut gov = MeshRelayGovernor::with_config(
1932 "relay-node",
1933 MeshRelayConfig {
1934 burst: 2.0,
1935 rate_per_sec: 1.0,
1936 peer_rate_per_sec: 10_000.0,
1937 peer_burst: 10_000.0,
1938 ..immediate_config()
1939 },
1940 );
1941
1942 gov.admit(&frame(), Some("b"), 3);
1943 assert!(
1944 !gov.take_due(Instant::now()).is_empty(),
1945 "a small burst must still release forwards"
1946 );
1947 assert!(gov.take_send_token(), "and still allow transmitting them");
1948 }
1949
1950 #[test]
1951 fn a_settled_duplicate_is_absorbed_without_the_full_admission_path() {
1952 let mut gov = governor();
1953 let msg = frame();
1954
1955 assert!(
1956 !gov.absorb_settled_duplicate(&msg.id.as_str()),
1957 "an id never seen is not a duplicate"
1958 );
1959
1960 gov.admit(&msg, Some("b"), 3);
1961 // While our copy is still pending, a copy narrows our forward, so the
1962 // cheap path must decline it and leave it to `admit`.
1963 assert!(!gov.absorb_settled_duplicate(&msg.id.as_str()));
1964
1965 // Once it has gone out, further copies are settled duplicates.
1966 gov.take_due(Instant::now());
1967 assert!(gov.absorb_settled_duplicate(&msg.id.as_str()));
1968 assert_eq!(gov.counters().duplicates_suppressed, 1);
1969 }
1970
1971 #[test]
1972 fn eviction_prefers_a_frame_that_has_not_yet_gone_out() {
1973 // A due frame is on its way to the radio this tick; taking it costs a
1974 // transmission that was about to happen, while the frame replacing it
1975 // still has to wait out its hold.
1976 let mut gov = MeshRelayGovernor::with_config(
1977 "relay-node",
1978 MeshRelayConfig {
1979 queue_capacity: 2,
1980 jitter_min: Duration::from_millis(0),
1981 jitter_max: Duration::from_millis(0),
1982 peer_rate_per_sec: 10_000.0,
1983 peer_burst: 10_000.0,
1984 ..Default::default()
1985 },
1986 );
1987
1988 // One low-priority frame due immediately.
1989 let due_now = frame_with(8, MessagePriority::Low);
1990 gov.admit(&due_now, Some("b"), 3);
1991
1992 // A second low-priority frame that is not due for a while.
1993 gov.config.jitter_min = Duration::from_millis(500);
1994 gov.config.jitter_max = Duration::from_millis(500);
1995 let waiting = frame_with(8, MessagePriority::Low);
1996 gov.admit(&waiting, Some("b"), 3);
1997
1998 // An urgent frame needs the room.
1999 gov.admit(&frame_with(8, MessagePriority::Critical), Some("b"), 3);
2000
2001 assert_eq!(gov.counters().queue_evicted, 1);
2002 assert_eq!(
2003 gov.counters().queue_full,
2004 0,
2005 "making room is not the same as refusing"
2006 );
2007 assert!(
2008 gov.pending
2009 .iter()
2010 .any(|p| p.message.id.as_str() == due_now.id.as_str()),
2011 "the frame already due must survive"
2012 );
2013 assert!(
2014 !gov.pending
2015 .iter()
2016 .any(|p| p.message.id.as_str() == waiting.id.as_str()),
2017 "the frame still waiting is the one displaced"
2018 );
2019 }
2020
2021 #[test]
2022 fn a_full_peer_table_keeps_the_links_that_are_spending() {
2023 // Clearing the table wholesale hands every neighbor a fresh burst,
2024 // including the noisy one the per-link limit exists to hold back.
2025 let mut gov = MeshRelayGovernor::with_config("relay-node", immediate_config());
2026
2027 // One neighbor spends most of its allowance.
2028 for _ in 0..(DEFAULT_RELAY_PEER_BURST as usize - 1) {
2029 gov.admit(&frame(), Some("noisy"), 3);
2030 }
2031
2032 // Fill the table with links that arrive once and go quiet.
2033 for i in 0..MAX_RELAY_RATE_PEERS {
2034 gov.admit(&frame(), Some(&format!("idle-{i}")), 3);
2035 }
2036
2037 assert!(gov.peer_budgets.len() <= MAX_RELAY_RATE_PEERS);
2038 // Still tracked at all: the entry the limit exists to hold back is the
2039 // one it can least afford to forget, and it never sends again here, so
2040 // an eviction would leave it absent rather than rebuilt.
2041 let noisy = gov
2042 .peer_budgets
2043 .get("noisy")
2044 .expect("a link mid-spend must not be forgotten to make room for idle ones");
2045 assert!(
2046 noisy.tokens < noisy.capacity,
2047 "the noisy link kept its spent allowance rather than being handed a fresh burst"
2048 );
2049 }
2050
2051 #[test]
2052 fn a_refused_frame_leaves_the_way_open_for_another_copy() {
2053 // A frame turned away must not be remembered as handled: the copy
2054 // arriving over a healthy link a moment later is the alternate path,
2055 // and suppressing it would blank every route through this device for
2056 // the whole retention window.
2057 let mut gov = governor();
2058
2059 // Refused for having no hops left — the cheapest thing an attacker can
2060 // forge, since nothing authenticates the claim.
2061 let msg = frame_with(1, MessagePriority::Medium);
2062 assert_eq!(
2063 gov.admit(&msg, Some("hostile"), 3),
2064 RelayAdmission::Rejected(RelayRejection::HopLimitReached)
2065 );
2066
2067 // The same message, arriving intact by another route.
2068 let mut healthy = msg.clone();
2069 healthy.ttl = TTL::new(8).unwrap();
2070 assert_eq!(
2071 gov.admit(&healthy, Some("good"), 3),
2072 RelayAdmission::Queued,
2073 "a refused frame must not blackhole its id"
2074 );
2075 }
2076
2077 #[test]
2078 fn a_rate_limited_neighbor_does_not_blackhole_the_ids_it_sent() {
2079 let mut gov = governor();
2080
2081 // Exhaust one neighbor's allowance with distinct frames.
2082 let mut refused = Vec::new();
2083 for _ in 0..200 {
2084 let msg = frame();
2085 if gov.admit(&msg, Some("noisy"), 3)
2086 == RelayAdmission::Rejected(RelayRejection::PeerRateLimited)
2087 {
2088 refused.push(msg);
2089 }
2090 }
2091 assert!(!refused.is_empty());
2092
2093 // Those same messages, reaching us through someone else, must still be
2094 // carried.
2095 let victim = &refused[0];
2096 assert_eq!(gov.admit(victim, Some("quiet"), 3), RelayAdmission::Queued);
2097 }
2098
2099 #[test]
2100 fn the_last_hop_does_not_stand_down_for_a_neighbor() {
2101 // Our copy is going to the recipient over a link only we are known to
2102 // hold. A neighbor's copy must leave it queued.
2103 let mut gov = MeshRelayGovernor::with_config(
2104 "relay-node",
2105 MeshRelayConfig {
2106 jitter_min: Duration::from_millis(50),
2107 jitter_max: Duration::from_millis(200),
2108 ..Default::default()
2109 },
2110 );
2111 let msg = frame();
2112
2113 assert_eq!(gov.admit(&msg, Some("b"), 3), RelayAdmission::Queued);
2114 assert_eq!(
2115 gov.admit(&msg, Some("c"), 3),
2116 RelayAdmission::Rejected(RelayRejection::NarrowedByDuplicate)
2117 );
2118
2119 assert_eq!(gov.pending_len(), 1, "the delivering copy must survive");
2120 assert_eq!(gov.counters().cancelled_by_duplicate, 0);
2121 }
2122
2123 #[test]
2124 fn a_forward_waiting_far_past_its_turn_is_abandoned() {
2125 let mut gov = governor();
2126 gov.admit(&frame(), Some("b"), 3);
2127
2128 // Long enough that other paths have carried it or the sender has
2129 // retransmitted; holding it would only displace newer traffic.
2130 let due = gov.take_due(Instant::now() + RELAY_QUEUE_MAX_OVERDUE + Duration::from_secs(1));
2131 assert!(due.is_empty());
2132 assert_eq!(gov.counters().abandoned_overdue, 1);
2133 assert_eq!(gov.pending_len(), 0);
2134 }
2135
2136 #[test]
2137 fn a_full_queue_refuses_rather_than_growing() {
2138 let mut gov = MeshRelayGovernor::with_config(
2139 "relay-node",
2140 MeshRelayConfig {
2141 queue_capacity: 4,
2142 jitter_min: Duration::from_millis(500),
2143 jitter_max: Duration::from_millis(500),
2144 peer_rate_per_sec: 10_000.0,
2145 peer_burst: 10_000.0,
2146 ..Default::default()
2147 },
2148 );
2149
2150 for _ in 0..4 {
2151 assert_eq!(gov.admit(&frame(), Some("b"), 3), RelayAdmission::Queued);
2152 }
2153 assert_eq!(
2154 gov.admit(&frame(), Some("b"), 3),
2155 RelayAdmission::Rejected(RelayRejection::QueueFull)
2156 );
2157 assert_eq!(gov.pending_len(), 4);
2158 }
2159
2160 #[test]
2161 fn a_full_queue_still_takes_an_urgent_frame_over_a_routine_one() {
2162 let mut gov = MeshRelayGovernor::with_config(
2163 "relay-node",
2164 MeshRelayConfig {
2165 queue_capacity: 2,
2166 jitter_min: Duration::from_millis(500),
2167 jitter_max: Duration::from_millis(500),
2168 peer_rate_per_sec: 10_000.0,
2169 peer_burst: 10_000.0,
2170 ..Default::default()
2171 },
2172 );
2173
2174 gov.admit(&frame_with(8, MessagePriority::Low), Some("b"), 3);
2175 gov.admit(&frame_with(8, MessagePriority::Low), Some("b"), 3);
2176
2177 assert_eq!(
2178 gov.admit(&frame_with(8, MessagePriority::Critical), Some("b"), 3,),
2179 RelayAdmission::Queued
2180 );
2181 assert_eq!(gov.pending_len(), 2);
2182 assert!(gov
2183 .take_due(Instant::now() + Duration::from_secs(1))
2184 .iter()
2185 .any(|r| r.message.priority == MessagePriority::Critical));
2186 }
2187
2188 #[test]
2189 fn a_frame_is_never_sent_back_where_it_came_from() {
2190 let gov = governor();
2191 let targets = gov.select_targets(
2192 [("alice", 80), ("b", 80), ("c", 80), ("d", 80)],
2193 &["b", "alice"], // arrival neighbor and the peer that wrote it
2194 "msg-1",
2195 );
2196
2197 assert!(!targets.contains(&"b".to_string()));
2198 assert!(!targets.contains(&"alice".to_string()));
2199 assert!(targets.contains(&"c".to_string()));
2200 assert!(targets.contains(&"d".to_string()));
2201 }
2202
2203 #[test]
2204 fn fanout_is_capped_and_stable_for_a_given_frame() {
2205 let gov = governor();
2206 // All equally good, so the tie-break is what decides — which is the
2207 // property under test.
2208 let neighbors = [
2209 ("n0", 70),
2210 ("n1", 70),
2211 ("n2", 70),
2212 ("n3", 70),
2213 ("n4", 70),
2214 ("n5", 70),
2215 ("n6", 70),
2216 ("n7", 70),
2217 ];
2218
2219 let first = gov.select_targets(neighbors, &[], "msg-1");
2220 let again = gov.select_targets(neighbors, &[], "msg-1");
2221
2222 assert_eq!(first.len(), DEFAULT_RELAY_FANOUT);
2223 assert_eq!(first, again, "a second copy must pick the same neighbors");
2224
2225 // Different frames spread across different neighbors rather than
2226 // hammering one set.
2227 let other = gov.select_targets(neighbors, &[], "msg-2");
2228 assert_eq!(other.len(), DEFAULT_RELAY_FANOUT);
2229 }
2230
2231 #[test]
2232 fn a_frame_we_originated_is_not_carried_back_for_someone_else() {
2233 // Our own message, handed to neighbors. When a neighbor's copy reaches
2234 // us again we must recognize it, not take it on as third-party traffic.
2235 let mut gov = governor();
2236 let msg = frame();
2237
2238 gov.mark_handled(&msg.id.as_str());
2239
2240 assert_eq!(
2241 gov.admit(&msg, Some("b"), 3),
2242 RelayAdmission::Rejected(RelayRejection::AlreadySeen)
2243 );
2244 }
2245
2246 #[test]
2247 fn the_suppression_cache_holds_a_saturated_node_for_its_full_window() {
2248 // Capacity has to outlast in-flight copies at the maximum rate the node
2249 // will accept traffic. If it did not, an id could be forgotten while
2250 // copies were still circulating and be forwarded a second time — by
2251 // every node, which is how a flood becomes a storm.
2252 let gov = governor();
2253 let max_admissions_per_sec = DEFAULT_RELAY_PEER_RATE_PER_SEC * MAX_RELAY_RATE_PEERS as f32;
2254 let sustained_ceiling = DEFAULT_RELAY_RATE_PER_SEC.min(max_admissions_per_sec);
2255 let ids_in_window = sustained_ceiling * gov.config().seen.retention.as_secs_f32();
2256
2257 assert!(
2258 (gov.config().seen.capacity as f32) >= ids_in_window,
2259 "cache holds {} ids but a saturated node sees {} within the retention window",
2260 gov.config().seen.capacity,
2261 ids_in_window
2262 );
2263 }
2264
2265 // ====================================================================
2266 // Capability bias
2267 // ====================================================================
2268
2269 /// A governor with a real jitter window, so bias has something to shift.
2270 fn biased_governor() -> MeshRelayGovernor {
2271 MeshRelayGovernor::with_config("relay-node", MeshRelayConfig::default())
2272 }
2273
2274 #[test]
2275 fn a_weak_device_always_waits_longer_than_a_capable_one() {
2276 // The whole mechanism: the weak device's earliest delay must be later
2277 // than the capable device's latest, or the two windows overlap and the
2278 // weak one sometimes transmits first — spending the airtime the bias
2279 // exists to save.
2280 let msg = frame();
2281
2282 let mut capable = biased_governor();
2283 capable.set_conditions(Some(100), false, false, 30);
2284
2285 let mut weak = biased_governor();
2286 weak.set_conditions(Some(30), false, false, 30);
2287
2288 let capable_delay = capable.jitter_for(&msg.id.as_str(), 3);
2289 let weak_delay = weak.jitter_for(&msg.id.as_str(), 3);
2290 assert!(
2291 weak_delay > capable_delay,
2292 "weak waited {weak_delay:?}, capable {capable_delay:?}"
2293 );
2294
2295 // Independent of which frame it is: the handicap shifts the window, so
2296 // the worst case for the weak device beats the best case for the
2297 // capable one on every id.
2298 //
2299 // The bound that has to clear the span is the handicap a device can
2300 // actually pay, not the configured ceiling. A device at the floor
2301 // scales to `bias_min_scale`, never to zero, so the most it ever pays
2302 // is `(1 - bias_min_scale) * bias_max_handicap` — mirroring
2303 // `jitter_for`'s expression, truncation included. Asserting the raw
2304 // ceiling instead would stay green while a raised min scale silently
2305 // let the two windows overlap.
2306 let span = DEFAULT_RELAY_JITTER_MAX_MS - DEFAULT_RELAY_JITTER_MIN_MS;
2307 let reachable_handicap_ms = ((1.0 - DEFAULT_RELAY_BIAS_MIN_SCALE)
2308 * DEFAULT_RELAY_BIAS_MAX_HANDICAP_MS as f32) as u64;
2309 assert!(
2310 reachable_handicap_ms >= span,
2311 "the reachable handicap ({reachable_handicap_ms}ms: a \
2312 {DEFAULT_RELAY_BIAS_MAX_HANDICAP_MS}ms ceiling scaled by 1 - \
2313 {DEFAULT_RELAY_BIAS_MIN_SCALE}) must cover the jitter span ({span}ms) or the two \
2314 windows overlap"
2315 );
2316 }
2317
2318 #[test]
2319 fn bias_adds_a_bounded_delay_rather_than_a_multiplied_one() {
2320 // Bias must cost redundancy, never delivery. The jitter span already
2321 // grows with density, so the one shape that must not exist is a
2322 // handicap that grows with it too: at every density the biased device
2323 // waits at most one fixed handicap longer than the capable one.
2324 let msg = frame();
2325 let cap = Duration::from_millis(DEFAULT_RELAY_BIAS_MAX_HANDICAP_MS);
2326
2327 for degree in [0usize, 3, 6, 30, 100, MAX_RELAY_RATE_PEERS] {
2328 let mut capable = biased_governor();
2329 capable.set_conditions(Some(100), false, false, 30);
2330 let mut weak = biased_governor();
2331 weak.set_conditions(Some(0), false, false, 30);
2332
2333 let capable_delay = capable.jitter_for(&msg.id.as_str(), degree);
2334 let weak_delay = weak.jitter_for(&msg.id.as_str(), degree);
2335 assert!(weak_delay >= capable_delay, "degree {degree}");
2336 assert!(
2337 weak_delay - capable_delay <= cap,
2338 "at degree {degree} bias added {:?}, past its {cap:?} ceiling",
2339 weak_delay - capable_delay
2340 );
2341 }
2342 }
2343
2344 #[test]
2345 fn a_biased_forward_still_transmits_inside_the_overdue_cutoff() {
2346 // Asserted over *every* possible message id by computing the worst
2347 // case, not by sampling one: the delay is `min + handicap + hash %
2348 // span`, so a sampled id passes or fails on its hash and would make
2349 // this a coin toss rather than a guarantee.
2350 //
2351 // Bounded to the venue-sized neighborhood this design targets (~100
2352 // nodes). Past roughly 155 neighbors the delay does outrun the
2353 // cut-off — but the density scaling alone does that at ~167 with bias
2354 // switched off entirely, so it is a property of the pre-existing
2355 // density term rather than of bias, and out of scope here.
2356 let config = MeshRelayConfig::default();
2357 let min_ms = config.jitter_min.as_millis() as u64;
2358 let max_ms = config.jitter_max.as_millis() as u64;
2359 let degree = 100usize;
2360
2361 let density_scale = 1 + (degree / config.dense_degree.max(1)) as u64;
2362 let span = (max_ms - min_ms).saturating_mul(density_scale).max(1);
2363 // The full handicap, which only a device biased to the floor pays.
2364 let worst = Duration::from_millis(
2365 min_ms + config.bias_max_handicap.as_millis() as u64 + (span - 1),
2366 );
2367
2368 assert!(
2369 worst < RELAY_QUEUE_MAX_OVERDUE,
2370 "the worst biased wait at degree {degree} is {worst:?}, past the \
2371 {RELAY_QUEUE_MAX_OVERDUE:?} cut-off"
2372 );
2373 }
2374
2375 #[test]
2376 fn charging_and_eager_devices_are_not_biased_down() {
2377 let msg = frame();
2378 let mut unfed = biased_governor();
2379 let baseline = unfed.jitter_for(&msg.id.as_str(), 3);
2380
2381 // Charging at a level that would otherwise be heavily penalised.
2382 let mut charging = biased_governor();
2383 charging.set_conditions(Some(20), true, false, 30);
2384 assert_eq!(charging.jitter_for(&msg.id.as_str(), 3), baseline);
2385
2386 // Configured to relay eagerly, on battery.
2387 let mut eager = biased_governor();
2388 eager.set_conditions(Some(20), false, true, 30);
2389 assert_eq!(eager.jitter_for(&msg.id.as_str(), 3), baseline);
2390
2391 // And an unfed device is treated as fully capable, matching the
2392 // forwarding gate's "unknown means willing".
2393 unfed.set_conditions(None, false, false, 30);
2394 assert_eq!(unfed.jitter_for(&msg.id.as_str(), 3), baseline);
2395 }
2396
2397 #[test]
2398 fn a_weak_device_forwards_to_fewer_neighbors_but_never_none() {
2399 let neighbors: Vec<(&str, u8)> = vec![("a", 90), ("b", 80), ("c", 70), ("d", 60)];
2400
2401 let mut capable = biased_governor();
2402 capable.set_conditions(Some(100), false, false, 30);
2403 let capable_targets = capable.select_targets(neighbors.clone(), &[], "msg-1");
2404 assert_eq!(capable_targets.len(), DEFAULT_RELAY_FANOUT);
2405
2406 let mut weak = biased_governor();
2407 weak.set_conditions(Some(0), false, false, 30);
2408 let weak_targets = weak.select_targets(neighbors, &[], "msg-1");
2409 assert!(
2410 !weak_targets.is_empty(),
2411 "a fan-out of zero is a silent drop, not a cheaper forward"
2412 );
2413 assert!(
2414 weak_targets.len() < capable_targets.len(),
2415 "weak fanned out to {} neighbors, capable to {}",
2416 weak_targets.len(),
2417 capable_targets.len()
2418 );
2419 }
2420
2421 #[test]
2422 fn a_weak_device_offers_its_own_frames_to_every_neighbor_it_would_have() {
2423 // Bias narrows the fan-out for other people's traffic, where neighbors
2424 // hold copies and the sender is still retrying, so the cost is
2425 // redundancy. A frame this device originated has neither: the fan-out
2426 // *is* the delivery attempt, and because targets are chosen stably per
2427 // message id, a narrowed set would be the same narrowed set on every
2428 // retransmission — one dead-end neighbor forever instead of three
2429 // chances.
2430 let neighbors: Vec<(&str, u8)> = vec![("a", 90), ("b", 80), ("c", 70), ("d", 60)];
2431
2432 let mut weak = biased_governor();
2433 weak.set_conditions(Some(0), false, false, 30);
2434
2435 assert!(
2436 weak.select_targets(neighbors.clone(), &[], "msg-1").len() < DEFAULT_RELAY_FANOUT,
2437 "the forwarding path must still be biased",
2438 );
2439 assert_eq!(
2440 weak.select_origination_targets(neighbors.clone(), &[], "msg-1")
2441 .len(),
2442 DEFAULT_RELAY_FANOUT,
2443 "this device's own frame must reach as many neighbors as a capable device's",
2444 );
2445
2446 // And identical to what a fully capable device would have picked, not
2447 // merely as many: same neighbors, same order.
2448 let mut capable = biased_governor();
2449 capable.set_conditions(Some(100), false, false, 30);
2450 assert_eq!(
2451 weak.select_origination_targets(neighbors.clone(), &[], "msg-1"),
2452 capable.select_origination_targets(neighbors, &[], "msg-1"),
2453 );
2454 }
2455
2456 #[test]
2457 fn bias_does_not_throttle_this_devices_own_traffic() {
2458 // Bias scales the *forwarding* share of the airtime ceiling, not the
2459 // ceiling. Scaling the shared bucket would slow a low-battery device's
2460 // own messages and acknowledgements in step with its battery, which is
2461 // delivery rather than redundancy — and an absent acknowledgement is
2462 // what makes a delivered message look lost to its sender.
2463 let mut capable = MeshRelayGovernor::with_config("relay-node", immediate_config());
2464 capable.set_conditions(Some(100), false, false, 30);
2465 let capable_own = (0..1000).filter(|_| capable.take_own_send_token()).count();
2466
2467 let mut weak = MeshRelayGovernor::with_config("relay-node", immediate_config());
2468 weak.set_conditions(Some(0), false, false, 30);
2469 let weak_own = (0..1000).filter(|_| weak.take_own_send_token()).count();
2470
2471 assert_eq!(
2472 weak_own, capable_own,
2473 "a weak device got {weak_own} of its own transmissions against a capable \
2474 device's {capable_own}",
2475 );
2476 assert!(capable_own > 0, "the fixture must actually grant something");
2477 }
2478
2479 #[test]
2480 fn a_weak_device_transmits_less_before_hitting_its_ceiling() {
2481 let mut capable = MeshRelayGovernor::with_config("relay-node", immediate_config());
2482 capable.set_conditions(Some(100), false, false, 30);
2483 let capable_grants = (0..1000).filter(|_| capable.take_send_token()).count();
2484
2485 let mut weak = MeshRelayGovernor::with_config("relay-node", immediate_config());
2486 weak.set_conditions(Some(0), false, false, 30);
2487 let weak_grants = (0..1000).filter(|_| weak.take_send_token()).count();
2488
2489 assert!(
2490 weak_grants < capable_grants,
2491 "weak got {weak_grants} transmissions, capable {capable_grants}"
2492 );
2493 assert!(
2494 weak_grants > 0,
2495 "bias scales the budget down, it does not switch forwarding off"
2496 );
2497 }
2498
2499 #[test]
2500 fn a_worsening_battery_does_not_mint_a_fresh_burst() {
2501 // `set_conditions` runs every tick, so re-sizing the bucket must keep
2502 // what has been spent spent. If it refilled, a device whose battery
2503 // oscillates around any point in the ramp would transmit without limit.
2504 let mut gov = MeshRelayGovernor::with_config("relay-node", immediate_config());
2505 gov.set_conditions(Some(100), false, false, 30);
2506
2507 let first = (0..1000).filter(|_| gov.take_send_token()).count();
2508 assert!(first > 0);
2509 assert!(!gov.take_send_token(), "the budget must be spent");
2510
2511 for level in [90, 100, 90, 100] {
2512 gov.set_conditions(Some(level), false, false, 30);
2513 assert!(
2514 !gov.take_send_token(),
2515 "re-sizing the budget handed back tokens that were already spent"
2516 );
2517 }
2518 }
2519
2520 // ====================================================================
2521 // Activity-derived relay standing
2522 // ====================================================================
2523
2524 /// Short windows so activity transitions are reachable without waiting a
2525 /// real minute; `observe_activity` takes `now`, so the clock is explicit.
2526 fn activity_config() -> MeshRelayConfig {
2527 MeshRelayConfig {
2528 activity_window: Duration::from_secs(10),
2529 activity_min_forwards: 2,
2530 activity_idle_windows: 2,
2531 ..immediate_config()
2532 }
2533 }
2534
2535 #[test]
2536 fn carrying_traffic_is_what_makes_a_device_a_relay() {
2537 let mut gov = MeshRelayGovernor::with_config("relay-node", activity_config());
2538 let start = Instant::now();
2539 assert!(!gov.is_active_relay());
2540
2541 // A window that carried nothing changes nothing.
2542 assert_eq!(gov.observe_activity(start + Duration::from_secs(11)), None);
2543 assert!(!gov.is_active_relay());
2544
2545 // Carrying enough in one window promotes, exactly once.
2546 gov.record_forwarded();
2547 gov.record_forwarded();
2548 assert_eq!(
2549 gov.observe_activity(start + Duration::from_secs(22)),
2550 Some(RelayActivity::Began { forwarded: 2 })
2551 );
2552 assert!(gov.is_active_relay());
2553
2554 gov.record_forwarded();
2555 gov.record_forwarded();
2556 assert_eq!(
2557 gov.observe_activity(start + Duration::from_secs(33)),
2558 None,
2559 "a relay that keeps carrying traffic must not re-announce"
2560 );
2561 assert!(gov.is_active_relay());
2562 }
2563
2564 #[test]
2565 fn a_relay_stops_being_one_only_after_sustained_quiet() {
2566 // Asymmetric on purpose: one busy window promotes, several quiet ones
2567 // demote. A mesh with nothing to say for a window has not stopped
2568 // having a relay in it.
2569 let mut gov = MeshRelayGovernor::with_config("relay-node", activity_config());
2570 let start = Instant::now();
2571
2572 gov.record_forwarded();
2573 gov.record_forwarded();
2574 assert!(matches!(
2575 gov.observe_activity(start + Duration::from_secs(11)),
2576 Some(RelayActivity::Began { .. })
2577 ));
2578
2579 // One quiet window is not enough.
2580 assert_eq!(gov.observe_activity(start + Duration::from_secs(22)), None);
2581 assert!(gov.is_active_relay());
2582
2583 assert_eq!(
2584 gov.observe_activity(start + Duration::from_secs(33)),
2585 Some(RelayActivity::Ceased)
2586 );
2587 assert!(!gov.is_active_relay());
2588
2589 // And it stays ceased rather than re-announcing every window.
2590 assert_eq!(gov.observe_activity(start + Duration::from_secs(44)), None);
2591 }
2592
2593 #[test]
2594 fn traffic_within_a_window_resets_the_run_of_quiet_ones() {
2595 let mut gov = MeshRelayGovernor::with_config("relay-node", activity_config());
2596 let start = Instant::now();
2597
2598 gov.record_forwarded();
2599 gov.record_forwarded();
2600 assert!(matches!(
2601 gov.observe_activity(start + Duration::from_secs(11)),
2602 Some(RelayActivity::Began { .. })
2603 ));
2604
2605 // Quiet, then busy again, then quiet: the run restarts, so the second
2606 // quiet window must not be the one that demotes.
2607 assert_eq!(gov.observe_activity(start + Duration::from_secs(22)), None);
2608 gov.record_forwarded();
2609 gov.record_forwarded();
2610 assert_eq!(gov.observe_activity(start + Duration::from_secs(33)), None);
2611 assert_eq!(gov.observe_activity(start + Duration::from_secs(44)), None);
2612 assert!(gov.is_active_relay(), "the quiet run restarted");
2613
2614 assert_eq!(
2615 gov.observe_activity(start + Duration::from_secs(55)),
2616 Some(RelayActivity::Ceased)
2617 );
2618 }
2619
2620 #[test]
2621 fn observing_before_a_window_has_elapsed_does_nothing() {
2622 let mut gov = MeshRelayGovernor::with_config("relay-node", activity_config());
2623 let start = Instant::now();
2624
2625 gov.record_forwarded();
2626 gov.record_forwarded();
2627 assert_eq!(gov.observe_activity(start + Duration::from_secs(5)), None);
2628 assert!(
2629 !gov.is_active_relay(),
2630 "the window must complete before it counts"
2631 );
2632
2633 // And the frames carried are still counted toward it.
2634 assert!(matches!(
2635 gov.observe_activity(start + Duration::from_secs(11)),
2636 Some(RelayActivity::Began { forwarded: 2 })
2637 ));
2638 }
2639
2640 #[test]
2641 fn a_closing_gate_drops_the_relay_standing_at_once() {
2642 // Configuration or battery closing the forwarding gate stops traffic
2643 // immediately, so waiting out the idle windows would report a relay
2644 // that had already stopped being one.
2645 let mut gov = MeshRelayGovernor::with_config("relay-node", activity_config());
2646 let start = Instant::now();
2647
2648 gov.record_forwarded();
2649 gov.record_forwarded();
2650 assert!(matches!(
2651 gov.observe_activity(start + Duration::from_secs(11)),
2652 Some(RelayActivity::Began { .. })
2653 ));
2654
2655 assert!(gov.force_inactive(), "reports that the standing was held");
2656 assert!(!gov.is_active_relay());
2657 assert!(
2658 !gov.force_inactive(),
2659 "a device that was not a relay must not report a demotion"
2660 );
2661
2662 // The window's tally went with it, so re-opening the gate starts from
2663 // scratch rather than promoting on stale traffic.
2664 assert_eq!(gov.observe_activity(start + Duration::from_secs(22)), None);
2665 assert!(!gov.is_active_relay());
2666 }
2667
2668 // ====================================================================
2669 // Custody: the held-frame intake and the drop point
2670 // ====================================================================
2671
2672 /// A frame carrying a deposit request, as a depositor offers it.
2673 fn deposit_frame() -> Message {
2674 let mut msg = frame();
2675 msg.metadata
2676 .insert(CUSTODY_META_KEY.to_string(), "data".to_string());
2677 msg
2678 }
2679
2680 #[test]
2681 fn a_forwarder_strips_the_deposit_request_and_the_drop_point_keeps_it() {
2682 // The key is unsigned metadata outside the sealed body, so nothing
2683 // but this strip keeps a deposit to one hop: left in place, a two-hop
2684 // network deposits a frame at a custodian that never saw its sender.
2685 let mut gov = governor();
2686 let msg = deposit_frame();
2687 assert_eq!(gov.admit(&msg, Some("alice"), 3), RelayAdmission::Queued);
2688
2689 let (due, abandoned) = gov.release_due(Instant::now());
2690 assert!(abandoned.is_empty());
2691 let relay = &due[0];
2692 assert!(
2693 !relay.message.metadata.contains_key(CUSTODY_META_KEY),
2694 "the copy that travels on carries no request"
2695 );
2696 assert_eq!(
2697 relay.custody_request.as_deref(),
2698 Some("data"),
2699 "the request is kept beside the copy, for the drop point alone"
2700 );
2701 assert_eq!(
2702 relay.custody_target, None,
2703 "an ordinary forward is not marked"
2704 );
2705 assert_eq!(
2706 relay.message.content, msg.content,
2707 "only the outer metadata is touched"
2708 );
2709 }
2710
2711 #[test]
2712 fn an_abandoned_forward_is_handed_to_the_drop_point_with_its_id_released() {
2713 let mut gov = governor();
2714 let msg = deposit_frame();
2715 gov.admit(&msg, Some("alice"), 3);
2716 let (due, _) = gov.release_due(Instant::now());
2717 gov.requeue(due.into_iter().next().unwrap());
2718
2719 let later = Instant::now() + RELAY_QUEUE_MAX_OVERDUE + Duration::from_millis(1);
2720 let (due, abandoned) = gov.release_due(later);
2721 assert!(due.is_empty());
2722 assert_eq!(
2723 abandoned.len(),
2724 1,
2725 "the drop point sees the abandoned forward"
2726 );
2727 assert_eq!(abandoned[0].custody_request.as_deref(), Some("data"));
2728 assert!(
2729 !gov.is_suppressed(&msg.id.as_str()),
2730 "released before custody judges it, so a custodian never blanks its own route"
2731 );
2732 assert_eq!(gov.counters().abandoned_overdue, 1);
2733 }
2734
2735 #[test]
2736 fn the_held_intake_skips_suppression_and_hop_accounting() {
2737 let mut gov = governor();
2738 let msg = frame_with(3, MessagePriority::Medium);
2739
2740 // The ordinary path has handled this id: a copy would be refused.
2741 assert_eq!(gov.admit(&msg, Some("alice"), 3), RelayAdmission::Queued);
2742 let (due, _) = gov.release_due(Instant::now());
2743 assert_eq!(due.len(), 1);
2744 assert_eq!(
2745 gov.admit(&msg, Some("alice"), 3),
2746 RelayAdmission::Rejected(RelayRejection::AlreadySeen)
2747 );
2748
2749 // The held intake queues it anyway, toward one neighbor, with the hop
2750 // fields exactly as handed in.
2751 let held = due.into_iter().next().unwrap().message;
2752 let hop_before = held.hop_count.value();
2753 let ttl_before = held.ttl.value();
2754 gov.admit_held(held, "dave", Instant::now())
2755 .expect("queued");
2756 let (due, _) = gov.release_due(Instant::now());
2757 assert_eq!(due.len(), 1);
2758 let relay = &due[0];
2759 assert_eq!(relay.custody_target.as_deref(), Some("dave"));
2760 assert_eq!(relay.arrival_peer, None);
2761 assert_eq!(relay.message.hop_count.value(), hop_before, "no hop spent");
2762 assert_eq!(relay.message.ttl.value(), ttl_before, "no budget spent");
2763
2764 // And it recorded nothing: the cache is exactly as it was.
2765 let seen_before = gov.seen.len();
2766 gov.admit_held(relay.message.clone(), "erin", Instant::now())
2767 .expect("queued");
2768 assert_eq!(gov.seen.len(), seen_before);
2769 }
2770
2771 #[test]
2772 fn a_marked_forward_never_touches_the_suppression_cache() {
2773 let mut gov = governor();
2774 let msg = frame();
2775 // The depositor's own retransmission is legitimately handled: taken
2776 // on, released to the radio, and its id recorded for the window.
2777 gov.admit(&msg, Some("alice"), 3);
2778 let (due, _) = gov.release_due(Instant::now());
2779 assert_eq!(due.len(), 1, "transmitted");
2780 assert!(gov.is_suppressed(&msg.id.as_str()));
2781
2782 // A held copy of the same id, abandoned: the ordinary entry stays.
2783 gov.admit_held(msg.clone(), "dave", Instant::now())
2784 .expect("queued");
2785 let later = Instant::now() + RELAY_QUEUE_MAX_OVERDUE + Duration::from_millis(1);
2786 let (_, abandoned) = gov.release_due(later);
2787 let held: Vec<_> = abandoned
2788 .iter()
2789 .filter(|r| r.custody_target.is_some())
2790 .collect();
2791 assert_eq!(held.len(), 1);
2792 assert!(
2793 gov.is_suppressed(&msg.id.as_str()),
2794 "abandoning a marked forward releases nothing"
2795 );
2796 // Nor does refusing it room on the way back.
2797 let counters_before = gov.counters().clone();
2798 let mut full = MeshRelayGovernor::with_config(
2799 "relay-node",
2800 MeshRelayConfig {
2801 queue_capacity: 1,
2802 ..immediate_config()
2803 },
2804 );
2805 full.admit(&frame(), Some("alice"), 3);
2806 let returned = full.requeue(PendingRelay {
2807 message: frame(),
2808 arrival_peer: None,
2809 heard_from: Vec::new(),
2810 due_at: Instant::now(),
2811 custody_request: None,
2812 custody_target: Some("dave".to_string()),
2813 });
2814 assert!(returned.is_some(), "handed back to the custody store");
2815 assert_eq!(full.counters().queue_full, 0, "not counted as a loss");
2816 assert_eq!(
2817 gov.counters().abandoned_overdue,
2818 counters_before.abandoned_overdue
2819 );
2820 }
2821
2822 #[test]
2823 fn dropping_held_forwards_leaves_the_ordinary_ones_queued() {
2824 let mut gov = governor();
2825 gov.admit(&frame(), Some("alice"), 3);
2826 gov.admit_held(frame(), "dave", Instant::now())
2827 .expect("queued");
2828 gov.admit_held(frame(), "erin", Instant::now())
2829 .expect("queued");
2830 assert_eq!(gov.pending_len(), 3);
2831
2832 gov.drop_held();
2833
2834 assert_eq!(gov.pending_len(), 1);
2835 let (due, _) = gov.release_due(Instant::now());
2836 assert_eq!(due.len(), 1);
2837 assert!(due[0].custody_target.is_none());
2838 }
2839
2840 #[test]
2841 fn the_held_intake_still_takes_the_queue_and_the_neighbor_rate() {
2842 let mut gov = MeshRelayGovernor::with_config(
2843 "relay-node",
2844 MeshRelayConfig {
2845 queue_capacity: 1,
2846 peer_burst: 1.0,
2847 peer_rate_per_sec: 0.0001,
2848 ..immediate_config()
2849 },
2850 );
2851 let now = Instant::now();
2852 gov.admit_held(frame(), "dave", now)
2853 .expect("first one queues");
2854 assert_eq!(
2855 gov.admit_held(frame(), "erin", now).err(),
2856 Some(RelayRejection::QueueFull),
2857 "a full queue is a full queue"
2858 );
2859 let (due, _) = gov.release_due(now);
2860 assert_eq!(due.len(), 1);
2861 assert_eq!(
2862 gov.admit_held(frame(), "dave", now).err(),
2863 Some(RelayRejection::PeerRateLimited),
2864 "one neighbor's share is spent"
2865 );
2866 // Erin's share went with the refusal above, as it does on the
2867 // ordinary path; a neighbor nobody has charged still admits one.
2868 assert!(gov.admit_held(frame(), "frank", now).is_ok());
2869 }
2870}