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
use alloc::vec::Vec;
use crate::{
auction::{Bid, DelegatedAmounts, DelegationRate},
bytesrepr::{self, FromBytes, ToBytes},
CLType, CLTyped, U512,
};
#[cfg_attr(test, derive(Debug))]
#[derive(Default, PartialEq, Clone)]
pub struct SeigniorageRecipient {
pub stake: U512,
pub delegation_rate: DelegationRate,
pub delegators: DelegatedAmounts,
}
impl SeigniorageRecipient {
pub fn total_stake(&self) -> U512 {
self.stake + self.delegator_total_stake()
}
pub fn delegator_total_stake(&self) -> U512 {
self.delegators.values().cloned().sum()
}
}
impl CLTyped for SeigniorageRecipient {
fn cl_type() -> CLType {
CLType::Any
}
}
impl ToBytes for SeigniorageRecipient {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
let mut result = bytesrepr::allocate_buffer(self)?;
result.extend(self.stake.to_bytes()?);
result.extend(self.delegation_rate.to_bytes()?);
result.extend(self.delegators.to_bytes()?);
Ok(result)
}
fn serialized_length(&self) -> usize {
self.stake.serialized_length()
+ self.delegation_rate.serialized_length()
+ self.delegators.serialized_length()
}
}
impl FromBytes for SeigniorageRecipient {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (stake, bytes) = FromBytes::from_bytes(bytes)?;
let (delegation_rate, bytes) = FromBytes::from_bytes(bytes)?;
let (delegators, bytes) = FromBytes::from_bytes(bytes)?;
Ok((
SeigniorageRecipient {
stake,
delegation_rate,
delegators,
},
bytes,
))
}
}
impl From<&Bid> for SeigniorageRecipient {
fn from(founding_validator: &Bid) -> Self {
Self {
stake: founding_validator.staked_amount,
delegation_rate: founding_validator.delegation_rate,
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use alloc::collections::BTreeMap;
use core::iter::FromIterator;
use super::SeigniorageRecipient;
use crate::{auction::DelegationRate, bytesrepr, PublicKey, U512};
#[test]
fn serialization_roundtrip() {
let seigniorage_recipient = SeigniorageRecipient {
stake: U512::max_value(),
delegation_rate: DelegationRate::max_value(),
delegators: BTreeMap::from_iter(vec![
(PublicKey::Ed25519([42; 32]), U512::one()),
(PublicKey::Ed25519([43; 32]), U512::max_value()),
(PublicKey::Ed25519([44; 32]), U512::zero()),
]),
};
bytesrepr::test_serialization_roundtrip(&seigniorage_recipient);
}
}