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
//! Protocol state synchronization implementation.
use crate::state::ProtocolState;
use thiserror::Error;
/// Errors that can occur during synchronization.
#[derive(Debug, Error)]
pub enum SyncError {
/// Synchronization failed
#[error("Synchronization failed")]
SyncFailed,
/// Invalid state received
#[error("Invalid state received")]
InvalidState,
/// Version mismatch
#[error("Version mismatch")]
VersionMismatch,
}
/// Synchronization mode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SyncMode {
/// Full state sync
Full,
/// Incremental state sync
Incremental,
/// Partial state sync
Partial,
}
/// Synchronization configuration.
#[derive(Debug, Clone)]
pub struct SyncConfig {
/// Sync mode
pub mode: SyncMode,
/// Sync interval in seconds
pub interval: u64,
/// Maximum sync attempts
pub max_attempts: u32,
}
/// State synchronization trait defining the interface for sync operations.
pub trait StateSynchronization {
/// Initialize synchronization with configuration.
fn init(config: SyncConfig) -> Result<(), SyncError>;
/// Start state synchronization.
fn start_sync(&mut self) -> Result<(), SyncError>;
/// Stop state synchronization.
fn stop_sync(&mut self) -> Result<(), SyncError>;
/// Request state from peers.
fn request_state(&mut self) -> Result<ProtocolState, SyncError>;
/// Send state to peers.
fn send_state(&mut self, state: &ProtocolState) -> Result<(), SyncError>;
/// Resolve state conflicts.
fn resolve_conflicts(&mut self, states: Vec<ProtocolState>)
-> Result<ProtocolState, SyncError>;
}