diff --git a/light-client/src/client_state.rs b/light-client/src/client_state.rs index ee00f71..2f21719 100644 --- a/light-client/src/client_state.rs +++ b/light-client/src/client_state.rs @@ -159,6 +159,13 @@ impl TryFrom for ClientState { let chain_id = ChainId::new(value.chain_id); + if chain_id.version() != raw_latest_height.revision_number { + return Err(Error::UnexpectedLatestHeightRevision( + chain_id.version(), + raw_latest_height.revision_number, + )); + } + let latest_height = new_height( raw_latest_height.revision_number, raw_latest_height.revision_height, @@ -489,6 +496,19 @@ mod test { err => unreachable!("{:?}", err), } + cs.latest_height = Some(Height { + revision_number: 1, + revision_height: 0, + }); + let err = ClientState::try_from(cs.clone()).unwrap_err(); + match err { + Error::UnexpectedLatestHeightRevision(e1, e2) => { + assert_eq!(e1, 0); + assert_eq!(e2, 1); + } + err => unreachable!("{:?}", err), + } + cs.latest_height = Some(Height::default()); let err = ClientState::try_from(cs.clone()).unwrap_err(); match err { @@ -609,7 +629,7 @@ mod test { Time::from_unix_timestamp_nanos( untrusted_header_timestamp.unix_timestamp_nanos() as u128 ) - .unwrap(), + .unwrap(), Time::from_unix_timestamp_nanos(trusted_state_timestamp.unix_timestamp_nanos() as u128) .unwrap(), ); @@ -631,7 +651,7 @@ mod test { Time::from_unix_timestamp_nanos( untrusted_header_timestamp.unix_timestamp_nanos() as u128 ) - .unwrap(), + .unwrap(), Time::from_unix_timestamp_nanos(trusted_state_timestamp.unix_timestamp_nanos() as u128) .unwrap(), ); @@ -660,7 +680,7 @@ mod test { Time::from_unix_timestamp_nanos( untrusted_header_timestamp.unix_timestamp_nanos() as u128 ) - .unwrap(), + .unwrap(), Time::from_unix_timestamp_nanos(trusted_state_timestamp.unix_timestamp_nanos() as u128) .unwrap(), ); diff --git a/light-client/src/errors.rs b/light-client/src/errors.rs index 7c152ce..a90d2a5 100644 --- a/light-client/src/errors.rs +++ b/light-client/src/errors.rs @@ -60,6 +60,7 @@ pub enum Error { UnexpectedTrustedHeight(BlockNumber, BlockNumber), EmptyHeader, UnexpectedHeaderRevision(u64, u64), + UnexpectedLatestHeightRevision(u64, u64), UnexpectedSignature(BlockNumber, signature::Error), MissingVanityInExtraData(BlockNumber, usize, usize), MissingSignatureInExtraData(BlockNumber, usize, usize), @@ -163,6 +164,9 @@ impl core::fmt::Display for Error { Error::UnexpectedHeaderRevision(e1, e2) => { write!(f, "UnexpectedHeaderRevision: {} {}", e1, e2) } + Error::UnexpectedLatestHeightRevision(e1, e2) => { + write!(f, "UnexpectedLatestHeightRevision: {} {}", e1, e2) + } Error::UnexpectedSignature(e1, e2) => write!(f, "UnexpectedSignature: {} {}", e1, e2), Error::MissingVanityInExtraData(e1, e2, e3) => { write!(f, "MissingVanityInExtraData: {} {} {}", e1, e2, e3)