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 346 347
// Copyright 2017 The Exonum Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Common widely used typedefs. use serde::{Serialize, Serializer, Deserialize, Deserializer}; use std::fmt; /// Number of milliseconds. pub type Milliseconds = u64; /// Blockchain's height (number of blocks). #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Height(pub u64); impl Height { /// Returns zero value of the height. /// /// # Examples /// /// ``` /// use exonum::helpers::Height; /// /// let height = Height::zero(); /// assert_eq!(0, height.0); /// ``` pub fn zero() -> Self { Height(0) } /// Returns next value of the height. /// /// # Examples /// /// ``` /// use exonum::helpers::Height; /// /// let height = Height(10); /// let next_height = height.next(); /// assert_eq!(11, next_height.0); /// ``` pub fn next(&self) -> Self { Height(self.0 + 1) } /// Returns previous value of the height. /// /// # Panics /// /// Panics if `self.0` is equal to zero. /// /// # Examples /// /// ``` /// use exonum::helpers::Height; /// /// let height = Height(10); /// let previous_height = height.previous(); /// assert_eq!(9, previous_height.0); /// ``` pub fn previous(&self) -> Self { assert_ne!(0, self.0); Height(self.0 - 1) } /// Increments the height value. /// /// # Examples /// /// ``` /// use exonum::helpers::Height; /// /// let mut height = Height::zero(); /// height.increment(); /// assert_eq!(1, height.0); /// ``` pub fn increment(&mut self) { self.0 += 1; } /// Decrements the height value. /// /// # Panics /// /// Panics if `self.0` is equal to zero. /// /// # Examples /// /// ``` /// use exonum::helpers::Height; /// /// let mut height = Height(20); /// height.decrement(); /// assert_eq!(19, height.0); /// ``` pub fn decrement(&mut self) { assert_ne!(0, self.0); self.0 -= 1; } } /// Consensus round index. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct Round(pub u32); impl Round { /// Returns zero value of the round. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let round = Round::zero(); /// assert_eq!(0, round.0); /// ``` pub fn zero() -> Self { Round(0) } /// Returns first value of the round. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let round = Round::first(); /// assert_eq!(1, round.0); /// ``` pub fn first() -> Self { Round(1) } /// Returns next value of the round. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let round = Round(20); /// let next_round = round.next(); /// assert_eq!(21, next_round.0); /// ``` pub fn next(&self) -> Self { Round(self.0 + 1) } /// Returns previous value of the round. /// /// # Panics /// /// Panics if `self.0` is equal to zero. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let round = Round(10); /// let previous_round = round.previous(); /// assert_eq!(9, previous_round.0); /// ``` pub fn previous(&self) -> Self { assert_ne!(0, self.0); Round(self.0 - 1) } /// Increments the round value. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let mut round = Round::zero(); /// round.increment(); /// assert_eq!(1, round.0); /// ``` pub fn increment(&mut self) { self.0 += 1; } /// Decrements the round value. /// /// # Panics /// /// Panics if `self.0` is equal to zero. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let mut round = Round(20); /// round.decrement(); /// assert_eq!(19, round.0); /// ``` pub fn decrement(&mut self) { assert_ne!(0, self.0); self.0 -= 1; } /// Returns the iterator over rounds in the range from `self` to `to - 1`. /// /// # Examples /// /// ``` /// use exonum::helpers::Round; /// /// let round = Round::zero(); /// let mut iter = round.iter_to(Round(2)); /// assert_eq!(Some(Round(0)), iter.next()); /// assert_eq!(Some(Round(1)), iter.next()); /// assert_eq!(None, iter.next()); /// ``` pub fn iter_to(&self, to: Round) -> RoundRangeIter { RoundRangeIter { next: *self, last: to, } } } /// Validators identifier. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct ValidatorId(pub u16); impl ValidatorId { /// Returns zero value of the validator id. /// /// # Examples /// /// ``` /// use exonum::helpers::ValidatorId; /// /// let id = ValidatorId::zero(); /// assert_eq!(0, id.0); /// ``` pub fn zero() -> Self { ValidatorId(0) } } impl fmt::Display for Height { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } } impl From<Height> for u64 { fn from(val: Height) -> Self { val.0 } } impl fmt::Display for Round { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } } impl From<Round> for u32 { fn from(val: Round) -> Self { val.0 } } impl From<Round> for u64 { fn from(val: Round) -> Self { u64::from(val.0) } } impl fmt::Display for ValidatorId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } } impl From<ValidatorId> for u16 { fn from(val: ValidatorId) -> Self { val.0 } } impl From<ValidatorId> for usize { fn from(val: ValidatorId) -> Self { val.0 as usize } } // Serialization/deserialization is implemented manually because TOML round-trip for the tuple // structs is broken currently. See https://github.com/alexcrichton/toml-rs/issues/194 for details. impl Serialize for Height { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { self.0.serialize(serializer) } } impl<'de> Deserialize<'de> for Height { fn deserialize<D>(deserializer: D) -> Result<Height, D::Error> where D: Deserializer<'de>, { Ok(Height(u64::deserialize(deserializer)?)) } } /// Iterator over rounds range. #[derive(Debug)] pub struct RoundRangeIter { next: Round, last: Round, } // TODO: Add (or replace by) `Step` implementation (ECR-165). impl Iterator for RoundRangeIter { type Item = Round; fn next(&mut self) -> Option<Self::Item> { if self.next < self.last { let res = Some(self.next); self.next.increment(); res } else { None } } }