casper_types/chainspec/
highway_config.rs1#[cfg(feature = "datasize")]
2use datasize::DataSize;
3
4#[cfg(any(feature = "testing", test))]
5use rand::Rng;
6use serde::{Deserialize, Serialize};
7
8#[cfg(any(feature = "testing", test))]
9use crate::testing::TestRng;
10use crate::{
11 bytesrepr::{self, FromBytes, ToBytes},
12 TimeDiff,
13};
14
15#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, Default)]
17#[cfg_attr(feature = "datasize", derive(DataSize))]
18#[serde(deny_unknown_fields)]
20pub struct HighwayConfig {
21 pub maximum_round_length: TimeDiff,
23}
24
25impl HighwayConfig {
26 pub fn is_valid(&self) -> Result<(), String> {
28 Ok(())
29 }
30
31 #[cfg(any(feature = "testing", test))]
33 pub fn random(rng: &mut TestRng) -> Self {
34 let maximum_round_length = TimeDiff::from_seconds(rng.gen_range(60..600));
35
36 HighwayConfig {
37 maximum_round_length,
38 }
39 }
40}
41
42impl ToBytes for HighwayConfig {
43 fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
44 let mut buffer = bytesrepr::allocate_buffer(self)?;
45 buffer.extend(self.maximum_round_length.to_bytes()?);
46 Ok(buffer)
47 }
48
49 fn serialized_length(&self) -> usize {
50 self.maximum_round_length.serialized_length()
51 }
52}
53
54impl FromBytes for HighwayConfig {
55 fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
56 let (maximum_round_length, remainder) = TimeDiff::from_bytes(bytes)?;
57 let config = HighwayConfig {
58 maximum_round_length,
59 };
60 Ok((config, remainder))
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use rand::SeedableRng;
67
68 use super::*;
69
70 #[test]
71 fn bytesrepr_roundtrip() {
72 let mut rng = TestRng::from_entropy();
73 let config = HighwayConfig::random(&mut rng);
74 bytesrepr::test_serialization_roundtrip(&config);
75 }
76}