qudag_protocol/
types.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3use thiserror::Error;
4
5/// Protocol errors
6#[derive(Debug, Error)]
7pub enum ProtocolError {
8    #[error("Network error: {0}")]
9    NetworkError(String),
10
11    #[error("Consensus error: {0}")]
12    ConsensusError(String),
13
14    #[error("Crypto error: {0}")]
15    CryptoError(String),
16
17    #[error("State error: {0}")]
18    StateError(String),
19
20    #[error("Internal error: {0}")]
21    Internal(String),
22
23    #[error("Invalid message: {0}")]
24    InvalidMessage(String),
25}
26
27/// Protocol events
28#[derive(Debug, Clone)]
29pub enum ProtocolEvent {
30    /// New message received
31    MessageReceived {
32        /// Message ID
33        id: String,
34        /// Message payload
35        payload: Vec<u8>,
36        /// Source peer
37        source: Vec<u8>,
38    },
39
40    /// Message finalized by consensus
41    MessageFinalized {
42        /// Message ID
43        id: String,
44        /// Finalization time
45        time: Duration,
46    },
47
48    /// Protocol state changed
49    StateChanged {
50        /// Previous state
51        old_state: ProtocolState,
52        /// New state
53        new_state: ProtocolState,
54    },
55
56    /// Protocol error occurred
57    Error {
58        /// Error description
59        error: String,
60        /// Error context
61        context: String,
62    },
63}
64
65/// Protocol state
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
67pub enum ProtocolState {
68    /// Initial state
69    Initial,
70    /// Handshake in progress
71    Handshaking,
72    /// Protocol running
73    Running,
74    /// Protocol stopping
75    Stopping,
76    /// Protocol stopped
77    Stopped,
78    /// Error state
79    Error,
80}
81
82/// Protocol metrics
83#[derive(Debug, Clone, Default)]
84pub struct ProtocolMetrics {
85    /// Messages processed per second
86    pub messages_per_second: f64,
87    /// Average finalization time
88    pub avg_finalization_time: Duration,
89    /// Memory usage in bytes
90    pub memory_usage: usize,
91    /// Active consensus rounds
92    pub active_rounds: usize,
93}