Skip to content

Commit 21c14d8

Browse files
committed
factor types into cuprate-types
1 parent 58a91c4 commit 21c14d8

File tree

9 files changed

+74
-161
lines changed

9 files changed

+74
-161
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

binaries/cuprated/src/rpc/request/address_book.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,14 @@ pub(crate) async fn connection_info<Z: NetworkZone>(
5555
let vec = vec
5656
.into_iter()
5757
.map(|info| {
58-
/// Message to use when casting between enums with `u8` fails.
59-
/// This should never happen.
60-
const EXPECT: &str = "u8 repr between these types should be 1-1";
61-
62-
let address_type =
63-
cuprate_rpc_types::misc::AddressType::from_u8(info.address_type.to_u8())
64-
.expect(EXPECT);
65-
66-
let state = cuprate_rpc_types::misc::ConnectionState::from_u8(info.state.to_u8())
67-
.expect(EXPECT);
68-
6958
let (ip, port) = match info.socket_addr {
7059
Some(socket) => (socket.ip().to_string(), socket.port().to_string()),
7160
None => (String::new(), String::new()),
7261
};
7362

7463
ConnectionInfo {
7564
address: info.address.to_string(),
76-
address_type,
65+
address_type: info.address_type,
7766
avg_download: info.avg_download,
7867
avg_upload: info.avg_upload,
7968
connection_id: String::from(FIELD_NOT_SUPPORTED),
@@ -95,7 +84,7 @@ pub(crate) async fn connection_info<Z: NetworkZone>(
9584
rpc_port: info.rpc_port,
9685
send_count: info.send_count,
9786
send_idle_time: info.send_idle_time,
98-
state,
87+
state: info.state,
9988
support_flags: info.support_flags,
10089
}
10190
})

p2p/p2p-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ borsh = ["dep:borsh", "cuprate-pruning/borsh"]
1313
cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false }
1414
cuprate-wire = { path = "../../net/wire", features = ["tracing"] }
1515
cuprate-pruning = { path = "../../pruning" }
16+
cuprate-types = { path = "../../types" }
1617

1718
tokio = { workspace = true, features = ["net", "sync", "macros", "time", "rt", "rt-multi-thread"]}
1819
tokio-util = { workspace = true, features = ["codec"] }

p2p/p2p-core/src/types.rs

Lines changed: 1 addition & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::time::{Duration, Instant};
44

55
use cuprate_pruning::PruningSeed;
6+
use cuprate_types::{AddressType, ConnectionState};
67

78
use crate::NetZoneAddress;
89

@@ -24,125 +25,6 @@ pub struct BanState<A: NetZoneAddress> {
2425
pub unban_instant: Option<Instant>,
2526
}
2627

27-
/// An enumeration of address types.
28-
///
29-
/// Used [`ConnectionInfo::address_type`].
30-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
31-
#[repr(u8)]
32-
pub enum AddressType {
33-
#[default]
34-
Invalid,
35-
Ipv4,
36-
Ipv6,
37-
I2p,
38-
Tor,
39-
}
40-
41-
impl AddressType {
42-
/// Convert [`Self`] to a [`u8`].
43-
///
44-
/// ```rust
45-
/// use cuprate_p2p_core::types::AddressType as A;
46-
///
47-
/// assert_eq!(A::Invalid.to_u8(), 0);
48-
/// assert_eq!(A::Ipv4.to_u8(), 1);
49-
/// assert_eq!(A::Ipv6.to_u8(), 2);
50-
/// assert_eq!(A::I2p.to_u8(), 3);
51-
/// assert_eq!(A::Tor.to_u8(), 4);
52-
/// ```
53-
pub const fn to_u8(self) -> u8 {
54-
self as u8
55-
}
56-
57-
/// Convert a [`u8`] to a [`Self`].
58-
///
59-
/// # Errors
60-
/// This returns [`None`] if `u > 4`.
61-
///
62-
/// ```rust
63-
/// use cuprate_p2p_core::types::AddressType as A;
64-
///
65-
/// assert_eq!(A::from_u8(0), Some(A::Invalid));
66-
/// assert_eq!(A::from_u8(1), Some(A::Ipv4));
67-
/// assert_eq!(A::from_u8(2), Some(A::Ipv6));
68-
/// assert_eq!(A::from_u8(3), Some(A::I2p));
69-
/// assert_eq!(A::from_u8(4), Some(A::Tor));
70-
/// assert_eq!(A::from_u8(5), None);
71-
/// ```
72-
pub const fn from_u8(u: u8) -> Option<Self> {
73-
Some(match u {
74-
0 => Self::Invalid,
75-
1 => Self::Ipv4,
76-
2 => Self::Ipv6,
77-
3 => Self::I2p,
78-
4 => Self::Tor,
79-
_ => return None,
80-
})
81-
}
82-
}
83-
84-
/// An enumeration of P2P connection states.
85-
///
86-
/// Used [`ConnectionInfo::state`].
87-
///
88-
/// Original definition:
89-
/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/cryptonote_basic/connection_context.h#L49>
90-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
91-
#[repr(u8)]
92-
pub enum ConnectionState {
93-
BeforeHandshake,
94-
Synchronizing,
95-
Standby,
96-
Idle,
97-
#[default]
98-
Normal,
99-
}
100-
101-
impl ConnectionState {
102-
/// Convert [`Self`] to a [`u8`].
103-
///
104-
/// ```rust
105-
/// use cuprate_p2p_core::types::ConnectionState as C;
106-
///
107-
/// assert_eq!(C::BeforeHandshake.to_u8(), 0);
108-
/// assert_eq!(C::Synchronizing.to_u8(), 1);
109-
/// assert_eq!(C::Standby.to_u8(), 2);
110-
/// assert_eq!(C::Idle.to_u8(), 3);
111-
/// assert_eq!(C::Normal.to_u8(), 4);
112-
/// ```
113-
pub const fn to_u8(self) -> u8 {
114-
self as u8
115-
}
116-
117-
/// Convert a [`u8`] to a [`Self`].
118-
///
119-
/// # Errors
120-
/// This returns [`None`] if `u > 4`.
121-
///
122-
/// ```rust
123-
/// use cuprate_p2p_core::types::ConnectionState as C;
124-
///
125-
/// assert_eq!(C::from_u8(0), Some(C::BeforeHandShake));
126-
/// assert_eq!(C::from_u8(1), Some(C::Synchronizing));
127-
/// assert_eq!(C::from_u8(2), Some(C::Standby));
128-
/// assert_eq!(C::from_u8(3), Some(C::Idle));
129-
/// assert_eq!(C::from_u8(4), Some(C::Normal));
130-
/// assert_eq!(C::from_u8(5), None);
131-
/// ```
132-
pub const fn from_u8(u: u8) -> Option<Self> {
133-
Some(match u {
134-
0 => Self::BeforeHandshake,
135-
1 => Self::Synchronizing,
136-
2 => Self::Standby,
137-
3 => Self::Idle,
138-
4 => Self::Normal,
139-
_ => return None,
140-
})
141-
}
142-
}
143-
144-
// TODO: reduce fields and map to RPC type.
145-
//
14628
/// Data within [`crate::services::AddressBookResponse::ConnectionInfo`].
14729
pub struct ConnectionInfo<A: NetZoneAddress> {
14830
// The following fields are mostly the same as `monerod`.

rpc/types/src/misc/misc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ define_struct_and_impl_epee! {
110110
/// Used in [`crate::json::GetConnectionsResponse`].
111111
ConnectionInfo {
112112
address: String,
113-
address_type: crate::misc::AddressType,
113+
address_type: cuprate_types::AddressType,
114114
avg_download: u64,
115115
avg_upload: u64,
116116
connection_id: String,
@@ -135,7 +135,7 @@ define_struct_and_impl_epee! {
135135
// Exists in the original definition, but isn't
136136
// used or (de)serialized for RPC purposes.
137137
// ssl: bool,
138-
state: crate::misc::ConnectionState,
138+
state: cuprate_types::ConnectionState,
139139
support_flags: u32,
140140
}
141141
}

rpc/types/src/misc/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
)]
1313

1414
//---------------------------------------------------------------------------------------------------- Mod
15-
mod address_type;
1615
mod binary_string;
17-
mod connection_state;
1816
mod distribution;
1917
mod key_image_spent_status;
2018
#[expect(clippy::module_inception)]
@@ -23,9 +21,7 @@ mod pool_info_extent;
2321
mod status;
2422
mod tx_entry;
2523

26-
pub use address_type::AddressType;
2724
pub use binary_string::BinaryString;
28-
pub use connection_state::ConnectionState;
2925
pub use distribution::{Distribution, DistributionCompressedBinary, DistributionUncompressed};
3026
pub use key_image_spent_status::KeyImageSpentStatus;
3127
pub use misc::{

rpc/types/src/misc/address_type.rs renamed to types/src/address_type.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
//! Types of network addresses; used in P2P.
22
3-
use cuprate_epee_encoding::Marker;
4-
53
#[cfg(feature = "serde")]
64
use serde::{Deserialize, Serialize};
75

86
#[cfg(feature = "epee")]
97
use cuprate_epee_encoding::{
108
error,
119
macros::bytes::{Buf, BufMut},
12-
EpeeValue,
10+
EpeeValue, Marker,
11+
};
12+
13+
use strum::{
14+
AsRefStr, Display, EnumCount, EnumIs, EnumString, FromRepr, IntoStaticStr, VariantArray,
1315
};
1416

15-
/// Used in [`crate::misc::ConnectionInfo::address_type`].
16-
#[doc = crate::macros::monero_definition_link!(
17-
cc73fe71162d564ffda8e549b79a350bca53c454,
18-
"epee/include/net/enums.h",
19-
39..=47
17+
/// An enumeration of address types.
18+
///
19+
/// Used in `cuprate_p2p` and `cuprate_types`
20+
///
21+
/// Original definition:
22+
/// - <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/epee/include/net/enums.h/#L49>
23+
#[derive(
24+
Copy,
25+
Clone,
26+
Default,
27+
Debug,
28+
PartialEq,
29+
Eq,
30+
PartialOrd,
31+
Ord,
32+
Hash,
33+
AsRefStr,
34+
Display,
35+
EnumCount,
36+
EnumIs,
37+
EnumString,
38+
FromRepr,
39+
IntoStaticStr,
40+
VariantArray,
2041
)]
21-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2242
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2343
#[cfg_attr(feature = "serde", serde(untagged, try_from = "u8", into = "u8"))]
2444
#[repr(u8)]
@@ -35,7 +55,7 @@ impl AddressType {
3555
/// Convert [`Self`] to a [`u8`].
3656
///
3757
/// ```rust
38-
/// use cuprate_rpc_types::misc::AddressType as A;
58+
/// use cuprate_types::AddressType as A;
3959
///
4060
/// assert_eq!(A::Invalid.to_u8(), 0);
4161
/// assert_eq!(A::Ipv4.to_u8(), 1);
@@ -53,7 +73,7 @@ impl AddressType {
5373
/// This returns [`None`] if `u > 4`.
5474
///
5575
/// ```rust
56-
/// use cuprate_rpc_types::misc::AddressType as A;
76+
/// use cuprate_types::AddressType as A;
5777
///
5878
/// assert_eq!(A::from_u8(0), Some(A::Invalid));
5979
/// assert_eq!(A::from_u8(1), Some(A::Ipv4));

rpc/types/src/misc/connection_state.rs renamed to types/src/connection_state.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
//! Types of network addresses; used in P2P.
2-
3-
use cuprate_epee_encoding::Marker;
1+
//! [`ConnectionState`].
42
53
#[cfg(feature = "serde")]
64
use serde::{Deserialize, Serialize};
@@ -9,16 +7,38 @@ use serde::{Deserialize, Serialize};
97
use cuprate_epee_encoding::{
108
error,
119
macros::bytes::{Buf, BufMut},
12-
EpeeValue,
10+
EpeeValue, Marker,
11+
};
12+
13+
use strum::{
14+
AsRefStr, Display, EnumCount, EnumIs, EnumString, FromRepr, IntoStaticStr, VariantArray,
1315
};
1416

15-
/// Used in [`crate::misc::ConnectionInfo::address_type`].
16-
#[doc = crate::macros::monero_definition_link!(
17-
cc73fe71162d564ffda8e549b79a350bca53c454,
18-
"cryptonote_basic/connection_context.h",
19-
49..=56
17+
/// An enumeration of P2P connection states.
18+
///
19+
/// Used in `cuprate_p2p` and `cuprate_rpc_types`.
20+
///
21+
/// Original definition:
22+
/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/cryptonote_basic/connection_context.h#L49>
23+
#[derive(
24+
Copy,
25+
Clone,
26+
Default,
27+
Debug,
28+
PartialEq,
29+
Eq,
30+
PartialOrd,
31+
Ord,
32+
Hash,
33+
AsRefStr,
34+
Display,
35+
EnumCount,
36+
EnumIs,
37+
EnumString,
38+
FromRepr,
39+
IntoStaticStr,
40+
VariantArray,
2041
)]
21-
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2242
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2343
#[cfg_attr(feature = "serde", serde(untagged, try_from = "u8", into = "u8"))]
2444
#[repr(u8)]
@@ -35,7 +55,7 @@ impl ConnectionState {
3555
/// Convert [`Self`] to a [`u8`].
3656
///
3757
/// ```rust
38-
/// use cuprate_p2p_core::types::ConnectionState as C;
58+
/// use cuprate_types::ConnectionState as C;
3959
///
4060
/// assert_eq!(C::BeforeHandshake.to_u8(), 0);
4161
/// assert_eq!(C::Synchronizing.to_u8(), 1);
@@ -53,7 +73,7 @@ impl ConnectionState {
5373
/// This returns [`None`] if `u > 4`.
5474
///
5575
/// ```rust
56-
/// use cuprate_p2p_core::types::ConnectionState as C;
76+
/// use cuprate_types::ConnectionState as C;
5777
///
5878
/// assert_eq!(C::from_u8(0), Some(C::BeforeHandShake));
5979
/// assert_eq!(C::from_u8(1), Some(C::Synchronizing));

types/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
//
1010
// Documentation for each module is located in the respective file.
1111

12+
mod address_type;
1213
mod block_complete_entry;
14+
mod connection_state;
1315
mod hard_fork;
1416
mod transaction_verification_data;
1517
mod types;
1618

19+
pub use address_type::AddressType;
1720
pub use block_complete_entry::{BlockCompleteEntry, PrunedTxBlobEntry, TransactionBlobs};
21+
pub use connection_state::ConnectionState;
1822
pub use hard_fork::{HardFork, HardForkError};
1923
pub use transaction_verification_data::{
2024
CachedVerificationState, TransactionVerificationData, TxVersion,

0 commit comments

Comments
 (0)