forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork_id.rs
345 lines (304 loc) · 10.7 KB
/
network_id.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0
use crate::config::{PeerRole, RoleType};
use aptos_types::PeerId;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use short_hex_str::AsShortHexStr;
use std::{fmt, str::FromStr};
/// A grouping of common information between all networking code for logging.
/// This should greatly reduce the groupings between these given everywhere, and will allow
/// for logging accordingly.
#[derive(Clone, Copy, Eq, PartialEq, Serialize)]
pub struct NetworkContext {
/// The type of node
role: RoleType,
#[serde(serialize_with = "NetworkId::serialize_str")]
network_id: NetworkId,
peer_id: PeerId,
}
impl fmt::Debug for NetworkContext {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl fmt::Display for NetworkContext {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{},{},{}]",
self.role,
self.network_id.as_str(),
self.peer_id.short_str(),
)
}
}
impl NetworkContext {
pub fn new(role: RoleType, network_id: NetworkId, peer_id: PeerId) -> NetworkContext {
NetworkContext {
role,
network_id,
peer_id,
}
}
pub fn role(&self) -> RoleType {
self.role
}
pub fn network_id(&self) -> NetworkId {
self.network_id
}
pub fn peer_id(&self) -> PeerId {
self.peer_id
}
#[cfg(any(test, feature = "testing", feature = "fuzzing"))]
pub fn mock_with_peer_id(peer_id: PeerId) -> Self {
Self::new(RoleType::Validator, NetworkId::Validator, peer_id)
}
#[cfg(any(test, feature = "testing", feature = "fuzzing"))]
pub fn mock() -> Self {
Self::new(RoleType::Validator, NetworkId::Validator, PeerId::random())
}
}
/// A representation of the network being used in communication.
/// There should only be one of each NetworkId used for a single node (except for NetworkId::Public),
/// and handshakes should verify that the NetworkId being used is the same during a handshake,
/// to effectively ensure communication is restricted to a network. Network should be checked that
/// it is not the `DEFAULT_NETWORK`
#[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[repr(u8)]
pub enum NetworkId {
Validator = 0,
Vfn = 3,
Public = 4,
}
// This serializer is here for backwards compatibility with the old version, once all nodes have the
// new format, we can do a migration path towards the current representations
impl Serialize for NetworkId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
#[derive(Serialize)]
#[serde(rename = "NetworkId", rename_all = "snake_case")]
enum ConvertNetworkId {
Validator,
Public,
Private(String),
}
let converted = match self {
NetworkId::Validator => ConvertNetworkId::Validator,
NetworkId::Public => ConvertNetworkId::Public,
// TODO: Once all validators & VFNs are on this version, convert to using new serialization as number
NetworkId::Vfn => ConvertNetworkId::Private(VFN_NETWORK.to_string()),
};
converted.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for NetworkId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(rename = "NetworkId", rename_all = "snake_case")]
enum ConvertNetworkId {
Validator,
Public,
Private(String),
// These are here for migration, since both need to have their representation changed
// in the 2nd step of migration, we can move to these identifiers
Vfn,
NewPublic,
}
// A hack around NetworkId to convert the old type to the new version
match ConvertNetworkId::deserialize(deserializer)? {
ConvertNetworkId::Validator => Ok(NetworkId::Validator),
ConvertNetworkId::Public => Ok(NetworkId::Public),
ConvertNetworkId::Vfn => Ok(NetworkId::Vfn),
ConvertNetworkId::NewPublic => Ok(NetworkId::Public),
// Technically, there could be a different private network, but it isn't used right now
ConvertNetworkId::Private(_) => Ok(NetworkId::Vfn),
}
}
}
/// Default needed to handle downstream structs that use `Default`
impl Default for NetworkId {
fn default() -> NetworkId {
NetworkId::Public
}
}
impl fmt::Debug for NetworkId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl fmt::Display for NetworkId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}
const VFN_NETWORK: &str = "vfn";
impl NetworkId {
pub fn is_vfn_network(&self) -> bool {
self == &NetworkId::Vfn
}
pub fn is_validator_network(&self) -> bool {
self == &NetworkId::Validator
}
/// Roles for a prioritization of relative upstreams
pub fn upstream_roles(&self, role: &RoleType) -> &'static [PeerRole] {
match self {
NetworkId::Validator => &[PeerRole::Validator],
NetworkId::Public => &[
PeerRole::PreferredUpstream,
PeerRole::Upstream,
PeerRole::ValidatorFullNode,
],
NetworkId::Vfn => match role {
RoleType::Validator => &[],
RoleType::FullNode => &[PeerRole::Validator],
},
}
}
/// Roles for a prioritization of relative downstreams
pub fn downstream_roles(&self, role: &RoleType) -> &'static [PeerRole] {
match self {
NetworkId::Validator => &[PeerRole::Validator],
// In order to allow fallbacks, we must allow for nodes to accept ValidatorFullNodes
NetworkId::Public => &[
PeerRole::ValidatorFullNode,
PeerRole::Downstream,
PeerRole::Known,
PeerRole::Unknown,
],
NetworkId::Vfn => match role {
RoleType::Validator => &[PeerRole::ValidatorFullNode],
RoleType::FullNode => &[],
},
}
}
pub fn as_str(&self) -> &str {
match self {
NetworkId::Validator => "Validator",
NetworkId::Public => "Public",
NetworkId::Vfn => VFN_NETWORK,
}
}
fn serialize_str<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
self.as_str().serialize(serializer)
}
}
impl FromStr for NetworkId {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"validator" => Ok(NetworkId::Validator),
"public" => Ok(NetworkId::Public),
VFN_NETWORK => Ok(NetworkId::Vfn),
_ => Err("Invalid network name"),
}
}
}
#[derive(Clone, Copy, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
/// Identifier of a node, represented as (network_id, peer_id)
pub struct PeerNetworkId {
network_id: NetworkId,
peer_id: PeerId,
}
impl PeerNetworkId {
pub fn new(network_id: NetworkId, peer_id: PeerId) -> Self {
Self {
network_id,
peer_id,
}
}
pub fn network_id(&self) -> NetworkId {
self.network_id
}
pub fn peer_id(&self) -> PeerId {
self.peer_id
}
#[cfg(any(test, feature = "fuzzing"))]
pub fn random() -> Self {
Self::new(NetworkId::Public, PeerId::random())
}
#[cfg(any(test, feature = "fuzzing"))]
pub fn random_validator() -> Self {
Self::new(NetworkId::Validator, PeerId::random())
}
}
impl fmt::Debug for PeerNetworkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}
impl fmt::Display for PeerNetworkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.network_id(), self.peer_id().short_str(),)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ensure_network_id_order() {
assert!(NetworkId::Validator < NetworkId::Vfn);
assert!(NetworkId::Vfn < NetworkId::Public);
assert!(NetworkId::Validator < NetworkId::Public);
}
#[test]
fn test_serialization() {
for id in [NetworkId::Validator, NetworkId::Vfn, NetworkId::Public] {
let encoded = serde_yaml::to_string(&id).unwrap();
let decoded: NetworkId = serde_yaml::from_str(encoded.as_str()).unwrap();
assert_eq!(id, decoded);
let encoded = bcs::to_bytes(&id).unwrap();
let decoded: NetworkId = bcs::from_bytes(&encoded).unwrap();
assert_eq!(id, decoded);
}
}
#[test]
fn test_network_context_serialization() {
let peer_id = PeerId::random();
let context = NetworkContext::new(RoleType::Validator, NetworkId::Vfn, peer_id);
let expected = format!(
"---\nrole: {}\nnetwork_id: {}\npeer_id: {:x}\n",
RoleType::Validator,
VFN_NETWORK,
peer_id
);
assert_eq!(expected, serde_yaml::to_string(&context).unwrap());
}
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename = "NetworkId", rename_all = "snake_case")]
enum OldNetworkId {
Validator,
Public,
Private(String),
}
#[test]
fn test_backwards_compatibility() {
for (old, new) in [
(OldNetworkId::Validator, NetworkId::Validator),
(OldNetworkId::Public, NetworkId::Public),
(
OldNetworkId::Private(VFN_NETWORK.to_string()),
NetworkId::Vfn,
),
] {
// Old version can be decoded as new version
let encoded = serde_yaml::to_string(&old).unwrap();
let decoded: NetworkId = serde_yaml::from_str(&encoded).unwrap();
assert_eq!(new, decoded);
let encoded = bcs::to_bytes(&old).unwrap();
let decoded: NetworkId = bcs::from_bytes(&encoded).unwrap();
assert_eq!(new, decoded);
// New version can be decoded as old version
let encoded = serde_yaml::to_string(&new).unwrap();
let decoded: OldNetworkId = serde_yaml::from_str(&encoded).unwrap();
assert_eq!(old, decoded);
let encoded = bcs::to_bytes(&new).unwrap();
let decoded: OldNetworkId = bcs::from_bytes(&encoded).unwrap();
assert_eq!(old, decoded);
}
}
}