Skip to content

Commit d57bba4

Browse files
authored
imp(types): refactor Default implementations with concrete names (#1099)
* use concrete channel order enum * refactor(ics04): remove Version::default() * refactor(ics04): remove TimeoutHeight::default() * refactor: remove channelId::default() * fix: Version::default() to Version::empty() * remove derive default from Timestamp * refactor default builder * remove Sequence::default() * remove Memo::default() * refactor: add ChannelId::zero() * add From<&str> implmentation * refactor(ibccore): remove ClientId::default() * refactor(ibccore): remove version::Default() * refactor(ibccore): remove ConnectionEnd::default() * refactor(ibc-apps): remove TracePath::default() * refactor(ibc-core): remove ProofSpecs::default() * fix(ibc-app): remove upgrade path default * remove trust threshold default * refactor(ibc-testkit): remove Default from client_state initialization * refactor(ibc-testkit): remove Default() of ClientId, ConnectionId,... * chore: add unclog * refactor: add Connection::zero() * use into() instead of ::from() * refactor: add Version::compatibles() * refactor(ics24-host): make 07-tendermint-0 a constant in tests * chore: remove commented out impl Default * add dummy ClientId * refactor(ics24-host): remove From<&str> to avoid panic on host * remove get_compatible_versions * fix syntax error when import Version * chore: run cargo fmt * revert markdown format * various refactor * chore: update unclog
1 parent e512d37 commit d57bba4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+352
-358
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [types] Refactor `Default` implementations with concrete names
2+
([\#1074](https://github.com/cosmos/ibc-rs/issues/1074))

docs/architecture/adr-005-handlers-redesign.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ trait ValidationContext {
296296
/// Function required by ICS 03. Returns the list of all possible versions that the connection
297297
/// handshake protocol supports.
298298
fn get_compatible_versions(&self) -> Vec<Version> {
299-
get_compatible_versions()
299+
ConnectionVersion::compatibles()
300300
}
301301

302302
/// Function required by ICS 03. Returns one version out of the supplied list of versions, which the

ibc-apps/ics20-transfer/types/src/denom.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Display for TracePrefix {
109109
)]
110110
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
111111
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
112-
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, From)]
112+
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, From)]
113113
pub struct TracePath(Vec<TracePrefix>);
114114

115115
impl TracePath {
@@ -134,6 +134,11 @@ impl TracePath {
134134
pub fn is_empty(&self) -> bool {
135135
self.0.is_empty()
136136
}
137+
138+
/// Return empty trace path
139+
pub fn empty() -> Self {
140+
Self(vec![])
141+
}
137142
}
138143

139144
impl<'a> TryFrom<Vec<&'a str>> for TracePath {
@@ -294,7 +299,7 @@ impl FromStr for PrefixedDenom {
294299

295300
let (base_denom, trace_path) = {
296301
if last_part == s {
297-
(BaseDenom::from_str(s)?, TracePath::default())
302+
(BaseDenom::from_str(s)?, TracePath::empty())
298303
} else {
299304
let base_denom = BaseDenom::from_str(last_part)?;
300305
let trace_path = TracePath::try_from(parts)?;
@@ -334,7 +339,7 @@ impl From<PrefixedDenom> for RawDenomTrace {
334339
impl From<BaseDenom> for PrefixedDenom {
335340
fn from(denom: BaseDenom) -> Self {
336341
Self {
337-
trace_path: Default::default(),
342+
trace_path: TracePath::empty(),
338343
base_denom: denom,
339344
}
340345
}

ibc-apps/ics20-transfer/types/src/memo.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ impl From<String> for Memo {
4545
}
4646
}
4747

48+
impl From<&str> for Memo {
49+
fn from(memo: &str) -> Self {
50+
Self(memo.to_owned())
51+
}
52+
}
53+
4854
impl FromStr for Memo {
4955
type Err = Infallible;
5056

ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ where
8181
&msg.chan_id_on_a,
8282
class_id,
8383
token_id,
84-
&packet_data.memo.clone().unwrap_or_default(),
84+
&packet_data.memo.clone().unwrap_or("".into()),
8585
)?;
8686
} else {
8787
transfer_ctx.burn_nft_validate(
8888
&sender,
8989
class_id,
9090
token_id,
91-
&packet_data.memo.clone().unwrap_or_default(),
91+
&packet_data.memo.clone().unwrap_or("".into()),
9292
)?;
9393
}
9494
let nft = transfer_ctx.get_nft(class_id, token_id)?;
@@ -184,14 +184,14 @@ where
184184
&msg.chan_id_on_a,
185185
class_id,
186186
token_id,
187-
&packet_data.memo.clone().unwrap_or_default(),
187+
&packet_data.memo.clone().unwrap_or("".into()),
188188
)?;
189189
} else {
190190
transfer_ctx.burn_nft_execute(
191191
&sender,
192192
class_id,
193193
token_id,
194-
&packet_data.memo.clone().unwrap_or_default(),
194+
&packet_data.memo.clone().unwrap_or("".into()),
195195
)?;
196196
}
197197
let nft = transfer_ctx.get_nft(class_id, token_id)?;
@@ -242,7 +242,7 @@ where
242242
receiver: packet_data.receiver,
243243
class: packet_data.class_id,
244244
tokens: packet_data.token_ids,
245-
memo: packet_data.memo.unwrap_or_default(),
245+
memo: packet_data.memo.unwrap_or("".into()),
246246
};
247247
send_packet_ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into())?;
248248

ibc-apps/ics721-nft-transfer/src/module.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Provides IBC module callbacks implementation for the ICS-721 transfer.
2-
32
use ibc_core::channel::types::acknowledgement::{Acknowledgement, AcknowledgementStatus};
43
use ibc_core::channel::types::channel::{Counterparty, Order};
54
use ibc_core::channel::types::packet::Packet;
@@ -190,7 +189,7 @@ pub fn on_recv_packet_execute(
190189
receiver: data.receiver,
191190
class: data.class_id,
192191
tokens: data.token_ids,
193-
memo: data.memo.unwrap_or_default(),
192+
memo: data.memo.unwrap_or("".into()),
194193
success: ack.is_successful(),
195194
};
196195
extras.events.push(recv_event.into());
@@ -250,7 +249,7 @@ pub fn on_acknowledgement_packet_execute(
250249
receiver: data.receiver,
251250
class: data.class_id,
252251
tokens: data.token_ids,
253-
memo: data.memo.unwrap_or_default(),
252+
memo: data.memo.unwrap_or("".into()),
254253
acknowledgement: acknowledgement.clone(),
255254
};
256255

@@ -295,7 +294,7 @@ pub fn on_timeout_packet_execute(
295294
refund_receiver: data.sender,
296295
refund_class: data.class_id,
297296
refund_tokens: data.token_ids,
298-
memo: data.memo.unwrap_or_default(),
297+
memo: data.memo.unwrap_or("".into()),
299298
};
300299

301300
let extras = ModuleExtras {

ibc-apps/ics721-nft-transfer/types/src/class.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Display for TracePrefix {
106106
)]
107107
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
108108
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
109-
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, From)]
109+
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, From)]
110110
pub struct TracePath(Vec<TracePrefix>);
111111

112112
impl TracePath {
@@ -131,6 +131,11 @@ impl TracePath {
131131
pub fn is_empty(&self) -> bool {
132132
self.0.is_empty()
133133
}
134+
135+
/// Return empty trace path
136+
pub fn empty() -> Self {
137+
Self(vec![])
138+
}
134139
}
135140

136141
impl<'a> TryFrom<Vec<&'a str>> for TracePath {
@@ -268,7 +273,7 @@ impl FromStr for PrefixedClassId {
268273

269274
let (base_class_id, trace_path) = {
270275
if last_part == s {
271-
(ClassId::from_str(s)?, TracePath::default())
276+
(ClassId::from_str(s)?, TracePath::empty())
272277
} else {
273278
let base_class_id = ClassId::from_str(last_part)?;
274279
let trace_path = TracePath::try_from(parts)?;
@@ -308,7 +313,7 @@ impl From<PrefixedClassId> for RawClassTrace {
308313
impl From<ClassId> for PrefixedClassId {
309314
fn from(class_id: ClassId) -> Self {
310315
Self {
311-
trace_path: Default::default(),
316+
trace_path: TracePath::empty(),
312317
base_class_id: class_id,
313318
}
314319
}

ibc-apps/ics721-nft-transfer/types/src/data.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::error::NftTransferError;
2424
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
2525
)]
2626
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27-
#[derive(Clone, Debug, Default, PartialEq, Eq, derive_more::From)]
27+
#[derive(Clone, Debug, PartialEq, Eq, derive_more::From)]
2828
pub struct Data(String);
2929

3030
#[cfg(feature = "serde")]
@@ -88,7 +88,7 @@ impl<'de> serde::Deserialize<'de> for Data {
8888
)]
8989
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9090
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
91-
#[derive(Clone, Debug, Default, PartialEq, Eq)]
91+
#[derive(Clone, Debug, PartialEq, Eq)]
9292
pub struct Ics721Data(BTreeMap<String, DataValue>);
9393

9494
#[cfg(feature = "serde")]
@@ -100,7 +100,7 @@ impl FromStr for Ics721Data {
100100
}
101101
}
102102

103-
#[derive(Clone, Debug, Default, PartialEq, Eq)]
103+
#[derive(Clone, Debug, PartialEq, Eq)]
104104
pub struct DataValue {
105105
value: String,
106106
mime: Option<Mime>,

ibc-apps/ics721-nft-transfer/types/src/memo.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ibc_core::primitives::prelude::*;
2424
)]
2525
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2626
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27-
#[derive(Clone, Debug, Default, PartialEq, Eq)]
27+
#[derive(Clone, Debug, PartialEq, Eq)]
2828
pub struct Memo(String);
2929

3030
impl AsRef<str> for Memo {
@@ -45,6 +45,12 @@ impl From<String> for Memo {
4545
}
4646
}
4747

48+
impl From<&str> for Memo {
49+
fn from(memo: &str) -> Self {
50+
Self(memo.to_owned())
51+
}
52+
}
53+
4854
impl FromStr for Memo {
4955
type Err = Infallible;
5056

ibc-clients/ics07-tendermint/src/client_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ mod tests {
111111
unbonding_period: Duration::new(128_000, 0),
112112
max_clock_drift: Duration::new(3, 0),
113113
latest_height: Height::new(1, 10).expect("Never fails"),
114-
proof_specs: ProofSpecs::default(),
115-
upgrade_path: Default::default(),
114+
proof_specs: ProofSpecs::cosmos(),
115+
upgrade_path: Vec::new(),
116116
allow_update: AllowUpdate {
117117
after_expiry: false,
118118
after_misbehaviour: false,

ibc-clients/ics07-tendermint/types/src/client_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ mod tests {
438438
unbonding_period: Duration::new(128_000, 0),
439439
max_clock_drift: Duration::new(3, 0),
440440
latest_height: Height::new(0, 10).expect("Never fails"),
441-
proof_specs: ProofSpecs::default(),
442-
upgrade_path: Default::default(),
441+
proof_specs: ProofSpecs::cosmos(),
442+
upgrade_path: Vec::new(),
443443
allow_update: AllowUpdate {
444444
after_expiry: false,
445445
after_misbehaviour: false,

ibc-clients/ics07-tendermint/types/src/trust_threshold.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ impl TryFrom<Fraction> for TrustThreshold {
134134
}
135135
}
136136

137-
impl Default for TrustThreshold {
138-
fn default() -> Self {
139-
Self::ONE_THIRD
140-
}
141-
}
142-
143137
impl Display for TrustThreshold {
144138
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
145139
write!(f, "{}/{}", self.numerator, self.denominator)

ibc-core/ics03-connection/types/src/connection.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Defines the types that define a connection
22
33
use core::fmt::{Display, Error as FmtError, Formatter};
4+
use core::str::FromStr;
45
use core::time::Duration;
56
use core::u64;
67

@@ -200,18 +201,6 @@ mod sealed {
200201
}
201202
}
202203

203-
impl Default for ConnectionEnd {
204-
fn default() -> Self {
205-
Self {
206-
state: State::Uninitialized,
207-
client_id: Default::default(),
208-
counterparty: Default::default(),
209-
versions: Vec::new(),
210-
delay_period: ZERO_DURATION,
211-
}
212-
}
213-
}
214-
215204
impl Protobuf<RawConnectionEnd> for ConnectionEnd {}
216205

217206
impl TryFrom<RawConnectionEnd> for ConnectionEnd {
@@ -220,7 +209,17 @@ impl TryFrom<RawConnectionEnd> for ConnectionEnd {
220209
let state = value.state.try_into()?;
221210

222211
if state == State::Uninitialized {
223-
return Ok(ConnectionEnd::default());
212+
return ConnectionEnd::new(
213+
State::Uninitialized,
214+
ClientId::from_str("07-tendermint-0").expect("should not fail"),
215+
Counterparty::new(
216+
ClientId::from_str("07-tendermint-0").expect("should not fail"),
217+
None,
218+
CommitmentPrefix::empty(),
219+
),
220+
Vec::new(),
221+
ZERO_DURATION,
222+
);
224223
}
225224

226225
if value.client_id.is_empty() {
@@ -378,7 +377,7 @@ impl ConnectionEnd {
378377
)]
379378
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
380379
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
381-
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
380+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
382381
pub struct Counterparty {
383382
pub client_id: ClientId,
384383
pub connection_id: Option<ConnectionId>,

ibc-core/ics03-connection/types/src/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub const COUNTERPARTY_CLIENT_ID_ATTRIBUTE_KEY: &str = "counterparty_client_id";
2929
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
3030
)]
3131
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32-
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
32+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
3333
struct Attributes {
3434
pub connection_id: ConnectionId,
3535
pub client_id: ClientId,
@@ -323,7 +323,7 @@ mod tests {
323323

324324
let client_type = ClientType::from_str("07-tendermint")
325325
.expect("never fails because it's a valid client type");
326-
let conn_id_on_a = ConnectionId::default();
326+
let conn_id_on_a = ConnectionId::zero();
327327
let client_id_on_a = client_type.build_client_id(0);
328328
let conn_id_on_b = ConnectionId::new(1);
329329
let client_id_on_b = client_type.build_client_id(1);

0 commit comments

Comments
 (0)