mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-22 19:49:33 +00:00
factor types into cuprate-types
This commit is contained in:
parent
58a91c4e65
commit
21c14d8d43
9 changed files with 74 additions and 161 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -834,6 +834,7 @@ dependencies = [
|
|||
"cuprate-helper",
|
||||
"cuprate-pruning",
|
||||
"cuprate-test-utils",
|
||||
"cuprate-types",
|
||||
"cuprate-wire",
|
||||
"futures",
|
||||
"hex",
|
||||
|
|
|
@ -55,17 +55,6 @@ pub(crate) async fn connection_info<Z: NetworkZone>(
|
|||
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<Z: NetworkZone>(
|
|||
|
||||
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<Z: NetworkZone>(
|
|||
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,
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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<A: NetZoneAddress> {
|
|||
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::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<Self> {
|
||||
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:
|
||||
/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/cryptonote_basic/connection_context.h#L49>
|
||||
#[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<Self> {
|
||||
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<A: NetZoneAddress> {
|
||||
// The following fields are mostly the same as `monerod`.
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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::{
|
||||
|
|
|
@ -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:
|
||||
/// - <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/epee/include/net/enums.h/#L49>
|
||||
#[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));
|
|
@ -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:
|
||||
/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/cryptonote_basic/connection_context.h#L49>
|
||||
#[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));
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue