reth_eth_wire/
protocol.rs1use crate::{Capability, EthMessageID, EthVersion, SnapVersion};
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Protocol {
12 pub cap: Capability,
14 messages: u8,
18}
19
20impl Protocol {
21 pub const fn new(cap: Capability, messages: u8) -> Self {
23 Self { cap, messages }
24 }
25
26 pub const fn eth(version: EthVersion) -> Self {
28 let cap = Capability::eth(version);
29 let messages = EthMessageID::message_count(version);
30 Self::new(cap, messages)
31 }
32
33 pub const fn snap(version: SnapVersion) -> Self {
35 let cap = Capability::snap(version);
36 let messages = version.message_count();
37 Self::new(cap, messages)
38 }
39
40 pub const fn eth_66() -> Self {
42 Self::eth(EthVersion::Eth66)
43 }
44
45 pub const fn eth_67() -> Self {
47 Self::eth(EthVersion::Eth67)
48 }
49
50 pub const fn eth_68() -> Self {
52 Self::eth(EthVersion::Eth68)
53 }
54
55 pub const fn snap_2() -> Self {
57 Self::snap(SnapVersion::V2)
58 }
59
60 #[inline]
62 pub(crate) fn split(self) -> (Capability, u8) {
63 (self.cap, self.messages)
64 }
65
66 pub const fn messages(&self) -> u8 {
68 self.messages
69 }
70}
71
72impl From<EthVersion> for Protocol {
73 fn from(version: EthVersion) -> Self {
74 Self::eth(version)
75 }
76}
77
78#[derive(Clone, Debug, PartialEq, Eq, Hash)]
80pub(crate) struct ProtoVersion {
81 pub(crate) messages: u8,
83 pub(crate) version: usize,
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn test_protocol_eth_message_count() {
93 assert_eq!(Protocol::eth(EthVersion::Eth66).messages(), 17);
96 assert_eq!(Protocol::eth(EthVersion::Eth67).messages(), 17);
97 assert_eq!(Protocol::eth(EthVersion::Eth68).messages(), 17);
98 assert_eq!(Protocol::eth(EthVersion::Eth69).messages(), 18);
99 assert_eq!(Protocol::eth(EthVersion::Eth70).messages(), 18);
100 assert_eq!(Protocol::eth(EthVersion::Eth71).messages(), 20);
101 assert_eq!(Protocol::snap(SnapVersion::V2).messages(), 10);
102 }
103}