calimero-node 0.10.1-rc.18

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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! Protocol execution for simulation testing.
//!
//! Runs the **production** sync protocol implementations using simulation
//! infrastructure (`SimStream`, `SimStorage`) for end-to-end testing.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │                    execute_hash_comparison_sync                 │
//! │                                                                 │
//! │  ┌────────────────────┐          ┌────────────────────┐         │
//! │  │  Initiator Task    │          │  Responder Task    │         │
//! │  │  (alice)           │◄───────-►│  (bob)             │         │
//! │  │                    │ SimStream│                    │         │
//! │  │  Store (InMemory)  │  pair   │  Store (InMemory)   │         │
//! │  └────────────────────┘         └────────────────────┘         │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Key Design: Same Code, Different Backends
//!
//! This module calls the **exact same** `HashComparisonProtocol` that runs
//! in production. The only difference is the backends:
//! - Production: `StreamTransport` (network) + `Store<RocksDB>`
//! - Simulation: `SimStream` (channels) + `Store<InMemoryDB>`
//!
//! # Invariants Tested
//!
//! - **I4**: Strategy equivalence (same final state as other protocols)
//! - **I5**: No silent data loss (CRDT merge at leaves)
//! - **I6**: Delta buffering during sync

use calimero_node::sync::{
    HashComparisonConfig, HashComparisonFirstRequest, HashComparisonProtocol, HashComparisonStats,
};
use calimero_node_primitives::sync::{
    InitPayload, StreamMessage, SyncProtocolExecutor, SyncTransport,
};
use calimero_primitives::identity::PublicKey;
use eyre::{bail, Result, WrapErr};

use super::node::SimNode;
use super::transport::SimStream;

/// Statistics from a simulated HashComparison sync session.
///
/// This is a thin wrapper around the production `HashComparisonStats`
/// with simulation-specific additions if needed.
#[derive(Debug, Default, Clone)]
pub struct SimSyncStats {
    /// Number of tree nodes compared.
    pub nodes_compared: u64,
    /// Number of leaf entities transferred (pulled from peer).
    pub entities_transferred: u64,
    /// Number of leaf entities pushed to peer (bidirectional sync).
    pub entities_pushed: u64,
    /// Number of nodes skipped (hashes matched).
    pub nodes_skipped: u64,
    /// Number of request/response rounds.
    pub rounds: u64,
}

impl From<HashComparisonStats> for SimSyncStats {
    fn from(stats: HashComparisonStats) -> Self {
        Self {
            nodes_compared: stats.nodes_compared,
            entities_transferred: stats.entities_merged,
            entities_pushed: stats.entities_pushed,
            nodes_skipped: stats.nodes_skipped,
            rounds: stats.requests_sent,
        }
    }
}

/// Execute HashComparison sync between two SimNodes.
///
/// This runs the **production** `HashComparisonProtocol`:
/// 1. Creates bidirectional SimStream
/// 2. Spawns initiator and responder tasks
/// 3. Returns when sync completes
///
/// # Arguments
///
/// * `initiator` - Node initiating sync (will pull from responder)
/// * `responder` - Node responding to sync requests
///
/// # Returns
///
/// Statistics about the sync session.
///
/// # Example
///
/// ```ignore
/// let mut alice = SimNode::new("alice");
/// let mut bob = SimNode::new("bob");
///
/// // Set up state...
///
/// let stats = execute_hash_comparison_sync(&mut alice, &mut bob).await?;
/// assert_eq!(alice.root_hash(), bob.root_hash()); // Converged!
/// ```
pub async fn execute_hash_comparison_sync(
    initiator: &mut SimNode,
    responder: &SimNode,
) -> Result<SimSyncStats> {
    let (mut init_stream, mut resp_stream) = SimStream::pair();

    // Get root hashes for comparison
    let init_root = initiator.root_hash();
    let resp_root = responder.root_hash();

    // If already in sync, no work needed
    if init_root == resp_root {
        return Ok(SimSyncStats::default());
    }

    // Get stores and context info
    let initiator_store = initiator.storage().store();
    let responder_store = responder.storage().store();
    let initiator_context = initiator.context_id();
    let responder_context = responder.context_id();

    // Dummy identity for simulation
    let identity = PublicKey::from([0u8; 32]);

    // Config for initiator
    let config = HashComparisonConfig {
        remote_root_hash: resp_root,
    };

    // Run both sides concurrently using the PRODUCTION protocol
    let initiator_fut = async {
        HashComparisonProtocol::run_initiator(
            &mut init_stream,
            initiator_store,
            initiator_context,
            identity,
            config,
        )
        .await
    };

    let responder_fut = async {
        // Simulate manager behavior: receive first message for routing
        let first_msg = resp_stream
            .recv()
            .await?
            .ok_or_else(|| eyre::eyre!("Stream closed before first message"))?;

        // Extract first request data (like the manager does)
        let first_request = match first_msg {
            StreamMessage::Init {
                payload:
                    InitPayload::TreeNodeRequest {
                        node_id, max_depth, ..
                    },
                ..
            } => HashComparisonFirstRequest { node_id, max_depth },
            _ => bail!("Expected TreeNodeRequest Init message"),
        };

        // Now call the protocol with the extracted first request
        HashComparisonProtocol::run_responder(
            &mut resp_stream,
            responder_store,
            responder_context,
            identity,
            first_request,
        )
        .await
    };

    // Run both sides
    let (init_result, resp_result) = tokio::join!(initiator_fut, responder_fut);

    // Check for errors
    resp_result.wrap_err("responder failed")?;
    let stats = init_result.wrap_err("initiator failed")?;

    Ok(stats.into())
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sync_sim::actions::EntityMetadata;
    use crate::sync_sim::types::EntityId;
    use calimero_primitives::context::ContextId;

    /// Create a shared context ID for testing.
    fn shared_context() -> ContextId {
        ContextId::from(SimNode::DEFAULT_CONTEXT_ID)
    }

    #[tokio::test]
    async fn test_sync_empty_to_populated() {
        // Both nodes share the same context (as they would in production)
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Bob has entities, Alice is empty
        bob.insert_entity_with_metadata(
            EntityId::from_u64(1),
            b"hello".to_vec(),
            EntityMetadata::default(),
        );
        bob.insert_entity_with_metadata(
            EntityId::from_u64(2),
            b"world".to_vec(),
            EntityMetadata::default(),
        );

        assert_ne!(alice.root_hash(), bob.root_hash());

        // Sync
        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        // Verify convergence (Invariant I4)
        assert_eq!(
            alice.root_hash(),
            bob.root_hash(),
            "root hashes should match after sync"
        );
        assert!(
            stats.entities_transferred > 0,
            "should have transferred entities"
        );
        assert_eq!(
            alice.entity_count(),
            bob.entity_count(),
            "entity counts should match after sync"
        );
    }

    #[tokio::test]
    async fn test_sync_already_in_sync() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let bob = SimNode::new_in_context("bob", ctx);

        // Both empty = already in sync
        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        assert_eq!(stats.rounds, 0, "no rounds needed when already in sync");
    }

    #[tokio::test]
    async fn test_sync_partial_overlap() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Shared entity
        let shared_id = EntityId::from_u64(100);
        alice.insert_entity_with_metadata(shared_id, b"shared".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(shared_id, b"shared".to_vec(), EntityMetadata::default());

        // Bob-only entity
        bob.insert_entity_with_metadata(
            EntityId::from_u64(200),
            b"bob-only".to_vec(),
            EntityMetadata::default(),
        );

        // Sync
        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        // Verify Alice got Bob's entity
        assert!(stats.entities_transferred >= 1);
    }

    // =========================================================================
    // 3-Node Sync Tests (Issue: reports of 3-node sync failing)
    // =========================================================================

    /// Test 3 nodes where each has unique data, syncing in a chain: A→B→C→A
    ///
    /// This tests the scenario where:
    /// - A has entities 1-3
    /// - B has entities 4-6
    /// - C has entities 7-9
    ///
    /// After chain sync, all should have entities 1-9.
    #[tokio::test]
    async fn test_three_node_chain_sync() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);
        let mut charlie = SimNode::new_in_context("charlie", ctx);

        // Alice has entities 1-3
        for i in 1..=3 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("alice-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // Bob has entities 4-6
        for i in 4..=6 {
            bob.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("bob-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // Charlie has entities 7-9
        for i in 7..=9 {
            charlie.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("charlie-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // Initial state: all different
        assert_ne!(alice.root_hash(), bob.root_hash());
        assert_ne!(bob.root_hash(), charlie.root_hash());
        assert_ne!(alice.root_hash(), charlie.root_hash());

        println!("Initial state:");
        println!(
            "  Alice: {} entities, hash {:?}",
            alice.entity_count(),
            &alice.root_hash()[..4]
        );
        println!(
            "  Bob: {} entities, hash {:?}",
            bob.entity_count(),
            &bob.root_hash()[..4]
        );
        println!(
            "  Charlie: {} entities, hash {:?}",
            charlie.entity_count(),
            &charlie.root_hash()[..4]
        );

        // Step 1: Alice syncs FROM Bob (Alice pulls Bob's data)
        let stats1 = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("alice <- bob sync should succeed");
        println!("\nAfter Alice <- Bob:");
        println!(
            "  Alice: {} entities (transferred {})",
            alice.entity_count(),
            stats1.entities_transferred
        );

        // Step 2: Alice syncs FROM Charlie (Alice pulls Charlie's data)
        let stats2 = execute_hash_comparison_sync(&mut alice, &charlie)
            .await
            .expect("alice <- charlie sync should succeed");
        println!("\nAfter Alice <- Charlie:");
        println!(
            "  Alice: {} entities (transferred {})",
            alice.entity_count(),
            stats2.entities_transferred
        );

        // Now Alice has all 9 entities
        assert_eq!(alice.entity_count(), 9, "Alice should have all 9 entities");

        // Step 3: Bob syncs FROM Alice (Bob pulls Alice's data, which now includes Charlie's)
        let stats3 = execute_hash_comparison_sync(&mut bob, &alice)
            .await
            .expect("bob <- alice sync should succeed");
        println!("\nAfter Bob <- Alice:");
        println!(
            "  Bob: {} entities (transferred {})",
            bob.entity_count(),
            stats3.entities_transferred
        );

        // Step 4: Charlie syncs FROM Alice
        let stats4 = execute_hash_comparison_sync(&mut charlie, &alice)
            .await
            .expect("charlie <- alice sync should succeed");
        println!("\nAfter Charlie <- Alice:");
        println!(
            "  Charlie: {} entities (transferred {})",
            charlie.entity_count(),
            stats4.entities_transferred
        );

        // Final state: ALL should be converged
        println!("\nFinal state:");
        println!(
            "  Alice: {} entities, hash {:?}",
            alice.entity_count(),
            &alice.root_hash()[..4]
        );
        println!(
            "  Bob: {} entities, hash {:?}",
            bob.entity_count(),
            &bob.root_hash()[..4]
        );
        println!(
            "  Charlie: {} entities, hash {:?}",
            charlie.entity_count(),
            &charlie.root_hash()[..4]
        );

        // Verify convergence (Invariant I4)
        assert_eq!(
            alice.root_hash(),
            bob.root_hash(),
            "Alice and Bob should converge"
        );
        assert_eq!(
            bob.root_hash(),
            charlie.root_hash(),
            "Bob and Charlie should converge"
        );
        assert_eq!(alice.entity_count(), 9);
        assert_eq!(bob.entity_count(), 9);
        assert_eq!(charlie.entity_count(), 9);
    }

    /// Test 3 nodes with mesh sync (all pairs sync bidirectionally)
    ///
    /// This is a more thorough test: every node syncs with every other node.
    #[tokio::test]
    async fn test_three_node_mesh_sync() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);
        let mut charlie = SimNode::new_in_context("charlie", ctx);

        // Each node has 5 unique entities
        for i in 0..5 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(100 + i),
                format!("a-{i}").into_bytes(),
                EntityMetadata::default(),
            );
            bob.insert_entity_with_metadata(
                EntityId::from_u64(200 + i),
                format!("b-{i}").into_bytes(),
                EntityMetadata::default(),
            );
            charlie.insert_entity_with_metadata(
                EntityId::from_u64(300 + i),
                format!("c-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // Full mesh sync: each node pulls from both others
        // Round 1: Everyone pulls from everyone else
        execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("a<-b");
        execute_hash_comparison_sync(&mut alice, &charlie)
            .await
            .expect("a<-c");
        execute_hash_comparison_sync(&mut bob, &alice)
            .await
            .expect("b<-a");
        execute_hash_comparison_sync(&mut bob, &charlie)
            .await
            .expect("b<-c");
        execute_hash_comparison_sync(&mut charlie, &alice)
            .await
            .expect("c<-a");
        execute_hash_comparison_sync(&mut charlie, &bob)
            .await
            .expect("c<-b");

        // All should have 15 entities and same hash
        assert_eq!(alice.entity_count(), 15, "Alice should have 15 entities");
        assert_eq!(bob.entity_count(), 15, "Bob should have 15 entities");
        assert_eq!(
            charlie.entity_count(),
            15,
            "Charlie should have 15 entities"
        );

        assert_eq!(
            alice.root_hash(),
            bob.root_hash(),
            "Alice and Bob should match"
        );
        assert_eq!(
            bob.root_hash(),
            charlie.root_hash(),
            "Bob and Charlie should match"
        );
    }

    /// Test 3 nodes where one starts empty (fresh join scenario)
    #[tokio::test]
    async fn test_three_node_fresh_join() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);
        let mut charlie = SimNode::new_in_context("charlie", ctx); // Fresh, empty

        // Alice and Bob have shared state
        for i in 1..=5 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("shared-{i}").into_bytes(),
                EntityMetadata::default(),
            );
            bob.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("shared-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // Alice and Bob are synced
        assert_eq!(alice.root_hash(), bob.root_hash());
        // Charlie is empty
        assert_eq!(charlie.entity_count(), 0);

        // Charlie joins by syncing from Alice
        execute_hash_comparison_sync(&mut charlie, &alice)
            .await
            .expect("charlie <- alice sync should succeed");

        // Charlie should now match Alice and Bob
        assert_eq!(charlie.root_hash(), alice.root_hash());
        assert_eq!(charlie.entity_count(), 5);
    }

    /// Test 3 nodes with conflicting updates to same entity (CRDT merge)
    #[tokio::test]
    async fn test_three_node_crdt_conflict() {
        use calimero_primitives::crdt::CrdtType;

        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);
        let mut charlie = SimNode::new_in_context("charlie", ctx);

        // All three modify the same entity with different timestamps
        let conflict_id = EntityId::from_u64(999);

        alice.insert_entity_with_metadata(
            conflict_id,
            b"alice-version".to_vec(),
            EntityMetadata::new(CrdtType::lww_register("test"), 100), // oldest
        );
        bob.insert_entity_with_metadata(
            conflict_id,
            b"bob-version".to_vec(),
            EntityMetadata::new(CrdtType::lww_register("test"), 200), // middle
        );
        charlie.insert_entity_with_metadata(
            conflict_id,
            b"charlie-version".to_vec(),
            EntityMetadata::new(CrdtType::lww_register("test"), 300), // newest - should win
        );

        // Sync all to Alice (Alice pulls from both)
        execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("a<-b");
        execute_hash_comparison_sync(&mut alice, &charlie)
            .await
            .expect("a<-c");

        // Sync others from Alice
        execute_hash_comparison_sync(&mut bob, &alice)
            .await
            .expect("b<-a");
        execute_hash_comparison_sync(&mut charlie, &alice)
            .await
            .expect("c<-a");

        // All should converge to same hash (winner is charlie's version with ts=300)
        assert_eq!(alice.root_hash(), bob.root_hash(), "A and B should match");
        assert_eq!(bob.root_hash(), charlie.root_hash(), "B and C should match");

        // All should have exactly 1 entity
        assert_eq!(alice.entity_count(), 1);
        assert_eq!(bob.entity_count(), 1);
        assert_eq!(charlie.entity_count(), 1);
    }

    // =========================================================================
    // Bidirectional Sync Tests (Bug: initiator-has-more-data)
    // =========================================================================
    // These tests reproduce the bug from FIX-HASH-COMPARISON-SYNC.md where
    // HashComparison protocol fails to transfer data when the INITIATOR has
    // more data than the RESPONDER. The old protocol was pull-only.
    //
    // NOTE: Empty nodes (root_hash == [0;32]) use Snapshot, not HashComparison.
    // The bug only manifests when BOTH nodes have state but the initiator has
    // MORE entities. The root hashes differ but neither is zero.

    /// **BUG REPRODUCTION**: Both nodes have state but initiator has more.
    ///
    /// Mirrors the CI failure scenario:
    /// - Both nodes share a base entity (both initialized, has_state=true)
    /// - Node 1 (alice) then writes 10 additional seed entities
    /// - Alice initiates sync with Bob via HashComparison
    /// - Old behavior: alice pulls nothing (bob has no new data), alice's
    ///   local-only subtrees are ignored → bob never gets the seed data
    /// - Fixed behavior: alice detects local-only children and pushes them
    #[tokio::test]
    async fn test_initiator_has_more_data_push_to_peer() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Both share a base entity (so both have has_state=true, non-zero root)
        let base_id = EntityId::from_u64(1000);
        alice.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        // Sanity: both have the same base state
        assert_eq!(alice.root_hash(), bob.root_hash());

        // Now alice writes 10 additional seed entities
        for i in 1..=10 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("seed-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        assert_eq!(alice.entity_count(), 11); // 1 base + 10 seed
        assert_eq!(bob.entity_count(), 1); // 1 base only
        assert_ne!(alice.root_hash(), bob.root_hash());

        // Alice initiates sync WITH bob (alice is initiator = puller)
        // With the bug: alice gets nothing from bob, bob gets nothing.
        // With the fix: alice pushes her local-only data to bob.
        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        println!(
            "Stats: entities_pushed={}, entities_transferred={}, rounds={}",
            stats.entities_pushed, stats.entities_transferred, stats.rounds
        );

        // Bob should now have all 11 entities (1 base + 10 pushed by alice)
        assert_eq!(
            bob.entity_count(),
            11,
            "Bob should have all 11 entities after bidirectional sync"
        );

        // Alice should still have her 11 entities
        assert_eq!(alice.entity_count(), 11);

        // Root hashes should converge
        assert_eq!(
            alice.root_hash(),
            bob.root_hash(),
            "Root hashes should match after sync"
        );

        // Verify stats: entities were pushed, not pulled
        assert!(
            stats.entities_pushed >= 10,
            "Should have pushed at least 10 entities, got {}",
            stats.entities_pushed
        );
    }

    /// **BUG REPRODUCTION**: Both nodes have unique data, initiator has MORE.
    ///
    /// Alice has 1 shared + 10 unique, Bob has 1 shared + 3 unique.
    /// After single sync, both should have 1 + 10 + 3 = 14.
    #[tokio::test]
    async fn test_bidirectional_both_have_unique_data() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Shared base entity
        let base_id = EntityId::from_u64(1000);
        alice.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());

        // Alice adds 10 unique entities
        for i in 1..=10 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("alice-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // Bob adds 3 unique entities (completely different IDs)
        for i in 101..=103 {
            bob.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("bob-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        assert_eq!(alice.entity_count(), 11); // 1 shared + 10 unique
        assert_eq!(bob.entity_count(), 4); // 1 shared + 3 unique
        assert_ne!(alice.root_hash(), bob.root_hash());

        // Alice initiates sync WITH bob
        // - Alice should pull Bob's 3 unique entities (entities_transferred)
        // - Alice should push her 10 unique entities to Bob (entities_pushed)
        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        println!(
            "Stats: pushed={}, transferred={}, compared={}, skipped={}, rounds={}",
            stats.entities_pushed,
            stats.entities_transferred,
            stats.nodes_compared,
            stats.nodes_skipped,
            stats.rounds
        );

        // Alice should have all 14 entities (1 shared + 10 own + 3 from bob)
        assert_eq!(
            alice.entity_count(),
            14,
            "Alice should have 14 entities (1 shared + 10 own + 3 from bob)"
        );

        // Bob should also have all 14 entities (1 shared + 3 own + 10 from alice)
        assert_eq!(
            bob.entity_count(),
            14,
            "Bob should have 14 entities (1 shared + 3 own + 10 from alice)"
        );

        // Root hashes should converge
        assert_eq!(
            alice.root_hash(),
            bob.root_hash(),
            "Root hashes should match after bidirectional sync"
        );
    }

    /// Verify that single-direction pull sync still works (no regression).
    ///
    /// Both have base state, Bob has extra data, Alice initiates → Alice pulls.
    #[tokio::test]
    async fn test_pull_direction_still_works() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Shared base
        let base_id = EntityId::from_u64(1000);
        alice.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());

        // Bob has 5 extra entities
        for i in 1..=5 {
            bob.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("bob-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        assert_eq!(alice.entity_count(), 6, "Alice should have 6 entities");
        assert_eq!(alice.root_hash(), bob.root_hash(), "Hashes should match");
        assert!(
            stats.entities_transferred >= 5,
            "Should have transferred at least 5 entities"
        );
    }

    /// 4-node scenario mimicking the fuzzy test with initialized nodes.
    ///
    /// All nodes share a base entity. Node 1 then writes seed data.
    /// After sync rounds, all should converge.
    #[tokio::test]
    async fn test_four_node_seed_data_propagation() {
        let ctx = shared_context();
        let mut node1 = SimNode::new_in_context("node1", ctx);
        let mut node2 = SimNode::new_in_context("node2", ctx);
        let mut node3 = SimNode::new_in_context("node3", ctx);
        let mut node4 = SimNode::new_in_context("node4", ctx);

        // All share a base entity (all initialized)
        let base_id = EntityId::from_u64(1000);
        for node in [&mut node1, &mut node2, &mut node3, &mut node4] {
            node.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        }

        // Node 1 writes 10 seed values (exactly like the fuzzy test)
        for i in 1..=10 {
            node1.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("seed-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        assert_eq!(node1.entity_count(), 11);
        assert_eq!(node2.entity_count(), 1);
        assert_eq!(node3.entity_count(), 1);
        assert_eq!(node4.entity_count(), 1);

        // Node 1 initiates sync with each other node (push direction)
        execute_hash_comparison_sync(&mut node1, &node2)
            .await
            .expect("n1<->n2");
        execute_hash_comparison_sync(&mut node1, &node3)
            .await
            .expect("n1<->n3");
        execute_hash_comparison_sync(&mut node1, &node4)
            .await
            .expect("n1<->n4");

        // All nodes should have the seed data after one round
        assert_eq!(
            node2.entity_count(),
            11,
            "Node 2 should have 11 entities after sync with node 1"
        );
        assert_eq!(
            node3.entity_count(),
            11,
            "Node 3 should have 11 entities after sync with node 1"
        );
        assert_eq!(
            node4.entity_count(),
            11,
            "Node 4 should have 11 entities after sync with node 1"
        );

        // All should converge to same root hash
        assert_eq!(node1.root_hash(), node2.root_hash());
        assert_eq!(node1.root_hash(), node3.root_hash());
        assert_eq!(node1.root_hash(), node4.root_hash());
    }

    /// Minimal reproduction: initiator has 1 extra entity beyond shared base.
    #[tokio::test]
    async fn test_single_entity_push_with_shared_base() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Shared base
        let base_id = EntityId::from_u64(1000);
        alice.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());

        // Alice adds one extra entity
        alice.insert_entity_with_metadata(
            EntityId::from_u64(42),
            b"hello".to_vec(),
            EntityMetadata::default(),
        );

        assert_eq!(alice.entity_count(), 2);
        assert_eq!(bob.entity_count(), 1);

        let stats = execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("sync should succeed");

        assert_eq!(bob.entity_count(), 2, "Bob should have 2 entities");
        assert_eq!(alice.root_hash(), bob.root_hash());
        assert!(
            stats.entities_pushed >= 1,
            "Should have pushed at least 1 entity, got {}",
            stats.entities_pushed,
        );
    }

    /// CRDT conflict during push: alice pushes entity that bob already has
    /// with a different value. LWW merge should resolve deterministically.
    #[tokio::test]
    async fn test_crdt_conflict_during_push() {
        use calimero_primitives::crdt::CrdtType;

        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Shared base
        let base_id = EntityId::from_u64(1000);
        alice.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());

        // Both have the conflict entity, different values and timestamps
        let conflict_id = EntityId::from_u64(42);
        alice.insert_entity_with_metadata(
            conflict_id,
            b"alice-wins".to_vec(),
            EntityMetadata::new(CrdtType::lww_register("test"), 200), // newer
        );
        bob.insert_entity_with_metadata(
            conflict_id,
            b"bob-loses".to_vec(),
            EntityMetadata::new(CrdtType::lww_register("test"), 100), // older
        );

        // Alice also has extra entities to trigger push
        for i in 1..=3 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("alice-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        assert_ne!(alice.root_hash(), bob.root_hash());

        // Round 1: Alice initiates → pushes her data to bob, pulls bob's
        execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("a->b should succeed");

        // Round 2: Bob initiates → pushes merged state back to alice
        execute_hash_comparison_sync(&mut bob, &alice)
            .await
            .expect("b->a should succeed");

        // After two rounds, both should converge
        assert_eq!(
            alice.root_hash(),
            bob.root_hash(),
            "Should converge after bidirectional CRDT merge"
        );

        // Both should have: 1 base + 1 conflict (merged) + 3 unique = 5
        assert_eq!(alice.entity_count(), 5);
        assert_eq!(bob.entity_count(), 5);
    }

    /// Symmetric sync: A→B then B→A should produce identical state.
    #[tokio::test]
    async fn test_symmetric_sync_converges() {
        let ctx = shared_context();
        let mut alice = SimNode::new_in_context("alice", ctx);
        let mut bob = SimNode::new_in_context("bob", ctx);

        // Shared base
        let base_id = EntityId::from_u64(1000);
        alice.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());
        bob.insert_entity_with_metadata(base_id, b"base".to_vec(), EntityMetadata::default());

        // Each has unique data
        for i in 1..=5 {
            alice.insert_entity_with_metadata(
                EntityId::from_u64(i),
                format!("a-{i}").into_bytes(),
                EntityMetadata::default(),
            );
            bob.insert_entity_with_metadata(
                EntityId::from_u64(100 + i),
                format!("b-{i}").into_bytes(),
                EntityMetadata::default(),
            );
        }

        // A→B: alice initiates, pushes her data, pulls bob's
        execute_hash_comparison_sync(&mut alice, &bob)
            .await
            .expect("a->b");

        // Should already converge after one bidirectional sync
        assert_eq!(alice.entity_count(), 11); // 1 base + 5 alice + 5 bob
        assert_eq!(bob.entity_count(), 11);
        assert_eq!(alice.root_hash(), bob.root_hash());

        // B→A: bob initiates (should be no-op since already converged)
        let stats = execute_hash_comparison_sync(&mut bob, &alice)
            .await
            .expect("b->a");

        assert_eq!(stats.entities_transferred, 0, "no-op when already synced");
        assert_eq!(stats.entities_pushed, 0, "no-op when already synced");
        assert_eq!(alice.root_hash(), bob.root_hash());
    }
}