Skip to content

Commit

Permalink
fix: serde support for Height without revision_number (#1397)
Browse files Browse the repository at this point in the history
* put tests under mod

* optional rev number during Height deserialization

* add test

* use serde cfg_attr

* changelog entry

* nit: changelog

---------

Co-authored-by: Farhad Shabani <[email protected]>
  • Loading branch information
rnbguy and Farhad-Shabani authored Jan 28, 2025
1 parent a5f9fbf commit 230e7a5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ibc-core-client-types] Serde support for `Height` without `revision_number`
([#1262](https://github.com/cosmos/ibc-rs/issues/1262)).
3 changes: 2 additions & 1 deletion ibc-core/ics02-client/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ parity-scale-codec = { workspace = true, optional = true }
scale-info = { workspace = true, optional = true }

[dev-dependencies]
rstest = { workspace = true }
rstest = { workspace = true }
serde-json = { workspace = true }

[features]
default = [ "std" ]
Expand Down
82 changes: 49 additions & 33 deletions ibc-core/ics02-client/types/src/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::error::ClientError;
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Height {
/// Previously known as "epoch"
#[cfg_attr(feature = "serde", serde(default))]
revision_number: u64,

/// The height of a block
Expand Down Expand Up @@ -179,37 +180,52 @@ impl FromStr for Height {
}
}

#[test]
fn test_valid_height() {
assert_eq!(
"1-1".parse::<Height>().unwrap(),
Height {
revision_number: 1,
revision_height: 1
}
);
assert_eq!(
"1-10".parse::<Height>().unwrap(),
Height {
revision_number: 1,
revision_height: 10
}
);
}

#[test]
fn test_invalid_height() {
assert!("0-0".parse::<Height>().is_err());
assert!("0-".parse::<Height>().is_err());
assert!("-0".parse::<Height>().is_err());
assert!("-".parse::<Height>().is_err());
assert!("1-1-1".parse::<Height>().is_err());

let decoding_err = "1".parse::<Height>().unwrap_err();
let decoding_err = decoding_err.to_string();
assert!(decoding_err.contains("height `1` not properly formatted"));

let decoding_err = "".parse::<Height>().unwrap_err();
let decoding_err = decoding_err.to_string();
assert!(decoding_err.contains("height `` not properly formatted"));
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_valid_height() {
assert_eq!(
"1-1".parse::<Height>().unwrap(),
Height {
revision_number: 1,
revision_height: 1
}
);
assert_eq!(
"1-10".parse::<Height>().unwrap(),
Height {
revision_number: 1,
revision_height: 10
}
);
}

#[test]
fn test_invalid_height() {
assert!("0-0".parse::<Height>().is_err());
assert!("0-".parse::<Height>().is_err());
assert!("-0".parse::<Height>().is_err());
assert!("-".parse::<Height>().is_err());
assert!("1-1-1".parse::<Height>().is_err());

let decoding_err = "1".parse::<Height>().unwrap_err();
let decoding_err = decoding_err.to_string();
assert!(decoding_err.contains("height `1` not properly formatted"));

let decoding_err = "".parse::<Height>().unwrap_err();
let decoding_err = decoding_err.to_string();
assert!(decoding_err.contains("height `` not properly formatted"));
}

#[test]
fn test_empty_rev_number_deserialization() {
// #1262: ibc-go uses `omitempty` in JSON serialization
let json_str = r#"{"revision_height": 10}"#;
let actual: Height = serde_json::from_str(json_str).unwrap();
let expected = Height::new(0, 10).unwrap();

assert_eq!(actual, expected);
}
}

0 comments on commit 230e7a5

Please sign in to comment.