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

imp(types): refactor Default implementations with concrete names #1099

Merged
merged 40 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4f68431
use concrete channel order enum
tuantran1702 Feb 24, 2024
59df516
Merge branch 'main' into tuan/refactor-default
tuantran1702 Feb 24, 2024
f194383
refactor(ics04): remove Version::default()
tuantran1702 Feb 26, 2024
de47605
refactor(ics04): remove TimeoutHeight::default()
tuantran1702 Feb 26, 2024
e384dca
refactor: remove channelId::default()
tuantran1702 Feb 26, 2024
526acde
fix: Version::default() to Version::empty()
tuantran1702 Feb 26, 2024
f4f0dae
remove derive default from Timestamp
tuantran1702 Feb 26, 2024
000959e
refactor default builder
tuantran1702 Feb 26, 2024
2cb0808
remove Sequence::default()
tuantran1702 Feb 26, 2024
6a31d85
remove Memo::default()
tuantran1702 Feb 26, 2024
bab6785
refactor: add ChannelId::zero()
tuantran1702 Feb 29, 2024
332e9e6
Merge branch 'main' into tuan/refactor-default
tuantran1702 Feb 29, 2024
e800ea8
add From<&str> implmentation
tuantran1702 Feb 29, 2024
c957bd9
refactor(ibccore): remove ClientId::default()
tuantran1702 Mar 8, 2024
36fba96
refactor(ibccore): remove version::Default()
tuantran1702 Mar 8, 2024
48656b7
refactor(ibccore): remove ConnectionEnd::default()
tuantran1702 Mar 8, 2024
f1c1f13
refactor(ibc-apps): remove TracePath::default()
tuantran1702 Mar 8, 2024
436703f
refactor(ibc-core): remove ProofSpecs::default()
tuantran1702 Mar 8, 2024
6d41e93
fix(ibc-app): remove upgrade path default
tuantran1702 Mar 8, 2024
04ae141
remove trust threshold default
tuantran1702 Mar 8, 2024
d0562e0
refactor(ibc-testkit): remove Default from client_state initialization
tuantran1702 Mar 8, 2024
8aea66d
refactor(ibc-testkit): remove Default() of ClientId, ConnectionId,...
tuantran1702 Mar 8, 2024
37ce288
chore: add unclog
tuantran1702 Mar 9, 2024
3572e3b
Merge branch 'main' into tuan/refactor-default
tuantran1702 Mar 9, 2024
cd2addd
refactor: add Connection::zero()
tuantran1702 Mar 11, 2024
ec84de5
use into() instead of ::from()
tuantran1702 Mar 11, 2024
7750afd
refactor: add Version::compatibles()
tuantran1702 Mar 11, 2024
f279965
Merge branch 'tuan/refactor-default' of github.com:notional-labs/ibc-…
tuantran1702 Mar 11, 2024
a860979
refactor(ics24-host): make 07-tendermint-0 a constant in tests
tuantran1702 Mar 12, 2024
a206527
chore: remove commented out impl Default
tuantran1702 Mar 12, 2024
596eccb
add dummy ClientId
tuantran1702 Mar 12, 2024
6aba54f
refactor(ics24-host): remove From<&str> to avoid panic on host
tuantran1702 Mar 12, 2024
9f4bd71
remove get_compatible_versions
tuantran1702 Mar 12, 2024
91dedf1
fix syntax error when import Version
tuantran1702 Mar 12, 2024
c7e86e8
chore: run cargo fmt
tuantran1702 Mar 12, 2024
4fd5dca
revert markdown format
tuantran1702 Mar 12, 2024
cd22fb2
Merge branch 'main' into tuan/refactor-default
tuantran1702 Mar 12, 2024
fabc36c
Merge branch 'tuan/refactor-default' of github.com:notional-labs/ibc-…
tuantran1702 Mar 12, 2024
fdf2a7c
various refactor
tuantran1702 Mar 12, 2024
46910f3
chore: update unclog
tuantran1702 Mar 12, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [types] Refactor Default implementations with concrete names
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved
([\#1074](https://github.com/cosmos/ibc-rs/issues/1074))
11 changes: 8 additions & 3 deletions ibc-apps/ics20-transfer/types/src/denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Display for TracePrefix {
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, From)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, From)]
pub struct TracePath(Vec<TracePrefix>);

impl TracePath {
Expand All @@ -134,6 +134,11 @@ impl TracePath {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Return empty trace path
pub fn empty() -> Self {
Self(vec![])
}
}

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

let (base_denom, trace_path) = {
if last_part == s {
(BaseDenom::from_str(s)?, TracePath::default())
(BaseDenom::from_str(s)?, TracePath::empty())
} else {
let base_denom = BaseDenom::from_str(last_part)?;
let trace_path = TracePath::try_from(parts)?;
Expand Down Expand Up @@ -334,7 +339,7 @@ impl From<PrefixedDenom> for RawDenomTrace {
impl From<BaseDenom> for PrefixedDenom {
fn from(denom: BaseDenom) -> Self {
Self {
trace_path: Default::default(),
trace_path: TracePath::empty(),
base_denom: denom,
}
}
Expand Down
6 changes: 6 additions & 0 deletions ibc-apps/ics20-transfer/types/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ impl From<String> for Memo {
}
}

impl From<&str> for Memo {
fn from(memo: &str) -> Self {
Self(memo.to_owned())
}
}

impl FromStr for Memo {
type Err = Infallible;

Expand Down
10 changes: 5 additions & 5 deletions ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@
&msg.chan_id_on_a,
class_id,
token_id,
&packet_data.memo.clone().unwrap_or_default(),
&packet_data.memo.clone().unwrap_or("".into()),

Check warning on line 84 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L84 was not covered by tests
)?;
} else {
transfer_ctx.burn_nft_validate(
&sender,
class_id,
token_id,
&packet_data.memo.clone().unwrap_or_default(),
&packet_data.memo.clone().unwrap_or("".into()),

Check warning on line 91 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L91 was not covered by tests
)?;
}
let nft = transfer_ctx.get_nft(class_id, token_id)?;
Expand Down Expand Up @@ -184,14 +184,14 @@
&msg.chan_id_on_a,
class_id,
token_id,
&packet_data.memo.clone().unwrap_or_default(),
&packet_data.memo.clone().unwrap_or("".into()),

Check warning on line 187 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L187 was not covered by tests
)?;
} else {
transfer_ctx.burn_nft_execute(
&sender,
class_id,
token_id,
&packet_data.memo.clone().unwrap_or_default(),
&packet_data.memo.clone().unwrap_or("".into()),

Check warning on line 194 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L194 was not covered by tests
)?;
}
let nft = transfer_ctx.get_nft(class_id, token_id)?;
Expand Down Expand Up @@ -242,7 +242,7 @@
receiver: packet_data.receiver,
class: packet_data.class_id,
tokens: packet_data.token_ids,
memo: packet_data.memo.unwrap_or_default(),
memo: packet_data.memo.unwrap_or("".into()),

Check warning on line 245 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L245 was not covered by tests
};
send_packet_ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into())?;

Expand Down
7 changes: 3 additions & 4 deletions ibc-apps/ics721-nft-transfer/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Provides IBC module callbacks implementation for the ICS-721 transfer.

use ibc_core::channel::types::acknowledgement::{Acknowledgement, AcknowledgementStatus};
use ibc_core::channel::types::channel::{Counterparty, Order};
use ibc_core::channel::types::packet::Packet;
Expand Down Expand Up @@ -194,7 +193,7 @@
receiver: data.receiver,
class: data.class_id,
tokens: data.token_ids,
memo: data.memo.unwrap_or_default(),
memo: data.memo.unwrap_or("".into()),

Check warning on line 196 in ibc-apps/ics721-nft-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L196 was not covered by tests
success: ack.is_successful(),
};
extras.events.push(recv_event.into());
Expand Down Expand Up @@ -259,7 +258,7 @@
receiver: data.receiver,
class: data.class_id,
tokens: data.token_ids,
memo: data.memo.unwrap_or_default(),
memo: data.memo.unwrap_or("".into()),

Check warning on line 261 in ibc-apps/ics721-nft-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L261 was not covered by tests
acknowledgement: acknowledgement.clone(),
};

Expand Down Expand Up @@ -307,7 +306,7 @@
refund_receiver: data.sender,
refund_class: data.class_id,
refund_tokens: data.token_ids,
memo: data.memo.unwrap_or_default(),
memo: data.memo.unwrap_or("".into()),

Check warning on line 309 in ibc-apps/ics721-nft-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L309 was not covered by tests
};

let extras = ModuleExtras {
Expand Down
11 changes: 8 additions & 3 deletions ibc-apps/ics721-nft-transfer/types/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, From)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, From)]
pub struct TracePath(Vec<TracePrefix>);

impl TracePath {
Expand All @@ -131,6 +131,11 @@
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Return empty trace path
pub fn empty() -> Self {
Self(vec![])
}
}

impl<'a> TryFrom<Vec<&'a str>> for TracePath {
Expand Down Expand Up @@ -268,7 +273,7 @@

let (base_class_id, trace_path) = {
if last_part == s {
(ClassId::from_str(s)?, TracePath::default())
(ClassId::from_str(s)?, TracePath::empty())
} else {
let base_class_id = ClassId::from_str(last_part)?;
let trace_path = TracePath::try_from(parts)?;
Expand Down Expand Up @@ -308,7 +313,7 @@
impl From<ClassId> for PrefixedClassId {
fn from(class_id: ClassId) -> Self {
Self {
trace_path: Default::default(),
trace_path: TracePath::empty(),

Check warning on line 316 in ibc-apps/ics721-nft-transfer/types/src/class.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L316 was not covered by tests
base_class_id: class_id,
}
}
Expand Down
6 changes: 3 additions & 3 deletions ibc-apps/ics721-nft-transfer/types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Eq, derive_more::From)]
#[derive(Clone, Debug, PartialEq, Eq, derive_more::From)]
pub struct Data(String);

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -88,7 +88,7 @@
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]

Check warning on line 91 in ibc-apps/ics721-nft-transfer/types/src/data.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L91 was not covered by tests
pub struct Ics721Data(BTreeMap<String, DataValue>);

#[cfg(feature = "serde")]
Expand All @@ -100,7 +100,7 @@
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DataValue {
value: String,
mime: Option<Mime>,
Expand Down
8 changes: 7 additions & 1 deletion ibc-apps/ics721-nft-transfer/types/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Memo(String);

impl AsRef<str> for Memo {
Expand All @@ -45,6 +45,12 @@
}
}

impl From<&str> for Memo {
fn from(memo: &str) -> Self {
Self(memo.to_owned())
}

Check warning on line 51 in ibc-apps/ics721-nft-transfer/types/src/memo.rs

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L49 - L51 were not covered by tests
}

impl FromStr for Memo {
type Err = Infallible;

Expand Down
4 changes: 2 additions & 2 deletions ibc-clients/ics07-tendermint/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ mod tests {
unbonding_period: Duration::new(128000, 0),
max_clock_drift: Duration::new(3, 0),
latest_height: Height::new(1, 10).expect("Never fails"),
proof_specs: ProofSpecs::default(),
upgrade_path: Default::default(),
proof_specs: ProofSpecs::cosmos(),
upgrade_path: Vec::new(),
allow_update: AllowUpdate {
after_expiry: false,
after_misbehaviour: false,
Expand Down
4 changes: 2 additions & 2 deletions ibc-clients/ics07-tendermint/types/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ mod tests {
unbonding_period: Duration::new(128000, 0),
max_clock_drift: Duration::new(3, 0),
latest_height: Height::new(0, 10).expect("Never fails"),
proof_specs: ProofSpecs::default(),
upgrade_path: Default::default(),
proof_specs: ProofSpecs::cosmos(),
upgrade_path: Vec::new(),
allow_update: AllowUpdate {
after_expiry: false,
after_misbehaviour: false,
Expand Down
10 changes: 5 additions & 5 deletions ibc-clients/ics07-tendermint/types/src/trust_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ impl TryFrom<Fraction> for TrustThreshold {
}
}

impl Default for TrustThreshold {
fn default() -> Self {
Self::ONE_THIRD
}
}
// impl Default for TrustThreshold {
// fn default() -> Self {
// Self::ONE_THIRD
// }
// }

impl Display for TrustThreshold {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
Expand Down
22 changes: 8 additions & 14 deletions ibc-core/ics03-connection/types/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,6 @@
}
}

impl Default for ConnectionEnd {
fn default() -> Self {
Self {
state: State::Uninitialized,
client_id: Default::default(),
counterparty: Default::default(),
versions: Vec::new(),
delay_period: ZERO_DURATION,
}
}
}

impl Protobuf<RawConnectionEnd> for ConnectionEnd {}

impl TryFrom<RawConnectionEnd> for ConnectionEnd {
Expand All @@ -220,7 +208,13 @@
let state = value.state.try_into()?;

if state == State::Uninitialized {
return Ok(ConnectionEnd::default());
rnbguy marked this conversation as resolved.
Show resolved Hide resolved
return ConnectionEnd::new(
State::Uninitialized,
"07-tendermint-0".into(),
Counterparty::new("07-tendermint-0".into(), None, CommitmentPrefix::empty()),
Vec::new(),
ZERO_DURATION,
);

Check warning on line 217 in ibc-core/ics03-connection/types/src/connection.rs

View check run for this annotation

Codecov / codecov/patch

ibc-core/ics03-connection/types/src/connection.rs#L211-L217

Added lines #L211 - L217 were not covered by tests
}

if value.client_id.is_empty() {
Expand Down Expand Up @@ -378,7 +372,7 @@
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Counterparty {
pub client_id: ClientId,
pub connection_id: Option<ConnectionId>,
Expand Down
4 changes: 2 additions & 2 deletions ibc-core/ics03-connection/types/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub const COUNTERPARTY_CLIENT_ID_ATTRIBUTE_KEY: &str = "counterparty_client_id";
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Attributes {
pub connection_id: ConnectionId,
pub client_id: ClientId,
Expand Down Expand Up @@ -323,7 +323,7 @@ mod tests {

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 conn_id_on_a = ConnectionId::zero();
let client_id_on_a = client_type.build_client_id(0);
let conn_id_on_b = ConnectionId::new(1);
let client_id_on_b = client_type.build_client_id(1);
Expand Down
Loading
Loading