1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
use crate::VerificationStatus;
use chrono::{self, offset::Utc, DateTime};
use crev_data::{
    self,
    proof::{
        self,
        review::{self, Rating},
        trust::TrustLevel,
        Content, ContentCommon,
    },
    Digest, Id, Url,
};
use default::default;
use semver::Version;
use std::collections::{hash_map, BTreeMap, BTreeSet, HashMap, HashSet};

/// A `T` with a timestamp
///
/// This allows easily keeping track of a most recent version
/// of `T`. Typically `T` is a *proof* of some kind.
#[derive(Clone, Debug)]
pub struct Timestamped<T> {
    pub date: chrono::DateTime<Utc>,
    value: T,
}

impl<T> Timestamped<T> {
    fn update_to_more_recent(&mut self, date: &chrono::DateTime<Utc>, value: T) {
        if self.date < *date {
            self.value = value;
        }
    }

    fn insert_into_or_update_to_more_recent<K>(self, entry: hash_map::Entry<K, Timestamped<T>>) {
        match entry {
            hash_map::Entry::Occupied(mut entry) => entry
                .get_mut()
                .update_to_more_recent(&self.date, self.value),
            hash_map::Entry::Vacant(entry) => {
                entry.insert(self);
            }
        }
    }
}

type TimestampedUrl = Timestamped<Url>;
type TimestampedTrustLevel = Timestamped<TrustLevel>;
type TimestampedReview = Timestamped<review::Review>;

impl From<proof::Trust> for TimestampedTrustLevel {
    fn from(trust: proof::Trust) -> Self {
        TimestampedTrustLevel {
            date: trust.date().with_timezone(&Utc),
            value: trust.trust,
        }
    }
}

impl<'a, T: review::Common> From<&'a T> for TimestampedReview {
    fn from(review: &T) -> Self {
        TimestampedReview {
            value: review.review().to_owned(),
            date: review.date().with_timezone(&Utc),
        }
    }
}

/// Unique package review
///
/// Since package review can be overwritten, it's useful
/// to refer to a review by an unique combination of
///
/// * author's ID
/// * source
/// * crate
/// * version
#[derive(Hash, Debug, Clone, PartialEq, Eq)]
pub struct UniquePackageReview {
    from: Id,
    source: String,
    name: String,
    version: Version,
}

type TimestampedSignature = Timestamped<String>;

impl From<review::Package> for UniquePackageReview {
    fn from(review: review::Package) -> Self {
        Self {
            from: review.from.id,
            source: review.package.source,
            name: review.package.name,
            version: review.package.version,
        }
    }
}

impl<Tz> From<(&DateTime<Tz>, String)> for TimestampedSignature
where
    Tz: chrono::TimeZone,
{
    fn from(args: (&DateTime<Tz>, String)) -> Self {
        Self {
            date: args.0.with_timezone(&Utc),
            value: args.1,
        }
    }
}
/// In memory database tracking information from proofs
///
/// After population, used for calculating the effcttive trust set, etc.
///
/// Right now, for every invocation of crev, we just load it up with
/// all known proofs, and then query. If it ever becomes too slow,
/// all the logic here will have to be moved to a real embedded db
/// of some kind.
pub struct ProofDB {
    trust_id_to_id: HashMap<Id, HashMap<Id, TimestampedTrustLevel>>, // who -(trusts)-> whom
    url_by_id: HashMap<Id, TimestampedUrl>,
    url_by_id_secondary: HashMap<Id, TimestampedUrl>,

    package_review_by_signature: HashMap<String, review::Package>,

    package_review_signatures_by_package_digest:
        HashMap<Vec<u8>, HashMap<UniquePackageReview, TimestampedSignature>>,
    package_review_signatures_by_unique_package_review:
        HashMap<UniquePackageReview, TimestampedSignature>,

    package_reviews_by_source: BTreeMap<String, HashSet<UniquePackageReview>>,
    package_reviews_by_name: BTreeMap<(String, String), HashSet<UniquePackageReview>>,
    package_reviews_by_version: BTreeMap<(String, String, Version), HashSet<UniquePackageReview>>,
    // available_unique_package_reviews_by_version:
}

impl Default for ProofDB {
    fn default() -> Self {
        ProofDB {
            trust_id_to_id: default(),
            url_by_id: default(),
            url_by_id_secondary: default(),
            package_review_signatures_by_package_digest: default(),
            package_review_signatures_by_unique_package_review: default(),
            package_review_by_signature: default(),
            package_reviews_by_source: default(),
            package_reviews_by_name: default(),
            package_reviews_by_version: default(),
        }
    }
}

impl ProofDB {
    pub fn new() -> Self {
        default()
    }

    pub fn get_advisories(
        &self,
        source: &str,
        name: Option<&str>,
        version: Option<&Version>,
    ) -> HashMap<Version, proof::review::Package> {
        match (name, version) {
            (Some(name), Some(version)) => self.get_advisories_for_version(source, name, version),

            (Some(name), None) => self.get_advisories_for_package(source, name),
            (None, None) => self.get_advisories_for_source(source),
            (None, Some(_)) => panic!("Wrong usage"),
        }
    }

    pub fn get_advisories_for_version(
        &self,
        source: &str,
        name: &str,
        queried_version: &Version,
    ) -> HashMap<Version, proof::review::Package> {
        self.package_reviews_by_version
            .range(
                (
                    source.to_owned(),
                    name.to_owned(),
                    queried_version.to_owned(),
                )..,
            )
            .take_while(|((review_source, review_name, _), _)| {
                review_source == source && review_name == name
            })
            .flat_map(|(review_version, uniq_pkg_reviews)| {
                uniq_pkg_reviews.iter().filter_map(move |uniq_pkg_review| {
                    let review_version = review_version.2.to_owned();
                    let review = self.package_review_by_signature[&self
                        .package_review_signatures_by_unique_package_review[uniq_pkg_review]
                        .value]
                        .to_owned();

                    if review.is_advisory_for(queried_version) {
                        Some((review_version, review))
                    } else {
                        None
                    }
                })
            })
            .collect()
    }

    pub fn get_advisories_for_package(
        &self,
        source: &str,
        name: &str,
    ) -> HashMap<Version, proof::review::Package> {
        let min_possible_version = Version::parse("0.0.0").unwrap();
        self.package_reviews_by_version
            .range((source.to_owned(), name.to_owned(), min_possible_version)..)
            .take_while(|((review_source, review_name, _), _)| {
                review_source == source && review_name == name
            })
            .flat_map(|(review_version, uniq_pkg_reviews)| {
                uniq_pkg_reviews.iter().filter_map(move |uniq_pkg_review| {
                    let review_version = review_version.2.to_owned();
                    let review = self.package_review_by_signature[&self
                        .package_review_signatures_by_unique_package_review[uniq_pkg_review]
                        .value]
                        .to_owned();

                    if review.advisory.is_some() {
                        Some((review_version, review))
                    } else {
                        None
                    }
                })
            })
            .collect()
    }

    pub fn get_advisories_for_source(
        &self,
        source: &str,
    ) -> HashMap<Version, proof::review::Package> {
        self.package_reviews_by_version
            .iter()
            .filter(|((review_source, _, _), _)| review_source == source)
            .flat_map(|(review_version, uniq_pkg_reviews)| {
                uniq_pkg_reviews.iter().filter_map(move |uniq_pkg_review| {
                    let review_version = review_version.2.to_owned();
                    let review = self.package_review_by_signature[&self
                        .package_review_signatures_by_unique_package_review[uniq_pkg_review]
                        .value]
                        .to_owned();

                    if review.advisory.is_some() {
                        Some((review_version, review))
                    } else {
                        None
                    }
                })
            })
            .collect()
    }

    pub fn unique_package_review_proof_count(&self) -> usize {
        self.package_review_signatures_by_unique_package_review
            .len()
    }

    pub fn unique_trust_proof_count(&self) -> usize {
        self.trust_id_to_id
            .iter()
            .fold(0, |count, (_id, set)| count + set.len())
    }

    fn add_code_review(&mut self, review: &review::Code) {
        let from = &review.from;
        self.record_url_from_from_field(&review.date_utc(), &from);
        for _file in &review.files {
            // not implemented right now; just ignore
        }
    }

    fn add_package_review(&mut self, review: &review::Package, signature: &str) {
        let from = &review.from;
        self.record_url_from_from_field(&review.date_utc(), &from);

        self.package_review_by_signature
            .entry(signature.to_owned())
            .or_insert_with(|| review.to_owned());

        let unique = UniquePackageReview::from(review.clone());
        let timestamp_signature = TimestampedSignature::from((review.date(), signature.to_owned()));

        timestamp_signature
            .clone()
            .insert_into_or_update_to_more_recent(
                self.package_review_signatures_by_package_digest
                    .entry(review.package.digest.to_owned())
                    .or_insert_with(default)
                    .entry(unique.clone()),
            );

        timestamp_signature.insert_into_or_update_to_more_recent(
            self.package_review_signatures_by_unique_package_review
                .entry(unique.clone()),
        );

        self.package_reviews_by_source
            .entry(review.package.source.to_owned())
            .or_default()
            .insert(unique.clone());
        self.package_reviews_by_name
            .entry((
                review.package.source.to_owned(),
                review.package.name.to_owned(),
            ))
            .or_default()
            .insert(unique.clone());
        self.package_reviews_by_version
            .entry((
                review.package.source.to_owned(),
                review.package.name.to_owned(),
                review.package.version.to_owned(),
            ))
            .or_default()
            .insert(unique);
    }

    pub fn get_package_review_count(
        &self,
        source: &str,
        name: Option<&str>,
        version: Option<&Version>,
    ) -> usize {
        self.get_package_reviews_for_package(source, name, version)
            .count()
    }

    pub fn get_package_reviews_for_package(
        &self,
        source: &str,
        name: Option<&str>,
        version: Option<&Version>,
    ) -> impl Iterator<Item = proof::review::Package> {
        let mut proofs: Vec<_> = match (name, version) {
            (Some(name), Some(version)) => self.package_reviews_by_version.get(&(
                source.to_owned(),
                name.to_owned(),
                version.to_owned(),
            )),

            (Some(name), None) => self
                .package_reviews_by_name
                .get(&(source.to_owned(), name.to_owned())),

            (None, None) => self.package_reviews_by_source.get(source),

            (None, Some(_)) => panic!("Wrong usage"),
        }
        .into_iter()
        .flat_map(|s| s)
        .map(|unique_package_review| {
            self.package_review_by_signature[&self
                .package_review_signatures_by_unique_package_review[unique_package_review]
                .value]
                .clone()
        })
        .collect();

        proofs.sort_by(|a, b| a.date().cmp(&b.date()));

        proofs.into_iter()
    }

    pub fn get_package_review_by_author(
        &self,
        source: &str,
        name: &str,
        version: &Version,
        id: &Id,
    ) -> Option<proof::review::Package> {
        self.get_package_reviews_for_package(source, Some(name), Some(version))
            .find(|pkg_review| pkg_review.from.id == *id)
    }

    fn add_trust_raw(&mut self, from: &Id, to: &Id, date: DateTime<Utc>, trust: TrustLevel) {
        TimestampedTrustLevel { value: trust, date }.insert_into_or_update_to_more_recent(
            self.trust_id_to_id
                .entry(from.to_owned())
                .or_insert_with(HashMap::new)
                .entry(to.to_owned()),
        );
    }

    fn add_trust(&mut self, trust: &proof::Trust) {
        let from = &trust.from;
        self.record_url_from_from_field(&trust.date_utc(), &from);
        for to in &trust.ids {
            self.add_trust_raw(&from.id, &to.id, trust.date_utc(), trust.trust);
        }
        for to in &trust.ids {
            self.record_url_from_to_field(&trust.date_utc(), &to)
        }
    }

    pub fn all_known_ids(&self) -> BTreeSet<Id> {
        self.url_by_id
            .keys()
            .chain(self.url_by_id_secondary.keys())
            .cloned()
            .collect()
    }

    /// Get all Ids that authored a proof (with total count)
    pub fn all_author_ids(&self) -> BTreeMap<Id, usize> {
        let mut res = BTreeMap::new();
        for (id, set) in &self.trust_id_to_id {
            *res.entry(id.to_owned()).or_default() += set.len();
        }

        for uniq_rev in self
            .package_review_signatures_by_unique_package_review
            .keys()
        {
            *res.entry(uniq_rev.from.clone()).or_default() += 1;
        }

        res
    }

    pub fn get_package_reviews_by_digest<'a>(
        &'a self,
        digest: &Digest,
    ) -> impl Iterator<Item = review::Package> + 'a {
        self.package_review_signatures_by_package_digest
            .get(digest.as_slice())
            .into_iter()
            .flat_map(move |unique_reviews| {
                unique_reviews
                    .iter()
                    .map(move |(_unique_review, signature)| {
                        self.package_review_by_signature[&signature.value].clone()
                    })
            })
    }

    pub fn verify_package_digest(
        &self,
        digest: &Digest,
        trust_set: &TrustSet,
    ) -> VerificationStatus {
        let reviews: HashMap<Id, review::Package> = self
            .get_package_reviews_by_digest(digest)
            .map(|review| (review.from.id.clone(), review))
            .collect();
        // Faster somehow maybe?
        let reviews_by: HashSet<Id, _> = reviews.keys().cloned().collect();
        let trusted_ids: HashSet<_> = trust_set.trusted_ids().cloned().collect();
        let matching_reviewers = trusted_ids.intersection(&reviews_by);
        let mut trust_count = 0;
        let mut trust_level = TrustLevel::None;
        let mut flagged_count = 0;
        let mut dangerous_count = 0;
        for matching_reviewer in matching_reviewers {
            let rating = &reviews[matching_reviewer].review.rating;
            if Rating::Neutral <= *rating {
                trust_count += 1;
                trust_level = std::cmp::max(
                    trust_level,
                    trust_set
                        .get_effective_trust_level(matching_reviewer)
                        .expect("Id should have been there"),
                );
            } else if *rating <= Rating::Dangerous {
                dangerous_count += 1;
            } else if *rating < Rating::Neutral {
                flagged_count += 1;
            }
        }

        if dangerous_count > 0 {
            VerificationStatus::Dangerous
        } else if flagged_count > 0 {
            VerificationStatus::Flagged
        } else if trust_count > 0 {
            VerificationStatus::Verified(trust_level)
        } else {
            VerificationStatus::None
        }
    }

    fn record_url_from_to_field(&mut self, date: &DateTime<Utc>, to: &crev_data::PubId) {
        self.url_by_id_secondary
            .entry(to.id.clone())
            .or_insert_with(|| TimestampedUrl {
                value: to.url.clone(),
                date: *date,
            });
    }

    fn record_url_from_from_field(&mut self, date: &DateTime<Utc>, from: &crev_data::PubId) {
        TimestampedUrl {
            value: from.url.clone(),
            date: date.to_owned(),
        }
        .insert_into_or_update_to_more_recent(self.url_by_id.entry(from.id.clone()));
    }
    fn add_proof(&mut self, proof: &proof::Proof) {
        proof
            .verify()
            .expect("All proofs were supposed to be valid here");
        match proof.content {
            Content::Code(ref review) => self.add_code_review(&review),
            Content::Package(ref review) => self.add_package_review(&review, &proof.signature),
            Content::Trust(ref trust) => self.add_trust(&trust),
        }
    }

    pub fn import_from_iter(&mut self, i: impl Iterator<Item = proof::Proof>) {
        for proof in i {
            self.add_proof(&proof);
        }
    }

    fn get_trust_list_of_id(&self, id: &Id) -> impl Iterator<Item = (TrustLevel, &Id)> {
        if let Some(map) = self.trust_id_to_id.get(id) {
            Some(map.iter().map(|(id, trust)| (trust.value, id)))
        } else {
            None
        }
        .into_iter()
        .flatten()
    }

    pub fn calculate_trust_set(&self, for_id: &Id, params: &TrustDistanceParams) -> TrustSet {
        let mut distrusted = HashMap::new();

        // We keep retrying the whole thing, with more and more
        // distrusted Ids
        loop {
            let prev_distrusted_len = distrusted.len();
            let trust_set = self.calculate_trust_set_internal(for_id, params, distrusted);
            if trust_set.distrusted.len() <= prev_distrusted_len {
                return trust_set;
            }
            distrusted = trust_set.distrusted;
        }
    }

    /// Calculate the effective trust levels for IDs inside a WoT.
    ///
    /// This is one of the most important functions in `crev-lib`.
    fn calculate_trust_set_internal(
        &self,
        for_id: &Id,
        params: &TrustDistanceParams,
        distrusted: HashMap<Id, HashSet<Id>>,
    ) -> TrustSet {
        #[derive(PartialOrd, Ord, Eq, PartialEq, Clone, Debug)]
        struct Visit {
            distance: u64,
            id: Id,
        }

        let mut pending = BTreeSet::new();
        let mut visited = TrustSet::default();
        visited.distrusted = distrusted;

        pending.insert(Visit {
            distance: 0,
            id: for_id.clone(),
        });
        visited.record_trusted_id(for_id.clone(), for_id.clone(), 0, TrustLevel::High);

        while let Some(current) = pending.iter().next().cloned() {
            pending.remove(&current);

            for (level, candidate_id) in self.get_trust_list_of_id(&&current.id) {
                if level == TrustLevel::Distrust {
                    visited
                        .distrusted
                        .entry(candidate_id.clone())
                        .or_default()
                        .insert(current.id.clone());
                    continue;
                }

                let candidate_distance_from_current =
                    if let Some(v) = params.distance_by_level(level) {
                        v
                    } else {
                        continue;
                    };

                if visited.distrusted.contains_key(candidate_id) {
                    continue;
                }
                let candidate_total_distance = current.distance + candidate_distance_from_current;

                if candidate_total_distance > params.max_distance {
                    continue;
                }

                let candidate_effective_trust = std::cmp::min(
                    level,
                    visited
                        .get_effective_trust_level(&current.id)
                        .expect("Id should have been inserted to `visited` beforehand"),
                );

                if candidate_effective_trust < TrustLevel::None {
                    unreachable!(
                        "this should not happen: candidate_effective_trust < TrustLevel::None"
                    );
                }

                if visited.record_trusted_id(
                    candidate_id.clone(),
                    current.id.clone(),
                    candidate_total_distance,
                    candidate_effective_trust,
                ) {
                    pending.insert(Visit {
                        distance: candidate_total_distance,
                        id: candidate_id.to_owned(),
                    });
                }
            }
        }

        visited
    }

    pub fn lookup_url(&self, id: &Id) -> Option<&Url> {
        self.url_by_id
            .get(id)
            .or_else(|| self.url_by_id_secondary.get(id))
            .map(|url| &url.value)
    }
}

/// Details of a one Id that is
#[derive(Debug)]
struct TrustedIdDetails {
    distance: u64,
    // effective, global trust from the root of the WoT
    effective_trust: TrustLevel,
    referers: HashMap<Id, TrustLevel>,
}

#[derive(Default, Debug)]
pub struct TrustSet {
    trusted: HashMap<Id, TrustedIdDetails>,
    distrusted: HashMap<Id, HashSet<Id>>,
}

impl TrustSet {
    pub fn trusted_ids(&self) -> impl Iterator<Item = &Id> {
        self.trusted.keys()
    }

    pub fn contains_trusted(&self, id: &Id) -> bool {
        self.trusted.contains_key(id)
    }

    /// Record that an Id is considered trusted
    ///
    /// Returns `true` if this actually added or changed the `subject` details,
    /// which requires revising it's own downstream trusted Id details in the graph algorithm for it.
    fn record_trusted_id(
        &mut self,
        subject: Id,
        referer: Id,
        distance: u64,
        effective_trust: TrustLevel,
    ) -> bool {
        // TODO: turn into log or something
        // eprintln!(
        //     "{} -> {} {} ({})",
        //     referer, subject, distance, effective_trust
        // );
        use std::collections::hash_map::Entry;

        match self.trusted.entry(subject) {
            Entry::Vacant(entry) => {
                let mut referers = HashMap::default();
                referers.insert(referer, effective_trust);
                entry.insert(TrustedIdDetails {
                    distance,
                    effective_trust,
                    referers,
                });
                true
            }
            Entry::Occupied(mut entry) => {
                let mut changed = false;
                let details = entry.get_mut();
                if details.distance > distance {
                    details.distance = distance;
                    changed = true;
                }
                if details.effective_trust < effective_trust {
                    details.effective_trust = effective_trust;
                    changed = true;
                }
                match details.referers.entry(referer.clone()) {
                    Entry::Vacant(entry) => {
                        entry.insert(effective_trust);
                        changed = true;
                    }
                    Entry::Occupied(mut entry) => {
                        let level = entry.get_mut();
                        if *level < effective_trust {
                            *level = effective_trust;
                            changed = true;
                        }
                    }
                }
                changed
            }
        }
    }

    pub fn get_effective_trust_level(&self, id: &Id) -> Option<TrustLevel> {
        self.trusted.get(id).map(|details| details.effective_trust)
    }
}

pub struct TrustDistanceParams {
    pub max_distance: u64,
    pub high_trust_distance: u64,
    pub medium_trust_distance: u64,
    pub low_trust_distance: u64,
}

impl TrustDistanceParams {
    fn distance_by_level(&self, level: TrustLevel) -> Option<u64> {
        use crev_data::proof::trust::TrustLevel::*;
        Some(match level {
            Distrust => return Option::None,
            None => return Option::None,
            Low => self.low_trust_distance,
            Medium => self.medium_trust_distance,
            High => self.high_trust_distance,
        })
    }
}

impl Default for TrustDistanceParams {
    fn default() -> Self {
        Self {
            max_distance: 10,
            high_trust_distance: 0,
            medium_trust_distance: 1,
            low_trust_distance: 5,
        }
    }
}