casper_types/chainspec/
next_upgrade.rs

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
use std::fmt::{self, Display, Formatter};

#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    bytesrepr::{self, FromBytes, ToBytes},
    ActivationPoint, ProtocolConfig, ProtocolVersion,
};

#[cfg(any(feature = "testing", test))]
use rand::Rng;

#[cfg(any(feature = "testing", test))]
use crate::testing::TestRng;

/// Information about the next protocol upgrade.
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Copy)]
pub struct NextUpgrade {
    activation_point: ActivationPoint,
    protocol_version: ProtocolVersion,
}

impl NextUpgrade {
    /// Creates a new `NextUpgrade`.
    pub fn new(activation_point: ActivationPoint, protocol_version: ProtocolVersion) -> Self {
        NextUpgrade {
            activation_point,
            protocol_version,
        }
    }

    /// Returns the activation point of the next upgrade.
    pub fn activation_point(&self) -> ActivationPoint {
        self.activation_point
    }

    /// Random.
    #[cfg(any(feature = "testing", test))]
    pub fn random(rng: &mut TestRng) -> Self {
        Self {
            activation_point: ActivationPoint::random(rng),
            protocol_version: ProtocolVersion::from_parts(rng.gen(), rng.gen(), rng.gen()),
        }
    }
}

impl From<ProtocolConfig> for NextUpgrade {
    fn from(protocol_config: ProtocolConfig) -> Self {
        NextUpgrade {
            activation_point: protocol_config.activation_point,
            protocol_version: protocol_config.version,
        }
    }
}

impl Display for NextUpgrade {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "next upgrade to {} at start of era {}",
            self.protocol_version,
            self.activation_point.era_id()
        )
    }
}

impl ToBytes for NextUpgrade {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut buffer = bytesrepr::allocate_buffer(self)?;
        self.write_bytes(&mut buffer)?;
        Ok(buffer)
    }

    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
        self.activation_point.write_bytes(writer)?;
        self.protocol_version.write_bytes(writer)
    }

    fn serialized_length(&self) -> usize {
        self.activation_point.serialized_length() + self.protocol_version.serialized_length()
    }
}

impl FromBytes for NextUpgrade {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (activation_point, remainder) = ActivationPoint::from_bytes(bytes)?;
        let (protocol_version, remainder) = ProtocolVersion::from_bytes(remainder)?;
        Ok((
            NextUpgrade {
                activation_point,
                protocol_version,
            },
            remainder,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::TestRng;

    #[test]
    fn bytesrepr_roundtrip() {
        let rng = &mut TestRng::new();

        let val = NextUpgrade::random(rng);
        bytesrepr::test_serialization_roundtrip(&val);
    }
}