Skip to main content

Module protocol_trait

Module protocol_trait 

Source
Expand description

Common trait for sync protocol implementations.

This module defines the SyncProtocolExecutor trait that all sync protocols implement. This enables:

  • Protocol implementation details contained within each protocol module
  • Common interface for SyncManager to invoke any protocol
  • Same code path for production and simulation (only Store backend differs)

§Architecture

┌─────────────────────────────────────────────────────────────────┐
│                     SyncProtocolExecutor trait                   │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐ │
│  │ HashComparison  │  │    Snapshot     │  │   LevelWise     │ │
│  │    Protocol     │  │    Protocol     │  │    Protocol     │ │
│  └────────┬────────┘  └────────┬────────┘  └────────┬────────┘ │
│           │                    │                    │          │
│           └────────────────────┼────────────────────┘          │
│                                │                               │
│                    ┌───────────┴───────────┐                   │
│                    │   SyncTransport       │                   │
│                    │ (Stream or SimStream) │                   │
│                    └───────────────────────┘                   │
└─────────────────────────────────────────────────────────────────┘

§Responder Dispatch Model

The SyncManager dispatches incoming sync requests using this flow:

  1. Manager receives stream and calls recv() to get the first message
  2. Manager matches on InitPayload to determine which protocol to use
  3. Manager extracts protocol-specific data from the first message
  4. Manager calls run_responder() passing the extracted data via ResponderInit

This design is necessary because the manager must peek at the first message for routing, but once consumed it cannot be “un-read”. The ResponderInit associated type allows each protocol to declare what data it needs from the first request.

§Example

use calimero_node_primitives::sync::{SyncProtocolExecutor, HashComparisonProtocol};

// Production initiator
let mut transport = StreamTransport::new(&mut stream);
let stats = HashComparisonProtocol::run_initiator(
    &mut transport,
    &store,
    context_id,
    identity,
    HashComparisonConfig { remote_root_hash },
).await?;

// Production responder (manager extracts first request data)
let first_request = HashComparisonFirstRequest { node_id, max_depth };
HashComparisonProtocol::run_responder(
    &mut transport,
    &store,
    context_id,
    identity,
    first_request,
).await?;

Traits§

SyncProtocolExecutor
Trait for sync protocol implementations.