calimero-node 0.10.0-rc.52

Core Calimero infrastructure and tools
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
//! Network simulation tests
//!
//! Tests real-world network scenarios with mocked libp2p infrastructure:
//! - Encrypted delta broadcast
//! - P2P delta requests
//! - Missing delta catch-up
//! - Divergence detection
//!
//! These tests bridge the gap between unit tests and e2e tests by simulating
//! network behavior without requiring actual libp2p runtime.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use calimero_crypto::{Nonce, SharedKey};
use calimero_dag::{CausalDelta, DagStore};
use calimero_primitives::context::ContextId;

// Re-export types for convenience
type ContextHash = [u8; 32];
use calimero_primitives::identity::{PrivateKey, PublicKey};
use calimero_storage::action::Action;
use calimero_storage::address::Id;
use calimero_storage::delta::StorageDelta;
use tokio::sync::{Mutex, RwLock};

// ============================================================
// Mock Network Infrastructure
// ============================================================

/// Simulated peer in the network
struct MockPeer {
    peer_id: String,
    public_key: PublicKey,
    private_key: Arc<PrivateKey>, // Arc so we can clone
    dag: Arc<RwLock<DagStore<Vec<Action>>>>,

    /// Received deltas (for verification)
    received_deltas: Arc<Mutex<Vec<([u8; 32], Vec<Action>)>>>,

    /// Network delay simulation (milliseconds)
    latency_ms: u64,
}

impl Clone for MockPeer {
    fn clone(&self) -> Self {
        Self {
            peer_id: self.peer_id.clone(),
            public_key: self.public_key,
            private_key: Arc::clone(&self.private_key),
            dag: Arc::clone(&self.dag),
            received_deltas: Arc::clone(&self.received_deltas),
            latency_ms: self.latency_ms,
        }
    }
}

impl MockPeer {
    fn new(peer_id: &str, latency_ms: u64) -> Self {
        let private_key = PrivateKey::random(&mut rand::thread_rng());
        let public_key = private_key.public_key();

        Self {
            peer_id: peer_id.to_string(),
            public_key,
            private_key: Arc::new(private_key),
            dag: Arc::new(RwLock::new(DagStore::new([0; 32]))),
            received_deltas: Arc::new(Mutex::new(Vec::new())),
            latency_ms,
        }
    }

    /// Simulate network latency
    async fn simulate_latency(&self) {
        if self.latency_ms > 0 {
            tokio::time::sleep(Duration::from_millis(self.latency_ms)).await;
        }
    }
}

/// Mock network that simulates libp2p gossipsub and streams
struct MockNetwork {
    peers: HashMap<String, MockPeer>,
    /// Topic subscribers (context_id -> peer_ids)
    subscriptions: Arc<RwLock<HashMap<ContextId, Vec<String>>>>,
    /// Broadcast queue for async delivery
    broadcast_queue: Arc<Mutex<Vec<BroadcastMessage>>>,
}

#[derive(Clone)]
struct BroadcastMessage {
    context_id: ContextId,
    author_id: PublicKey,
    delta_id: [u8; 32],
    parent_ids: Vec<[u8; 32]>,
    root_hash: ContextHash,
    encrypted_artifact: Vec<u8>,
    nonce: Nonce,
}

impl MockNetwork {
    fn new() -> Self {
        Self {
            peers: HashMap::new(),
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
            broadcast_queue: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Add a peer to the network
    fn add_peer(&mut self, peer: MockPeer) {
        self.peers.insert(peer.peer_id.clone(), peer);
    }

    /// Subscribe a peer to a context (topic)
    async fn subscribe(&self, peer_id: &str, context_id: ContextId) {
        let mut subs = self.subscriptions.write().await;
        subs.entry(context_id)
            .or_insert_with(Vec::new)
            .push(peer_id.to_string());
    }

    /// Broadcast a delta to all subscribers
    async fn broadcast(
        &self,
        context_id: ContextId,
        author_peer_id: &str,
        delta_id: [u8; 32],
        parent_ids: Vec<[u8; 32]>,
        root_hash: ContextHash,
        artifact: Vec<u8>,
        sender_key: &PrivateKey,
    ) -> eyre::Result<()> {
        // Encrypt artifact
        let shared_key = SharedKey::from_sk(sender_key);
        let nonce: Nonce = rand::random();
        let encrypted = shared_key
            .encrypt(artifact.clone(), nonce)
            .ok_or_else(|| eyre::eyre!("Failed to encrypt"))?;

        let msg = BroadcastMessage {
            context_id,
            author_id: sender_key.public_key(),
            delta_id,
            parent_ids,
            root_hash,
            encrypted_artifact: encrypted,
            nonce,
        };

        // Queue broadcast for async delivery
        self.broadcast_queue.lock().await.push(msg.clone());

        // Deliver to all subscribers (except sender)
        let subs = self.subscriptions.read().await;
        if let Some(subscribers) = subs.get(&context_id) {
            for peer_id in subscribers {
                if peer_id != author_peer_id {
                    if let Some(peer) = self.peers.get(peer_id) {
                        // Simulate network latency
                        peer.simulate_latency().await;

                        // Decrypt and process
                        let decrypted = shared_key
                            .decrypt(msg.encrypted_artifact.clone(), msg.nonce)
                            .ok_or_else(|| eyre::eyre!("Decryption failed"))?;

                        let storage_delta: StorageDelta = borsh::from_slice(&decrypted)?;

                        if let StorageDelta::Actions(actions) = storage_delta {
                            // Store for verification
                            peer.received_deltas
                                .lock()
                                .await
                                .push((msg.delta_id, actions.clone()));

                            // Add to DAG (simplified - no applier for mock)
                            let dag_delta = CausalDelta {
                                id: msg.delta_id,
                                parents: msg.parent_ids.clone(),
                                payload: actions,
                                hlc: calimero_storage::env::hlc_timestamp(),
                                expected_root_hash: msg.root_hash.into(),
                                kind: calimero_dag::DeltaKind::Regular,
                            };

                            // Note: In real implementation, this would use DeltaApplier
                            // For tests, we just track receipt
                            peer.dag.write().await.has_delta(&dag_delta.id);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Request a specific delta from a peer (P2P stream)
    async fn request_delta(
        &self,
        from_peer_id: &str,
        delta_id: [u8; 32],
    ) -> Option<CausalDelta<Vec<Action>>> {
        let peer = self.peers.get(from_peer_id)?;

        // Simulate network latency
        peer.simulate_latency().await;

        // Return delta if peer has it (clone to own it)
        peer.dag.read().await.get_delta(&delta_id).cloned()
    }

    /// Get DAG heads from a peer
    async fn request_dag_heads(&self, from_peer_id: &str) -> Option<(Vec<[u8; 32]>, ContextHash)> {
        let peer = self.peers.get(from_peer_id)?;
        peer.simulate_latency().await;

        let heads = peer.dag.read().await.get_heads();
        Some((heads, [0; 32])) // Simplified root hash
    }
}

// ============================================================
// Test: Encrypted Delta Broadcast
// ============================================================

#[tokio::test]
async fn test_encrypted_delta_broadcast() {
    let mut network = MockNetwork::new();
    let context_id = ContextId::from([1; 32]);

    // Create 3 peers
    let peer_a = MockPeer::new("peer_a", 10);
    let peer_b = MockPeer::new("peer_b", 15);
    let peer_c = MockPeer::new("peer_c", 20);

    network.add_peer(peer_a.clone());
    network.add_peer(peer_b.clone());
    network.add_peer(peer_c.clone());

    // All peers subscribe to context
    network.subscribe("peer_a", context_id).await;
    network.subscribe("peer_b", context_id).await;
    network.subscribe("peer_c", context_id).await;

    // Peer A creates and broadcasts a delta
    let id = Id::from([100; 32]);
    let action = Action::Update {
        id,
        data: vec![1, 2, 3, 4],
        ancestors: vec![],
        metadata: Default::default(),
    };

    let storage_delta = StorageDelta::Actions(vec![action.clone()]);
    let artifact = borsh::to_vec(&storage_delta).unwrap();

    network
        .broadcast(
            context_id,
            "peer_a",
            [1; 32],
            vec![[0; 32]],
            [0; 32],
            artifact,
            &peer_a.private_key,
        )
        .await
        .unwrap();

    // Wait for network propagation
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Verify peer B received the delta
    let peer_b_received = peer_b.received_deltas.lock().await;
    assert_eq!(peer_b_received.len(), 1);
    assert_eq!(peer_b_received[0].0, [1; 32]);
    assert_eq!(peer_b_received[0].1.len(), 1);

    // Verify peer C received the delta
    let peer_c_received = peer_c.received_deltas.lock().await;
    assert_eq!(peer_c_received.len(), 1);
    assert_eq!(peer_c_received[0].0, [1; 32]);

    // Verify sender (peer A) did NOT receive own message
    let peer_a_received = peer_a.received_deltas.lock().await;
    assert_eq!(peer_a_received.len(), 0);
}

#[tokio::test]
async fn test_encrypted_broadcast_with_different_keys() {
    let mut network = MockNetwork::new();
    let context_id = ContextId::from([2; 32]);

    let peer_a = MockPeer::new("peer_a", 0);
    let peer_b = MockPeer::new("peer_b", 0);

    network.add_peer(peer_a.clone());
    network.add_peer(peer_b.clone());

    network.subscribe("peer_a", context_id).await;
    network.subscribe("peer_b", context_id).await;

    // Each peer uses their own key for encryption
    let storage_delta = StorageDelta::Actions(vec![]);
    let artifact = borsh::to_vec(&storage_delta).unwrap();

    // Peer A broadcasts
    network
        .broadcast(
            context_id,
            "peer_a",
            [10; 32],
            vec![[0; 32]],
            [0; 32],
            artifact.clone(),
            &peer_a.private_key,
        )
        .await
        .unwrap();

    // Peer B broadcasts
    network
        .broadcast(
            context_id,
            "peer_b",
            [20; 32],
            vec![[0; 32]],
            [0; 32],
            artifact,
            &peer_b.private_key,
        )
        .await
        .unwrap();

    tokio::time::sleep(Duration::from_millis(10)).await;

    // Both peers should receive each other's messages
    assert_eq!(peer_a.received_deltas.lock().await.len(), 1);
    assert_eq!(peer_b.received_deltas.lock().await.len(), 1);
}

// ============================================================
// Test: P2P Delta Requests (Missing Delta Catch-Up)
// ============================================================

#[tokio::test]
async fn test_p2p_delta_request() {
    let mut network = MockNetwork::new();

    let peer_a = MockPeer::new("peer_a", 5);
    let peer_b = MockPeer::new("peer_b", 5);

    network.add_peer(peer_a.clone());
    network.add_peer(peer_b.clone());

    // Peer A has a delta that peer B needs
    let _delta = CausalDelta {
        id: [50; 32],
        parents: vec![[0; 32]],
        payload: vec![Action::Update {
            id: Id::from([99; 32]),
            data: vec![5, 6, 7],
            ancestors: vec![],
            metadata: Default::default(),
        }],
        hlc: calimero_storage::env::hlc_timestamp(),
        expected_root_hash: [0xDE; 32],
        kind: calimero_dag::DeltaKind::Regular,
    };

    // Manually add to peer A's DAG (simulating it received this earlier)
    // Note: We can't actually call add_delta without an applier, so we just verify the request mechanism

    // Peer B requests the delta from peer A
    let requested = network.request_delta("peer_a", [50; 32]).await;

    // Should get None since we didn't actually add it (no applier in mock)
    // In real implementation with full DeltaStore, this would return Some(delta)
    assert!(requested.is_none());
}

// ============================================================
// Test: DAG Heads Request/Response
// ============================================================

#[tokio::test]
async fn test_dag_heads_request() {
    let mut network = MockNetwork::new();

    let peer_a = MockPeer::new("peer_a", 0);
    let peer_b = MockPeer::new("peer_b", 0);

    network.add_peer(peer_a.clone());
    network.add_peer(peer_b.clone());

    // Peer B requests DAG heads from peer A
    let result = network.request_dag_heads("peer_a").await;

    assert!(result.is_some());
    let (heads, _root_hash) = result.unwrap();

    // New DAG should have just the root
    assert_eq!(heads, vec![[0; 32]]);
}

// ============================================================
// Test: Network Latency Simulation
// ============================================================

#[tokio::test]
async fn test_network_latency_simulation() {
    let mut network = MockNetwork::new();
    let context_id = ContextId::from([4; 32]);

    // Peer A has low latency, Peer B has high latency
    let peer_a = MockPeer::new("peer_a", 5);
    let peer_b = MockPeer::new("peer_b", 100);
    let peer_recv = MockPeer::new("peer_recv", 0);

    network.add_peer(peer_a.clone());
    network.add_peer(peer_b.clone());
    network.add_peer(peer_recv.clone());

    network.subscribe("peer_a", context_id).await;
    network.subscribe("peer_b", context_id).await;
    network.subscribe("peer_recv", context_id).await;

    let storage_delta = StorageDelta::Actions(vec![]);
    let artifact = borsh::to_vec(&storage_delta).unwrap();

    // Peer A broadcasts with low latency
    network
        .broadcast(
            context_id,
            "peer_a",
            [10; 32],
            vec![[0; 32]],
            [0; 32],
            artifact.clone(),
            &peer_a.private_key,
        )
        .await
        .unwrap();

    // Wait for peer A's message to be delivered (5ms latency)
    tokio::time::sleep(Duration::from_millis(20)).await;

    // Peer B broadcasts with high latency
    network
        .broadcast(
            context_id,
            "peer_b",
            [20; 32],
            vec![[0; 32]],
            [0; 32],
            artifact,
            &peer_b.private_key,
        )
        .await
        .unwrap();

    // Wait for peer B's message (100ms latency)
    tokio::time::sleep(Duration::from_millis(120)).await;

    // peer_recv should have received both deltas
    let received = peer_recv.received_deltas.lock().await;
    assert_eq!(received.len(), 2, "Should receive deltas from both peers");

    // Verify both deltas were received (order may vary due to async timing)
    let delta_ids: Vec<_> = received.iter().map(|(id, _)| *id).collect();
    assert!(
        delta_ids.contains(&[10; 32]),
        "Should receive peer_a's delta"
    );
    assert!(
        delta_ids.contains(&[20; 32]),
        "Should receive peer_b's delta"
    );
}

#[tokio::test]
async fn test_concurrent_broadcasts_from_multiple_peers() {
    let mut network = MockNetwork::new();
    let context_id = ContextId::from([5; 32]);

    // Create 5 peers
    let peers: Vec<_> = (0..5)
        .map(|i| {
            let peer = MockPeer::new(&format!("peer_{}", i), i * 5);
            network.add_peer(peer.clone());
            peer
        })
        .collect();

    // All subscribe
    for i in 0..5 {
        network.subscribe(&format!("peer_{}", i), context_id).await;
    }

    // All peers broadcast concurrently
    for (i, peer) in peers.iter().enumerate() {
        let storage_delta = StorageDelta::Actions(vec![]);
        let artifact = borsh::to_vec(&storage_delta).unwrap();
        let peer_id = format!("peer_{}", i);

        network
            .broadcast(
                context_id,
                &peer_id,
                [i as u8; 32],
                vec![[0; 32]],
                [0; 32],
                artifact,
                &peer.private_key,
            )
            .await
            .unwrap();
    }

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Each peer should receive 4 deltas (all except own)
    for (i, peer) in peers.iter().enumerate() {
        let received = peer.received_deltas.lock().await;
        assert_eq!(received.len(), 4, "Peer {} should receive 4 deltas", i);
    }
}

// ============================================================
// Test: Subscription Management
// ============================================================

#[tokio::test]
async fn test_only_subscribed_peers_receive_broadcasts() {
    let mut network = MockNetwork::new();
    let context_a = ContextId::from([10; 32]);
    let context_b = ContextId::from([20; 32]);

    let peer_1 = MockPeer::new("peer_1", 0);
    let peer_2 = MockPeer::new("peer_2", 0);
    let peer_3 = MockPeer::new("peer_3", 0);

    network.add_peer(peer_1.clone());
    network.add_peer(peer_2.clone());
    network.add_peer(peer_3.clone());

    // Peer 1 & 2 subscribe to context A
    network.subscribe("peer_1", context_a).await;
    network.subscribe("peer_2", context_a).await;

    // Peer 3 subscribes to context B (different context)
    network.subscribe("peer_3", context_b).await;

    // Broadcast to context A
    let storage_delta = StorageDelta::Actions(vec![]);
    let artifact = borsh::to_vec(&storage_delta).unwrap();

    network
        .broadcast(
            context_a,
            "peer_1",
            [1; 32],
            vec![[0; 32]],
            [0; 32],
            artifact,
            &peer_1.private_key,
        )
        .await
        .unwrap();

    tokio::time::sleep(Duration::from_millis(10)).await;

    // Peer 2 should receive (subscribed to context A)
    assert_eq!(peer_2.received_deltas.lock().await.len(), 1);

    // Peer 3 should NOT receive (subscribed to different context)
    assert_eq!(peer_3.received_deltas.lock().await.len(), 0);
}