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
//! Receive-owned evidence for structural walks, never a source of object content.
use crate::{IdMap, Object, ObjectId, ObjectKind, ReadLimits};
/// Kind, size and structure of every identity-checked received object.
///
/// Commit and tag edges stay per object for ancestry and connectivity walks. Tree entries are the
/// bulk of a history's edges (62.9M on git/git, against 409k objects), so import records only the
/// kind each tree expects of each distinct target. Connectivity then proves every received tree
/// at once from that table instead of walking tree edges again.
#[derive(Debug, Default)]
pub(super) struct VerifiedObjects {
pub objects: IdMap<VerifiedObject>,
trees: TreeTargets,
}
/// Entry targets named by received trees, with the kind each entry's mode requires.
#[derive(Debug, Default)]
struct TreeTargets {
/// Targets are names from received trees, unverified until connectivity, so a peer chooses
/// them.
kinds: crate::PeerIdMap<ObjectKind>,
/// Typed tree entries, including repeats, for connectivity edge budgets.
edges: usize,
/// A received tree failed to parse or named one target with two kinds. Only a walk that
/// reaches that tree can decide the outcome.
unproven: bool,
}
/// Received trees whose entries all parsed with consistent target kinds.
pub(super) struct TreeProof<'a> {
/// Each distinct target with the kind every naming tree requires.
pub targets: &'a crate::PeerIdMap<ObjectKind>,
/// Typed entries across all received trees.
pub edges: usize,
}
impl VerifiedObjects {
/// Records an identity-checked object's kind, size and structure.
pub fn insert(&mut self, id: ObjectId, object: &Object) {
self.record(id, Described::new(id, object));
}
/// Records a description computed by [`Described::new`], possibly on another thread.
pub fn record(&mut self, id: ObjectId, described: Described) {
let Described {
kind,
bytes,
parsed,
} = described;
let structure = match parsed {
Parsed::Edges(edges) => Structure::Edges(edges),
Parsed::Tree(entries) => {
self.trees.edges += entries.len();
for (target, kind) in entries {
if *self.trees.kinds.entry(target).or_insert(kind) != kind {
self.trees.unproven = true;
}
}
Structure::Tree
}
Parsed::Malformed => {
if kind == ObjectKind::Tree {
self.trees.unproven = true;
}
Structure::Malformed
}
};
self.objects.insert(
id,
VerifiedObject {
kind,
bytes,
structure,
read: ReadLimits {
max_object_bytes: bytes,
max_delta_bytes: 0,
max_delta_depth: 0,
max_decode_bytes: bytes,
max_input_bytes: 0,
},
},
);
}
/// Received objects, their payload bytes and their typed edges, for walk budgets.
pub fn totals(&self) -> (usize, usize, usize) {
let bytes = self.objects.values().map(|object| object.bytes).sum();
let edges = self
.objects
.values()
.filter_map(VerifiedObject::edges)
.map(<[_]>::len)
.sum::<usize>();
(self.objects.len(), bytes, edges + self.trees.edges)
}
/// The received trees' entry targets, when every received tree parsed consistently.
pub fn tree_proof(&self) -> Option<TreeProof<'_>> {
(!self.trees.unproven).then_some(TreeProof {
targets: &self.trees.kinds,
edges: self.trees.edges,
})
}
}
/// An identity-checked object's kind, size and parsed edges, before it joins [`VerifiedObjects`].
pub(super) struct Described {
kind: ObjectKind,
bytes: usize,
parsed: Parsed,
}
enum Parsed {
Edges(Vec<(ObjectId, ObjectKind)>),
Tree(Vec<(ObjectId, ObjectKind)>),
Malformed,
}
impl Described {
/// Parses `object`'s typed edges; needs no shared state, so it can run on any thread.
pub fn new(id: ObjectId, object: &Object) -> Self {
let mut edges = Vec::new();
let parsed = crate::edges::visit::<super::FetchError>(
id,
object.kind(),
object.data(),
|id, kind| {
edges.push((id, kind));
Ok(())
},
);
Self {
kind: object.kind(),
bytes: object.data().len(),
parsed: match parsed {
Err(_) => Parsed::Malformed,
Ok(()) if object.kind() == ObjectKind::Tree => Parsed::Tree(edges),
Ok(()) => Parsed::Edges(edges),
},
}
}
}
#[cfg(test)]
impl VerifiedObjects {
pub fn len(&self) -> usize {
self.objects.len()
}
pub fn contains(&self, id: ObjectId) -> bool {
self.objects.contains_key(&id)
}
/// Typed edges kept after import for objects of `kind`.
pub fn retained_edges(&self, kind: ObjectKind) -> usize {
self.objects
.values()
.filter(|object| object.kind == kind)
.filter_map(VerifiedObject::edges)
.map(<[_]>::len)
.sum()
}
}
#[derive(Debug)]
pub(super) struct VerifiedObject {
pub kind: ObjectKind,
pub bytes: usize,
structure: Structure,
pub read: ReadLimits,
}
#[derive(Debug)]
enum Structure {
/// Typed edges of a commit or tag; blobs have none.
Edges(Vec<(ObjectId, ObjectKind)>),
/// A tree whose entries [`VerifiedObjects::tree_proof`] covers.
Tree,
/// Unselected malformed server extras need not parse during import.
Malformed,
}
impl VerifiedObject {
/// Typed edges of a received commit, tag or blob. Trees are proven collectively instead.
pub fn edges(&self) -> Option<&[(ObjectId, ObjectKind)]> {
match &self.structure {
Structure::Edges(edges) => Some(edges),
Structure::Tree | Structure::Malformed => None,
}
}
/// Whether this is a received tree covered by [`VerifiedObjects::tree_proof`].
pub fn is_proven_tree(&self) -> bool {
matches!(self.structure, Structure::Tree)
}
/// Certify the uncached representation's bounds. Smaller limits use the ordinary reader,
/// which preserves its delta-base cache behavior and exact errors.
pub fn fits(&self, limits: ReadLimits) -> bool {
self.edges().is_some() && self.within(limits)
}
/// Whether reading the uncached representation stays within `limits`.
pub fn within(&self, limits: ReadLimits) -> bool {
self.read.max_object_bytes <= limits.max_object_bytes
&& self.read.max_delta_bytes <= limits.max_delta_bytes
&& self.read.max_delta_depth <= limits.max_delta_depth
&& self.read.max_decode_bytes <= limits.max_decode_bytes
&& self.read.max_input_bytes <= limits.max_input_bytes
}
}
/// Proof lives only for one pinned destination snapshot and one receive result. Nothing here
/// returns payloads or grants trust to a different representation of the same identity.
pub(super) struct InstalledObjects<'a> {
store: &'a crate::Objects,
verified: &'a VerifiedObjects,
pack: Option<&'a crate::pack::FilePack>,
}
impl<'a> InstalledObjects<'a> {
pub fn store(&self) -> &'a crate::Objects {
self.store
}
pub fn bind(
store: &'a crate::Objects,
received: &'a super::ReceivedFetch,
cancel: &std::sync::atomic::AtomicBool,
) -> Result<Self, super::FetchError> {
let mut selected = None;
if !received.verified.objects.is_empty() {
for pack in store.transfer_packs() {
if pack
.matches_received(
&received.index,
received.pack.reader()?,
received.pack.len(),
cancel,
)
.map_err(super::FetchError::Destination)?
{
selected = Some(pack);
break;
}
}
}
Ok(Self {
store,
verified: &received.verified,
pack: selected,
})
}
/// Whether normal lookup selects the validated pack for every received object.
///
/// Then every received object reads as the bytes import identity-checked, so import's
/// connectivity proof holds for the installed graph. Objects whose recorded representation
/// costs exceed `read` need the ordinary reader's exact limit checks, so they also answer
/// `false`.
pub fn selects_every_received(
&self,
read: ReadLimits,
cancel: &std::sync::atomic::AtomicBool,
) -> Result<bool, crate::ObjectReadError> {
let Some(pack) = self.pack else {
return Ok(false);
};
for (&id, object) in &self.verified.objects {
if !object.within(read) || !self.store.selects_received(id, pack, cancel)? {
return Ok(false);
}
}
Ok(true)
}
pub fn verified(&self) -> &'a VerifiedObjects {
self.verified
}
pub fn get(
&self,
id: ObjectId,
read: ReadLimits,
cancel: &std::sync::atomic::AtomicBool,
) -> Result<Option<&VerifiedObject>, crate::ObjectReadError> {
let Some(object) = self.verified.objects.get(&id).filter(|o| o.fits(read)) else {
return Ok(None);
};
let Some(pack) = self.pack else {
return Ok(None);
};
let selected = self.store.selects_received(id, pack, cancel)?;
Ok(selected.then_some(object))
}
}