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
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::{
    entities::{Epoch, SignerWithStake},
    signable_builder::Artifact,
};

use super::ProtocolParameters;

/// Mithril Stake Distribution
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct MithrilStakeDistribution {
    /// Epoch at which the Mithril Stake Distribution is created
    pub epoch: Epoch,

    /// List of signers with stakes of the Mithril Stake Distribution
    pub signers_with_stake: Vec<SignerWithStake>,

    /// Hash of the Mithril Stake Distribution (different from the AVK).
    pub hash: String,

    /// Protocol parameters used to sign this stake distribution
    pub protocol_parameters: ProtocolParameters,
}

impl MithrilStakeDistribution {
    /// MithrilStakeDistribution artifact factory
    pub fn new(
        epoch: Epoch,
        signers_with_stake: Vec<SignerWithStake>,
        protocol_parameters: &ProtocolParameters,
    ) -> Self {
        let mut signers_with_stake_sorted = signers_with_stake;
        signers_with_stake_sorted.sort();
        let mut mithril_stake_distribution: MithrilStakeDistribution = Self {
            epoch,
            signers_with_stake: signers_with_stake_sorted,
            hash: "".to_string(),
            protocol_parameters: protocol_parameters.to_owned(),
        };
        mithril_stake_distribution.hash = mithril_stake_distribution.compute_hash();
        mithril_stake_distribution
    }

    /// Do not add other parameters to the compute hash.
    /// Mithril Stake Distribution is defined by the epoch and signers
    fn compute_hash(&self) -> String {
        let mut hasher = Sha256::new();
        hasher.update(self.epoch.to_be_bytes());

        for signer_with_stake in &self.signers_with_stake {
            hasher.update(signer_with_stake.compute_hash().as_bytes());
        }

        hex::encode(hasher.finalize())
    }
}

#[typetag::serde]
impl Artifact for MithrilStakeDistribution {
    fn get_id(&self) -> String {
        self.hash.clone()
    }
}

#[cfg(test)]
mod tests {
    use crate::test_utils::{fake_data, MithrilFixtureBuilder};

    use super::*;

    const EXPECTED_HASH: &str = "47675a6fad57040b194655b103bc0439ff9d6920a7ba86bd53756db7ce2952f5";

    #[test]
    fn test_compute_hash() {
        let fixtures = MithrilFixtureBuilder::default().with_signers(100).build();
        let stake_distribution = MithrilStakeDistribution::new(
            Epoch(1),
            fixtures.signers_with_stake(),
            &fake_data::protocol_parameters(),
        );

        assert_eq!(EXPECTED_HASH.to_owned(), stake_distribution.compute_hash());
    }

    #[test]
    fn test_hash_fail_for_different_stake() {
        let fixtures = MithrilFixtureBuilder::default().with_signers(100).build();
        let mut signers = fixtures.signers_with_stake();
        signers[0].stake += 1;
        let stake_distribution =
            MithrilStakeDistribution::new(Epoch(1), signers, &fake_data::protocol_parameters());

        assert_ne!(EXPECTED_HASH.to_owned(), stake_distribution.compute_hash());
    }

    #[test]
    fn test_hash_fail_for_different_epoch() {
        let fixtures = MithrilFixtureBuilder::default().with_signers(100).build();
        let stake_distribution = MithrilStakeDistribution::new(
            Epoch(2),
            fixtures.signers_with_stake(),
            &fake_data::protocol_parameters(),
        );

        assert_ne!(EXPECTED_HASH.to_owned(), stake_distribution.compute_hash());
    }

    #[test]
    fn test_independence_protocol_parameters() {
        let signers = MithrilFixtureBuilder::default()
            .with_signers(100)
            .build()
            .signers_with_stake();
        let protocol_parameters = ProtocolParameters {
            k: 100,
            m: 0,
            phi_f: 0.0,
        };
        let stake_distribution =
            MithrilStakeDistribution::new(Epoch(1), signers, &protocol_parameters);

        assert_eq!(EXPECTED_HASH.to_owned(), stake_distribution.compute_hash());
    }
}