-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add missing ClientType
, ClientId
validation checks
#639
Changes from all commits
e1bee48
eef2633
3244dbc
f2e599f
24877e4
e5f6710
3010b6d
262170e
4d437cb
c6ebd3e
72f635e
f04699e
5f02452
5080510
1396ad6
94bfc5e
6e36c3a
dd42949
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- Add missing `ClientType` and `ClientId` validation checks and move `ClientType` under the | ||
ICS24 section | ||
([#621](https://github.com/cosmos/ibc-rs/issues/621)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,10 @@ const VALID_SPECIAL_CHARS: &str = "._+-#[]<>"; | |
|
||
/// Default validator function for identifiers. | ||
/// | ||
/// A valid identifier only contain lowercase alphabetic characters, and be of a given min and max | ||
/// length. | ||
/// A valid identifier only contain valid characters, and be of a given min and | ||
/// max length as specified in the | ||
/// [`ICS-24`](https://github.com/cosmos/ibc/tree/main/spec/core/ics-024-host-requirements#paths-identifiers-separators)] | ||
/// spec. | ||
pub fn validate_identifier(id: &str, min: usize, max: usize) -> Result<(), Error> { | ||
assert!(max >= min); | ||
|
||
|
@@ -48,43 +50,76 @@ pub fn validate_identifier(id: &str, min: usize, max: usize) -> Result<(), Error | |
Ok(()) | ||
} | ||
|
||
/// Checks if the prefix can form a valid identifier with the given min/max identifier's length. | ||
fn validate_prefix_epoch_format( | ||
prefix: &str, | ||
min_id_length: usize, | ||
max_id_length: usize, | ||
) -> Result<(), Error> { | ||
// Checks if the prefix is not blank | ||
if prefix.is_empty() { | ||
return Err(Error::Empty)?; | ||
} | ||
|
||
// Checks if the prefix forms a valid identifier when used with `0` | ||
validate_identifier( | ||
&format!("{prefix}-{}", u64::MIN), | ||
min_id_length, | ||
max_id_length, | ||
)?; | ||
|
||
// Checks if the prefix forms a valid identifier when used with `u64::MAX` | ||
validate_identifier( | ||
&format!("{prefix}-{}", u64::MAX), | ||
min_id_length, | ||
max_id_length, | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Default validator function for the Client types. | ||
pub fn validate_client_type(id: &str) -> Result<(), Error> { | ||
validate_prefix_epoch_format(id, 9, 64) | ||
} | ||
|
||
/// Default validator function for Client identifiers. | ||
/// | ||
/// A valid identifier must be between 9-64 characters and only contain lowercase | ||
/// alphabetic characters, | ||
/// A valid client identifier must be between 9-64 characters as specified in | ||
/// the ICS-24 spec. | ||
pub fn validate_client_identifier(id: &str) -> Result<(), Error> { | ||
validate_identifier(id, 9, 64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer correct; client identifiers are not required to be in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. Not required by spec. Though, check here please. This is the only format we allow for client creation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another point: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the current way that we create client identifiers, which is a subset of all allowed client identifiers. In other words, if ever we changed how we do this in the future (e.g. to allow for other formats), then this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per our discussion, needed changes applied. Please check it out again. |
||
} | ||
|
||
/// Default validator function for Connection identifiers. | ||
/// | ||
/// A valid Identifier must be between 10-64 characters and only contain lowercase | ||
/// alphabetic characters, | ||
/// A valid connection identifier must be between 10-64 characters as specified | ||
/// in the ICS-24 spec. | ||
pub fn validate_connection_identifier(id: &str) -> Result<(), Error> { | ||
validate_identifier(id, 10, 64) | ||
} | ||
|
||
/// Default validator function for Port identifiers. | ||
/// | ||
/// A valid Identifier must be between 2-128 characters and only contain lowercase | ||
/// alphabetic characters, | ||
/// A valid port identifier must be between 2-128 characters as specified in the | ||
/// ICS-24 spec. | ||
pub fn validate_port_identifier(id: &str) -> Result<(), Error> { | ||
validate_identifier(id, 2, 128) | ||
} | ||
|
||
/// Default validator function for Channel identifiers. | ||
/// | ||
/// A valid identifier must be between 8-64 characters and only contain | ||
/// alphabetic characters, | ||
/// A valid channel identifier must be between 8-64 characters as specified in | ||
/// the ICS-24 spec. | ||
pub fn validate_channel_identifier(id: &str) -> Result<(), Error> { | ||
validate_identifier(id, 8, 64) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::core::ics24_host::validate::{ | ||
validate_channel_identifier, validate_client_identifier, validate_connection_identifier, | ||
validate_identifier, validate_port_identifier, | ||
validate_channel_identifier, validate_client_identifier, validate_client_type, | ||
validate_connection_identifier, validate_identifier, validate_port_identifier, | ||
}; | ||
use test_log::test; | ||
|
||
|
@@ -172,4 +207,22 @@ mod tests { | |
let id = validate_identifier("id/1", 1, 10); | ||
assert!(id.is_err()) | ||
} | ||
|
||
#[test] | ||
fn parse_healthy_client_type() { | ||
let id = validate_client_type("07-tendermint"); | ||
assert!(id.is_ok()) | ||
} | ||
|
||
#[test] | ||
fn parse_invalid_short_client_type() { | ||
let id = validate_client_type("<7Char"); | ||
assert!(id.is_err()) | ||
} | ||
|
||
#[test] | ||
fn parse_invalid_lengthy_client_type() { | ||
let id = validate_client_type("InvalidClientTypeWithLengthOfClientId>65Char"); | ||
assert!(id.is_err()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does it make more sense for ClientType to be in ics24_host? It’s still used as a prefix to a ClientId; seems to make more sense to live under ics02_client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since all the identifiers including the
ClientId
live inICS24_host
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ICS-24 defines the concept of an identifier.
ClientType
is, well, the "type" of a client, which turns out is used to create an identifier; it is not an identifier itself. Basically, it is a "client" concept, which is also used to create identifiers. I think it makes more sense for it to live where it wasThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe naming is getting us confused. We don't have such a type in the spec anymore, and honestly, Looking into the role of/functionality
ClientType
already serves (being only used as part/prefix ofClientId
). It's more ofClientIdPrefix
, so I still think it should be placed in ics24. Though, it's not a big deal. AlrightThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
94bfc5e