Skip to content

Commit

Permalink
Valid TrustThresholdFraction and further simplify tests code (#142)
Browse files Browse the repository at this point in the history
* Valid TrustThresholdFraction for real

* Further simplify tests: init requester with final state

* Apply review suggestions and assert_bisection_err fn

* remove redundant clone

* add another err case

* unpublicize verify_commit_full and verify_commit_trusting (ref #136)

* ensure the untrusted_header.bft_time() > trusted_header.bft_time()

* Fix failing test and test and add another erroring one

* remove dupl test

* Consistent comments in tests

* Refactor init_requester (#152)

* added bisection test for insufficient commits

* refactored init_requester, TODO: update tests

* fixed tests

* Added bisection test for insufficient commits

* fixed failures

* cargo fmt

* Changed tests to use ValsAndCommit struct

* satisfy clippy

* Dealing with the merge conflict's aftermath

Co-authored-by: Shivani Joshi <46731446+Shivani912@users.noreply.github.com>
  • Loading branch information
liamsi and Shivani912 committed Feb 19, 2020
1 parent 8df037d commit afcf99a
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 80 deletions.
4 changes: 4 additions & 0 deletions tendermint/src/lite/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub enum Kind {
#[error("expected height >= {expected} (got: {got})")]
NonIncreasingHeight { got: u64, expected: u64 },

/// Header time is in the past compared to already trusted header.
#[error("untrusted header time <= trusted header time")]
NonIncreasingTime,

/// Invalid validator hash.
#[error("header's validator hash does not match actual validator hash ({header_val_hash:?}!={val_hash:?})")]
InvalidValidatorSet {
Expand Down
21 changes: 18 additions & 3 deletions tendermint/src/lite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,22 @@ pub struct TrustThresholdFraction {
}

impl TrustThresholdFraction {
pub fn new(numerator: u64, denominator: u64) -> Result<Self, Kind> {
if numerator <= denominator && denominator > 0 {
/// Instantiate a TrustThresholdFraction if the given denominator and
/// numerator are valid.
///
/// The parameters are valid iff `1/3 <= numerator/denominator <= 1`.
/// In any other case we return [`Error::InvalidTrustThreshold`].
pub fn new(numerator: u64, denominator: u64) -> Result<Self, Error> {
if numerator <= denominator && denominator > 0 && 3 * numerator >= denominator {
return Ok(Self {
numerator,
denominator,
});
}
Err(Kind::InvalidTrustThreshold {
got: format!("{}/{}", numerator, denominator),
})
}
.into())
}
}

Expand Down Expand Up @@ -227,6 +233,10 @@ pub(super) mod mocks {
next_vals,
}
}

pub fn set_time(&mut self, new_time: SystemTime) {
self.time = new_time
}
}

impl Header for MockHeader {
Expand Down Expand Up @@ -430,8 +440,13 @@ mod tests {
TrustThresholdFraction::new(1, 3).expect("mustn't panic")
);
assert!(TrustThresholdFraction::new(2, 3).is_ok());
assert!(TrustThresholdFraction::new(1, 1).is_ok());

assert!(TrustThresholdFraction::new(3, 1).is_err());
assert!(TrustThresholdFraction::new(1, 4).is_err());
assert!(TrustThresholdFraction::new(1, 5).is_err());
assert!(TrustThresholdFraction::new(2, 7).is_err());
assert!(TrustThresholdFraction::new(0, 1).is_err());
assert!(TrustThresholdFraction::new(1, 0).is_err());
}
}
Loading

0 comments on commit afcf99a

Please sign in to comment.