diff --git a/crates/ibc/src/clients/ics07_tendermint/mod.rs b/crates/ibc/src/clients/ics07_tendermint/mod.rs index 76cb008f2..2dc390e56 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 core::str::FromStr; pub mod client_state; pub mod consensus_state; @@ -16,5 +15,5 @@ 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::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 853caf644..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,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 FromStr for ClientType { + type Err = IdentifierError; + + 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 096f4b4f7..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,8 @@ mod tests { expected_values: Vec<&'static str>, } - let client_type = ClientType::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(); 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..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,8 @@ mod tests { expected_values: Vec<&'static str>, } - let client_type = ClientType::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(); let conn_id_on_b = ConnectionId::new(1); diff --git a/crates/ibc/src/core/ics24_host/identifier.rs b/crates/ibc/src/core/ics24_host/identifier.rs index d73b30359..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::from("07-tendermint".to_string()), 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 4ca0c651c..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,7 +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::from(MOCK_CLIENT_TYPE.to_string()) + 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.