1
//! Facilities to build circuits directly, instead of via a circuit manager.
2

            
3
use crate::path::{OwnedPath, TorPath};
4
use crate::timeouts::{self, Action};
5
use crate::{Error, Result};
6
use async_trait::async_trait;
7
use futures::Future;
8
use futures::task::SpawnExt;
9
use oneshot_fused_workaround as oneshot;
10
use std::sync::{
11
    Arc,
12
    atomic::{AtomicU32, Ordering},
13
};
14
use std::time::{Duration, Instant};
15
use tor_chanmgr::{ChanMgr, ChanProvenance, ChannelUsage};
16
use tor_error::into_internal;
17
use tor_guardmgr::GuardStatus;
18
use tor_linkspec::{IntoOwnedChanTarget, OwnedChanTarget, OwnedCircTarget};
19
use tor_netdir::params::NetParameters;
20
use tor_proto::ccparams::{self, AlgorithmType};
21
use tor_proto::client::circuit::{CircParameters, PendingClientTunnel};
22
use tor_proto::{CellCount, ClientTunnel, FlowCtrlParameters};
23
use tor_rtcompat::{Runtime, SleepProviderExt};
24
use tor_units::Percentage;
25

            
26
#[cfg(all(feature = "vanguards", feature = "hs-common"))]
27
use tor_guardmgr::vanguards::VanguardMgr;
28

            
29
mod guardstatus;
30

            
31
pub(crate) use guardstatus::GuardStatusHandle;
32

            
33
/// Represents an objects that can be constructed in a circuit-like way.
34
///
35
/// This is only a separate trait for testing purposes, so that we can swap
36
/// our some other type when we're testing Builder.
37
///
38
/// TODO: I'd like to have a simpler testing strategy here; this one
39
/// complicates things a bit.
40
#[async_trait]
41
pub(crate) trait Buildable: Sized {
42
    /// Our equivalent to a tor_proto::Channel.
43
    type Chan: Send + Sync;
44

            
45
    /// Use a channel manager to open a new channel (or find an existing channel)
46
    /// to a provided [`OwnedChanTarget`].
47
    async fn open_channel<RT: Runtime>(
48
        chanmgr: &ChanMgr<RT>,
49
        ct: &OwnedChanTarget,
50
        guard_status: &GuardStatusHandle,
51
        usage: ChannelUsage,
52
    ) -> Result<Arc<Self::Chan>>;
53

            
54
    /// Launch a new one-hop circuit to a given relay, given only a
55
    /// channel target `ct` specifying that relay.
56
    ///
57
    /// (Since we don't have a CircTarget here, we can't extend the circuit
58
    /// to be multihop later on.)
59
    async fn create_chantarget<RT: Runtime>(
60
        chan: Arc<Self::Chan>,
61
        rt: &RT,
62
        ct: &OwnedChanTarget,
63
        params: CircParameters,
64
        timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
65
    ) -> Result<Self>;
66

            
67
    /// Launch a new circuit through a given relay, given a circuit target
68
    /// `ct` specifying that relay.
69
    async fn create<RT: Runtime>(
70
        chan: Arc<Self::Chan>,
71
        rt: &RT,
72
        ct: &OwnedCircTarget,
73
        params: CircParameters,
74
        timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
75
    ) -> Result<Self>;
76

            
77
    /// Extend this circuit-like object by one hop, to the location described
78
    /// in `ct`.
79
    async fn extend<RT: Runtime>(
80
        &self,
81
        rt: &RT,
82
        ct: &OwnedCircTarget,
83
        params: CircParameters,
84
    ) -> Result<()>;
85
}
86

            
87
/// Try to make a [`PendingClientTunnel`] to a given relay, and start its
88
/// reactor.
89
///
90
/// This is common code, shared by all the first-hop functions in the
91
/// implementation of `Buildable` for `ClientTunnel`.
92
async fn create_common<RT: Runtime>(
93
    chan: Arc<tor_proto::channel::Channel>,
94
    timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
95
    rt: &RT,
96
) -> Result<PendingClientTunnel> {
97
    // Construct the (zero-hop) circuit.
98
    let (pending_tunnel, reactor) =
99
        chan.new_tunnel(timeouts)
100
            .await
101
            .map_err(|error| Error::Protocol {
102
                error,
103
                peer: None, // we don't blame the peer, because new_circ() does no networking.
104
                action: "initializing circuit",
105
                unique_id: None,
106
            })?;
107

            
108
    rt.spawn(async {
109
        let _ = reactor.run().await;
110
    })
111
    .map_err(|e| Error::from_spawn("circuit reactor task", e))?;
112

            
113
    Ok(pending_tunnel)
114
}
115

            
116
#[async_trait]
117
impl Buildable for ClientTunnel {
118
    type Chan = tor_proto::channel::Channel;
119

            
120
    async fn open_channel<RT: Runtime>(
121
        chanmgr: &ChanMgr<RT>,
122
        target: &OwnedChanTarget,
123
        guard_status: &GuardStatusHandle,
124
        usage: ChannelUsage,
125
    ) -> Result<Arc<Self::Chan>> {
126
        // If we fail now, it's the guard's fault.
127
        guard_status.pending(GuardStatus::Failure);
128

            
129
        // Get or construct the channel.
130
        let result = chanmgr.get_or_launch(target, usage).await;
131

            
132
        // Report the clock skew if appropriate, and exit if there has been an error.
133
        match result {
134
            Ok((chan, ChanProvenance::NewlyCreated)) => {
135
                guard_status.skew(chan.clock_skew());
136
                Ok(chan)
137
            }
138
            Ok((chan, _)) => Ok(chan),
139
            Err(cause) => {
140
                if let Some(skew) = cause.clock_skew() {
141
                    guard_status.skew(skew);
142
                }
143
                Err(Error::Channel {
144
                    peer: target.to_logged(),
145
                    cause,
146
                })
147
            }
148
        }
149
    }
150

            
151
    async fn create_chantarget<RT: Runtime>(
152
        chan: Arc<Self::Chan>,
153
        rt: &RT,
154
        ct: &OwnedChanTarget,
155
        params: CircParameters,
156
        timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
157
    ) -> Result<Self> {
158
        let pending_tunnel = create_common(chan, timeouts, rt).await?;
159
        let unique_id = Some(pending_tunnel.peek_unique_id());
160
        pending_tunnel
161
            .create_firsthop_fast(params)
162
            .await
163
            .map_err(|error| Error::Protocol {
164
                peer: Some(ct.to_logged()),
165
                error,
166
                action: "running CREATE_FAST handshake",
167
                unique_id,
168
            })
169
    }
170
    async fn create<RT: Runtime>(
171
        chan: Arc<Self::Chan>,
172
        rt: &RT,
173
        ct: &OwnedCircTarget,
174
        params: CircParameters,
175
        timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
176
    ) -> Result<Self> {
177
        let pending_tunnel = create_common(chan, timeouts, rt).await?;
178
        let unique_id = Some(pending_tunnel.peek_unique_id());
179

            
180
        let handshake_res = pending_tunnel.create_firsthop(ct, params).await;
181
        handshake_res.map_err(|error| Error::Protocol {
182
            peer: Some(ct.to_logged()),
183
            error,
184
            action: "creating first hop",
185
            unique_id,
186
        })
187
    }
188
    async fn extend<RT: Runtime>(
189
        &self,
190
        _rt: &RT,
191
        ct: &OwnedCircTarget,
192
        params: CircParameters,
193
    ) -> Result<()> {
194
        let circ = self.as_single_circ().map_err(|error| Error::Protocol {
195
            peer: Some(ct.to_logged()),
196
            error,
197
            action: "extend tunnel",
198
            unique_id: Some(self.unique_id()),
199
        })?;
200

            
201
        let res = circ.extend(ct, params).await;
202
        res.map_err(|error| Error::Protocol {
203
            error,
204
            // We can't know who caused the error, since it may have been
205
            // the hop we were extending from, or the hop we were extending
206
            // to.
207
            peer: None,
208
            action: "extending circuit",
209
            unique_id: Some(self.unique_id()),
210
        })
211
    }
212
}
213

            
214
/// An implementation type for [`TunnelBuilder`].
215
///
216
/// A `TunnelBuilder` holds references to all the objects that are needed
217
/// to build circuits correctly.
218
///
219
/// In general, you should not need to construct or use this object yourself,
220
/// unless you are choosing your own paths.
221
struct Builder<R: Runtime, C: Buildable + Sync + Send + 'static> {
222
    /// The runtime used by this circuit builder.
223
    runtime: R,
224
    /// A channel manager that this circuit builder uses to make channels.
225
    chanmgr: Arc<ChanMgr<R>>,
226
    /// An estimator to determine the correct timeouts for circuit building.
227
    timeouts: Arc<timeouts::Estimator>,
228
    /// We don't actually hold any clientcircs, so we need to put this
229
    /// type here so the compiler won't freak out.
230
    _phantom: std::marker::PhantomData<C>,
231
}
232

            
233
impl<R: Runtime, C: Buildable + Sync + Send + 'static> Builder<R, C> {
234
    /// Construct a new [`Builder`].
235
50
    fn new(runtime: R, chanmgr: Arc<ChanMgr<R>>, timeouts: timeouts::Estimator) -> Self {
236
50
        Builder {
237
50
            runtime,
238
50
            chanmgr,
239
50
            timeouts: Arc::new(timeouts),
240
50
            _phantom: std::marker::PhantomData,
241
50
        }
242
50
    }
243

            
244
    /// Build a circuit, without performing any timeout operations.
245
    ///
246
    /// After each hop is built, increments n_hops_built.  Make sure that
247
    /// `guard_status` has its pending status set correctly to correspond
248
    /// to a circuit failure at any given stage.
249
    ///
250
    /// Requires that `channel` is a channel to the first hop of `path`.
251
    ///
252
    /// (TODO: Find
253
    /// a better design there.)
254
16
    async fn build_notimeout(
255
16
        self: Arc<Self>,
256
16
        path: OwnedPath,
257
16
        channel: Arc<C::Chan>,
258
16
        params: CircParameters,
259
16
        start_time: Instant,
260
16
        n_hops_built: Arc<AtomicU32>,
261
16
        guard_status: Arc<GuardStatusHandle>,
262
16
    ) -> Result<C> {
263
16
        match path {
264
4
            OwnedPath::ChannelOnly(target) => {
265
4
                let timeouts = Arc::clone(&self.timeouts);
266
4
                let circ =
267
4
                    C::create_chantarget(channel, &self.runtime, &target, params, timeouts).await?;
268
4
                self.timeouts
269
4
                    .note_hop_completed(0, self.runtime.now() - start_time, true);
270
4
                n_hops_built.fetch_add(1, Ordering::SeqCst);
271
4
                Ok(circ)
272
            }
273
12
            OwnedPath::Normal(p) => {
274
12
                assert!(!p.is_empty());
275
12
                let n_hops = p.len() as u8;
276
12
                let timeouts = Arc::clone(&self.timeouts);
277
                // Each hop has its own circ parameters. This is for the first hop (CREATE).
278
12
                let circ =
279
12
                    C::create(channel, &self.runtime, &p[0], params.clone(), timeouts).await?;
280
12
                self.timeouts
281
12
                    .note_hop_completed(0, self.runtime.now() - start_time, n_hops == 0);
282
                // If we fail after this point, we can't tell whether it's
283
                // the fault of the guard or some later relay.
284
12
                guard_status.pending(GuardStatus::Indeterminate);
285
12
                n_hops_built.fetch_add(1, Ordering::SeqCst);
286
12
                let mut hop_num = 1;
287
24
                for relay in p[1..].iter() {
288
                    // Get the params per subsequent hop (EXTEND).
289
24
                    circ.extend(&self.runtime, relay, params.clone()).await?;
290
20
                    n_hops_built.fetch_add(1, Ordering::SeqCst);
291
20
                    self.timeouts.note_hop_completed(
292
20
                        hop_num,
293
20
                        self.runtime.now() - start_time,
294
20
                        hop_num == (n_hops - 1),
295
20
                    );
296
20
                    hop_num += 1;
297
                }
298
8
                Ok(circ)
299
            }
300
        }
301
12
    }
302

            
303
    /// Build a circuit from an [`OwnedPath`].
304
16
    async fn build_owned(
305
16
        self: &Arc<Self>,
306
16
        path: OwnedPath,
307
16
        params: &CircParameters,
308
16
        guard_status: Arc<GuardStatusHandle>,
309
16
        usage: ChannelUsage,
310
16
    ) -> Result<C> {
311
16
        let action = Action::BuildCircuit { length: path.len() };
312
16
        let (timeout, abandon_timeout) = self.timeouts.timeouts(&action);
313

            
314
        // TODO: This is probably not the best way for build_notimeout to
315
        // tell us how many hops it managed to build, but at least it is
316
        // isolated here.
317
16
        let hops_built = Arc::new(AtomicU32::new(0));
318

            
319
16
        let self_clone = Arc::clone(self);
320
16
        let params = params.clone();
321

            
322
        // We open the channel separately from the rest of the circuit, since we don't want to count
323
        // it towards the circuit timeout.
324
        //
325
        // We don't need a separate timeout here, since ChanMgr already implements its own timeouts.
326
16
        let channel = C::open_channel(
327
16
            &self.chanmgr,
328
16
            path.first_hop_as_chantarget(),
329
16
            guard_status.as_ref(),
330
16
            usage,
331
16
        )
332
16
        .await?;
333

            
334
16
        let start_time = self.runtime.now();
335

            
336
16
        let circuit_future = self_clone.build_notimeout(
337
16
            path,
338
16
            channel,
339
16
            params,
340
16
            start_time,
341
16
            Arc::clone(&hops_built),
342
16
            guard_status,
343
        );
344

            
345
16
        match double_timeout(&self.runtime, circuit_future, timeout, abandon_timeout).await {
346
8
            Ok(circuit) => Ok(circuit),
347
8
            Err(Error::CircTimeout(unique_id)) => {
348
8
                let n_built = hops_built.load(Ordering::SeqCst);
349
8
                self.timeouts
350
8
                    .note_circ_timeout(n_built as u8, self.runtime.now() - start_time);
351
8
                Err(Error::CircTimeout(unique_id))
352
            }
353
            Err(e) => Err(e),
354
        }
355
16
    }
356

            
357
    /// Return a reference to this Builder runtime.
358
    pub(crate) fn runtime(&self) -> &R {
359
        &self.runtime
360
    }
361

            
362
    /// Return a reference to this Builder's timeout estimator.
363
    pub(crate) fn estimator(&self) -> &timeouts::Estimator {
364
        &self.timeouts
365
    }
366
}
367

            
368
/// A factory object to build circuits.
369
///
370
/// A `TunnelBuilder` holds references to all the objects that are needed
371
/// to build circuits correctly.
372
///
373
/// In general, you should not need to construct or use this object yourself,
374
/// unless you are choosing your own paths.
375
pub struct TunnelBuilder<R: Runtime> {
376
    /// The underlying [`Builder`] object
377
    builder: Arc<Builder<R, ClientTunnel>>,
378
    /// Configuration for how to choose paths for circuits.
379
    path_config: tor_config::MutCfg<crate::PathConfig>,
380
    /// State-manager object to use in storing current state.
381
    storage: crate::TimeoutStateHandle,
382
    /// Guard manager to tell us which guards nodes to use for the circuits
383
    /// we build.
384
    guardmgr: tor_guardmgr::GuardMgr<R>,
385
    /// The vanguard manager object used for HS circuits.
386
    #[cfg(all(feature = "vanguards", feature = "hs-common"))]
387
    vanguardmgr: Arc<VanguardMgr<R>>,
388
}
389

            
390
impl<R: Runtime> TunnelBuilder<R> {
391
    /// Construct a new [`TunnelBuilder`].
392
    // TODO: eventually I'd like to make this a public function, but
393
    // TimeoutStateHandle is private.
394
34
    pub(crate) fn new(
395
34
        runtime: R,
396
34
        chanmgr: Arc<ChanMgr<R>>,
397
34
        path_config: crate::PathConfig,
398
34
        storage: crate::TimeoutStateHandle,
399
34
        guardmgr: tor_guardmgr::GuardMgr<R>,
400
34
        #[cfg(all(feature = "vanguards", feature = "hs-common"))] vanguardmgr: VanguardMgr<R>,
401
34
    ) -> Self {
402
34
        let timeouts = timeouts::Estimator::from_storage(&storage);
403

            
404
34
        TunnelBuilder {
405
34
            builder: Arc::new(Builder::new(runtime, chanmgr, timeouts)),
406
34
            path_config: path_config.into(),
407
34
            storage,
408
34
            guardmgr,
409
34
            #[cfg(all(feature = "vanguards", feature = "hs-common"))]
410
34
            vanguardmgr: Arc::new(vanguardmgr),
411
34
        }
412
34
    }
413

            
414
    /// Return this builder's [`PathConfig`](crate::PathConfig).
415
8
    pub(crate) fn path_config(&self) -> Arc<crate::PathConfig> {
416
8
        self.path_config.get()
417
8
    }
418

            
419
    /// Replace this builder's [`PathConfig`](crate::PathConfig).
420
4
    pub(crate) fn set_path_config(&self, new_config: crate::PathConfig) {
421
4
        self.path_config.replace(new_config);
422
4
    }
423

            
424
    /// Flush state to the state manager if we own the lock.
425
    ///
426
    /// Return `Ok(true)` if we saved, and `Ok(false)` if we didn't hold the lock.
427
36
    pub(crate) fn save_state(&self) -> Result<bool> {
428
36
        if !self.storage.can_store() {
429
12
            return Ok(false);
430
24
        }
431
        // TODO: someday we'll want to only do this if there is something
432
        // changed.
433
24
        self.builder.timeouts.save_state(&self.storage)?;
434
24
        self.guardmgr.store_persistent_state()?;
435
24
        Ok(true)
436
36
    }
437

            
438
    /// Replace our state with a new owning state, assuming we have
439
    /// storage permission.
440
    pub(crate) fn upgrade_to_owned_state(&self) -> Result<()> {
441
        self.builder
442
            .timeouts
443
            .upgrade_to_owning_storage(&self.storage);
444
        self.guardmgr.upgrade_to_owned_persistent_state()?;
445
        Ok(())
446
    }
447

            
448
    /// Reload persistent state from disk, if we don't have storage permission.
449
    pub(crate) fn reload_state(&self) -> Result<()> {
450
        if !self.storage.can_store() {
451
            self.builder
452
                .timeouts
453
                .reload_readonly_from_storage(&self.storage);
454
        }
455
        self.guardmgr.reload_persistent_state()?;
456
        Ok(())
457
    }
458

            
459
    /// Reconfigure this builder using the latest set of network parameters.
460
    ///
461
    /// (NOTE: for now, this only affects circuit timeout estimation.)
462
    pub fn update_network_parameters(&self, p: &tor_netdir::params::NetParameters) {
463
        self.builder.timeouts.update_params(p);
464
    }
465

            
466
    /// Like `build`, but construct a new circuit from an [`OwnedPath`].
467
    pub(crate) async fn build_owned(
468
        &self,
469
        path: OwnedPath,
470
        params: &CircParameters,
471
        guard_status: Arc<GuardStatusHandle>,
472
        usage: ChannelUsage,
473
    ) -> Result<ClientTunnel> {
474
        self.builder
475
            .build_owned(path, params, guard_status, usage)
476
            .await
477
    }
478

            
479
    /// Try to construct a new circuit from a given path, using appropriate
480
    /// timeouts.
481
    ///
482
    /// This circuit is _not_ automatically registered with any
483
    /// circuit manager; if you don't hang on it it, it will
484
    /// automatically go away when the last reference is dropped.
485
    pub async fn build(
486
        &self,
487
        path: &TorPath<'_>,
488
        params: &CircParameters,
489
        usage: ChannelUsage,
490
    ) -> Result<ClientTunnel> {
491
        let owned = path.try_into()?;
492
        self.build_owned(owned, params, Arc::new(None.into()), usage)
493
            .await
494
    }
495

            
496
    /// Return true if this builder is currently learning timeout info.
497
    pub(crate) fn learning_timeouts(&self) -> bool {
498
        self.builder.timeouts.learning_timeouts()
499
    }
500

            
501
    /// Return a reference to this builder's `GuardMgr`.
502
32
    pub(crate) fn guardmgr(&self) -> &tor_guardmgr::GuardMgr<R> {
503
32
        &self.guardmgr
504
32
    }
505

            
506
    /// Return a reference to this builder's `VanguardMgr`.
507
    #[cfg(all(feature = "vanguards", feature = "hs-common"))]
508
30
    pub(crate) fn vanguardmgr(&self) -> &Arc<VanguardMgr<R>> {
509
30
        &self.vanguardmgr
510
30
    }
511

            
512
    /// Return a reference to this builder's runtime
513
    pub(crate) fn runtime(&self) -> &R {
514
        self.builder.runtime()
515
    }
516

            
517
    /// Return a reference to this builder's timeout estimator.
518
    pub(crate) fn estimator(&self) -> &timeouts::Estimator {
519
        self.builder.estimator()
520
    }
521
}
522

            
523
/// Return the congestion control Vegas algorithm using the given network parameters.
524
#[cfg(feature = "flowctl-cc")]
525
22
fn build_cc_vegas(
526
22
    inp: &NetParameters,
527
22
    vegas_queue_params: ccparams::VegasQueueParams,
528
22
) -> ccparams::Algorithm {
529
22
    ccparams::Algorithm::Vegas(
530
22
        ccparams::VegasParamsBuilder::default()
531
22
            .cell_in_queue_params(vegas_queue_params)
532
22
            .ss_cwnd_max(inp.cc_ss_max.into())
533
22
            .cwnd_full_gap(inp.cc_cwnd_full_gap.into())
534
22
            .cwnd_full_min_pct(Percentage::new(
535
22
                inp.cc_cwnd_full_minpct.as_percent().get() as u32
536
22
            ))
537
22
            .cwnd_full_per_cwnd(inp.cc_cwnd_full_per_cwnd.into())
538
22
            .build()
539
22
            .expect("Unable to build Vegas params from NetParams"),
540
22
    )
541
22
}
542

            
543
/// Return the congestion control FixedWindow algorithm using the given network parameters.
544
fn build_cc_fixedwindow(inp: &NetParameters) -> ccparams::Algorithm {
545
    ccparams::Algorithm::FixedWindow(build_cc_fixedwindow_params(inp))
546
}
547

            
548
/// Return the parameters for the congestion control FixedWindow algorithm
549
/// using the given network parameters.
550
22
fn build_cc_fixedwindow_params(inp: &NetParameters) -> ccparams::FixedWindowParams {
551
22
    ccparams::FixedWindowParamsBuilder::default()
552
22
        .circ_window_start(inp.circuit_window.get() as u16)
553
22
        .circ_window_min(inp.circuit_window.lower() as u16)
554
22
        .circ_window_max(inp.circuit_window.upper() as u16)
555
22
        .build()
556
22
        .expect("Unable to build FixedWindow params from NetParams")
557
22
}
558

            
559
/// Return a new circuit parameter struct using the given network parameters and algorithm to use.
560
22
fn circparameters_from_netparameters(
561
22
    inp: &NetParameters,
562
22
    alg: ccparams::Algorithm,
563
22
) -> Result<CircParameters> {
564
22
    let cwnd_params = ccparams::CongestionWindowParamsBuilder::default()
565
22
        .cwnd_init(inp.cc_cwnd_init.into())
566
22
        .cwnd_inc_pct_ss(Percentage::new(
567
22
            inp.cc_cwnd_inc_pct_ss.as_percent().get() as u32
568
22
        ))
569
22
        .cwnd_inc(inp.cc_cwnd_inc.into())
570
22
        .cwnd_inc_rate(inp.cc_cwnd_inc_rate.into())
571
22
        .cwnd_min(inp.cc_cwnd_min.into())
572
22
        .cwnd_max(inp.cc_cwnd_max.into())
573
22
        .sendme_inc(inp.cc_sendme_inc.into())
574
22
        .build()
575
22
        .map_err(into_internal!(
576
            "Unable to build CongestionWindow params from NetParams"
577
        ))?;
578
22
    let rtt_params = ccparams::RoundTripEstimatorParamsBuilder::default()
579
22
        .ewma_cwnd_pct(Percentage::new(
580
22
            inp.cc_ewma_cwnd_pct.as_percent().get() as u32
581
22
        ))
582
22
        .ewma_max(inp.cc_ewma_max.into())
583
22
        .ewma_ss_max(inp.cc_ewma_ss.into())
584
22
        .rtt_reset_pct(Percentage::new(
585
22
            inp.cc_rtt_reset_pct.as_percent().get() as u32
586
22
        ))
587
22
        .build()
588
22
        .map_err(into_internal!("Unable to build RTT params from NetParams"))?;
589
22
    let ccontrol = ccparams::CongestionControlParamsBuilder::default()
590
22
        .alg(alg)
591
22
        .fixed_window_params(build_cc_fixedwindow_params(inp))
592
22
        .cwnd_params(cwnd_params)
593
22
        .rtt_params(rtt_params)
594
22
        .build()
595
22
        .map_err(into_internal!(
596
            "Unable to build CongestionControl params from NetParams"
597
        ))?;
598
22
    let flow_ctrl_params = FlowCtrlParameters {
599
22
        cc_xoff_client: CellCount::new(inp.cc_xoff_client.get_u32()),
600
22
        cc_xoff_exit: CellCount::new(inp.cc_xoff_exit.get_u32()),
601
22
        cc_xon_rate: CellCount::new(inp.cc_xon_rate.get_u32()),
602
22
        cc_xon_change_pct: inp.cc_xon_change_pct.get_u32(),
603
22
        cc_xon_ewma_cnt: inp.cc_xon_ewma_cnt.get_u32(),
604
22
    };
605
22
    Ok(CircParameters::new(
606
22
        inp.extend_by_ed25519_id.into(),
607
22
        ccontrol,
608
22
        flow_ctrl_params,
609
22
    ))
610
22
}
611

            
612
/// Extract a [`CircParameters`] from the [`NetParameters`] from a consensus for an exit circuit or
613
/// single onion service (when implemented).
614
22
pub fn exit_circparams_from_netparams(inp: &NetParameters) -> Result<CircParameters> {
615
22
    let alg = match AlgorithmType::from(inp.cc_alg.get()) {
616
        #[cfg(feature = "flowctl-cc")]
617
22
        AlgorithmType::VEGAS => build_cc_vegas(
618
22
            inp,
619
22
            (
620
22
                inp.cc_vegas_alpha_exit.into(),
621
22
                inp.cc_vegas_beta_exit.into(),
622
22
                inp.cc_vegas_delta_exit.into(),
623
22
                inp.cc_vegas_gamma_exit.into(),
624
22
                inp.cc_vegas_sscap_exit.into(),
625
22
            )
626
22
                .into(),
627
        ),
628
        // Unrecognized, fallback to fixed window as in SENDME v0.
629
        _ => build_cc_fixedwindow(inp),
630
    };
631
22
    circparameters_from_netparameters(inp, alg)
632
22
}
633

            
634
/// Extract a [`CircParameters`] from the [`NetParameters`] from a consensus for an onion circuit
635
/// which also includes an onion service with Vanguard.
636
pub fn onion_circparams_from_netparams(inp: &NetParameters) -> Result<CircParameters> {
637
    let alg = match AlgorithmType::from(inp.cc_alg.get()) {
638
        #[cfg(feature = "flowctl-cc")]
639
        AlgorithmType::VEGAS => {
640
            // NOTE: At the time of writing, we don't yet support cc negotiation for onion services.
641
            // See `HopSettings::onion_circparams_from_netparams()` where we use a fallback
642
            // algorithm for HsV3 circuits instead, and see arti#2037.
643
            build_cc_vegas(
644
                inp,
645
                (
646
                    inp.cc_vegas_alpha_onion.into(),
647
                    inp.cc_vegas_beta_onion.into(),
648
                    inp.cc_vegas_delta_onion.into(),
649
                    inp.cc_vegas_gamma_onion.into(),
650
                    inp.cc_vegas_sscap_onion.into(),
651
                )
652
                    .into(),
653
            )
654
        }
655
        // Unrecognized, fallback to fixed window as in SENDME v0.
656
        _ => build_cc_fixedwindow(inp),
657
    };
658
    circparameters_from_netparameters(inp, alg)
659
}
660

            
661
/// Helper function: spawn a future as a background task, and run it with
662
/// two separate timeouts.
663
///
664
/// If the future does not complete by `timeout`, then return a
665
/// timeout error immediately, but keep running the future in the
666
/// background.
667
///
668
/// If the future does not complete by `abandon`, then abandon the
669
/// future completely.
670
32
async fn double_timeout<R, F, T>(
671
32
    runtime: &R,
672
32
    fut: F,
673
32
    timeout: Duration,
674
32
    abandon: Duration,
675
32
) -> Result<T>
676
32
where
677
32
    R: Runtime,
678
32
    F: Future<Output = Result<T>> + Send + 'static,
679
32
    T: Send + 'static,
680
32
{
681
32
    let (snd, rcv) = oneshot::channel();
682
32
    let rt = runtime.clone();
683
    // We create these futures now, since we want them to look at the current
684
    // time when they decide when to expire.
685
32
    let inner_timeout_future = rt.timeout(abandon, fut);
686
32
    let outer_timeout_future = rt.timeout(timeout, rcv);
687

            
688
32
    runtime
689
32
        .spawn(async move {
690
32
            let result = inner_timeout_future.await;
691
32
            let _ignore_cancelled_error = snd.send(result);
692
32
        })
693
32
        .map_err(|e| Error::from_spawn("circuit construction task", e))?;
694

            
695
32
    let outcome = outer_timeout_future.await;
696
    // 4 layers of error to collapse:
697
    //     One from the receiver being cancelled.
698
    //     One from the outer timeout.
699
    //     One from the inner timeout.
700
    //     One from the actual future's result.
701
    //
702
    // (Technically, we could refrain from unwrapping the future's result,
703
    // but doing it this way helps make it more certain that we really are
704
    // collapsing all the layers into one.)
705
32
    outcome
706
32
        .map_err(|_| Error::CircTimeout(None))??
707
16
        .map_err(|_| Error::CircTimeout(None))?
708
32
}
709

            
710
#[cfg(test)]
711
mod test {
712
    // @@ begin test lint list maintained by maint/add_warning @@
713
    #![allow(clippy::bool_assert_comparison)]
714
    #![allow(clippy::clone_on_copy)]
715
    #![allow(clippy::dbg_macro)]
716
    #![allow(clippy::mixed_attributes_style)]
717
    #![allow(clippy::print_stderr)]
718
    #![allow(clippy::print_stdout)]
719
    #![allow(clippy::single_char_pattern)]
720
    #![allow(clippy::unwrap_used)]
721
    #![allow(clippy::unchecked_duration_subtraction)]
722
    #![allow(clippy::useless_vec)]
723
    #![allow(clippy::needless_pass_by_value)]
724
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
725
    use super::*;
726
    use crate::timeouts::TimeoutEstimator;
727
    use futures::FutureExt;
728
    use std::sync::Mutex;
729
    use tor_chanmgr::ChannelConfig;
730
    use tor_chanmgr::ChannelUsage as CU;
731
    use tor_linkspec::ChanTarget;
732
    use tor_linkspec::{HasRelayIds, RelayIdType, RelayIds};
733
    use tor_llcrypto::pk::ed25519::Ed25519Identity;
734
    use tor_memquota::ArcMemoryQuotaTrackerExt as _;
735
    use tor_proto::memquota::ToplevelAccount;
736
    use tor_rtcompat::SleepProvider;
737
    use tracing::trace;
738

            
739
    /// Make a new nonfunctional `Arc<GuardStatusHandle>`
740
    fn gs() -> Arc<GuardStatusHandle> {
741
        Arc::new(None.into())
742
    }
743

            
744
    #[test]
745
    // Re-enabled after work from eta, discussed in arti#149
746
    fn test_double_timeout() {
747
        let t1 = Duration::from_secs(1);
748
        let t10 = Duration::from_secs(10);
749
        /// Return true if d1 is in range [d2...d2 + 0.5sec]
750
        fn duration_close_to(d1: Duration, d2: Duration) -> bool {
751
            d1 >= d2 && d1 <= d2 + Duration::from_millis(500)
752
        }
753

            
754
        tor_rtmock::MockRuntime::test_with_various(|rto| async move {
755
            // Try a future that's ready immediately.
756
            let x = double_timeout(&rto, async { Ok(3_u32) }, t1, t10).await;
757
            assert!(x.is_ok());
758
            assert_eq!(x.unwrap(), 3_u32);
759

            
760
            trace!("acquiesce after test1");
761
            #[allow(clippy::clone_on_copy)]
762
            #[allow(deprecated)] // TODO #1885
763
            let rt = tor_rtmock::MockSleepRuntime::new(rto.clone());
764

            
765
            // Try a future that's ready after a short delay.
766
            let rt_clone = rt.clone();
767
            // (We only want the short delay to fire, not any of the other timeouts.)
768
            rt_clone.block_advance("manually controlling advances");
769
            let x = rt
770
                .wait_for(double_timeout(
771
                    &rt,
772
                    async move {
773
                        let sl = rt_clone.sleep(Duration::from_millis(100));
774
                        rt_clone.allow_one_advance(Duration::from_millis(100));
775
                        sl.await;
776
                        Ok(4_u32)
777
                    },
778
                    t1,
779
                    t10,
780
                ))
781
                .await;
782
            assert!(x.is_ok());
783
            assert_eq!(x.unwrap(), 4_u32);
784

            
785
            trace!("acquiesce after test2");
786
            #[allow(clippy::clone_on_copy)]
787
            #[allow(deprecated)] // TODO #1885
788
            let rt = tor_rtmock::MockSleepRuntime::new(rto.clone());
789

            
790
            // Try a future that passes the first timeout, and make sure that
791
            // it keeps running after it times out.
792
            let rt_clone = rt.clone();
793
            let (snd, rcv) = oneshot::channel();
794
            let start = rt.now();
795
            rt.block_advance("manually controlling advances");
796
            let x = rt
797
                .wait_for(double_timeout(
798
                    &rt,
799
                    async move {
800
                        let sl = rt_clone.sleep(Duration::from_secs(2));
801
                        rt_clone.allow_one_advance(Duration::from_secs(2));
802
                        sl.await;
803
                        snd.send(()).unwrap();
804
                        Ok(4_u32)
805
                    },
806
                    t1,
807
                    t10,
808
                ))
809
                .await;
810
            assert!(matches!(x, Err(Error::CircTimeout(_))));
811
            let end = rt.now();
812
            assert!(duration_close_to(end - start, Duration::from_secs(1)));
813
            let waited = rt.wait_for(rcv).await;
814
            assert_eq!(waited, Ok(()));
815

            
816
            trace!("acquiesce after test3");
817
            #[allow(clippy::clone_on_copy)]
818
            #[allow(deprecated)] // TODO #1885
819
            let rt = tor_rtmock::MockSleepRuntime::new(rto.clone());
820

            
821
            // Try a future that times out and gets abandoned.
822
            let rt_clone = rt.clone();
823
            rt.block_advance("manually controlling advances");
824
            let (snd, rcv) = oneshot::channel();
825
            let start = rt.now();
826
            // Let it hit the first timeout...
827
            rt.allow_one_advance(Duration::from_secs(1));
828
            let x = rt
829
                .wait_for(double_timeout(
830
                    &rt,
831
                    async move {
832
                        rt_clone.sleep(Duration::from_secs(30)).await;
833
                        snd.send(()).unwrap();
834
                        Ok(4_u32)
835
                    },
836
                    t1,
837
                    t10,
838
                ))
839
                .await;
840
            assert!(matches!(x, Err(Error::CircTimeout(_))));
841
            let end = rt.now();
842
            // ...and let it hit the second, too.
843
            rt.allow_one_advance(Duration::from_secs(9));
844
            let waited = rt.wait_for(rcv).await;
845
            assert!(waited.is_err());
846
            let end2 = rt.now();
847
            assert!(duration_close_to(end - start, Duration::from_secs(1)));
848
            assert!(duration_close_to(end2 - start, Duration::from_secs(10)));
849
        });
850
    }
851

            
852
    /// Get a pair of timeouts that we've encoded as an Ed25519 identity.
853
    ///
854
    /// In our FakeCircuit code below, the first timeout is the amount of
855
    /// time that we should sleep while building a hop to this key,
856
    /// and the second timeout is the length of time-advance we should allow
857
    /// after the hop is built.
858
    ///
859
    /// (This is pretty silly, but it's good enough for testing.)
860
    fn timeouts_from_key(id: &Ed25519Identity) -> (Duration, Duration) {
861
        let mut be = [0; 8];
862
        be[..].copy_from_slice(&id.as_bytes()[0..8]);
863
        let dur = u64::from_be_bytes(be);
864
        be[..].copy_from_slice(&id.as_bytes()[8..16]);
865
        let dur2 = u64::from_be_bytes(be);
866
        (Duration::from_millis(dur), Duration::from_millis(dur2))
867
    }
868
    /// Encode a pair of timeouts as an Ed25519 identity.
869
    ///
870
    /// In our FakeCircuit code below, the first timeout is the amount of
871
    /// time that we should sleep while building a hop to this key,
872
    /// and the second timeout is the length of time-advance we should allow
873
    /// after the hop is built.
874
    ///
875
    /// (This is pretty silly but it's good enough for testing.)
876
    fn key_from_timeouts(d1: Duration, d2: Duration) -> Ed25519Identity {
877
        let mut bytes = [0; 32];
878
        let dur = (d1.as_millis() as u64).to_be_bytes();
879
        bytes[0..8].copy_from_slice(&dur);
880
        let dur = (d2.as_millis() as u64).to_be_bytes();
881
        bytes[8..16].copy_from_slice(&dur);
882
        bytes.into()
883
    }
884

            
885
    /// As [`timeouts_from_key`], but first extract the relevant key from the
886
    /// OwnedChanTarget.
887
    fn timeouts_from_chantarget<CT: ChanTarget>(ct: &CT) -> (Duration, Duration) {
888
        // Extracting the Ed25519 identity should always succeed in this case:
889
        // we put it there ourselves!
890
        let ed_id = ct
891
            .identity(RelayIdType::Ed25519)
892
            .expect("No ed25519 key was present for fake ChanTarget‽")
893
            .try_into()
894
            .expect("ChanTarget provided wrong key type");
895
        timeouts_from_key(ed_id)
896
    }
897

            
898
    /// Replacement type for circuit, to implement buildable.
899
    #[derive(Debug, Clone)]
900
    struct FakeCirc {
901
        hops: Vec<RelayIds>,
902
        onehop: bool,
903
    }
904
    #[async_trait]
905
    impl Buildable for Mutex<FakeCirc> {
906
        type Chan = ();
907

            
908
        async fn open_channel<RT: Runtime>(
909
            _chanmgr: &ChanMgr<RT>,
910
            _ct: &OwnedChanTarget,
911
            _guard_status: &GuardStatusHandle,
912
            _usage: ChannelUsage,
913
        ) -> Result<Arc<Self::Chan>> {
914
            Ok(Arc::new(()))
915
        }
916

            
917
        async fn create_chantarget<RT: Runtime>(
918
            _: Arc<Self::Chan>,
919
            rt: &RT,
920
            ct: &OwnedChanTarget,
921
            _: CircParameters,
922
            _timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
923
        ) -> Result<Self> {
924
            let (d1, d2) = timeouts_from_chantarget(ct);
925
            rt.sleep(d1).await;
926
            if !d2.is_zero() {
927
                rt.allow_one_advance(d2);
928
            }
929

            
930
            let c = FakeCirc {
931
                hops: vec![RelayIds::from_relay_ids(ct)],
932
                onehop: true,
933
            };
934
            Ok(Mutex::new(c))
935
        }
936
        async fn create<RT: Runtime>(
937
            _: Arc<Self::Chan>,
938
            rt: &RT,
939
            ct: &OwnedCircTarget,
940
            _: CircParameters,
941
            _timeouts: Arc<dyn tor_proto::client::circuit::TimeoutEstimator>,
942
        ) -> Result<Self> {
943
            let (d1, d2) = timeouts_from_chantarget(ct);
944
            rt.sleep(d1).await;
945
            if !d2.is_zero() {
946
                rt.allow_one_advance(d2);
947
            }
948

            
949
            let c = FakeCirc {
950
                hops: vec![RelayIds::from_relay_ids(ct)],
951
                onehop: false,
952
            };
953
            Ok(Mutex::new(c))
954
        }
955
        async fn extend<RT: Runtime>(
956
            &self,
957
            rt: &RT,
958
            ct: &OwnedCircTarget,
959
            _: CircParameters,
960
        ) -> Result<()> {
961
            let (d1, d2) = timeouts_from_chantarget(ct);
962
            rt.sleep(d1).await;
963
            if !d2.is_zero() {
964
                rt.allow_one_advance(d2);
965
            }
966

            
967
            {
968
                let mut c = self.lock().unwrap();
969
                c.hops.push(RelayIds::from_relay_ids(ct));
970
            }
971
            Ok(())
972
        }
973
    }
974

            
975
    /// Fake implementation of TimeoutEstimator that just records its inputs.
976
    struct TimeoutRecorder<R> {
977
        runtime: R,
978
        hist: Vec<(bool, u8, Duration)>,
979
        // How much advance to permit after being told of a timeout?
980
        on_timeout: Duration,
981
        // How much advance to permit after being told of a success?
982
        on_success: Duration,
983

            
984
        snd_success: Option<oneshot::Sender<()>>,
985
        rcv_success: Option<oneshot::Receiver<()>>,
986
    }
987

            
988
    impl<R> TimeoutRecorder<R> {
989
        fn new(runtime: R) -> Self {
990
            Self::with_delays(runtime, Duration::from_secs(0), Duration::from_secs(0))
991
        }
992

            
993
        fn with_delays(runtime: R, on_timeout: Duration, on_success: Duration) -> Self {
994
            let (snd_success, rcv_success) = oneshot::channel();
995
            Self {
996
                runtime,
997
                hist: Vec::new(),
998
                on_timeout,
999
                on_success,
                rcv_success: Some(rcv_success),
                snd_success: Some(snd_success),
            }
        }
    }
    impl<R: Runtime> TimeoutEstimator for Arc<Mutex<TimeoutRecorder<R>>> {
        fn note_hop_completed(&mut self, hop: u8, delay: Duration, is_last: bool) {
            if !is_last {
                return;
            }
            let (rt, advance) = {
                let mut this = self.lock().unwrap();
                this.hist.push((true, hop, delay));
                let _ = this.snd_success.take().unwrap().send(());
                (this.runtime.clone(), this.on_success)
            };
            if !advance.is_zero() {
                rt.allow_one_advance(advance);
            }
        }
        fn note_circ_timeout(&mut self, hop: u8, delay: Duration) {
            let (rt, advance) = {
                let mut this = self.lock().unwrap();
                this.hist.push((false, hop, delay));
                (this.runtime.clone(), this.on_timeout)
            };
            if !advance.is_zero() {
                rt.allow_one_advance(advance);
            }
        }
        fn timeouts(&mut self, _action: &Action) -> (Duration, Duration) {
            (Duration::from_secs(3), Duration::from_secs(100))
        }
        fn learning_timeouts(&self) -> bool {
            false
        }
        fn update_params(&mut self, _params: &tor_netdir::params::NetParameters) {}
        fn build_state(&mut self) -> Option<crate::timeouts::pareto::ParetoTimeoutState> {
            None
        }
    }
    /// Testing only: create a bogus circuit target
    fn circ_t(id: Ed25519Identity) -> OwnedCircTarget {
        let mut builder = OwnedCircTarget::builder();
        builder
            .chan_target()
            .ed_identity(id)
            .rsa_identity([0x20; 20].into());
        builder
            .ntor_onion_key([0x33; 32].into())
            .protocols("".parse().unwrap())
            .build()
            .unwrap()
    }
    /// Testing only: create a bogus channel target
    fn chan_t(id: Ed25519Identity) -> OwnedChanTarget {
        OwnedChanTarget::builder()
            .ed_identity(id)
            .rsa_identity([0x20; 20].into())
            .build()
            .unwrap()
    }
    async fn run_builder_test(
        rt: tor_rtmock::MockRuntime,
        advance_initial: Duration,
        path: OwnedPath,
        advance_on_timeout: Option<(Duration, Duration)>,
        usage: ChannelUsage,
    ) -> (Result<FakeCirc>, Vec<(bool, u8, Duration)>) {
        let chanmgr = Arc::new(ChanMgr::new(
            rt.clone(),
            &ChannelConfig::default(),
            Default::default(),
            &Default::default(),
            ToplevelAccount::new_noop(),
            None,
        ));
        // always has 3 second timeout, 100 second abandon.
        let timeouts = match advance_on_timeout {
            Some((d1, d2)) => TimeoutRecorder::with_delays(rt.clone(), d1, d2),
            None => TimeoutRecorder::new(rt.clone()),
        };
        let timeouts = Arc::new(Mutex::new(timeouts));
        let builder: Builder<_, Mutex<FakeCirc>> = Builder::new(
            rt.clone(),
            chanmgr,
            timeouts::Estimator::new(Arc::clone(&timeouts)),
        );
        rt.block_advance("manually controlling advances");
        rt.allow_one_advance(advance_initial);
        let outcome = rt.spawn_join("build-owned", async move {
            let arcbuilder = Arc::new(builder);
            let params = exit_circparams_from_netparams(&NetParameters::default())?;
            arcbuilder.build_owned(path, &params, gs(), usage).await
        });
        // Now we wait for a success to finally, finally be reported.
        if advance_on_timeout.is_some() {
            let receiver = { timeouts.lock().unwrap().rcv_success.take().unwrap() };
            rt.spawn_identified("receiver", async move {
                receiver.await.unwrap();
            });
        }
        rt.advance_until_stalled().await;
        let circ = outcome.map(|m| Ok(m?.lock().unwrap().clone())).await;
        let timeouts = timeouts.lock().unwrap().hist.clone();
        (circ, timeouts)
    }
    #[test]
    fn build_onehop() {
        tor_rtmock::MockRuntime::test_with_various(|rt| async move {
            let id_100ms = key_from_timeouts(Duration::from_millis(100), Duration::from_millis(0));
            let path = OwnedPath::ChannelOnly(chan_t(id_100ms));
            let (outcome, timeouts) =
                run_builder_test(rt, Duration::from_millis(100), path, None, CU::UserTraffic).await;
            let circ = outcome.unwrap();
            assert!(circ.onehop);
            assert_eq!(circ.hops.len(), 1);
            assert!(circ.hops[0].same_relay_ids(&chan_t(id_100ms)));
            assert_eq!(timeouts.len(), 1);
            assert!(timeouts[0].0); // success
            assert_eq!(timeouts[0].1, 0); // one-hop
            assert_eq!(timeouts[0].2, Duration::from_millis(100));
        });
    }
    #[test]
    fn build_threehop() {
        tor_rtmock::MockRuntime::test_with_various(|rt| async move {
            let id_100ms =
                key_from_timeouts(Duration::from_millis(100), Duration::from_millis(200));
            let id_200ms =
                key_from_timeouts(Duration::from_millis(200), Duration::from_millis(300));
            let id_300ms = key_from_timeouts(Duration::from_millis(300), Duration::from_millis(0));
            let path =
                OwnedPath::Normal(vec![circ_t(id_100ms), circ_t(id_200ms), circ_t(id_300ms)]);
            let (outcome, timeouts) =
                run_builder_test(rt, Duration::from_millis(100), path, None, CU::UserTraffic).await;
            let circ = outcome.unwrap();
            assert!(!circ.onehop);
            assert_eq!(circ.hops.len(), 3);
            assert!(circ.hops[0].same_relay_ids(&chan_t(id_100ms)));
            assert!(circ.hops[1].same_relay_ids(&chan_t(id_200ms)));
            assert!(circ.hops[2].same_relay_ids(&chan_t(id_300ms)));
            assert_eq!(timeouts.len(), 1);
            assert!(timeouts[0].0); // success
            assert_eq!(timeouts[0].1, 2); // three-hop
            assert_eq!(timeouts[0].2, Duration::from_millis(600));
        });
    }
    #[test]
    fn build_huge_timeout() {
        tor_rtmock::MockRuntime::test_with_various(|rt| async move {
            let id_100ms =
                key_from_timeouts(Duration::from_millis(100), Duration::from_millis(200));
            let id_200ms =
                key_from_timeouts(Duration::from_millis(200), Duration::from_millis(2700));
            let id_hour = key_from_timeouts(Duration::from_secs(3600), Duration::from_secs(0));
            let path = OwnedPath::Normal(vec![circ_t(id_100ms), circ_t(id_200ms), circ_t(id_hour)]);
            let (outcome, timeouts) =
                run_builder_test(rt, Duration::from_millis(100), path, None, CU::UserTraffic).await;
            assert!(matches!(outcome, Err(Error::CircTimeout(_))));
            assert_eq!(timeouts.len(), 1);
            assert!(!timeouts[0].0); // timeout
            // BUG: Sometimes this is 1 and sometimes this is 2.
            // assert_eq!(timeouts[0].1, 2); // at third hop.
            assert_eq!(timeouts[0].2, Duration::from_millis(3000));
        });
    }
    #[test]
    fn build_modest_timeout() {
        tor_rtmock::MockRuntime::test_with_various(|rt| async move {
            let id_100ms =
                key_from_timeouts(Duration::from_millis(100), Duration::from_millis(200));
            let id_200ms =
                key_from_timeouts(Duration::from_millis(200), Duration::from_millis(2700));
            let id_3sec = key_from_timeouts(Duration::from_millis(3000), Duration::from_millis(0));
            let timeout_advance = (Duration::from_millis(4000), Duration::from_secs(0));
            let path = OwnedPath::Normal(vec![circ_t(id_100ms), circ_t(id_200ms), circ_t(id_3sec)]);
            let (outcome, timeouts) = run_builder_test(
                rt.clone(),
                Duration::from_millis(100),
                path,
                Some(timeout_advance),
                CU::UserTraffic,
            )
            .await;
            assert!(matches!(outcome, Err(Error::CircTimeout(_))));
            assert_eq!(timeouts.len(), 2);
            assert!(!timeouts[0].0); // timeout
            // BUG: Sometimes this is 1 and sometimes this is 2.
            //assert_eq!(timeouts[0].1, 2); // at third hop.
            assert_eq!(timeouts[0].2, Duration::from_millis(3000));
            assert!(timeouts[1].0); // success
            assert_eq!(timeouts[1].1, 2); // three-hop
            // BUG: This timer is not always reliable, due to races.
            //assert_eq!(timeouts[1].2, Duration::from_millis(3300));
        });
    }
}