Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify some light client types and testing #132

Merged
merged 33 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e05cc8f
Differentiate error from verify_commit_(trusting vs full)
liamsi Jan 25, 2020
5b2355f
Fix failing test :-)
liamsi Jan 25, 2020
2886635
Refactoring and simplifications: param struct for TrustedState, Signe…
liamsi Jan 26, 2020
7505e82
Remove obsolete TODO: we don't take a ref anymore
liamsi Jan 26, 2020
368461f
some checked arithmetic for height
liamsi Jan 26, 2020
07e289c
type alias Mock types
liamsi Jan 26, 2020
68e7bb1
Add TrustThresholdFraction and a default for it
liamsi Jan 26, 2020
a471444
remove obsolete clone
liamsi Jan 26, 2020
eecef32
remove unused lifetime (initially introduced to get rid of `clone`s
liamsi Jan 27, 2020
9a18314
improve doc
liamsi Jan 27, 2020
0da2f2c
remove TestSuite struct from JSON tests
liamsi Jan 27, 2020
45d6400
Add tests for TrustThresholdFraction
liamsi Jan 27, 2020
43af079
add test-case for `is_within_trust_period` to increase test-coverage
liamsi Jan 27, 2020
215d702
remove `votes_len` from commit; add a validate method to commit
liamsi Jan 27, 2020
847ee8f
unexport `verify_single` and use `verify_and_update_single` in tests …
liamsi Jan 28, 2020
2b8d5e9
type alias doesn't need to be `pub` here
liamsi Jan 28, 2020
cf24fc0
update doc comment and revert to making State public in tendermint-lite
liamsi Jan 28, 2020
c00205d
Remove len & is_empty() from ValidatSet trait
liamsi Jan 29, 2020
418cd04
add some very simple unit tests for basic types
liamsi Jan 29, 2020
28ecf94
update comments and move mock types into a mock module (only accessib…
liamsi Jan 30, 2020
4831a41
add missing #[cfg(test)] attribute (saves some compile time)
liamsi Jan 30, 2020
31550c3
remove store from inner recursion function
liamsi Jan 30, 2020
8b2b964
remove store from `verify_and_update_bisection` too
liamsi Jan 30, 2020
d3d1d65
rename some public methods of the verifier to be more aligned with th…
liamsi Jan 30, 2020
9f161ec
make trust_level satisfy Copy + Clone and don't pass by reference
liamsi Jan 30, 2020
48a7a37
refactor bisection to return a list of intermediate states (using mem…
liamsi Jan 30, 2020
ed8c06e
use Vec instead of HashMap to memoize / return the interm. sates
liamsi Jan 30, 2020
5f5b416
rename test helpers the assert_single_*
liamsi Jan 30, 2020
712ead7
WIP: quick first test for bisection
liamsi Jan 30, 2020
691acbc
WIP: add debug output to mocks & modify init_trusted_state to take ne…
liamsi Jan 31, 2020
e1e9f3c
WIP: minor refactoring: intro method to ensure uniqueness in the retu…
liamsi Jan 31, 2020
1879aba
Add some comments and clean-up the code to be slightly more consisten…
liamsi Jan 31, 2020
b7282b8
fix accidentally broken test
liamsi Jan 31, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tendermint-lite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pub mod requester;
pub mod state;
pub mod store;
pub mod threshold;
ebuchman marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 11 additions & 23 deletions tendermint-lite/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use tendermint::hash;
use tendermint::lite;
use tendermint::lite::Error;
use tendermint::lite::{
Header as _, Requester as _, SignedHeader as _, Store as _, TrustedState as _,
ValidatorSet as _,
};
use tendermint::lite::{Error, TrustThresholdFraction};
use tendermint::lite::{Header as _, Requester as _, Store as _, ValidatorSet as _};
use tendermint::rpc;
use tendermint::{block::Height, Hash};

use tendermint_lite::{
requester::RPCRequester, state::State, store::MemStore, threshold::TrustThresholdOneThird,
};
use tendermint_lite::{requester::RPCRequester, store::MemStore};

use core::future::Future;
use std::time::{Duration, SystemTime};
use tendermint_lite::store::State;
use tokio::runtime::Builder;

// TODO: these should be config/args
Expand All @@ -22,9 +18,6 @@ static SUBJECTIVE_VALS_HASH_HEX: &str =
"A5A7DEA707ADE6156F8A981777CA093F178FC790475F6EC659B6617E704871DD";
static RPC_ADDR: &str = "localhost:26657";

// TODO: this should somehow be configurable ...
static THRESHOLD: &TrustThresholdOneThird = &TrustThresholdOneThird {};

pub fn block_on<F: Future>(future: F) -> F::Output {
Builder::new()
.basic_scheduler()
Expand Down Expand Up @@ -52,7 +45,7 @@ fn main() {
let latest = (&req).signed_header(0).unwrap();
let latest_peer_height = latest.header().height();

let latest = store.get(Height::from(0)).unwrap();
let latest = store.get(0).unwrap();
let latest_height = latest.last_header().header().height();

// only bisect to higher heights
Expand All @@ -63,19 +56,14 @@ fn main() {

println!(
"attempting bisection from height {:?} to height {:?}",
store
.get(Height::from(0))
.unwrap()
.last_header()
.header()
.height(),
store.get(0).unwrap().last_header().header().height(),
latest_peer_height,
);

let now = &SystemTime::now();
lite::verify_and_update_bisection(
latest_peer_height,
THRESHOLD,
&TrustThresholdFraction::default(),
&trusting_period,
now,
&req,
Expand Down Expand Up @@ -106,13 +94,13 @@ fn subjective_init(
store: &mut MemStore,
req: &RPCRequester,
) -> Result<(), Error> {
if store.get(height).is_ok() {
if store.get(height.value()).is_ok() {
// we already have this !
return Ok(());
}

// check that the val hash matches
let vals = req.validator_set(height)?;
let vals = req.validator_set(height.value())?;

if vals.hash() != vals_hash {
// TODO
Expand All @@ -123,11 +111,11 @@ fn subjective_init(

// TODO: validate signed_header.commit() with the vals ...

let next_vals = req.validator_set(height.increment())?;
let next_vals = req.validator_set(height.increment().value())?;

// TODO: check next_vals ...

let trusted_state = &State::new(&signed_header, &next_vals);
let trusted_state = State::new(&signed_header, &next_vals);

store.add(trusted_state)?;

Expand Down
25 changes: 10 additions & 15 deletions tendermint-lite/src/requester.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use tendermint::block;
use tendermint::lite;
use tendermint::block::signed_header::SignedHeader as TMCommit;
use tendermint::block::Header as TMHeader;
use tendermint::rpc;
use tendermint::validator;
use tendermint::{block, lite};

use core::future::Future;
use tendermint::lite::{Height, SignedHeader};
use tendermint::validator::Set;
use tokio::runtime::Builder;

/// RPCRequester wraps the Tendermint rpc::Client.
Expand All @@ -17,33 +20,26 @@ impl RPCRequester {
}
}

impl lite::types::Requester for RPCRequester {
type SignedHeader = block::signed_header::SignedHeader;
type ValidatorSet = validator::Set;
type TMSignedHeader = SignedHeader<TMCommit, TMHeader>;

impl lite::types::Requester<TMCommit, TMHeader> for RPCRequester {
/// Request the signed header at height h.
/// If h==0, request the latest signed header.
/// TODO: use an enum instead of h==0.
fn signed_header<H>(&self, h: H) -> Result<Self::SignedHeader, lite::Error>
where
H: Into<block::Height>,
{
fn signed_header(&self, h: Height) -> Result<TMSignedHeader, lite::Error> {
let height: block::Height = h.into();
let r = match height.value() {
0 => block_on(self.client.latest_commit()),
_ => block_on(self.client.commit(height)),
};
match r {
Ok(response) => Ok(response.signed_header),
Ok(response) => Ok(response.signed_header.into()),
Err(_error) => Err(lite::Error::RequestFailed),
}
}

/// Request the validator set at height h.
fn validator_set<H>(&self, h: H) -> Result<Self::ValidatorSet, lite::Error>
where
H: Into<block::Height>,
{
fn validator_set(&self, h: Height) -> Result<Set, lite::Error> {
let r = block_on(self.client.validators(h));
match r {
Ok(response) => Ok(validator::Set::new(response.validators)),
Expand All @@ -66,7 +62,6 @@ mod tests {
use super::*;
use tendermint::lite::types::Header as LiteHeader;
use tendermint::lite::types::Requester as LiteRequester;
use tendermint::lite::types::SignedHeader as LiteSignedHeader;
use tendermint::lite::types::ValidatorSet as LiteValSet;
use tendermint::rpc;

Expand Down
30 changes: 0 additions & 30 deletions tendermint-lite/src/state.rs

This file was deleted.

21 changes: 10 additions & 11 deletions tendermint-lite/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::state::State;
use tendermint::block::Height;
use tendermint::lite::{Error, Header, SignedHeader, Store, TrustedState};
use tendermint::lite::{Error, Header, Height, Store, TrustedState};

use std::collections::HashMap;
use tendermint::block;

pub type State = TrustedState<block::signed_header::SignedHeader, block::header::Header>;

#[derive(Default)]
pub struct MemStore {
Expand All @@ -13,25 +14,23 @@ pub struct MemStore {
impl MemStore {
pub fn new() -> MemStore {
MemStore {
height: Height::from(0),
height: 0,
store: HashMap::new(),
}
}
}

impl Store for MemStore {
type TrustedState = State;

fn add(&mut self, trusted: &Self::TrustedState) -> Result<(), Error> {
impl Store<block::signed_header::SignedHeader, block::header::Header> for MemStore {
fn add(&mut self, trusted: State) -> Result<(), Error> {
let height = trusted.last_header().header().height();
self.height = height;
self.store.insert(height, trusted.clone());
self.store.insert(height, trusted);
Ok(())
}

fn get(&self, h: Height) -> Result<&Self::TrustedState, Error> {
fn get(&self, h: Height) -> Result<&State, Error> {
let mut height = h;
if h.value() == 0 {
if h == 0 {
height = self.height
}
match self.store.get(&height) {
Expand Down
11 changes: 0 additions & 11 deletions tendermint-lite/src/threshold.rs

This file was deleted.

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"}
liamsi marked this conversation as resolved.
Show resolved Hide resolved
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};
Loading