1
//! routerstatus calculation - interface between rs selection and per-rs calculation
2

            
3
use super::*;
4
pub(super) use netstatus::vote::RouterStatus as RouterStatusVote;
5
pub(super) use routerdesc::RdDigest;
6
pub(super) use tor_netdoc::doc::routerdesc;
7

            
8
/// Router id-tuple as used in consensus calculations
9
///
10
/// See
11
/// <https://spec.torproject.org/dir-spec/computing-consensus.html#choosing-relay-ids>
12
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deftly, derive_more::Display)]
13
#[derive_deftly(DiscrepReportFilter)]
14
#[display("ed={ed} / rsa={rsa}")]
15
pub(super) struct IdTuple {
16
    /// ed25519
17
    pub ed: Ed25519Public,
18

            
19
    /// RSA
20
    pub rsa: Base64Fingerprint,
21
}
22

            
23
/// Router status-tuple: critical info unified as an aggregate, "most voters wins"
24
///
25
/// See
26
/// <https://spec.torproject.org/dir-spec/computing-consensus.html#choosing-relay-descs>
27
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deftly)]
28
#[derive_deftly(StatusTupleFromRouterStatusVote)]
29
pub(super) struct StatusTuple {
30
    /// routerdesc digest
31
    pub doc_digest: RdDigest,
32
    /// publication field
33
    pub publication: Iso8601TimeSp,
34
    /// nickname
35
    pub nickname: Nickname,
36
    /// ip
37
    pub ip: Ipv4Addr,
38
    /// dir_port
39
    pub dir_port: u16,
40
    /// or_port
41
    pub or_port: u16,
42
}
43

            
44
/// "Already computed" context type for router status entries
45
///
46
/// Having a newtype around `PlainPreamble` allows us to specially mark
47
/// trait impls that apply only inside routerstatus entries.
48
///
49
/// That way we can `impl Aggregate` for types that appear in several places,
50
/// and the `AC` on the `Aggregate` impl avoids using the wrong one.
51
#[derive(derive_more::Deref)]
52
pub(super) struct RouterStatusAC<'r>(pub &'r PlainPreamble);
53

            
54
/// The `RouterStatusVote` provided by each voter that voted for this id-tuple
55
///
56
/// `None` for voters who didn't vote for this id-tuple.
57
pub(super) type RouterStatusPerRelevantVoter<'i> =
58
    TiSlice<VoterNum, Option<&'i netstatus::vote::RouterStatus>>;
59

            
60
/// Input to per-routerstatus routerstatus calculation
61
pub(super) struct ResolvedRouterStatusInputs<'i> {
62
    /// id-tuple
63
    pub id: IdTuple,
64

            
65
    /// status-tuple
66
    pub status_tuple: StatusTuple,
67

            
68
    /// routerstatuses from each vote that voted for this id-tuple
69
    pub per_voter: Box<RouterStatusPerRelevantVoter<'i>>,
70
}
71

            
72
impl IdTuple {
73
    /// Extract the id-tuple from a vote's routerstatus
74
836
    pub(super) fn from_rs(rs: &RouterStatusVote) -> Self {
75
836
        IdTuple {
76
836
            ed: rs.ed25519_id.pk,
77
836
            rsa: rs.r.identity,
78
836
        }
79
836
    }
80
}
81

            
82
define_derive_deftly! {
83
    /// Defines `struct DiscrepReportFilter`
84
    DiscrepReportFilter beta_deftly:
85

            
86
    /// Filter for discrepancy report algorithm
87
    ///
88
    /// Embodies the *chosen* relay identities, so that we can report the *other*
89
    /// relay identities for which we do output a routerdesc mentioning at least one
90
    /// of the relevant identities.
91
    ///
92
    /// Helps implement the logging requirement in
93
    /// <https://spec.torproject.org/dir-spec/computing-consensus.html#choosing-relay-ids>
94
    #[derive(Debug)]
95
    pub(super) struct DiscrepReportFilter<'i> {
96
        output_ids: &'i HashSet<&'i IdTuple>,
97
      $(
98
        $<included_ $fname>: HashSet<&'i $ftype>,
99
      )
100
    }
101

            
102
    impl<'i> DiscrepReportFilter<'i> {
103
        /// Make a new filter from the set of chosen output identities
104
16
        pub(super) fn from_outputs(output_ids: &'i HashSet<&'i IdTuple>) -> Self {
105
            Self {
106
                output_ids,
107
              $(
108
                $<included_ $fname>:
109
                    output_ids
110
                        .clone()
111
                        .into_iter()
112
142
                        .map(|id| &id.$fname)
113
                        .collect(),
114
              )
115
            }
116
        }
117

            
118
        /// Is this id discrepant (ie, should it be logged) ?
119
76
        pub(super) fn id_is_discrepant(&self, id: &IdTuple) -> bool {
120
            if self.output_ids.contains(id) {
121
                return false;
122
            }
123
          $(
124
            self.$<included_$fname>.contains(&id.$fname) ||
125
          )
126
            false
127
        }
128
    }
129
}
130
use derive_deftly_template_DiscrepReportFilter;
131

            
132
define_derive_deftly! {
133
    /// Derives `StatusTuple::from_rs` by copying the corresponding fields
134
    ///
135
    /// The data comes from the intro item `r`; it so happens that all the fields for use
136
    /// in the status-tuple are in the intro item.
137
    StatusTupleFromRouterStatusVote beta_deftly, meta_quoted strip:
138

            
139
    $impl {
140
        /// Extract the `StatusTuple` from a routerstatus
141
806
        pub(super) fn from_rs(rs: &RouterStatusVote) -> Self {
142
            Self { $(
143
                $fname: rs.r.$fname.clone().into(),
144
            ) }
145
        }
146
    }
147
}
148
use derive_deftly_template_StatusTupleFromRouterStatusVote;