reth_network_p2p/bodies/
client.rs

1use std::{
2    ops::RangeInclusive,
3    pin::Pin,
4    task::{ready, Context, Poll},
5};
6
7use crate::{download::DownloadClient, error::PeerRequestResult, priority::Priority};
8use alloy_primitives::B256;
9use futures::{Future, FutureExt};
10use reth_primitives_traits::BlockBody;
11
12/// The bodies future type
13pub type BodiesFut<B = reth_ethereum_primitives::BlockBody> =
14    Pin<Box<dyn Future<Output = PeerRequestResult<Vec<B>>> + Send + Sync>>;
15
16/// A client capable of downloading block bodies.
17#[auto_impl::auto_impl(&, Arc, Box)]
18pub trait BodiesClient: DownloadClient {
19    /// The body type this client fetches.
20    type Body: BlockBody;
21    /// The output of the request future for querying block bodies.
22    type Output: Future<Output = PeerRequestResult<Vec<Self::Body>>> + Sync + Send + Unpin;
23
24    /// Fetches the block body for the requested block.
25    fn get_block_bodies(&self, hashes: Vec<B256>) -> Self::Output {
26        self.get_block_bodies_with_priority(hashes, Priority::Normal)
27    }
28
29    /// Fetches the block body for the requested block with priority
30    fn get_block_bodies_with_priority(
31        &self,
32        hashes: Vec<B256>,
33        priority: Priority,
34    ) -> Self::Output {
35        self.get_block_bodies_with_priority_and_range_hint(hashes, priority, None)
36    }
37
38    /// Fetches the block body for the requested block with priority and a range hint for the
39    /// requested blocks.
40    ///
41    /// The range hint is not required, but can be used to optimize the routing of the request if
42    /// the hashes are continuous or close together and the range hint is `[earliest, latest]` for
43    /// the requested blocks.
44    fn get_block_bodies_with_priority_and_range_hint(
45        &self,
46        hashes: Vec<B256>,
47        priority: Priority,
48        range_hint: Option<RangeInclusive<u64>>,
49    ) -> Self::Output;
50
51    /// Fetches a single block body for the requested hash.
52    fn get_block_body(&self, hash: B256) -> SingleBodyRequest<Self::Output> {
53        self.get_block_body_with_priority(hash, Priority::Normal)
54    }
55
56    /// Fetches a single block body for the requested hash with priority
57    fn get_block_body_with_priority(
58        &self,
59        hash: B256,
60        priority: Priority,
61    ) -> SingleBodyRequest<Self::Output> {
62        let fut = self.get_block_bodies_with_priority(vec![hash], priority);
63        SingleBodyRequest { fut }
64    }
65}
66
67/// A Future that resolves to a single block body.
68#[derive(Debug)]
69#[must_use = "futures do nothing unless polled"]
70pub struct SingleBodyRequest<Fut> {
71    fut: Fut,
72}
73
74impl<Fut, B> Future for SingleBodyRequest<Fut>
75where
76    Fut: Future<Output = PeerRequestResult<Vec<B>>> + Sync + Send + Unpin,
77{
78    type Output = PeerRequestResult<Option<B>>;
79
80    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
81        let resp = ready!(self.get_mut().fut.poll_unpin(cx));
82        let resp = resp.map(|res| res.map(|bodies| bodies.into_iter().next()));
83        Poll::Ready(resp)
84    }
85}