1use 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#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
13#[error("Unknown eth protocol version: {0}")]
14pub struct ParseVersionError(String);
15
16#[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 Eth66 = 66,
24 Eth67 = 67,
26 Eth68 = 68,
28 Eth69 = 69,
30 Eth70 = 70,
35 Eth71 = 71,
40 Eth72 = 72,
46}
47
48impl EthVersion {
49 pub const LATEST: Self = Self::Eth69;
51
52 pub const ALL_VERSIONS: &'static [Self] = &[Self::Eth69, Self::Eth68, Self::Eth67, Self::Eth66];
54
55 pub const fn is_eth66(&self) -> bool {
57 matches!(self, Self::Eth66)
58 }
59
60 pub const fn is_eth67(&self) -> bool {
62 matches!(self, Self::Eth67)
63 }
64
65 pub const fn is_eth68(&self) -> bool {
67 matches!(self, Self::Eth68)
68 }
69
70 pub const fn has_eth68_metadata(&self) -> bool {
72 matches!(self, Self::Eth68 | Self::Eth69 | Self::Eth70 | Self::Eth71 | Self::Eth72)
73 }
74
75 pub const fn is_eth69(&self) -> bool {
77 matches!(self, Self::Eth69)
78 }
79
80 pub const fn is_eth70(&self) -> bool {
82 matches!(self, Self::Eth70)
83 }
84
85 pub const fn is_eth71(&self) -> bool {
87 matches!(self, Self::Eth71)
88 }
89
90 pub const fn is_eth72(&self) -> bool {
92 matches!(self, Self::Eth72)
93 }
94
95 pub const fn is_eth69_or_newer(&self) -> bool {
97 matches!(self, Self::Eth69 | Self::Eth70 | Self::Eth71 | Self::Eth72)
98 }
99}
100
101impl 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
112impl 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
121impl 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
148impl 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#[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 V4 = 4,
214 #[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 (*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}