Skip to content

Commit

Permalink
unexport verify_single and use verify_and_update_single in tests …
Browse files Browse the repository at this point in the history
…instead

- use MemStore of light crate in tests as a Mock
  • Loading branch information
liamsi committed Jan 28, 2020
1 parent e58f12c commit 19331b9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
2 changes: 2 additions & 0 deletions tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ zeroize = { version = "1.1", features = ["zeroize_derive"] }
[dev-dependencies]
serde_json = "1"
tokio = "0.2"
# Note: used in tests only:
tendermint-lite = {path = "../tendermint-lite"}
4 changes: 0 additions & 4 deletions tendermint/src/lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ pub mod verifier;

pub use self::types::*;

// TODO: don't expose this once the json tests
// switch to using one of the other functions
pub use self::verifier::verify_single;

pub use self::verifier::{verify_and_update_bisection, verify_and_update_single};
1 change: 1 addition & 0 deletions tendermint/src/lite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod tests {
assert_eq!(threshold.is_enough_power(1, 100), false);
}
}

/// Requester can be used to request [`SignedHeader`]s and [`ValidatorSet`]s for a
/// given height, e.g., by talking to a tendermint fullnode through RPC.
pub trait Requester<C, H>
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/lite/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ where
// 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.
pub fn verify_single<H, C, L>(
fn verify_single<H, C, L>(
trusted_state: &TrustedState<C, H>,
untrusted_sh: &SignedHeader<C, H>,
untrusted_vals: &C::ValidatorSet,
Expand Down
33 changes: 15 additions & 18 deletions tendermint/tests/lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use serde::{de::Error as _, Deserialize, Deserializer, Serialize};
use serde_json;
use std::{fs, path::PathBuf};
use tendermint::block::Header;
use tendermint::lite::TrustThresholdFraction;
use tendermint::lite::{Store, TrustThresholdFraction};
use tendermint::{block::signed_header::SignedHeader, lite, validator::Set, Time};
use tendermint_lite::store;

#[derive(Clone, Debug)]
struct Duration(u64);
Expand Down Expand Up @@ -65,42 +66,38 @@ type Trusted = lite::TrustedState<SignedHeader, Header>;
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 =
let trusted_state =
Trusted::new(&tc.initial.signed_header.clone().into(), &trusted_next_vals);
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;
*/
let tm_now = tc.initial.now;
let now = tm_now.to_system_time().unwrap();
// Note: Alternative to using mem-store from the light node crate would be mocking
// the store here directly.
let mut store = store::MemStore::new();
store
.add(trusted_state)
.expect("adding trusted state to store failed");

for (_, input) in tc.input.iter().enumerate() {
println!("{}", tc.description);
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.
match lite::verify_single(
&trusted_state,
match lite::verify_and_update_single(
&untrusted_signed_header.into(),
&untrusted_vals,
&untrusted_next_vals,
&TrustThresholdFraction::default(),
&trusting_period,
&now,
&mut store,
) {
Ok(_) => {
let last: lite::SignedHeader<SignedHeader, Header> =
untrusted_signed_header.into();
trusted_state = Trusted::new(&last, &untrusted_next_vals);
assert!(!expects_err);
}
Err(_) => {
Expand Down

0 comments on commit 19331b9

Please sign in to comment.