reth_eth_wire_types/
primitives.rs

1//! Abstraction over primitive types in network messages.
2
3use alloy_consensus::{RlpDecodableReceipt, RlpEncodableReceipt, TxReceipt};
4use alloy_rlp::{Decodable, Encodable};
5use core::fmt::Debug;
6use reth_primitives_traits::{Block, BlockBody, BlockHeader, NodePrimitives, SignedTransaction};
7
8/// Abstraction over primitive types which might appear in network messages. See
9/// [`crate::EthMessage`] for more context.
10pub trait NetworkPrimitives:
11    Send + Sync + Unpin + Clone + Debug + PartialEq + Eq + 'static
12{
13    /// The block header type.
14    type BlockHeader: BlockHeader + 'static;
15
16    /// The block body type.
17    type BlockBody: BlockBody + 'static;
18
19    /// Full block type.
20    type Block: Block<Header = Self::BlockHeader, Body = Self::BlockBody>
21        + Encodable
22        + Decodable
23        + 'static;
24
25    /// The transaction type which peers announce in `Transactions` messages. It is different from
26    /// `PooledTransactions` to account for Ethereum case where EIP-4844 transactions are not being
27    /// announced and can only be explicitly requested from peers.
28    type BroadcastedTransaction: SignedTransaction + 'static;
29
30    /// The transaction type which peers return in `PooledTransactions` messages.
31    type PooledTransaction: SignedTransaction + TryFrom<Self::BroadcastedTransaction> + 'static;
32
33    /// The transaction type which peers return in `GetReceipts` messages.
34    type Receipt: TxReceipt + RlpEncodableReceipt + RlpDecodableReceipt + Unpin + 'static;
35}
36
37/// This is a helper trait for use in bounds, where some of the [`NetworkPrimitives`] associated
38/// types must be the same as the [`NodePrimitives`] associated types.
39pub trait NetPrimitivesFor<N: NodePrimitives>:
40    NetworkPrimitives<
41    BlockHeader = N::BlockHeader,
42    BlockBody = N::BlockBody,
43    Block = N::Block,
44    Receipt = N::Receipt,
45>
46{
47}
48
49impl<N, T> NetPrimitivesFor<N> for T
50where
51    N: NodePrimitives,
52    T: NetworkPrimitives<
53        BlockHeader = N::BlockHeader,
54        BlockBody = N::BlockBody,
55        Block = N::Block,
56        Receipt = N::Receipt,
57    >,
58{
59}
60
61/// Network primitive types used by Ethereum networks.
62#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
63#[non_exhaustive]
64pub struct EthNetworkPrimitives;
65
66impl NetworkPrimitives for EthNetworkPrimitives {
67    type BlockHeader = alloy_consensus::Header;
68    type BlockBody = reth_ethereum_primitives::BlockBody;
69    type Block = reth_ethereum_primitives::Block;
70    type BroadcastedTransaction = reth_ethereum_primitives::TransactionSigned;
71    type PooledTransaction = reth_ethereum_primitives::PooledTransaction;
72    type Receipt = reth_ethereum_primitives::Receipt;
73}