1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! The Exit Pool and the Proven Client acquisition every operation shares.
#![forbid(unsafe_code)]
/// Exit Pool, defined below the seam (ADR 0046).
pub(crate) use zingo_netutils::exit as exit_pool;
use std::time::{Duration, Instant};
use crate::mixnet::acquire;
/// How many proving births one acquisition may attempt before it refuses,
/// bounded so a mixnet failing everywhere refuses in a stated time rather
/// than birthing forever.
pub(crate) const MAX_PROVING_BIRTHS: usize = 6;
/// The transport surface a member needs; production implements it on the
/// spawned `MixnetProxy`.
pub(crate) trait PoolTransport: Send + 'static {
/// The transport's local SOCKS5 address, while it lives.
fn socks5_addr(&self) -> Option<std::net::SocketAddr>;
/// Tears the transport down.
fn stop(self) -> impl std::future::Future<Output = ()> + Send;
}
/// One ready client: a transport with a bound exit and the owning lease of
/// the Exit Node it bound.
pub(crate) struct Member<T> {
transport: T,
lease: exit_pool::Reservation,
}
impl<T: PoolTransport> Member<T> {
/// A member over `transport`, holding `lease` for its life.
pub(crate) fn new(transport: T, lease: exit_pool::Reservation) -> Self {
Member { transport, lease }
}
/// The bound Exit Node's identity.
pub(crate) fn node(&self) -> &crate::mixnet::ExitNodeId {
self.lease.node()
}
/// Stops the transport and recycles the lease.
pub(crate) async fn retire(self) {
self.transport.stop().await;
}
/// The tunnel address for one more contact, while the transport lives.
pub(crate) fn addr(&self) -> Option<std::net::SocketAddr> {
self.transport.socks5_addr()
}
}
/// The session's exit authority: the Exit Pool of Reservations, the
/// NodeHealthIndex behind its draws, and the acquirer Proven Clients are
/// born from.
pub(crate) struct Pools {
/// The session's sole issuer of Exit Node Reservations; reservations
/// hold a weak ledger handle and recycle themselves on drop.
pub(crate) exits: std::sync::Arc<std::sync::Mutex<exit_pool::ExitPool>>,
/// What births acquire transports from; `None` until a session sets
/// one, and always `None` for attached sessions.
acquirer: std::sync::Mutex<Option<std::sync::Arc<crate::mixnet::acquire::Acquirer>>>,
/// Which exit holds which role, recorded when boot proves its quartet.
/// The binding outlives every client, so the next client doing a job
/// binds the exit that already carries that job's role (ADR 0045).
roles: std::sync::Mutex<
std::collections::HashMap<crate::mixnet::quartet::Role, crate::mixnet::ExitNodeId>,
>,
/// The conduit boot proved for each role it has not yet spent, so the
/// job a role names runs over a proven exit without paying for a birth.
conduits:
std::sync::Mutex<std::collections::HashMap<crate::mixnet::quartet::Role, ProvenBirth>>,
}
impl Pools {
/// An empty exit authority for a fresh session.
pub(crate) fn new() -> std::sync::Arc<Self> {
std::sync::Arc::new(Pools {
exits: std::sync::Arc::new(std::sync::Mutex::new(exit_pool::ExitPool::default())),
acquirer: std::sync::Mutex::new(None),
roles: std::sync::Mutex::new(std::collections::HashMap::new()),
conduits: std::sync::Mutex::new(std::collections::HashMap::new()),
})
}
/// Holds the conduit boot proved for `role` until that role's job takes
/// it.
pub(crate) fn hold_conduit(&self, role: crate::mixnet::quartet::Role, conduit: ProvenBirth) {
self.conduits
.lock()
.expect("pool conduit mutex")
.insert(role, conduit);
}
/// Takes the conduit boot proved for `role`, leaving none behind,
/// because the job a role names runs once per boot.
pub(crate) fn take_conduit(&self, role: crate::mixnet::quartet::Role) -> Option<ProvenBirth> {
self.conduits
.lock()
.expect("pool conduit mutex")
.remove(&role)
}
/// The conduit for `role`: the one boot proved while it stands unspent,
/// and a proving birth once it is taken.
pub(crate) async fn conduit_for<A>(
&self,
role: crate::mixnet::quartet::Role,
acquirer: &A,
) -> Result<ProvenBirth, acquire::TransportError>
where
A: crate::mixnet::acquire::TransportAcquirable + ?Sized,
{
match self.take_conduit(role) {
Some(held) => Ok(held),
None => self.acquire_proven(acquirer).await,
}
}
/// Takes every conduit still held, for a teardown that must leave no
/// proxy behind.
pub(crate) fn drain_conduits(&self) -> Vec<ProvenBirth> {
self.conduits
.lock()
.expect("pool conduit mutex")
.drain()
.map(|(_role, conduit)| conduit)
.collect()
}
/// Draws one exit reservation, seeding the Exit Pool from the directory
/// first when this session has not yet learned the population.
pub(crate) async fn draw_exit<A>(
&self,
acquirer: &A,
) -> Result<exit_pool::Reservation, acquire::TransportError>
where
A: crate::mixnet::acquire::TransportAcquirable + ?Sized,
{
let seeded = self.exits.lock().expect("exit pool mutex").is_seeded();
if !seeded {
let discovered = acquirer.discover().await?;
self.exits.lock().expect("exit pool mutex").seed(discovered);
}
Ok(exit_pool::ExitPool::draw_exit(&self.exits)?)
}
/// Records what this session acquires transports from.
pub(crate) fn set_acquirer(&self, acquirer: std::sync::Arc<crate::mixnet::acquire::Acquirer>) {
*self.acquirer.lock().expect("pool acquirer mutex") = Some(acquirer);
}
/// Forgets the acquirer at session teardown, so nothing births against
/// a torn-down session.
pub(crate) fn clear_acquirer(&self) {
*self.acquirer.lock().expect("pool acquirer mutex") = None;
}
/// This session's acquirer, when one is set.
pub(crate) fn acquirer(&self) -> Option<std::sync::Arc<crate::mixnet::acquire::Acquirer>> {
self.acquirer.lock().expect("pool acquirer mutex").clone()
}
/// Records that `exit` holds `role` for as long as its proof stands.
pub(crate) fn assign_role(
&self,
role: crate::mixnet::quartet::Role,
exit: crate::mixnet::ExitNodeId,
) {
self.roles
.lock()
.expect("role binding mutex")
.insert(role, exit);
}
/// Keeps `verdict` as `exit`'s current observation, earned now.
pub(crate) fn remember(
&self,
exit: crate::mixnet::ExitNodeId,
verdict: exit_pool::ExitNodeHealthVerdict,
) {
self.exits.lock().expect("exit pool mutex").remember(
exit,
exit_pool::Observation::earned(verdict, Instant::now()),
);
}
/// Acquires one ready transport over a freshly drawn exit, publishing
/// its lifecycle into `publisher`; a bind failure names the exit it drew,
/// so the birth loop can convict it.
async fn acquire_bound<A>(
&self,
acquirer: &A,
publisher: &crate::mixnet::driver::StatusPublisher,
) -> Result<(crate::mixnet::MixnetProxy, exit_pool::Reservation), BindFailure>
where
A: crate::mixnet::acquire::TransportAcquirable + ?Sized,
{
let lease = self
.draw_exit(acquirer)
.await
.map_err(BindFailure::undrawn)?;
let nodes = vec![lease.node().clone()];
let (transport, exits) = match crate::mixnet::supervisor::acquire_ready_transport(
acquirer,
&nodes,
std::sync::Arc::clone(publisher),
)
.await
{
Ok(bound) => bound,
Err(cause) => {
return Err(BindFailure {
cause,
drawn: nodes,
});
}
};
// The birth drew one exit, so a report naming any other is a
// defective host or child: refuse typed, stop the transport, and let
// the reservation recycle on drop.
if !exits.contains(lease.node()) {
transport.stop().await;
return Err(BindFailure {
cause: acquire::TransportError::ExitOutsideClutch { reported: exits },
drawn: nodes,
});
}
Ok((transport, lease))
}
/// Births one Proven Client, carrying a [`Proof::Inherited`] when the
/// bound exit's fresh observation is trusted and a [`Proof::Earned`]
/// when the Sentinel answered, condemning the exit and trying a
/// successor when it does not, and refusing typed when every birth
/// failed its proof. A bind-stage failure spends a birth too: the drawn
/// exit is convicted and a fresh one drawn, so one slow or dead exit is
/// absorbed by the budget instead of aborting the whole acquisition
/// with nothing learned.
pub(crate) async fn acquire_proven<A>(
&self,
acquirer: &A,
) -> Result<ProvenBirth, acquire::TransportError>
where
A: crate::mixnet::acquire::TransportAcquirable + ?Sized,
{
// The birth channel is minted here, never taken from the caller, so
// no candidate lifecycle can reach the session's subscribers: the
// returned birth carries the channel, and the slot owner alone
// decides what the session hears.
let lifecycle = crate::mixnet::status_publisher();
for _birth in 0..MAX_PROVING_BIRTHS {
let (transport, lease) = match self.acquire_bound(acquirer, &lifecycle).await {
Ok(bound) => bound,
Err(failure) if failure.retryable() => {
// The whole drawn Clutch produced no ready bound
// transport within budget: convict its exits so the
// next draw learns — unless the child's own report was
// defective — and spend a birth on it.
log::warn!("a birth failed at the bind stage: {}", failure.cause);
if failure.condemns_drawn() {
for node in failure.drawn {
self.remember(node, exit_pool::ExitNodeHealthVerdict::Failed);
}
}
continue;
}
Err(failure) => return Err(failure.cause),
};
// A trusted exit's own observation carries the deadline: the
// birth inherits it rather than starting an epoch it never
// earned, so the watchdog fires when the original proof ages out.
let inherited = {
let exits = self.exits.lock().expect("exit pool mutex");
exits
.epoch_proven(lease.node(), Instant::now())
.then(|| exits.proven_until(lease.node()))
.flatten()
};
if let Some(proven_until) = inherited {
return Ok(ProvenBirth {
transport,
lease,
proof: Proof::Inherited { proven_until },
lifecycle,
});
}
let Some(socks5) = transport.socks5_addr() else {
transport.stop().await;
return Err(acquire::TransportError::DiedBeforeUse);
};
let evidence = zingo_netutils::sentinel::probe_sentinel(
socks5,
zingo_netutils::time::SENTINEL_BUDGET,
)
.await;
if let zingo_netutils::sentinel::ExitEvidence::Answered { millis } = evidence {
self.remember(
lease.node().clone(),
exit_pool::ExitNodeHealthVerdict::EpochProven,
);
return Ok(ProvenBirth {
transport,
lease,
proof: Proof::Earned {
round_trip: Duration::from_millis(millis),
},
lifecycle,
});
}
self.remember(
lease.node().clone(),
exit_pool::ExitNodeHealthVerdict::Failed,
);
transport.stop().await;
}
Err(acquire::TransportError::NoProvenExit {
probed: MAX_PROVING_BIRTHS,
budget: zingo_netutils::time::SENTINEL_BUDGET,
})
}
}
/// One failed bind: its typed cause, and the exits the failed Clutch drew.
struct BindFailure {
/// The typed failure the bind stage produced.
cause: acquire::TransportError,
/// The drawn exits, none of which produced a ready bound transport.
drawn: Vec<crate::mixnet::ExitNodeId>,
}
impl BindFailure {
/// A failure from before any Clutch was drawn, which no retry helps.
fn undrawn(cause: acquire::TransportError) -> Self {
BindFailure {
cause,
drawn: Vec::new(),
}
}
/// Whether spending another birth can help: true for the failures of a
/// drawn Clutch that would not repeat on fresh exits, false for the
/// environment's own refusals — a missing binary, an unreachable host,
/// an unseeded or exhausted pool.
fn retryable(&self) -> bool {
match &self.cause {
acquire::TransportError::NotReady { .. }
| acquire::TransportError::DiedDuringBootstrap { .. }
| acquire::TransportError::StatusChannelClosed
| acquire::TransportError::ExitOutsideClutch { .. } => !self.drawn.is_empty(),
_ => false,
}
}
/// Whether the failure indicts the drawn exits themselves: a Clutch
/// that never became ready condemns its exits, while a defective exit
/// report indicts the child, not the exits it failed to name.
fn condemns_drawn(&self) -> bool {
!matches!(
self.cause,
acquire::TransportError::ExitOutsideClutch { .. }
)
}
}
/// One Proven Client's birth: the transport, its bound exit's lease, and
/// whether the proof was earned by this birth's own Sentinel answer rather
/// than trusted from a stale EpochProven observation.
/// What a birth's proof rests on, which decides how long the client may be
/// trusted before a fresh ProofAcquisition is due.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Proof {
/// The birth answered the Sentinel itself, so its exit is proven for a
/// whole epoch from now.
Earned {
/// How long the Sentinel's round trip took.
round_trip: Duration,
},
/// The birth trusted a fresh `EpochProven` observation an earlier client
/// earned, so it inherits that observation's expiry rather than starting
/// an epoch of its own.
Inherited {
/// When the inherited observation stops being epoch-fresh.
proven_until: Instant,
},
}
pub(crate) struct ProvenBirth {
/// The ready transport.
pub(crate) transport: crate::mixnet::MixnetProxy,
/// The bound exit's lease.
pub(crate) lease: exit_pool::Reservation,
/// What this birth's proof rests on.
pub(crate) proof: Proof,
/// The birth's own status channel, carrying the condemned candidates'
/// churn and the settled client's later transitions.
pub(crate) lifecycle: crate::mixnet::driver::StatusPublisher,
}
#[cfg(test)]
mod tests {
use super::*;
struct FakeTransport {
ready: bool,
}
impl PoolTransport for FakeTransport {
fn socks5_addr(&self) -> Option<std::net::SocketAddr> {
self.ready
.then(|| "127.0.0.1:7".parse().expect("the fake address parses"))
}
fn stop(self) -> impl std::future::Future<Output = ()> + Send {
std::future::ready(())
}
}
/// HYPOTHESIS: a member serves repeated dials for its one holder, then
/// retires once.
#[tokio::test]
async fn a_member_serves_repeated_dials() {
let member: Member<FakeTransport> = Member::new(
FakeTransport { ready: true },
exit_pool::Reservation::dangling_for_test("exit-shared"),
);
let first = member.addr().expect("a live shared member has an address");
let second = member.addr().expect("a second dial is permitted");
assert_eq!(first, second, "one tunnel serves the whole fan-out");
member.retire().await;
}
/// HYPOTHESIS: clearing the acquirer forbids later births, the teardown
/// property vacating a session relies on.
#[test]
fn clearing_the_acquirer_forbids_later_births() {
let pools = Pools::new();
assert!(
pools.acquirer().is_none(),
"a fresh session has no acquirer"
);
pools.clear_acquirer();
assert!(pools.acquirer().is_none(), "clearing is idempotent");
}
}
#[cfg(test)]
mod bind_failure_absorption {
//! Finding F2's contract: a bind-stage failure spends a birth and
//! convicts the drawn exits instead of escaping the six-birth loop.
use std::sync::atomic::{AtomicUsize, Ordering};
use super::*;
/// An acquirer whose every acquisition fails at the bind stage, with a
/// census of exactly one exit per birth, so every drawn exit is
/// distinct and every one of them is convicted.
struct BindRefusingAcquirer {
census: usize,
acquisitions: AtomicUsize,
}
impl crate::mixnet::acquire::TransportAcquirable for BindRefusingAcquirer {
async fn discover(
&self,
) -> Result<std::collections::HashSet<crate::mixnet::ExitNodeId>, acquire::TransportError>
{
Ok((0..self.census)
.map(|index| crate::mixnet::ExitNodeId::from(format!("exit-{index}").as_str()))
.collect())
}
async fn acquire(
&self,
_clutch: &[crate::mixnet::ExitNodeId],
_publisher: crate::mixnet::driver::StatusPublisher,
) -> Result<crate::mixnet::MixnetProxy, acquire::TransportError> {
self.acquisitions.fetch_add(1, Ordering::AcqRel);
Err(acquire::TransportError::NotReady {
budget: zingo_netutils::time::NYM_LIFECYCLE_TIMEOUT,
})
}
fn rotation_verdict(&self) -> zingo_netutils::provider::RotationVerdict {
zingo_netutils::provider::RotationVerdict::Never
}
}
/// HYPOTHESIS: a bind-stage failure spends one of the six births and
/// convicts every drawn exit, so the loop absorbs it and the final
/// refusal is the typed NoProvenExit — never the first bind error
/// escaping with nothing learned. Falsified if the loop exits early or
/// the drawn-and-dead exits stay eligible.
#[tokio::test]
async fn a_bind_failure_spends_a_birth_and_convicts_the_drawn() {
let pools = Pools::new();
let acquirer = BindRefusingAcquirer {
census: MAX_PROVING_BIRTHS,
acquisitions: AtomicUsize::new(0),
};
let Err(refusal) = pools.acquire_proven(&acquirer).await else {
panic!("every bind fails, so no birth can succeed");
};
assert!(
matches!(
refusal,
acquire::TransportError::NoProvenExit { probed, .. }
if probed == MAX_PROVING_BIRTHS
),
"the loop absorbs bind failures into its budget, got: {refusal}"
);
assert_eq!(
acquirer.acquisitions.load(Ordering::Acquire),
MAX_PROVING_BIRTHS,
"every birth was spent on an acquisition"
);
let convicted = {
let exits = pools.exits.lock().expect("exit pool mutex");
let now = Instant::now();
(0..acquirer.census)
.filter(|index| {
exits.epoch_failed(
&crate::mixnet::ExitNodeId::from(format!("exit-{index}").as_str()),
now,
)
})
.count()
};
assert_eq!(
convicted, acquirer.census,
"every drawn-and-dead exit is convicted rather than staying eligible"
);
}
}