reth_ress_protocol/
types.rs

1use alloy_primitives::bytes::{Buf, BufMut};
2use alloy_rlp::{Decodable, Encodable};
3
4/// Node type variant.
5#[repr(u8)]
6#[derive(PartialEq, Eq, Copy, Clone, Debug)]
7#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
8pub enum NodeType {
9    /// Stateless ress node.
10    Stateless = 0x00,
11    /// Stateful reth node.
12    Stateful,
13}
14
15impl Encodable for NodeType {
16    fn encode(&self, out: &mut dyn BufMut) {
17        out.put_u8(*self as u8);
18    }
19
20    fn length(&self) -> usize {
21        1
22    }
23}
24
25impl Decodable for NodeType {
26    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
27        let id = match buf.first().ok_or(alloy_rlp::Error::InputTooShort)? {
28            0x00 => Self::Stateless,
29            0x01 => Self::Stateful,
30            _ => return Err(alloy_rlp::Error::Custom("Invalid message type")),
31        };
32        buf.advance(1);
33        Ok(id)
34    }
35}
36
37impl NodeType {
38    /// Return `true` if node type is stateful.
39    pub const fn is_stateful(&self) -> bool {
40        matches!(self, Self::Stateful)
41    }
42
43    /// Return `true` if the connection between this and other node types
44    /// can be considered valid.
45    ///
46    /// Validity:
47    ///           | stateless | stateful |
48    /// ----------|-----------|----------|
49    /// stateless |     +     |     +    |
50    /// stateful  |     +     |     -    |
51    pub const fn is_valid_connection(&self, other: &Self) -> bool {
52        !self.is_stateful() || !other.is_stateful()
53    }
54}