Skip to content

Commit

Permalink
optimize validate_prefix_length
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Nov 3, 2023
1 parent a4648d7 commit 69301f8
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions crates/ibc/src/core/ics24_host/identifier/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,26 @@ pub fn validate_prefix_length(
min_id_length: u64,
max_id_length: u64,
) -> Result<(), Error> {
assert!(max_id_length >= min_id_length);

if prefix.is_empty() {
return Err(Error::InvalidPrefix {
prefix: prefix.into(),
});
}

// Checks if the prefix forms a valid identifier length when constructed with `u64::MIN`
validate_identifier_length(
&format!("{prefix}-{}", u64::MIN),
min_id_length,
max_id_length,
)?;

// Checks if the prefix forms a valid identifier length when constructed with `u64::MAX`
validate_identifier_length(
&format!("{prefix}-{}", u64::MAX),
min_id_length,
max_id_length,
)?;
// Checks if the prefix forms a valid identifier length when constructed with `u64::MIN` and `u64::MAX`
// Checks `prefix` directly adding the 2 and 21 chars for the separator and the `u64::MIN` and `u64::MAX` value.
// Valid condition: (min <= len + 2 <= max) && (min <= len + 21 <= max) which can be simplified to:
// (min <= len + 2) && (len + 21 <= max)
if (prefix.len() as u64 + 2) < min_id_length || (prefix.len() as u64 + 21) > max_id_length {
return Err(Error::InvalidLength {
id: prefix.into(),
length: prefix.len() as u64,
min: min_id_length,
max: max_id_length,
});
}

Ok(())
}
Expand Down

0 comments on commit 69301f8

Please sign in to comment.