Skip to content
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

feat: use FromStr in client_type functions to construct ClientType #731

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions crates/ibc/src/clients/ics07_tendermint/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
}
16 changes: 10 additions & 6 deletions crates/ibc/src/core/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Self, IdentifierError> {
pub fn new(s: &str) -> Result<Self, IdentifierError> {
let s_trim = s.trim();
validate_client_type(s_trim)?;
Ok(Self(s_trim.to_string()))
Expand All @@ -38,10 +41,11 @@ impl ClientType {
}
}

impl From<String> 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, Self::Err> {
Self::new(s)
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/ibc/src/core/ics02_client/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()];
Expand Down
4 changes: 3 additions & 1 deletion crates/ibc/src/core/ics03_connection/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion crates/ibc/src/core/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
/// ```
Expand Down
3 changes: 2 additions & 1 deletion crates/ibc/src/mock/client_state.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down