Skip to content

Commit 1c5b50b

Browse files
authored
Serialize ChainId without tendermint::chain::Id (#732)
* 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).
1 parent 7a57224 commit 1c5b50b

File tree

7 files changed

+18
-20
lines changed

7 files changed

+18
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ChainId should serialize itself without using tendermint::chain::Id
2+
([#729](https://github.com/cosmos/ibc-rs/issues/729))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- feat: use FromStr in client_type functions to construct ClientType
2+
([#731](https://github.com/cosmos/ibc-rs/pull/731))

crates/ibc/src/clients/ics07_tendermint/client_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ pub mod test_util {
11271127

11281128
pub fn new_dummy_from_header(tm_header: Header) -> ClientState {
11291129
ClientState::new(
1130-
tm_header.chain_id.clone().into(),
1130+
tm_header.chain_id.to_string().into(),
11311131
Default::default(),
11321132
Duration::from_secs(64000),
11331133
Duration::from_secs(128000),

crates/ibc/src/clients/ics07_tendermint/client_state/misbehaviour.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ impl ClientState {
7878
// main header verification, delegated to the tendermint-light-client crate.
7979
let untrusted_state = header.as_untrusted_block_state();
8080

81-
let chain_id = self.chain_id.clone().into();
81+
let chain_id = self
82+
.chain_id
83+
.to_string()
84+
.try_into()
85+
.map_err(|e| ClientError::Other {
86+
description: format!("failed to parse chain id: {}", e),
87+
})?;
8288
let trusted_state = header.as_trusted_block_state(trusted_consensus_state, &chain_id)?;
8389

8490
let options = self.as_light_client_options()?;

crates/ibc/src/clients/ics07_tendermint/client_state/update_client.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ impl ClientState {
4242
check_header_trusted_next_validator_set(&header, &trusted_consensus_state)?;
4343

4444
TrustedBlockState {
45-
chain_id: &self.chain_id.clone().into(),
45+
chain_id: &self.chain_id.to_string().try_into().map_err(|e| {
46+
ClientError::Other {
47+
description: format!("failed to parse chain id: {}", e),
48+
}
49+
})?,
4650
header_time: trusted_consensus_state.timestamp,
4751
height: header.trusted_height.revision_height().try_into().map_err(
4852
|_| ClientError::ClientSpecific {

crates/ibc/src/core/ics02_client/handler/update_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ mod tests {
329329
let client_state = {
330330
#[allow(deprecated)]
331331
let raw_client_state = RawTmClientState {
332-
chain_id: ChainId::from(tm_block.header().chain_id.clone()).to_string(),
332+
chain_id: ChainId::from(tm_block.header().chain_id.to_string()).to_string(),
333333
trust_level: Some(Fraction {
334334
numerator: 1,
335335
denominator: 3,

crates/ibc/src/core/ics24_host/identifier.rs

-16
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ const TRANSFER_PORT_ID: &str = "transfer";
4141
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
4242
)]
4343
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44-
#[cfg_attr(
45-
feature = "serde",
46-
serde(from = "tendermint::chain::Id", into = "tendermint::chain::Id")
47-
)]
4844
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
4945
pub struct ChainId {
5046
id: String,
@@ -165,18 +161,6 @@ impl Display for ChainId {
165161
}
166162
}
167163

168-
impl From<ChainId> for tendermint::chain::Id {
169-
fn from(id: ChainId) -> Self {
170-
tendermint::chain::Id::from_str(id.as_str()).unwrap()
171-
}
172-
}
173-
174-
impl From<tendermint::chain::Id> for ChainId {
175-
fn from(id: tendermint::chain::Id) -> Self {
176-
ChainId::from(id.to_string())
177-
}
178-
}
179-
180164
impl Default for ChainId {
181165
fn default() -> Self {
182166
Self::from_string(DEFAULT_CHAIN_ID)

0 commit comments

Comments
 (0)