casper_types/block/
era_end.rs

1mod era_end_v1;
2mod era_end_v2;
3
4use alloc::{collections::BTreeMap, vec::Vec};
5use core::fmt::{self, Display, Formatter};
6
7#[cfg(feature = "datasize")]
8use datasize::DataSize;
9use serde::{Deserialize, Serialize};
10
11use crate::{
12    bytesrepr::{self, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
13    PublicKey, Rewards, U512,
14};
15pub use era_end_v1::{EraEndV1, EraReport};
16pub use era_end_v2::EraEndV2;
17
18const TAG_LENGTH: usize = U8_SERIALIZED_LENGTH;
19
20/// Tag for block body v1.
21pub const ERA_END_V1_TAG: u8 = 0;
22/// Tag for block body v2.
23pub const ERA_END_V2_TAG: u8 = 1;
24
25/// The versioned era end of a block, storing the data for a switch block.
26/// It encapsulates different variants of the EraEnd struct.
27#[cfg_attr(feature = "datasize", derive(DataSize))]
28#[cfg_attr(any(feature = "testing", test), derive(PartialEq))]
29#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
30pub enum EraEnd {
31    /// The legacy, initial version of the body portion of a block.
32    V1(EraEndV1),
33    /// The version 2 of the body portion of a block, which includes the
34    /// `past_finality_signatures`.
35    V2(EraEndV2),
36}
37
38impl EraEnd {
39    /// Returns the equivocators.
40    pub fn equivocators(&self) -> &[PublicKey] {
41        match self {
42            EraEnd::V1(v1) => v1.equivocators(),
43            EraEnd::V2(v2) => v2.equivocators(),
44        }
45    }
46
47    /// Returns the inactive validators.
48    pub fn inactive_validators(&self) -> &[PublicKey] {
49        match self {
50            EraEnd::V1(v1) => v1.inactive_validators(),
51            EraEnd::V2(v2) => v2.inactive_validators(),
52        }
53    }
54
55    /// Returns the weights of validators in the upcoming era.
56    pub fn next_era_validator_weights(&self) -> &BTreeMap<PublicKey, U512> {
57        match self {
58            EraEnd::V1(v1) => v1.next_era_validator_weights(),
59            EraEnd::V2(v2) => v2.next_era_validator_weights(),
60        }
61    }
62
63    /// Returns the rewards.
64    pub fn rewards(&self) -> Rewards {
65        match self {
66            EraEnd::V1(v1) => Rewards::V1(v1.rewards()),
67            EraEnd::V2(v2) => Rewards::V2(v2.rewards()),
68        }
69    }
70}
71
72impl Display for EraEnd {
73    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
74        match self {
75            EraEnd::V1(v1) => Display::fmt(&v1, formatter),
76            EraEnd::V2(v2) => Display::fmt(&v2, formatter),
77        }
78    }
79}
80
81impl From<EraEndV1> for EraEnd {
82    fn from(era_end: EraEndV1) -> Self {
83        EraEnd::V1(era_end)
84    }
85}
86
87impl From<EraEndV2> for EraEnd {
88    fn from(era_end: EraEndV2) -> Self {
89        EraEnd::V2(era_end)
90    }
91}
92
93impl ToBytes for EraEnd {
94    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
95        let mut buffer = bytesrepr::allocate_buffer(self)?;
96        match self {
97            EraEnd::V1(v1) => {
98                buffer.insert(0, ERA_END_V1_TAG);
99                buffer.extend(v1.to_bytes()?);
100            }
101            EraEnd::V2(v2) => {
102                buffer.insert(0, ERA_END_V2_TAG);
103                buffer.extend(v2.to_bytes()?);
104            }
105        }
106        Ok(buffer)
107    }
108
109    fn serialized_length(&self) -> usize {
110        TAG_LENGTH
111            + match self {
112                EraEnd::V1(v1) => v1.serialized_length(),
113                EraEnd::V2(v2) => v2.serialized_length(),
114            }
115    }
116}
117
118impl FromBytes for EraEnd {
119    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
120        let (tag, remainder) = u8::from_bytes(bytes)?;
121        match tag {
122            ERA_END_V1_TAG => {
123                let (body, remainder): (EraEndV1, _) = FromBytes::from_bytes(remainder)?;
124                Ok((Self::V1(body), remainder))
125            }
126            ERA_END_V2_TAG => {
127                let (body, remainder): (EraEndV2, _) = FromBytes::from_bytes(remainder)?;
128                Ok((Self::V2(body), remainder))
129            }
130            _ => Err(bytesrepr::Error::Formatting),
131        }
132    }
133}