Skip to content

Commit 230e7a5

Browse files
fix: serde support for Height without revision_number (#1397)
* 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]>
1 parent a5f9fbf commit 230e7a5

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [ibc-core-client-types] Serde support for `Height` without `revision_number`
2+
([#1262](https://github.com/cosmos/ibc-rs/issues/1262)).

ibc-core/ics02-client/types/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ parity-scale-codec = { workspace = true, optional = true }
4242
scale-info = { workspace = true, optional = true }
4343

4444
[dev-dependencies]
45-
rstest = { workspace = true }
45+
rstest = { workspace = true }
46+
serde-json = { workspace = true }
4647

4748
[features]
4849
default = [ "std" ]

ibc-core/ics02-client/types/src/height.rs

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::error::ClientError;
3131
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
3232
pub struct Height {
3333
/// Previously known as "epoch"
34+
#[cfg_attr(feature = "serde", serde(default))]
3435
revision_number: u64,
3536

3637
/// The height of a block
@@ -179,37 +180,52 @@ impl FromStr for Height {
179180
}
180181
}
181182

182-
#[test]
183-
fn test_valid_height() {
184-
assert_eq!(
185-
"1-1".parse::<Height>().unwrap(),
186-
Height {
187-
revision_number: 1,
188-
revision_height: 1
189-
}
190-
);
191-
assert_eq!(
192-
"1-10".parse::<Height>().unwrap(),
193-
Height {
194-
revision_number: 1,
195-
revision_height: 10
196-
}
197-
);
198-
}
199-
200-
#[test]
201-
fn test_invalid_height() {
202-
assert!("0-0".parse::<Height>().is_err());
203-
assert!("0-".parse::<Height>().is_err());
204-
assert!("-0".parse::<Height>().is_err());
205-
assert!("-".parse::<Height>().is_err());
206-
assert!("1-1-1".parse::<Height>().is_err());
207-
208-
let decoding_err = "1".parse::<Height>().unwrap_err();
209-
let decoding_err = decoding_err.to_string();
210-
assert!(decoding_err.contains("height `1` not properly formatted"));
211-
212-
let decoding_err = "".parse::<Height>().unwrap_err();
213-
let decoding_err = decoding_err.to_string();
214-
assert!(decoding_err.contains("height `` not properly formatted"));
183+
#[cfg(test)]
184+
mod tests {
185+
use super::*;
186+
187+
#[test]
188+
fn test_valid_height() {
189+
assert_eq!(
190+
"1-1".parse::<Height>().unwrap(),
191+
Height {
192+
revision_number: 1,
193+
revision_height: 1
194+
}
195+
);
196+
assert_eq!(
197+
"1-10".parse::<Height>().unwrap(),
198+
Height {
199+
revision_number: 1,
200+
revision_height: 10
201+
}
202+
);
203+
}
204+
205+
#[test]
206+
fn test_invalid_height() {
207+
assert!("0-0".parse::<Height>().is_err());
208+
assert!("0-".parse::<Height>().is_err());
209+
assert!("-0".parse::<Height>().is_err());
210+
assert!("-".parse::<Height>().is_err());
211+
assert!("1-1-1".parse::<Height>().is_err());
212+
213+
let decoding_err = "1".parse::<Height>().unwrap_err();
214+
let decoding_err = decoding_err.to_string();
215+
assert!(decoding_err.contains("height `1` not properly formatted"));
216+
217+
let decoding_err = "".parse::<Height>().unwrap_err();
218+
let decoding_err = decoding_err.to_string();
219+
assert!(decoding_err.contains("height `` not properly formatted"));
220+
}
221+
222+
#[test]
223+
fn test_empty_rev_number_deserialization() {
224+
// #1262: ibc-go uses `omitempty` in JSON serialization
225+
let json_str = r#"{"revision_height": 10}"#;
226+
let actual: Height = serde_json::from_str(json_str).unwrap();
227+
let expected = Height::new(0, 10).unwrap();
228+
229+
assert_eq!(actual, expected);
230+
}
215231
}

0 commit comments

Comments
 (0)