diff --git a/light-client/src/client_state.rs b/light-client/src/client_state.rs index ee00f71..28880b9 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 { diff --git a/light-client/src/errors.rs b/light-client/src/errors.rs index 9b0d653..dfa7371 100644 --- a/light-client/src/errors.rs +++ b/light-client/src/errors.rs @@ -61,6 +61,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), @@ -164,6 +165,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)