Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub enum Error {
),
#[error("OutOfTrustingPeriod: current={0} deadline={1}")]
OutOfTrustingPeriod(Time, Time),
#[error("CurrentTimeBeforeTrustedState: current={0} trusted={1}")]
CurrentTimeBeforeTrustedState(Time, Time),
#[error("HeaderFromFuture: current={0} drift={1:?} header_ts={2}")]
HeaderFromFuture(Time, core::time::Duration, Time),
#[error("VerifyMembershipError: err={0:?}")]
Expand Down
33 changes: 33 additions & 0 deletions light-client/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub fn validate_state_timestamp_within_trusting_period(
trusting_period: Duration,
trusted_consensus_state_timestamp: Time,
) -> Result<(), Error> {
if current_timestamp.lt(&trusted_consensus_state_timestamp) {
return Err(Error::CurrentTimeBeforeTrustedState(
current_timestamp,
trusted_consensus_state_timestamp,
));
}
let trusting_period_end =
(trusted_consensus_state_timestamp + trusting_period).map_err(Error::TimeError)?;
if !trusting_period_end.gt(&current_timestamp) {
Expand Down Expand Up @@ -81,6 +87,11 @@ mod test {
3,
trusted_state_timestamp,
);
validate_and_assert_trusted_ts_error(
current_timestamp,
3,
current_timestamp + Duration::new(0, 1),
);
}

// clock drift
Expand Down Expand Up @@ -174,4 +185,26 @@ mod test {
panic!("expected error");
}
}

fn validate_and_assert_trusted_ts_error(
current_timestamp: OffsetDateTime,
trusting_period: u64,
trusted_state_timestamp: OffsetDateTime,
) {
let result = validate_state_timestamp_within_trusting_period(
Time::from_unix_timestamp_nanos(current_timestamp.unix_timestamp_nanos() as u128)
.unwrap(),
Duration::from_nanos(trusting_period),
Time::from_unix_timestamp_nanos(trusted_state_timestamp.unix_timestamp_nanos() as u128)
.unwrap(),
);
if let Err(e) = result {
match e {
Error::CurrentTimeBeforeTrustedState(_, _) => {}
_ => panic!("unexpected error: {e}"),
}
} else {
panic!("expected error");
}
}
}
Loading