/peerlist

This commit is contained in:
hinto.janai 2024-12-13 19:50:19 -05:00
parent b3314d4981
commit c924f24765
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
6 changed files with 90 additions and 7 deletions

View file

@ -13,6 +13,7 @@ use cuprate_constants::rpc::{
};
use cuprate_helper::cast::usize_to_u64;
use cuprate_hex::Hex;
use cuprate_p2p_core::{client::handshaker::builder::DummyAddressBook, ClearNet};
use cuprate_rpc_interface::RpcHandler;
use cuprate_rpc_types::{
base::{AccessResponseBase, ResponseBase},
@ -50,6 +51,8 @@ use crate::{
},
};
use super::request::address_book;
/// Map a [`OtherRequest`] to the function that will lead to a [`OtherResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandler,
@ -483,9 +486,12 @@ async fn get_peer_list(
state: CupratedRpcHandler,
request: GetPeerListRequest,
) -> Result<GetPeerListResponse, Error> {
let (white_list, gray_list) = address_book::peerlist::<ClearNet>(&mut DummyAddressBook).await?;
Ok(GetPeerListResponse {
base: helper::response_base(false),
..todo!()
white_list,
gray_list,
})
}

View file

@ -1,11 +1,14 @@
//! Functions to send [`AddressBookRequest`]s.
use std::net::SocketAddrV4;
use anyhow::{anyhow, Error};
use cuprate_types::rpc::Peer;
use tower::ServiceExt;
use cuprate_helper::cast::usize_to_u64;
use cuprate_helper::{cast::usize_to_u64, map::u32_from_ipv4};
use cuprate_p2p_core::{
services::{AddressBookRequest, AddressBookResponse},
services::{AddressBookRequest, AddressBookResponse, ZoneSpecificPeerListEntryBase},
types::{BanState, ConnectionId},
AddressBook, NetworkZone,
};
@ -161,3 +164,62 @@ pub(crate) async fn get_bans<Z: NetworkZone>(
Ok(bans)
}
/// [`AddressBookRequest::Peerlist`]
pub(crate) async fn peerlist<Z: NetworkZone>(
address_book: &mut impl AddressBook<Z>,
) -> Result<(Vec<Peer>, Vec<Peer>), Error> {
let AddressBookResponse::Peerlist(peerlist) = address_book
.ready()
.await
.map_err(|e| anyhow!(e))?
.call(AddressBookRequest::Peerlist)
.await
.map_err(|e| anyhow!(e))?
else {
unreachable!();
};
fn map<Z: NetworkZone>(peers: Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>) -> Vec<Peer> {
peers
.into_iter()
.map(|peer| {
let ZoneSpecificPeerListEntryBase {
adr,
id,
last_seen,
pruning_seed,
rpc_port,
rpc_credits_per_hash,
} = peer;
let host = adr.to_string();
let (ip, port) = if let Ok(socket_addr) = host.parse::<SocketAddrV4>() {
(u32_from_ipv4(*socket_addr.ip()), socket_addr.port())
} else {
(0, 0)
};
let last_seen = last_seen.try_into().unwrap_or(0);
let pruning_seed = pruning_seed.compress();
Peer {
id,
host,
ip,
port,
rpc_port,
rpc_credits_per_hash,
last_seen,
pruning_seed,
}
})
.collect()
}
let white = map::<Z>(peerlist.white);
let grey = map::<Z>(peerlist.grey);
Ok((white, grey))
}

View file

@ -420,7 +420,8 @@ impl<Z: BorshNetworkZone> Service<AddressBookRequest<Z>> for AddressBook<Z> {
AddressBookRequest::GetBan(addr) => Ok(AddressBookResponse::GetBan {
unban_instant: self.peer_unban_instant(&addr).map(Instant::into_std),
}),
AddressBookRequest::PeerlistSize
AddressBookRequest::Peerlist
| AddressBookRequest::PeerlistSize
| AddressBookRequest::ConnectionCount
| AddressBookRequest::SetBan(_)
| AddressBookRequest::GetBans

View file

@ -108,7 +108,8 @@ impl<N: NetworkZone> Service<AddressBookRequest<N>> for DummyAddressBook {
AddressBookRequest::GetBan(_) => AddressBookResponse::GetBan {
unban_instant: None,
},
AddressBookRequest::PeerlistSize
AddressBookRequest::Peerlist
| AddressBookRequest::PeerlistSize
| AddressBookRequest::ConnectionCount
| AddressBookRequest::SetBan(_)
| AddressBookRequest::GetBans

View file

@ -6,7 +6,7 @@ use cuprate_wire::{CoreSyncData, PeerListEntryBase};
use crate::{
client::InternalPeerID,
handles::ConnectionHandle,
types::{BanState, ConnectionInfo, SetBan},
types::{BanState, ConnectionInfo, Peerlist, SetBan},
NetZoneAddress, NetworkAddressIncorrectZone, NetworkZone,
};
@ -115,6 +115,9 @@ pub enum AddressBookRequest<Z: NetworkZone> {
/// Gets the specified number of white peers, or less if we don't have enough.
GetWhitePeers(usize),
/// Get info on all peers, white & grey.
Peerlist,
/// Get the amount of white & grey peers.
PeerlistSize,
@ -152,6 +155,9 @@ pub enum AddressBookResponse<Z: NetworkZone> {
/// Response to [`AddressBookRequest::GetWhitePeers`].
Peers(Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>),
/// Response to [`AddressBookRequest::Peerlist`].
Peerlist(Peerlist<Z::Addr>),
/// Response to [`AddressBookRequest::PeerlistSize`].
PeerlistSize { white: usize, grey: usize },

View file

@ -5,7 +5,7 @@ use std::time::{Duration, Instant};
use cuprate_pruning::PruningSeed;
use cuprate_types::{AddressType, ConnectionState};
use crate::NetZoneAddress;
use crate::{NetZoneAddress, ZoneSpecificPeerListEntryBase};
/// Data within [`crate::services::AddressBookRequest::SetBan`].
pub struct SetBan<A: NetZoneAddress> {
@ -94,3 +94,10 @@ pub struct Span<A: NetZoneAddress> {
pub speed: u32,
pub start_block_height: u64,
}
/// Used in RPC's `/get_peer_list`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Peerlist<A: NetZoneAddress> {
pub white: Vec<ZoneSpecificPeerListEntryBase<A>>,
pub grey: Vec<ZoneSpecificPeerListEntryBase<A>>,
}