forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheight.rs
122 lines (103 loc) Β· 2.81 KB
/
height.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
use super::*;
#[derive(Copy, Clone, Debug, Display, FromStr, Ord, Eq, Serialize, PartialEq, PartialOrd)]
pub(crate) struct Height(pub(crate) u64);
impl Height {
pub(crate) fn n(self) -> u64 {
self.0
}
pub(crate) fn subsidy(self) -> u64 {
Epoch::from(self).subsidy()
}
pub(crate) fn starting_sat(self) -> Sat {
let epoch = Epoch::from(self);
let epoch_starting_sat = epoch.starting_sat();
let epoch_starting_height = epoch.starting_height();
epoch_starting_sat + (self - epoch_starting_height.n()).n() * epoch.subsidy()
}
pub(crate) fn period_offset(self) -> u64 {
self.0 % DIFFCHANGE_INTERVAL
}
}
impl Add<u64> for Height {
type Output = Self;
fn add(self, other: u64) -> Height {
Self(self.0 + other)
}
}
impl Sub<u64> for Height {
type Output = Self;
fn sub(self, other: u64) -> Height {
Self(self.0 - other)
}
}
impl PartialEq<u64> for Height {
fn eq(&self, other: &u64) -> bool {
self.0 == *other
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn n() {
assert_eq!(Height(0).n(), 0);
assert_eq!(Height(1).n(), 1);
}
#[test]
fn add() {
assert_eq!(Height(0) + 1, 1);
assert_eq!(Height(1) + 100, 101);
}
#[test]
fn sub() {
assert_eq!(Height(1) - 1, 0);
assert_eq!(Height(100) - 50, 50);
}
#[test]
fn eq() {
assert_eq!(Height(0), 0);
assert_eq!(Height(100), 100);
}
#[test]
fn from_str() {
assert_eq!("0".parse::<Height>().unwrap(), 0);
assert!("foo".parse::<Height>().is_err());
}
#[test]
fn subsidy() {
assert_eq!(Height(0).subsidy(), 5000000000);
assert_eq!(Height(1).subsidy(), 5000000000);
assert_eq!(Height(SUBSIDY_HALVING_INTERVAL - 1).subsidy(), 5000000000);
assert_eq!(Height(SUBSIDY_HALVING_INTERVAL).subsidy(), 2500000000);
assert_eq!(Height(SUBSIDY_HALVING_INTERVAL + 1).subsidy(), 2500000000);
}
#[test]
fn starting_sat() {
assert_eq!(Height(0).starting_sat(), 0);
assert_eq!(Height(1).starting_sat(), 5000000000);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL - 1).starting_sat(),
(SUBSIDY_HALVING_INTERVAL - 1) * 5000000000
);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL).starting_sat(),
SUBSIDY_HALVING_INTERVAL * 5000000000
);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL + 1).starting_sat(),
SUBSIDY_HALVING_INTERVAL * 5000000000 + 2500000000
);
assert_eq!(
Height(u64::max_value()).starting_sat(),
*Epoch::STARTING_SATS.last().unwrap()
);
}
#[test]
fn period_offset() {
assert_eq!(Height(0).period_offset(), 0);
assert_eq!(Height(1).period_offset(), 1);
assert_eq!(Height(DIFFCHANGE_INTERVAL - 1).period_offset(), 2015);
assert_eq!(Height(DIFFCHANGE_INTERVAL).period_offset(), 0);
assert_eq!(Height(DIFFCHANGE_INTERVAL + 1).period_offset(), 1);
}
}