qudag-protocol 0.5.0

Protocol implementation for QuDAG - Orchestrates crypto, DAG, and network components
Documentation
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Protocol state machine implementation.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant, SystemTime};
use thiserror::Error;
use tracing::{debug, error, warn};
use uuid::Uuid;

use crate::message::{HandshakeType, Message, MessageType, ProtocolVersion};

/// Errors that can occur during state operations.
#[derive(Debug, Error)]
pub enum StateError {
    /// Invalid state transition
    #[error("Invalid state transition from {from:?} to {to:?}")]
    InvalidTransition {
        from: ProtocolState,
        to: ProtocolState,
    },

    /// State synchronization failed
    #[error("State synchronization failed: {reason}")]
    SyncFailed { reason: String },

    /// Invalid state data
    #[error("Invalid state data: {reason}")]
    InvalidData { reason: String },

    /// State operation timeout
    #[error("State operation timed out after {timeout:?}")]
    Timeout { timeout: Duration },

    /// Session not found
    #[error("Session not found: {session_id}")]
    SessionNotFound { session_id: Uuid },

    /// Protocol version mismatch
    #[error("Protocol version mismatch: expected {expected:?}, got {actual:?}")]
    VersionMismatch {
        expected: ProtocolVersion,
        actual: ProtocolVersion,
    },
}

/// Protocol state enumeration with detailed substates
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ProtocolState {
    /// Initial state - node starting up
    #[default]
    Initial,

    /// Handshake states
    Handshake(HandshakeState),

    /// Active protocol states
    Active(ActiveState),

    /// Synchronization states
    Synchronizing(SyncState),

    /// Error states
    Error(ErrorState),

    /// Shutting down
    Shutdown,
}

/// Handshake state substates
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HandshakeState {
    /// Waiting for handshake initiation
    Waiting,
    /// Handshake in progress - sent init, waiting for response
    InProgress,
    /// Received handshake response, processing
    Processing,
    /// Handshake completed successfully
    Completed,
    /// Handshake failed
    Failed,
}

/// Active protocol substates
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActiveState {
    /// Normal operation - processing messages
    Normal,
    /// High load - prioritizing critical messages
    HighLoad,
    /// Degraded - some components not functioning optimally
    Degraded,
}

/// Synchronization substates
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyncState {
    /// Requesting state from peers
    Requesting,
    /// Receiving state data
    Receiving,
    /// Applying received state
    Applying,
    /// Verifying synchronized state
    Verifying,
}

/// Error state types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ErrorState {
    /// Network connectivity issues
    NetworkError,
    /// Consensus failure
    ConsensusError,
    /// Cryptographic error
    CryptoError,
    /// Resource exhaustion
    ResourceError,
    /// Internal protocol error
    InternalError,
}

/// Session information for tracking peer connections
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionInfo {
    /// Session identifier
    pub id: Uuid,
    /// Peer identifier
    pub peer_id: Vec<u8>,
    /// Protocol version negotiated
    pub protocol_version: ProtocolVersion,
    /// Session state
    pub state: ProtocolState,
    /// Session start time
    pub started_at: SystemTime,
    /// Last activity timestamp
    pub last_activity: SystemTime,
    /// Session capabilities
    pub capabilities: Vec<String>,
    /// Session metrics
    pub metrics: SessionMetrics,
}

/// Session performance metrics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SessionMetrics {
    /// Messages sent
    pub messages_sent: u64,
    /// Messages received
    pub messages_received: u64,
    /// Bytes sent
    pub bytes_sent: u64,
    /// Bytes received
    pub bytes_received: u64,
    /// Average response time
    pub avg_response_time: Duration,
    /// Error count
    pub error_count: u64,
}

/// Protocol state manager
#[derive(Debug)]
pub struct ProtocolStateMachine {
    /// Current protocol state
    current_state: ProtocolState,
    /// Previous state for rollback
    previous_state: Option<ProtocolState>,
    /// State transition history
    state_history: Vec<StateTransition>,
    /// Active sessions
    sessions: HashMap<Uuid, SessionInfo>,
    /// State machine start time
    started_at: Instant,
    /// Protocol version
    protocol_version: ProtocolVersion,
    /// State machine configuration
    config: StateMachineConfig,
}

/// State transition record
#[derive(Debug, Clone)]
pub struct StateTransition {
    /// Timestamp of transition
    pub timestamp: Instant,
    /// Previous state
    pub from: ProtocolState,
    /// New state
    pub to: ProtocolState,
    /// Reason for transition
    pub reason: String,
    /// Duration in previous state
    pub duration: Duration,
}

/// State machine configuration
#[derive(Debug, Clone)]
pub struct StateMachineConfig {
    /// Maximum number of concurrent sessions
    pub max_sessions: usize,
    /// Session timeout duration
    pub session_timeout: Duration,
    /// Handshake timeout duration
    pub handshake_timeout: Duration,
    /// Sync timeout duration
    pub sync_timeout: Duration,
    /// Maximum state history size
    pub max_history_size: usize,
}

impl Default for StateMachineConfig {
    fn default() -> Self {
        Self {
            max_sessions: 1000,
            session_timeout: Duration::from_secs(300), // 5 minutes
            handshake_timeout: Duration::from_secs(30), // 30 seconds
            sync_timeout: Duration::from_secs(60),     // 1 minute
            max_history_size: 1000,
        }
    }
}

impl ProtocolStateMachine {
    /// Create a new protocol state machine
    pub fn new(protocol_version: ProtocolVersion) -> Self {
        Self {
            current_state: ProtocolState::Initial,
            previous_state: None,
            state_history: Vec::new(),
            sessions: HashMap::new(),
            started_at: Instant::now(),
            protocol_version,
            config: StateMachineConfig::default(),
        }
    }

    /// Create with custom configuration
    pub fn with_config(protocol_version: ProtocolVersion, config: StateMachineConfig) -> Self {
        Self {
            current_state: ProtocolState::Initial,
            previous_state: None,
            state_history: Vec::new(),
            sessions: HashMap::new(),
            started_at: Instant::now(),
            protocol_version,
            config,
        }
    }

    /// Get current state
    pub fn current_state(&self) -> &ProtocolState {
        &self.current_state
    }

    /// Get active sessions count
    pub fn active_sessions(&self) -> usize {
        self.sessions.len()
    }

    /// Get protocol version
    pub fn protocol_version(&self) -> &ProtocolVersion {
        &self.protocol_version
    }

    /// Get state machine uptime
    pub fn uptime(&self) -> Duration {
        self.started_at.elapsed()
    }

    /// Transition to a new state
    pub fn transition_to(
        &mut self,
        new_state: ProtocolState,
        reason: String,
    ) -> Result<(), StateError> {
        // Validate transition
        if !self.is_valid_transition(&self.current_state, &new_state) {
            return Err(StateError::InvalidTransition {
                from: self.current_state.clone(),
                to: new_state,
            });
        }

        let now = Instant::now();
        let duration = if let Some(last_transition) = self.state_history.last() {
            now.duration_since(last_transition.timestamp)
        } else {
            now.duration_since(self.started_at)
        };

        // Record state transition
        let transition = StateTransition {
            timestamp: now,
            from: self.current_state.clone(),
            to: new_state.clone(),
            reason: reason.clone(),
            duration,
        };

        debug!(
            "State transition: {:?} -> {:?} ({})",
            self.current_state, new_state, reason
        );

        // Update states
        self.previous_state = Some(self.current_state.clone());
        self.current_state = new_state;

        // Add to history
        self.state_history.push(transition);

        // Limit history size
        if self.state_history.len() > self.config.max_history_size {
            self.state_history.remove(0);
        }

        // Perform state entry actions
        self.on_state_entry(&reason)?;

        Ok(())
    }

    /// Check if a state transition is valid
    fn is_valid_transition(&self, from: &ProtocolState, to: &ProtocolState) -> bool {
        use ActiveState::*;
        use ErrorState::*;
        use HandshakeState::*;
        use ProtocolState::*;
        use SyncState::*;

        match (from, to) {
            // From Initial
            (Initial, Handshake(Waiting)) => true,
            (Initial, Error(_)) => true,
            (Initial, Shutdown) => true,

            // From Handshake states
            (Handshake(Waiting), Handshake(InProgress)) => true,
            (Handshake(InProgress), Handshake(Processing)) => true,
            (Handshake(InProgress), Handshake(Failed)) => true,
            (Handshake(Processing), Handshake(Completed)) => true,
            (Handshake(Processing), Handshake(Failed)) => true,
            (Handshake(Completed), Active(Normal)) => true,
            (Handshake(Failed), Error(NetworkError)) => true,
            (Handshake(_), Shutdown) => true,

            // From Active states
            (Active(Normal), Active(HighLoad)) => true,
            (Active(Normal), Active(Degraded)) => true,
            (Active(Normal), Synchronizing(Requesting)) => true,
            (Active(HighLoad), Active(Normal)) => true,
            (Active(HighLoad), Active(Degraded)) => true,
            (Active(Degraded), Active(Normal)) => true,
            (Active(Degraded), Synchronizing(Requesting)) => true,
            (Active(_), Error(_)) => true,
            (Active(_), Shutdown) => true,

            // From Synchronizing states
            (Synchronizing(Requesting), Synchronizing(Receiving)) => true,
            (Synchronizing(Requesting), Error(NetworkError)) => true,
            (Synchronizing(Receiving), Synchronizing(Applying)) => true,
            (Synchronizing(Receiving), Error(NetworkError)) => true,
            (Synchronizing(Applying), Synchronizing(Verifying)) => true,
            (Synchronizing(Applying), Error(InternalError)) => true,
            (Synchronizing(Verifying), Active(Normal)) => true,
            (Synchronizing(Verifying), Error(InternalError)) => true,
            (Synchronizing(_), Shutdown) => true,

            // From Error states
            (Error(_), Initial) => true, // Recovery
            (Error(_), Shutdown) => true,

            // From Shutdown
            (Shutdown, _) => false, // No transitions from shutdown

            // Same state (for updates)
            (a, b) if a == b => true,

            _ => false,
        }
    }

    /// Perform actions when entering a new state
    fn on_state_entry(&mut self, reason: &str) -> Result<(), StateError> {
        let current_state = self.current_state.clone();
        match &current_state {
            ProtocolState::Initial => {
                debug!("Entered Initial state: {}", reason);
                // Clean up any existing sessions
                self.sessions.clear();
            }

            ProtocolState::Handshake(handshake_state) => {
                debug!("Entered Handshake state {:?}: {}", handshake_state, reason);
                match handshake_state {
                    HandshakeState::InProgress => {
                        // Start handshake timeout
                        // TODO: Implement timeout mechanism
                    }
                    HandshakeState::Failed => {
                        warn!("Handshake failed: {}", reason);
                        // Clean up failed handshake sessions
                        self.cleanup_failed_sessions();
                    }
                    _ => {}
                }
            }

            ProtocolState::Active(active_state) => {
                debug!("Entered Active state {:?}: {}", active_state, reason);
                match active_state {
                    ActiveState::HighLoad => {
                        // Implement load shedding
                        warn!("Entering high load state, implementing load shedding");
                    }
                    ActiveState::Degraded => {
                        warn!("Entering degraded state: {}", reason);
                    }
                    _ => {}
                }
            }

            ProtocolState::Synchronizing(sync_state) => {
                debug!("Entered Synchronizing state {:?}: {}", sync_state, reason);
            }

            ProtocolState::Error(error_state) => {
                error!("Entered Error state {:?}: {}", error_state, reason);
                // Implement error recovery procedures
                self.handle_error_state(error_state, reason)?;
            }

            ProtocolState::Shutdown => {
                debug!("Entered Shutdown state: {}", reason);
                // Begin graceful shutdown
                self.begin_shutdown();
            }
        }

        Ok(())
    }

    /// Handle error state entry
    fn handle_error_state(
        &mut self,
        error_state: &ErrorState,
        reason: &str,
    ) -> Result<(), StateError> {
        match error_state {
            ErrorState::NetworkError => {
                // Close problematic connections
                self.cleanup_failed_sessions();
            }
            ErrorState::ConsensusError => {
                // Request state synchronization
                // TODO: Trigger sync process
            }
            ErrorState::CryptoError => {
                // Critical error - may need to restart
                error!("Critical cryptographic error: {}", reason);
            }
            ErrorState::ResourceError => {
                // Implement resource cleanup
                self.cleanup_resources();
            }
            ErrorState::InternalError => {
                // Log detailed error information
                error!("Internal protocol error: {}", reason);
            }
        }
        Ok(())
    }

    /// Begin graceful shutdown
    fn begin_shutdown(&mut self) {
        debug!("Beginning graceful shutdown");

        // Close all active sessions
        for (session_id, session) in &mut self.sessions {
            debug!("Closing session: {}", session_id);
            session.state = ProtocolState::Shutdown;
        }

        // TODO: Send disconnect messages to peers
        // TODO: Flush pending operations
    }

    /// Clean up failed sessions
    fn cleanup_failed_sessions(&mut self) {
        let failed_sessions: Vec<Uuid> = self
            .sessions
            .iter()
            .filter(|(_, session)| {
                matches!(
                    session.state,
                    ProtocolState::Error(_) | ProtocolState::Handshake(HandshakeState::Failed)
                )
            })
            .map(|(id, _)| *id)
            .collect();

        for session_id in failed_sessions {
            debug!("Cleaning up failed session: {}", session_id);
            self.sessions.remove(&session_id);
        }
    }

    /// Clean up resources
    fn cleanup_resources(&mut self) {
        debug!("Cleaning up resources");

        // Remove old state history
        if self.state_history.len() > self.config.max_history_size / 2 {
            let keep_from = self.state_history.len() - self.config.max_history_size / 2;
            self.state_history.drain(0..keep_from);
        }

        // Remove timed out sessions
        self.cleanup_timed_out_sessions();
    }

    /// Clean up timed out sessions
    fn cleanup_timed_out_sessions(&mut self) {
        let now = SystemTime::now();
        let timeout = self.config.session_timeout;

        let timed_out_sessions: Vec<Uuid> = self
            .sessions
            .iter()
            .filter(|(_, session)| {
                now.duration_since(session.last_activity)
                    .unwrap_or(Duration::ZERO)
                    > timeout
            })
            .map(|(id, _)| *id)
            .collect();

        for session_id in timed_out_sessions {
            debug!("Removing timed out session: {}", session_id);
            self.sessions.remove(&session_id);
        }
    }

    /// Create a new session
    pub fn create_session(
        &mut self,
        peer_id: Vec<u8>,
        protocol_version: ProtocolVersion,
        capabilities: Vec<String>,
    ) -> Result<Uuid, StateError> {
        // Check session limit
        if self.sessions.len() >= self.config.max_sessions {
            return Err(StateError::InvalidData {
                reason: "Maximum number of sessions reached".to_string(),
            });
        }

        // Verify protocol version compatibility
        if !self.protocol_version.is_compatible(&protocol_version) {
            return Err(StateError::VersionMismatch {
                expected: self.protocol_version.clone(),
                actual: protocol_version,
            });
        }

        let session_id = Uuid::new_v4();
        let now = SystemTime::now();

        let session = SessionInfo {
            id: session_id,
            peer_id,
            protocol_version,
            state: ProtocolState::Handshake(HandshakeState::Waiting),
            started_at: now,
            last_activity: now,
            capabilities,
            metrics: SessionMetrics::default(),
        };

        self.sessions.insert(session_id, session);

        debug!("Created new session: {}", session_id);
        Ok(session_id)
    }

    /// Update session state
    pub fn update_session_state(
        &mut self,
        session_id: Uuid,
        new_state: ProtocolState,
    ) -> Result<(), StateError> {
        // First get the current session state for validation
        let current_session_state = self
            .sessions
            .get(&session_id)
            .ok_or(StateError::SessionNotFound { session_id })?
            .state
            .clone();

        // Validate session state transition
        if !self.is_valid_transition(&current_session_state, &new_state) {
            return Err(StateError::InvalidTransition {
                from: current_session_state,
                to: new_state,
            });
        }

        // Now get mutable reference and update
        let session = self
            .sessions
            .get_mut(&session_id)
            .ok_or(StateError::SessionNotFound { session_id })?;
        session.state = new_state;
        session.last_activity = SystemTime::now();

        Ok(())
    }

    /// Get session information
    pub fn get_session(&self, session_id: &Uuid) -> Option<&SessionInfo> {
        self.sessions.get(session_id)
    }

    /// Remove a session
    pub fn remove_session(&mut self, session_id: &Uuid) -> Option<SessionInfo> {
        self.sessions.remove(session_id)
    }

    /// Process a protocol message and update state accordingly
    pub fn process_message(
        &mut self,
        message: &Message,
        session_id: Option<Uuid>,
    ) -> Result<(), StateError> {
        // Update last activity for session if provided
        if let Some(session_id) = session_id {
            if let Some(session) = self.sessions.get_mut(&session_id) {
                session.last_activity = SystemTime::now();
                session.metrics.messages_received += 1;
                session.metrics.bytes_received += message.payload.len() as u64;
            }
        }

        // Process message based on type and current state
        match &message.msg_type {
            MessageType::Handshake(handshake_type) => {
                self.process_handshake_message(handshake_type, message, session_id)?;
            }
            MessageType::Control(_) => {
                // Control messages can be processed in most states
                if !matches!(self.current_state, ProtocolState::Shutdown) {
                    // Process control message
                    debug!(
                        "Processing control message in state {:?}",
                        self.current_state
                    );
                }
            }
            _ => {
                // Other messages require active state
                match &self.current_state {
                    ProtocolState::Active(_) => {
                        // Process message in active state
                        debug!("Processing message in active state");
                    }
                    _ => {
                        warn!(
                            "Received message in non-active state: {:?}",
                            self.current_state
                        );
                    }
                }
            }
        }

        Ok(())
    }

    /// Process handshake messages
    fn process_handshake_message(
        &mut self,
        handshake_type: &HandshakeType,
        _message: &Message,
        session_id: Option<Uuid>,
    ) -> Result<(), StateError> {
        match handshake_type {
            HandshakeType::Init => {
                if matches!(
                    self.current_state,
                    ProtocolState::Initial | ProtocolState::Handshake(_)
                ) {
                    self.transition_to(
                        ProtocolState::Handshake(HandshakeState::InProgress),
                        "Received handshake init".to_string(),
                    )?;
                }
            }
            HandshakeType::Response => {
                if matches!(
                    self.current_state,
                    ProtocolState::Handshake(HandshakeState::InProgress)
                ) {
                    self.transition_to(
                        ProtocolState::Handshake(HandshakeState::Processing),
                        "Received handshake response".to_string(),
                    )?;
                }
            }
            HandshakeType::Complete => {
                if matches!(
                    self.current_state,
                    ProtocolState::Handshake(HandshakeState::Processing)
                ) {
                    self.transition_to(
                        ProtocolState::Handshake(HandshakeState::Completed),
                        "Handshake completed".to_string(),
                    )?;

                    // Transition to active state
                    self.transition_to(
                        ProtocolState::Active(ActiveState::Normal),
                        "Handshake successful, entering active state".to_string(),
                    )?;
                }
            }
            HandshakeType::VersionNegotiation => {
                // Handle version negotiation
                debug!("Processing version negotiation");
            }
        }

        // Update session state if provided
        if let Some(session_id) = session_id {
            if let Some(session) = self.sessions.get_mut(&session_id) {
                session.state = self.current_state.clone();
            }
        }

        Ok(())
    }

    /// Get state transition history
    pub fn get_state_history(&self) -> &[StateTransition] {
        &self.state_history
    }

    /// Get all active sessions
    pub fn get_sessions(&self) -> &HashMap<Uuid, SessionInfo> {
        &self.sessions
    }

    /// Check if the state machine is in a healthy state
    pub fn is_healthy(&self) -> bool {
        !matches!(
            self.current_state,
            ProtocolState::Error(_) | ProtocolState::Shutdown
        )
    }

    /// Get state machine metrics
    pub fn get_metrics(&self) -> StateMachineMetrics {
        let mut total_messages_sent = 0;
        let mut total_messages_received = 0;
        let mut total_bytes_sent = 0;
        let mut total_bytes_received = 0;
        let mut total_errors = 0;

        for session in self.sessions.values() {
            total_messages_sent += session.metrics.messages_sent;
            total_messages_received += session.metrics.messages_received;
            total_bytes_sent += session.metrics.bytes_sent;
            total_bytes_received += session.metrics.bytes_received;
            total_errors += session.metrics.error_count;
        }

        StateMachineMetrics {
            current_state: self.current_state.clone(),
            uptime: self.uptime(),
            active_sessions: self.sessions.len(),
            total_state_transitions: self.state_history.len(),
            total_messages_sent,
            total_messages_received,
            total_bytes_sent,
            total_bytes_received,
            total_errors,
        }
    }
}

/// State machine performance metrics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StateMachineMetrics {
    /// Current protocol state
    pub current_state: ProtocolState,
    /// State machine uptime
    pub uptime: Duration,
    /// Number of active sessions
    pub active_sessions: usize,
    /// Total state transitions
    pub total_state_transitions: usize,
    /// Total messages sent across all sessions
    pub total_messages_sent: u64,
    /// Total messages received across all sessions
    pub total_messages_received: u64,
    /// Total bytes sent
    pub total_bytes_sent: u64,
    /// Total bytes received
    pub total_bytes_received: u64,
    /// Total errors
    pub total_errors: u64,
}

/// State management trait defining the interface for state operations.
pub trait StateManager {
    /// Initialize protocol state.
    fn init() -> Result<ProtocolStateMachine, StateError>;

    /// Transition to a new state.
    fn transition(&mut self, new_state: ProtocolState) -> Result<(), StateError>;

    /// Get current state.
    fn get_state(&self) -> &ProtocolState;

    /// Validate state transition.
    fn validate_transition(&self, new_state: &ProtocolState) -> bool;
}

impl StateManager for ProtocolStateMachine {
    fn init() -> Result<ProtocolStateMachine, StateError> {
        Ok(ProtocolStateMachine::new(ProtocolVersion::CURRENT))
    }

    fn transition(&mut self, new_state: ProtocolState) -> Result<(), StateError> {
        self.transition_to(new_state, "Manual transition".to_string())
    }

    fn get_state(&self) -> &ProtocolState {
        &self.current_state
    }

    fn validate_transition(&self, new_state: &ProtocolState) -> bool {
        self.is_valid_transition(&self.current_state, new_state)
    }
}