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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use alloc::{collections::BTreeMap, vec::Vec};

use crate::{
    auction::{Bid, DelegationRate, Delegator},
    bytesrepr::{self, FromBytes, ToBytes},
    CLType, CLTyped, PublicKey, U512,
};

/// The seigniorage recipient details.
#[derive(Default, PartialEq, Clone, Debug)]
pub struct SeigniorageRecipient {
    /// Validator stake (not including delegators)
    stake: U512,
    /// Delegation rate of a seigniorage recipient.
    delegation_rate: DelegationRate,
    /// List of delegators and their accumulated bids.
    delegators: BTreeMap<PublicKey, Delegator>,
}

impl SeigniorageRecipient {
    /// Returns stake of the provided recipient
    pub fn stake(&self) -> &U512 {
        &self.stake
    }

    /// Returns delegation rate of the provided recipient
    pub fn delegation_rate(&self) -> &DelegationRate {
        &self.delegation_rate
    }

    /// Returns delegators of the provided recipient
    pub fn delegators(&self) -> &BTreeMap<PublicKey, Delegator> {
        &self.delegators
    }

    /// Calculates total stake, including delegators' total stake
    pub fn total_stake(&self) -> U512 {
        self.stake + self.delegator_total_stake()
    }

    /// Caculates total stake for all delegators
    pub fn delegator_total_stake(&self) -> U512 {
        self.delegators
            .values()
            .map(Delegator::staked_amount)
            .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(bid: &Bid) -> Self {
        Self {
            stake: *bid.staked_amount(),
            delegation_rate: *bid.delegation_rate(),
            delegators: bid.delegators().clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::collections::BTreeMap;
    use core::iter::FromIterator;

    use crate::{
        auction::{DelegationRate, Delegator, SeigniorageRecipient},
        bytesrepr, AccessRights, SecretKey, URef, U512,
    };

    #[test]
    fn serialization_roundtrip() {
        let uref = URef::new([0; 32], AccessRights::READ_ADD_WRITE);
        let validator_key = SecretKey::ed25519([1; SecretKey::ED25519_LENGTH]).into();
        let delegator_1_key = SecretKey::ed25519([42; SecretKey::ED25519_LENGTH]).into();
        let delegator_2_key = SecretKey::ed25519([43; SecretKey::ED25519_LENGTH]).into();
        let delegator_3_key = SecretKey::ed25519([44; SecretKey::ED25519_LENGTH]).into();

        let seigniorage_recipient = SeigniorageRecipient {
            stake: U512::max_value(),
            delegation_rate: DelegationRate::max_value(),
            delegators: BTreeMap::from_iter(vec![
                (
                    validator_key,
                    Delegator::new(U512::max_value(), uref, delegator_1_key),
                ),
                (
                    validator_key,
                    Delegator::new(U512::max_value(), uref, delegator_2_key),
                ),
                (
                    validator_key,
                    Delegator::new(U512::zero(), uref, delegator_3_key),
                ),
            ]),
        };
        bytesrepr::test_serialization_roundtrip(&seigniorage_recipient);
    }
}