reth_network_p2p/
either.rs

1//! Support for different download types.
2
3use crate::{
4    bodies::client::BodiesClient,
5    download::DownloadClient,
6    headers::client::{HeadersClient, HeadersRequest},
7    priority::Priority,
8};
9use alloy_primitives::B256;
10
11pub use futures::future::Either;
12
13impl<A, B> DownloadClient for Either<A, B>
14where
15    A: DownloadClient,
16    B: DownloadClient,
17{
18    fn report_bad_message(&self, peer_id: reth_network_peers::PeerId) {
19        match self {
20            Self::Left(a) => a.report_bad_message(peer_id),
21            Self::Right(b) => b.report_bad_message(peer_id),
22        }
23    }
24    fn num_connected_peers(&self) -> usize {
25        match self {
26            Self::Left(a) => a.num_connected_peers(),
27            Self::Right(b) => b.num_connected_peers(),
28        }
29    }
30}
31
32impl<A, B> BodiesClient for Either<A, B>
33where
34    A: BodiesClient,
35    B: BodiesClient<Body = A::Body>,
36{
37    type Body = A::Body;
38    type Output = Either<A::Output, B::Output>;
39
40    fn get_block_bodies_with_priority(
41        &self,
42        hashes: Vec<B256>,
43        priority: Priority,
44    ) -> Self::Output {
45        match self {
46            Self::Left(a) => Either::Left(a.get_block_bodies_with_priority(hashes, priority)),
47            Self::Right(b) => Either::Right(b.get_block_bodies_with_priority(hashes, priority)),
48        }
49    }
50}
51
52impl<A, B> HeadersClient for Either<A, B>
53where
54    A: HeadersClient,
55    B: HeadersClient<Header = A::Header>,
56{
57    type Header = A::Header;
58    type Output = Either<A::Output, B::Output>;
59
60    fn get_headers_with_priority(
61        &self,
62        request: HeadersRequest,
63        priority: Priority,
64    ) -> Self::Output {
65        match self {
66            Self::Left(a) => Either::Left(a.get_headers_with_priority(request, priority)),
67            Self::Right(b) => Either::Right(b.get_headers_with_priority(request, priority)),
68        }
69    }
70}