From 21c14d8d432770334a8f01887590b42f2ff6c0f6 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 23 Oct 2024 20:02:33 -0400 Subject: [PATCH] factor types into `cuprate-types` --- Cargo.lock | 1 + .../cuprated/src/rpc/request/address_book.rs | 15 +-- p2p/p2p-core/Cargo.toml | 1 + p2p/p2p-core/src/types.rs | 120 +----------------- rpc/types/src/misc/misc.rs | 4 +- rpc/types/src/misc/mod.rs | 4 - .../src/misc => types/src}/address_type.rs | 42 ++++-- .../misc => types/src}/connection_state.rs | 44 +++++-- types/src/lib.rs | 4 + 9 files changed, 74 insertions(+), 161 deletions(-) rename {rpc/types/src/misc => types/src}/address_type.rs (76%) rename {rpc/types/src/misc => types/src}/connection_state.rs (75%) diff --git a/Cargo.lock b/Cargo.lock index ca0174b..f3957f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -834,6 +834,7 @@ dependencies = [ "cuprate-helper", "cuprate-pruning", "cuprate-test-utils", + "cuprate-types", "cuprate-wire", "futures", "hex", diff --git a/binaries/cuprated/src/rpc/request/address_book.rs b/binaries/cuprated/src/rpc/request/address_book.rs index 771acb2..fb787a2 100644 --- a/binaries/cuprated/src/rpc/request/address_book.rs +++ b/binaries/cuprated/src/rpc/request/address_book.rs @@ -55,17 +55,6 @@ pub(crate) async fn connection_info( let vec = vec .into_iter() .map(|info| { - /// Message to use when casting between enums with `u8` fails. - /// This should never happen. - const EXPECT: &str = "u8 repr between these types should be 1-1"; - - let address_type = - cuprate_rpc_types::misc::AddressType::from_u8(info.address_type.to_u8()) - .expect(EXPECT); - - let state = cuprate_rpc_types::misc::ConnectionState::from_u8(info.state.to_u8()) - .expect(EXPECT); - let (ip, port) = match info.socket_addr { Some(socket) => (socket.ip().to_string(), socket.port().to_string()), None => (String::new(), String::new()), @@ -73,7 +62,7 @@ pub(crate) async fn connection_info( ConnectionInfo { address: info.address.to_string(), - address_type, + address_type: info.address_type, avg_download: info.avg_download, avg_upload: info.avg_upload, connection_id: String::from(FIELD_NOT_SUPPORTED), @@ -95,7 +84,7 @@ pub(crate) async fn connection_info( rpc_port: info.rpc_port, send_count: info.send_count, send_idle_time: info.send_idle_time, - state, + state: info.state, support_flags: info.support_flags, } }) diff --git a/p2p/p2p-core/Cargo.toml b/p2p/p2p-core/Cargo.toml index a30590f..a13357e 100644 --- a/p2p/p2p-core/Cargo.toml +++ b/p2p/p2p-core/Cargo.toml @@ -13,6 +13,7 @@ borsh = ["dep:borsh", "cuprate-pruning/borsh"] cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false } cuprate-wire = { path = "../../net/wire", features = ["tracing"] } cuprate-pruning = { path = "../../pruning" } +cuprate-types = { path = "../../types" } tokio = { workspace = true, features = ["net", "sync", "macros", "time", "rt", "rt-multi-thread"]} tokio-util = { workspace = true, features = ["codec"] } diff --git a/p2p/p2p-core/src/types.rs b/p2p/p2p-core/src/types.rs index 083a703..3b7c3cb 100644 --- a/p2p/p2p-core/src/types.rs +++ b/p2p/p2p-core/src/types.rs @@ -3,6 +3,7 @@ use std::time::{Duration, Instant}; use cuprate_pruning::PruningSeed; +use cuprate_types::{AddressType, ConnectionState}; use crate::NetZoneAddress; @@ -24,125 +25,6 @@ pub struct BanState { pub unban_instant: Option, } -/// 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::types::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::types::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 { - Some(match u { - 0 => Self::Invalid, - 1 => Self::Ipv4, - 2 => Self::Ipv6, - 3 => Self::I2p, - 4 => Self::Tor, - _ => return None, - }) - } -} - -/// An enumeration of P2P connection states. -/// -/// Used [`ConnectionInfo::state`]. -/// -/// Original definition: -/// - -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[repr(u8)] -pub enum ConnectionState { - BeforeHandshake, - Synchronizing, - Standby, - Idle, - #[default] - Normal, -} - -impl ConnectionState { - /// Convert [`Self`] to a [`u8`]. - /// - /// ```rust - /// use cuprate_p2p_core::types::ConnectionState as C; - /// - /// assert_eq!(C::BeforeHandshake.to_u8(), 0); - /// assert_eq!(C::Synchronizing.to_u8(), 1); - /// assert_eq!(C::Standby.to_u8(), 2); - /// assert_eq!(C::Idle.to_u8(), 3); - /// assert_eq!(C::Normal.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::types::ConnectionState as C; - /// - /// assert_eq!(C::from_u8(0), Some(C::BeforeHandShake)); - /// assert_eq!(C::from_u8(1), Some(C::Synchronizing)); - /// assert_eq!(C::from_u8(2), Some(C::Standby)); - /// assert_eq!(C::from_u8(3), Some(C::Idle)); - /// assert_eq!(C::from_u8(4), Some(C::Normal)); - /// assert_eq!(C::from_u8(5), None); - /// ``` - pub const fn from_u8(u: u8) -> Option { - Some(match u { - 0 => Self::BeforeHandshake, - 1 => Self::Synchronizing, - 2 => Self::Standby, - 3 => Self::Idle, - 4 => Self::Normal, - _ => return None, - }) - } -} - -// TODO: reduce fields and map to RPC type. -// /// Data within [`crate::services::AddressBookResponse::ConnectionInfo`]. pub struct ConnectionInfo { // The following fields are mostly the same as `monerod`. diff --git a/rpc/types/src/misc/misc.rs b/rpc/types/src/misc/misc.rs index 38f1597..30e396c 100644 --- a/rpc/types/src/misc/misc.rs +++ b/rpc/types/src/misc/misc.rs @@ -110,7 +110,7 @@ define_struct_and_impl_epee! { /// Used in [`crate::json::GetConnectionsResponse`]. ConnectionInfo { address: String, - address_type: crate::misc::AddressType, + address_type: cuprate_types::AddressType, avg_download: u64, avg_upload: u64, connection_id: String, @@ -135,7 +135,7 @@ define_struct_and_impl_epee! { // Exists in the original definition, but isn't // used or (de)serialized for RPC purposes. // ssl: bool, - state: crate::misc::ConnectionState, + state: cuprate_types::ConnectionState, support_flags: u32, } } diff --git a/rpc/types/src/misc/mod.rs b/rpc/types/src/misc/mod.rs index 6f16403..e09f847 100644 --- a/rpc/types/src/misc/mod.rs +++ b/rpc/types/src/misc/mod.rs @@ -12,9 +12,7 @@ )] //---------------------------------------------------------------------------------------------------- Mod -mod address_type; mod binary_string; -mod connection_state; mod distribution; mod key_image_spent_status; #[expect(clippy::module_inception)] @@ -23,9 +21,7 @@ mod pool_info_extent; mod status; mod tx_entry; -pub use address_type::AddressType; pub use binary_string::BinaryString; -pub use connection_state::ConnectionState; pub use distribution::{Distribution, DistributionCompressedBinary, DistributionUncompressed}; pub use key_image_spent_status::KeyImageSpentStatus; pub use misc::{ diff --git a/rpc/types/src/misc/address_type.rs b/types/src/address_type.rs similarity index 76% rename from rpc/types/src/misc/address_type.rs rename to types/src/address_type.rs index ff2b382..3118293 100644 --- a/rpc/types/src/misc/address_type.rs +++ b/types/src/address_type.rs @@ -1,7 +1,5 @@ //! Types of network addresses; used in P2P. -use cuprate_epee_encoding::Marker; - #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -9,16 +7,38 @@ use serde::{Deserialize, Serialize}; use cuprate_epee_encoding::{ error, macros::bytes::{Buf, BufMut}, - EpeeValue, + EpeeValue, Marker, }; -/// Used in [`crate::misc::ConnectionInfo::address_type`]. -#[doc = crate::macros::monero_definition_link!( - cc73fe71162d564ffda8e549b79a350bca53c454, - "epee/include/net/enums.h", - 39..=47 +use strum::{ + AsRefStr, Display, EnumCount, EnumIs, EnumString, FromRepr, IntoStaticStr, VariantArray, +}; + +/// An enumeration of address types. +/// +/// Used in `cuprate_p2p` and `cuprate_types` +/// +/// Original definition: +/// - +#[derive( + Copy, + Clone, + Default, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + AsRefStr, + Display, + EnumCount, + EnumIs, + EnumString, + FromRepr, + IntoStaticStr, + VariantArray, )] -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(untagged, try_from = "u8", into = "u8"))] #[repr(u8)] @@ -35,7 +55,7 @@ impl AddressType { /// Convert [`Self`] to a [`u8`]. /// /// ```rust - /// use cuprate_rpc_types::misc::AddressType as A; + /// use cuprate_types::AddressType as A; /// /// assert_eq!(A::Invalid.to_u8(), 0); /// assert_eq!(A::Ipv4.to_u8(), 1); @@ -53,7 +73,7 @@ impl AddressType { /// This returns [`None`] if `u > 4`. /// /// ```rust - /// use cuprate_rpc_types::misc::AddressType as A; + /// use cuprate_types::AddressType as A; /// /// assert_eq!(A::from_u8(0), Some(A::Invalid)); /// assert_eq!(A::from_u8(1), Some(A::Ipv4)); diff --git a/rpc/types/src/misc/connection_state.rs b/types/src/connection_state.rs similarity index 75% rename from rpc/types/src/misc/connection_state.rs rename to types/src/connection_state.rs index aa8bb68..4dbd061 100644 --- a/rpc/types/src/misc/connection_state.rs +++ b/types/src/connection_state.rs @@ -1,6 +1,4 @@ -//! Types of network addresses; used in P2P. - -use cuprate_epee_encoding::Marker; +//! [`ConnectionState`]. #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -9,16 +7,38 @@ use serde::{Deserialize, Serialize}; use cuprate_epee_encoding::{ error, macros::bytes::{Buf, BufMut}, - EpeeValue, + EpeeValue, Marker, }; -/// Used in [`crate::misc::ConnectionInfo::address_type`]. -#[doc = crate::macros::monero_definition_link!( - cc73fe71162d564ffda8e549b79a350bca53c454, - "cryptonote_basic/connection_context.h", - 49..=56 +use strum::{ + AsRefStr, Display, EnumCount, EnumIs, EnumString, FromRepr, IntoStaticStr, VariantArray, +}; + +/// An enumeration of P2P connection states. +/// +/// Used in `cuprate_p2p` and `cuprate_rpc_types`. +/// +/// Original definition: +/// - +#[derive( + Copy, + Clone, + Default, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + AsRefStr, + Display, + EnumCount, + EnumIs, + EnumString, + FromRepr, + IntoStaticStr, + VariantArray, )] -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(untagged, try_from = "u8", into = "u8"))] #[repr(u8)] @@ -35,7 +55,7 @@ impl ConnectionState { /// Convert [`Self`] to a [`u8`]. /// /// ```rust - /// use cuprate_p2p_core::types::ConnectionState as C; + /// use cuprate_types::ConnectionState as C; /// /// assert_eq!(C::BeforeHandshake.to_u8(), 0); /// assert_eq!(C::Synchronizing.to_u8(), 1); @@ -53,7 +73,7 @@ impl ConnectionState { /// This returns [`None`] if `u > 4`. /// /// ```rust - /// use cuprate_p2p_core::types::ConnectionState as C; + /// use cuprate_types::ConnectionState as C; /// /// assert_eq!(C::from_u8(0), Some(C::BeforeHandShake)); /// assert_eq!(C::from_u8(1), Some(C::Synchronizing)); diff --git a/types/src/lib.rs b/types/src/lib.rs index fa35153..a5a04f9 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -9,12 +9,16 @@ // // Documentation for each module is located in the respective file. +mod address_type; mod block_complete_entry; +mod connection_state; mod hard_fork; mod transaction_verification_data; mod types; +pub use address_type::AddressType; pub use block_complete_entry::{BlockCompleteEntry, PrunedTxBlobEntry, TransactionBlobs}; +pub use connection_state::ConnectionState; pub use hard_fork::{HardFork, HardForkError}; pub use transaction_verification_data::{ CachedVerificationState, TransactionVerificationData, TxVersion,