-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathcurrent_unordered_operations.rs
More file actions
394 lines (353 loc) · 16.2 KB
/
current_unordered_operations.rs
File metadata and controls
394 lines (353 loc) · 16.2 KB
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
#![no_main]
use arbitrary::Arbitrary;
use commonware_cryptography::{sha256::Digest, Hasher, Sha256};
use commonware_parallel::Sequential;
use commonware_runtime::{buffer::paged::CacheRef, deterministic, Runner};
use commonware_storage::{
journal::contiguous::fixed::Config as FConfig,
merkle::{full::Config as MerkleConfig, mmb, mmr, Graftable, Location},
qmdb::current::{unordered::fixed::Db as CurrentDb, FixedConfig as Config},
translator::TwoCap,
};
use commonware_utils::{sequence::FixedBytes, NZUsize, NZU16, NZU64};
use libfuzzer_sys::fuzz_target;
use std::{
collections::HashMap,
num::{NonZeroU16, NonZeroU64},
};
type Key = FixedBytes<32>;
type Value = FixedBytes<32>;
type RawKey = [u8; 32];
type RawValue = [u8; 32];
type Db<F> = CurrentDb<F, deterministic::Context, Key, Value, Sha256, TwoCap, 32>;
#[derive(Arbitrary, Debug, Clone)]
enum CurrentOperation {
Update {
key: RawKey,
value: RawValue,
},
Delete {
key: RawKey,
},
Get {
key: RawKey,
},
Commit,
Prune,
OpCount,
Root,
RangeProof {
start_loc: u64,
max_ops: NonZeroU64,
},
KeyValueProof {
key: RawKey,
},
ArbitraryProof {
start_loc: u64,
bad_digests: Vec<[u8; 32]>,
max_ops: NonZeroU64,
bad_chunks: Vec<[u8; 32]>,
bad_prefix_peaks: Vec<[u8; 32]>,
bad_suffix_peaks: Vec<[u8; 32]>,
},
}
const MAX_OPERATIONS: usize = 100;
#[derive(Debug)]
struct FuzzInput {
operations: Vec<CurrentOperation>,
}
impl<'a> Arbitrary<'a> for FuzzInput {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let num_ops = u.int_in_range(1..=MAX_OPERATIONS)?;
let operations = (0..num_ops)
.map(|_| CurrentOperation::arbitrary(u))
.collect::<Result<Vec<_>, _>>()?;
Ok(FuzzInput { operations })
}
}
const PAGE_SIZE: NonZeroU16 = NZU16!(88);
const PAGE_CACHE_SIZE: usize = 8;
const MERKLE_ITEMS_PER_BLOB: u64 = 11;
const LOG_ITEMS_PER_BLOB: u64 = 7;
const WRITE_BUFFER_SIZE: usize = 1024;
async fn commit_pending<F: Graftable>(
db: &mut Db<F>,
pending_writes: &mut Vec<(Key, Option<Value>)>,
committed_state: &mut HashMap<RawKey, Option<RawValue>>,
pending_expected: &mut HashMap<RawKey, Option<RawValue>>,
) {
let mut batch = db.new_batch();
for (k, v) in pending_writes.drain(..) {
batch = batch.write(k, v);
}
let merkleized = batch.merkleize(db, None).await.unwrap();
db.apply_batch(merkleized)
.await
.expect("commit should not fail");
db.commit().await.expect("commit fsync should not fail");
committed_state.extend(pending_expected.drain());
}
fn fuzz_family<F: Graftable>(data: &FuzzInput, suffix: &str) {
let runner = deterministic::Runner::default();
let suffix = suffix.to_string();
let operations = data.operations.clone();
runner.start(|context| async move {
let mut hasher = Sha256::new();
let page_cache = CacheRef::from_pooler(
&context,
PAGE_SIZE,
NZUsize!(PAGE_CACHE_SIZE),
);
let cfg = Config {
merkle_config: MerkleConfig {
journal_partition: format!("fuzz-current-{suffix}-merkle-journal"),
metadata_partition: format!("fuzz-current-{suffix}-merkle-metadata"),
items_per_blob: NZU64!(MERKLE_ITEMS_PER_BLOB),
write_buffer: NZUsize!(WRITE_BUFFER_SIZE),
strategy: Sequential,
page_cache: page_cache.clone(),
},
journal_config: FConfig {
partition: format!("fuzz-current-{suffix}-log-journal"),
items_per_blob: NZU64!(LOG_ITEMS_PER_BLOB),
write_buffer: NZUsize!(WRITE_BUFFER_SIZE),
page_cache,
},
grafted_metadata_partition: format!("fuzz-current-{suffix}-grafted-merkle-metadata"),
translator: TwoCap,
};
let mut db: Db<F> = Db::init(context.clone(), cfg)
.await
.expect("Failed to initialize Current database");
// committed_state tracks state after apply_batch. pending_expected tracks
// uncommitted mutations that haven't been applied yet.
let mut committed_state: HashMap<RawKey, Option<RawValue>> = HashMap::new();
let mut pending_expected: HashMap<RawKey, Option<RawValue>> = HashMap::new();
let mut all_keys = std::collections::HashSet::new();
let mut pending_writes: Vec<(Key, Option<Value>)> = Vec::new();
let mut committed_op_count = Location::<F>::new(1);
for op in &operations {
match op {
CurrentOperation::Update { key, value } => {
let k = Key::new(*key);
let v = Value::new(*value);
pending_writes.push((k, Some(v)));
pending_expected.insert(*key, Some(*value));
all_keys.insert(*key);
}
CurrentOperation::Delete { key } => {
let k = Key::new(*key);
// Check if key exists in committed state or pending writes.
let key_existed = db.get(&k).await.expect("Get before delete should not fail").is_some()
|| pending_expected.get(key).is_some_and(|v| v.is_some());
if key_existed {
pending_writes.push((k, None));
pending_expected.insert(*key, None);
all_keys.insert(*key);
}
}
CurrentOperation::Get { key } => {
let k = Key::new(*key);
let result = db.get(&k).await.expect("Get should not fail");
// Verify against committed state only.
match committed_state.get(key) {
Some(Some(expected_value)) => {
assert!(result.is_some(), "Expected value for key {key:?}");
let actual_value = result.expect("Should have value");
let actual_bytes: &[u8; 32] = actual_value.as_ref().try_into().expect("Value should be 32 bytes");
assert_eq!(actual_bytes, expected_value, "Value mismatch for key {key:?}");
}
Some(None) => {
assert!(result.is_none(), "Expected no value for deleted key {key:?}");
}
None => {
assert!(result.is_none(), "Expected no value for unset key {key:?}");
}
}
all_keys.insert(*key);
}
CurrentOperation::OpCount => {
let actual = db.bounds().await.end;
assert_eq!(
actual, committed_op_count,
"Op count mismatch: expected {committed_op_count}, got {actual}"
);
}
CurrentOperation::Commit => {
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
committed_op_count = db.bounds().await.end;
}
CurrentOperation::Prune => {
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
committed_op_count = db.bounds().await.end;
db.prune(db.sync_boundary()).await.expect("Prune should not fail");
}
CurrentOperation::Root => {
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
committed_op_count = db.bounds().await.end;
let _root = db.root();
}
CurrentOperation::RangeProof { start_loc, max_ops } => {
let current_op_count = db.bounds().await.end;
if current_op_count == 0 {
continue;
}
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
committed_op_count = db.bounds().await.end;
let current_root = db.root();
let current_op_count = db.bounds().await.end;
let start_loc = Location::<F>::new(start_loc % *current_op_count);
let oldest_loc = db.sync_boundary();
if start_loc >= oldest_loc {
let (proof, ops, chunks) = db
.range_proof(&mut hasher, start_loc, *max_ops)
.await
.expect("Range proof should not fail");
assert!(
Db::<F>::verify_range_proof(
&mut hasher,
&proof,
start_loc,
&ops,
&chunks,
¤t_root
),
"Range proof verification failed for start_loc={start_loc}, max_ops={max_ops}"
);
}
}
CurrentOperation::ArbitraryProof {
start_loc,
bad_digests,
max_ops,
bad_chunks,
bad_prefix_peaks,
bad_suffix_peaks,
} => {
let current_op_count = db.bounds().await.end;
if current_op_count == 0 {
continue;
}
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
committed_op_count = db.bounds().await.end;
let current_op_count = db.bounds().await.end;
let start_loc = Location::<F>::new(start_loc % current_op_count.as_u64());
let root = db.root();
if let Ok((range_proof, ops, chunks)) = db
.range_proof(&mut hasher, start_loc, *max_ops)
.await {
// Try to verify the proof when providing bad proof digests.
let bad_digests = bad_digests.iter().map(|d| Digest::from(*d)).collect();
if range_proof.proof.digests != bad_digests {
let mut bad_proof = range_proof.clone();
bad_proof.proof.digests = bad_digests;
assert!(!Db::<F>::verify_range_proof(
&mut hasher,
&bad_proof,
start_loc,
&ops,
&chunks,
&root
), "proof with bad digests should not verify");
}
// Try to verify the proof when providing bad input chunks.
if &chunks != bad_chunks {
assert!(!Db::<F>::verify_range_proof(
&mut hasher,
&range_proof,
start_loc,
&ops,
bad_chunks,
&root
), "proof with bad chunks should not verify");
}
let bad_prefix_peaks =
bad_prefix_peaks.iter().map(|d| Digest::from(*d)).collect();
if range_proof.unfolded_prefix_peaks != bad_prefix_peaks {
let mut bad_proof = range_proof.clone();
bad_proof.unfolded_prefix_peaks = bad_prefix_peaks;
assert!(!Db::<F>::verify_range_proof(
&mut hasher,
&bad_proof,
start_loc,
&ops,
&chunks,
&root
), "proof with bad prefix peaks should not verify");
}
let bad_suffix_peaks =
bad_suffix_peaks.iter().map(|d| Digest::from(*d)).collect();
if range_proof.unfolded_suffix_peaks != bad_suffix_peaks {
let mut bad_proof = range_proof.clone();
bad_proof.unfolded_suffix_peaks = bad_suffix_peaks;
assert!(!Db::<F>::verify_range_proof(
&mut hasher,
&bad_proof,
start_loc,
&ops,
&chunks,
&root
), "proof with bad suffix peaks should not verify");
}
}
}
CurrentOperation::KeyValueProof { key } => {
let k = Key::new(*key);
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
committed_op_count = db.bounds().await.end;
let current_root = db.root();
match db.key_value_proof(&mut hasher, k.clone()).await {
Ok(proof) => {
let value = db.get(&k).await.expect("get should not fail").expect("key should exist");
let verification_result = Db::<F>::verify_key_value_proof(
&mut hasher,
k,
value,
&proof,
¤t_root,
);
assert!(verification_result, "Key value proof verification failed for key {key:?}");
}
Err(commonware_storage::qmdb::Error::KeyNotFound) => {
let expected_value = committed_state.get(key);
if let Some(Some(_)) = expected_value {
panic!("Key {key:?} should exist but proof generation failed");
}
}
Err(e) => {
panic!("Unexpected error during key value proof generation: {e:?}");
}
}
}
}
}
// Final commit to ensure all pending operations are persisted.
if !pending_writes.is_empty() {
commit_pending(&mut db, &mut pending_writes, &mut committed_state, &mut pending_expected).await;
}
for key in &all_keys {
let k = Key::new(*key);
let result = db.get(&k).await.expect("Final get should not fail");
match committed_state.get(key) {
Some(Some(expected_value)) => {
assert!(result.is_some(), "Lost value for key {key:?} at end");
let actual_value = result.expect("Should have value");
let actual_bytes: &[u8; 32] = actual_value.as_ref().try_into().expect("Value should be 32 bytes");
assert_eq!(actual_bytes, expected_value, "Final value mismatch for key {key:?}");
}
Some(None) => {
assert!(result.is_none(), "Deleted key {key:?} should remain deleted");
}
None => {
assert!(result.is_none(), "Unset key {key:?} should not exist");
}
}
}
db.destroy().await.expect("destroy should not fail");
});
}
fuzz_target!(|input: FuzzInput| {
fuzz_family::<mmr::Family>(&input, "mmr");
fuzz_family::<mmb::Family>(&input, "mmb");
});