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