polkadot_collator_protocol/validator_side_experimental/
common.rs1use std::num::NonZeroU16;
18
19use polkadot_node_network_protocol::peer_set::CollationVersion;
20use polkadot_primitives::Id as ParaId;
21
22pub const MAX_SCORE: u16 = 5000;
24
25pub const CONNECTED_PEERS_LIMIT: NonZeroU16 = NonZeroU16::new(300).expect("300 is greater than 0");
27
28pub const CONNECTED_PEERS_PARA_LIMIT: NonZeroU16 = const {
31 assert!(CONNECTED_PEERS_LIMIT.get() >= 100);
32 NonZeroU16::new(100).expect("100 is greater than 0")
33};
34
35pub const MAX_STARTUP_ANCESTRY_LOOKBACK: u32 = 20;
38
39pub const VALID_INCLUDED_CANDIDATE_BUMP: u16 = 50;
41
42pub const INACTIVITY_DECAY: u16 = 1;
45
46pub const MAX_STORED_SCORES_PER_PARA: u8 = 150;
49#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Default)]
51pub struct Score(u16);
52
53impl Score {
54 pub const fn new(val: u16) -> Option<Self> {
56 if val > MAX_SCORE {
57 None
58 } else {
59 Some(Self(val))
60 }
61 }
62
63 pub fn saturating_add(&mut self, val: u16) {
65 if (self.0 + val) <= MAX_SCORE {
66 self.0 += val;
67 } else {
68 self.0 = MAX_SCORE;
69 }
70 }
71
72 pub fn saturating_sub(&mut self, val: u16) {
74 self.0 = self.0.saturating_sub(val);
75 }
76}
77
78impl From<Score> for u16 {
79 fn from(value: Score) -> Self {
80 value.0
81 }
82}
83
84#[derive(PartialEq, Debug, Clone)]
86pub struct PeerInfo {
87 pub version: CollationVersion,
89 pub state: PeerState,
91}
92
93#[derive(PartialEq, Debug, Clone)]
95pub enum PeerState {
96 Connected,
98 Collating(ParaId),
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
108 fn score_functions() {
109 assert!(MAX_SCORE > 50);
110
111 for score in (0..MAX_SCORE).step_by(10) {
113 assert_eq!(u16::from(Score::new(score).unwrap()), score);
114 }
115 assert_eq!(u16::from(Score::new(MAX_SCORE).unwrap()), MAX_SCORE);
116 for score in ((MAX_SCORE + 1)..(MAX_SCORE + 50)).step_by(5) {
117 assert_eq!(Score::new(score), None);
118 }
119
120 let score = Score::new(50).unwrap();
122
123 for other_score in (0..(MAX_SCORE - 50)).step_by(10) {
125 let expected_value = u16::from(score) + other_score;
126
127 let mut score = score;
128 score.saturating_add(other_score);
129
130 assert_eq!(expected_value, u16::from(score));
131 }
132
133 for other_score in ((MAX_SCORE - 50)..MAX_SCORE).step_by(10) {
135 let mut score = score;
136 score.saturating_add(other_score);
137
138 assert_eq!(MAX_SCORE, u16::from(score));
139 }
140
141 for other_score in (0..50).step_by(10) {
143 let expected_value = u16::from(score) - other_score;
144
145 let mut score = score;
146 score.saturating_sub(other_score);
147
148 assert_eq!(expected_value, u16::from(score));
149 }
150
151 for other_score in (50..100).step_by(10) {
153 let mut score = score;
154 score.saturating_sub(other_score);
155
156 assert_eq!(0, u16::from(score));
157 }
158 }
159}