Skip to main content

Module peer_sync_protocol

Module peer_sync_protocol 

Source
Expand description

Peer Sync Protocol — bidirectional state synchronisation using vector clocks and CRDTs.

PeerSyncProtocol implements a production-grade, conflict-free replicated data type (CRDT) engine. Each node maintains a VectorClock that it increments on every local write. Remote operations are applied through PeerSyncProtocol::apply_remote_op, which resolves write–write conflicts according to a pluggable ConflictPolicy.

§Design goals

  • Convergence: any two nodes that have received the same set of operations end up in identical state regardless of delivery order.
  • No data loss by default: LastWriteWins and HighestClock policies always keep one version; MergeBytes retains both.
  • Tombstoning: deleted keys are permanently suppressed so late-arriving puts for the same key are silently discarded.
  • Delta sync: PeerSyncProtocol::generate_delta returns only the entries that a remote peer is missing, batched into a single SyncOperation::Merge.

§Example

use ipfrs_network::peer_sync_protocol::{
    PeerSyncProtocol, ConflictPolicy, SyncOperation, VectorClock,
};

let mut node_a = PeerSyncProtocol::new("node-a".to_string(), ConflictPolicy::LastWriteWins);
let entry = node_a.local_put("greeting".to_string(), b"hello".to_vec(), 1000);

let mut node_b = PeerSyncProtocol::new("node-b".to_string(), ConflictPolicy::LastWriteWins);
let op = SyncOperation::Put { entry };
node_b.apply_remote_op("node-a", op, 1001).unwrap();

assert_eq!(node_b.get("greeting").unwrap().value, b"hello");

Structs§

PeerSyncProtocol
Bidirectional state-synchronisation protocol backed by vector clocks and CRDTs.
PspSyncStats
A snapshot of key metrics for a PeerSyncProtocol instance.
SyncEntry
A key-value record annotated with causal metadata.
SyncState
The replicated state held by a single node.
VectorClock
A logical clock that tracks causality per node.

Enums§

ConflictPolicy
Strategy used to resolve write–write conflicts (concurrent updates to the same key).
SyncError
Errors returned by PeerSyncProtocol operations.
SyncOperation
An atomic operation that can be applied to a PeerSyncProtocol.