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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use gumdrop::Options;
use serde::{Deserialize, Serialize};
use simple_error::*;
use crate::helpers::parse_as;
use crate::validator::generate_validators;
use crate::{Commit, Generator, Header, Validator};
use tendermint::node::Id as PeerId;
use tendermint::validator;
use tendermint::validator::Set as ValidatorSet;
use tendermint::{block::signed_header::SignedHeader, Hash};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TmLightBlock {
pub signed_header: SignedHeader,
pub validators: ValidatorSet,
pub next_validators: ValidatorSet,
pub provider: PeerId,
}
#[derive(Debug, Options, Serialize, Deserialize, Clone)]
pub struct LightBlock {
#[options(help = "header (required)", parse(try_from_str = "parse_as::<Header>"))]
pub header: Option<Header>,
#[options(help = "commit (required)", parse(try_from_str = "parse_as::<Commit>"))]
pub commit: Option<Commit>,
#[options(
help = "validators (required), encoded as array of 'validator' parameters",
parse(try_from_str = "parse_as::<Vec<Validator>>")
)]
pub validators: Option<Vec<Validator>>,
#[options(
help = "next validators (default: same as validators), encoded as array of 'validator' parameters",
parse(try_from_str = "parse_as::<Vec<Validator>>")
)]
pub next_validators: Option<Vec<Validator>>,
#[options(help = "peer id (default: default_peer_id())")]
pub provider: Option<PeerId>,
}
impl LightBlock {
pub fn new(header: Header, commit: Commit) -> Self {
Self {
header: Some(header),
commit: Some(commit),
validators: None,
next_validators: None,
provider: None,
}
}
set_option!(validators, &[Validator], Some(validators.to_vec()));
set_option!(
next_validators,
&[Validator],
Some(next_validators.to_vec())
);
set_option!(provider, &str, Some(provider.parse().unwrap()));
pub fn new_default(height: u64) -> Self {
let validators = [
Validator::new("1").voting_power(50),
Validator::new("2").voting_power(50),
];
let header = Header::new(&validators)
.height(height)
.chain_id("test-chain")
.next_validators(&validators)
.time(height);
let commit = Commit::new(header.clone(), 1);
Self {
header: Some(header),
commit: Some(commit),
validators: Some(validators.to_vec()),
next_validators: Some(validators.to_vec()),
provider: Some(default_peer_id()),
}
}
pub fn new_default_with_time_and_chain_id(chain_id: String, time: u64, height: u64) -> Self {
let validators = [
Validator::new("1").voting_power(50),
Validator::new("2").voting_power(50),
];
let header = Header::new(&validators)
.height(height)
.chain_id(&chain_id)
.next_validators(&validators)
.time(time);
let commit = Commit::new(header.clone(), 1);
Self {
header: Some(header),
commit: Some(commit),
validators: Some(validators.to_vec()),
next_validators: Some(validators.to_vec()),
provider: Some(default_peer_id()),
}
}
pub fn next(&self) -> Self {
let header = self.header.as_ref().expect("header is missing").next();
let commit = Commit::new(header.clone(), 1);
Self {
header: Some(header),
commit: Some(commit),
validators: self.next_validators.clone(),
next_validators: self.next_validators.clone(),
provider: self.provider,
}
}
pub fn height(&self) -> u64 {
self.header
.as_ref()
.expect("header is missing")
.height
.expect("header height is missing")
}
pub fn chain_id(&self) -> String {
self.header
.as_ref()
.expect("header is missing")
.chain_id
.as_ref()
.expect("chain_id is missing")
.to_string()
}
pub fn last_block_id_hash(&self) -> Option<Hash> {
self.header
.as_ref()
.expect("header is missing")
.last_block_id_hash
}
}
impl std::str::FromStr for LightBlock {
type Err = SimpleError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let light_block = match parse_as::<LightBlock>(s) {
Ok(input) => input,
Err(_) => LightBlock::new(parse_as::<Header>(s)?, Commit::from_str(s)?),
};
Ok(light_block)
}
}
impl Generator<TmLightBlock> for LightBlock {
fn merge_with_default(self, default: Self) -> Self {
Self {
header: self.header.or(default.header),
commit: self.commit.or(default.commit),
validators: self.validators.or(default.validators),
next_validators: self.next_validators.or(default.next_validators),
provider: self.provider.or(default.provider),
}
}
fn generate(&self) -> Result<TmLightBlock, SimpleError> {
let header = match &self.header {
None => bail!("header is missing"),
Some(h) => h,
};
let commit = match &self.commit {
None => bail!("commit is missing"),
Some(c) => c,
};
let signed_header =
generate_signed_header(header, commit).expect("Could not generate signed header");
let validators = match &self.validators {
None => validator::Set::without_proposer(generate_validators(
header
.validators
.as_ref()
.expect("missing validators in header"),
)?),
Some(vals) => validator::Set::without_proposer(generate_validators(vals)?),
};
let next_validators = match &self.next_validators {
Some(next_vals) => validator::Set::without_proposer(generate_validators(next_vals)?),
None => validators.clone(),
};
let provider = match self.provider {
Some(peer) => peer,
None => default_peer_id(),
};
let light_block = TmLightBlock {
signed_header,
validators,
next_validators,
provider,
};
Ok(light_block)
}
}
pub fn generate_signed_header(
raw_header: &Header,
raw_commit: &Commit,
) -> Result<SignedHeader, SimpleError> {
let header = match raw_header.generate() {
Err(e) => bail!("Failed to generate header with error: {}", e),
Ok(h) => h,
};
let commit = match raw_commit.generate() {
Err(e) => bail!("Failed to generate commit with error: {}", e),
Ok(c) => c,
};
Ok(SignedHeader::new(header, commit).unwrap())
}
pub fn default_peer_id() -> PeerId {
"BADFADAD0BEFEEDC0C0ADEADBEEFC0FFEEFACADE".parse().unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_light_block() {
let light_block_1 = LightBlock::new_default(1);
let light_block_2 = LightBlock::new_default(1);
assert_eq!(light_block_1.generate(), light_block_2.generate());
let validators = [Validator::new("10"), Validator::new("20")];
let light_block_3 = LightBlock::new_default(1).validators(&validators);
assert_ne!(light_block_2.generate(), light_block_3.generate());
let light_block_4 = LightBlock::new_default(4).validators(&validators);
assert_ne!(light_block_3.generate(), light_block_4.generate());
let light_block_5 = light_block_3.next();
let lb_5_height: u64 = light_block_5
.generate()
.unwrap()
.signed_header
.header
.height
.into();
assert_eq!(2, lb_5_height);
let header_6 = Header::new(&validators)
.next_validators(&validators)
.height(10)
.time(10)
.chain_id("test-chain");
let commit_6 = Commit::new(header_6.clone(), 1);
let light_block_6 = LightBlock::new(header_6.clone(), commit_6);
let header_7 = header_6.next();
let commit_7 = Commit::new(header_7.clone(), 1);
let light_block_7 = LightBlock::new(header_7, commit_7);
assert_eq!(light_block_7.height(), 11);
assert_eq!(light_block_7.chain_id(), "test-chain");
assert_ne!(light_block_6.generate(), light_block_7.generate());
}
}