reth_network_p2p/bodies/
client.rs1use 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
12pub type BodiesFut<B = reth_ethereum_primitives::BlockBody> =
14 Pin<Box<dyn Future<Output = PeerRequestResult<Vec<B>>> + Send + Sync>>;
15
16#[auto_impl::auto_impl(&, Arc, Box)]
18pub trait BodiesClient: DownloadClient {
19 type Body: BlockBody;
21 type Output: Future<Output = PeerRequestResult<Vec<Self::Body>>> + Sync + Send + Unpin;
23
24 fn get_block_bodies(&self, hashes: Vec<B256>) -> Self::Output {
26 self.get_block_bodies_with_priority(hashes, Priority::Normal)
27 }
28
29 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 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 fn get_block_body(&self, hash: B256) -> SingleBodyRequest<Self::Output> {
53 self.get_block_body_with_priority(hash, Priority::Normal)
54 }
55
56 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#[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}