casper_client/types/
era_end.rs

1use std::{
2    collections::BTreeMap,
3    fmt::{self, Display, Formatter},
4};
5
6use serde::{Deserialize, Serialize};
7
8use casper_types::{
9    bytesrepr::{self, ToBytes},
10    crypto::PublicKey,
11    U512,
12};
13
14/// A reward to be given to the specified validator.
15#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
16#[serde(deny_unknown_fields)]
17pub struct Reward {
18    validator: PublicKey,
19    amount: u64,
20}
21
22impl Reward {
23    /// Returns the validator's public key.
24    pub fn validator(&self) -> &PublicKey {
25        &self.validator
26    }
27
28    /// Returns the number of rewarded motes.
29    pub fn amount(&self) -> u64 {
30        self.amount
31    }
32}
33
34/// Equivocation and reward information included in switch blocks.
35#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
36#[serde(deny_unknown_fields)]
37pub struct EraReport {
38    pub(super) equivocators: Vec<PublicKey>,
39    pub(super) rewards: Vec<Reward>,
40    pub(super) inactive_validators: Vec<PublicKey>,
41}
42
43impl EraReport {
44    /// Returns the collection of validators which have equivocated in this era.
45    pub fn equivocators(&self) -> impl Iterator<Item = &PublicKey> {
46        self.equivocators.iter()
47    }
48
49    /// Returns the collection of rewards due.
50    pub fn rewards(&self) -> impl Iterator<Item = &Reward> {
51        self.rewards.iter()
52    }
53
54    /// Returns the collection of validators which were marked inactive in this era.
55    pub fn inactive_validators(&self) -> impl Iterator<Item = &PublicKey> {
56        self.inactive_validators.iter()
57    }
58}
59
60impl ToBytes for EraReport {
61    fn write_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
62        let rewards: BTreeMap<PublicKey, u64> = self
63            .rewards
64            .iter()
65            .map(|reward| (reward.validator.clone(), reward.amount))
66            .collect();
67
68        self.equivocators.write_bytes(buffer)?;
69        rewards.write_bytes(buffer)?;
70        self.inactive_validators.write_bytes(buffer)
71    }
72
73    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
74        let mut buffer = vec![];
75        self.write_bytes(&mut buffer)?;
76        Ok(buffer)
77    }
78
79    fn serialized_length(&self) -> usize {
80        let rewards: BTreeMap<PublicKey, u64> = self
81            .rewards
82            .iter()
83            .map(|reward| (reward.validator.clone(), reward.amount))
84            .collect();
85        self.equivocators.serialized_length()
86            + rewards.serialized_length()
87            + self.inactive_validators.serialized_length()
88    }
89}
90
91/// The amount of weight a given validator has.
92#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
93#[serde(deny_unknown_fields)]
94pub struct ValidatorWeight {
95    validator: PublicKey,
96    weight: U512,
97}
98
99impl ValidatorWeight {
100    /// Returns the validator's public key.
101    pub fn validator(&self) -> &PublicKey {
102        &self.validator
103    }
104
105    /// Returns the validator's weight.
106    pub fn weight(&self) -> U512 {
107        self.weight
108    }
109}
110
111/// Information included in switch blocks about the era the block concludes and the subsequent era.
112#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
113#[serde(deny_unknown_fields)]
114pub struct EraEnd {
115    /// The report on the era just concluded.
116    pub(super) era_report: EraReport,
117    /// The validator weights for the subsequent era.
118    pub(super) next_era_validator_weights: Vec<ValidatorWeight>,
119}
120
121impl EraEnd {
122    /// Returns the report on the era just concluded.
123    pub fn era_report(&self) -> &EraReport {
124        &self.era_report
125    }
126
127    /// Returns the validator weights for the subsequent era.
128    pub fn next_era_validator_weights(&self) -> impl Iterator<Item = &ValidatorWeight> {
129        self.next_era_validator_weights.iter()
130    }
131}
132
133impl Display for EraEnd {
134    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
135        write!(formatter, "{:?}", self.era_report)
136    }
137}
138
139impl ToBytes for EraEnd {
140    fn write_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
141        let next_era_validator_weights: BTreeMap<PublicKey, U512> = self
142            .next_era_validator_weights
143            .iter()
144            .map(|validator_weight| (validator_weight.validator.clone(), validator_weight.weight))
145            .collect();
146
147        self.era_report.write_bytes(buffer)?;
148        next_era_validator_weights.write_bytes(buffer)
149    }
150
151    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
152        let mut buffer = vec![];
153        self.write_bytes(&mut buffer)?;
154        Ok(buffer)
155    }
156
157    fn serialized_length(&self) -> usize {
158        let next_era_validator_weights: BTreeMap<PublicKey, U512> = self
159            .next_era_validator_weights
160            .iter()
161            .map(|validator_weight| (validator_weight.validator.clone(), validator_weight.weight))
162            .collect();
163        self.era_report.serialized_length() + next_era_validator_weights.serialized_length()
164    }
165}