reth_network_p2p/
either.rs

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