qudag_protocol/
synchronization.rs

1//! Protocol state synchronization implementation.
2
3use crate::state::ProtocolState;
4use thiserror::Error;
5
6/// Errors that can occur during synchronization.
7#[derive(Debug, Error)]
8pub enum SyncError {
9    /// Synchronization failed
10    #[error("Synchronization failed")]
11    SyncFailed,
12
13    /// Invalid state received
14    #[error("Invalid state received")]
15    InvalidState,
16
17    /// Version mismatch
18    #[error("Version mismatch")]
19    VersionMismatch,
20}
21
22/// Synchronization mode.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum SyncMode {
25    /// Full state sync
26    Full,
27
28    /// Incremental state sync
29    Incremental,
30
31    /// Partial state sync
32    Partial,
33}
34
35/// Synchronization configuration.
36#[derive(Debug, Clone)]
37pub struct SyncConfig {
38    /// Sync mode
39    pub mode: SyncMode,
40
41    /// Sync interval in seconds
42    pub interval: u64,
43
44    /// Maximum sync attempts
45    pub max_attempts: u32,
46}
47
48/// State synchronization trait defining the interface for sync operations.
49pub trait StateSynchronization {
50    /// Initialize synchronization with configuration.
51    fn init(config: SyncConfig) -> Result<(), SyncError>;
52
53    /// Start state synchronization.
54    fn start_sync(&mut self) -> Result<(), SyncError>;
55
56    /// Stop state synchronization.
57    fn stop_sync(&mut self) -> Result<(), SyncError>;
58
59    /// Request state from peers.
60    fn request_state(&mut self) -> Result<ProtocolState, SyncError>;
61
62    /// Send state to peers.
63    fn send_state(&mut self, state: &ProtocolState) -> Result<(), SyncError>;
64
65    /// Resolve state conflicts.
66    fn resolve_conflicts(&mut self, states: Vec<ProtocolState>)
67        -> Result<ProtocolState, SyncError>;
68}