Skip to main content

reth_network/
message.rs

1//! Capability messaging
2//!
3//! An `RLPx` stream is multiplexed via the prepended message-id of a framed message.
4//! Capabilities are exchanged via the `RLPx` `Hello` message as pairs of `(id, version)`, <https://github.com/ethereum/devp2p/blob/master/rlpx.md#capability-messaging>
5
6use crate::types::{BlockAccessLists, Receipts69, Receipts70};
7use alloy_consensus::{BlockHeader, ReceiptWithBloom};
8use alloy_primitives::{Bytes, B256};
9use futures::FutureExt;
10use reth_eth_wire::{
11    message::RequestPair, BlockBodies, BlockHeaders, BlockRangeUpdate, EthMessage,
12    EthNetworkPrimitives, GetBlockBodies, GetBlockHeaders, GetReceipts, NetworkPrimitives,
13    NewBlock, NewBlockHashes, NewBlockPayload, NewPooledTransactionHashes, NodeData,
14    PooledTransactions, Receipts, SharedTransactions, Transactions,
15};
16use reth_eth_wire_types::RawCapabilityMessage;
17use reth_network_api::PeerRequest;
18use reth_network_p2p::error::{RequestError, RequestResult};
19use reth_primitives_traits::Block;
20use std::{
21    sync::Arc,
22    task::{ready, Context, Poll},
23};
24use tokio::sync::oneshot;
25
26/// Internal form of a `NewBlock` message
27#[derive(Debug, Clone)]
28pub struct NewBlockMessage<P = NewBlock<reth_ethereum_primitives::Block>> {
29    /// Hash of the block
30    pub hash: B256,
31    /// Raw received message
32    pub block: Arc<P>,
33}
34
35// === impl NewBlockMessage ===
36
37impl<P: NewBlockPayload> NewBlockMessage<P> {
38    /// Returns the block number of the block
39    pub fn number(&self) -> u64 {
40        self.block.block().header().number()
41    }
42}
43
44/// All Bi-directional eth-message variants that can be sent to a session or received from a
45/// session.
46#[derive(Debug)]
47pub enum PeerMessage<N: NetworkPrimitives = EthNetworkPrimitives> {
48    /// Announce new block hashes
49    NewBlockHashes(NewBlockHashes),
50    /// Broadcast new block.
51    NewBlock(NewBlockMessage<N::NewBlockPayload>),
52    /// Received transactions _from_ the peer
53    ReceivedTransaction(Transactions<N::BroadcastedTransaction>),
54    /// Broadcast transactions _from_ local _to_ a peer.
55    SendTransactions(SharedTransactions<N::BroadcastedTransaction>),
56    /// Send new pooled transactions
57    PooledTransactions(NewPooledTransactionHashes),
58    /// All `eth` request variants.
59    EthRequest(PeerRequest<N>),
60    /// Announces when `BlockRange` is updated.
61    BlockRangeUpdated(BlockRangeUpdate),
62    /// Any other or manually crafted eth message.
63    ///
64    /// Caution: It is expected that this is a valid `eth_` capability message.
65    Other(RawCapabilityMessage),
66}
67
68/// Request Variants that only target block related data.
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub enum BlockRequest {
71    /// Requests block headers from the peer.
72    ///
73    /// The response should be sent through the channel.
74    GetBlockHeaders(GetBlockHeaders),
75
76    /// Requests block bodies from the peer.
77    ///
78    /// The response should be sent through the channel.
79    GetBlockBodies(GetBlockBodies),
80
81    /// Requests receipts from the peer.
82    ///
83    /// The response should be sent through the channel.
84    GetReceipts(GetReceipts),
85}
86
87/// Corresponding variant for [`PeerRequest`].
88#[derive(Debug)]
89pub enum PeerResponse<N: NetworkPrimitives = EthNetworkPrimitives> {
90    /// Represents a response to a request for block headers.
91    BlockHeaders {
92        /// The receiver channel for the response to a block headers request.
93        response: oneshot::Receiver<RequestResult<BlockHeaders<N::BlockHeader>>>,
94    },
95    /// Represents a response to a request for block bodies.
96    BlockBodies {
97        /// The receiver channel for the response to a block bodies request.
98        response: oneshot::Receiver<RequestResult<BlockBodies<N::BlockBody>>>,
99    },
100    /// Represents a response to a request for pooled transactions.
101    PooledTransactions {
102        /// The receiver channel for the response to a pooled transactions request.
103        response: oneshot::Receiver<RequestResult<PooledTransactions<N::PooledTransaction>>>,
104    },
105    /// Represents a response to a request for `NodeData`.
106    NodeData {
107        /// The receiver channel for the response to a `NodeData` request.
108        response: oneshot::Receiver<RequestResult<NodeData>>,
109    },
110    /// Represents a response to a request for receipts.
111    Receipts {
112        /// The receiver channel for the response to a receipts request.
113        response: oneshot::Receiver<RequestResult<Receipts<N::Receipt>>>,
114    },
115    /// Represents a response to a request for receipts.
116    ///
117    /// This is a variant of `Receipts` that was introduced in `eth/69`.
118    /// The difference is that this variant does not require the inclusion of bloom filters in the
119    /// response, making it more lightweight.
120    Receipts69 {
121        /// The receiver channel for the response to a receipts request.
122        response: oneshot::Receiver<RequestResult<Receipts69<N::Receipt>>>,
123    },
124    /// Represents a response to a request for receipts using eth/70.
125    Receipts70 {
126        /// The receiver channel for the response to a receipts request.
127        response: oneshot::Receiver<RequestResult<Receipts70<N::Receipt>>>,
128    },
129    /// Represents a response to a request for block access lists.
130    BlockAccessLists {
131        /// The receiver channel for the response to a block access lists request.
132        response: oneshot::Receiver<RequestResult<BlockAccessLists>>,
133    },
134}
135
136// === impl PeerResponse ===
137
138impl<N: NetworkPrimitives> PeerResponse<N> {
139    /// Polls the type to completion.
140    pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll<PeerResponseResult<N>> {
141        macro_rules! poll_request {
142            ($response:ident, $item:ident, $cx:ident) => {
143                match ready!($response.poll_unpin($cx)) {
144                    Ok(res) => PeerResponseResult::$item(res.map(|item| item.0)),
145                    Err(err) => PeerResponseResult::$item(Err(err.into())),
146                }
147            };
148        }
149
150        let res = match self {
151            Self::BlockHeaders { response } => {
152                poll_request!(response, BlockHeaders, cx)
153            }
154            Self::BlockBodies { response } => {
155                poll_request!(response, BlockBodies, cx)
156            }
157            Self::PooledTransactions { response } => {
158                poll_request!(response, PooledTransactions, cx)
159            }
160            Self::NodeData { response } => {
161                poll_request!(response, NodeData, cx)
162            }
163            Self::Receipts { response } => {
164                poll_request!(response, Receipts, cx)
165            }
166            Self::Receipts69 { response } => {
167                poll_request!(response, Receipts69, cx)
168            }
169            Self::Receipts70 { response } => match ready!(response.poll_unpin(cx)) {
170                Ok(res) => PeerResponseResult::Receipts70(res),
171                Err(err) => PeerResponseResult::Receipts70(Err(err.into())),
172            },
173            Self::BlockAccessLists { response } => match ready!(response.poll_unpin(cx)) {
174                Ok(res) => PeerResponseResult::BlockAccessLists(res),
175                Err(err) => PeerResponseResult::BlockAccessLists(Err(err.into())),
176            },
177        };
178        Poll::Ready(res)
179    }
180}
181
182/// All response variants for [`PeerResponse`]
183#[derive(Debug)]
184pub enum PeerResponseResult<N: NetworkPrimitives = EthNetworkPrimitives> {
185    /// Represents a result containing block headers or an error.
186    BlockHeaders(RequestResult<Vec<N::BlockHeader>>),
187    /// Represents a result containing block bodies or an error.
188    BlockBodies(RequestResult<Vec<N::BlockBody>>),
189    /// Represents a result containing pooled transactions or an error.
190    PooledTransactions(RequestResult<Vec<N::PooledTransaction>>),
191    /// Represents a result containing node data or an error.
192    NodeData(RequestResult<Vec<Bytes>>),
193    /// Represents a result containing receipts or an error.
194    Receipts(RequestResult<Vec<Vec<ReceiptWithBloom<N::Receipt>>>>),
195    /// Represents a result containing receipts or an error for eth/69.
196    Receipts69(RequestResult<Vec<Vec<N::Receipt>>>),
197    /// Represents a result containing receipts or an error for eth/70.
198    Receipts70(RequestResult<Receipts70<N::Receipt>>),
199    /// Represents a result containing block access lists or an error.
200    BlockAccessLists(RequestResult<BlockAccessLists>),
201}
202
203// === impl PeerResponseResult ===
204
205impl<N: NetworkPrimitives> PeerResponseResult<N> {
206    /// Converts this response into an [`EthMessage`]
207    pub fn try_into_message(self, id: u64) -> RequestResult<EthMessage<N>> {
208        macro_rules! to_message {
209            ($response:ident, $item:ident, $request_id:ident) => {
210                match $response {
211                    Ok(res) => {
212                        let request = RequestPair { request_id: $request_id, message: $item(res) };
213                        Ok(EthMessage::$item(request))
214                    }
215                    Err(err) => Err(err),
216                }
217            };
218        }
219        match self {
220            Self::BlockHeaders(resp) => {
221                to_message!(resp, BlockHeaders, id)
222            }
223            Self::BlockBodies(resp) => {
224                to_message!(resp, BlockBodies, id)
225            }
226            Self::PooledTransactions(resp) => {
227                to_message!(resp, PooledTransactions, id)
228            }
229            Self::NodeData(resp) => {
230                to_message!(resp, NodeData, id)
231            }
232            Self::Receipts(resp) => {
233                to_message!(resp, Receipts, id)
234            }
235            Self::Receipts69(resp) => {
236                to_message!(resp, Receipts69, id)
237            }
238            Self::Receipts70(resp) => match resp {
239                Ok(res) => {
240                    let request = RequestPair { request_id: id, message: res };
241                    Ok(EthMessage::Receipts70(request))
242                }
243                Err(err) => Err(err),
244            },
245            Self::BlockAccessLists(resp) => match resp {
246                Ok(res) => {
247                    let request = RequestPair { request_id: id, message: res };
248                    Ok(EthMessage::BlockAccessLists(request))
249                }
250                Err(err) => Err(err),
251            },
252        }
253    }
254
255    /// Returns the `Err` value if the result is an error.
256    pub fn err(&self) -> Option<&RequestError> {
257        match self {
258            Self::BlockHeaders(res) => res.as_ref().err(),
259            Self::BlockBodies(res) => res.as_ref().err(),
260            Self::PooledTransactions(res) => res.as_ref().err(),
261            Self::NodeData(res) => res.as_ref().err(),
262            Self::Receipts(res) => res.as_ref().err(),
263            Self::Receipts69(res) => res.as_ref().err(),
264            Self::Receipts70(res) => res.as_ref().err(),
265            Self::BlockAccessLists(res) => res.as_ref().err(),
266        }
267    }
268
269    /// Returns whether this result is an error.
270    pub fn is_err(&self) -> bool {
271        self.err().is_some()
272    }
273}