1
//! Experimental support for vanguards.
2
//!
3
//! For more information, see the [vanguards spec].
4
//!
5
//! [vanguards spec]: https://spec.torproject.org/vanguards-spec/index.html.
6

            
7
pub mod config;
8
mod err;
9
mod set;
10

            
11
use std::sync::{Arc, RwLock, Weak};
12
use std::time::{Duration, SystemTime};
13

            
14
use futures::stream::BoxStream;
15
use futures::{FutureExt as _, future};
16
use futures::{StreamExt as _, select_biased};
17
use postage::stream::Stream as _;
18
use postage::watch;
19
use rand::RngCore;
20
use tor_rtcompat::SpawnExt as _;
21

            
22
use tor_async_utils::PostageWatchSenderExt as _;
23
use tor_config::ReconfigureError;
24
use tor_error::{error_report, internal, into_internal};
25
use tor_netdir::{DirEvent, NetDir, NetDirProvider, Timeliness};
26
use tor_persist::{DynStorageHandle, StateMgr};
27
use tor_relay_selection::RelaySelector;
28
use tor_rtcompat::Runtime;
29
use tracing::{debug, info, instrument};
30

            
31
use crate::{RetireCircuits, VanguardMode};
32

            
33
use set::VanguardSets;
34

            
35
use crate::VanguardConfig;
36
pub use config::VanguardParams;
37
pub use err::VanguardMgrError;
38
pub use set::Vanguard;
39

            
40
/// The key used for storing the vanguard sets to persistent storage using `StateMgr`.
41
const STORAGE_KEY: &str = "vanguards";
42

            
43
/// The vanguard manager.
44
pub struct VanguardMgr<R: Runtime> {
45
    /// The mutable state.
46
    inner: RwLock<Inner>,
47
    /// The runtime.
48
    runtime: R,
49
    /// The persistent storage handle, used for writing the vanguard sets to disk
50
    /// if full vanguards are enabled.
51
    storage: DynStorageHandle<VanguardSets>,
52
}
53

            
54
/// The mutable inner state of [`VanguardMgr`].
55
struct Inner {
56
    /// The current vanguard parameters.
57
    params: VanguardParams,
58
    /// Whether to use full, lite, or no vanguards.
59
    ///
60
    // TODO(#1382): we should derive the mode from the
61
    // vanguards-enabled and vanguards-hs-service consensus params.
62
    mode: VanguardMode,
63
    /// The L2 and L3 vanguards.
64
    ///
65
    /// The L3 vanguards are only used if we are running in
66
    /// [`Full`](VanguardMode::Full) vanguard mode.
67
    /// Otherwise, the L3 set is not populated, or read from.
68
    ///
69
    /// If [`Full`](VanguardMode::Full) vanguard mode is enabled,
70
    /// the vanguard sets will be persisted to disk whenever
71
    /// vanuards are rotated, added, or removed.
72
    ///
73
    /// The vanguard sets are updated and persisted to storage by
74
    /// [`update_vanguard_sets`](Inner::update_vanguard_sets).
75
    ///
76
    /// If the `VanguardSets` change while we are in "lite" mode,
77
    /// the changes will not *not* be written to storage.
78
    /// If we later switch to "full" vanguards, those previous changes still
79
    /// won't be persisted to storage: we only flush to storage if the
80
    /// [`VanguardSets`] change *while* we are in "full" mode
81
    /// (changing the [`VanguardMode`] does not constitute a change in the `VanguardSets`).
82
    //
83
    // TODO HS-VANGUARDS: the correct behaviour here might be to never switch back to lite mode
84
    // after enabling full vanguards. If we do that, persisting the vanguard sets will be simpler,
85
    // as we can just unconditionally flush to storage if the vanguard mode is switched to full.
86
    // Right now we can't do that, because we don't remember the "mode":
87
    // we derive it on the fly from `has_onion_svc` and the current `VanguardParams`.
88
    //
89
    ///
90
    /// This is initialized with the vanguard sets read from the vanguard state file,
91
    /// if the file exists, or with a [`Default`] `VanguardSets`, if it doesn't.
92
    ///
93
    /// Note: The `VanguardSets` are read from the vanguard state file
94
    /// even if full vanguards are not enabled. They are *not*, however, written
95
    /// to the state file unless full vanguards are in use.
96
    vanguard_sets: VanguardSets,
97
    /// Whether we're running an onion service.
98
    ///
99
    // TODO(#1382): This should be used for deciding whether to use the `vanguards_hs_service` or the
100
    // `vanguards_enabled` [`NetParameter`](tor_netdir::params::NetParameters).
101
    #[allow(unused)]
102
    has_onion_svc: bool,
103
    /// A channel for sending VanguardConfig changes to the vanguard maintenance task.
104
    config_tx: watch::Sender<VanguardConfig>,
105
}
106

            
107
/// Whether the [`VanguardMgr::maintain_vanguard_sets`] task
108
/// should continue running or shut down.
109
///
110
/// Returned from [`VanguardMgr::run_once`].
111
#[derive(Copy, Clone, Debug)]
112
enum ShutdownStatus {
113
    /// Continue calling `run_once`.
114
    Continue,
115
    /// The `VanguardMgr` was dropped, terminate the task.
116
    Terminate,
117
}
118

            
119
impl<R: Runtime> VanguardMgr<R> {
120
    /// Create a new `VanguardMgr`.
121
    ///
122
    /// The `state_mgr` handle is used for persisting the "vanguards-full" guard pools to disk.
123
1806
    pub fn new<S>(
124
1806
        config: &VanguardConfig,
125
1806
        runtime: R,
126
1806
        state_mgr: S,
127
1806
        has_onion_svc: bool,
128
1806
    ) -> Result<Self, VanguardMgrError>
129
1806
    where
130
1806
        S: StateMgr + Send + Sync + 'static,
131
    {
132
        // Note: we start out with default vanguard params, but we adjust them
133
        // as soon as we obtain a NetDir (see Self::run_once()).
134
1806
        let params = VanguardParams::default();
135
1806
        let storage: DynStorageHandle<VanguardSets> = state_mgr.create_handle(STORAGE_KEY);
136

            
137
1806
        let vanguard_sets = match storage.load()? {
138
8
            Some(mut sets) => {
139
8
                info!("Loading vanguards from vanguard state file");
140
                // Discard the now-expired the vanguards
141
8
                let now = runtime.wallclock();
142
8
                let _ = sets.remove_expired(now);
143
8
                sets
144
            }
145
            None => {
146
1794
                debug!("Vanguard state file not found, selecting new vanguards");
147
                // Initially, all sets have a target size of 0.
148
                // This is OK because the target is only used for repopulating the vanguard sets,
149
                // and we can't repopulate the sets without a netdir.
150
                // The target gets adjusted once we obtain a netdir.
151
1794
                Default::default()
152
            }
153
        };
154

            
155
1802
        let (config_tx, _config_rx) = watch::channel();
156
1802
        let inner = Inner {
157
1802
            params,
158
1802
            mode: config.mode(),
159
1802
            vanguard_sets,
160
1802
            has_onion_svc,
161
1802
            config_tx,
162
1802
        };
163

            
164
1802
        Ok(Self {
165
1802
            inner: RwLock::new(inner),
166
1802
            runtime,
167
1802
            storage,
168
1802
        })
169
1806
    }
170

            
171
    /// Launch the vanguard pool management tasks.
172
    ///
173
    /// These run until the `VanguardMgr` is dropped.
174
    //
175
    // This spawns [`VanguardMgr::maintain_vanguard_sets`].
176
    #[instrument(level = "trace", skip_all)]
177
106
    pub fn launch_background_tasks(
178
106
        self: &Arc<Self>,
179
106
        netdir_provider: &Arc<dyn NetDirProvider>,
180
106
    ) -> Result<(), VanguardMgrError>
181
106
    where
182
106
        R: Runtime,
183
    {
184
106
        let netdir_provider = Arc::clone(netdir_provider);
185
106
        let config_rx = self
186
106
            .inner
187
106
            .write()
188
106
            .expect("poisoned lock")
189
106
            .config_tx
190
106
            .subscribe();
191
106
        self.runtime
192
106
            .spawn(Self::maintain_vanguard_sets(
193
106
                Arc::downgrade(self),
194
106
                Arc::downgrade(&netdir_provider),
195
106
                config_rx,
196
            ))
197
106
            .map_err(|e| VanguardMgrError::Spawn(Arc::new(e)))?;
198

            
199
106
        Ok(())
200
106
    }
201

            
202
    /// Replace the configuration in this `VanguardMgr` with the specified `config`.
203
20
    pub fn reconfigure(&self, config: &VanguardConfig) -> Result<RetireCircuits, ReconfigureError> {
204
        // TODO(#1382): abolish VanguardConfig and derive the mode from the VanguardParams
205
        // and has_onion_svc instead.
206
        //
207
        // TODO(#1382): update has_onion_svc if the new config enables onion svc usage
208
        //
209
        // Perhaps we should always escalate to Full if we start running an onion service,
210
        // but not decessarily downgrade to lite if we stop.
211
        // See <https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/2083#note_3018173>
212
20
        let mut inner = self.inner.write().expect("poisoned lock");
213
20
        let new_mode = config.mode();
214
20
        if new_mode != inner.mode {
215
16
            inner.mode = new_mode;
216

            
217
            // Wake up the maintenance task to replenish the vanguard pools.
218
16
            inner.config_tx.maybe_send(|_| config.clone());
219

            
220
16
            Ok(RetireCircuits::All)
221
        } else {
222
4
            Ok(RetireCircuits::None)
223
        }
224
20
    }
225

            
226
    /// Return a [`Vanguard`] relay for use in the specified layer.
227
    ///
228
    /// The `relay_selector` must exclude the relays that would neighbor this vanguard
229
    /// in the path.
230
    ///
231
    /// Specifically, it should exclude
232
    ///   * the last relay in the path (the one immediately preceding the vanguard): the same relay
233
    ///     cannot be used in consecutive positions in the path (a relay won't let you extend the
234
    ///     circuit to itself).
235
    ///   * the penultimate relay of the path, if there is one: relays don't allow extending the
236
    ///     circuit to their previous hop
237
    ///
238
    /// If [`Full`](VanguardMode::Full) vanguards are in use, this function can be used
239
    /// for selecting both [`Layer2`](Layer::Layer2) and [`Layer3`](Layer::Layer3) vanguards.
240
    ///
241
    /// If [`Lite`](VanguardMode::Lite) vanguards are in use, this function can only be used
242
    /// for selecting [`Layer2`](Layer::Layer2) vanguards.
243
    /// It will return an error if a [`Layer3`](Layer::Layer3) is requested.
244
    ///
245
    /// Returns an error if vanguards are disabled.
246
    ///
247
    /// Returns a [`NoSuitableRelay`](VanguardMgrError::NoSuitableRelay) error
248
    /// if none of our vanguards satisfy the `layer` and `neighbor_exclusion` requirements.
249
    ///
250
    /// Returns a [`BootstrapRequired`](VanguardMgrError::BootstrapRequired) error
251
    /// if called before the vanguard manager has finished bootstrapping,
252
    /// or if all the vanguards have become unusable
253
    /// (by expiring or no longer being listed in the consensus)
254
    /// and we are unable to replenish them.
255
    ///
256
    ///  ### Example
257
    ///
258
    ///  If the partially built path is of the form `G - L2` and we are selecting the L3 vanguard,
259
    ///  the `RelayExclusion` should contain `G` and `L2` (to prevent building a path of the form
260
    ///  `G - L2 - G`, or `G - L2 - L2`).
261
    ///
262
    ///  If the path only contains the L1 guard (`G`), then the `RelayExclusion` should only
263
    ///  exclude `G`.
264
116
    pub fn select_vanguard<'a, Rng: RngCore>(
265
116
        &self,
266
116
        rng: &mut Rng,
267
116
        netdir: &'a NetDir,
268
116
        layer: Layer,
269
116
        relay_selector: &RelaySelector<'a>,
270
116
    ) -> Result<Vanguard<'a>, VanguardMgrError> {
271
        use VanguardMode::*;
272

            
273
116
        let inner = self.inner.read().expect("poisoned lock");
274

            
275
        // All our vanguard sets are empty. This means select_vanguards() was called before
276
        // maintain_vanguard_sets() managed to obtain a netdir and populate the vanguard sets,
277
        // or all the vanguards have become unusable and we have been unable to replenish them.
278
116
        if inner.vanguard_sets.l2().is_empty() && inner.vanguard_sets.l3().is_empty() {
279
4
            return Err(VanguardMgrError::BootstrapRequired {
280
4
                action: "select vanguard",
281
4
            });
282
112
        }
283

            
284
104
        let relay =
285
112
            match (layer, inner.mode) {
286
64
                (Layer::Layer2, Full) | (Layer::Layer2, Lite) => inner
287
64
                    .vanguard_sets
288
64
                    .l2()
289
64
                    .pick_relay(rng, netdir, relay_selector),
290
                (Layer::Layer3, Full) => {
291
40
                    inner
292
40
                        .vanguard_sets
293
40
                        .l3()
294
40
                        .pick_relay(rng, netdir, relay_selector)
295
                }
296
                _ => {
297
8
                    return Err(VanguardMgrError::LayerNotSupported {
298
8
                        layer,
299
8
                        mode: inner.mode,
300
8
                    });
301
                }
302
            };
303

            
304
104
        relay.ok_or(VanguardMgrError::NoSuitableRelay(layer))
305
116
    }
306

            
307
    /// The vanguard set management task.
308
    ///
309
    /// This is a background task that:
310
    /// * removes vanguards from the L2 and L3 vanguard sets when they expire
311
    /// * ensures the vanguard sets are repopulated with new vanguards
312
    ///   when the number of vanguards drops below a certain threshold
313
    /// * handles `NetDir` changes, updating the vanguard set sizes as needed
314
106
    async fn maintain_vanguard_sets(
315
106
        mgr: Weak<Self>,
316
106
        netdir_provider: Weak<dyn NetDirProvider>,
317
106
        mut config_rx: watch::Receiver<VanguardConfig>,
318
106
    ) {
319
106
        let mut netdir_events = match netdir_provider.upgrade() {
320
102
            Some(provider) => provider.events(),
321
            None => {
322
4
                return;
323
            }
324
        };
325

            
326
        loop {
327
328
            match Self::run_once(
328
328
                Weak::clone(&mgr),
329
328
                Weak::clone(&netdir_provider),
330
328
                &mut netdir_events,
331
328
                &mut config_rx,
332
            )
333
328
            .await
334
            {
335
226
                Ok(ShutdownStatus::Continue) => continue,
336
                Ok(ShutdownStatus::Terminate) => {
337
                    debug!("Vanguard manager is shutting down");
338
                    break;
339
                }
340
4
                Err(e) => {
341
4
                    error_report!(e, "Vanguard manager crashed");
342
4
                    break;
343
                }
344
            }
345
        }
346
8
    }
347

            
348
    /// Wait until a vanguard expires or until there is a new [`NetDir`].
349
    ///
350
    /// This populates the L2 and L3 vanguard sets,
351
    /// and rotates the vanguards when their lifetime expires.
352
    ///
353
    /// Note: the L3 set is only populated with vanguards if
354
    /// [`Full`](VanguardMode::Full) vanguards are enabled.
355
328
    async fn run_once(
356
328
        mgr: Weak<Self>,
357
328
        netdir_provider: Weak<dyn NetDirProvider>,
358
328
        netdir_events: &mut BoxStream<'static, DirEvent>,
359
328
        config_rx: &mut watch::Receiver<VanguardConfig>,
360
328
    ) -> Result<ShutdownStatus, VanguardMgrError> {
361
328
        let (mgr, netdir_provider) = match (mgr.upgrade(), netdir_provider.upgrade()) {
362
328
            (Some(mgr), Some(netdir_provider)) => (mgr, netdir_provider),
363
            _ => return Ok(ShutdownStatus::Terminate),
364
        };
365

            
366
328
        let now = mgr.runtime.wallclock();
367
328
        let next_to_expire = mgr.rotate_expired(&netdir_provider, now)?;
368
        // A future that sleeps until the next vanguard expires
369
328
        let sleep_fut = async {
370
222
            if let Some(dur) = next_to_expire {
371
124
                let () = mgr.runtime.sleep(dur).await;
372
            } else {
373
98
                future::pending::<()>().await;
374
            }
375
16
        };
376

            
377
328
        select_biased! {
378
328
            event = netdir_events.next().fuse() => {
379
104
                if let Some(DirEvent::NewConsensus) = event {
380
104
                    let netdir = netdir_provider.netdir(Timeliness::Timely)?;
381
104
                    mgr.inner.write().expect("poisoned lock")
382
104
                        .update_vanguard_sets(&mgr.runtime, &mgr.storage, &netdir)?;
383
                }
384

            
385
100
                Ok(ShutdownStatus::Continue)
386
            },
387
328
            _config = config_rx.recv().fuse() => {
388
110
                if let Some(netdir) = Self::timely_netdir(&netdir_provider)? {
389
                    // If we have a NetDir, replenish the vanguard sets that don't have enough vanguards.
390
                    //
391
                    // For example, if the config change enables full vanguards for the first time,
392
                    // this will cause the L3 vanguard set to be populated.
393
8
                    mgr.inner.write().expect("poisoned lock")
394
8
                        .update_vanguard_sets(&mgr.runtime, &mgr.storage, &netdir)?;
395
102
                }
396

            
397
110
                Ok(ShutdownStatus::Continue)
398
            },
399
328
            () = sleep_fut.fuse() => {
400
                // A vanguard expired, time to run the cleanup
401
16
                Ok(ShutdownStatus::Continue)
402
            },
403
        }
404
230
    }
405

            
406
    /// Return a timely `NetDir`, if one is available.
407
    ///
408
    /// Returns `None` if no directory information is available.
409
438
    fn timely_netdir(
410
438
        netdir_provider: &Arc<dyn NetDirProvider>,
411
438
    ) -> Result<Option<Arc<NetDir>>, VanguardMgrError> {
412
        use tor_netdir::Error as NetDirError;
413

            
414
438
        match netdir_provider.netdir(Timeliness::Timely) {
415
132
            Ok(netdir) => Ok(Some(netdir)),
416
306
            Err(NetDirError::NoInfo) | Err(NetDirError::NotEnoughInfo) => Ok(None),
417
            Err(e) => Err(e.into()),
418
        }
419
438
    }
420

            
421
    /// Rotate the vanguards that have expired,
422
    /// returning how long until the next vanguard will expire,
423
    /// or `None` if there are no vanguards in any of our sets.
424
328
    fn rotate_expired(
425
328
        &self,
426
328
        netdir_provider: &Arc<dyn NetDirProvider>,
427
328
        now: SystemTime,
428
328
    ) -> Result<Option<Duration>, VanguardMgrError> {
429
328
        let mut inner = self.inner.write().expect("poisoned lock");
430
328
        let inner = &mut *inner;
431

            
432
328
        let vanguard_sets = &mut inner.vanguard_sets;
433
328
        let expired_count = vanguard_sets.remove_expired(now);
434

            
435
328
        if expired_count > 0 {
436
16
            info!("Rotating vanguards");
437
312
        }
438

            
439
328
        if let Some(netdir) = Self::timely_netdir(netdir_provider)? {
440
            // If we have a NetDir, replenish the vanguard sets that don't have enough vanguards.
441
124
            inner.update_vanguard_sets(&self.runtime, &self.storage, &netdir)?;
442
204
        }
443

            
444
328
        let Some(expiry) = inner.vanguard_sets.next_expiry() else {
445
            // Both vanguard sets are empty
446
196
            return Ok(None);
447
        };
448

            
449
132
        expiry
450
132
            .duration_since(now)
451
132
            .map_err(|_| internal!("when > now, but now is later than when?!").into())
452
132
            .map(Some)
453
328
    }
454

            
455
    /// Get the current [`VanguardMode`].
456
144
    pub fn mode(&self) -> VanguardMode {
457
144
        self.inner.read().expect("poisoned lock").mode
458
144
    }
459
}
460

            
461
impl Inner {
462
    /// Update the vanguard sets, handling any potential vanguard parameter changes.
463
    ///
464
    /// This updates the [`VanguardSets`]s based on the [`VanguardParams`]
465
    /// derived from the new `NetDir`, replenishing the sets if necessary.
466
    ///
467
    /// NOTE: if the new `VanguardParams` specify different lifetime ranges
468
    /// than the previous `VanguardParams`, the new lifetime requirements only
469
    /// apply to newly selected vanguards. They are **not** retroactively applied
470
    /// to our existing vanguards.
471
    //
472
    // TODO(#1352): we might want to revisit this decision.
473
    // We could, for example, adjust the lifetime of our existing vanguards
474
    // to comply with the new lifetime requirements.
475
236
    fn update_vanguard_sets<R: Runtime>(
476
236
        &mut self,
477
236
        runtime: &R,
478
236
        storage: &DynStorageHandle<VanguardSets>,
479
236
        netdir: &Arc<NetDir>,
480
236
    ) -> Result<(), VanguardMgrError> {
481
236
        let params = VanguardParams::try_from(netdir.params())
482
236
            .map_err(into_internal!("invalid NetParameters"))?;
483

            
484
        // Update our params with the new values.
485
236
        self.update_params(params.clone());
486

            
487
236
        self.vanguard_sets.remove_unlisted(netdir);
488

            
489
        // If we loaded some vanguards from persistent storage but we still need more,
490
        // we select them here.
491
        //
492
        // If full vanguards are not enabled and we started with an empty (default)
493
        // vanguard set, we populate the sets here.
494
        //
495
        // If we have already populated the vanguard sets in a previous iteration,
496
        // this will ensure they have enough vanguards.
497
236
        self.vanguard_sets
498
236
            .replenish_vanguards(runtime, netdir, &params, self.mode)?;
499

            
500
        // Flush the vanguard sets to disk.
501
236
        self.flush_to_storage(storage)?;
502

            
503
232
        Ok(())
504
236
    }
505

            
506
    /// Update our vanguard params.
507
2588
    fn update_params(&mut self, new_params: VanguardParams) {
508
2588
        self.params = new_params;
509
2588
    }
510

            
511
    /// Flush the vanguard sets to storage, if the mode is "vanguards-full".
512
2588
    fn flush_to_storage(
513
2588
        &self,
514
2588
        storage: &DynStorageHandle<VanguardSets>,
515
2588
    ) -> Result<(), VanguardMgrError> {
516
2588
        match self.mode {
517
1160
            VanguardMode::Lite | VanguardMode::Disabled => Ok(()),
518
            VanguardMode::Full => {
519
1428
                debug!("The vanguards may have changed; flushing to vanguard state file");
520
1428
                Ok(storage.store(&self.vanguard_sets)?)
521
            }
522
        }
523
2588
    }
524
}
525

            
526
#[cfg(any(test, feature = "testing"))]
527
use {
528
    tor_config::ExplicitOrAuto, tor_netdir::testprovider::TestNetDirProvider,
529
    tor_persist::TestingStateMgr, tor_rtmock::MockRuntime,
530
};
531

            
532
/// Helpers for tests involving vanguards
533
#[cfg(any(test, feature = "testing"))]
534
impl VanguardMgr<MockRuntime> {
535
    /// Create a new VanguardMgr for testing.
536
1256
    pub fn new_testing(
537
1256
        rt: &MockRuntime,
538
1256
        mode: VanguardMode,
539
1256
    ) -> Result<Arc<VanguardMgr<MockRuntime>>, VanguardMgrError> {
540
1256
        let config = VanguardConfig {
541
1256
            mode: ExplicitOrAuto::Explicit(mode),
542
1256
        };
543
1256
        let statemgr = TestingStateMgr::new();
544
1256
        let lock = statemgr.try_lock()?;
545
1256
        assert!(lock.held());
546
        // TODO(#1382): has_onion_svc doesn't matter right now
547
1256
        let has_onion_svc = false;
548
1256
        Ok(Arc::new(VanguardMgr::new(
549
1256
            &config,
550
1256
            rt.clone(),
551
1256
            statemgr,
552
1256
            has_onion_svc,
553
        )?))
554
1256
    }
555

            
556
    /// Wait until the vanguardmgr has populated its vanguard sets.
557
    ///
558
    /// Returns a [`TestNetDirProvider`] that can be used to notify
559
    /// the `VanguardMgr` of netdir changes.
560
1256
    pub async fn init_vanguard_sets(
561
1256
        self: &Arc<VanguardMgr<MockRuntime>>,
562
1256
        netdir: &NetDir,
563
1296
    ) -> Result<Arc<TestNetDirProvider>, VanguardMgrError> {
564
80
        let netdir_provider = Arc::new(TestNetDirProvider::new());
565
80
        self.launch_background_tasks(&(netdir_provider.clone() as Arc<dyn NetDirProvider>))?;
566
80
        self.runtime.progress_until_stalled().await;
567

            
568
        // Call set_netdir_and_notify to trigger an event
569
80
        netdir_provider
570
80
            .set_netdir_and_notify(Arc::new(netdir.clone()))
571
80
            .await;
572

            
573
        // Wait until the vanguard mgr has finished handling the netdir event.
574
80
        self.runtime.progress_until_stalled().await;
575

            
576
80
        Ok(netdir_provider)
577
80
    }
578
}
579

            
580
/// The vanguard layer.
581
#[derive(Debug, Clone, Copy, PartialEq)] //
582
#[derive(derive_more::Display)] //
583
#[non_exhaustive]
584
pub enum Layer {
585
    /// L2 vanguard.
586
    #[display("layer 2")]
587
    Layer2,
588
    /// L3 vanguard.
589
    #[display("layer 3")]
590
    Layer3,
591
}
592

            
593
#[cfg(test)]
594
mod test {
595
    // @@ begin test lint list maintained by maint/add_warning @@
596
    #![allow(clippy::bool_assert_comparison)]
597
    #![allow(clippy::clone_on_copy)]
598
    #![allow(clippy::dbg_macro)]
599
    #![allow(clippy::mixed_attributes_style)]
600
    #![allow(clippy::print_stderr)]
601
    #![allow(clippy::print_stdout)]
602
    #![allow(clippy::single_char_pattern)]
603
    #![allow(clippy::unwrap_used)]
604
    #![allow(clippy::unchecked_time_subtraction)]
605
    #![allow(clippy::useless_vec)]
606
    #![allow(clippy::needless_pass_by_value)]
607
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
608

            
609
    use std::{fmt, time};
610

            
611
    use set::TimeBoundVanguard;
612
    use tor_config::ExplicitOrAuto;
613
    use tor_relay_selection::RelayExclusion;
614

            
615
    use super::*;
616

            
617
    use Layer::*;
618
    use tor_basic_utils::test_rng::testing_rng;
619
    use tor_linkspec::{HasRelayIds, RelayIds};
620
    use tor_netdir::{
621
        testnet::{self, construct_custom_netdir_with_params},
622
        testprovider::TestNetDirProvider,
623
    };
624
    use tor_persist::FsStateMgr;
625
    use tor_rtmock::MockRuntime;
626

            
627
    use itertools::Itertools;
628

            
629
    /// Enable lite vanguards for onion services.
630
    const ENABLE_LITE_VANGUARDS: [(&str, i32); 1] = [("vanguards-hs-service", 1)];
631

            
632
    /// Enable full vanguards for hidden services.
633
    const ENABLE_FULL_VANGUARDS: [(&str, i32); 1] = [("vanguards-hs-service", 2)];
634

            
635
    /// A valid vanguard state file.
636
    const VANGUARDS_JSON: &str = include_str!("../testdata/vanguards.json");
637

            
638
    /// A invalid vanguard state file.
639
    const INVALID_VANGUARDS_JSON: &str = include_str!("../testdata/vanguards_invalid.json");
640

            
641
    /// Create the `StateMgr`, populating the vanguards.json state file with the specified JSON string.
642
    fn state_dir_with_vanguards(vanguards_json: &str) -> (FsStateMgr, tempfile::TempDir) {
643
        let dir = tempfile::TempDir::new().unwrap();
644
        std::fs::create_dir_all(dir.path().join("state")).unwrap();
645
        std::fs::write(dir.path().join("state/vanguards.json"), vanguards_json).unwrap();
646

            
647
        let statemgr = FsStateMgr::from_path_and_mistrust(
648
            dir.path(),
649
            &fs_mistrust::Mistrust::new_dangerously_trust_everyone(),
650
        )
651
        .unwrap();
652

            
653
        (statemgr, dir)
654
    }
655

            
656
    impl fmt::Debug for Vanguard<'_> {
657
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
658
            f.debug_struct("Vanguard").finish()
659
        }
660
    }
661

            
662
    impl Inner {
663
        /// Return the L2 vanguard set.
664
        pub(super) fn l2_vanguards(&self) -> &Vec<TimeBoundVanguard> {
665
            self.vanguard_sets.l2_vanguards()
666
        }
667

            
668
        /// Return the L3 vanguard set.
669
        pub(super) fn l3_vanguards(&self) -> &Vec<TimeBoundVanguard> {
670
            self.vanguard_sets.l3_vanguards()
671
        }
672
    }
673

            
674
    /// Return a maximally permissive RelaySelector for a vanguard.
675
    fn permissive_selector() -> RelaySelector<'static> {
676
        RelaySelector::new(
677
            tor_relay_selection::RelayUsage::vanguard(),
678
            RelayExclusion::no_relays_excluded(),
679
        )
680
    }
681

            
682
    /// Look up the vanguard in the specified VanguardSet.
683
    fn find_in_set<R: Runtime>(
684
        relay_ids: &RelayIds,
685
        mgr: &VanguardMgr<R>,
686
        layer: Layer,
687
    ) -> Option<TimeBoundVanguard> {
688
        let inner = mgr.inner.read().unwrap();
689

            
690
        let vanguards = match layer {
691
            Layer2 => inner.l2_vanguards(),
692
            Layer3 => inner.l3_vanguards(),
693
        };
694

            
695
        // Look up the TimeBoundVanguard that corresponds to this Vanguard,
696
        // and figure out its expiry.
697
        vanguards.iter().find(|v| v.id == *relay_ids).cloned()
698
    }
699

            
700
    /// Get the total number of vanguard entries (L2 + L3).
701
    fn vanguard_count<R: Runtime>(mgr: &VanguardMgr<R>) -> usize {
702
        let inner = mgr.inner.read().unwrap();
703
        inner.l2_vanguards().len() + inner.l3_vanguards().len()
704
    }
705

            
706
    /// Return a `Duration` representing how long until this vanguard expires.
707
    fn duration_until_expiry<R: Runtime>(
708
        relay_ids: &RelayIds,
709
        mgr: &VanguardMgr<R>,
710
        runtime: &R,
711
        layer: Layer,
712
    ) -> Duration {
713
        // Look up the TimeBoundVanguard that corresponds to this Vanguard,
714
        // and figure out its expiry.
715
        let vanguard = find_in_set(relay_ids, mgr, layer).unwrap();
716

            
717
        vanguard
718
            .when
719
            .duration_since(runtime.wallclock())
720
            .unwrap_or_default()
721
    }
722

            
723
    /// Assert the lifetime of the specified `vanguard` is within the bounds of its `layer`.
724
    fn assert_expiry_in_bounds<R: Runtime>(
725
        vanguard: &Vanguard<'_>,
726
        mgr: &VanguardMgr<R>,
727
        runtime: &R,
728
        params: &VanguardParams,
729
        layer: Layer,
730
    ) {
731
        let (min, max) = match layer {
732
            Layer2 => (params.l2_lifetime_min(), params.l2_lifetime_max()),
733
            Layer3 => (params.l3_lifetime_min(), params.l3_lifetime_max()),
734
        };
735

            
736
        let vanguard = RelayIds::from_relay_ids(vanguard.relay());
737
        // This is not exactly the lifetime of the vanguard,
738
        // but rather the time left until it expires (but it's close enough for our purposes).
739
        let lifetime = duration_until_expiry(&vanguard, mgr, runtime, layer);
740

            
741
        assert!(
742
            lifetime >= min && lifetime <= max,
743
            "lifetime {lifetime:?} not between {min:?} and {max:?}",
744
        );
745
    }
746

            
747
    /// Assert that the vanguard manager's pools are empty.
748
    fn assert_sets_empty<R: Runtime>(vanguardmgr: &VanguardMgr<R>) {
749
        let inner = vanguardmgr.inner.read().unwrap();
750
        // The sets are initially empty, and the targets are set to 0
751
        assert_eq!(inner.vanguard_sets.l2_vanguards_deficit(), 0);
752
        assert_eq!(inner.vanguard_sets.l3_vanguards_deficit(), 0);
753
        assert_eq!(vanguard_count(vanguardmgr), 0);
754
    }
755

            
756
    /// Assert that the vanguard manager's pools have been filled.
757
    fn assert_sets_filled<R: Runtime>(vanguardmgr: &VanguardMgr<R>, params: &VanguardParams) {
758
        let inner = vanguardmgr.inner.read().unwrap();
759
        let l2_pool_size = params.l2_pool_size();
760
        // The sets are initially empty
761
        assert_eq!(inner.vanguard_sets.l2_vanguards_deficit(), 0);
762

            
763
        if inner.mode == VanguardMode::Full {
764
            assert_eq!(inner.vanguard_sets.l3_vanguards_deficit(), 0);
765
            let l3_pool_size = params.l3_pool_size();
766
            assert_eq!(vanguard_count(vanguardmgr), l2_pool_size + l3_pool_size);
767
        }
768
    }
769

            
770
    /// Assert the target size of the specified vanguard set matches the target from `params`.
771
    fn assert_set_vanguards_targets_match_params<R: Runtime>(
772
        mgr: &VanguardMgr<R>,
773
        params: &VanguardParams,
774
    ) {
775
        let inner = mgr.inner.read().unwrap();
776
        assert_eq!(
777
            inner.vanguard_sets.l2_vanguards_target(),
778
            params.l2_pool_size()
779
        );
780
        if inner.mode == VanguardMode::Full {
781
            assert_eq!(
782
                inner.vanguard_sets.l3_vanguards_target(),
783
                params.l3_pool_size()
784
            );
785
        }
786
    }
787

            
788
    #[test]
789
    fn full_vanguards_disabled() {
790
        MockRuntime::test_with_various(|rt| async move {
791
            let vanguardmgr = VanguardMgr::new_testing(&rt, VanguardMode::Lite).unwrap();
792
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
793
            let mut rng = testing_rng();
794
            // Wait until the vanguard manager has bootstrapped
795
            // (otherwise we'll get a BootstrapRequired error)
796
            let _netdir_provider = vanguardmgr.init_vanguard_sets(&netdir).await.unwrap();
797

            
798
            // Cannot select an L3 vanguard when running in "Lite" mode.
799
            let err = vanguardmgr
800
                .select_vanguard(&mut rng, &netdir, Layer3, &permissive_selector())
801
                .unwrap_err();
802
            assert!(
803
                matches!(
804
                    err,
805
                    VanguardMgrError::LayerNotSupported {
806
                        layer: Layer::Layer3,
807
                        mode: VanguardMode::Lite
808
                    }
809
                ),
810
                "{err}"
811
            );
812
        });
813
    }
814

            
815
    #[test]
816
    fn background_task_not_spawned() {
817
        MockRuntime::test_with_various(|rt| async move {
818
            let vanguardmgr = VanguardMgr::new_testing(&rt, VanguardMode::Lite).unwrap();
819
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
820
            let mut rng = testing_rng();
821

            
822
            // The sets are initially empty
823
            assert_sets_empty(&vanguardmgr);
824

            
825
            // VanguardMgr::launch_background tasks was not called, so select_vanguard will return
826
            // an error (because the vanguard sets are empty)
827
            let err = vanguardmgr
828
                .select_vanguard(&mut rng, &netdir, Layer2, &permissive_selector())
829
                .unwrap_err();
830

            
831
            assert!(
832
                matches!(
833
                    err,
834
                    VanguardMgrError::BootstrapRequired {
835
                        action: "select vanguard"
836
                    }
837
                ),
838
                "{err:?}"
839
            );
840
        });
841
    }
842

            
843
    #[test]
844
    fn select_vanguards() {
845
        MockRuntime::test_with_various(|rt| async move {
846
            let vanguardmgr = VanguardMgr::new_testing(&rt, VanguardMode::Full).unwrap();
847

            
848
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
849
            let params = VanguardParams::try_from(netdir.params()).unwrap();
850
            let mut rng = testing_rng();
851

            
852
            // The sets are initially empty
853
            assert_sets_empty(&vanguardmgr);
854

            
855
            // Wait until the vanguard manager has bootstrapped
856
            let _netdir_provider = vanguardmgr.init_vanguard_sets(&netdir).await.unwrap();
857

            
858
            assert_sets_filled(&vanguardmgr, &params);
859

            
860
            let vanguard1 = vanguardmgr
861
                .select_vanguard(&mut rng, &netdir, Layer2, &permissive_selector())
862
                .unwrap();
863
            assert_expiry_in_bounds(&vanguard1, &vanguardmgr, &rt, &params, Layer2);
864

            
865
            let exclusion = RelayExclusion::exclude_identities(
866
                vanguard1
867
                    .relay()
868
                    .identities()
869
                    .map(|id| id.to_owned())
870
                    .collect(),
871
            );
872
            let selector =
873
                RelaySelector::new(tor_relay_selection::RelayUsage::vanguard(), exclusion);
874

            
875
            let vanguard2 = vanguardmgr
876
                .select_vanguard(&mut rng, &netdir, Layer3, &selector)
877
                .unwrap();
878

            
879
            assert_expiry_in_bounds(&vanguard2, &vanguardmgr, &rt, &params, Layer3);
880
            // Ensure we didn't select the same vanguard twice
881
            assert_ne!(
882
                vanguard1.relay().identities().collect_vec(),
883
                vanguard2.relay().identities().collect_vec()
884
            );
885
        });
886
    }
887

            
888
    /// Override the vanguard params from the netdir, returning the new VanguardParams.
889
    ///
890
    /// This also waits until the vanguard manager has had a chance to process the changes.
891
    async fn install_new_params(
892
        rt: &MockRuntime,
893
        netdir_provider: &TestNetDirProvider,
894
        params: impl IntoIterator<Item = (&str, i32)>,
895
    ) -> VanguardParams {
896
        let new_netdir = testnet::construct_custom_netdir_with_params(|_, _, _| {}, params, None)
897
            .unwrap()
898
            .unwrap_if_sufficient()
899
            .unwrap();
900
        let new_params = VanguardParams::try_from(new_netdir.params()).unwrap();
901

            
902
        netdir_provider.set_netdir_and_notify(new_netdir).await;
903

            
904
        // Wait until the vanguard mgr has finished handling the new netdir.
905
        rt.progress_until_stalled().await;
906

            
907
        new_params
908
    }
909

            
910
    /// Switch the vanguard "mode" of the VanguardMgr to `mode`,
911
    /// by setting the vanguards-hs-service parameter.
912
    //
913
    // TODO(#1382): use this instead of switch_hs_mode_config.
914
    #[allow(unused)]
915
    async fn switch_hs_mode(
916
        rt: &MockRuntime,
917
        vanguardmgr: &VanguardMgr<MockRuntime>,
918
        netdir_provider: &TestNetDirProvider,
919
        mode: VanguardMode,
920
    ) {
921
        use VanguardMode::*;
922

            
923
        let _params = match mode {
924
            Lite => install_new_params(rt, netdir_provider, ENABLE_LITE_VANGUARDS).await,
925
            Full => install_new_params(rt, netdir_provider, ENABLE_FULL_VANGUARDS).await,
926
            Disabled => panic!("cannot disable vanguards in the vanguard tests!"),
927
        };
928

            
929
        assert_eq!(vanguardmgr.mode(), mode);
930
    }
931

            
932
    /// Switch the vanguard "mode" of the VanguardMgr to `mode`,
933
    /// by calling `VanguardMgr::reconfigure`.
934
    fn switch_hs_mode_config(vanguardmgr: &VanguardMgr<MockRuntime>, mode: VanguardMode) {
935
        let _ = vanguardmgr
936
            .reconfigure(&VanguardConfig {
937
                mode: ExplicitOrAuto::Explicit(mode),
938
            })
939
            .unwrap();
940

            
941
        assert_eq!(vanguardmgr.mode(), mode);
942
    }
943

            
944
    /// Use a new NetDir that excludes one of our L2 vanguards
945
    async fn install_netdir_excluding_vanguard<'a>(
946
        runtime: &MockRuntime,
947
        vanguard: &Vanguard<'_>,
948
        params: impl IntoIterator<Item = (&'a str, i32)>,
949
        netdir_provider: &TestNetDirProvider,
950
    ) -> NetDir {
951
        let new_netdir = construct_custom_netdir_with_params(
952
            |_idx, bld, _| {
953
                let md_so_far = bld.md.testing_md().unwrap();
954
                if md_so_far.ed25519_id() == vanguard.relay().id() {
955
                    bld.omit_rs = true;
956
                }
957
            },
958
            params,
959
            None,
960
        )
961
        .unwrap()
962
        .unwrap_if_sufficient()
963
        .unwrap();
964

            
965
        netdir_provider
966
            .set_netdir_and_notify(new_netdir.clone())
967
            .await;
968
        // Wait until the vanguard mgr has finished handling the new netdir.
969
        runtime.progress_until_stalled().await;
970

            
971
        new_netdir
972
    }
973

            
974
    #[test]
975
    fn override_vanguard_set_size() {
976
        MockRuntime::test_with_various(|rt| async move {
977
            let vanguardmgr = VanguardMgr::new_testing(&rt, VanguardMode::Lite).unwrap();
978
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
979
            // Wait until the vanguard manager has bootstrapped
980
            let netdir_provider = vanguardmgr.init_vanguard_sets(&netdir).await.unwrap();
981

            
982
            let params = VanguardParams::try_from(netdir.params()).unwrap();
983
            let old_size = params.l2_pool_size();
984
            assert_set_vanguards_targets_match_params(&vanguardmgr, &params);
985

            
986
            const PARAMS: [[(&str, i32); 2]; 2] = [
987
                [("guard-hs-l2-number", 1), ("guard-hs-l3-number", 10)],
988
                [("guard-hs-l2-number", 10), ("guard-hs-l3-number", 10)],
989
            ];
990

            
991
            for params in PARAMS {
992
                let new_params = install_new_params(&rt, &netdir_provider, params).await;
993

            
994
                // Ensure the target size was updated.
995
                assert_set_vanguards_targets_match_params(&vanguardmgr, &new_params);
996
                {
997
                    let inner = vanguardmgr.inner.read().unwrap();
998
                    let l2_vanguards = inner.l2_vanguards();
999
                    let l3_vanguards = inner.l3_vanguards();
                    let new_l2_size = params[0].1 as usize;
                    if new_l2_size < old_size {
                        // The actual size of the set hasn't changed: it's OK to have more vanguards than
                        // needed in the set (they extraneous ones will eventually expire).
                        assert_eq!(l2_vanguards.len(), old_size);
                    } else {
                        // The new size is greater, so we have more L2 vanguards now.
                        assert_eq!(l2_vanguards.len(), new_l2_size);
                    }
                    // There are no L3 vanguards because full vanguards are not in use.
                    assert_eq!(l3_vanguards.len(), 0);
                }
            }
        });
    }
    #[test]
    fn expire_vanguards() {
        MockRuntime::test_with_various(|rt| async move {
            let vanguardmgr = VanguardMgr::new_testing(&rt, VanguardMode::Lite).unwrap();
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
            let params = VanguardParams::try_from(netdir.params()).unwrap();
            let initial_l2_number = params.l2_pool_size();
            // Wait until the vanguard manager has bootstrapped
            let netdir_provider = vanguardmgr.init_vanguard_sets(&netdir).await.unwrap();
            assert_eq!(vanguard_count(&vanguardmgr), params.l2_pool_size());
            // Find the RelayIds of the vanguard that is due to expire next
            let vanguard_id = {
                let inner = vanguardmgr.inner.read().unwrap();
                let next_expiry = inner.vanguard_sets.next_expiry().unwrap();
                inner
                    .l2_vanguards()
                    .iter()
                    .find(|v| v.when == next_expiry)
                    .cloned()
                    .unwrap()
                    .id
            };
            const FEWER_VANGUARDS_PARAM: [(&str, i32); 1] = [("guard-hs-l2-number", 1)];
            // Set the number of L2 vanguards to a lower value to ensure the vanguard that is about
            // to expire is not replaced. This allows us to test that it has indeed expired
            // (we can't simply check that the relay is no longer is the set,
            // because it's possible for the set to get replenished with the same relay).
            let new_params = install_new_params(&rt, &netdir_provider, FEWER_VANGUARDS_PARAM).await;
            // The vanguard has not expired yet.
            let timebound_vanguard = find_in_set(&vanguard_id, &vanguardmgr, Layer2);
            assert!(timebound_vanguard.is_some());
            assert_eq!(vanguard_count(&vanguardmgr), initial_l2_number);
            let lifetime = duration_until_expiry(&vanguard_id, &vanguardmgr, &rt, Layer2);
            // Wait until this vanguard expires
            rt.advance_by(lifetime).await.unwrap();
            rt.progress_until_stalled().await;
            let timebound_vanguard = find_in_set(&vanguard_id, &vanguardmgr, Layer2);
            // The vanguard expired, but was not replaced.
            assert!(timebound_vanguard.is_none());
            assert_eq!(vanguard_count(&vanguardmgr), initial_l2_number - 1);
            // Wait until more vanguards expire. This will reduce the set size to 1
            // (the new target size we set by overriding the params).
            for _ in 0..initial_l2_number - 1 {
                let vanguard_id = {
                    let inner = vanguardmgr.inner.read().unwrap();
                    let next_expiry = inner.vanguard_sets.next_expiry().unwrap();
                    inner
                        .l2_vanguards()
                        .iter()
                        .find(|v| v.when == next_expiry)
                        .cloned()
                        .unwrap()
                        .id
                };
                let lifetime = duration_until_expiry(&vanguard_id, &vanguardmgr, &rt, Layer2);
                rt.advance_by(lifetime).await.unwrap();
                rt.progress_until_stalled().await;
            }
            assert_eq!(vanguard_count(&vanguardmgr), new_params.l2_pool_size());
            // Update the L2 set size again, to force the vanguard manager to replenish the L2 set.
            const MORE_VANGUARDS_PARAM: [(&str, i32); 1] = [("guard-hs-l2-number", 5)];
            // Set the number of L2 vanguards to a lower value to ensure the vanguard that is about
            // to expire is not replaced. This allows us to test that it has indeed expired
            // (we can't simply check that the relay is no longer is the set,
            // because it's possible for the set to get replenished with the same relay).
            let new_params = install_new_params(&rt, &netdir_provider, MORE_VANGUARDS_PARAM).await;
            // Check that we replaced the expired vanguard with a new one:
            assert_eq!(vanguard_count(&vanguardmgr), new_params.l2_pool_size());
            {
                let inner = vanguardmgr.inner.read().unwrap();
                let l2_count = inner.l2_vanguards().len();
                assert_eq!(l2_count, new_params.l2_pool_size());
            }
        });
    }
    #[test]
    fn full_vanguards_persistence() {
        MockRuntime::test_with_various(|rt| async move {
            let vanguardmgr = VanguardMgr::new_testing(&rt, VanguardMode::Lite).unwrap();
            let netdir =
                construct_custom_netdir_with_params(|_, _, _| {}, ENABLE_LITE_VANGUARDS, None)
                    .unwrap()
                    .unwrap_if_sufficient()
                    .unwrap();
            let netdir_provider = vanguardmgr.init_vanguard_sets(&netdir).await.unwrap();
            // Full vanguards are not enabled, so we don't expect anything to be written
            // to persistent storage.
            assert_eq!(vanguardmgr.mode(), VanguardMode::Lite);
            assert!(vanguardmgr.storage.load().unwrap().is_none());
            let mut rng = testing_rng();
            assert!(
                vanguardmgr
                    .select_vanguard(&mut rng, &netdir, Layer3, &permissive_selector())
                    .is_err()
            );
            // Enable full vanguards again.
            //
            // We expect VanguardMgr to populate the L3 set, and write the VanguardSets to storage.
            switch_hs_mode_config(&vanguardmgr, VanguardMode::Full);
            rt.progress_until_stalled().await;
            let vanguard_sets_orig = vanguardmgr.storage.load().unwrap();
            assert!(
                vanguardmgr
                    .select_vanguard(&mut rng, &netdir, Layer3, &permissive_selector())
                    .is_ok()
            );
            // Switch to lite vanguards.
            switch_hs_mode_config(&vanguardmgr, VanguardMode::Lite);
            // The vanguard sets should not change when switching between lite and full vanguards.
            assert_eq!(vanguard_sets_orig, vanguardmgr.storage.load().unwrap());
            switch_hs_mode_config(&vanguardmgr, VanguardMode::Full);
            assert_eq!(vanguard_sets_orig, vanguardmgr.storage.load().unwrap());
            // TODO HS-VANGUARDS: we may want to disable the ability to switch back to lite
            // vanguards.
            // Switch to lite vanguards and remove a relay from the consensus.
            // The relay should *not* be persisted to storage until we switch back to full
            // vanguards.
            switch_hs_mode_config(&vanguardmgr, VanguardMode::Lite);
            let mut rng = testing_rng();
            let excluded_vanguard = vanguardmgr
                .select_vanguard(&mut rng, &netdir, Layer2, &permissive_selector())
                .unwrap();
            let _ = install_netdir_excluding_vanguard(
                &rt,
                &excluded_vanguard,
                ENABLE_LITE_VANGUARDS,
                &netdir_provider,
            )
            .await;
            // The vanguard sets from storage haven't changed, because we are in "lite" mode.
            assert_eq!(vanguard_sets_orig, vanguardmgr.storage.load().unwrap());
            let _ = install_netdir_excluding_vanguard(
                &rt,
                &excluded_vanguard,
                ENABLE_FULL_VANGUARDS,
                &netdir_provider,
            )
            .await;
        });
    }
    #[test]
    fn load_from_state_file() {
        MockRuntime::test_with_various(|rt| async move {
            // Set the wallclock to a time when some of the stored vanguards are still valid.
            let now = time::UNIX_EPOCH + Duration::from_secs(1610000000);
            rt.jump_wallclock(now);
            let config = VanguardConfig {
                mode: ExplicitOrAuto::Explicit(VanguardMode::Full),
            };
            // The state file contains no vanguards
            let (statemgr, _dir) =
                state_dir_with_vanguards(r#"{ "l2_vanguards": [], "l3_vanguards": [] }"#);
            let vanguardmgr = VanguardMgr::new(&config, rt.clone(), statemgr, false).unwrap();
            {
                let inner = vanguardmgr.inner.read().unwrap();
                // The vanguard sets should be empty too
                assert!(inner.vanguard_sets.l2().is_empty());
                assert!(inner.vanguard_sets.l3().is_empty());
            }
            let (statemgr, _dir) = state_dir_with_vanguards(VANGUARDS_JSON);
            let vanguardmgr =
                Arc::new(VanguardMgr::new(&config, rt.clone(), statemgr, false).unwrap());
            let (initial_l2, initial_l3) = {
                let inner = vanguardmgr.inner.read().unwrap();
                let l2_vanguards = inner.vanguard_sets.l2_vanguards().clone();
                let l3_vanguards = inner.vanguard_sets.l3_vanguards().clone();
                // The sets actually contain 4 and 5 vanguards, respectively,
                // but the expired ones are discarded.
                assert_eq!(l2_vanguards.len(), 3);
                assert_eq!(l3_vanguards.len(), 2);
                // We don't know how many vanguards we're going to need
                // until we fetch the consensus.
                assert_eq!(inner.vanguard_sets.l2_vanguards_target(), 0);
                assert_eq!(inner.vanguard_sets.l3_vanguards_target(), 0);
                assert_eq!(inner.vanguard_sets.l2_vanguards_deficit(), 0);
                assert_eq!(inner.vanguard_sets.l3_vanguards_deficit(), 0);
                (l2_vanguards, l3_vanguards)
            };
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
            let _netdir_provider = vanguardmgr.init_vanguard_sets(&netdir).await.unwrap();
            {
                let inner = vanguardmgr.inner.read().unwrap();
                let l2_vanguards = inner.vanguard_sets.l2_vanguards();
                let l3_vanguards = inner.vanguard_sets.l3_vanguards();
                // The sets were replenished with more vanguards
                assert_eq!(l2_vanguards.len(), 4);
                assert_eq!(l3_vanguards.len(), 8);
                // We now know we need 4 L2 vanguards and 8 L3 ones.
                assert_eq!(inner.vanguard_sets.l2_vanguards_target(), 4);
                assert_eq!(inner.vanguard_sets.l3_vanguards_target(), 8);
                assert_eq!(inner.vanguard_sets.l2_vanguards_deficit(), 0);
                assert_eq!(inner.vanguard_sets.l3_vanguards_deficit(), 0);
                // All of the vanguards read from the state file should still be in the sets.
                assert!(initial_l2.iter().all(|v| l2_vanguards.contains(v)));
                assert!(initial_l3.iter().all(|v| l3_vanguards.contains(v)));
            }
        });
    }
    #[test]
    fn invalid_state_file() {
        MockRuntime::test_with_various(|rt| async move {
            let config = VanguardConfig {
                mode: ExplicitOrAuto::Explicit(VanguardMode::Full),
            };
            let (statemgr, _dir) = state_dir_with_vanguards(INVALID_VANGUARDS_JSON);
            let res = VanguardMgr::new(&config, rt.clone(), statemgr, false);
            assert!(matches!(res, Err(VanguardMgrError::State(_))));
        });
    }
}