3
3
use std:: time:: { Duration , Instant } ;
4
4
5
5
use cuprate_pruning:: PruningSeed ;
6
+ use cuprate_types:: { AddressType , ConnectionState } ;
6
7
7
8
use crate :: NetZoneAddress ;
8
9
@@ -24,125 +25,6 @@ pub struct BanState<A: NetZoneAddress> {
24
25
pub unban_instant : Option < Instant > ,
25
26
}
26
27
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
- //
146
28
/// Data within [`crate::services::AddressBookResponse::ConnectionInfo`].
147
29
pub struct ConnectionInfo < A : NetZoneAddress > {
148
30
// The following fields are mostly the same as `monerod`.
0 commit comments