-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdelta_buffer.rs
More file actions
502 lines (438 loc) · 16.8 KB
/
delta_buffer.rs
File metadata and controls
502 lines (438 loc) · 16.8 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Delta buffering for sync scenarios (Invariant I6).
//!
//! When a snapshot sync is in progress, incoming deltas are buffered so they
//! can be replayed after the snapshot completes. This ensures that:
//! 1. Deltas arriving during sync aren't lost (Invariant I6 - Liveness Guarantee)
//! 2. Event handlers can execute for buffered deltas after context is initialized
//!
//! ## Delivery Contract
//!
//! - **Buffer size**: Configurable, default 10,000 deltas per context
//! - **Drop policy**: Oldest-first when buffer full (with metric increment)
//! - **Backpressure**: None (fire-and-forget from network layer)
//! - **Metrics**: `drops` counter MUST be observable
//!
//! ## Minimum Capacity Warning
//!
//! If buffer capacity is set below `MIN_RECOMMENDED_CAPACITY`, a warning should
//! be logged at startup. Zero capacity is valid but will drop ALL deltas.
use std::collections::HashSet;
use calimero_crypto::Nonce;
use calimero_primitives::hash::Hash;
use calimero_primitives::identity::PublicKey;
/// Default buffer capacity (10,000 deltas per context).
pub const DEFAULT_BUFFER_CAPACITY: usize = 10_000;
/// Minimum recommended buffer capacity.
///
/// Capacities below this value may cause excessive delta loss under normal load.
/// A warning should be logged if capacity is set below this threshold.
pub const MIN_RECOMMENDED_CAPACITY: usize = 100;
/// Result of pushing a delta to the buffer.
///
/// Provides clear semantics about what happened to both the incoming delta
/// and any evicted delta.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PushResult {
/// Delta was added to the buffer without eviction.
Added,
/// Delta was a duplicate (already in buffer) - no action taken.
Duplicate,
/// Delta was added, but oldest delta was evicted due to capacity.
/// Contains the ID of the evicted delta.
Evicted([u8; 32]),
/// Delta was dropped immediately (zero capacity buffer).
/// Contains the ID of the dropped delta.
DroppedZeroCapacity([u8; 32]),
}
impl PushResult {
/// Returns true if the delta was successfully added to the buffer.
#[must_use]
pub fn was_added(&self) -> bool {
matches!(self, Self::Added | Self::Evicted(_))
}
/// Returns true if any delta was lost (evicted or dropped).
#[must_use]
pub fn had_data_loss(&self) -> bool {
matches!(self, Self::Evicted(_) | Self::DroppedZeroCapacity(_))
}
/// Returns the ID of the lost delta, if any.
#[must_use]
pub fn lost_delta_id(&self) -> Option<[u8; 32]> {
match self {
Self::Evicted(id) | Self::DroppedZeroCapacity(id) => Some(*id),
Self::Added | Self::Duplicate => None,
}
}
}
/// A single buffered delta.
///
/// Contains ALL fields needed for replay after snapshot sync completes.
/// Previously missing fields (nonce, author_id, root_hash, events) caused
/// data loss because deltas couldn't be decrypted or processed.
///
/// **POC Bug 7**: This struct MUST include all fields for replay - not just
/// `id`, `parents`, `hlc`, `payload`, but also `nonce`, `author_id`, `root_hash`,
/// `events`, and `source_peer`.
#[derive(Debug, Clone)]
pub struct BufferedDelta {
/// Delta ID.
pub id: [u8; 32],
/// Parent IDs.
pub parents: Vec<[u8; 32]>,
/// HLC timestamp.
pub hlc: u64,
/// Serialized (encrypted) payload.
pub payload: Vec<u8>,
/// Nonce for decryption (12 bytes for XChaCha20-Poly1305).
pub nonce: Nonce,
/// Author public key (needed to get sender key for decryption).
pub author_id: PublicKey,
/// Expected root hash after applying this delta.
pub root_hash: Hash,
/// Optional serialized events (for handler execution after replay).
pub events: Option<Vec<u8>>,
/// Source peer ID (for requesting sender key if needed).
pub source_peer: libp2p::PeerId,
/// Group key identifier for decryption.
pub key_id: [u8; 32],
}
/// Buffer for storing deltas during snapshot sync.
///
/// Implements Invariant I6: Deltas received during state-based sync MUST be
/// preserved and applied after sync completes.
///
/// When the buffer is full, the oldest delta is evicted (FIFO eviction policy)
/// and the `drops` counter is incremented. Drops MUST be observable via metrics.
///
/// ## Deduplication
///
/// The buffer tracks seen delta IDs to prevent duplicate deltas from being buffered.
/// This protects against replay attacks where an adversary might flood the buffer
/// with duplicate deltas to cause eviction of legitimate deltas.
#[derive(Debug)]
pub struct DeltaBuffer {
/// Buffered deltas (FIFO queue - oldest at front).
deltas: std::collections::VecDeque<BufferedDelta>,
/// Set of delta IDs currently in the buffer (for O(1) deduplication).
seen_ids: HashSet<[u8; 32]>,
/// HLC timestamp when buffering started.
sync_start_hlc: u64,
/// Maximum buffer size before eviction.
capacity: usize,
/// Number of deltas dropped due to buffer overflow (observable metric).
drops: u64,
}
impl DeltaBuffer {
/// Create a new delta buffer with specified capacity.
///
/// # Capacity Warning
///
/// If capacity is below `MIN_RECOMMENDED_CAPACITY`, callers should log a
/// warning at startup. Zero capacity is valid but will drop ALL deltas.
#[must_use]
pub fn new(capacity: usize, sync_start_hlc: u64) -> Self {
Self {
deltas: std::collections::VecDeque::with_capacity(capacity.min(1000)),
seen_ids: HashSet::with_capacity(capacity.min(1000)),
sync_start_hlc,
capacity,
drops: 0,
}
}
/// Check if capacity is below recommended minimum.
///
/// Callers should log a warning at session start if this returns true.
#[must_use]
pub fn is_capacity_below_recommended(&self) -> bool {
self.capacity < MIN_RECOMMENDED_CAPACITY
}
/// Add a delta to the buffer.
///
/// Returns a `PushResult` indicating what happened:
/// - `Added`: Delta was added successfully
/// - `Duplicate`: Delta ID was already in buffer (no action taken)
/// - `Evicted(id)`: Delta was added but oldest delta was evicted
/// - `DroppedZeroCapacity(id)`: Delta was dropped (zero capacity buffer)
///
/// # Deduplication
///
/// If a delta with the same ID is already in the buffer, it is not added
/// again and `PushResult::Duplicate` is returned. This prevents replay attacks.
///
/// # Edge case: zero capacity
///
/// If capacity is 0, the incoming delta is immediately dropped (not added)
/// and `PushResult::DroppedZeroCapacity` is returned with the dropped delta's ID.
pub fn push(&mut self, delta: BufferedDelta) -> PushResult {
let delta_id = delta.id;
// Handle zero capacity: drop incoming delta immediately
if self.capacity == 0 {
self.drops += 1;
return PushResult::DroppedZeroCapacity(delta_id);
}
// Deduplication check (#2: prevents replay attacks)
if self.seen_ids.contains(&delta_id) {
return PushResult::Duplicate;
}
if self.deltas.len() >= self.capacity {
// Evict oldest delta (front of queue)
if let Some(evicted) = self.deltas.pop_front() {
self.seen_ids.remove(&evicted.id);
let evicted_id = evicted.id;
self.drops += 1;
self.seen_ids.insert(delta_id);
self.deltas.push_back(delta);
PushResult::Evicted(evicted_id)
} else {
// This shouldn't happen, but handle gracefully
self.seen_ids.insert(delta_id);
self.deltas.push_back(delta);
PushResult::Added
}
} else {
self.seen_ids.insert(delta_id);
self.deltas.push_back(delta);
PushResult::Added
}
}
/// Get all buffered deltas for replay, clearing the buffer.
///
/// Returns deltas in FIFO order (oldest first), preserving causality.
/// Also clears the deduplication set.
#[must_use]
pub fn drain(&mut self) -> Vec<BufferedDelta> {
self.seen_ids.clear();
self.deltas.drain(..).collect()
}
/// Check if a delta ID is already in the buffer.
///
/// This is O(1) due to the internal HashSet tracking.
#[must_use]
pub fn contains(&self, id: &[u8; 32]) -> bool {
self.seen_ids.contains(id)
}
/// Number of buffered deltas.
#[must_use]
pub fn len(&self) -> usize {
self.deltas.len()
}
/// Check if buffer is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.deltas.is_empty()
}
/// Get the sync start HLC.
#[must_use]
pub fn sync_start_hlc(&self) -> u64 {
self.sync_start_hlc
}
/// Get the number of deltas dropped due to buffer overflow.
///
/// This metric MUST be observable per Invariant I6 delivery contract.
#[must_use]
pub fn drops(&self) -> u64 {
self.drops
}
/// Get the buffer capacity.
#[must_use]
pub fn capacity(&self) -> usize {
self.capacity
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_test_delta(id: u8) -> BufferedDelta {
BufferedDelta {
id: [id; 32],
parents: vec![[0; 32]],
hlc: 12345,
payload: vec![1, 2, 3],
nonce: [0; 12],
author_id: PublicKey::from([0; 32]),
root_hash: Hash::from([0; 32]),
events: None,
source_peer: libp2p::PeerId::random(),
key_id: [0; 32],
}
}
#[test]
fn test_buffer_basic() {
let mut buffer = DeltaBuffer::new(100, 12345);
assert!(buffer.is_empty());
assert_eq!(buffer.sync_start_hlc(), 12345);
assert_eq!(buffer.capacity(), 100);
assert_eq!(buffer.drops(), 0);
assert!(!buffer.is_capacity_below_recommended());
let result = buffer.push(make_test_delta(1));
assert_eq!(result, PushResult::Added, "Should add without eviction");
assert!(result.was_added());
assert!(!result.had_data_loss());
assert_eq!(buffer.len(), 1);
let drained = buffer.drain();
assert_eq!(drained.len(), 1);
assert!(buffer.is_empty());
}
#[test]
fn test_buffer_only_during_sync() {
// Buffer should only accept deltas - caller decides when to buffer
let mut buffer = DeltaBuffer::new(10, 12345);
assert!(buffer.is_empty());
// Push deltas
assert_eq!(buffer.push(make_test_delta(1)), PushResult::Added);
assert_eq!(buffer.push(make_test_delta(2)), PushResult::Added);
assert_eq!(buffer.len(), 2);
// Drain returns all in FIFO order
let drained = buffer.drain();
assert_eq!(drained.len(), 2);
assert_eq!(drained[0].id[0], 1);
assert_eq!(drained[1].id[0], 2);
}
#[test]
fn test_buffer_overflow_drops_oldest() {
let mut buffer = DeltaBuffer::new(2, 0);
// Fill buffer
assert_eq!(buffer.push(make_test_delta(1)), PushResult::Added);
assert_eq!(buffer.push(make_test_delta(2)), PushResult::Added);
assert_eq!(buffer.drops(), 0);
// Third delta causes eviction of oldest (delta 1)
let result = buffer.push(make_test_delta(3));
assert_eq!(result, PushResult::Evicted([1; 32]), "Should evict delta 1");
assert!(result.had_data_loss());
assert_eq!(result.lost_delta_id(), Some([1; 32]));
assert_eq!(buffer.drops(), 1);
assert_eq!(buffer.len(), 2);
// Fourth delta causes another eviction (delta 2)
let result = buffer.push(make_test_delta(4));
assert_eq!(result, PushResult::Evicted([2; 32]), "Should evict delta 2");
assert_eq!(buffer.drops(), 2);
assert_eq!(buffer.len(), 2);
// Verify remaining deltas are 3 and 4 (FIFO order)
let drained = buffer.drain();
assert_eq!(drained.len(), 2);
assert_eq!(drained[0].id[0], 3);
assert_eq!(drained[1].id[0], 4);
}
#[test]
fn test_zero_capacity_drops_immediately() {
let mut buffer = DeltaBuffer::new(0, 0);
assert!(buffer.is_empty());
assert_eq!(buffer.capacity(), 0);
assert_eq!(buffer.drops(), 0);
assert!(buffer.is_capacity_below_recommended());
// First push should drop immediately
let result = buffer.push(make_test_delta(1));
assert_eq!(
result,
PushResult::DroppedZeroCapacity([1; 32]),
"Zero capacity should drop incoming delta"
);
assert!(result.had_data_loss());
assert!(!result.was_added());
assert_eq!(result.lost_delta_id(), Some([1; 32]));
assert_eq!(buffer.drops(), 1);
assert!(buffer.is_empty(), "Buffer should remain empty");
assert_eq!(buffer.len(), 0);
// Second push should also drop
let result = buffer.push(make_test_delta(2));
assert_eq!(result, PushResult::DroppedZeroCapacity([2; 32]));
assert_eq!(buffer.drops(), 2);
assert!(buffer.is_empty());
}
#[test]
fn test_finish_sync_returns_fifo() {
let mut buffer = DeltaBuffer::new(100, 0);
// Add deltas in order
buffer.push(make_test_delta(1));
buffer.push(make_test_delta(2));
buffer.push(make_test_delta(3));
// Drain should return in FIFO order
let drained = buffer.drain();
assert_eq!(drained.len(), 3);
assert_eq!(drained[0].id[0], 1);
assert_eq!(drained[1].id[0], 2);
assert_eq!(drained[2].id[0], 3);
}
#[test]
fn test_cancel_sync_clears_buffer() {
let mut buffer = DeltaBuffer::new(100, 0);
buffer.push(make_test_delta(1));
buffer.push(make_test_delta(2));
assert_eq!(buffer.len(), 2);
// Simulate cancel by draining and discarding
let _ = buffer.drain();
assert!(buffer.is_empty());
assert_eq!(buffer.len(), 0);
}
#[test]
fn test_drops_counter_observable() {
let mut buffer = DeltaBuffer::new(1, 0);
assert_eq!(buffer.drops(), 0);
buffer.push(make_test_delta(1));
assert_eq!(buffer.drops(), 0);
// Each overflow increments drops
buffer.push(make_test_delta(2));
assert_eq!(buffer.drops(), 1);
buffer.push(make_test_delta(3));
assert_eq!(buffer.drops(), 2);
buffer.push(make_test_delta(4));
assert_eq!(buffer.drops(), 3);
}
#[test]
fn test_deduplication_prevents_double_buffering() {
let mut buffer = DeltaBuffer::new(10, 0);
// Add a delta
assert_eq!(buffer.push(make_test_delta(1)), PushResult::Added);
assert_eq!(buffer.len(), 1);
// Try to add same delta again - should be duplicate
let result = buffer.push(make_test_delta(1));
assert_eq!(result, PushResult::Duplicate);
assert!(!result.had_data_loss());
assert!(!result.was_added()); // Duplicate counts as "not added"
assert_eq!(buffer.len(), 1); // Still only 1
// Add a different delta - should work
assert_eq!(buffer.push(make_test_delta(2)), PushResult::Added);
assert_eq!(buffer.len(), 2);
}
#[test]
fn test_deduplication_cleared_on_drain() {
let mut buffer = DeltaBuffer::new(10, 0);
// Add a delta
assert_eq!(buffer.push(make_test_delta(1)), PushResult::Added);
assert!(buffer.contains(&[1; 32]));
// Drain
let _ = buffer.drain();
assert!(!buffer.contains(&[1; 32]));
// Now can add same delta again
assert_eq!(buffer.push(make_test_delta(1)), PushResult::Added);
assert_eq!(buffer.len(), 1);
}
#[test]
fn test_deduplication_cleared_on_eviction() {
let mut buffer = DeltaBuffer::new(2, 0);
// Fill buffer
buffer.push(make_test_delta(1));
buffer.push(make_test_delta(2));
assert!(buffer.contains(&[1; 32]));
// Evict delta 1 by adding delta 3
buffer.push(make_test_delta(3));
assert!(!buffer.contains(&[1; 32])); // delta 1 evicted
assert!(buffer.contains(&[2; 32]));
assert!(buffer.contains(&[3; 32]));
// Can now add delta 1 again (it was evicted)
let result = buffer.push(make_test_delta(1));
assert_eq!(result, PushResult::Evicted([2; 32])); // delta 2 gets evicted
}
#[test]
fn test_capacity_below_recommended() {
// Below recommended
let buffer = DeltaBuffer::new(50, 0);
assert!(buffer.is_capacity_below_recommended());
// At recommended
let buffer = DeltaBuffer::new(MIN_RECOMMENDED_CAPACITY, 0);
assert!(!buffer.is_capacity_below_recommended());
// Above recommended
let buffer = DeltaBuffer::new(MIN_RECOMMENDED_CAPACITY + 1, 0);
assert!(!buffer.is_capacity_below_recommended());
}
}