1
//! Data structures that wrap [`FallbackDir`] and related primitives into
2
//! data structures required for bootstrapping Tor properly.
3
//!
4
//! These data structures primiarily carry some internal state that only gets
5
//! observed during bootstrap.
6

            
7
use crate::{dirstatus::DirStatus, skew::SkewObservation};
8
use rand::seq::IteratorRandom;
9
use std::time::{Duration, Instant};
10
use tor_dircommon::fallback::{FallbackDir, FallbackList};
11
use tor_linkspec::HasRelayIds;
12

            
13
use crate::{PickGuardError, ids::FallbackId};
14
use tor_basic_utils::iter::{FilterCount, IteratorExt as _};
15

            
16
/// A set of fallback directories, in usable form.
17
#[derive(Debug, Clone)]
18
pub(crate) struct FallbackState {
19
    /// The list of fallbacks in the set.
20
    ///
21
    /// We require that these are sorted and unique by (ED,RSA) keys.
22
    fallbacks: Vec<Entry>,
23
}
24

            
25
/// Wrapper type for FallbackDir converted into crate::Guard, and the status
26
/// information that we store about it.
27
///
28
/// Defines a sort order to ensure that we can look up fallback directories by
29
/// binary search on keys.
30
#[derive(Debug, Clone)]
31
pub(super) struct Entry {
32
    /// The inner fallback directory.
33
    fallback: FallbackDir,
34

            
35
    /// Whether the directory is currently usable, and if not, when we can retry
36
    /// it.
37
    status: DirStatus,
38
    /// The latest clock skew observation we have from this fallback directory
39
    /// (if any).
40
    clock_skew: Option<SkewObservation>,
41
}
42

            
43
/// Least amount of time we'll wait before retrying a fallback cache.
44
//
45
// TODO: we may want to make this configurable to a smaller value for chutney networks.
46
const FALLBACK_RETRY_FLOOR: Duration = Duration::from_secs(150);
47

            
48
impl From<FallbackDir> for Entry {
49
114456
    fn from(fallback: FallbackDir) -> Self {
50
114456
        let status = DirStatus::new(FALLBACK_RETRY_FLOOR);
51
114456
        Entry {
52
114456
            fallback,
53
114456
            status,
54
114456
            clock_skew: None,
55
114456
        }
56
114456
    }
57
}
58

            
59
impl HasRelayIds for Entry {
60
1992336
    fn identity(
61
1992336
        &self,
62
1992336
        key_type: tor_linkspec::RelayIdType,
63
1992336
    ) -> Option<tor_linkspec::RelayIdRef<'_>> {
64
1992336
        self.fallback.identity(key_type)
65
1992336
    }
66
}
67

            
68
impl From<&FallbackList> for FallbackState {
69
14284
    fn from(list: &FallbackList) -> Self {
70
114823
        let mut fallbacks: Vec<Entry> = list.iter().map(|fb| fb.clone().into()).collect();
71
882501
        fallbacks.sort_by(|x, y| x.cmp_by_relay_ids(y));
72
114246
        fallbacks.dedup_by(|x, y| x.same_relay_ids(y));
73
14284
        FallbackState { fallbacks }
74
14284
    }
75
}
76

            
77
impl FallbackState {
78
    /// Return a random member of this FallbackSet that's usable at `now`.
79
404
    pub(crate) fn choose<R: rand::Rng>(
80
404
        &self,
81
404
        rng: &mut R,
82
404
        now: Instant,
83
404
        filter: &crate::GuardFilter,
84
404
    ) -> Result<&FallbackDir, PickGuardError> {
85
404
        if self.fallbacks.is_empty() {
86
2
            return Err(PickGuardError::NoCandidatesAvailable);
87
402
        }
88

            
89
402
        let mut running = FilterCount::default();
90
402
        let mut filtered = FilterCount::default();
91

            
92
402
        self.fallbacks
93
402
            .iter()
94
1608
            .filter_cnt(&mut running, |ent| ent.status.usable_at(now))
95
1400
            .filter_cnt(&mut filtered, |ent| filter.permits(&ent.fallback))
96
402
            .choose(rng)
97
402
            .map(|ent| &ent.fallback)
98
402
            .ok_or_else(|| PickGuardError::AllFallbacksDown {
99
2
                retry_at: self.next_retry(),
100
2
                running,
101
2
                filtered,
102
2
            })
103
404
    }
104

            
105
    /// Return the next time at which any member of this set will become ready.
106
    ///
107
    /// Returns None if no elements are failing.
108
8
    fn next_retry(&self) -> Option<Instant> {
109
8
        self.fallbacks
110
8
            .iter()
111
36
            .filter_map(|ent| ent.status.next_retriable())
112
8
            .min()
113
8
    }
114

            
115
    /// Return a reference to the entry whose identity is `id`, if there is one.
116
24
    fn get(&self, id: &FallbackId) -> Option<&Entry> {
117
24
        match self.fallbacks.binary_search_by(|e| e.cmp_by_relay_ids(id)) {
118
            Ok(idx) => Some(&self.fallbacks[idx]),
119
24
            Err(_) => None,
120
        }
121
24
    }
122

            
123
    /// Return a mutable reference to the entry whose identity is `id`, if there is one.
124
40
    fn get_mut(&mut self, id: &FallbackId) -> Option<&mut Entry> {
125
154
        match self.fallbacks.binary_search_by(|e| e.cmp_by_relay_ids(id)) {
126
36
            Ok(idx) => Some(&mut self.fallbacks[idx]),
127
4
            Err(_) => None,
128
        }
129
40
    }
130

            
131
    /// Return true if this set contains some entry with the given `id`.
132
24
    pub(crate) fn contains(&self, id: &FallbackId) -> bool {
133
24
        self.get(id).is_some()
134
24
    }
135

            
136
    /// Record that a success has occurred for the fallback with the given
137
    /// identity.
138
    ///
139
    /// Be aware that for fallbacks, we only count a successful directory
140
    /// operation as a success: a circuit success is not enough.
141
2
    pub(crate) fn note_success(&mut self, id: &FallbackId) {
142
2
        if let Some(entry) = self.get_mut(id) {
143
2
            entry.status.note_success();
144
2
        }
145
2
    }
146

            
147
    /// Record that a failure has occurred for the fallback with the given
148
    /// identity.
149
14
    pub(crate) fn note_failure(&mut self, id: &FallbackId, now: Instant) {
150
14
        if let Some(entry) = self.get_mut(id) {
151
14
            entry.status.note_failure(now);
152
14
        }
153
14
    }
154

            
155
    /// Consume `other` and copy all of its fallback status entries into the corresponding entries for `self`.
156
90
    pub(crate) fn take_status_from(&mut self, other: FallbackState) {
157
        use itertools::EitherOrBoth::Both;
158

            
159
17617
        itertools::merge_join_by(self.fallbacks.iter_mut(), other.fallbacks, |a, b| {
160
17614
            a.fallback.cmp_by_relay_ids(&b.fallback)
161
17614
        })
162
17617
        .for_each(|entry| {
163
17614
            if let Both(entry, other) = entry {
164
17606
                debug_assert!(entry.fallback.same_relay_ids(&other.fallback));
165
17606
                entry.status = other.status;
166
8
            }
167
17614
        });
168
90
    }
169

            
170
    /// Record that a given fallback has told us about clock skew.
171
    pub(crate) fn note_skew(&mut self, id: &FallbackId, observation: SkewObservation) {
172
        if let Some(entry) = self.get_mut(id) {
173
            entry.clock_skew = Some(observation);
174
        }
175
    }
176

            
177
    /// Return an iterator over all the clock skew observations we've made for fallback directories
178
    pub(crate) fn skew_observations(&self) -> impl Iterator<Item = &SkewObservation> {
179
        self.fallbacks
180
            .iter()
181
            .filter_map(|fb| fb.clock_skew.as_ref())
182
    }
183
}
184

            
185
#[cfg(test)]
186
mod test {
187
    // @@ begin test lint list maintained by maint/add_warning @@
188
    #![allow(clippy::bool_assert_comparison)]
189
    #![allow(clippy::clone_on_copy)]
190
    #![allow(clippy::dbg_macro)]
191
    #![allow(clippy::mixed_attributes_style)]
192
    #![allow(clippy::print_stderr)]
193
    #![allow(clippy::print_stdout)]
194
    #![allow(clippy::single_char_pattern)]
195
    #![allow(clippy::unwrap_used)]
196
    #![allow(clippy::unchecked_time_subtraction)]
197
    #![allow(clippy::useless_vec)]
198
    #![allow(clippy::needless_pass_by_value)]
199
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
200

            
201
    use super::*;
202
    use rand::Rng;
203
    use tor_basic_utils::test_rng::testing_rng;
204

            
205
    /// Construct a `FallbackDir` with random identity keys and addresses.
206
    ///
207
    /// Since there are 416 bits of random id here, the risk of collision is
208
    /// negligible.
209
    fn rand_fb<R: Rng>(rng: &mut R) -> FallbackDir {
210
        let ed: [u8; 32] = rng.random();
211
        let rsa: [u8; 20] = rng.random();
212
        let ip: u32 = rng.random();
213
        let mut bld = FallbackDir::builder();
214
        bld.ed_identity(ed.into())
215
            .rsa_identity(rsa.into())
216
            .orports()
217
            .push(std::net::SocketAddrV4::new(ip.into(), 9090).into());
218
        bld.build().unwrap()
219
    }
220

            
221
    #[test]
222
    fn construct_fallback_set() {
223
        use rand::seq::SliceRandom;
224
        use std::cmp::Ordering as O;
225

            
226
        // fabricate some fallbacks.
227
        let mut rng = testing_rng();
228
        let fbs = vec![
229
            rand_fb(&mut rng),
230
            rand_fb(&mut rng),
231
            rand_fb(&mut rng),
232
            rand_fb(&mut rng),
233
        ];
234
        let fb_other = rand_fb(&mut rng);
235
        let id_other = FallbackId::from_relay_ids(&fb_other);
236

            
237
        // basic case: construct a set
238
        let list: FallbackList = fbs.clone().into();
239
        assert!(!list.is_empty());
240
        assert_eq!(list.len(), 4);
241
        let mut set: FallbackState = (&list).into();
242

            
243
        // inspect the generated set
244
        assert_eq!(set.fallbacks.len(), 4);
245
        assert_eq!(
246
            set.fallbacks[0].cmp_by_relay_ids(&set.fallbacks[1]),
247
            O::Less
248
        );
249
        assert_eq!(
250
            set.fallbacks[1].cmp_by_relay_ids(&set.fallbacks[2]),
251
            O::Less
252
        );
253
        assert_eq!(
254
            set.fallbacks[2].cmp_by_relay_ids(&set.fallbacks[3]),
255
            O::Less
256
        );
257

            
258
        // use the constructed set a little.
259
        for fb in fbs.iter() {
260
            let id = FallbackId::from_relay_ids(fb);
261
            assert_eq!(set.get_mut(&id).unwrap().cmp_by_relay_ids(&id), O::Equal);
262
        }
263
        assert!(set.get_mut(&id_other).is_none());
264

            
265
        // Now try an input set with duplicates.
266
        let mut redundant_fbs = fbs.clone();
267
        redundant_fbs.extend(fbs.clone());
268
        redundant_fbs.extend(fbs[0..2].iter().map(Clone::clone));
269
        redundant_fbs[..].shuffle(&mut testing_rng());
270
        let list2 = redundant_fbs.into();
271
        assert_ne!(&list, &list2);
272
        let set2: FallbackState = (&list2).into();
273

            
274
        // It should have the same elements, in the same order.
275
        assert_eq!(set.fallbacks.len(), set2.fallbacks.len());
276
        assert!(
277
            set.fallbacks
278
                .iter()
279
                .zip(set2.fallbacks.iter())
280
                .all(|(ent1, ent2)| ent1.same_relay_ids(ent2))
281
        );
282
    }
283

            
284
    #[test]
285
    fn set_choose() {
286
        dbg!("X");
287

            
288
        let mut rng = testing_rng();
289
        let fbs = vec![
290
            rand_fb(&mut rng),
291
            rand_fb(&mut rng),
292
            rand_fb(&mut rng),
293
            rand_fb(&mut rng),
294
        ];
295
        let list: FallbackList = fbs.into();
296
        let mut set: FallbackState = (&list).into();
297
        let filter = crate::GuardFilter::unfiltered();
298

            
299
        let mut counts = [0_usize; 4];
300
        let now = Instant::now();
301
        dbg!("A");
302
        fn lookup_idx(set: &FallbackState, id: &impl HasRelayIds) -> Option<usize> {
303
            set.fallbacks
304
                .binary_search_by(|ent| ent.fallback.cmp_by_relay_ids(id))
305
                .ok()
306
        }
307
        // Basic case: everybody is up.
308
        for _ in 0..100 {
309
            let fb = set.choose(&mut rng, now, &filter).unwrap();
310
            let idx = lookup_idx(&set, fb).unwrap();
311
            counts[idx] += 1;
312
        }
313
        dbg!("B");
314
        assert!(counts.iter().all(|v| *v > 0));
315

            
316
        // Mark somebody down and make sure they don't get chosen.
317
        let ids: Vec<_> = set
318
            .fallbacks
319
            .iter()
320
            .map(|ent| FallbackId::from_relay_ids(&ent.fallback))
321
            .collect();
322
        set.note_failure(&ids[2], now);
323
        counts = [0; 4];
324
        for _ in 0..100 {
325
            let fb = set.choose(&mut rng, now, &filter).unwrap();
326
            let idx = lookup_idx(&set, fb).unwrap();
327
            counts[idx] += 1;
328
        }
329
        assert_eq!(counts.iter().filter(|v| **v > 0).count(), 3);
330
        assert_eq!(counts[2], 0);
331

            
332
        // Mark everybody down; make sure we get the right error.
333
        for id in ids.iter() {
334
            set.note_failure(id, now);
335
        }
336
        assert!(matches!(
337
            set.choose(&mut rng, now, &filter),
338
            Err(PickGuardError::AllFallbacksDown { .. })
339
        ));
340

            
341
        // Construct an empty set; make sure we get the right error.
342
        let empty_set = FallbackState::from(&FallbackList::from(vec![]));
343
        assert!(matches!(
344
            empty_set.choose(&mut rng, now, &filter),
345
            Err(PickGuardError::NoCandidatesAvailable)
346
        ));
347

            
348
        // TODO: test restrictions and filters once they're implemented.
349
    }
350

            
351
    #[test]
352
    fn test_status() {
353
        let mut rng = testing_rng();
354
        let fbs = vec![
355
            rand_fb(&mut rng),
356
            rand_fb(&mut rng),
357
            rand_fb(&mut rng),
358
            rand_fb(&mut rng),
359
        ];
360
        let list: FallbackList = fbs.clone().into();
361
        let mut set: FallbackState = (&list).into();
362
        let ids: Vec<_> = set
363
            .fallbacks
364
            .iter()
365
            .map(|ent| FallbackId::from_relay_ids(&ent.fallback))
366
            .collect();
367

            
368
        let now = Instant::now();
369

            
370
        // There's no "next retry time" when everybody's up.
371
        assert!(set.next_retry().is_none());
372

            
373
        // Mark somebody down; try accessors.
374
        set.note_failure(&ids[3], now);
375
        assert!(set.fallbacks[3].status.next_retriable().unwrap() > now);
376
        assert!(!set.fallbacks[3].status.usable_at(now));
377
        assert_eq!(set.next_retry(), set.fallbacks[3].status.next_retriable());
378

            
379
        // Mark somebody else down; try accessors.
380
        set.note_failure(&ids[0], now);
381
        assert!(set.fallbacks[0].status.next_retriable().unwrap() > now);
382
        assert!(!set.fallbacks[0].status.usable_at(now));
383
        assert_eq!(
384
            set.next_retry().unwrap(),
385
            std::cmp::min(
386
                set.fallbacks[0].status.next_retriable().unwrap(),
387
                set.fallbacks[3].status.next_retriable().unwrap()
388
            )
389
        );
390

            
391
        // Mark somebody as running; try accessors.
392
        set.note_success(&ids[0]);
393
        assert!(set.fallbacks[0].status.next_retriable().is_none());
394
        assert!(set.fallbacks[0].status.usable_at(now));
395

            
396
        // Make a new set with slightly different members; make sure that we can copy stuff successfully.
397
        let mut fbs2: Vec<_> = fbs
398
            .into_iter()
399
            // (Remove the fallback with id==ids[2])
400
            .filter(|fb| FallbackId::from_relay_ids(fb) != ids[2])
401
            .collect();
402
        // add 2 new ones.
403
        let fbs_new = vec![rand_fb(&mut rng), rand_fb(&mut rng), rand_fb(&mut rng)];
404
        fbs2.extend(fbs_new.clone());
405

            
406
        let mut set2 = FallbackState::from(&FallbackList::from(fbs2.clone()));
407
        set2.take_status_from(set); // consumes set.
408
        assert_eq!(set2.fallbacks.len(), 6); // Started with 4, added 3, removed 1.
409

            
410
        // Make sure that the status entries  are correctly copied.
411
        assert!(set2.get_mut(&ids[0]).unwrap().status.usable_at(now));
412
        assert!(set2.get_mut(&ids[1]).unwrap().status.usable_at(now));
413
        assert!(set2.get_mut(&ids[2]).is_none());
414
        assert!(!set2.get_mut(&ids[3]).unwrap().status.usable_at(now));
415

            
416
        // Make sure that the new fbs are there.
417
        for new_fb in fbs_new {
418
            assert!(
419
                set2.get_mut(&FallbackId::from_relay_ids(&new_fb))
420
                    .unwrap()
421
                    .status
422
                    .usable_at(now)
423
            );
424
        }
425
    }
426
}