casper_types/chainspec/
vacancy_config.rs

1#[cfg(any(feature = "testing", test))]
2use crate::testing::TestRng;
3use crate::{
4    bytesrepr,
5    bytesrepr::{Error, FromBytes, ToBytes},
6};
7#[cfg(feature = "datasize")]
8use datasize::DataSize;
9#[cfg(any(feature = "testing", test))]
10use rand::Rng;
11use serde::{Deserialize, Serialize};
12
13/// The configuration to determine gas price based on block vacancy.
14#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Debug)]
15#[cfg_attr(feature = "datasize", derive(DataSize))]
16#[serde(deny_unknown_fields)]
17pub struct VacancyConfig {
18    /// The upper threshold to determine an increment in gas price
19    pub upper_threshold: u64,
20    /// The lower threshold to determine a decrement in gas price
21    pub lower_threshold: u64,
22    /// The upper limit of the gas price.
23    pub max_gas_price: u8,
24    /// The lower limit of the gas price.
25    pub min_gas_price: u8,
26}
27
28impl Default for VacancyConfig {
29    fn default() -> Self {
30        Self {
31            upper_threshold: 90,
32            lower_threshold: 50,
33            max_gas_price: 3,
34            min_gas_price: 1,
35        }
36    }
37}
38
39impl VacancyConfig {
40    /// Returns a random [`VacancyConfig`]
41    #[cfg(any(feature = "testing", test))]
42    pub fn random(rng: &mut TestRng) -> Self {
43        Self {
44            upper_threshold: rng.gen_range(49..100),
45            lower_threshold: rng.gen_range(0..50),
46            max_gas_price: rng.gen_range(3..5),
47            min_gas_price: rng.gen_range(1..3),
48        }
49    }
50}
51
52impl ToBytes for VacancyConfig {
53    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), Error> {
54        self.upper_threshold.write_bytes(writer)?;
55        self.lower_threshold.write_bytes(writer)?;
56        self.max_gas_price.write_bytes(writer)?;
57        self.min_gas_price.write_bytes(writer)
58    }
59    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
60        let mut buffer = bytesrepr::allocate_buffer(self)?;
61        self.write_bytes(&mut buffer)?;
62        Ok(buffer)
63    }
64    fn serialized_length(&self) -> usize {
65        self.upper_threshold.serialized_length()
66            + self.lower_threshold.serialized_length()
67            + self.max_gas_price.serialized_length()
68            + self.min_gas_price.serialized_length()
69    }
70}
71
72impl FromBytes for VacancyConfig {
73    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
74        let (upper_threshold, remainder) = u64::from_bytes(bytes)?;
75        let (lower_threshold, remainder) = u64::from_bytes(remainder)?;
76        let (max_gas_price, remainder) = u8::from_bytes(remainder)?;
77        let (min_gas_price, remainder) = u8::from_bytes(remainder)?;
78        Ok((
79            Self {
80                upper_threshold,
81                lower_threshold,
82                max_gas_price,
83                min_gas_price,
84            },
85            remainder,
86        ))
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn bytesrepr_roundtrip() {
96        let mut rng = TestRng::new();
97        let config = VacancyConfig::random(&mut rng);
98        bytesrepr::test_serialization_roundtrip(&config);
99    }
100}