From 8a326f732ccd9a59abc34a79a44c73fc8b388bbb Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 26 Jun 2023 12:44:29 +0800 Subject: [PATCH 1/4] feat: use `TryFrom` in `client_type` functions to construct `ClientType` --- crates/ibc/src/clients/ics07_tendermint/mod.rs | 3 ++- crates/ibc/src/core/ics02_client/client_type.rs | 9 +++++---- crates/ibc/src/core/ics02_client/events.rs | 3 ++- crates/ibc/src/core/ics03_connection/events.rs | 3 ++- crates/ibc/src/mock/client_state.rs | 3 ++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/ibc/src/clients/ics07_tendermint/mod.rs b/crates/ibc/src/clients/ics07_tendermint/mod.rs index 76cb008f2..db06832a0 100644 --- a/crates/ibc/src/clients/ics07_tendermint/mod.rs +++ b/crates/ibc/src/clients/ics07_tendermint/mod.rs @@ -16,5 +16,6 @@ pub(crate) const TENDERMINT_CLIENT_TYPE: &str = "07-tendermint"; /// Returns the tendermint `ClientType` pub fn client_type() -> ClientType { - ClientType::from(TENDERMINT_CLIENT_TYPE.to_string()) + ClientType::try_from(TENDERMINT_CLIENT_TYPE.to_string()) + .expect("Never fails because it's valid") } diff --git a/crates/ibc/src/core/ics02_client/client_type.rs b/crates/ibc/src/core/ics02_client/client_type.rs index 853caf644..7cb83df8c 100644 --- a/crates/ibc/src/core/ics02_client/client_type.rs +++ b/crates/ibc/src/core/ics02_client/client_type.rs @@ -38,10 +38,11 @@ impl ClientType { } } -impl From for ClientType { - /// Constructs a new `ClientType` from the given `String` without performing any validation. - fn from(value: String) -> Self { - Self(value) +impl TryFrom for ClientType { + type Error = IdentifierError; + + fn try_from(s: String) -> Result { + Self::new(s) } } diff --git a/crates/ibc/src/core/ics02_client/events.rs b/crates/ibc/src/core/ics02_client/events.rs index 096f4b4f7..ea986a0c0 100644 --- a/crates/ibc/src/core/ics02_client/events.rs +++ b/crates/ibc/src/core/ics02_client/events.rs @@ -430,7 +430,8 @@ mod tests { expected_values: Vec<&'static str>, } - let client_type = ClientType::from("07-tendermint".to_string()); + let client_type = ClientType::try_from("07-tendermint".to_string()) + .expect("never fails because it's a valid client type"); let client_id = ClientId::new(client_type.clone(), 0).unwrap(); let consensus_height = Height::new(0, 5).unwrap(); let consensus_heights = vec![Height::new(0, 5).unwrap(), Height::new(0, 7).unwrap()]; diff --git a/crates/ibc/src/core/ics03_connection/events.rs b/crates/ibc/src/core/ics03_connection/events.rs index 01e8be2d5..046be1649 100644 --- a/crates/ibc/src/core/ics03_connection/events.rs +++ b/crates/ibc/src/core/ics03_connection/events.rs @@ -319,7 +319,8 @@ mod tests { expected_values: Vec<&'static str>, } - let client_type = ClientType::from("07-tendermint".to_string()); + let client_type = ClientType::try_from("07-tendermint".to_string()) + .expect("never fails because it's a valid client type"); let conn_id_on_a = ConnectionId::default(); let client_id_on_a = ClientId::new(client_type.clone(), 0).unwrap(); let conn_id_on_b = ConnectionId::new(1); diff --git a/crates/ibc/src/mock/client_state.rs b/crates/ibc/src/mock/client_state.rs index 4ca0c651c..e8c6ab860 100644 --- a/crates/ibc/src/mock/client_state.rs +++ b/crates/ibc/src/mock/client_state.rs @@ -32,7 +32,8 @@ pub const MOCK_CLIENT_STATE_TYPE_URL: &str = "/ibc.mock.ClientState"; pub const MOCK_CLIENT_TYPE: &str = "9999-mock"; pub fn client_type() -> ClientType { - ClientType::from(MOCK_CLIENT_TYPE.to_string()) + ClientType::try_from(MOCK_CLIENT_TYPE.to_string()) + .expect("never fails because it's valid client type") } /// A mock of an IBC client record as it is stored in a mock context. From b763c7dbf71d553023be0a4e522bc4439efc4c50 Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 26 Jun 2023 12:53:22 +0800 Subject: [PATCH 2/4] Fix ClientType conversion method in identifier.rs --- crates/ibc/src/core/ics24_host/identifier.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ibc/src/core/ics24_host/identifier.rs b/crates/ibc/src/core/ics24_host/identifier.rs index d73b30359..7de0dafec 100644 --- a/crates/ibc/src/core/ics24_host/identifier.rs +++ b/crates/ibc/src/core/ics24_host/identifier.rs @@ -213,7 +213,7 @@ impl ClientId { /// ``` /// # use ibc::core::ics24_host::identifier::ClientId; /// # use ibc::core::ics02_client::client_type::ClientType; - /// let tm_client_id = ClientId::new(ClientType::from("07-tendermint".to_string()), 0); + /// let tm_client_id = ClientId::new(ClientType::try_from("07-tendermint".to_string()).unwrap(), 0); /// assert!(tm_client_id.is_ok()); /// tm_client_id.map(|id| { assert_eq!(&id, "07-tendermint-0") }); /// ``` From c37cecfbd9da68b935616be17937751966d4ab1d Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 26 Jun 2023 22:14:02 +0800 Subject: [PATCH 3/4] Refactor client type parsing using `FromStr` in `ics07_tendermint`, `ics02_client`, `ics02_client/events`, `ics03_connection/events` and `mock/client_state` --- crates/ibc/src/clients/ics07_tendermint/mod.rs | 6 ++---- crates/ibc/src/core/ics02_client/client_type.rs | 13 ++++++++----- crates/ibc/src/core/ics02_client/events.rs | 3 ++- crates/ibc/src/core/ics03_connection/events.rs | 3 ++- crates/ibc/src/core/ics24_host/identifier.rs | 3 ++- crates/ibc/src/mock/client_state.rs | 4 ++-- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/ibc/src/clients/ics07_tendermint/mod.rs b/crates/ibc/src/clients/ics07_tendermint/mod.rs index db06832a0..a7dac8171 100644 --- a/crates/ibc/src/clients/ics07_tendermint/mod.rs +++ b/crates/ibc/src/clients/ics07_tendermint/mod.rs @@ -1,9 +1,8 @@ //! ICS 07: Tendermint Client implements a client verification algorithm for blockchains which use //! the Tendermint consensus algorithm. -use alloc::string::ToString; - use crate::core::ics02_client::client_type::ClientType; +use std::str::FromStr; pub mod client_state; pub mod consensus_state; @@ -16,6 +15,5 @@ pub(crate) const TENDERMINT_CLIENT_TYPE: &str = "07-tendermint"; /// Returns the tendermint `ClientType` pub fn client_type() -> ClientType { - ClientType::try_from(TENDERMINT_CLIENT_TYPE.to_string()) - .expect("Never fails because it's valid") + ClientType::from_str(TENDERMINT_CLIENT_TYPE).expect("Never fails because it's valid") } diff --git a/crates/ibc/src/core/ics02_client/client_type.rs b/crates/ibc/src/core/ics02_client/client_type.rs index 7cb83df8c..d842e84b3 100644 --- a/crates/ibc/src/core/ics02_client/client_type.rs +++ b/crates/ibc/src/core/ics02_client/client_type.rs @@ -1,7 +1,10 @@ //! Defines the `ClientType` format, typically used in chain IDs. use crate::prelude::*; -use core::fmt::{Display, Error as FmtError, Formatter}; +use core::{ + fmt::{Display, Error as FmtError, Formatter}, + str::FromStr, +}; use crate::core::ics24_host::{ identifier::validate::validate_client_type, identifier::IdentifierError, @@ -26,7 +29,7 @@ pub struct ClientType(String); impl ClientType { /// Constructs a new `ClientType` from the given `String` if it ends with a valid client identifier. - pub fn new(s: String) -> Result { + pub fn new(s: &str) -> Result { let s_trim = s.trim(); validate_client_type(s_trim)?; Ok(Self(s_trim.to_string())) @@ -38,10 +41,10 @@ impl ClientType { } } -impl TryFrom for ClientType { - type Error = IdentifierError; +impl FromStr for ClientType { + type Err = IdentifierError; - fn try_from(s: String) -> Result { + fn from_str(s: &str) -> Result { Self::new(s) } } diff --git a/crates/ibc/src/core/ics02_client/events.rs b/crates/ibc/src/core/ics02_client/events.rs index ea986a0c0..cbaf6dd7f 100644 --- a/crates/ibc/src/core/ics02_client/events.rs +++ b/crates/ibc/src/core/ics02_client/events.rs @@ -419,6 +419,7 @@ mod tests { use crate::core::timestamp::Timestamp; use crate::mock::header::MockHeader; use ibc_proto::google::protobuf::Any; + use std::str::FromStr; use tendermint::abci::Event as AbciEvent; #[test] @@ -430,7 +431,7 @@ mod tests { expected_values: Vec<&'static str>, } - let client_type = ClientType::try_from("07-tendermint".to_string()) + let client_type = ClientType::from_str("07-tendermint") .expect("never fails because it's a valid client type"); let client_id = ClientId::new(client_type.clone(), 0).unwrap(); let consensus_height = Height::new(0, 5).unwrap(); diff --git a/crates/ibc/src/core/ics03_connection/events.rs b/crates/ibc/src/core/ics03_connection/events.rs index 046be1649..face5278d 100644 --- a/crates/ibc/src/core/ics03_connection/events.rs +++ b/crates/ibc/src/core/ics03_connection/events.rs @@ -308,6 +308,7 @@ mod tests { use super::*; use crate::core::ics02_client::client_type::ClientType; + use std::str::FromStr; use tendermint::abci::Event as AbciEvent; #[test] @@ -319,7 +320,7 @@ mod tests { expected_values: Vec<&'static str>, } - let client_type = ClientType::try_from("07-tendermint".to_string()) + let client_type = ClientType::from_str("07-tendermint") .expect("never fails because it's a valid client type"); let conn_id_on_a = ConnectionId::default(); let client_id_on_a = ClientId::new(client_type.clone(), 0).unwrap(); diff --git a/crates/ibc/src/core/ics24_host/identifier.rs b/crates/ibc/src/core/ics24_host/identifier.rs index 7de0dafec..b55b5237b 100644 --- a/crates/ibc/src/core/ics24_host/identifier.rs +++ b/crates/ibc/src/core/ics24_host/identifier.rs @@ -213,7 +213,8 @@ impl ClientId { /// ``` /// # use ibc::core::ics24_host::identifier::ClientId; /// # use ibc::core::ics02_client::client_type::ClientType; - /// let tm_client_id = ClientId::new(ClientType::try_from("07-tendermint".to_string()).unwrap(), 0); + /// # use std::str::FromStr; + /// let tm_client_id = ClientId::new(ClientType::from_str("07-tendermint").unwrap(), 0); /// assert!(tm_client_id.is_ok()); /// tm_client_id.map(|id| { assert_eq!(&id, "07-tendermint-0") }); /// ``` diff --git a/crates/ibc/src/mock/client_state.rs b/crates/ibc/src/mock/client_state.rs index e8c6ab860..cf84f6d83 100644 --- a/crates/ibc/src/mock/client_state.rs +++ b/crates/ibc/src/mock/client_state.rs @@ -1,6 +1,7 @@ use crate::prelude::*; use alloc::collections::btree_map::BTreeMap as HashMap; +use core::str::FromStr; use core::time::Duration; use ibc_proto::google::protobuf::Any; @@ -32,8 +33,7 @@ pub const MOCK_CLIENT_STATE_TYPE_URL: &str = "/ibc.mock.ClientState"; pub const MOCK_CLIENT_TYPE: &str = "9999-mock"; pub fn client_type() -> ClientType { - ClientType::try_from(MOCK_CLIENT_TYPE.to_string()) - .expect("never fails because it's valid client type") + ClientType::from_str(MOCK_CLIENT_TYPE).expect("never fails because it's valid client type") } /// A mock of an IBC client record as it is stored in a mock context. From 8cc97ff2d28d9f39ed52f5d28b5cf6d008b3dae6 Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 26 Jun 2023 22:15:36 +0800 Subject: [PATCH 4/4] refactor: use core::str instead of std::str in mod.rs file of ICS07 Tendermint client --- crates/ibc/src/clients/ics07_tendermint/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ibc/src/clients/ics07_tendermint/mod.rs b/crates/ibc/src/clients/ics07_tendermint/mod.rs index a7dac8171..2dc390e56 100644 --- a/crates/ibc/src/clients/ics07_tendermint/mod.rs +++ b/crates/ibc/src/clients/ics07_tendermint/mod.rs @@ -2,7 +2,7 @@ //! the Tendermint consensus algorithm. use crate::core::ics02_client::client_type::ClientType; -use std::str::FromStr; +use core::str::FromStr; pub mod client_state; pub mod consensus_state;