Skip to main content

solana_version/
client_ids.rs

1use std::fmt;
2
3#[derive(Clone, Debug, Eq, PartialEq)]
4pub enum ClientId {
5    SolanaLabs,
6    JitoLabs,
7    Frankendancer,
8    Agave,
9    AgavePaladin,
10    Firedancer,
11    AgaveBam,
12    Sig,
13    // If new variants are added, update From<u16> and TryFrom<ClientId>.
14    Unknown(u16),
15}
16
17impl fmt::Display for ClientId {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match self {
20            Self::SolanaLabs => write!(f, "SolanaLabs"),
21            Self::JitoLabs => write!(f, "JitoLabs"),
22            Self::Frankendancer => write!(f, "Frankendancer"),
23            Self::Agave => write!(f, "Agave"),
24            Self::AgavePaladin => write!(f, "AgavePaladin"),
25            Self::Firedancer => write!(f, "Firedancer"),
26            Self::AgaveBam => write!(f, "AgaveBam"),
27            Self::Sig => write!(f, "Sig"),
28            Self::Unknown(id) => write!(f, "Unknown({id})"),
29        }
30    }
31}
32
33impl From<u16> for ClientId {
34    fn from(client: u16) -> Self {
35        match client {
36            0u16 => Self::SolanaLabs,
37            1u16 => Self::JitoLabs,
38            2u16 => Self::Frankendancer,
39            3u16 => Self::Agave,
40            4u16 => Self::AgavePaladin,
41            5u16 => Self::Firedancer,
42            6u16 => Self::AgaveBam,
43            7u16 => Self::Sig,
44            _ => Self::Unknown(client),
45        }
46    }
47}
48
49impl TryFrom<ClientId> for u16 {
50    type Error = String;
51
52    fn try_from(client: ClientId) -> Result<Self, Self::Error> {
53        match client {
54            ClientId::SolanaLabs => Ok(0u16),
55            ClientId::JitoLabs => Ok(1u16),
56            ClientId::Frankendancer => Ok(2u16),
57            ClientId::Agave => Ok(3u16),
58            ClientId::AgavePaladin => Ok(4u16),
59            ClientId::Firedancer => Ok(5u16),
60            ClientId::AgaveBam => Ok(6u16),
61            ClientId::Sig => Ok(7u16),
62            ClientId::Unknown(client @ 0u16..=7u16) => Err(format!("Invalid client: {client}")),
63            ClientId::Unknown(client) => Ok(client),
64        }
65    }
66}
67
68impl ClientId {
69    pub const fn this_client() -> Self {
70        // Other client implementations need to modify this line.
71        Self::Agave
72    }
73}
74
75#[cfg(test)]
76mod test {
77    use super::*;
78
79    #[test]
80    fn test_client_id() {
81        assert_eq!(ClientId::from(0u16), ClientId::SolanaLabs);
82        assert_eq!(ClientId::from(1u16), ClientId::JitoLabs);
83        assert_eq!(ClientId::from(2u16), ClientId::Frankendancer);
84        assert_eq!(ClientId::from(3u16), ClientId::Agave);
85        assert_eq!(ClientId::from(4u16), ClientId::AgavePaladin);
86        assert_eq!(ClientId::from(5u16), ClientId::Firedancer);
87        assert_eq!(ClientId::from(6u16), ClientId::AgaveBam);
88        assert_eq!(ClientId::from(7u16), ClientId::Sig);
89        for client in 8u16..=u16::MAX {
90            assert_eq!(ClientId::from(client), ClientId::Unknown(client));
91        }
92        assert_eq!(u16::try_from(ClientId::SolanaLabs), Ok(0u16));
93        assert_eq!(u16::try_from(ClientId::JitoLabs), Ok(1u16));
94        assert_eq!(u16::try_from(ClientId::Frankendancer), Ok(2u16));
95        assert_eq!(u16::try_from(ClientId::Agave), Ok(3u16));
96        assert_eq!(u16::try_from(ClientId::AgavePaladin), Ok(4u16));
97        assert_eq!(u16::try_from(ClientId::Firedancer), Ok(5u16));
98        assert_eq!(u16::try_from(ClientId::AgaveBam), Ok(6u16));
99        assert_eq!(u16::try_from(ClientId::Sig), Ok(7u16));
100        for client in 0..=7u16 {
101            assert_eq!(
102                u16::try_from(ClientId::Unknown(client)),
103                Err(format!("Invalid client: {client}"))
104            );
105        }
106        for client in 8u16..=u16::MAX {
107            assert_eq!(u16::try_from(ClientId::Unknown(client)), Ok(client));
108        }
109    }
110
111    #[test]
112    fn test_fmt() {
113        assert_eq!(format!("{}", ClientId::SolanaLabs), "SolanaLabs");
114        assert_eq!(format!("{}", ClientId::JitoLabs), "JitoLabs");
115        assert_eq!(format!("{}", ClientId::Frankendancer), "Frankendancer");
116        assert_eq!(format!("{}", ClientId::Agave), "Agave");
117        assert_eq!(format!("{}", ClientId::AgavePaladin), "AgavePaladin");
118        assert_eq!(format!("{}", ClientId::Firedancer), "Firedancer");
119        assert_eq!(format!("{}", ClientId::AgaveBam), "AgaveBam");
120        assert_eq!(format!("{}", ClientId::Sig), "Sig");
121        assert_eq!(format!("{}", ClientId::Unknown(0)), "Unknown(0)");
122        assert_eq!(format!("{}", ClientId::Unknown(u16::MAX)), "Unknown(65535)");
123    }
124}