cuprated/rpc/request/
address_book.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Functions for TODO: doc enum message.

use std::convert::Infallible;

use anyhow::{anyhow, Error};
use tower::ServiceExt;

use cuprate_helper::cast::usize_to_u64;
use cuprate_p2p_core::{
    services::{AddressBookRequest, AddressBookResponse},
    types::{BanState, ConnectionId},
    AddressBook, NetworkZone,
};
use cuprate_pruning::PruningSeed;
use cuprate_rpc_types::misc::{ConnectionInfo, Span};

use crate::rpc::constants::FIELD_NOT_SUPPORTED;

// FIXME: use `anyhow::Error` over `tower::BoxError` in address book.

/// [`AddressBookRequest::PeerlistSize`]
pub(crate) async fn peerlist_size<Z: NetworkZone>(
    address_book: &mut impl AddressBook<Z>,
) -> Result<(u64, u64), Error> {
    let AddressBookResponse::PeerlistSize { white, grey } = address_book
        .ready()
        .await
        .map_err(|e| anyhow!(e))?
        .call(AddressBookRequest::PeerlistSize)
        .await
        .map_err(|e| anyhow!(e))?
    else {
        unreachable!();
    };

    Ok((usize_to_u64(white), usize_to_u64(grey)))
}

/// [`AddressBookRequest::ConnectionInfo`]
pub(crate) async fn connection_info<Z: NetworkZone>(
    address_book: &mut impl AddressBook<Z>,
) -> Result<Vec<ConnectionInfo>, Error> {
    let AddressBookResponse::ConnectionInfo(vec) = address_book
        .ready()
        .await
        .map_err(|e| anyhow!(e))?
        .call(AddressBookRequest::ConnectionInfo)
        .await
        .map_err(|e| anyhow!(e))?
    else {
        unreachable!();
    };

    // FIXME: impl this map somewhere instead of inline.
    let vec = vec
        .into_iter()
        .map(|info| {
            let (ip, port) = match info.socket_addr {
                Some(socket) => (socket.ip().to_string(), socket.port().to_string()),
                None => (String::new(), String::new()),
            };

            ConnectionInfo {
                address: info.address.to_string(),
                address_type: info.address_type,
                avg_download: info.avg_download,
                avg_upload: info.avg_upload,
                connection_id: String::from(ConnectionId::DEFAULT_STR),
                current_download: info.current_download,
                current_upload: info.current_upload,
                height: info.height,
                host: info.host,
                incoming: info.incoming,
                ip,
                live_time: info.live_time,
                localhost: info.localhost,
                local_ip: info.local_ip,
                peer_id: hex::encode(info.peer_id.to_ne_bytes()),
                port,
                pruning_seed: info.pruning_seed.compress(),
                recv_count: info.recv_count,
                recv_idle_time: info.recv_idle_time,
                rpc_credits_per_hash: info.rpc_credits_per_hash,
                rpc_port: info.rpc_port,
                send_count: info.send_count,
                send_idle_time: info.send_idle_time,
                state: info.state,
                support_flags: info.support_flags,
            }
        })
        .collect();

    Ok(vec)
}

/// [`AddressBookRequest::ConnectionCount`]
pub(crate) async fn connection_count<Z: NetworkZone>(
    address_book: &mut impl AddressBook<Z>,
) -> Result<(u64, u64), Error> {
    let AddressBookResponse::ConnectionCount { incoming, outgoing } = address_book
        .ready()
        .await
        .map_err(|e| anyhow!(e))?
        .call(AddressBookRequest::ConnectionCount)
        .await
        .map_err(|e| anyhow!(e))?
    else {
        unreachable!();
    };

    Ok((usize_to_u64(incoming), usize_to_u64(outgoing)))
}

/// [`AddressBookRequest::SetBan`]
pub(crate) async fn set_ban<Z: NetworkZone>(
    address_book: &mut impl AddressBook<Z>,
    set_ban: cuprate_p2p_core::types::SetBan<Z::Addr>,
) -> Result<(), Error> {
    let AddressBookResponse::Ok = address_book
        .ready()
        .await
        .map_err(|e| anyhow!(e))?
        .call(AddressBookRequest::SetBan(set_ban))
        .await
        .map_err(|e| anyhow!(e))?
    else {
        unreachable!();
    };

    Ok(())
}

/// [`AddressBookRequest::GetBan`]
pub(crate) async fn get_ban<Z: NetworkZone>(
    address_book: &mut impl AddressBook<Z>,
    peer: Z::Addr,
) -> Result<Option<std::time::Instant>, Error> {
    let AddressBookResponse::GetBan { unban_instant } = address_book
        .ready()
        .await
        .map_err(|e| anyhow!(e))?
        .call(AddressBookRequest::GetBan(peer))
        .await
        .map_err(|e| anyhow!(e))?
    else {
        unreachable!();
    };

    Ok(unban_instant)
}

/// [`AddressBookRequest::GetBans`]
pub(crate) async fn get_bans<Z: NetworkZone>(
    address_book: &mut impl AddressBook<Z>,
) -> Result<Vec<BanState<Z::Addr>>, Error> {
    let AddressBookResponse::GetBans(bans) = address_book
        .ready()
        .await
        .map_err(|e| anyhow!(e))?
        .call(AddressBookRequest::GetBans)
        .await
        .map_err(|e| anyhow!(e))?
    else {
        unreachable!();
    };

    Ok(bans)
}