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
|
//! Declare a general purpose "document ID type" for tracking which
//! documents we want and which we have.
use std::{borrow::Borrow, collections::HashMap};
use tor_dirclient::request;
use tor_netdoc::doc::{
authcert::AuthCertKeyIds, microdesc::MdDigest, netstatus::ConsensusFlavor, routerdesc::RdDigest,
};
/// The identity of a single document, in enough detail to load it
/// from storage.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[non_exhaustive]
pub enum DocId {
/// A request for the most recent consensus document.
LatestConsensus {
/// The flavor of consensus to request.
flavor: ConsensusFlavor,
/// Rules for loading this consensus from the cache.
cache_usage: CacheUsage,
},
/// A request for an authority certificate, by the SHA1 digests of
/// its identity key and signing key.
AuthCert(AuthCertKeyIds),
/// A request for a single microdescriptor, by SHA256 digest.
Microdesc(MdDigest),
/// A request for the router descriptor of a public relay, by SHA1
/// digest.
RouterDesc(RdDigest),
}
/// The underlying type of a DocId.
///
/// Documents with the same type can be grouped into the same query; others
/// cannot.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[non_exhaustive]
pub(crate) enum DocType {
/// A consensus document
Consensus(ConsensusFlavor),
/// An authority certificate
AuthCert,
/// A microdescriptor
Microdesc,
/// A router descriptor.
RouterDesc,
}
impl DocId {
/// Return the associated doctype of this DocId.
pub(crate) fn doctype(&self) -> DocType {
use DocId::*;
use DocType as T;
match self {
LatestConsensus { flavor: f, .. } => T::Consensus(*f),
AuthCert(_) => T::AuthCert,
Microdesc(_) => T::Microdesc,
RouterDesc(_) => T::RouterDesc,
}
}
}
/// A request for a specific kind of directory resource that a DirMgr can
/// request.
#[derive(Clone, Debug)]
pub(crate) enum ClientRequest {
/// Request for a consensus
Consensus(request::ConsensusRequest),
/// Request for one or more authority certificates
AuthCert(request::AuthCertRequest),
/// Request for one or more microdescriptors
Microdescs(request::MicrodescRequest),
/// Request for one or more router descriptors
RouterDescs(request::RouterDescRequest),
}
impl ClientRequest {
/// Turn a ClientRequest into a Requestable.
pub(crate) fn as_requestable(&self) -> &(dyn request::Requestable + Send + Sync) {
use ClientRequest::*;
match self {
Consensus(a) => a,
AuthCert(a) => a,
Microdescs(a) => a,
RouterDescs(a) => a,
}
}
}
/// Description of how to start out a given bootstrap attempt.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum CacheUsage {
/// The bootstrap attempt will only use the cache. Therefore, don't
/// load a pending consensus from the cache, since we won't be able
/// to find enough information to make it usable.
CacheOnly,
/// The bootstrap attempt is willing to download information or to
/// use the cache. Therefore, we want the latest cached
/// consensus, whether it is pending or not.
CacheOkay,
/// The bootstrap attempt is trying to fetch a new consensus. Therefore,
/// we don't want a consensus from the cache.
MustDownload,
}
impl CacheUsage {
/// Turn this CacheUsage into a pending field for use with
/// SqliteStorage.
pub(crate) fn pending_requirement(&self) -> Option<bool> {
match self {
CacheUsage::CacheOnly => Some(false),
_ => None,
}
}
}
/// A group of DocIds that can be downloaded or loaded from the database
/// together.
///
/// TODO: Perhaps this should be the same as ClientRequest?
#[derive(Clone, Debug)]
pub(crate) enum DocQuery {
/// A request for the lastet consensus
LatestConsensus {
/// A desired flavor of consenus
flavor: ConsensusFlavor,
/// Whether we can or must use the cache
cache_usage: CacheUsage,
},
/// A request for authority certificates
AuthCert(Vec<AuthCertKeyIds>),
/// A request for microdescriptors
Microdesc(Vec<MdDigest>),
/// A request for router descriptors
RouterDesc(Vec<RdDigest>),
}
impl DocQuery {
/// Construct an "empty" docquery from the given DocId
pub(crate) fn empty_from_docid(id: &DocId) -> Self {
match *id {
DocId::LatestConsensus {
flavor,
cache_usage,
} => Self::LatestConsensus {
flavor,
cache_usage,
},
DocId::AuthCert(_) => Self::AuthCert(Vec::new()),
DocId::Microdesc(_) => Self::Microdesc(Vec::new()),
DocId::RouterDesc(_) => Self::RouterDesc(Vec::new()),
}
}
/// Add `id` to this query, if possible.
fn push(&mut self, id: DocId) {
match (self, id) {
(Self::LatestConsensus { .. }, DocId::LatestConsensus { .. }) => {}
(Self::AuthCert(ids), DocId::AuthCert(id)) => ids.push(id),
(Self::Microdesc(ids), DocId::Microdesc(id)) => ids.push(id),
(Self::RouterDesc(ids), DocId::RouterDesc(id)) => ids.push(id),
(_, _) => panic!(),
}
}
/// If this query contains too many documents to download with a single
/// request, divide it up.
pub(crate) fn split_for_download(self) -> Vec<Self> {
use DocQuery::*;
/// How many objects can be put in a single HTTP GET line?
const N: usize = 500;
match self {
LatestConsensus { .. } => vec![self],
AuthCert(mut v) => {
v.sort_unstable();
v[..].chunks(N).map(|s| AuthCert(s.to_vec())).collect()
}
Microdesc(mut v) => {
v.sort_unstable();
v[..].chunks(N).map(|s| Microdesc(s.to_vec())).collect()
}
RouterDesc(mut v) => {
v.sort_unstable();
v[..].chunks(N).map(|s| RouterDesc(s.to_vec())).collect()
}
}
}
}
impl From<DocId> for DocQuery {
fn from(d: DocId) -> DocQuery {
let mut result = DocQuery::empty_from_docid(&d);
result.push(d);
result
}
}
/// Given a list of DocId, split them up into queries, by type.
pub(crate) fn partition_by_type<T>(collection: T) -> HashMap<DocType, DocQuery>
where
T: IntoIterator<Item = DocId>,
{
let mut result = HashMap::new();
for item in collection.into_iter() {
let b = item.borrow();
let tp = b.doctype();
result
.entry(tp)
.or_insert_with(|| DocQuery::empty_from_docid(b))
.push(item);
}
result
}
|