reth_network_p2p/bodies/
client.rsuse std::{
pin::Pin,
task::{ready, Context, Poll},
};
use crate::{download::DownloadClient, error::PeerRequestResult, priority::Priority};
use alloy_primitives::B256;
use futures::{Future, FutureExt};
use reth_primitives::BlockBody;
pub type BodiesFut<B = BlockBody> =
Pin<Box<dyn Future<Output = PeerRequestResult<Vec<B>>> + Send + Sync>>;
#[auto_impl::auto_impl(&, Arc, Box)]
pub trait BodiesClient: DownloadClient {
type Body: Send + Sync + Unpin + 'static;
type Output: Future<Output = PeerRequestResult<Vec<Self::Body>>> + Sync + Send + Unpin;
fn get_block_bodies(&self, hashes: Vec<B256>) -> Self::Output {
self.get_block_bodies_with_priority(hashes, Priority::Normal)
}
fn get_block_bodies_with_priority(&self, hashes: Vec<B256>, priority: Priority)
-> Self::Output;
fn get_block_body(&self, hash: B256) -> SingleBodyRequest<Self::Output> {
self.get_block_body_with_priority(hash, Priority::Normal)
}
fn get_block_body_with_priority(
&self,
hash: B256,
priority: Priority,
) -> SingleBodyRequest<Self::Output> {
let fut = self.get_block_bodies_with_priority(vec![hash], priority);
SingleBodyRequest { fut }
}
}
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct SingleBodyRequest<Fut> {
fut: Fut,
}
impl<Fut, B> Future for SingleBodyRequest<Fut>
where
Fut: Future<Output = PeerRequestResult<Vec<B>>> + Sync + Send + Unpin,
{
type Output = PeerRequestResult<Option<B>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let resp = ready!(self.get_mut().fut.poll_unpin(cx));
let resp = resp.map(|res| res.map(|bodies| bodies.into_iter().next()));
Poll::Ready(resp)
}
}