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
use std::fmt::{self, Debug, Display};
use crate::serializers;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub trait TrustThreshold: Copy + Clone + Debug + Serialize + DeserializeOwned {
fn is_enough_power(&self, signed_voting_power: u64, total_voting_power: u64) -> bool;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TrustThresholdFraction {
#[serde(with = "serializers::from_str")]
pub numerator: u64,
#[serde(with = "serializers::from_str")]
pub denominator: u64,
}
impl TrustThresholdFraction {
pub const TWO_THIRDS: Self = Self {
numerator: 2,
denominator: 3,
};
pub fn new(numerator: u64, denominator: u64) -> Option<Self> {
if numerator <= denominator && denominator > 0 && 3 * numerator >= denominator {
Some(Self {
numerator,
denominator,
})
} else {
None
}
}
}
impl TrustThreshold for TrustThresholdFraction {
fn is_enough_power(&self, signed_voting_power: u64, total_voting_power: u64) -> bool {
signed_voting_power * self.denominator > total_voting_power * self.numerator
}
}
impl Default for TrustThresholdFraction {
fn default() -> Self {
Self::new(1, 3)
.expect("initializing TrustThresholdFraction with valid fraction mustn't panic")
}
}
impl Display for TrustThresholdFraction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.numerator, self.denominator)
}
}