Skip to content

Commit

Permalink
Serialize ChainId without tendermint::chain::Id (#732)
Browse files Browse the repository at this point in the history
* Refactor chain_id to avoid cloning to chain_id.to_string(). Convert chain_id from a raw string slice to a chain identifier type in several code locations.

* feat(ibc): remove `tendermint::chain::Id` dependency and use `FromStr` to construct `ClientType` in `ics07_tendermint` and `ics02_client` module (#729 and #731).
  • Loading branch information
DaviRain-Su authored Jun 27, 2023
1 parent 7a57224 commit 1c5b50b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- ChainId should serialize itself without using tendermint::chain::Id
([#729](https://github.com/cosmos/ibc-rs/issues/729))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- feat: use FromStr in client_type functions to construct ClientType
([#731](https://github.com/cosmos/ibc-rs/pull/731))
2 changes: 1 addition & 1 deletion crates/ibc/src/clients/ics07_tendermint/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ pub mod test_util {

pub fn new_dummy_from_header(tm_header: Header) -> ClientState {
ClientState::new(
tm_header.chain_id.clone().into(),
tm_header.chain_id.to_string().into(),
Default::default(),
Duration::from_secs(64000),
Duration::from_secs(128000),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ impl ClientState {
// main header verification, delegated to the tendermint-light-client crate.
let untrusted_state = header.as_untrusted_block_state();

let chain_id = self.chain_id.clone().into();
let chain_id = self
.chain_id
.to_string()
.try_into()
.map_err(|e| ClientError::Other {
description: format!("failed to parse chain id: {}", e),
})?;
let trusted_state = header.as_trusted_block_state(trusted_consensus_state, &chain_id)?;

let options = self.as_light_client_options()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ impl ClientState {
check_header_trusted_next_validator_set(&header, &trusted_consensus_state)?;

TrustedBlockState {
chain_id: &self.chain_id.clone().into(),
chain_id: &self.chain_id.to_string().try_into().map_err(|e| {
ClientError::Other {
description: format!("failed to parse chain id: {}", e),
}
})?,
header_time: trusted_consensus_state.timestamp,
height: header.trusted_height.revision_height().try_into().map_err(
|_| ClientError::ClientSpecific {
Expand Down
2 changes: 1 addition & 1 deletion crates/ibc/src/core/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ mod tests {
let client_state = {
#[allow(deprecated)]
let raw_client_state = RawTmClientState {
chain_id: ChainId::from(tm_block.header().chain_id.clone()).to_string(),
chain_id: ChainId::from(tm_block.header().chain_id.to_string()).to_string(),
trust_level: Some(Fraction {
numerator: 1,
denominator: 3,
Expand Down
16 changes: 0 additions & 16 deletions crates/ibc/src/core/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ const TRANSFER_PORT_ID: &str = "transfer";
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(from = "tendermint::chain::Id", into = "tendermint::chain::Id")
)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainId {
id: String,
Expand Down Expand Up @@ -165,18 +161,6 @@ impl Display for ChainId {
}
}

impl From<ChainId> for tendermint::chain::Id {
fn from(id: ChainId) -> Self {
tendermint::chain::Id::from_str(id.as_str()).unwrap()
}
}

impl From<tendermint::chain::Id> for ChainId {
fn from(id: tendermint::chain::Id) -> Self {
ChainId::from(id.to_string())
}
}

impl Default for ChainId {
fn default() -> Self {
Self::from_string(DEFAULT_CHAIN_ID)
Expand Down

0 comments on commit 1c5b50b

Please sign in to comment.