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
use derive_more::Display;
use serde::{Deserialize, Serialize};
use tendermint::{
account::Id as TMAccountId,
block::{
header::Header as TMHeader, signed_header::SignedHeader as TMSignedHeader,
Commit as TMCommit,
},
trust_threshold::TrustThresholdFraction,
validator::Info as TMValidatorInfo,
validator::Set as TMValidatorSet,
};
pub use tendermint::{block::Height, hash::Hash, time::Time};
pub type PeerId = tendermint::node::Id;
pub type TrustThreshold = TrustThresholdFraction;
pub type Header = TMHeader;
pub type ValidatorSet = TMValidatorSet;
pub type Validator = TMValidatorInfo;
pub type ValidatorAddress = TMAccountId;
pub type Commit = TMCommit;
pub type SignedHeader = TMSignedHeader;
pub type TrustedState = LightBlock;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Status {
Failed,
Unverified,
Verified,
Trusted,
}
impl Status {
pub fn iter() -> &'static [Self] {
use Status::*;
static ALL: &[Status] = &[Unverified, Verified, Trusted, Failed];
ALL
}
pub fn most_trusted(a: Self, b: Self) -> Self {
std::cmp::max(a, b)
}
}
#[derive(Clone, Debug, Display, PartialEq, Serialize, Deserialize)]
#[display(fmt = "{:?}", self)]
pub struct LightBlock {
pub signed_header: SignedHeader,
#[serde(rename = "validator_set")]
pub validators: ValidatorSet,
#[serde(rename = "next_validator_set")]
pub next_validators: ValidatorSet,
pub provider: PeerId,
}
impl LightBlock {
pub fn new(
signed_header: SignedHeader,
validators: ValidatorSet,
next_validators: ValidatorSet,
provider: PeerId,
) -> LightBlock {
Self {
signed_header,
validators,
next_validators,
provider,
}
}
pub fn height(&self) -> Height {
self.signed_header.header.height
}
}
#[derive(Clone, Debug, Display, PartialEq, Serialize, Deserialize)]
#[display(fmt = "{:?}", self)]
pub struct LatestStatus {
pub height: Option<u64>,
pub block_hash: Option<Hash>,
pub valset_hash: Option<Hash>,
pub connected_nodes: Vec<PeerId>,
}
impl LatestStatus {
pub fn new(
height: Option<u64>,
block_hash: Option<Hash>,
valset_hash: Option<Hash>,
connected_nodes: Vec<PeerId>,
) -> Self {
Self {
height,
block_hash,
valset_hash,
connected_nodes,
}
}
}
#[cfg(test)]
mod tests {
mod status {
use crate::types::Status;
use Status::*;
#[test]
fn ord_impl() {
assert!(Trusted > Verified);
assert!(Verified > Unverified);
assert!(Unverified > Failed);
}
#[test]
fn most_trusted() {
for (a, b) in cross(Status::iter()) {
if a > b {
assert_eq!(Status::most_trusted(a, b), a);
} else {
assert_eq!(Status::most_trusted(a, b), b);
}
}
}
fn cross<T>(xs: &[T]) -> Vec<(T, T)>
where
T: Copy,
{
xs.iter()
.copied()
.flat_map(|y| xs.iter().copied().map(move |x| (x, y)))
.collect()
}
}
}