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
//! HashComparison sync protocol responder (CIP §2.3 Rules 3, 7).
//!
//! This module contains the responder side of the HashComparison protocol.
//! The initiator logic is in `hash_comparison_protocol.rs`.
//!
//! # Responder Flow
//!
//! ```text
//! Initiator Responder (this module)
//! │ │
//! │ ── TreeNodeRequest (root) ───────────────► │
//! │ │ handle_tree_node_request
//! │ ◄── TreeNodeResponse (children hashes) ─── │
//! │ │
//! │ ── TreeNodeRequest (child) ──────────────► │
//! │ ◄── TreeNodeResponse ─────────────────────│
//! │ │
//! │ ...repeat until initiator closes stream... │
//! └────────────────────────────────────────────┘
//! ```
use crate::sync::helpers::handle_entity_push;
use calimero_crypto::Nonce;
use calimero_node_primitives::sync::{
create_runtime_env, InitPayload, LeafMetadata, MessagePayload, StreamMessage, SyncTransport,
TreeLeafData, TreeNode, TreeNodeResponse, MAX_NODES_PER_RESPONSE,
};
use calimero_primitives::context::ContextId;
use calimero_primitives::crdt::CrdtType;
use calimero_storage::address::Id;
use calimero_storage::env::{with_runtime_env, RuntimeEnv};
use calimero_storage::index::Index;
use calimero_storage::interface::Interface;
use calimero_storage::store::MainStorage;
use eyre::Result;
use tracing::{debug, info, trace, warn};
use super::manager::SyncManager;
/// Maximum depth allowed in TreeNodeRequest.
///
/// Prevents malicious peers from requesting expensive deep traversals.
pub const MAX_REQUEST_DEPTH: u8 = 16;
use super::hash_comparison_protocol::MAX_HASH_COMPARISON_REQUESTS;
// =============================================================================
// SyncManager Responder Implementation
// =============================================================================
impl SyncManager {
/// Handle incoming TreeNodeRequest from a peer.
///
/// This is the responder side of HashComparison sync.
/// Handles the first request (already parsed) and then loops to handle
/// subsequent requests until the stream closes.
///
/// # Arguments
///
/// * `context_id` - Context being synchronized
/// * `first_node_id` - Node ID from the first request (already parsed)
/// * `first_max_depth` - Max depth from the first request
/// * `transport` - Transport for sending/receiving messages
/// * `_nonce` - Reserved for future encrypted sync (currently unused as each
/// response generates its own nonce via `generate_nonce()`)
pub async fn handle_tree_node_request<T: SyncTransport>(
&self,
context_id: ContextId,
first_node_id: [u8; 32],
first_max_depth: Option<u8>,
transport: &mut T,
_nonce: Nonce,
) -> Result<()> {
info!(%context_id, "Starting HashComparison responder");
// Get our identity for RuntimeEnv - look up from context members
let identities = self
.context_client
.get_context_members(&context_id, Some(true));
let our_identity = match crate::utils::choose_stream(identities, &mut rand::thread_rng())
.await
.transpose()?
{
Some((identity, _)) => identity,
None => {
warn!(%context_id, "No owned identity for context, cannot respond to TreeNodeRequest");
// Send not-found response
let mut sqx = super::tracking::Sequencer::default();
let msg = StreamMessage::Message {
sequence_id: sqx.next(),
payload: MessagePayload::TreeNodeResponse {
nodes: vec![],
not_found: true,
},
next_nonce: super::helpers::generate_nonce(),
};
transport.send(&msg).await?;
return Ok(());
}
};
let mut sqx = super::tracking::Sequencer::default();
let mut requests_handled = 0u64;
// Create RuntimeEnv once for all requests (optimization: avoids per-request allocation)
let datastore = self.context_client.datastore_handle().into_inner();
let runtime_env = create_runtime_env(&datastore, context_id, our_identity);
// Handle the first request (already parsed by handle_sync_request)
{
let clamped_depth = first_max_depth.map(|d| d.min(MAX_REQUEST_DEPTH));
let response = self
.build_tree_node_response(context_id, &first_node_id, clamped_depth, &runtime_env)
.await?;
let msg = StreamMessage::Message {
sequence_id: sqx.next(),
payload: MessagePayload::TreeNodeResponse {
nodes: response.nodes,
not_found: response.not_found,
},
next_nonce: super::helpers::generate_nonce(),
};
transport.send(&msg).await?;
requests_handled += 1;
}
// Loop to handle subsequent requests until stream closes
loop {
// DoS protection: limit total requests per session
if requests_handled >= MAX_HASH_COMPARISON_REQUESTS {
warn!(
%context_id,
requests_handled,
"Request limit reached, closing responder"
);
break;
}
let Some(request) = transport.recv().await? else {
debug!(%context_id, requests_handled, "Stream closed, responder done");
break;
};
let StreamMessage::Init { payload, .. } = request else {
debug!(%context_id, "Received non-Init message, ending responder");
break;
};
match payload {
InitPayload::TreeNodeRequest {
node_id, max_depth, ..
} => {
trace!(
%context_id,
node_id = %hex::encode(node_id),
?max_depth,
"Handling subsequent TreeNodeRequest"
);
let clamped_depth = max_depth.map(|d| d.min(MAX_REQUEST_DEPTH));
let response = self
.build_tree_node_response(context_id, &node_id, clamped_depth, &runtime_env)
.await?;
let msg = StreamMessage::Message {
sequence_id: sqx.next(),
payload: MessagePayload::TreeNodeResponse {
nodes: response.nodes,
not_found: response.not_found,
},
next_nonce: super::helpers::generate_nonce(),
};
transport.send(&msg).await?;
requests_handled += 1;
}
InitPayload::EntityPush { entities, .. } => {
let entity_count = entities.len();
trace!(%context_id, entity_count, "Handling EntityPush from initiator");
let outcome =
handle_entity_push(&datastore, &runtime_env, context_id, &entities);
let applied = outcome.applied;
// Dispatch any deferred root-entity merges through
// the WASM module before ACKing the push — keeps
// the responder side symmetric with the initiator,
// so a bidirectional push of root state still
// converges. Identical helper to the one HC /
// LevelWise initiators use.
if !outcome.deferred_root_merges.is_empty() {
super::protocol_selector::dispatch_deferred_root_merges(
&self.context_client,
&datastore,
context_id,
our_identity,
&outcome.deferred_root_merges,
)
.await;
}
let msg = StreamMessage::Message {
sequence_id: sqx.next(),
payload: MessagePayload::EntityPushAck {
applied_count: applied,
},
next_nonce: super::helpers::generate_nonce(),
};
transport.send(&msg).await?;
requests_handled += 1;
info!(
%context_id,
applied,
deferred_root_merges = outcome.deferred_root_merges.len(),
total = entity_count,
"Applied pushed entities via CRDT merge"
);
}
_ => {
debug!(%context_id, "Received unknown payload, ending responder");
break;
}
}
}
info!(%context_id, requests_handled, "HashComparison responder complete");
Ok(())
}
/// Build TreeNodeResponse for a requested node.
///
/// Uses the real Merkle tree Index via RuntimeEnv bridge.
///
/// # Arguments
///
/// * `context_id` - Context being synchronized
/// * `node_id` - ID of the node to retrieve
/// * `max_depth` - Maximum depth to traverse (clamped externally)
/// * `runtime_env` - Pre-created RuntimeEnv (shared across requests for efficiency)
async fn build_tree_node_response(
&self,
context_id: ContextId,
node_id: &[u8; 32],
max_depth: Option<u8>,
runtime_env: &RuntimeEnv,
) -> Result<TreeNodeResponse> {
// Get context to check if this is a root request
let context = self.context_client.get_context(&context_id)?;
let Some(context) = context else {
debug!(
%context_id,
"Context not found for TreeNodeRequest"
);
return Ok(TreeNodeResponse::not_found());
};
// Determine if this is a root request (node_id matches root_hash)
let is_root_request = node_id == context.root_hash.as_ref();
// Get the local node
let local_node = with_runtime_env(runtime_env.clone(), || {
self.get_local_tree_node_from_index(context_id, node_id, is_root_request)
})?;
let Some(node) = local_node else {
debug!(
%context_id,
node_id = %hex::encode(node_id),
"TreeNodeRequest: node not found"
);
return Ok(TreeNodeResponse::not_found());
};
let mut nodes = vec![node.clone()];
// If max_depth > 0 and this is an internal node, include children
let depth = max_depth.unwrap_or(0);
if depth > 0 && node.is_internal() {
// Include child nodes
for child_id in &node.children {
let child_node = with_runtime_env(runtime_env.clone(), || {
self.get_local_tree_node_from_index(context_id, child_id, false)
})?;
if let Some(child) = child_node {
nodes.push(child);
// Limit to avoid oversized responses
if nodes.len() >= MAX_NODES_PER_RESPONSE {
break;
}
}
}
}
debug!(
%context_id,
node_id = %hex::encode(node_id),
nodes_count = nodes.len(),
"TreeNodeRequest: returning nodes"
);
Ok(TreeNodeResponse::new(nodes))
}
/// Get local tree node from the real Merkle tree Index.
///
/// Must be called within `with_runtime_env` context.
fn get_local_tree_node_from_index(
&self,
context_id: ContextId,
node_id: &[u8; 32],
is_root_request: bool,
) -> Result<Option<TreeNode>> {
// Determine the entity ID to look up
let entity_id = if is_root_request {
// For root request, look up Id::root() (which equals context_id)
Id::new(*context_id.as_ref())
} else {
// For child requests, node_id IS the entity ID
Id::new(*node_id)
};
// Get the entity's index from the Merkle tree
let index = match Index::<MainStorage>::get_index(entity_id) {
Ok(Some(idx)) => idx,
Ok(None) => return Ok(None),
Err(e) => {
warn!(
%context_id,
entity_id = %entity_id,
error = %e,
"Failed to get index for entity"
);
return Ok(None);
}
};
// Get hashes from the index
let full_hash = index.full_hash();
// Get children from the index
let children_ids: Vec<[u8; 32]> = index
.children()
.map(|children| {
children
.iter()
.map(|child| *child.id().as_bytes())
.collect()
})
.unwrap_or_default();
// Determine if this is a leaf or internal node
if children_ids.is_empty() {
// Leaf node - try to get entity data
if let Some(entry_data) = Interface::<MainStorage>::find_by_id_raw(entity_id) {
let crdt_type = index.metadata.crdt_type.clone().unwrap_or_else(|| {
// No CRDT type ("opaque" leaf — e.g. the `Root<T>` state entry).
// Emit a real *leaf* (not a malformed empty `internal` node, which
// the peer's `TreeNode::is_valid()` rejects) carrying a synthetic
// LWW wire type — merge-equivalent to `None` and Merkle-hash-neutral.
// Same Model-S fix as `hash_comparison_protocol.rs` (see
// `OPAQUE_LEAF_CRDT_TYPE_NAME` there + opaque-leaf-sync design spec).
trace!(%entity_id, "opaque leaf, synthesised LWW wire type for sync");
CrdtType::lww_register(
super::hash_comparison_protocol::OPAQUE_LEAF_CRDT_TYPE_NAME,
)
});
// Carry the leaf's Merkle parent_id on the wire so the
// initiator can reconstruct the entity at its proper Merkle
// position. Pre-fix this field was unconditionally `None`
// and the initiator's apply path fell back to "direct child
// of context root" — corrupting the topology for any nested-
// collection entity (every `Root<T>`-wrapped app, which is
// ~all of them). See the design spec for the wire-format
// analysis: the field already exists on `LeafMetadata`, just
// wasn't populated.
let mut metadata =
LeafMetadata::new(crdt_type, index.metadata.updated_at(), [0u8; 32])
.with_created_at(index.metadata.created_at());
if let Some(parent_id) = index.parent_id() {
metadata = metadata.with_parent(*parent_id.as_bytes());
}
if let Some(auth) = crate::sync::helpers::wire_authorization_for(&index.metadata) {
metadata = metadata.with_authorization(auth);
}
let leaf_data = TreeLeafData::new(*entity_id.as_bytes(), entry_data, metadata);
Ok(Some(TreeNode::leaf(
*entity_id.as_bytes(),
full_hash,
leaf_data,
)))
} else {
// Index exists but no entry data - treat as internal node with no children
// This can happen for collection containers
Ok(Some(TreeNode::internal(
*entity_id.as_bytes(),
full_hash,
vec![],
)))
}
} else {
// Internal node with children
Ok(Some(TreeNode::internal(
*entity_id.as_bytes(),
full_hash,
children_ids,
)))
}
}
}