@@ -27,7 +27,7 @@ pub fn validate_identifier_chars(id: &str) -> Result<(), Error> {
27
27
/// [`ICS-24`](https://github.com/cosmos/ibc/tree/main/spec/core/ics-024-host-requirements#paths-identifiers-separators)]
28
28
/// spec.
29
29
pub fn validate_identifier_length ( id : & str , min : u64 , max : u64 ) -> Result < ( ) , Error > {
30
- assert ! ( 0 < min && min <= max ) ;
30
+ assert ! ( max >= min ) ;
31
31
32
32
// Check identifier length is between given min/max
33
33
if !( min..=max) . contains ( & ( id. len ( ) as u64 ) ) {
@@ -46,39 +46,30 @@ pub fn validate_prefix_length(
46
46
min_id_length : u64 ,
47
47
max_id_length : u64 ,
48
48
) -> Result < ( ) , Error > {
49
- assert ! ( max_id_length >= min_id_length) ;
49
+ // Check that
50
+ // 1. min_id_length allows for the shortest possible identifier such as
51
+ // `p-0`,
52
+ // 2. difference in length between minimum and maximum constraint allows for
53
+ // full range of revision numbers. The shortest revision number is just
54
+ // one digit; the longest is len(u64::MAX) which is
55
+ assert ! (
56
+ min_id_length >= 3 && max_id_length. saturating_sub( min_id_length) >= 19 ,
57
+ "{min_id_length} {max_id_length}"
58
+ ) ;
50
59
51
60
if prefix. is_empty ( ) {
52
61
return Err ( Error :: InvalidPrefix {
53
62
prefix : prefix. into ( ) ,
54
63
} ) ;
55
64
}
56
65
57
- // Statically checks if the prefix forms a valid identifier length when constructed with `u64::MAX`
58
- // len(prefix + '-' + u64::MAX) <= max_id_length (minimum prefix length is 1)
59
- if max_id_length < 22 {
60
- return Err ( Error :: InvalidLength {
61
- id : prefix. into ( ) ,
62
- min : 0 ,
63
- max : 0 ,
64
- } ) ;
65
- }
66
-
67
- // Checks if the prefix forms a valid identifier length when constructed with `u64::MIN`
68
- // len('-' + u64::MIN) = 2
69
- validate_identifier_length (
70
- prefix,
71
- min_id_length. saturating_sub ( 2 ) ,
72
- max_id_length. saturating_sub ( 2 ) ,
73
- ) ?;
74
-
75
- // Checks if the prefix forms a valid identifier length when constructed with `u64::MAX`
76
- // len('-' + u64::MAX) = 21
77
- validate_identifier_length (
78
- prefix,
79
- min_id_length. saturating_sub ( 21 ) ,
80
- max_id_length. saturating_sub ( 21 ) ,
81
- ) ?;
66
+ // 1. The shortest identifier constructed from prefix is `<prefix>-0` so the
67
+ // prefix must be at least min_id_length - 2 characters long for that to
68
+ // be valid identifier.
69
+ // 2. The longest identifier is `<prefix>-<u64::MAX>` (which adds 21
70
+ // characters to the prefix as len('-' + u64::MAX) = 21) so the prefix
71
+ // must be at most max_id_length - 21 characters long.
72
+ validate_identifier_length ( prefix, min_id_length. saturating_sub ( 2 ) , max_id_length - 21 ) ?;
82
73
83
74
Ok ( ( ) )
84
75
}
@@ -236,15 +227,14 @@ mod tests {
236
227
}
237
228
238
229
#[ rstest]
239
- #[ case:: empty_prefix( "" , 1 , 64 , false ) ]
240
- #[ case:: max_is_low( "a" , 1 , 10 , false ) ]
241
- #[ case:: u64_max_is_too_big( "a" , 3 , 21 , false ) ]
242
- #[ case:: u64_min_is_too_small( "a" , 4 , 22 , false ) ]
243
- #[ case:: u64_min_max_boundary( "a" , 3 , 22 , true ) ]
244
- #[ case( "chainA" , 1 , 32 , true ) ]
245
- #[ case( "chainA" , 1 , 64 , true ) ]
230
+ #[ case:: at_bounds( "a" , 3 , 22 , true ) ]
231
+ #[ case:: u64_max_too_long( "ab" , 3 , 22 , false ) ]
232
+ #[ case:: at_bounds_foo( "foo" , 5 , 25 , true ) ]
233
+ #[ case:: u64_min_too_short( "fo" , 5 , 25 , false ) ]
234
+ #[ case( "chainA" , 3 , 32 , true ) ]
235
+ #[ case( "chainA" , 3 , 64 , true ) ]
246
236
#[ test_log:: test]
247
- fn test_prefix_length_validation (
237
+ fn test_prefix_length_validation_success (
248
238
#[ case] prefix : & str ,
249
239
#[ case] min : u64 ,
250
240
#[ case] max : u64 ,
@@ -253,4 +243,13 @@ mod tests {
253
243
let result = validate_prefix_length ( prefix, min, max) ;
254
244
assert_eq ! ( result. is_ok( ) , success) ;
255
245
}
246
+
247
+ #[ rstest]
248
+ #[ case:: min_is_low( 2 , 21 ) ]
249
+ #[ case:: max_is_low( 3 , 21 ) ]
250
+ #[ should_panic]
251
+ #[ test_log:: test]
252
+ fn test_prefix_length_validation_panic ( #[ case] min : u64 , #[ case] max : u64 ) {
253
+ let _ = validate_prefix_length ( "a" , min, max) ;
254
+ }
256
255
}
0 commit comments