kitsune2_dht 0.4.1

The DHT model for kitsune2
Documentation
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use super::*;
use crate::dht::tests::harness::SyncWithOutcome;
use crate::test::test_store;
use harness::DhtSyncHarness;
use kitsune2_api::{DhtArc, DynOpStore, UNIX_TIMESTAMP};
use kitsune2_core::factories::MemoryOp;
use std::time::Duration;

mod harness;

const SECTOR_SIZE: u32 = 1u32 << 23;

#[tokio::test]
async fn from_store_empty() {
    Dht::try_from_store(UNIX_TIMESTAMP, test_store().await)
        .await
        .unwrap();
}

#[tokio::test]
async fn take_minimal_snapshot() {
    let store = test_store().await;
    store
        .process_incoming_ops(vec![
            MemoryOp::new(UNIX_TIMESTAMP, vec![7; 32]).into(),
        ])
        .await
        .unwrap();

    let dht = Dht::try_from_store(Timestamp::now(), store.clone())
        .await
        .unwrap();

    let arc_set = ArcSet::new(vec![DhtArc::FULL]).unwrap();

    let snapshot = dht.snapshot_minimal(arc_set).await.unwrap();
    match snapshot {
        DhtSnapshot::Minimal {
            disc_boundary,
            disc_top_hash,
            ring_top_hashes,
        } => {
            assert_eq!(dht.partition.full_slice_end_timestamp(), disc_boundary);
            assert_eq!(bytes::Bytes::from(vec![7; 32]), disc_top_hash);
            assert!(!ring_top_hashes.is_empty());
        }
        s => panic!("Unexpected snapshot type: {s:?}"),
    }
}

#[tokio::test]
async fn cannot_take_minimal_snapshot_with_empty_arc_set() {
    let current_time = Timestamp::now();
    let dht1 = DhtSyncHarness::new(current_time, DhtArc::Empty).await;

    let err = dht1
        .dht
        .snapshot_minimal(ArcSet::new(vec![dht1.arc]).unwrap())
        .await
        .unwrap_err();
    assert_eq!("No arcs to snapshot (src: None)", err.to_string());
}

#[tokio::test]
async fn cannot_handle_snapshot_with_empty_arc_set() {
    let current_time = Timestamp::now();
    let dht1 = DhtSyncHarness::new(current_time, DhtArc::Empty).await;

    // Declare a full arc to get a snapshot
    let snapshot = dht1
        .dht
        .snapshot_minimal(ArcSet::new(vec![DhtArc::FULL]).unwrap())
        .await
        .unwrap();

    // Now try to compare that snapshot to ourselves with an empty arc set
    let err = dht1
        .dht
        .handle_snapshot(
            snapshot,
            None,
            ArcSet::new(vec![DhtArc::Empty]).unwrap(),
            1_000,
        )
        .await
        .unwrap_err();

    assert_eq!("No arcs to snapshot (src: None)", err.to_string());
}

#[tokio::test]
async fn empty_dht_is_in_sync_with_empty() {
    let current_time = Timestamp::now();
    let dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
    assert!(dht2.is_in_sync_with(&dht1).await.unwrap());
}

#[tokio::test]
async fn one_way_disc_sync_from_initiator() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put historical data in the first DHT
    let op = MemoryOp::new(UNIX_TIMESTAMP, vec![41; 32]);
    let op_id = op.compute_op_id();
    dht1.inject_ops(vec![op]).await.unwrap();

    // Try a sync
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedDisc));

    // We shouldn't learn about any ops
    assert!(dht1.discovered_ops.is_empty());

    // The other agent should have learned about the op
    assert_eq!(1, dht2.discovered_ops.len());
    assert_eq!(1, dht2.discovered_ops[&dht1.agent_id].len());
    assert_eq!(vec![op_id], dht2.discovered_ops[&dht1.agent_id]);

    assert!(!dht1.is_in_sync_with(&dht2).await.unwrap());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn one_way_disc_sync_from_acceptor() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put historical data in the second DHT
    dht2.inject_ops(vec![MemoryOp::new(UNIX_TIMESTAMP, vec![41; 32])])
        .await
        .unwrap();

    // Try a sync initiated by the first agent
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedDisc));

    // They shouldn't learn about any ops
    assert!(dht2.discovered_ops.is_empty());

    // We should have learned about the op
    assert_eq!(1, dht1.discovered_ops.len());
    assert_eq!(1, dht1.discovered_ops[&dht2.agent_id].len());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn two_way_disc_sync() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put historical data in both DHTs
    dht1.inject_ops(vec![MemoryOp::new(UNIX_TIMESTAMP, vec![7; 32])])
        .await
        .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        // Two weeks later
        UNIX_TIMESTAMP + Duration::from_secs(14 * 24 * 60 * 60),
        vec![43; 32],
    )])
    .await
    .unwrap();

    // Try a sync initiated by the first agent
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedDisc));

    // Each learns about one op
    assert_eq!(1, dht1.discovered_ops.len());
    assert_eq!(1, dht1.discovered_ops[&dht2.agent_id].len());
    assert_eq!(1, dht2.discovered_ops.len());
    assert_eq!(1, dht2.discovered_ops[&dht1.agent_id].len());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn one_way_ring_sync_from_initiator() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put recent data in the first ring of the first DHT
    dht1.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![41; 32],
    )])
    .await
    .unwrap();

    // Try a sync
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    // We shouldn't learn about any ops
    assert!(dht1.discovered_ops.is_empty());

    // The other agent should have learned about the op
    assert_eq!(1, dht2.discovered_ops.len());
    assert_eq!(1, dht2.discovered_ops[&dht1.agent_id].len());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn one_way_ring_sync_from_acceptor() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put recent data in the first ring of the second DHT
    dht2.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![41; 32],
    )])
    .await
    .unwrap();

    // Try a sync initiated by the first agent
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    // They shouldn't learn about any ops
    assert!(dht2.discovered_ops.is_empty());

    // We should have learned about the op
    assert_eq!(1, dht1.discovered_ops.len());
    assert_eq!(1, dht1.discovered_ops[&dht2.agent_id].len());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn two_way_ring_sync() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put recent data in the first ring of both DHTs
    dht1.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![7; 32],
    )])
    .await
    .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        // Two weeks later
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![43; 32],
    )])
    .await
    .unwrap();

    // Try a sync initiated by the first agent
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    // Each learns about one op
    assert_eq!(1, dht1.discovered_ops.len());
    assert_eq!(1, dht1.discovered_ops[&dht2.agent_id].len());
    assert_eq!(1, dht2.discovered_ops.len());
    assert_eq!(1, dht2.discovered_ops[&dht1.agent_id].len());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn ring_sync_with_matching_disc() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put historical data in both DHTs
    let historical_ops = vec![
        MemoryOp::new(UNIX_TIMESTAMP, vec![7; 4]),
        MemoryOp::new(
            UNIX_TIMESTAMP + Duration::from_secs(14 * 24 * 60 * 60),
            (u32::MAX / 2).to_le_bytes().to_vec(),
        ),
    ];
    dht1.inject_ops(historical_ops.clone()).await.unwrap();
    dht2.inject_ops(historical_ops).await.unwrap();

    // Put recent data in the first ring of both DHTs
    dht1.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![17; 4],
    )])
    .await
    .unwrap();

    dht2.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![13; 4],
    )])
    .await
    .unwrap();

    // Try a sync initiated by the first agent
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    // Each learns about one op
    assert_eq!(1, dht1.discovered_ops.len());
    assert_eq!(1, dht1.discovered_ops[&dht2.agent_id].len());
    assert_eq!(1, dht2.discovered_ops.len());
    assert_eq!(1, dht2.discovered_ops[&dht1.agent_id].len());

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn two_stage_sync_with_symmetry() {
    let current_time = Timestamp::now();
    let mut dht1 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;
    let mut dht2 = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    // Put mismatched historical data in both DHTs
    dht1.inject_ops(vec![MemoryOp::new(UNIX_TIMESTAMP, vec![7; 32])])
        .await
        .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(UNIX_TIMESTAMP, vec![13; 32])])
        .await
        .unwrap();

    // Put mismatched recent data in the first ring of both DHTs
    dht1.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![11; 32],
    )])
    .await
    .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        vec![17; 32],
    )])
    .await
    .unwrap();

    // Try a sync initiated by the first agent
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedDisc));

    let learned1 = dht1.discovered_ops.clone();
    let learned2 = dht2.discovered_ops.clone();
    dht1.discovered_ops.clear();
    dht2.discovered_ops.clear();

    // Try a sync initiated by the second agent
    let outcome = dht2.sync_with(&mut dht1).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedDisc));

    // The outcome should be identical, regardless of who initiated the sync
    assert_eq!(learned1, dht1.discovered_ops);
    assert_eq!(learned2, dht2.discovered_ops);

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // That's the disc sync done, now we need to check the rings
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    let learned1 = dht1.discovered_ops.clone();
    let learned2 = dht2.discovered_ops.clone();
    dht1.discovered_ops.clear();
    dht2.discovered_ops.clear();

    // Try a sync initiated by the second agent
    let outcome = dht2.sync_with(&mut dht1).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    // The outcome should be identical, regardless of who initiated the sync
    assert_eq!(learned1, dht1.discovered_ops);
    assert_eq!(learned2, dht2.discovered_ops);

    // Move any discovered ops between the two DHTs
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the two DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn disc_sync_respects_arc() {
    let current_time = Timestamp::now();
    let mut dht1 =
        DhtSyncHarness::new(current_time, DhtArc::Arc(0, 3 * SECTOR_SIZE - 1))
            .await;
    let mut dht2 = DhtSyncHarness::new(
        current_time,
        DhtArc::Arc(2 * SECTOR_SIZE, 4 * SECTOR_SIZE - 1),
    )
    .await;

    // Put mismatched historical data in both DHTs, but in sectors that don't overlap
    dht1.inject_ops(vec![MemoryOp::new(
        UNIX_TIMESTAMP,
        SECTOR_SIZE.to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        UNIX_TIMESTAMP,
        (3 * SECTOR_SIZE).to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();

    // At this point, the DHTs should be in sync, because the mismatch is not in common sectors
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());

    // Now put mismatched data in the common sector
    dht1.inject_ops(vec![MemoryOp::new(
        UNIX_TIMESTAMP,
        (2 * SECTOR_SIZE).to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        UNIX_TIMESTAMP,
        (2 * SECTOR_SIZE + 1).to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();

    // Try to sync the DHTs
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedDisc));

    // Sync the ops
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn ring_sync_respects_arc() {
    let current_time = Timestamp::now();
    let mut dht1 =
        DhtSyncHarness::new(current_time, DhtArc::Arc(0, 3 * SECTOR_SIZE - 1))
            .await;
    let mut dht2 = DhtSyncHarness::new(
        current_time,
        DhtArc::Arc(2 * SECTOR_SIZE, 4 * SECTOR_SIZE - 1),
    )
    .await;

    // Put mismatched recent data in both DHTs, but in sectors that don't overlap
    dht1.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        SECTOR_SIZE.to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        (3 * SECTOR_SIZE).to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();

    // At this point, the DHTs should be in sync, because the mismatch is not in common sectors
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());

    // Now put mismatched data in the common sector
    dht1.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        (2 * SECTOR_SIZE).to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();
    dht2.inject_ops(vec![MemoryOp::new(
        dht1.dht.partition.full_slice_end_timestamp(),
        (2 * SECTOR_SIZE + 1).to_le_bytes().to_vec(),
    )])
    .await
    .unwrap();

    // Try to sync the DHTs
    let outcome = dht1.sync_with(&mut dht2).await.unwrap();
    assert!(matches!(outcome, SyncWithOutcome::SyncedRings));

    // Sync the ops
    dht1.apply_op_sync(&mut dht2).await.unwrap();

    // Now the DHTs should be in sync
    assert!(dht1.is_in_sync_with(&dht2).await.unwrap());
}

#[tokio::test]
async fn op_count_starts_at_zero_for_empty_store() {
    let dht = Dht::try_from_store(UNIX_TIMESTAMP, test_store().await)
        .await
        .unwrap();
    assert_eq!(0, dht.op_count());
}

#[tokio::test]
async fn op_count_incremented_by_inform_ops_stored() {
    let current_time = Timestamp::now();
    let mut harness = DhtSyncHarness::new(current_time, DhtArc::FULL).await;

    harness
        .inject_ops(vec![
            MemoryOp::new(UNIX_TIMESTAMP, vec![7; 32]),
            MemoryOp::new(UNIX_TIMESTAMP, vec![8; 32]),
        ])
        .await
        .unwrap();
    assert_eq!(2, harness.dht.op_count());

    harness
        .inject_ops(vec![MemoryOp::new(UNIX_TIMESTAMP, vec![9; 32])])
        .await
        .unwrap();
    assert_eq!(3, harness.dht.op_count());
}

#[tokio::test]
async fn op_count_loaded_from_store_on_startup() {
    let store: DynOpStore = test_store().await;
    store
        .process_incoming_ops(vec![
            MemoryOp::new(UNIX_TIMESTAMP, vec![1; 32]).into(),
            MemoryOp::new(UNIX_TIMESTAMP, vec![2; 32]).into(),
            MemoryOp::new(UNIX_TIMESTAMP, vec![3; 32]).into(),
        ])
        .await
        .unwrap();

    let dht = Dht::try_from_store(Timestamp::now(), store).await.unwrap();
    assert_eq!(3, dht.op_count());
}