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
Changes from 1 commit
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
128 changes: 103 additions & 25 deletions tendermint/src/lite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,6 @@ impl Default for TrustThresholdFraction {
}
}

#[cfg(test)]
mod tests {
use crate::lite::{TrustThreshold, TrustThresholdFraction};

#[test]
fn default_is_enough_power() {
let threshold = TrustThresholdFraction::default();

// 100% > 33%
assert!(threshold.is_enough_power(3, 3));

// 66% > 33%
assert!(threshold.is_enough_power(2, 3));

// 33% <= 33%
assert_eq!(threshold.is_enough_power(1, 3), false);

// 1% < 33%
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 Expand Up @@ -204,7 +182,7 @@ where
}

/// SignedHeader bundles a [`Header`] and a [`Commit`] for convenience.
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq)] // NOTE: Copy/Clone/Debug for convenience in testing ...
pub struct SignedHeader<C, H>
where
C: Commit,
Expand All @@ -222,6 +200,7 @@ where
pub fn new(commit: C, header: H) -> Self {
Self { commit, header }
}

pub fn commit(&self) -> &C {
&self.commit
}
Expand All @@ -243,10 +222,109 @@ pub enum Error {

InvalidValidatorSet,
InvalidNextValidatorSet,
InvalidCommitValue, // commit is not for the header we expected
InvalidCommitValue,
// commit is not for the header we expected
liamsi marked this conversation as resolved.
Show resolved Hide resolved
InvalidCommitSignatures,
liamsi marked this conversation as resolved.
Show resolved Hide resolved
liamsi marked this conversation as resolved.
Show resolved Hide resolved
InvalidCommit, // signers do not account for +2/3 of the voting power

InsufficientVotingPower, // trust threshold (default +1/3) is not met
InsufficientVotingPower,
// trust threshold (default +1/3) is not met
RequestFailed,
}

#[cfg(test)]
mod tests {
use crate::lite::{Commit, Error, Header, SignedHeader, TrustedState, ValidatorSet};
use crate::lite::{TrustThreshold, TrustThresholdFraction};
use crate::Hash;
use std::time::SystemTime;

#[test]
fn default_is_enough_power() {
let threshold = TrustThresholdFraction::default();

// 100% > 33%
assert!(threshold.is_enough_power(3, 3));

// 66% > 33%
assert!(threshold.is_enough_power(2, 3));

// 33% <= 33%
assert_eq!(threshold.is_enough_power(1, 3), false);

// 1% < 33%
assert_eq!(threshold.is_enough_power(1, 100), false);
}

#[derive(Clone, Debug, PartialEq)]
struct MockCommit {}

#[derive(Clone, Debug, PartialEq)]
struct MockValSet {}

impl ValidatorSet for MockValSet {
fn hash(&self) -> Hash {
unimplemented!()
}
fn total_power(&self) -> u64 {
0
}
}

#[derive(Clone, Debug, PartialEq)]
struct MockHeader {}

impl Header for MockHeader {
type Time = SystemTime;

fn height(&self) -> u64 {
unimplemented!()
}
fn bft_time(&self) -> Self::Time {
unimplemented!()
}
fn validators_hash(&self) -> Hash {
unimplemented!()
}
fn next_validators_hash(&self) -> Hash {
unimplemented!()
}
fn hash(&self) -> Hash {
unimplemented!()
}
}

impl Commit for MockCommit {
type ValidatorSet = MockValSet;

fn header_hash(&self) -> Hash {
unimplemented!()
}
fn voting_power_in(&self, _: &Self::ValidatorSet) -> Result<u64, Error> {
Ok(0)
}
fn validate(&self, _: &Self::ValidatorSet) -> Result<(), Error> {
Ok(())
liamsi marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[test]
fn signed_header() {
let h = MockHeader {};
let c = MockCommit {};
let sh = SignedHeader::new(c.clone(), h.clone());
assert_eq!(sh.commit(), &c);
assert_eq!(sh.header(), &h);
}

#[test]
fn trusted_state() {
let h = MockHeader {};
let c = MockCommit {};
let vs = MockValSet {};
let sh = SignedHeader::new(c, h);
let ts = TrustedState::new(&sh, &vs);
assert_eq!(ts.last_header(), &sh);
assert_eq!(ts.validators(), &vs);
}
liamsi marked this conversation as resolved.
Show resolved Hide resolved
}