Skip to main content

reth_network_p2p/test_utils/
receipts.rs

1use crate::{
2    download::DownloadClient,
3    error::PeerRequestResult,
4    priority::Priority,
5    receipts::client::{ReceiptsClient, ReceiptsFut, ReceiptsResponse},
6};
7use alloy_primitives::B256;
8use futures::FutureExt;
9use reth_ethereum_primitives::Receipt;
10use reth_network_peers::PeerId;
11use std::fmt::{Debug, Formatter};
12use tokio::sync::oneshot;
13
14/// A test client for fetching receipts
15pub struct TestReceiptsClient<F> {
16    /// The function that is called on each receipt request.
17    pub responder: F,
18}
19
20impl<F> Debug for TestReceiptsClient<F> {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        f.debug_struct("TestReceiptsClient").finish_non_exhaustive()
23    }
24}
25
26impl<F: Sync + Send> DownloadClient for TestReceiptsClient<F> {
27    fn report_bad_message(&self, _peer_id: PeerId) {}
28
29    fn num_connected_peers(&self) -> usize {
30        0
31    }
32}
33
34impl<F> ReceiptsClient for TestReceiptsClient<F>
35where
36    F: Fn(Vec<B256>) -> PeerRequestResult<ReceiptsResponse<Receipt>> + Send + Sync,
37{
38    type Receipt = Receipt;
39    type Output = ReceiptsFut;
40
41    fn get_receipts_with_priority(&self, hashes: Vec<B256>, _priority: Priority) -> Self::Output {
42        let (tx, rx) = oneshot::channel();
43        let _ = tx.send((self.responder)(hashes));
44        Box::pin(rx.map(|x| match x {
45            Ok(value) => value,
46            Err(err) => Err(err.into()),
47        }))
48    }
49}