Skip to content

Commit

Permalink
lite json tests use verify_single
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Dec 31, 2019
1 parent 97c9e06 commit 525c89f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
8 changes: 3 additions & 5 deletions tendermint/src/lite/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::{Duration, SystemTime};
// and hence it's possible to use it incorrectly.
// If trusted_state is not expired and this returns Ok, the
// untrusted_sh and untrusted_next_vals can be considered trusted.
fn verify_single<TS, SH, C, L>(
pub fn verify_single<TS, SH, C, L>(
trusted_state: &TS,
untrusted_sh: &SH,
untrusted_vals: &C::ValidatorSet,
Expand Down Expand Up @@ -41,14 +41,12 @@ where
let trusted_height = trusted_header.height();
let untrusted_height = untrusted_sh.header().height();
match untrusted_height.cmp(&trusted_height.increment()) {
Ordering::Less => {
return Err(Error::NonIncreasingHeight)
}
Ordering::Less => return Err(Error::NonIncreasingHeight),
Ordering::Equal => {
let trusted_vals_hash = trusted_header.next_validators_hash();
let untrusted_vals_hash = untrusted_header.validators_hash();
if trusted_vals_hash != untrusted_vals_hash {
// TODO: more specific error
// TODO: more specific error
// ie. differentiate from when next_vals.hash() doesnt
// match the header hash ...
return Err(Error::InvalidNextValidatorSet);
Expand Down
44 changes: 25 additions & 19 deletions tendermint/tests/lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,44 @@ fn run_test_cases(cases: TestCases) {
for (_, tc) in cases.test_cases.iter().enumerate() {
let trusted_next_vals = tc.initial.clone().next_validator_set;
let mut trusted_state = Trusted::new(&tc.initial.signed_header.clone(), &trusted_next_vals);
let trusting_period: std::time::Duration = tc.initial.clone().trusting_period.into();
let now = tc.initial.now;
let expects_err = match &tc.expected_output {
Some(eo) => eo.eq("error"),
None => false,
};

// TODO - we're currently using lite::verify_single which
// shouldn't even be exposed and doesn't check time.
// but the public functions take a store, which do check time,
// also take a store, so we need to mock one ...
/*
let trusting_period: std::time::Duration = tc.initial.clone().trusting_period.into();
let now = tc.initial.now;
*/

for (_, input) in tc.input.iter().enumerate() {
println!("{}", tc.description);
let new_signed_header = &input.signed_header;
let new_vals = &input.validator_set;
let untrusted_signed_header = &input.signed_header;
let untrusted_vals = &input.validator_set;
let untrusted_next_vals = &input.next_validator_set;
// Note that in the provided test files the other header is either assumed to
// be "trusted" (verification already happened), or, it's the signed header verified in
// the previous iteration of this loop. In both cases it is assumed that h1 was already
// verified.
let h2_verif_res = lite::verify(new_signed_header, new_vals);
let mut check_support_res: Result<(), lite::Error> = Ok(());
if h2_verif_res.is_ok() {
check_support_res = lite::check_support(
&trusted_state,
&new_signed_header,
&DefaultTrustLevel {},
&trusting_period,
&now.into(),
);
assert_eq!(check_support_res.is_err(), expects_err);
if check_support_res.is_ok() {
trusted_state = Trusted::new(&new_signed_header, &input.next_validator_set);
match lite::verify_single(
&trusted_state,
&untrusted_signed_header,
&untrusted_vals,
&untrusted_next_vals,
&DefaultTrustLevel {},
) {
Ok(_) => {
trusted_state = Trusted::new(&untrusted_signed_header, &untrusted_next_vals);
assert!(!expects_err);
}
Err(_) => {
assert!(expects_err);
}
}
let got_err = check_support_res.is_err() || h2_verif_res.is_err();
assert_eq!(expects_err, got_err);
}
}
}
Expand Down

0 comments on commit 525c89f

Please sign in to comment.