mithril_common/entities/
cardano_stake_distribution.rs

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::signable_builder::Artifact;

use super::{Epoch, StakeDistribution};

/// Cardano Stake Distribution
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct CardanoStakeDistribution {
    /// Unique hash of the Cardano Stake Distribution
    pub hash: String,

    /// Epoch at which the Cardano Stake Distribution is computed
    pub epoch: Epoch,

    /// StakeDistribution represents the list of participants in the Cardano chain with their associated stake
    pub stake_distribution: StakeDistribution,
}

impl CardanoStakeDistribution {
    /// Constructor
    pub fn new(epoch: Epoch, stake_distribution: StakeDistribution) -> CardanoStakeDistribution {
        let mut cardano_stake_distribution = CardanoStakeDistribution {
            hash: "".to_string(),
            epoch,
            stake_distribution,
        };
        cardano_stake_distribution.hash = cardano_stake_distribution.compute_hash();

        cardano_stake_distribution
    }

    /// Cardano stake distribution hash computation
    fn compute_hash(&self) -> String {
        let mut hasher = Sha256::new();
        hasher.update(self.epoch.to_be_bytes());
        self.stake_distribution.iter().for_each(|(k, v)| {
            hasher.update(k.as_bytes());
            hasher.update(v.to_be_bytes());
        });

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

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compute_hash() {
        assert_eq!(
            "7144dda132aa4fe758df8da5f85a774cef602ce85c8554baeeea006866710154",
            CardanoStakeDistribution::new(
                Epoch(1),
                StakeDistribution::from([("pool-1".to_string(), 100), ("pool-2".to_string(), 200)])
            )
            .compute_hash()
        );
    }

    #[test]
    fn compute_hash_returns_same_hash_with_same_cardano_stake_distribution() {
        let epoch = Epoch(1);

        assert_eq!(
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100), ("pool-2".to_string(), 200)])
            )
            .compute_hash(),
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100), ("pool-2".to_string(), 200)])
            )
            .compute_hash()
        );
    }

    #[test]
    fn compute_hash_returns_same_hash_whatever_the_stake_distribution_order() {
        let epoch = Epoch(1);

        assert_eq!(
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100), ("pool-2".to_string(), 200)])
            )
            .compute_hash(),
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-2".to_string(), 200), ("pool-1".to_string(), 100)])
            )
            .compute_hash()
        );
    }

    #[test]
    fn compute_hash_returns_different_hash_with_different_epoch() {
        assert_ne!(
            CardanoStakeDistribution::new(
                Epoch(1),
                StakeDistribution::from([("pool-1".to_string(), 100)])
            )
            .compute_hash(),
            CardanoStakeDistribution::new(
                Epoch(2),
                StakeDistribution::from([("pool-1".to_string(), 100)])
            )
            .compute_hash()
        );
    }

    #[test]
    fn compute_hash_returns_different_hash_with_different_stake_distribution_pool_id() {
        let epoch = Epoch(1);

        assert_ne!(
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100)])
            )
            .compute_hash(),
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-2".to_string(), 100)])
            )
            .compute_hash()
        );
    }

    #[test]
    fn compute_hash_returns_different_hash_with_different_stake_distribution_stakes() {
        let epoch = Epoch(1);

        assert_ne!(
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100)])
            )
            .compute_hash(),
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 150)])
            )
            .compute_hash()
        );
    }

    #[test]
    fn compute_hash_returns_different_hash_with_different_stake_distribution_size() {
        let epoch = Epoch(1);

        assert_ne!(
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100)])
            )
            .compute_hash(),
            CardanoStakeDistribution::new(
                epoch,
                StakeDistribution::from([("pool-1".to_string(), 100), ("pool-2".to_string(), 150)])
            )
            .compute_hash()
        );
    }
}