feat(sync-sim): add SimStorage with real Merkle tree for protocol testing#1968
feat(sync-sim): add SimStorage with real Merkle tree for protocol testing#1968
Conversation
There was a problem hiding this comment.
🤖 AI Code Reviewer
Reviewed by 3 agents | Quality score: 95% | Review time: 194.8s
💡 4 suggestions, 📝 3 nitpicks. See inline comments.
🤖 Generated by AI Code Reviewer | Review ID: review-d5aafdf7
…tocol testing Replaces the flat `DigestCache` HashMap in `SimNode` with `SimStorage`, which uses the real `calimero-storage::Index<MainStorage>` implementation backed by `InMemoryDB`. This enables accurate simulation of sync protocols that depend on tree structure (e.g., HashComparison). Key changes: - Add `SimStorage` with in-memory Merkle tree using `Store + InMemoryDB` - Add `RuntimeEnv` bridge to connect storage Key operations - Update `SimNode` to use hybrid storage: real tree + metadata cache - Add `insert_entity_hierarchical()` for creating proper tree depth - Make `Index::get_index()` and `get_children_of()` public for traversal - Add tree structure verification tests for protocol selection - Fix: prevent self-referencing cycle in hierarchical insertion The entity counting now correctly excludes intermediate tree nodes, and `iter_entities()` returns only "real" entities (with metadata). Tree depth now affects protocol selection: - SubtreePrefetch scenarios have max_depth > 3 - LevelWise scenarios have max_depth <= 2 Spec reference: Simulation Framework Spec §5, §7, §11 Co-authored-by: cursor[bot] <cursor@calimero.network>
97b514e to
6ebdebd
Compare
There was a problem hiding this comment.
🤖 AI Code Reviewer
Reviewed by 3 agents | Quality score: 92% | Review time: 265.6s
💡 6 suggestions, 📝 2 nitpicks. See inline comments.
🤖 Generated by AI Code Reviewer | Review ID: review-4c66ded3
There was a problem hiding this comment.
🤖 AI Code Reviewer
Reviewed by 3 agents | Quality score: 100% | Review time: 229.6s
🟡 1 warnings, 💡 3 suggestions. See inline comments.
🤖 Generated by AI Code Reviewer | Review ID: review-ae5b05a0
This comment has been minimized.
This comment has been minimized.
- Delegate apply_storage_op Insert/Update to insert_entity_with_metadata to avoid duplicating dual-write logic (cursor bugbot feedback) - Extract magic number 24 to MAX_HIERARCHICAL_DEPTH constant with docs - Add comprehensive documentation for max_depth() semantics explaining the difference between storage-level (root-inclusive) and protocol-level (root-exclusive) depth values
There was a problem hiding this comment.
🤖 AI Code Reviewer
Reviewed by 3 agents | Quality score: 89% | Review time: 235.6s
🟡 1 warnings, 💡 3 suggestions, 📝 2 nitpicks. See inline comments.
🤖 Generated by AI Code Reviewer | Review ID: review-32829545
|
Bugbot Autofix prepared fixes for 2 of the 2 bugs found in the latest run.
Or push these changes by commenting: |

Summary
Adds
SimStorageinfrastructure to the simulation framework, replacing the flatDigestCacheHashMap with a real Merkle tree implementation. This enables accurate simulation of sync protocols that depend on tree structure (e.g., HashComparison).Key changes:
SimStoragewith in-memory Merkle tree usingStore + InMemoryDBRuntimeEnvbridge to connect storage Key operationsSimNodeto use hybrid storage: real tree + metadata cacheinsert_entity_hierarchical()for creating proper tree depthIndex::get_index()andget_children_of()public for traversalEntity semantics improvements:
iter_entities()returns only "real" entities (with metadata)has_entity()checks metadata cache, not storage (excludes intermediate nodes)Protocol selection now uses real tree depth:
max_depth > 3max_depth <= 2Test Plan
Spec Reference
Note
Medium Risk
Moderate risk because it changes core simulation state/digest semantics and introduces new tree-building logic, plus exposes additional
calimero-storage::IndexAPIs that could affect downstream users.Overview
Simulation state now uses a real Merkle tree.
SimNodereplaces theDigestCache/HashMap-backed model with a newSimStorage(in-memoryStore + InMemoryDBbridged viaRuntimeEnv) and derivesstate_digest/root_hashfrom the storage root hash.Entity semantics were tightened. The simulation now tracks “real” entities via a separate metadata cache, adds
iter_entities()/entity_ids()and hierarchical insertion (insert_entity_hierarchical) to create intermediate tree nodes without counting them as entities; scenarios/assertions were updated accordingly and new tests verify depth-sensitive protocol negotiation.Storage traversal support was opened up.
crates/storagemakesIndex::get_index()andIndex::get_children_of()public and addsEntityIndexaccessor methods to support Merkle tree traversal from the simulation.Written by Cursor Bugbot for commit 22815bb. This will update automatically on new commits. Configure here.