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
use crate::{light_block::LightBlock, Generator};
use tendermint::block::{self, Height};
use tendermint::chain::Info;
use std::convert::{TryFrom, TryInto};
pub struct LightChain {
pub info: Info,
pub light_blocks: Vec<LightBlock>,
}
impl Default for LightChain {
fn default() -> Self {
let initial_block = LightBlock::new_default(1);
let id = initial_block.chain_id().parse().unwrap();
let height = initial_block.height().try_into().unwrap();
let info = Info {
id,
height,
last_block_id: None,
time: None,
};
Self::new(info, vec![initial_block])
}
}
impl LightChain {
pub fn new(info: Info, light_blocks: Vec<LightBlock>) -> Self {
LightChain { info, light_blocks }
}
pub fn default_with_length(num: u64) -> Self {
let mut light_chain = Self::default();
for _i in 2..=num {
light_chain.advance_chain();
}
light_chain
}
pub fn advance_chain(&mut self) -> &LightBlock {
let last_light_block = self
.light_blocks
.last()
.expect("Cannot find testgen light block");
let new_light_block = last_light_block.next();
self.info.height = Height::try_from(new_light_block.height())
.expect("failed to convert from u64 to Height");
let last_block_id_hash = new_light_block
.header
.as_ref()
.expect("missing header in new light block")
.generate()
.expect("failed to generate header")
.hash();
self.info.last_block_id = Some(block::Id {
hash: last_block_id_hash,
part_set_header: Default::default(),
});
self.light_blocks.push(new_light_block);
self.light_blocks.last().unwrap()
}
pub fn block(&self, target_height: u64) -> Option<&LightBlock> {
self.light_blocks
.iter()
.find(|lb| lb.height() == target_height)
}
pub fn latest_block(&self) -> &LightBlock {
self.light_blocks
.last()
.expect("cannot find last light block")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_advance_chain() {
let mut light_chain = LightChain::default_with_length(1);
let advance_1 = light_chain.advance_chain();
assert_eq!(2, advance_1.height());
assert_eq!(2, light_chain.info.height.value());
let advance_2 = light_chain.advance_chain();
assert_eq!(3, advance_2.height());
assert_eq!(3, light_chain.info.height.value());
}
#[test]
fn test_block() {
let mut light_chain = LightChain::default_with_length(1);
let first_block = light_chain.block(1);
assert_eq!(1, first_block.unwrap().height());
light_chain.advance_chain();
let second_block = light_chain.block(2);
assert_eq!(2, second_block.unwrap().height());
}
#[test]
fn test_latest_block() {
let mut light_chain = LightChain::default_with_length(1);
let first_block = light_chain.latest_block();
assert_eq!(1, first_block.height());
light_chain.advance_chain();
let second_block = light_chain.latest_block();
assert_eq!(2, second_block.height());
}
#[test]
fn test_light_chain_with_length() {
const CHAIN_HEIGHT: u64 = 10;
let chain = LightChain::default_with_length(CHAIN_HEIGHT);
let blocks = chain
.light_blocks
.into_iter()
.flat_map(|lb| lb.generate())
.collect::<Vec<_>>();
assert_eq!(blocks.len(), chain.info.height.value() as usize);
assert_eq!(blocks.len(), CHAIN_HEIGHT as usize);
let first_block = blocks.first().unwrap();
let last_block = blocks.last().unwrap();
assert_eq!(first_block.signed_header.header.height.value(), 1);
assert!(first_block.signed_header.header.last_block_id.is_none());
assert_eq!(last_block.signed_header.header.height, chain.info.height);
for i in 1..blocks.len() {
let prv = &blocks[i - 1];
let cur = &blocks[i];
assert_eq!(
cur.signed_header.header.height.value(),
prv.signed_header.header.height.value() + 1
);
assert_eq!(
cur.signed_header.header.last_block_id.map(|lbi| lbi.hash),
Some(prv.signed_header.header.hash())
);
}
}
}