reth_network_p2p/
either.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Support for different download types.

use crate::{
    bodies::client::BodiesClient,
    download::DownloadClient,
    headers::client::{HeadersClient, HeadersRequest},
    priority::Priority,
};
use alloy_primitives::B256;

pub use futures::future::Either;

impl<A, B> DownloadClient for Either<A, B>
where
    A: DownloadClient,
    B: DownloadClient,
{
    fn report_bad_message(&self, peer_id: reth_network_peers::PeerId) {
        match self {
            Self::Left(a) => a.report_bad_message(peer_id),
            Self::Right(b) => b.report_bad_message(peer_id),
        }
    }
    fn num_connected_peers(&self) -> usize {
        match self {
            Self::Left(a) => a.num_connected_peers(),
            Self::Right(b) => b.num_connected_peers(),
        }
    }
}

impl<A, B> BodiesClient for Either<A, B>
where
    A: BodiesClient,
    B: BodiesClient<Body = A::Body>,
{
    type Body = A::Body;
    type Output = Either<A::Output, B::Output>;

    fn get_block_bodies_with_priority(
        &self,
        hashes: Vec<B256>,
        priority: Priority,
    ) -> Self::Output {
        match self {
            Self::Left(a) => Either::Left(a.get_block_bodies_with_priority(hashes, priority)),
            Self::Right(b) => Either::Right(b.get_block_bodies_with_priority(hashes, priority)),
        }
    }
}

impl<A, B> HeadersClient for Either<A, B>
where
    A: HeadersClient,
    B: HeadersClient<Header = A::Header>,
{
    type Header = A::Header;
    type Output = Either<A::Output, B::Output>;

    fn get_headers_with_priority(
        &self,
        request: HeadersRequest,
        priority: Priority,
    ) -> Self::Output {
        match self {
            Self::Left(a) => Either::Left(a.get_headers_with_priority(request, priority)),
            Self::Right(b) => Either::Right(b.get_headers_with_priority(request, priority)),
        }
    }
}