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
use sc_network::config::{NonDefaultSetConfig, SetConfig};
use std::{
borrow::Cow,
ops::{Index, IndexMut},
};
use strum::{EnumIter, IntoEnumIterator};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
pub enum PeerSet {
Validation,
Collation,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum IsAuthority {
Yes,
No,
}
impl PeerSet {
pub fn get_info(self, is_authority: IsAuthority) -> NonDefaultSetConfig {
let protocol = self.into_protocol_name();
let max_notification_size = 100 * 1024;
match self {
PeerSet::Validation => NonDefaultSetConfig {
notifications_protocol: protocol,
fallback_names: Vec::new(),
max_notification_size,
set_config: sc_network::config::SetConfig {
in_peers: super::MIN_GOSSIP_PEERS as u32 / 2 - 1,
out_peers: super::MIN_GOSSIP_PEERS as u32 / 2 - 1,
reserved_nodes: Vec::new(),
non_reserved_mode: sc_network::config::NonReservedPeerMode::Accept,
},
},
PeerSet::Collation => NonDefaultSetConfig {
notifications_protocol: protocol,
fallback_names: Vec::new(),
max_notification_size,
set_config: SetConfig {
in_peers: if is_authority == IsAuthority::Yes { 25 } else { 0 },
out_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: if is_authority == IsAuthority::Yes {
sc_network::config::NonReservedPeerMode::Accept
} else {
sc_network::config::NonReservedPeerMode::Deny
},
},
},
}
}
pub const fn get_protocol_name_static(self) -> &'static str {
match self {
PeerSet::Validation => "/polkadot/validation/1",
PeerSet::Collation => "/polkadot/collation/1",
}
}
pub fn into_protocol_name(self) -> Cow<'static, str> {
self.get_protocol_name_static().into()
}
pub fn try_from_protocol_name(name: &Cow<'static, str>) -> Option<PeerSet> {
match name {
n if n == &PeerSet::Validation.into_protocol_name() => Some(PeerSet::Validation),
n if n == &PeerSet::Collation.into_protocol_name() => Some(PeerSet::Collation),
_ => None,
}
}
}
#[derive(Debug, Default)]
pub struct PerPeerSet<T> {
validation: T,
collation: T,
}
impl<T> Index<PeerSet> for PerPeerSet<T> {
type Output = T;
fn index(&self, index: PeerSet) -> &T {
match index {
PeerSet::Validation => &self.validation,
PeerSet::Collation => &self.collation,
}
}
}
impl<T> IndexMut<PeerSet> for PerPeerSet<T> {
fn index_mut(&mut self, index: PeerSet) -> &mut T {
match index {
PeerSet::Validation => &mut self.validation,
PeerSet::Collation => &mut self.collation,
}
}
}
pub fn peer_sets_info(is_authority: IsAuthority) -> Vec<sc_network::config::NonDefaultSetConfig> {
PeerSet::iter().map(|s| s.get_info(is_authority)).collect()
}