Skip to content

Commit fab3599

Browse files
author
Naohiro Yoshida
committed
add fork spect test
Signed-off-by: Naohiro Yoshida <[email protected]>
1 parent 6b3e2de commit fab3599

File tree

3 files changed

+257
-9
lines changed

3 files changed

+257
-9
lines changed

light-client/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::commitment::{
1919
};
2020
use crate::consensus_state::ConsensusState;
2121
use crate::errors::{ClientError, Error};
22-
use crate::fork_spec::{validate_sorted_asc, HeightOrTimestamp};
22+
use crate::fork_spec::{verify_sorted_asc, HeightOrTimestamp};
2323
use crate::header::constant::{MINIMUM_HEIGHT_SUPPORTED, MINIMUM_TIMESTAMP_SUPPORTED};
2424
use crate::header::Header;
2525
use crate::message::ClientMessage;
@@ -182,7 +182,7 @@ impl InnerLightClient {
182182
return Err(Error::EmptyForkSpec);
183183
}
184184

185-
validate_sorted_asc(&client_state.fork_specs)?;
185+
verify_sorted_asc(&client_state.fork_specs)?;
186186

187187
for spec in &client_state.fork_specs {
188188
match spec.height_or_timestamp {

light-client/src/fork_spec.rs

Lines changed: 255 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ pub fn find_target_fork_spec(
6464
.ok_or(Error::MissingForkSpec(current_height, current_timestamp))
6565
}
6666

67-
pub fn validate_sorted_asc(fork_specs: &[ForkSpec]) -> Result<(), Error> {
67+
pub fn verify_sorted_asc(fork_specs: &[ForkSpec]) -> Result<(), Error> {
6868
let mut last_height: Option<u64> = None;
6969
let mut last_timestamp: Option<u64> = None;
7070
for spec in fork_specs {
7171
match &spec.height_or_timestamp {
7272
HeightOrTimestamp::Height(height) => {
7373
if let Some(last_height) = &last_height {
7474
if height <= last_height {
75-
return Err(Error::UnexpectedForkSpecHeightOrder(*height, *last_height));
75+
return Err(Error::UnexpectedForkSpecHeightOrder(*last_height, *height));
7676
}
7777
}
7878
last_height = Some(*height);
@@ -81,8 +81,8 @@ pub fn validate_sorted_asc(fork_specs: &[ForkSpec]) -> Result<(), Error> {
8181
if let Some(last_timestamp) = &last_timestamp {
8282
if timestamp <= last_timestamp {
8383
return Err(Error::UnexpectedForkSpecTimestampOrder(
84-
*timestamp,
8584
*last_timestamp,
85+
*timestamp,
8686
));
8787
}
8888
}
@@ -92,3 +92,255 @@ pub fn validate_sorted_asc(fork_specs: &[ForkSpec]) -> Result<(), Error> {
9292
}
9393
Ok(())
9494
}
95+
96+
#[cfg(test)]
97+
mod test {
98+
use crate::errors::Error;
99+
use crate::fork_spec::ForkSpec;
100+
use crate::fork_spec::{find_target_fork_spec, verify_sorted_asc, HeightOrTimestamp};
101+
102+
#[test]
103+
fn test_success_find_target_spec_height_only() {
104+
let specs = &[
105+
ForkSpec {
106+
height_or_timestamp: HeightOrTimestamp::Height(10),
107+
additional_header_item_count: 1,
108+
},
109+
ForkSpec {
110+
height_or_timestamp: HeightOrTimestamp::Height(20),
111+
additional_header_item_count: 2,
112+
},
113+
];
114+
let v = find_target_fork_spec(specs, 10, 0).unwrap();
115+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Height(10));
116+
let v = find_target_fork_spec(specs, 11, 0).unwrap();
117+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Height(10));
118+
let v = find_target_fork_spec(specs, 19, 0).unwrap();
119+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Height(10));
120+
let v = find_target_fork_spec(specs, 20, 0).unwrap();
121+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Height(20));
122+
}
123+
124+
#[test]
125+
fn test_success_find_target_spec_timestamp_only() {
126+
let specs = &[
127+
ForkSpec {
128+
height_or_timestamp: HeightOrTimestamp::Time(10),
129+
additional_header_item_count: 1,
130+
},
131+
ForkSpec {
132+
height_or_timestamp: HeightOrTimestamp::Time(20),
133+
additional_header_item_count: 2,
134+
},
135+
];
136+
let v = find_target_fork_spec(specs, 0, 10).unwrap();
137+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
138+
let v = find_target_fork_spec(specs, 0, 11).unwrap();
139+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
140+
let v = find_target_fork_spec(specs, 0, 19).unwrap();
141+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
142+
let v = find_target_fork_spec(specs, 0, 20).unwrap();
143+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(20));
144+
}
145+
146+
#[test]
147+
fn test_success_find_target_spec_timestamp_and_height() {
148+
let specs = &[
149+
ForkSpec {
150+
height_or_timestamp: HeightOrTimestamp::Height(10),
151+
additional_header_item_count: 1,
152+
},
153+
ForkSpec {
154+
height_or_timestamp: HeightOrTimestamp::Time(10),
155+
additional_header_item_count: 20,
156+
},
157+
];
158+
// After value is primary
159+
let v = find_target_fork_spec(specs, 10, 10).unwrap();
160+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
161+
let v = find_target_fork_spec(specs, 11, 11).unwrap();
162+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
163+
let v = find_target_fork_spec(specs, 10, 19).unwrap();
164+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
165+
let v = find_target_fork_spec(specs, 20, 20).unwrap();
166+
assert_eq!(v.height_or_timestamp, HeightOrTimestamp::Time(10));
167+
}
168+
169+
#[test]
170+
fn test_error_find_target_spec_height_only() {
171+
let specs = &[
172+
ForkSpec {
173+
height_or_timestamp: HeightOrTimestamp::Height(10),
174+
additional_header_item_count: 1,
175+
},
176+
ForkSpec {
177+
height_or_timestamp: HeightOrTimestamp::Height(20),
178+
additional_header_item_count: 2,
179+
},
180+
];
181+
let v = find_target_fork_spec(specs, 9, 0).unwrap_err();
182+
match v {
183+
Error::MissingForkSpec(e1, e0) => {
184+
assert_eq!(e1, 9);
185+
assert_eq!(e0, 0);
186+
}
187+
_ => unreachable!("unexpected error"),
188+
}
189+
}
190+
191+
#[test]
192+
fn test_error_find_target_spec_timestamp_only() {
193+
let specs = &[
194+
ForkSpec {
195+
height_or_timestamp: HeightOrTimestamp::Time(10),
196+
additional_header_item_count: 1,
197+
},
198+
ForkSpec {
199+
height_or_timestamp: HeightOrTimestamp::Time(20),
200+
additional_header_item_count: 2,
201+
},
202+
];
203+
let v = find_target_fork_spec(specs, 0, 9).unwrap_err();
204+
match v {
205+
Error::MissingForkSpec(e1, e0) => {
206+
assert_eq!(e1, 0);
207+
assert_eq!(e0, 9);
208+
}
209+
_ => unreachable!("unexpected error"),
210+
}
211+
}
212+
213+
#[test]
214+
fn test_error_find_target_spec_timestamp_and_height() {
215+
let specs = &[
216+
ForkSpec {
217+
height_or_timestamp: HeightOrTimestamp::Height(10),
218+
additional_header_item_count: 1,
219+
},
220+
ForkSpec {
221+
height_or_timestamp: HeightOrTimestamp::Time(10),
222+
additional_header_item_count: 2,
223+
},
224+
];
225+
let v = find_target_fork_spec(specs, 9, 9).unwrap_err();
226+
match v {
227+
Error::MissingForkSpec(e1, e0) => {
228+
assert_eq!(e1, 9);
229+
assert_eq!(e0, 9);
230+
}
231+
_ => unreachable!("unexpected error"),
232+
}
233+
}
234+
235+
#[test]
236+
fn test_success_verify_sorted_asc_height() {
237+
let specs = &[
238+
ForkSpec {
239+
height_or_timestamp: HeightOrTimestamp::Height(10),
240+
additional_header_item_count: 1,
241+
},
242+
ForkSpec {
243+
height_or_timestamp: HeightOrTimestamp::Height(11),
244+
additional_header_item_count: 2,
245+
},
246+
];
247+
verify_sorted_asc(specs).unwrap();
248+
}
249+
250+
#[test]
251+
fn test_success_verify_sorted_asc_time() {
252+
let specs = &[
253+
ForkSpec {
254+
height_or_timestamp: HeightOrTimestamp::Time(10),
255+
additional_header_item_count: 1,
256+
},
257+
ForkSpec {
258+
height_or_timestamp: HeightOrTimestamp::Time(11),
259+
additional_header_item_count: 2,
260+
},
261+
];
262+
verify_sorted_asc(specs).unwrap();
263+
}
264+
265+
#[test]
266+
fn test_error_verify_sorted_asc_height() {
267+
let specs = &[
268+
ForkSpec {
269+
height_or_timestamp: HeightOrTimestamp::Height(10),
270+
additional_header_item_count: 1,
271+
},
272+
ForkSpec {
273+
height_or_timestamp: HeightOrTimestamp::Height(10),
274+
additional_header_item_count: 2,
275+
},
276+
];
277+
let v = verify_sorted_asc(specs).unwrap_err();
278+
match v {
279+
Error::UnexpectedForkSpecHeightOrder(e1, e0) => {
280+
assert_eq!(e1, 10);
281+
assert_eq!(e0, 10);
282+
}
283+
_ => unreachable!("unexpected error"),
284+
}
285+
286+
let specs = &[
287+
ForkSpec {
288+
height_or_timestamp: HeightOrTimestamp::Height(11),
289+
additional_header_item_count: 1,
290+
},
291+
ForkSpec {
292+
height_or_timestamp: HeightOrTimestamp::Height(10),
293+
additional_header_item_count: 2,
294+
},
295+
];
296+
let v = verify_sorted_asc(specs).unwrap_err();
297+
match v {
298+
Error::UnexpectedForkSpecHeightOrder(e1, e0) => {
299+
assert_eq!(e1, 11);
300+
assert_eq!(e0, 10);
301+
}
302+
_ => unreachable!("unexpected error"),
303+
}
304+
}
305+
306+
#[test]
307+
fn test_error_verify_sorted_asc_time() {
308+
let specs = &[
309+
ForkSpec {
310+
height_or_timestamp: HeightOrTimestamp::Time(10),
311+
additional_header_item_count: 1,
312+
},
313+
ForkSpec {
314+
height_or_timestamp: HeightOrTimestamp::Time(10),
315+
additional_header_item_count: 2,
316+
},
317+
];
318+
let v = verify_sorted_asc(specs).unwrap_err();
319+
match v {
320+
Error::UnexpectedForkSpecTimestampOrder(e1, e0) => {
321+
assert_eq!(e1, 10);
322+
assert_eq!(e0, 10);
323+
}
324+
_ => unreachable!("unexpected error"),
325+
}
326+
327+
let specs = &[
328+
ForkSpec {
329+
height_or_timestamp: HeightOrTimestamp::Time(11),
330+
additional_header_item_count: 1,
331+
},
332+
ForkSpec {
333+
height_or_timestamp: HeightOrTimestamp::Time(10),
334+
additional_header_item_count: 2,
335+
},
336+
];
337+
let v = verify_sorted_asc(specs).unwrap_err();
338+
match v {
339+
Error::UnexpectedForkSpecTimestampOrder(e1, e0) => {
340+
assert_eq!(e1, 11);
341+
assert_eq!(e0, 10);
342+
}
343+
_ => unreachable!("unexpected error"),
344+
}
345+
}
346+
}

light-client/src/misc.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ impl<'a> RlpIterator<'a> {
7373
Self { rlp, index: 0 }
7474
}
7575

76-
pub fn item_count(&self) -> Result<usize, Error> {
77-
self.rlp.item_count().map_err(Error::RLPDecodeError)
78-
}
79-
8076
pub fn try_next(&mut self) -> Result<Rlp<'a>, Error> {
8177
let index = self.index;
8278
let result = self.rlp.at(index).map_err(Error::RLPDecodeError)?;

0 commit comments

Comments
 (0)