Skip to content

Commit

Permalink
fix header timestamp validation
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 11, 2024
1 parent cd4fc7f commit 071109a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/ibc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum Error {
target_height: Height,
},
/// unexpected timestamp: expected={0} got={1}
UnexpectedTimestamp(u64, u64),
UnexpectedTimestamp(i128, i128),
/// missing trusting period
MissingTrustingPeriod,
/// negative max clock drift
Expand Down
59 changes: 54 additions & 5 deletions crates/ibc/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,27 @@ pub fn decode_header<const SYNC_COMMITTEE_SIZE: usize, B: Buf>(
impl<const SYNC_COMMITTEE_SIZE: usize> Header<SYNC_COMMITTEE_SIZE> {
pub fn validate<C: ChainContext>(&self, ctx: &C) -> Result<(), Error> {
self.trusted_sync_committee.sync_committee.validate()?;
let header_timestamp = self.timestamp.into_tm_time().unwrap().unix_timestamp() as u64;
let timestamp =
if self.timestamp.into_tm_time().is_none() {
return Err(Error::ZeroTimestampError);
}
let header_timestamp_nanos = self
.timestamp
.into_tm_time()
.unwrap()
.unix_timestamp_nanos();
let timestamp_secs =
compute_timestamp_at_slot(ctx, self.consensus_update.finalized_beacon_header().slot);
if header_timestamp != timestamp.0 {
let timestamp_nanos = i128::from(timestamp_secs.0)
.checked_mul(1_000_000_000)
.ok_or_else(|| {
Error::TimestampOverflowError(
ibc::timestamp::TimestampOverflowError::TimestampOverflow,
)
})?;
if header_timestamp_nanos != timestamp_nanos {
return Err(Error::UnexpectedTimestamp(
timestamp.into(),
header_timestamp,
timestamp_nanos,
header_timestamp_nanos,
));
}
Ok(())
Expand Down Expand Up @@ -266,6 +280,41 @@ mod tests {
let res = Header::<32>::try_from(any);
assert!(res.is_err(), "header with zero timestamp should fail");
}

let update = gen_light_client_update_with_params::<32, _>(
&ctx,
base_signature_slot,
base_attested_slot,
base_finalized_epoch,
dummy_execution_state_root,
dummy_execution_block_number.into(),
current_sync_committee,
scm.get_committee(2),
true,
32,
);
let update = to_consensus_update_info(update);
let header = Header {
trusted_sync_committee: TrustedSyncCommittee {
height: ibc::Height::new(1, 1).unwrap(),
sync_committee: current_sync_committee.to_committee().clone(),
is_next: true,
},
consensus_update: update.clone(),
execution_update: ExecutionUpdateInfo::default(),
account_update: AccountUpdateInfo::default(),
timestamp: Timestamp::from_nanoseconds(
compute_timestamp_at_slot(&ctx, update.finalized_beacon_header().slot).0
* 1_000_000_000
+ 1,
)
.unwrap(),
};
let res = header.validate(&ctx);
assert!(
res.is_err(),
"header validation should fail for wrong timestamp"
);
}

fn to_consensus_update_info<const SYNC_COMMITTEE_SIZE: usize>(
Expand Down

0 comments on commit 071109a

Please sign in to comment.