docs, ConnectionInfo, AddressType

This commit is contained in:
hinto.janai 2024-10-17 17:13:37 -04:00
parent bf07f2c84c
commit 98dfaa4870
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
6 changed files with 213 additions and 39 deletions
binaries/cuprated/src/rpc/request
p2p/p2p-core/src
rpc/types/src/misc

View file

@ -52,32 +52,45 @@ pub(crate) async fn connection_info<Z: NetworkZone>(
// FIXME: impl this map somewhere instead of inline. // FIXME: impl this map somewhere instead of inline.
let vec = vec let vec = vec
.into_iter() .into_iter()
.map(|info| ConnectionInfo { .map(|info| {
address: info.address.to_string(), use cuprate_p2p_core::types::AddressType as A1;
address_type: info.address_type, use cuprate_rpc_types::misc::AddressType as A2;
avg_download: info.avg_download,
avg_upload: info.avg_upload, let address_type = match info.address_type {
connection_id: info.connection_id, A1::Invalid => A2::Invalid,
current_download: info.current_download, A1::Ipv4 => A2::Ipv4,
current_upload: info.current_upload, A1::Ipv6 => A2::Ipv6,
height: info.height, A1::I2p => A2::I2p,
host: info.host, A1::Tor => A2::Tor,
incoming: info.incoming, };
ip: info.ip,
live_time: info.live_time, ConnectionInfo {
localhost: info.localhost, address: info.address.to_string(),
local_ip: info.local_ip, address_type,
peer_id: info.peer_id, avg_download: info.avg_download,
port: info.port, avg_upload: info.avg_upload,
pruning_seed: info.pruning_seed, connection_id: hex::encode(info.connection_id.to_ne_bytes()),
recv_count: info.recv_count, current_download: info.current_download,
recv_idle_time: info.recv_idle_time, current_upload: info.current_upload,
rpc_credits_per_hash: info.rpc_credits_per_hash, height: info.height,
rpc_port: info.rpc_port, host: info.host,
send_count: info.send_count, incoming: info.incoming,
send_idle_time: info.send_idle_time, ip: info.ip,
state: info.state, live_time: info.live_time,
support_flags: info.support_flags, localhost: info.localhost,
local_ip: info.local_ip,
peer_id: info.peer_id,
port: info.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(); .collect();
@ -177,10 +190,10 @@ pub(crate) async fn spans<Z: NetworkZone>(
let vec = vec let vec = vec
.into_iter() .into_iter()
.map(|span| Span { .map(|span| Span {
connection_id: span.connection_id, connection_id: hex::encode(span.connection_id.to_ne_bytes()),
nblocks: span.nblocks, nblocks: span.nblocks,
rate: span.rate, rate: span.rate,
remote_address: span.remote_address, remote_address: span.remote_address.to_string(),
size: span.size, size: span.size,
speed: span.speed, speed: span.speed,
start_block_height: span.start_block_height, start_block_height: span.start_block_height,

View file

@ -133,10 +133,13 @@ pub enum AddressBookRequest<Z: NetworkZone> {
/// Get the state of all bans. /// Get the state of all bans.
GetBans, GetBans,
/// TODO /// Get [`Span`] data.
///
/// This is data that describes an active downloading process,
/// if we are fully synced, this will return an empty [`Vec`].
Spans, Spans,
/// TODO /// Get the next [`PruningSeed`] needed for a pruned sync.
NextNeededPruningSeed, NextNeededPruningSeed,
} }
@ -177,7 +180,7 @@ pub enum AddressBookResponse<Z: NetworkZone> {
GetBans(Vec<BanState<Z::Addr>>), GetBans(Vec<BanState<Z::Addr>>),
/// Response to [`AddressBookRequest::Spans`]. /// Response to [`AddressBookRequest::Spans`].
Spans(Vec<Span>), Spans(Vec<Span<Z::Addr>>),
/// Response to [`AddressBookRequest::NextNeededPruningSeed`]. /// Response to [`AddressBookRequest::NextNeededPruningSeed`].
NextNeededPruningSeed(PruningSeed), NextNeededPruningSeed(PruningSeed),

View file

@ -2,6 +2,8 @@
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use cuprate_pruning::PruningSeed;
use crate::NetZoneAddress; use crate::NetZoneAddress;
/// Data within [`crate::services::AddressBookRequest::SetBan`]. /// Data within [`crate::services::AddressBookRequest::SetBan`].
@ -22,15 +24,72 @@ pub struct BanState<A: NetZoneAddress> {
pub unban_instant: Option<Instant>, pub unban_instant: Option<Instant>,
} }
/// An enumeration of address types.
///
/// Used [`ConnectionInfo::address_type`].
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum AddressType {
#[default]
Invalid,
Ipv4,
Ipv6,
I2p,
Tor,
}
impl AddressType {
/// Convert [`Self`] to a [`u8`].
///
/// ```rust
/// use cuprate_p2p_core::AddressType as A;
///
/// assert_eq!(A::Invalid.to_u8(), 0);
/// assert_eq!(A::Ipv4.to_u8(), 1);
/// assert_eq!(A::Ipv6.to_u8(), 2);
/// assert_eq!(A::I2p.to_u8(), 3);
/// assert_eq!(A::Tor.to_u8(), 4);
/// ```
pub const fn to_u8(self) -> u8 {
self as u8
}
/// Convert a [`u8`] to a [`Self`].
///
/// # Errors
/// This returns [`None`] if `u > 4`.
///
/// ```rust
/// use cuprate_p2p_core::AddressType as A;
///
/// assert_eq!(A::from_u8(0), Some(A::Invalid));
/// assert_eq!(A::from_u8(1), Some(A::Ipv4));
/// assert_eq!(A::from_u8(2), Some(A::Ipv6));
/// assert_eq!(A::from_u8(3), Some(A::I2p));
/// assert_eq!(A::from_u8(4), Some(A::Tor));
/// assert_eq!(A::from_u8(5), None);
/// ```
pub const fn from_u8(u: u8) -> Option<Self> {
Some(match u {
0 => Self::Invalid,
1 => Self::Ipv4,
2 => Self::Ipv6,
3 => Self::I2p,
4 => Self::Tor,
_ => return None,
})
}
}
// TODO: reduce fields and map to RPC type. // TODO: reduce fields and map to RPC type.
// //
/// Data within [`crate::services::AddressBookResponse::ConnectionInfo`]. /// Data within [`crate::services::AddressBookResponse::ConnectionInfo`].
pub struct ConnectionInfo<A: NetZoneAddress> { pub struct ConnectionInfo<A: NetZoneAddress> {
pub address: A, pub address: A,
pub address_type: u8, pub address_type: AddressType,
pub avg_download: u64, pub avg_download: u64,
pub avg_upload: u64, pub avg_upload: u64,
pub connection_id: String, pub connection_id: u64, // TODO: boost::uuids::uuid
pub current_download: u64, pub current_download: u64,
pub current_upload: u64, pub current_upload: u64,
pub height: u64, pub height: u64,
@ -42,14 +101,14 @@ pub struct ConnectionInfo<A: NetZoneAddress> {
pub local_ip: bool, pub local_ip: bool,
pub peer_id: String, pub peer_id: String,
pub port: String, pub port: String,
pub pruning_seed: u32, pub pruning_seed: PruningSeed,
pub recv_count: u64, pub recv_count: u64,
pub recv_idle_time: u64, pub recv_idle_time: u64,
pub rpc_credits_per_hash: u32, pub rpc_credits_per_hash: u32,
pub rpc_port: u16, pub rpc_port: u16,
pub send_count: u64, pub send_count: u64,
pub send_idle_time: u64, pub send_idle_time: u64,
pub state: String, pub state: String, // TODO: what type is this?
pub support_flags: u32, pub support_flags: u32,
} }
@ -57,11 +116,11 @@ pub struct ConnectionInfo<A: NetZoneAddress> {
/// ///
/// Data within [`crate::services::AddressBookResponse::Spans`]. /// Data within [`crate::services::AddressBookResponse::Spans`].
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span { pub struct Span<A: NetZoneAddress> {
pub connection_id: String, pub connection_id: u64, // TODO: boost::uuids::uuid
pub nblocks: u64, pub nblocks: u64,
pub rate: u32, pub rate: u32,
pub remote_address: String, pub remote_address: A,
pub size: u64, pub size: u64,
pub speed: u32, pub speed: u32,
pub start_block_height: u64, pub start_block_height: u64,

View file

@ -0,0 +1,97 @@
//! Types of network addresses; used in P2P.
use cuprate_epee_encoding::Marker;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "epee")]
use cuprate_epee_encoding::{
error,
macros::bytes::{Buf, BufMut},
EpeeValue,
};
/// Used in [`crate::misc::ConnectionInfo::address_type`].
#[doc = crate::macros::monero_definition_link!(
cc73fe71162d564ffda8e549b79a350bca53c454,
"epee/include/net/enums.h",
39..=47
)]
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
#[repr(u8)]
pub enum AddressType {
#[default]
Invalid,
Ipv4,
Ipv6,
I2p,
Tor,
}
impl AddressType {
/// Convert [`Self`] to a [`u8`].
///
/// ```rust
/// use cuprate_rpc_types::misc::AddressType as A;
///
/// assert_eq!(A::Invalid.to_u8(), 0);
/// assert_eq!(A::Ipv4.to_u8(), 1);
/// assert_eq!(A::Ipv6.to_u8(), 2);
/// assert_eq!(A::I2p.to_u8(), 3);
/// assert_eq!(A::Tor.to_u8(), 4);
/// ```
pub const fn to_u8(self) -> u8 {
self as u8
}
/// Convert a [`u8`] to a [`Self`].
///
/// # Errors
/// This returns [`None`] if `u > 4`.
///
/// ```rust
/// use cuprate_rpc_types::misc::AddressType as A;
///
/// assert_eq!(A::from_u8(0), Some(A::Invalid));
/// assert_eq!(A::from_u8(1), Some(A::Ipv4));
/// assert_eq!(A::from_u8(2), Some(A::Ipv6));
/// assert_eq!(A::from_u8(3), Some(A::I2p));
/// assert_eq!(A::from_u8(4), Some(A::Tor));
/// assert_eq!(A::from_u8(5), None);
/// ```
pub const fn from_u8(u: u8) -> Option<Self> {
Some(match u {
0 => Self::Invalid,
1 => Self::Ipv4,
2 => Self::Ipv6,
3 => Self::I2p,
4 => Self::Tor,
_ => return None,
})
}
}
impl From<AddressType> for u8 {
fn from(value: AddressType) -> Self {
value.to_u8()
}
}
#[cfg(feature = "epee")]
impl EpeeValue for AddressType {
const MARKER: Marker = u8::MARKER;
fn read<B: Buf>(r: &mut B, marker: &Marker) -> error::Result<Self> {
let u = u8::read(r, marker)?;
Self::from_u8(u).ok_or(error::Error::Format("u8 was greater than 4"))
}
fn write<B: BufMut>(self, w: &mut B) -> error::Result<()> {
let u = self.to_u8();
u8::write(u, w)?;
Ok(())
}
}

View file

@ -110,7 +110,7 @@ define_struct_and_impl_epee! {
/// Used in [`crate::json::GetConnectionsResponse`]. /// Used in [`crate::json::GetConnectionsResponse`].
ConnectionInfo { ConnectionInfo {
address: String, address: String,
address_type: u8, address_type: crate::misc::AddressType,
avg_download: u64, avg_download: u64,
avg_upload: u64, avg_upload: u64,
connection_id: String, connection_id: String,

View file

@ -12,6 +12,7 @@
)] )]
//---------------------------------------------------------------------------------------------------- Mod //---------------------------------------------------------------------------------------------------- Mod
mod address_type;
mod binary_string; mod binary_string;
mod distribution; mod distribution;
mod key_image_spent_status; mod key_image_spent_status;
@ -21,6 +22,7 @@ mod pool_info_extent;
mod status; mod status;
mod tx_entry; mod tx_entry;
pub use address_type::AddressType;
pub use binary_string::BinaryString; pub use binary_string::BinaryString;
pub use distribution::{Distribution, DistributionCompressedBinary, DistributionUncompressed}; pub use distribution::{Distribution, DistributionCompressedBinary, DistributionUncompressed};
pub use key_image_spent_status::KeyImageSpentStatus; pub use key_image_spent_status::KeyImageSpentStatus;