use crate::{ bodies::client::{BodiesClient, BodiesFut}, download::DownloadClient, error::PeerRequestResult, priority::Priority, }; use futures::FutureExt; use reth_primitives::{BlockBody, B256}; use std::fmt::{Debug, Formatter}; use tokio::sync::oneshot; /// A test client for fetching bodies pub struct TestBodiesClient { /// The function that is called on each body request. pub responder: F, } impl Debug for TestBodiesClient { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("TestBodiesClient").finish_non_exhaustive() } } impl DownloadClient for TestBodiesClient { fn report_bad_message(&self, _peer_id: reth_network_peers::PeerId) { // noop } fn num_connected_peers(&self) -> usize { 0 } } impl BodiesClient for TestBodiesClient where F: Fn(Vec) -> PeerRequestResult> + Send + Sync, { type Output = BodiesFut; fn get_block_bodies_with_priority( &self, hashes: Vec, _priority: Priority, ) -> Self::Output { let (tx, rx) = oneshot::channel(); let _ = tx.send((self.responder)(hashes)); Box::pin(rx.map(|x| match x { Ok(value) => value, Err(err) => Err(err.into()), })) } }