Skip to main content

reth_eth_wire_types/
version.rs

1//! Support for representing the version of the `eth`
2
3use crate::alloc::string::ToString;
4use alloc::string::String;
5use alloy_rlp::{Decodable, Encodable, Error as RlpError};
6use bytes::BufMut;
7use core::{fmt, str::FromStr};
8use derive_more::Display;
9use reth_codecs_derive::add_arbitrary_tests;
10
11/// Error thrown when failed to parse a valid [`EthVersion`].
12#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
13#[error("Unknown eth protocol version: {0}")]
14pub struct ParseVersionError(String);
15
16/// The `eth` protocol version.
17#[repr(u8)]
18#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Display)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
21pub enum EthVersion {
22    /// The `eth` protocol version 66.
23    Eth66 = 66,
24    /// The `eth` protocol version 67.
25    Eth67 = 67,
26    /// The `eth` protocol version 68.
27    Eth68 = 68,
28    /// The `eth` protocol version 69.
29    Eth69 = 69,
30    /// The `eth` protocol version 70.
31    ///
32    /// [EIP-7975](https://eips.ethereum.org/EIPS/eip-7975) adds partial block receipt
33    /// lists by extending `GetReceipts` and `Receipts` with pagination fields.
34    Eth70 = 70,
35    /// The `eth` protocol version 71.
36    ///
37    /// [EIP-8159](https://eips.ethereum.org/EIPS/eip-8159) adds block access list
38    /// exchange with `GetBlockAccessLists` and `BlockAccessLists`.
39    Eth71 = 71,
40    /// The `eth` protocol version 72.
41    ///
42    /// [EIP-8070](https://eips.ethereum.org/EIPS/eip-8070) adds sparse blobpool
43    /// support by extending `NewPooledTransactionHashes` with `cell_mask` and adding
44    /// `GetCells` and `Cells`.
45    Eth72 = 72,
46}
47
48impl EthVersion {
49    /// The latest known eth version
50    pub const LATEST: Self = Self::Eth69;
51
52    /// All known eth versions
53    pub const ALL_VERSIONS: &'static [Self] = &[Self::Eth69, Self::Eth68, Self::Eth67, Self::Eth66];
54
55    /// Returns true if the version is eth/66
56    pub const fn is_eth66(&self) -> bool {
57        matches!(self, Self::Eth66)
58    }
59
60    /// Returns true if the version is eth/67
61    pub const fn is_eth67(&self) -> bool {
62        matches!(self, Self::Eth67)
63    }
64
65    /// Returns true if the version is eth/68
66    pub const fn is_eth68(&self) -> bool {
67        matches!(self, Self::Eth68)
68    }
69
70    /// Returns true if the version carries eth/68 transaction announcement metadata.
71    pub const fn has_eth68_metadata(&self) -> bool {
72        matches!(self, Self::Eth68 | Self::Eth69 | Self::Eth70 | Self::Eth71 | Self::Eth72)
73    }
74
75    /// Returns true if the version is eth/69
76    pub const fn is_eth69(&self) -> bool {
77        matches!(self, Self::Eth69)
78    }
79
80    /// Returns true if the version is eth/70
81    pub const fn is_eth70(&self) -> bool {
82        matches!(self, Self::Eth70)
83    }
84
85    /// Returns true if the version is eth/71
86    pub const fn is_eth71(&self) -> bool {
87        matches!(self, Self::Eth71)
88    }
89
90    /// Returns true if the version is eth/72
91    pub const fn is_eth72(&self) -> bool {
92        matches!(self, Self::Eth72)
93    }
94
95    /// Returns true if the version is eth/69 or newer.
96    pub const fn is_eth69_or_newer(&self) -> bool {
97        matches!(self, Self::Eth69 | Self::Eth70 | Self::Eth71 | Self::Eth72)
98    }
99}
100
101/// RLP encodes `EthVersion` as a single byte (66-72).
102impl Encodable for EthVersion {
103    fn encode(&self, out: &mut dyn BufMut) {
104        (*self as u8).encode(out)
105    }
106
107    fn length(&self) -> usize {
108        (*self as u8).length()
109    }
110}
111
112/// RLP decodes a single byte into `EthVersion`.
113/// Returns error if byte is not a valid version (66-72).
114impl Decodable for EthVersion {
115    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
116        let version = u8::decode(buf)?;
117        Self::try_from(version).map_err(|_| RlpError::Custom("invalid eth version"))
118    }
119}
120
121/// Allow for converting from a `&str` to an `EthVersion`.
122///
123/// # Example
124/// ```
125/// use reth_eth_wire_types::EthVersion;
126///
127/// let version = EthVersion::try_from("67").unwrap();
128/// assert_eq!(version, EthVersion::Eth67);
129/// ```
130impl TryFrom<&str> for EthVersion {
131    type Error = ParseVersionError;
132
133    #[inline]
134    fn try_from(s: &str) -> Result<Self, Self::Error> {
135        match s {
136            "66" => Ok(Self::Eth66),
137            "67" => Ok(Self::Eth67),
138            "68" => Ok(Self::Eth68),
139            "69" => Ok(Self::Eth69),
140            "70" => Ok(Self::Eth70),
141            "71" => Ok(Self::Eth71),
142            "72" => Ok(Self::Eth72),
143            _ => Err(ParseVersionError(s.to_string())),
144        }
145    }
146}
147
148/// Allow for converting from a u8 to an `EthVersion`.
149///
150/// # Example
151/// ```
152/// use reth_eth_wire_types::EthVersion;
153///
154/// let version = EthVersion::try_from(67).unwrap();
155/// assert_eq!(version, EthVersion::Eth67);
156/// ```
157impl TryFrom<u8> for EthVersion {
158    type Error = ParseVersionError;
159
160    #[inline]
161    fn try_from(u: u8) -> Result<Self, Self::Error> {
162        match u {
163            66 => Ok(Self::Eth66),
164            67 => Ok(Self::Eth67),
165            68 => Ok(Self::Eth68),
166            69 => Ok(Self::Eth69),
167            70 => Ok(Self::Eth70),
168            71 => Ok(Self::Eth71),
169            72 => Ok(Self::Eth72),
170            _ => Err(ParseVersionError(u.to_string())),
171        }
172    }
173}
174
175impl FromStr for EthVersion {
176    type Err = ParseVersionError;
177
178    #[inline]
179    fn from_str(s: &str) -> Result<Self, Self::Err> {
180        Self::try_from(s)
181    }
182}
183
184impl From<EthVersion> for u8 {
185    #[inline]
186    fn from(v: EthVersion) -> Self {
187        v as Self
188    }
189}
190
191impl From<EthVersion> for &'static str {
192    #[inline]
193    fn from(v: EthVersion) -> &'static str {
194        match v {
195            EthVersion::Eth66 => "66",
196            EthVersion::Eth67 => "67",
197            EthVersion::Eth68 => "68",
198            EthVersion::Eth69 => "69",
199            EthVersion::Eth70 => "70",
200            EthVersion::Eth71 => "71",
201            EthVersion::Eth72 => "72",
202        }
203    }
204}
205
206/// `RLPx` `p2p` protocol version
207#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
208#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
209#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
210#[add_arbitrary_tests(rlp)]
211pub enum ProtocolVersion {
212    /// `p2p` version 4
213    V4 = 4,
214    /// `p2p` version 5
215    #[default]
216    V5 = 5,
217}
218
219impl fmt::Display for ProtocolVersion {
220    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221        write!(f, "v{}", *self as u8)
222    }
223}
224
225impl Encodable for ProtocolVersion {
226    fn encode(&self, out: &mut dyn BufMut) {
227        (*self as u8).encode(out)
228    }
229    fn length(&self) -> usize {
230        // the version should be a single byte
231        (*self as u8).length()
232    }
233}
234
235impl Decodable for ProtocolVersion {
236    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
237        let version = u8::decode(buf)?;
238        match version {
239            4 => Ok(Self::V4),
240            5 => Ok(Self::V5),
241            _ => Err(RlpError::Custom("unknown p2p protocol version")),
242        }
243    }
244}
245
246#[cfg(test)]
247mod tests {
248    use super::EthVersion;
249    use alloy_rlp::{Decodable, Encodable, Error as RlpError};
250    use bytes::BytesMut;
251
252    #[test]
253    fn test_eth_version_try_from_str() {
254        assert_eq!(EthVersion::Eth66, EthVersion::try_from("66").unwrap());
255        assert_eq!(EthVersion::Eth67, EthVersion::try_from("67").unwrap());
256        assert_eq!(EthVersion::Eth68, EthVersion::try_from("68").unwrap());
257        assert_eq!(EthVersion::Eth69, EthVersion::try_from("69").unwrap());
258        assert_eq!(EthVersion::Eth70, EthVersion::try_from("70").unwrap());
259        assert_eq!(EthVersion::Eth71, EthVersion::try_from("71").unwrap());
260        assert_eq!(EthVersion::Eth72, EthVersion::try_from("72").unwrap());
261    }
262
263    #[test]
264    fn test_eth_version_from_str() {
265        assert_eq!(EthVersion::Eth66, "66".parse().unwrap());
266        assert_eq!(EthVersion::Eth67, "67".parse().unwrap());
267        assert_eq!(EthVersion::Eth68, "68".parse().unwrap());
268        assert_eq!(EthVersion::Eth69, "69".parse().unwrap());
269        assert_eq!(EthVersion::Eth70, "70".parse().unwrap());
270        assert_eq!(EthVersion::Eth71, "71".parse().unwrap());
271        assert_eq!(EthVersion::Eth72, "72".parse().unwrap());
272    }
273
274    #[test]
275    fn test_has_eth68_metadata() {
276        assert!(!EthVersion::Eth66.has_eth68_metadata());
277        assert!(!EthVersion::Eth67.has_eth68_metadata());
278        assert!(EthVersion::Eth68.has_eth68_metadata());
279        assert!(EthVersion::Eth69.has_eth68_metadata());
280        assert!(EthVersion::Eth70.has_eth68_metadata());
281        assert!(EthVersion::Eth71.has_eth68_metadata());
282        assert!(EthVersion::Eth72.has_eth68_metadata());
283    }
284
285    #[test]
286    fn test_eth_version_rlp_encode() {
287        let versions = [
288            EthVersion::Eth66,
289            EthVersion::Eth67,
290            EthVersion::Eth68,
291            EthVersion::Eth69,
292            EthVersion::Eth70,
293            EthVersion::Eth71,
294            EthVersion::Eth72,
295        ];
296
297        for version in versions {
298            let mut encoded = BytesMut::new();
299            version.encode(&mut encoded);
300
301            assert_eq!(encoded.len(), 1);
302            assert_eq!(encoded[0], version as u8);
303        }
304    }
305    #[test]
306    fn test_eth_version_rlp_decode() {
307        let test_cases = [
308            (66_u8, Ok(EthVersion::Eth66)),
309            (67_u8, Ok(EthVersion::Eth67)),
310            (68_u8, Ok(EthVersion::Eth68)),
311            (69_u8, Ok(EthVersion::Eth69)),
312            (70_u8, Ok(EthVersion::Eth70)),
313            (71_u8, Ok(EthVersion::Eth71)),
314            (72_u8, Ok(EthVersion::Eth72)),
315            (65_u8, Err(RlpError::Custom("invalid eth version"))),
316        ];
317
318        for (input, expected) in test_cases {
319            let mut encoded = BytesMut::new();
320            input.encode(&mut encoded);
321
322            let mut slice = encoded.as_ref();
323            let result = EthVersion::decode(&mut slice);
324            assert_eq!(result, expected);
325        }
326    }
327}