Skip to main content

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    receipts::client::ReceiptsClient,
11};
12use alloy_primitives::B256;
13
14pub use futures::future::Either;
15
16impl<A, B> DownloadClient for Either<A, B>
17where
18    A: DownloadClient,
19    B: DownloadClient,
20{
21    fn report_bad_message(&self, peer_id: reth_network_peers::PeerId) {
22        match self {
23            Self::Left(a) => a.report_bad_message(peer_id),
24            Self::Right(b) => b.report_bad_message(peer_id),
25        }
26    }
27    fn num_connected_peers(&self) -> usize {
28        match self {
29            Self::Left(a) => a.num_connected_peers(),
30            Self::Right(b) => b.num_connected_peers(),
31        }
32    }
33}
34
35impl<A, B> BodiesClient for Either<A, B>
36where
37    A: BodiesClient,
38    B: BodiesClient<Body = A::Body>,
39{
40    type Body = A::Body;
41    type Output = Either<A::Output, B::Output>;
42
43    fn get_block_bodies_with_priority_and_range_hint(
44        &self,
45        hashes: Vec<B256>,
46        priority: Priority,
47        range_hint: Option<RangeInclusive<u64>>,
48    ) -> Self::Output {
49        match self {
50            Self::Left(a) => Either::Left(
51                a.get_block_bodies_with_priority_and_range_hint(hashes, priority, range_hint),
52            ),
53            Self::Right(b) => Either::Right(
54                b.get_block_bodies_with_priority_and_range_hint(hashes, priority, range_hint),
55            ),
56        }
57    }
58}
59
60impl<A, B> HeadersClient for Either<A, B>
61where
62    A: HeadersClient,
63    B: HeadersClient<Header = A::Header>,
64{
65    type Header = A::Header;
66    type Output = Either<A::Output, B::Output>;
67
68    fn get_headers_with_priority(
69        &self,
70        request: HeadersRequest,
71        priority: Priority,
72    ) -> Self::Output {
73        match self {
74            Self::Left(a) => Either::Left(a.get_headers_with_priority(request, priority)),
75            Self::Right(b) => Either::Right(b.get_headers_with_priority(request, priority)),
76        }
77    }
78}
79
80impl<A, B> ReceiptsClient for Either<A, B>
81where
82    A: ReceiptsClient,
83    B: ReceiptsClient<Receipt = A::Receipt>,
84{
85    type Receipt = A::Receipt;
86    type Output = Either<A::Output, B::Output>;
87
88    fn get_receipts_with_priority(&self, hashes: Vec<B256>, priority: Priority) -> Self::Output {
89        match self {
90            Self::Left(a) => Either::Left(a.get_receipts_with_priority(hashes, priority)),
91            Self::Right(b) => Either::Right(b.get_receipts_with_priority(hashes, priority)),
92        }
93    }
94}