-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternet_e2e.rs
More file actions
615 lines (568 loc) · 20.4 KB
/
Copy pathinternet_e2e.rs
File metadata and controls
615 lines (568 loc) · 20.4 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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! M3 node-level acceptance (first slice): two nodes exchange messages and
//! receipts over the libp2p internet transport on localhost — and when both
//! a millisecond link and a human-scale link are available, the scheduler
//! prefers the faster one.
use std::sync::Arc;
use std::time::Duration;
use rand::rngs::StdRng;
use rand::SeedableRng;
use kult_crypto::KdfProfile;
use kult_node::{DiscoveryMode, DiscoveryPublicationPolicy, Event, Node};
use kult_store::DeliveryState;
use kult_transport::{DeliveryHint, Libp2pTransport, SneakernetTransport};
const NOW: u64 = 1_800_000_000;
const TEST_KDF: KdfProfile = KdfProfile {
m_cost_kib: 8,
t_cost: 1,
p_cost: 1,
};
fn received_bodies(events: &[Event]) -> Vec<Vec<u8>> {
events
.iter()
.filter_map(|e| match e {
Event::MessageReceived { body, .. } => Some(body.clone()),
_ => None,
})
.collect()
}
async fn drive_direct_pair(
alice: &mut Node,
bob: &mut Node,
alice_rng: &mut StdRng,
bob_rng: &mut StdRng,
start: u64,
rounds: u64,
) -> (Vec<Event>, Vec<Event>) {
tokio::join!(
async {
let mut events = Vec::new();
for round in 0..rounds {
events.extend(alice.tick(start + round * 40, alice_rng).await.unwrap());
tokio::time::sleep(Duration::from_millis(25)).await;
}
events
},
async {
let mut events = Vec::new();
tokio::time::sleep(Duration::from_millis(250)).await;
for round in 0..rounds {
events.extend(bob.tick(start + round * 40 + 1, bob_rng).await.unwrap());
tokio::time::sleep(Duration::from_millis(25)).await;
}
events
}
)
}
#[tokio::test]
async fn nodes_exchange_over_localhost_quic() {
let mut rng = StdRng::seed_from_u64(7);
let dir = tempfile::tempdir().unwrap();
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut rng).unwrap();
let a_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
let b_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
let a_addr = a_net.wait_listen_addr().await.unwrap();
let b_addr = b_net.wait_listen_addr().await.unwrap();
alice.add_transport(a_net);
bob.add_transport(b_net);
// Out-of-band exchange: bundles + each other's multiaddrs.
let alice_bundle = alice.handshake_bundle(NOW, &mut rng).unwrap();
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::Multiaddr(b_addr)],
NOW,
&mut rng,
)
.unwrap();
let alice_id = bob
.add_contact(
"alice",
&alice_bundle,
&[DeliveryHint::Multiaddr(a_addr)],
NOW,
&mut rng,
)
.unwrap();
// Alice → Bob: handshake flight + session message, one tick each side.
let m1 = alice
.send_message(&bob_id, b"hello over quic", NOW, &mut rng)
.unwrap();
let m2 = alice
.send_message(&bob_id, b"and a second one", NOW, &mut rng)
.unwrap();
let mut alice_ticks = StdRng::seed_from_u64(0x3000_0001);
let mut bob_ticks = StdRng::seed_from_u64(0x3000_0002);
let (alice_events, bob_events) = drive_direct_pair(
&mut alice,
&mut bob,
&mut alice_ticks,
&mut bob_ticks,
NOW + 1,
8,
)
.await;
assert_eq!(
alice.queued().unwrap(),
0,
"both envelopes acked by next hop"
);
assert!(bob_events
.iter()
.any(|e| matches!(e, Event::SessionEstablished { peer } if *peer == alice_id)));
let bodies = received_bodies(&bob_events);
assert!(bodies.contains(&b"hello over quic".to_vec()));
assert!(bodies.contains(&b"and a second one".to_vec()));
// Bob's encrypted receipt flowed back through durable direct admission;
// Alice's records reach Delivered — end-to-end proof, not transport ack.
let delivered: Vec<[u8; 16]> = alice_events
.iter()
.filter_map(|e| match e {
Event::DeliveryUpdated {
id,
state: DeliveryState::Delivered,
} => Some(*id),
_ => None,
})
.collect();
assert!(delivered.contains(&m1) && delivered.contains(&m2));
// Bob replies over the established session.
bob.send_message(&alice_id, b"loud and clear", NOW + 4, &mut rng)
.unwrap();
let (_, alice_events) = drive_direct_pair(
&mut bob,
&mut alice,
&mut bob_ticks,
&mut alice_ticks,
NOW + 400,
5,
)
.await;
assert!(received_bodies(&alice_events).contains(&b"loud and clear".to_vec()));
}
#[tokio::test]
async fn simultaneous_first_flights_converge_with_follow_up_messages() {
let mut setup_rng = StdRng::seed_from_u64(0x3000_4000);
let dir = tempfile::tempdir().unwrap();
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut setup_rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut setup_rng).unwrap();
let a_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
let b_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
let a_addr = a_net.wait_listen_addr().await.unwrap();
let b_addr = b_net.wait_listen_addr().await.unwrap();
alice.add_transport(a_net);
bob.add_transport(b_net);
let alice_bundle = alice.handshake_bundle(NOW, &mut setup_rng).unwrap();
let bob_bundle = bob.handshake_bundle(NOW, &mut setup_rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::Multiaddr(b_addr)],
NOW,
&mut setup_rng,
)
.unwrap();
let alice_id = bob
.add_contact(
"alice",
&alice_bundle,
&[DeliveryHint::Multiaddr(a_addr)],
NOW,
&mut setup_rng,
)
.unwrap();
alice
.send_message(&bob_id, b"alice first", NOW, &mut setup_rng)
.unwrap();
alice
.send_message(&bob_id, b"alice follow-up", NOW + 1, &mut setup_rng)
.unwrap();
bob.send_message(&alice_id, b"bob first", NOW, &mut setup_rng)
.unwrap();
bob.send_message(&alice_id, b"bob follow-up", NOW + 1, &mut setup_rng)
.unwrap();
let mut alice_ticks = StdRng::seed_from_u64(0x3000_4001);
let mut bob_ticks = StdRng::seed_from_u64(0x3000_4002);
let (mut alice_events, mut bob_events) = drive_direct_pair(
&mut alice,
&mut bob,
&mut alice_ticks,
&mut bob_ticks,
NOW + 2,
10,
)
.await;
let (more_alice, more_bob) = drive_direct_pair(
&mut alice,
&mut bob,
&mut alice_ticks,
&mut bob_ticks,
NOW + 500,
10,
)
.await;
alice_events.extend(more_alice);
bob_events.extend(more_bob);
let alice_bodies = received_bodies(&alice_events);
let bob_bodies = received_bodies(&bob_events);
assert!(alice_bodies.contains(&b"bob first".to_vec()));
assert!(alice_bodies.contains(&b"bob follow-up".to_vec()));
assert!(bob_bodies.contains(&b"alice first".to_vec()));
assert!(bob_bodies.contains(&b"alice follow-up".to_vec()));
}
/// M3 acceptance slice: no manual configuration beyond sharing a Connect
/// code. Bob publishes his fixed capability-scoped record (with his multiaddr
/// inside it) on the DHT; Alice — knowing only the code and a common bootstrap
/// peer — fetches it, verifies it, and messages him. The message arriving over
/// Alice's only transport proves the delivery hints came from the DHT record,
/// not out-of-band.
#[tokio::test]
async fn contact_by_connect_code_via_dht() {
let mut rng = StdRng::seed_from_u64(9);
let dir = tempfile::tempdir().unwrap();
// Any reachable peer bootstraps the DHT — here a bare transport with no
// node behind it, standing in for a community node.
let seed = Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap();
let seed_addr = seed.wait_listen_addr().await.unwrap();
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut rng).unwrap();
let a_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
let b_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
a_net.bootstrap(&[seed_addr.as_str()]).await.unwrap();
b_net.bootstrap(&[seed_addr.as_str()]).await.unwrap();
// Bob publishes: authority, ingress bundle, and reachability under
// capability-derived weekly locators.
let b_hints: Vec<DeliveryHint> = b_net
.listen_addrs()
.into_iter()
.map(DeliveryHint::Multiaddr)
.collect();
let a_hints: Vec<DeliveryHint> = a_net
.listen_addrs()
.into_iter()
.map(DeliveryHint::Multiaddr)
.collect();
alice.add_transport(Arc::clone(&a_net) as Arc<dyn kult_transport::Transport>);
alice.add_discovery(a_net);
bob.add_transport(Arc::clone(&b_net) as Arc<dyn kult_transport::Transport>);
bob.add_discovery(Arc::clone(&b_net) as Arc<dyn kult_transport::Discovery>);
bob.publish_bundle_with_policy(
&b_hints,
DiscoveryPublicationPolicy {
mode: DiscoveryMode::Sovereign,
publish_direct_routes: true,
},
NOW,
)
.await
.unwrap();
// Alice publishes too: Bob learns her only through her (sealed-sender)
// handshake, which carries no return path — his receipt finds its way
// back via her DHT record.
alice
.publish_bundle_with_policy(
&a_hints,
DiscoveryPublicationPolicy {
mode: DiscoveryMode::Sovereign,
publish_direct_routes: true,
},
NOW,
)
.await
.unwrap();
// Alice knows nothing but Bob's capability-scoped Connect code.
let bob_id = alice
.add_contact_by_address("bob", &bob.connect_code().unwrap(), NOW, &mut rng)
.await
.unwrap();
assert_eq!(bob_id, bob.peer_id());
let m1 = alice
.send_message(&bob_id, b"found you by Connect code", NOW, &mut rng)
.unwrap();
let mut alice_ticks = StdRng::seed_from_u64(0x3000_1001);
let mut bob_ticks = StdRng::seed_from_u64(0x3000_1002);
let (alice_events, bob_events) = drive_direct_pair(
&mut alice,
&mut bob,
&mut alice_ticks,
&mut bob_ticks,
NOW + 1,
8,
)
.await;
assert!(received_bodies(&bob_events).is_empty());
assert!(bob_events
.iter()
.any(|event| matches!(event, Event::MessageRequestReceived { .. })));
assert!(bob.contacts().unwrap().is_empty());
let request = bob.message_requests().unwrap().remove(0);
assert_eq!(request.preview, "found you by Connect code");
bob.accept_message_request(&request.id, "alice", NOW + 320, &mut bob_ticks)
.unwrap();
assert_eq!(
bob.messages_with(&request.account).unwrap()[0].body,
b"found you by Connect code"
);
// Bob's encrypted receipt drives Alice's record to Delivered.
let (_, accepted_events) = drive_direct_pair(
&mut bob,
&mut alice,
&mut bob_ticks,
&mut alice_ticks,
NOW + 321,
6,
)
.await;
assert!(alice_events
.iter()
.chain(accepted_events.iter())
.any(|e| matches!(
e,
Event::DeliveryUpdated { id, state: DeliveryState::Delivered } if *id == m1
)));
// A Connect capability nobody published resolves to an honest
// BundleNotFound.
let ghost = Node::create(&dir.path().join("g.db"), b"g", TEST_KDF, &mut rng).unwrap();
assert!(matches!(
alice
.add_contact_by_address("ghost", &ghost.connect_code().unwrap(), NOW + 600, &mut rng,)
.await,
Err(kult_node::NodeError::BundleNotFound)
));
}
#[tokio::test]
async fn scheduler_prefers_fast_link_over_sneakernet() {
let mut rng = StdRng::seed_from_u64(8);
let dir = tempfile::tempdir().unwrap();
let bob_spool = dir.path().join("bob-spool");
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut rng).unwrap();
// Alice has both carriers; Bob is reachable by both hints.
let a_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/tcp/0"])
.await
.unwrap(),
);
let b_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/tcp/0"])
.await
.unwrap(),
);
let b_addr = b_net.wait_listen_addr().await.unwrap();
alice.add_transport(a_net);
alice.add_transport(Arc::new(
SneakernetTransport::new(dir.path().join("alice-spool")).unwrap(),
));
bob.add_transport(b_net);
bob.add_transport(Arc::new(SneakernetTransport::new(&bob_spool).unwrap()));
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[
DeliveryHint::Spool(bob_spool.clone()),
DeliveryHint::Multiaddr(b_addr),
],
NOW,
&mut rng,
)
.unwrap();
alice
.send_message(&bob_id, b"take the fast lane", NOW, &mut rng)
.unwrap();
let mut alice_ticks = StdRng::seed_from_u64(0x3000_2001);
let mut bob_ticks = StdRng::seed_from_u64(0x3000_2002);
let (_, bob_events) = drive_direct_pair(
&mut alice,
&mut bob,
&mut alice_ticks,
&mut bob_ticks,
NOW + 1,
8,
)
.await;
// The envelope went over the wire, not into the spool directory.
let spool_files = std::fs::read_dir(&bob_spool).unwrap().count();
assert_eq!(spool_files, 0, "millis link outranks human-scale link");
assert!(received_bodies(&bob_events).is_empty());
let request = bob.message_requests().unwrap().remove(0);
assert_eq!(request.preview, "take the fast lane");
assert_eq!(
request.transport,
kult_store::AdmissionTransportClass::Direct
);
bob.accept_message_request(&request.id, "alice", NOW + 320, &mut bob_ticks)
.unwrap();
assert_eq!(
bob.messages_with(&request.account).unwrap()[0].body,
b"take the fast lane"
);
}
/// A pairing-time hint can go stale whenever a peer rebinds to fresh
/// OS-assigned ports. Public discovery must not become a post-pairing
/// tracking oracle: only a bundle/control received over the authenticated
/// relationship may replace that route.
#[tokio::test]
async fn stale_pairing_hint_heals_only_via_authenticated_peer_update() {
let mut rng = StdRng::seed_from_u64(41);
let dir = tempfile::tempdir().unwrap();
let seed = Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap();
let seed_addr = seed.wait_listen_addr().await.unwrap();
let mut alice = Node::create(&dir.path().join("a.db"), b"a", TEST_KDF, &mut rng).unwrap();
let mut bob = Node::create(&dir.path().join("b.db"), b"b", TEST_KDF, &mut rng).unwrap();
let a_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
let b_net = Arc::new(
Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap(),
);
a_net.bootstrap(&[seed_addr.as_str()]).await.unwrap();
b_net.bootstrap(&[seed_addr.as_str()]).await.unwrap();
b_net.wait_listen_addr().await.unwrap();
let a_hints: Vec<DeliveryHint> = a_net
.listen_addrs()
.into_iter()
.map(DeliveryHint::Multiaddr)
.collect();
let b_hints: Vec<DeliveryHint> = b_net
.listen_addrs()
.into_iter()
.map(DeliveryHint::Multiaddr)
.collect();
alice.add_transport(Arc::clone(&a_net) as Arc<dyn kult_transport::Transport>);
alice.add_discovery(a_net);
bob.add_transport(Arc::clone(&b_net) as Arc<dyn kult_transport::Transport>);
bob.add_discovery(Arc::clone(&b_net) as Arc<dyn kult_transport::Discovery>);
// Public Standard records deliberately omit these direct routes. Their
// presence in the DHT therefore cannot heal a paired contact.
bob.publish_bundle(&b_hints, NOW).await.unwrap();
alice.publish_bundle(&a_hints, NOW).await.unwrap();
// Pairing exchange — but Alice captured Bob's address from a previous
// run. Every run mints a fresh transport pseudonym and fresh
// OS-assigned ports, so the stale hint names a peer id and port that no
// longer exist anywhere (the routing table cannot rescue the dial the
// way it could for a merely re-ported current pseudonym). TCP so the
// refusal is immediate.
let ghost = Libp2pTransport::new(&["/ip4/127.0.0.1/udp/0/quic-v1"])
.await
.unwrap();
let ghost_id = ghost
.wait_listen_addr()
.await
.unwrap()
.split_once("/p2p/")
.map(|(_, id)| id.to_owned())
.unwrap();
drop(ghost);
let stale = format!("/ip4/127.0.0.1/tcp/9/p2p/{ghost_id}");
let alice_bundle = alice.handshake_bundle(NOW, &mut rng).unwrap();
let bob_bundle = bob.handshake_bundle(NOW, &mut rng).unwrap();
let bob_id = alice
.add_contact(
"bob",
&bob_bundle,
&[DeliveryHint::Multiaddr(stale)],
NOW,
&mut rng,
)
.unwrap();
let alice_id = bob
.add_contact("alice", &alice_bundle, &a_hints, NOW, &mut rng)
.unwrap();
let m1 = alice
.send_message(&bob_id, b"through the refresh", NOW, &mut rng)
.unwrap();
// First flush dials the dead route and fails into backoff; nothing
// reaches Bob.
alice.tick(NOW + 1, &mut rng).await.unwrap();
let events = bob.tick(NOW + 2, &mut rng).await.unwrap();
assert_eq!(received_bodies(&events), Vec::<Vec<u8>>::new());
assert!(alice.queued().unwrap() > 0, "stuck on the stale route");
// A normal authenticated first flight in the other direction carries
// Bob's current relationship-scoped return bundle. Only that update may
// replace Alice's stale route.
bob.send_message(&alice_id, b"authenticated route update", NOW + 3, &mut rng)
.unwrap();
let mut alice_ticks = StdRng::seed_from_u64(0x3000_3001);
let mut bob_ticks = StdRng::seed_from_u64(0x3000_3002);
let (bob_update_events, alice_update_events) = drive_direct_pair(
&mut bob,
&mut alice,
&mut bob_ticks,
&mut alice_ticks,
NOW + 40,
8,
)
.await;
assert!(received_bodies(&alice_update_events).contains(&b"authenticated route update".to_vec()));
// The authenticated first flight replaced Alice's stale source. A second
// bounded pass retries her already-queued first flight over that route.
let (alice_events, bob_events) = drive_direct_pair(
&mut alice,
&mut bob,
&mut alice_ticks,
&mut bob_ticks,
NOW + 400,
8,
)
.await;
assert!(received_bodies(&bob_update_events)
.into_iter()
.chain(received_bodies(&bob_events))
.any(|body| body == b"through the refresh"));
let (_, receipt_events) = drive_direct_pair(
&mut bob,
&mut alice,
&mut bob_ticks,
&mut alice_ticks,
NOW + 800,
6,
)
.await;
// Bob's encrypted receipt drives Alice's record to Delivered over the
// pairwise-authenticated route.
assert!(alice_update_events
.iter()
.chain(alice_events.iter())
.chain(receipt_events.iter())
.any(|e| matches!(
e,
Event::DeliveryUpdated { id, state: DeliveryState::Delivered } if *id == m1
)));
}