-
Notifications
You must be signed in to change notification settings - Fork 228
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
Followup on 149 and 151 #155
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b38fb0d
use fail! where it makes sense
liamsi 4dce902
don't need formatting directive
liamsi 1833e77
simplify `is_within_trust_period` via `expires_at` var:
liamsi a6f7c64
error string
liamsi 301958f
Behave like spec and go-code for edge case: the header just expired a…
liamsi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,30 +15,38 @@ use crate::lite::error::{Error, Kind}; | |
use crate::lite::{ | ||
Commit, Header, Height, Requester, SignedHeader, TrustThreshold, TrustedState, ValidatorSet, | ||
}; | ||
use anomaly::ensure; | ||
use std::ops::Add; | ||
|
||
/// Returns an error if the header has expired according to the given | ||
/// trusting_period and current time. If so, the verifier must be reset subjectively. | ||
fn is_within_trust_period<H>( | ||
last_header: &H, | ||
trusting_period: &Duration, | ||
now: &SystemTime, | ||
trusting_period: Duration, | ||
now: SystemTime, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both |
||
) -> Result<(), Error> | ||
where | ||
H: Header, | ||
{ | ||
match now.duration_since(last_header.bft_time().into()) { | ||
Ok(passed) => { | ||
if passed > *trusting_period { | ||
return Err(Kind::Expired { | ||
at: last_header.bft_time().into() + *trusting_period, | ||
now: *now, | ||
} | ||
.into()); | ||
} | ||
Ok(()) | ||
let header_time: SystemTime = last_header.bft_time().into(); | ||
let expires_at = header_time.add(trusting_period); | ||
// Ensure now > expires_at. | ||
if expires_at <= now { | ||
return Err(Kind::Expired { | ||
at: expires_at, | ||
now, | ||
} | ||
Err(e) => Err(Kind::DurationOutOfRange(e).into()), | ||
.into()); | ||
} | ||
// Also make sure the header is not after now. | ||
ensure!( | ||
header_time <= now, | ||
Kind::DurationOutOfRange, | ||
"header time: ({:?}) > now: ({:?})", | ||
header_time, | ||
now | ||
); | ||
Ok(()) | ||
} | ||
|
||
/// Validate the validators, next validators, against the signed header. | ||
|
@@ -219,8 +227,8 @@ pub fn verify_single<H, C, L>( | |
untrusted_vals: &C::ValidatorSet, | ||
untrusted_next_vals: &C::ValidatorSet, | ||
trust_threshold: L, | ||
trusting_period: &Duration, | ||
now: &SystemTime, | ||
trusting_period: Duration, | ||
now: SystemTime, | ||
) -> Result<TrustedState<C, H>, Error> | ||
where | ||
H: Header, | ||
|
@@ -269,8 +277,8 @@ pub fn verify_bisection<C, H, L, R>( | |
trusted_state: TrustedState<C, H>, | ||
untrusted_height: Height, | ||
trust_threshold: L, | ||
trusting_period: &Duration, | ||
now: &SystemTime, | ||
trusting_period: Duration, | ||
now: SystemTime, | ||
req: &R, | ||
) -> Result<Vec<TrustedState<C, H>>, Error> | ||
where | ||
|
@@ -459,7 +467,7 @@ mod tests { | |
req | ||
} | ||
|
||
// make a state with the given vals and commit and ensure we get the error. | ||
// make a state with the given vals and commit and ensure we get the expected error kind. | ||
fn assert_single_err( | ||
ts: &TrustedState<MockCommit, MockHeader>, | ||
vals: Vec<usize>, | ||
|
@@ -475,7 +483,7 @@ mod tests { | |
TrustThresholdFraction::default(), | ||
); | ||
assert!(result.is_err()); | ||
assert_eq!(format!("{}", result.unwrap_err()), format!("{}", err)); | ||
assert_eq!(result.unwrap_err().to_string(), err.to_string()); | ||
} | ||
|
||
// make a state with the given vals and commit and ensure we get no error. | ||
|
@@ -682,20 +690,20 @@ mod tests { | |
|
||
// less than the period, OK | ||
let header = MockHeader::new(4, header_time, fixed_hash(), fixed_hash()); | ||
assert!(is_within_trust_period(&header, &period, &now).is_ok()); | ||
assert!(is_within_trust_period(&header, period, now).is_ok()); | ||
|
||
// equal to the period, OK | ||
// equal to the period, not OK | ||
let now = header_time + period; | ||
assert!(is_within_trust_period(&header, &period, &now).is_ok()); | ||
assert!(is_within_trust_period(&header, period, now).is_err()); | ||
|
||
// greater than the period, not OK | ||
let now = header_time + period + Duration::new(1, 0); | ||
assert!(is_within_trust_period(&header, &period, &now).is_err()); | ||
assert!(is_within_trust_period(&header, period, now).is_err()); | ||
|
||
// bft time in header is later than now, not OK: | ||
let now = SystemTime::UNIX_EPOCH; | ||
let later_than_now = now + Duration::new(60, 0); | ||
let future_header = MockHeader::new(4, later_than_now, fixed_hash(), fixed_hash()); | ||
assert!(is_within_trust_period(&future_header, &period, &now).is_err()); | ||
assert!(is_within_trust_period(&future_header, period, now).is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should rename this error? Note this is different from the NonIncreasingTime one introduced in #142, namely:
https://github.com/interchainio/tendermint-rs/blob/0b75eee8c88c4c012670c9e526742ae66ce5598c/tendermint/src/lite/error.rs#L27-L29
(or the Expired error)
See:
https://github.com/interchainio/tendermint-rs/blob/a6f7c64e8214d5fbf32adee5ee3b6cbac09ee9da/tendermint/src/lite/verifier.rs#L35-L49
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#137 (comment)