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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use serde::{Deserialize, Serialize};
use std::{
    cmp::Ordering,
    fmt::{self, Display, Formatter},
    ops::{Add, AddAssign},
    str::FromStr,
};

use penumbra_proto::{penumbra::core::component::governance::v1 as pb, DomainType};

use crate::{
    params::GovernanceParameters,
    proposal_state::{Outcome as StateOutcome, Withdrawn},
    vote::Vote,
};

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(try_from = "pb::Tally", into = "pb::Tally")]
pub struct Tally {
    yes: u64,
    no: u64,
    abstain: u64,
}

impl Tally {
    pub fn yes(&self) -> u64 {
        self.yes
    }

    pub fn no(&self) -> u64 {
        self.no
    }

    pub fn abstain(&self) -> u64 {
        self.abstain
    }

    pub fn total(&self) -> u64 {
        self.yes + self.no + self.abstain
    }
}

impl From<Tally> for pb::Tally {
    fn from(tally: Tally) -> Self {
        Self {
            yes: tally.yes,
            no: tally.no,
            abstain: tally.abstain,
        }
    }
}

impl From<pb::Tally> for Tally {
    fn from(tally: pb::Tally) -> Self {
        Self {
            yes: tally.yes,
            no: tally.no,
            abstain: tally.abstain,
        }
    }
}

impl DomainType for Tally {
    type Proto = pb::Tally;
}

impl From<(Vote, u64)> for Tally {
    fn from((vote, power): (Vote, u64)) -> Self {
        let mut tally = Self::default();
        *match vote {
            Vote::Yes => &mut tally.yes,
            Vote::No => &mut tally.no,
            Vote::Abstain => &mut tally.abstain,
        } = power;
        tally
    }
}

impl From<(u64, Vote)> for Tally {
    fn from((power, vote): (u64, Vote)) -> Self {
        Self::from((vote, power))
    }
}

impl Add for Tally {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            yes: self.yes + rhs.yes,
            no: self.no + rhs.no,
            abstain: self.abstain + rhs.abstain,
        }
    }
}

impl AddAssign for Tally {
    fn add_assign(&mut self, rhs: Self) {
        self.yes += rhs.yes;
        self.no += rhs.no;
        self.abstain += rhs.abstain;
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Outcome {
    Pass,
    Fail,
    Slash,
}

impl Outcome {
    pub fn is_pass(&self) -> bool {
        matches!(self, Self::Pass)
    }

    pub fn is_fail(&self) -> bool {
        matches!(self, Self::Fail)
    }

    pub fn is_slash(&self) -> bool {
        matches!(self, Self::Slash)
    }
}

impl<T> From<Outcome> for StateOutcome<T> {
    fn from(outcome: Outcome) -> Self {
        match outcome {
            Outcome::Pass => Self::Passed,
            Outcome::Fail => Self::Failed {
                withdrawn: Withdrawn::No,
            },
            Outcome::Slash => Self::Slashed {
                withdrawn: Withdrawn::No,
            },
        }
    }
}

impl Tally {
    fn meets_quorum(&self, total_voting_power: u64, params: &GovernanceParameters) -> bool {
        Ratio::new(self.total(), total_voting_power) >= params.proposal_valid_quorum
    }

    fn slashed(&self, params: &GovernanceParameters) -> bool {
        Ratio::new(self.no, self.total()) > params.proposal_slash_threshold
    }

    fn yes_ratio(&self) -> Ratio {
        Ratio::new(self.yes, (self.yes + self.no).min(1))
        // ^ in the above, the `.min(1)` is to prevent a divide-by-zero error when the only votes
        // cast are abstains -- this results in a 0:1 ratio in that case, which will never pass, as
        // desired in that situation
    }

    pub fn outcome(self, total_voting_power: u64, params: &GovernanceParameters) -> Outcome {
        use Outcome::*;

        // Check to see if we've met quorum
        if !self.meets_quorum(total_voting_power, params) {
            return Fail;
        }

        // Check to see if it has been slashed
        if self.slashed(params) {
            return Slash;
        }

        // Now that we've checked for slash and quorum, we can just check to see if it should pass
        if self.yes_ratio() > params.proposal_pass_threshold {
            Pass
        } else {
            Fail
        }
    }

    pub fn emergency_pass(self, total_voting_power: u64, params: &GovernanceParameters) -> bool {
        // Check to see if we've met quorum
        if !self.meets_quorum(total_voting_power, params) {
            return false;
        }

        // Check to see if it has been slashed (this check should be redundant, but we'll do it anyway)
        if self.slashed(params) {
            return false;
        }

        // Now that we've checked for slash and quorum, we can just check to see if it should pass in
        // the emergency condition of 2/3 majority of voting power
        Ratio::new(self.yes, total_voting_power) > Ratio::new(2, 3)
    }
}

/// This is a ratio of two `u64` values, intended to be used solely in governance parameters and
/// tallying. It only implements construction and comparison, not arithmetic, to reduce the trusted
/// codebase for governance.
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pb::Ratio", into = "pb::Ratio")]
pub struct Ratio {
    numerator: u64,
    denominator: u64,
}

impl Display for Ratio {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.numerator, self.denominator)
    }
}

impl FromStr for Ratio {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s.split('/');
        let numerator = parts
            .next()
            .ok_or_else(|| anyhow::anyhow!("missing numerator"))?
            .parse()?;
        let denominator = parts
            .next()
            .ok_or_else(|| anyhow::anyhow!("missing denominator"))?
            .parse()?;
        if parts.next().is_some() {
            anyhow::bail!("too many parts");
        }
        Ok(Ratio {
            numerator,
            denominator,
        })
    }
}

impl Ratio {
    pub fn new(numerator: u64, denominator: u64) -> Self {
        Self {
            numerator,
            denominator,
        }
    }
}

impl PartialEq for Ratio {
    fn eq(&self, other: &Self) -> bool {
        // Convert everything to `u128` to avoid overflow when multiplying
        u128::from(self.numerator) * u128::from(other.denominator)
            == u128::from(self.denominator) * u128::from(other.numerator)
    }
}

impl Eq for Ratio {}

impl PartialOrd for Ratio {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Ratio {
    fn cmp(&self, other: &Self) -> Ordering {
        // Convert everything to `u128` to avoid overflow when multiplying
        (u128::from(self.numerator) * u128::from(other.denominator))
            .cmp(&(u128::from(self.denominator) * u128::from(other.numerator)))
    }
}

impl From<Ratio> for pb::Ratio {
    fn from(ratio: Ratio) -> Self {
        pb::Ratio {
            numerator: ratio.numerator,
            denominator: ratio.denominator,
        }
    }
}

impl From<pb::Ratio> for Ratio {
    fn from(msg: pb::Ratio) -> Self {
        Ratio {
            numerator: msg.numerator,
            denominator: msg.denominator,
        }
    }
}