1
//! consensus document builders - items that vary by consensus flavor
2
//!
3
//! **This file is reincluded multiple times**,
4
//! by the macros in [`crate::doc::ns_variety_definition_macros`],
5
//! once for votes, and once for each consensus flavour.
6
//! It is *not* a module `crate::doc::netstatus::rs::each_flavor`.
7
//!
8
//! Each time this file is included by one of the macros mentioned above,
9
//! the `ns_***` macros (such as `ns_const_name!`) may expand to different values.
10
//!
11
//! See [`crate::doc::ns_variety_definition_macros`].
12

            
13
ns_use_this_variety! {
14
    use [crate::doc::netstatus::rs::build]::?::{RouterStatusBuilder};
15
    use [crate::doc::netstatus::rs]::?::{RouterStatus};
16
}
17
#[cfg(not(doc))]
18
ns_use_this_variety! {
19
    use [crate::doc::netstatus]::?::{Consensus, Preamble};
20
}
21
#[cfg(doc)]
22
ns_use_this_variety! {
23
    pub use [crate::doc::netstatus]::?::{Consensus, Preamble};
24
}
25

            
26
use super::*;
27

            
28
/// A builder object used to construct a consensus.
29
///
30
/// Create one of these with the [`Consensus::builder`] method.
31
///
32
/// This facility is only enabled when the crate is built with
33
/// the `build_docs` feature.
34
#[cfg_attr(docsrs, doc(cfg(feature = "build_docs")))]
35
pub struct ConsensusBuilder {
36
    /// See [`Consensus::flavor`]
37
    flavor: ConsensusFlavor,
38
    /// See [`Preamble::lifetime`]
39
    lifetime: Option<Lifetime>,
40
    /// See [`Preamble::client_versions`]
41
    client_versions: Vec<String>,
42
    /// See [`Preamble::server_versions`]
43
    server_versions: Vec<String>,
44
    /// See [`Preamble::proto_statuses`]
45
    client_protos: ProtoStatus,
46
    /// See [`Preamble::proto_statuses`]
47
    relay_protos: ProtoStatus,
48
    /// See [`Preamble::params`]
49
    params: NetParams<i32>,
50
    /// See [`Preamble::voting_delay`]
51
    voting_delay: Option<(u32, u32)>,
52
    /// See [`Preamble::consensus_method`]
53
    consensus_method: Option<u32>,
54
    /// See [`SharedRandStatuses::shared_rand_previous_value`]
55
    shared_rand_previous_value: Option<SharedRandStatus>,
56
    /// See [`SharedRandStatuses::shared_rand_current_value`]
57
    shared_rand_current_value: Option<SharedRandStatus>,
58
    /// See [`Consensus::voters`]
59
    voters: Vec<ConsensusAuthorityEntry>,
60
    /// See [`Consensus::relays`]
61
    relays: Vec<RouterStatus>,
62
    /// See [`ConsensusFooterFields::bandwidth_weights`]
63
    weights: NetParams<i32>,
64
}
65

            
66
impl ConsensusBuilder {
67
    /// Construct a new ConsensusBuilder object.
68
14335
    pub(crate) fn new(flavor: ConsensusFlavor) -> ConsensusBuilder {
69
14335
        ConsensusBuilder {
70
14335
            flavor,
71
14335
            lifetime: None,
72
14335
            client_versions: Vec::new(),
73
14335
            server_versions: Vec::new(),
74
14335
            client_protos: ProtoStatus::default(),
75
14335
            relay_protos: ProtoStatus::default(),
76
14335
            params: NetParams::new(),
77
14335
            voting_delay: None,
78
14335
            consensus_method: None,
79
14335
            shared_rand_previous_value: None,
80
14335
            shared_rand_current_value: None,
81
14335
            voters: Vec::new(),
82
14335
            relays: Vec::new(),
83
14335
            weights: NetParams::new(),
84
14335
        }
85
14335
    }
86

            
87
    /// Set the lifetime of this consensus.
88
    ///
89
    /// This value is required.
90
12295
    pub fn lifetime(&mut self, lifetime: Lifetime) -> &mut Self {
91
12295
        self.lifetime = Some(lifetime);
92
12295
        self
93
12295
    }
94

            
95
    /// Add a single recommended Tor client version to this consensus.
96
    ///
97
    /// These values are optional for testing.
98
4
    pub fn add_client_version(&mut self, ver: String) -> &mut Self {
99
4
        self.client_versions.push(ver);
100
4
        self
101
4
    }
102
    /// Add a single recommended Tor relay version to this consensus.
103
    ///
104
    /// These values are optional for testing.
105
4
    pub fn add_relay_version(&mut self, ver: String) -> &mut Self {
106
4
        self.server_versions.push(ver);
107
4
        self
108
4
    }
109
    /// Set the required client protocol versions for this consensus.
110
    ///
111
    /// This value defaults to "no protocol versions required."
112
4
    pub fn required_client_protos(&mut self, protos: Protocols) -> &mut Self {
113
4
        self.client_protos.required = protos;
114
4
        self
115
4
    }
116
    /// Set the recommended client protocol versions for this consensus.
117
    ///
118
    /// This value defaults to "no protocol versions recommended."
119
4
    pub fn recommended_client_protos(&mut self, protos: Protocols) -> &mut Self {
120
4
        self.client_protos.recommended = protos;
121
4
        self
122
4
    }
123
    /// Set the required relay protocol versions for this consensus.
124
    ///
125
    /// This value defaults to "no protocol versions required."
126
4
    pub fn required_relay_protos(&mut self, protos: Protocols) -> &mut Self {
127
4
        self.relay_protos.required = protos;
128
4
        self
129
4
    }
130
    /// Set the recommended client protocol versions for this consensus.
131
    ///
132
    /// This value defaults to "no protocol versions recommended."
133
4
    pub fn recommended_relay_protos(&mut self, protos: Protocols) -> &mut Self {
134
4
        self.relay_protos.recommended = protos;
135
4
        self
136
4
    }
137
    /// Set the value for a given consensus parameter by name.
138
10344
    pub fn param<S>(&mut self, param: S, val: i32) -> &mut Self
139
10344
    where
140
10344
        S: Into<String>,
141
    {
142
10344
        self.params.set(param.into(), val);
143
10344
        self
144
10344
    }
145
    /// Set the voting delays (in seconds) for this consensus.
146
4
    pub fn voting_delay(&mut self, vote_delay: u32, signature_delay: u32) -> &mut Self {
147
4
        self.voting_delay = Some((vote_delay, signature_delay));
148
4
        self
149
4
    }
150
    /// Set the declared consensus method for this consensus.
151
    ///
152
    /// This value is required.
153
11989
    pub fn consensus_method(&mut self, consensus_method: u32) -> &mut Self {
154
11989
        self.consensus_method = Some(consensus_method);
155
11989
        self
156
11989
    }
157
    /// Set the previous day's shared-random value for this consensus.
158
    ///
159
    /// This value is optional.
160
9031
    pub fn shared_rand_prev(
161
9031
        &mut self,
162
9031
        n_reveals: u8,
163
9031
        value: SharedRandVal,
164
9031
        timestamp: Option<SystemTime>,
165
9031
    ) -> &mut Self {
166
9031
        self.shared_rand_previous_value = Some(SharedRandStatus {
167
9031
            n_reveals,
168
9031
            value,
169
9031
            timestamp: timestamp.map(Iso8601TimeNoSp),
170
9031
        });
171
9031
        self
172
9031
    }
173
    /// Set the current day's shared-random value for this consensus.
174
    ///
175
    /// This value is optional.
176
9031
    pub fn shared_rand_cur(
177
9031
        &mut self,
178
9031
        n_reveals: u8,
179
9031
        value: SharedRandVal,
180
9031
        timestamp: Option<SystemTime>,
181
9031
    ) -> &mut Self {
182
9031
        self.shared_rand_current_value = Some(SharedRandStatus {
183
9031
            n_reveals,
184
9031
            value,
185
9031
            timestamp: timestamp.map(Iso8601TimeNoSp),
186
9031
        });
187
9031
        self
188
9031
    }
189
    /// Set a named weight parameter for this consensus.
190
8
    pub fn weight<S>(&mut self, param: S, val: i32) -> &mut Self
191
8
    where
192
8
        S: Into<String>,
193
    {
194
8
        self.weights.set(param.into(), val);
195
8
        self
196
8
    }
197
    /// Replace all weight parameters for this consensus.
198
11985
    pub fn weights(&mut self, weights: NetParams<i32>) -> &mut Self {
199
11985
        self.weights = weights;
200
11985
        self
201
11985
    }
202
    /// Create a VoterInfoBuilder to add a voter to this builder.
203
    ///
204
    /// In theory these are required, but nothing asks for them.
205
4
    pub fn voter(&self) -> VoterInfoBuilder {
206
4
        VoterInfoBuilder::new()
207
4
    }
208

            
209
    /// Insert a single routerstatus into this builder.
210
435085
    pub(crate) fn add_rs(&mut self, rs: RouterStatus) -> &mut Self {
211
435085
        self.relays.push(rs);
212
435085
        self
213
435085
    }
214
}
215

            
216
impl ConsensusBuilder {
217
    /// Create a RouterStatusBuilder to add a RouterStatus to this builder.
218
    ///
219
    /// You can make a consensus with no RouterStatus entries, but it
220
    /// won't actually be good for anything.
221
453190
    pub fn rs(&self) -> RouterStatusBuilder {
222
453190
        RouterStatusBuilder::new()
223
453190
    }
224

            
225
    /// Try to create a consensus object from this builder.
226
    ///
227
    /// This object might not have all of the data that a valid
228
    /// consensus would have. Therefore, it should only be used for
229
    /// testing.
230
11989
    pub fn testing_consensus(&self) -> Result<Consensus> {
231
11989
        let lifetime = self
232
11989
            .lifetime
233
11989
            .as_ref()
234
11989
            .ok_or(Error::CannotBuild("Missing lifetime."))?
235
11989
            .clone();
236

            
237
11989
        let proto_statuses = Arc::new(ProtoStatuses {
238
11989
            client: self.client_protos.clone(),
239
11989
            relay: self.relay_protos.clone(),
240
11989
        });
241

            
242
11989
        let consensus_method = self
243
11989
            .consensus_method
244
11989
            .ok_or(Error::CannotBuild("Missing consensus method."))?;
245

            
246
11989
        let shared_rand = SharedRandStatuses {
247
11989
            shared_rand_previous_value: self.shared_rand_previous_value.clone(),
248
11989
            shared_rand_current_value: self.shared_rand_current_value.clone(),
249
11989
            __non_exhaustive: (),
250
11989
        };
251

            
252
25625
        let mk_versions = |v: &[String]| {
253
25388
            crate::doc::netstatus::RecommendedTorVersions::from_iter(v.iter())
254
25388
                .map_err(|_| crate::NetdocErrorKind::BadApiUsage.err())
255
25388
        };
256

            
257
11989
        let preamble = Preamble {
258
11989
            lifetime,
259
11989
            client_versions: mk_versions(&self.client_versions)?,
260
11989
            server_versions: mk_versions(&self.server_versions)?,
261
11989
            proto_statuses,
262
11989
            params: self.params.clone(),
263
11989
            voting_delay: self.voting_delay,
264
11989
            consensus_method: (consensus_method,),
265
11989
            consensus_methods: NotPresent,
266
11989
            published: NotPresent,
267
11989
            known_flags: DocRelayFlags::new_empty_unknown_discarded(),
268
11989
            shared_rand,
269
11989
            __non_exhaustive: (),
270
        };
271

            
272
11989
        let footer = ConsensusFooterFields {
273
11989
            bandwidth_weights: self.weights.clone(),
274
11989
            __non_exhaustive: (),
275
11989
        };
276

            
277
11989
        let mut relays = self.relays.clone();
278
917807
        relays.sort_by_key(|r| *r.rsa_identity());
279
        // TODO: check for duplicates?
280

            
281
11989
        Ok(Consensus {
282
11989
            flavor: self.flavor,
283
11989
            preamble,
284
11989
            voters: self.voters.clone(),
285
11989
            relays,
286
11989
            footer,
287
11989
        })
288
11989
    }
289
}
290

            
291
/// Builder object for constructing a [`ConsensusAuthorityEntry`]
292
pub struct VoterInfoBuilder {
293
    /// See [`DirSource::nickname`]
294
    nickname: Option<String>,
295
    /// See [`DirSource::identity`]
296
    identity: Option<RsaIdentity>,
297
    /// See [`DirSource::ip`]
298
    ip: Option<IpAddr>,
299
    /// See [`ConsensusAuthorityEntry::contact`]
300
    contact: Option<String>,
301
    /// See [`ConsensusAuthorityEntry::vote_digest`]
302
    vote_digest: Vec<u8>,
303
    /// See [`DirSource::or_port`]
304
    or_port: u16,
305
    /// See [`DirSource::dir_port`]
306
    dir_port: u16,
307
}
308

            
309
impl VoterInfoBuilder {
310
    /// Construct a new VoterInfoBuilder.
311
4
    pub(crate) fn new() -> Self {
312
4
        VoterInfoBuilder {
313
4
            nickname: None,
314
4
            identity: None,
315
4
            ip: None,
316
4
            contact: None,
317
4
            vote_digest: Vec::new(),
318
4
            or_port: 0,
319
4
            dir_port: 0,
320
4
        }
321
4
    }
322

            
323
    /// Set a nickname.
324
    ///
325
    /// This value is required.
326
4
    pub fn nickname(&mut self, nickname: String) -> &mut Self {
327
4
        self.nickname = Some(nickname);
328
4
        self
329
4
    }
330

            
331
    /// Set an RSA identity.
332
    ///
333
    /// This value is required.
334
4
    pub fn identity(&mut self, identity: RsaIdentity) -> &mut Self {
335
4
        self.identity = Some(identity);
336
4
        self
337
4
    }
338

            
339
    /// Set a IP-valued address.
340
    ///
341
    /// This value is required.
342
4
    pub fn ip(&mut self, ip: IpAddr) -> &mut Self {
343
4
        self.ip = Some(ip);
344
4
        self
345
4
    }
346

            
347
    /// Set a contact line for this voter.
348
    ///
349
    /// This value is optional.
350
4
    pub fn contact(&mut self, contact: String) -> &mut Self {
351
4
        self.contact = Some(contact);
352
4
        self
353
4
    }
354

            
355
    /// Set the declared vote digest for this voter within a consensus.
356
    ///
357
    /// This value is required.
358
4
    pub fn vote_digest(&mut self, vote_digest: Vec<u8>) -> &mut Self {
359
4
        self.vote_digest = vote_digest;
360
4
        self
361
4
    }
362

            
363
    /// Set the declared OrPort for this voter.
364
4
    pub fn or_port(&mut self, or_port: u16) -> &mut Self {
365
4
        self.or_port = or_port;
366
4
        self
367
4
    }
368

            
369
    /// Set the declared DirPort for this voter.
370
4
    pub fn dir_port(&mut self, dir_port: u16) -> &mut Self {
371
4
        self.dir_port = dir_port;
372
4
        self
373
4
    }
374

            
375
    /// Add the voter that we've been building into the in-progress
376
    /// consensus of `builder`.
377
4
    pub fn build(&self, builder: &mut ConsensusBuilder) -> Result<()> {
378
4
        let nickname = self
379
4
            .nickname
380
4
            .as_ref()
381
4
            .ok_or(Error::CannotBuild("Missing nickname"))?
382
4
            .parse()
383
4
            .map_err(|_| Error::CannotBuild("Invalid nickname"))?;
384
4
        let identity = self
385
4
            .identity
386
4
            .ok_or(Error::CannotBuild("Missing identity"))?
387
4
            .into();
388
4
        let ip = self.ip.ok_or(Error::CannotBuild("Missing IP"))?;
389
4
        let contact = self
390
4
            .contact
391
4
            .as_ref()
392
4
            .ok_or(Error::CannotBuild("Missing contact"))?
393
4
            .parse()
394
4
            .map_err(|_| Error::CannotBuild("Invalid contact"))?;
395
4
        if self.vote_digest.is_empty() {
396
            return Err(Error::CannotBuild("Missing vote digest"));
397
4
        }
398
4
        let dir_source = DirSource {
399
4
            nickname,
400
4
            identity,
401
4
            hostname: "deprecated-builder.invalid".parse().expect("statically valid"),
402
4
            ip,
403
4
            dir_port: self.dir_port,
404
4
            or_port: self.or_port,
405
4
            __non_exhaustive: (),
406
4
        };
407

            
408
4
        let info = ConsensusAuthorityEntry {
409
4
            dir_source,
410
4
            contact,
411
4
            vote_digest: self.vote_digest.clone().into(),
412
4
            __non_exhaustive: (),
413
4
        };
414
4
        builder.voters.push(info);
415
4
        Ok(())
416
4
    }
417
}
418

            
419
#[cfg(test)]
420
mod test {
421
    // @@ begin test lint list maintained by maint/add_warning @@
422
    #![allow(clippy::bool_assert_comparison)]
423
    #![allow(clippy::clone_on_copy)]
424
    #![allow(clippy::dbg_macro)]
425
    #![allow(clippy::mixed_attributes_style)]
426
    #![allow(clippy::print_stderr)]
427
    #![allow(clippy::print_stdout)]
428
    #![allow(clippy::single_char_pattern)]
429
    #![allow(clippy::unwrap_used)]
430
    #![allow(clippy::unchecked_time_subtraction)]
431
    #![allow(clippy::useless_vec)]
432
    #![allow(clippy::needless_pass_by_value)]
433
    #![allow(clippy::string_slice)] // See arti#2571
434
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
435
    use super::*;
436
    use crate::types::relay_flags::RelayFlag;
437

            
438
    use std::net::SocketAddr;
439
    use web_time_compat::{Duration, SystemTime, SystemTimeExt};
440

            
441
    #[test]
442
    fn consensus() {
443
        let now = SystemTime::get();
444
        let one_hour = Duration::new(3600, 0);
445

            
446
        let mut builder = crate::doc::netstatus::MdConsensus::builder();
447
        builder
448
            .lifetime(Lifetime::new(now, now + one_hour, now + 2 * one_hour).unwrap())
449
            .add_client_version("0.4.5.8".into())
450
            .add_relay_version("0.4.5.9".into())
451
            .required_client_protos("DirCache=2 LinkAuth=3".parse().unwrap())
452
            .required_relay_protos("DirCache=1".parse().unwrap())
453
            .recommended_client_protos("DirCache=6".parse().unwrap())
454
            .recommended_relay_protos("DirCache=5".parse().unwrap())
455
            .param("wombat", 7)
456
            .param("knish", 1212)
457
            .voting_delay(7, 8)
458
            .consensus_method(32)
459
            .shared_rand_prev(1, SharedRandVal([b'x'; 32]), None)
460
            .shared_rand_cur(1, SharedRandVal([b'y'; 32]), None)
461
            .weight("Wxy", 303)
462
            .weight("Wow", 999);
463

            
464
        builder
465
            .voter()
466
            .nickname("Fuzzy".into())
467
            .identity([15; 20].into())
468
            .ip("10.0.0.200".parse().unwrap())
469
            .contact("admin@fuzzy.example.com".into())
470
            .vote_digest((*b"1234").into())
471
            .or_port(9001)
472
            .dir_port(9101)
473
            .build(&mut builder)
474
            .unwrap();
475

            
476
        builder
477
            .rs()
478
            .nickname("Fred".into())
479
            .identity([155; 20].into())
480
            .add_or_port(SocketAddr::from(([10, 0, 0, 60], 9100)))
481
            .add_or_port("[f00f::1]:9200".parse().unwrap())
482
            .doc_digest([99; 32])
483
            .set_flags(RelayFlag::Fast)
484
            .add_flags(RelayFlag::Stable | RelayFlag::V2Dir)
485
            .version("Arti 0.0.0".into())
486
            .protos("DirCache=7".parse().unwrap())
487
            .build_into(&mut builder)
488
            .unwrap();
489

            
490
        let _cons = builder.testing_consensus().unwrap();
491

            
492
        // TODO: Check actual members of `cons` above.
493
    }
494
}