From d992abcdb2b2e6ecf74d7b41891fbfde1650c323 Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 16 Feb 2023 16:10:38 +0800 Subject: [PATCH 1/5] impl Convert from &IbcEvent to abci::Event --- crates/ibc/src/events.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/ibc/src/events.rs b/crates/ibc/src/events.rs index 59726c6ca..fd7ab9d5e 100644 --- a/crates/ibc/src/events.rs +++ b/crates/ibc/src/events.rs @@ -261,6 +261,14 @@ impl TryFrom for abci::Event { } } +impl TryFrom<&IbcEvent> for abci::Event { + type Error = Error; + + fn try_from(event: &IbcEvent) -> Result { + abci::Event::try_from(event.clone()) + } +} + impl IbcEvent { pub fn event_type(&self) -> IbcEventType { match self { @@ -391,6 +399,7 @@ pub mod tests { Order::Unordered, ConnectionId::default(), )); + let _ = abci::Event::try_from(&ibc_event); let _ = abci::Event::try_from(ibc_event); } } From aaf2f2a5ca821e8acc1e1370d584bf6c12be2975 Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 16 Feb 2023 16:12:43 +0800 Subject: [PATCH 2/5] Create 416-conver-ref-ibc_event2abci_event.md --- .../unreleased/usability/416-conver-ref-ibc_event2abci_event.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/usability/416-conver-ref-ibc_event2abci_event.md diff --git a/.changelog/unreleased/usability/416-conver-ref-ibc_event2abci_event.md b/.changelog/unreleased/usability/416-conver-ref-ibc_event2abci_event.md new file mode 100644 index 000000000..0ba73e52c --- /dev/null +++ b/.changelog/unreleased/usability/416-conver-ref-ibc_event2abci_event.md @@ -0,0 +1,2 @@ +- Allow conversion from &IbcEvent to abci::Event + ([#416](https://github.com/cosmos/ibc-rs/issues/416)) \ No newline at end of file From d695ab0c9ca48d6a693acc27ef5b85b9a3473f89 Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 2 Mar 2023 23:38:36 +0800 Subject: [PATCH 3/5] change TryFrom IbcEvent by IbcEvent reference --- crates/ibc/src/core/ics02_client/events.rs | 88 +++--- .../ibc/src/core/ics03_connection/events.rs | 51 ++-- crates/ibc/src/core/ics04_channel/events.rs | 250 +++++++++--------- .../events/channel_attributes.rs | 24 +- .../ics04_channel/events/packet_attributes.rs | 48 ++-- crates/ibc/src/events.rs | 31 +-- 6 files changed, 245 insertions(+), 247 deletions(-) diff --git a/crates/ibc/src/core/ics02_client/events.rs b/crates/ibc/src/core/ics02_client/events.rs index a3237b609..319632fce 100644 --- a/crates/ibc/src/core/ics02_client/events.rs +++ b/crates/ibc/src/core/ics02_client/events.rs @@ -43,8 +43,8 @@ struct ClientIdAttribute { client_id: ClientId, } -impl From for abci::EventAttribute { - fn from(attr: ClientIdAttribute) -> Self { +impl From<&ClientIdAttribute> for abci::EventAttribute { + fn from(attr: &ClientIdAttribute) -> Self { (CLIENT_ID_ATTRIBUTE_KEY, attr.client_id.as_str()).into() } } @@ -67,8 +67,8 @@ struct ClientTypeAttribute { client_type: ClientType, } -impl From for abci::EventAttribute { - fn from(attr: ClientTypeAttribute) -> Self { +impl From<&ClientTypeAttribute> for abci::EventAttribute { + fn from(attr: &ClientTypeAttribute) -> Self { (CLIENT_TYPE_ATTRIBUTE_KEY, attr.client_type.as_str()).into() } } @@ -91,8 +91,8 @@ struct ConsensusHeightAttribute { consensus_height: Height, } -impl From for abci::EventAttribute { - fn from(attr: ConsensusHeightAttribute) -> Self { +impl From<&ConsensusHeightAttribute> for abci::EventAttribute { + fn from(attr: &ConsensusHeightAttribute) -> Self { (CONSENSUS_HEIGHT_ATTRIBUTE_KEY, attr.consensus_height).into() } } @@ -115,11 +115,11 @@ struct ConsensusHeightsAttribute { consensus_heights: Vec, } -impl From for abci::EventAttribute { - fn from(attr: ConsensusHeightsAttribute) -> Self { +impl From<&ConsensusHeightsAttribute> for abci::EventAttribute { + fn from(attr: &ConsensusHeightsAttribute) -> Self { let consensus_heights: Vec = attr .consensus_heights - .into_iter() + .iter() .map(|consensus_height| consensus_height.to_string()) .collect(); (CONSENSUS_HEIGHTS_ATTRIBUTE_KEY, consensus_heights.join(",")).into() @@ -144,11 +144,11 @@ struct HeaderAttribute { header: Any, } -impl From for abci::EventAttribute { - fn from(attr: HeaderAttribute) -> Self { +impl From<&HeaderAttribute> for abci::EventAttribute { + fn from(attr: &HeaderAttribute) -> Self { ( HEADER_ATTRIBUTE_KEY, - String::from_utf8(hex::encode(attr.header.value)).unwrap(), + String::from_utf8(hex::encode(&attr.header.value)).unwrap(), ) .into() } @@ -197,14 +197,14 @@ impl CreateClient { } } -impl From for abci::Event { - fn from(c: CreateClient) -> Self { +impl From<&CreateClient> for abci::Event { + fn from(c: &CreateClient) -> Self { Self { kind: IbcEventType::CreateClient.as_str().to_owned(), attributes: vec![ - c.client_id.into(), - c.client_type.into(), - c.consensus_height.into(), + abci::EventAttribute::from(&c.client_id), + abci::EventAttribute::from(&c.client_type), + abci::EventAttribute::from(&c.consensus_height), ], } } @@ -273,16 +273,16 @@ impl UpdateClient { } } -impl From for abci::Event { - fn from(u: UpdateClient) -> Self { +impl From<&UpdateClient> for abci::Event { + fn from(u: &UpdateClient) -> Self { Self { kind: IbcEventType::UpdateClient.as_str().to_owned(), attributes: vec![ - u.client_id.into(), - u.client_type.into(), - u.consensus_height.into(), - u.consensus_heights.into(), - u.header.into(), + abci::EventAttribute::from(&u.client_id), + abci::EventAttribute::from(&u.client_type), + abci::EventAttribute::from(&u.consensus_height), + abci::EventAttribute::from(&u.consensus_heights), + abci::EventAttribute::from(&u.header), ], } } @@ -326,11 +326,14 @@ impl ClientMisbehaviour { } } -impl From for abci::Event { - fn from(c: ClientMisbehaviour) -> Self { +impl From<&ClientMisbehaviour> for abci::Event { + fn from(c: &ClientMisbehaviour) -> Self { Self { kind: IbcEventType::ClientMisbehaviour.as_str().to_owned(), - attributes: vec![c.client_id.into(), c.client_type.into()], + attributes: vec![ + abci::EventAttribute::from(&c.client_id), + abci::EventAttribute::from(&c.client_type), + ], } } } @@ -378,14 +381,14 @@ impl UpgradeClient { } } -impl From for abci::Event { - fn from(u: UpgradeClient) -> Self { +impl From<&UpgradeClient> for abci::Event { + fn from(u: &UpgradeClient) -> Self { Self { kind: IbcEventType::UpgradeClient.as_str().to_owned(), attributes: vec![ - u.client_id.into(), - u.client_type.into(), - u.consensus_height.into(), + abci::EventAttribute::from(&u.client_id), + abci::EventAttribute::from(&u.client_type), + abci::EventAttribute::from(&u.consensus_height), ], } } @@ -434,34 +437,39 @@ mod tests { let tests: Vec = vec![ Test { kind: IbcEventType::CreateClient, - event: CreateClient::new(client_id.clone(), client_type.clone(), consensus_height) - .into(), + event: AbciEvent::from(&CreateClient::new( + client_id.clone(), + client_type.clone(), + consensus_height, + )), expected_keys: expected_keys[0..3].to_vec(), expected_values: expected_values[0..3].to_vec(), }, Test { kind: IbcEventType::UpdateClient, - event: UpdateClient::new( + event: AbciEvent::from(&UpdateClient::new( client_id.clone(), client_type.clone(), consensus_height, consensus_heights, header, - ) - .into(), + )), expected_keys: expected_keys.clone(), expected_values: expected_values.clone(), }, Test { kind: IbcEventType::UpgradeClient, - event: UpgradeClient::new(client_id.clone(), client_type.clone(), consensus_height) - .into(), + event: AbciEvent::from(&UpgradeClient::new( + client_id.clone(), + client_type.clone(), + consensus_height, + )), expected_keys: expected_keys[0..3].to_vec(), expected_values: expected_values[0..3].to_vec(), }, Test { kind: IbcEventType::ClientMisbehaviour, - event: ClientMisbehaviour::new(client_id, client_type).into(), + event: AbciEvent::from(&ClientMisbehaviour::new(client_id, client_type)), expected_keys: expected_keys[0..2].to_vec(), expected_values: expected_values[0..2].to_vec(), }, diff --git a/crates/ibc/src/core/ics03_connection/events.rs b/crates/ibc/src/core/ics03_connection/events.rs index d25558e43..682deff8d 100644 --- a/crates/ibc/src/core/ics03_connection/events.rs +++ b/crates/ibc/src/core/ics03_connection/events.rs @@ -34,8 +34,8 @@ struct Attributes { } /// Convert attributes to Tendermint ABCI tags -impl From for Vec { - fn from(a: Attributes) -> Self { +impl From<&Attributes> for Vec { + fn from(a: &Attributes) -> Self { let conn_id = (CONN_ID_ATTRIBUTE_KEY, a.connection_id.as_str()).into(); let client_id = (CLIENT_ID_ATTRIBUTE_KEY, a.client_id.as_str()).into(); @@ -108,11 +108,11 @@ impl OpenInit { } } -impl From for abci::Event { - fn from(v: OpenInit) -> Self { +impl From<&OpenInit> for abci::Event { + fn from(v: &OpenInit) -> Self { abci::Event { kind: IbcEventType::OpenInitConnection.as_str().to_owned(), - attributes: v.0.into(), + attributes: Vec::::from(&v.0), } } } @@ -163,11 +163,11 @@ impl OpenTry { } } -impl From for abci::Event { - fn from(v: OpenTry) -> Self { +impl From<&OpenTry> for abci::Event { + fn from(v: &OpenTry) -> Self { abci::Event { kind: IbcEventType::OpenTryConnection.as_str().to_owned(), - attributes: v.0.into(), + attributes: Vec::::from(&v.0), } } } @@ -218,11 +218,11 @@ impl OpenAck { } } -impl From for abci::Event { - fn from(v: OpenAck) -> Self { +impl From<&OpenAck> for abci::Event { + fn from(v: &OpenAck) -> Self { abci::Event { kind: IbcEventType::OpenAckConnection.as_str().to_owned(), - attributes: v.0.into(), + attributes: Vec::::from(&v.0), } } } @@ -273,11 +273,11 @@ impl OpenConfirm { } } -impl From for abci::Event { - fn from(v: OpenConfirm) -> Self { +impl From<&OpenConfirm> for abci::Event { + fn from(v: &OpenConfirm) -> Self { abci::Event { kind: IbcEventType::OpenConfirmConnection.as_str().to_owned(), - attributes: v.0.into(), + attributes: Vec::::from(&v.0), } } } @@ -318,12 +318,11 @@ mod tests { let tests: Vec = vec![ Test { kind: IbcEventType::OpenInitConnection, - event: OpenInit::new( + event: AbciEvent::from(&OpenInit::new( conn_id_on_a.clone(), client_id_on_a.clone(), client_id_on_b.clone(), - ) - .into(), + )), expected_keys: expected_keys.clone(), expected_values: expected_values .iter() @@ -333,32 +332,34 @@ mod tests { }, Test { kind: IbcEventType::OpenTryConnection, - event: OpenTry::new( + event: AbciEvent::from(&OpenTry::new( conn_id_on_b.clone(), client_id_on_b.clone(), conn_id_on_a.clone(), client_id_on_a.clone(), - ) - .into(), + )), expected_keys: expected_keys.clone(), expected_values: expected_values.iter().rev().cloned().collect(), }, Test { kind: IbcEventType::OpenAckConnection, - event: OpenAck::new( + event: AbciEvent::from(&OpenAck::new( conn_id_on_a.clone(), client_id_on_a.clone(), conn_id_on_b.clone(), client_id_on_b.clone(), - ) - .into(), + )), expected_keys: expected_keys.clone(), expected_values: expected_values.clone(), }, Test { kind: IbcEventType::OpenConfirmConnection, - event: OpenConfirm::new(conn_id_on_b, client_id_on_b, conn_id_on_a, client_id_on_a) - .into(), + event: AbciEvent::from(&OpenConfirm::new( + conn_id_on_b, + client_id_on_b, + conn_id_on_a, + client_id_on_a, + )), expected_keys: expected_keys.clone(), expected_values: expected_values.iter().rev().cloned().collect(), }, diff --git a/crates/ibc/src/core/ics04_channel/events.rs b/crates/ibc/src/core/ics04_channel/events.rs index 77e531836..4b7fe70df 100644 --- a/crates/ibc/src/core/ics04_channel/events.rs +++ b/crates/ibc/src/core/ics04_channel/events.rs @@ -84,17 +84,17 @@ impl OpenInit { } } -impl From for abci::Event { - fn from(o: OpenInit) -> Self { +impl From<&OpenInit> for abci::Event { + fn from(o: &OpenInit) -> Self { abci::Event { kind: IbcEventType::OpenInitChannel.as_str().to_owned(), attributes: vec![ - o.port_id.into(), - o.channel_id.into(), - o.counterparty_port_id.into(), + abci::EventAttribute::from(&o.port_id), + abci::EventAttribute::from(&o.channel_id), + abci::EventAttribute::from(&o.counterparty_port_id), (COUNTERPARTY_CHANNEL_ID_ATTRIBUTE_KEY, "").into(), - o.connection_id.into(), - o.version.into(), + abci::EventAttribute::from(&o.connection_id), + abci::EventAttribute::from(&o.version), ], } } @@ -161,17 +161,17 @@ impl OpenTry { } } -impl From for abci::Event { - fn from(o: OpenTry) -> Self { +impl From<&OpenTry> for abci::Event { + fn from(o: &OpenTry) -> Self { abci::Event { kind: IbcEventType::OpenTryChannel.as_str().to_owned(), attributes: vec![ - o.port_id.into(), - o.channel_id.into(), - o.counterparty_port_id.into(), - o.counterparty_channel_id.into(), - o.connection_id.into(), - o.version.into(), + abci::EventAttribute::from(&o.port_id), + abci::EventAttribute::from(&o.channel_id), + abci::EventAttribute::from(&o.counterparty_port_id), + abci::EventAttribute::from(&o.counterparty_channel_id), + abci::EventAttribute::from(&o.connection_id), + abci::EventAttribute::from(&o.version), ], } } @@ -232,16 +232,16 @@ impl OpenAck { } } -impl From for abci::Event { - fn from(o: OpenAck) -> Self { +impl From<&OpenAck> for abci::Event { + fn from(o: &OpenAck) -> Self { abci::Event { kind: IbcEventType::OpenAckChannel.as_str().to_owned(), attributes: vec![ - o.port_id.into(), - o.channel_id.into(), - o.counterparty_port_id.into(), - o.counterparty_channel_id.into(), - o.connection_id.into(), + abci::EventAttribute::from(&o.port_id), + abci::EventAttribute::from(&o.channel_id), + abci::EventAttribute::from(&o.counterparty_port_id), + abci::EventAttribute::from(&o.counterparty_channel_id), + abci::EventAttribute::from(&o.connection_id), ], } } @@ -302,16 +302,16 @@ impl OpenConfirm { } } -impl From for abci::Event { - fn from(o: OpenConfirm) -> Self { +impl From<&OpenConfirm> for abci::Event { + fn from(o: &OpenConfirm) -> Self { abci::Event { kind: IbcEventType::OpenConfirmChannel.as_str().to_owned(), attributes: vec![ - o.port_id.into(), - o.channel_id.into(), - o.counterparty_port_id.into(), - o.counterparty_channel_id.into(), - o.connection_id.into(), + abci::EventAttribute::from(&o.port_id), + abci::EventAttribute::from(&o.channel_id), + abci::EventAttribute::from(&o.counterparty_port_id), + abci::EventAttribute::from(&o.counterparty_channel_id), + abci::EventAttribute::from(&o.connection_id), ], } } @@ -372,16 +372,16 @@ impl CloseInit { } } -impl From for abci::Event { - fn from(o: CloseInit) -> Self { +impl From<&CloseInit> for abci::Event { + fn from(o: &CloseInit) -> Self { abci::Event { kind: IbcEventType::CloseInitChannel.as_str().to_owned(), attributes: vec![ - o.port_id.into(), - o.channel_id.into(), - o.counterparty_port_id.into(), - o.counterparty_channel_id.into(), - o.connection_id.into(), + abci::EventAttribute::from(&o.port_id), + abci::EventAttribute::from(&o.channel_id), + abci::EventAttribute::from(&o.counterparty_port_id), + abci::EventAttribute::from(&o.counterparty_channel_id), + abci::EventAttribute::from(&o.connection_id), ], } } @@ -442,16 +442,16 @@ impl CloseConfirm { } } -impl From for abci::Event { - fn from(o: CloseConfirm) -> Self { +impl From<&CloseConfirm> for abci::Event { + fn from(o: &CloseConfirm) -> Self { abci::Event { kind: IbcEventType::CloseConfirmChannel.as_str().to_owned(), attributes: vec![ - o.port_id.into(), - o.channel_id.into(), - o.counterparty_port_id.into(), - o.counterparty_channel_id.into(), - o.connection_id.into(), + abci::EventAttribute::from(&o.port_id), + abci::EventAttribute::from(&o.channel_id), + abci::EventAttribute::from(&o.counterparty_port_id), + abci::EventAttribute::from(&o.counterparty_channel_id), + abci::EventAttribute::from(&o.connection_id), ], } } @@ -524,20 +524,20 @@ impl ChannelClosed { } } -impl From for abci::Event { - fn from(ev: ChannelClosed) -> Self { +impl From<&ChannelClosed> for abci::Event { + fn from(ev: &ChannelClosed) -> Self { abci::Event { kind: IbcEventType::ChannelClosed.as_str().to_owned(), attributes: vec![ - ev.port_id.into(), - ev.channel_id.into(), - ev.counterparty_port_id.into(), - ev.maybe_counterparty_channel_id.map_or_else( + abci::EventAttribute::from(&ev.port_id), + abci::EventAttribute::from(&ev.channel_id), + abci::EventAttribute::from(&ev.counterparty_port_id), + ev.maybe_counterparty_channel_id.as_ref().map_or_else( || (COUNTERPARTY_CHANNEL_ID_ATTRIBUTE_KEY, "").into(), - |c| c.into(), + |c| abci::EventAttribute::from(c), ), - ev.connection_id.into(), - ev.channel_ordering.into(), + abci::EventAttribute::from(&ev.connection_id), + abci::EventAttribute::from(&ev.channel_ordering), ], } } @@ -627,21 +627,21 @@ impl SendPacket { } } -impl TryFrom for abci::Event { +impl TryFrom<&SendPacket> for abci::Event { type Error = ChannelError; - fn try_from(v: SendPacket) -> Result { + fn try_from(v: &SendPacket) -> Result { let mut attributes = Vec::with_capacity(11); - attributes.append(&mut v.packet_data.try_into()?); - attributes.push(v.timeout_height.into()); - attributes.push(v.timeout_timestamp.into()); - attributes.push(v.sequence.into()); - attributes.push(v.src_port_id.into()); - attributes.push(v.src_channel_id.into()); - attributes.push(v.dst_port_id.into()); - attributes.push(v.dst_channel_id.into()); - attributes.push(v.channel_ordering.into()); - attributes.push(v.src_connection_id.into()); + attributes.append(&mut Vec::::try_from(&v.packet_data)?); + attributes.push(abci::EventAttribute::from(&v.timeout_height)); + attributes.push(abci::EventAttribute::from(&v.timeout_timestamp)); + attributes.push(abci::EventAttribute::from(&v.sequence)); + attributes.push(abci::EventAttribute::from(&v.src_port_id)); + attributes.push(abci::EventAttribute::from(&v.src_channel_id)); + attributes.push(abci::EventAttribute::from(&v.dst_port_id)); + attributes.push(abci::EventAttribute::from(&v.dst_channel_id)); + attributes.push(abci::EventAttribute::from(&v.channel_ordering)); + attributes.push(abci::EventAttribute::from(&v.src_connection_id)); Ok(abci::Event { kind: IbcEventType::SendPacket.as_str().to_owned(), @@ -734,21 +734,21 @@ impl ReceivePacket { } } -impl TryFrom for abci::Event { +impl TryFrom<&ReceivePacket> for abci::Event { type Error = ChannelError; - fn try_from(v: ReceivePacket) -> Result { + fn try_from(v: &ReceivePacket) -> Result { let mut attributes = Vec::with_capacity(11); - attributes.append(&mut v.packet_data.try_into()?); - attributes.push(v.timeout_height.into()); - attributes.push(v.timeout_timestamp.into()); - attributes.push(v.sequence.into()); - attributes.push(v.src_port_id.into()); - attributes.push(v.src_channel_id.into()); - attributes.push(v.dst_port_id.into()); - attributes.push(v.dst_channel_id.into()); - attributes.push(v.channel_ordering.into()); - attributes.push(v.dst_connection_id.into()); + attributes.append(&mut Vec::::try_from(&v.packet_data)?); + attributes.push(abci::EventAttribute::from(&v.timeout_height)); + attributes.push(abci::EventAttribute::from(&v.timeout_timestamp)); + attributes.push(abci::EventAttribute::from(&v.sequence)); + attributes.push(abci::EventAttribute::from(&v.src_port_id)); + attributes.push(abci::EventAttribute::from(&v.src_channel_id)); + attributes.push(abci::EventAttribute::from(&v.dst_port_id)); + attributes.push(abci::EventAttribute::from(&v.dst_channel_id)); + attributes.push(abci::EventAttribute::from(&v.channel_ordering)); + attributes.push(abci::EventAttribute::from(&v.dst_connection_id)); Ok(abci::Event { kind: IbcEventType::ReceivePacket.as_str().to_owned(), @@ -845,21 +845,23 @@ impl WriteAcknowledgement { } } -impl TryFrom for abci::Event { +impl TryFrom<&WriteAcknowledgement> for abci::Event { type Error = ChannelError; - fn try_from(v: WriteAcknowledgement) -> Result { + fn try_from(v: &WriteAcknowledgement) -> Result { let mut attributes = Vec::with_capacity(11); - attributes.append(&mut v.packet_data.try_into()?); - attributes.push(v.timeout_height.into()); - attributes.push(v.timeout_timestamp.into()); - attributes.push(v.sequence.into()); - attributes.push(v.src_port_id.into()); - attributes.push(v.src_channel_id.into()); - attributes.push(v.dst_port_id.into()); - attributes.push(v.dst_channel_id.into()); - attributes.append(&mut v.acknowledgement.try_into()?); - attributes.push(v.dst_connection_id.into()); + attributes.append(&mut Vec::::try_from(&v.packet_data)?); + attributes.push(abci::EventAttribute::from(&v.timeout_height)); + attributes.push(abci::EventAttribute::from(&v.timeout_timestamp)); + attributes.push(abci::EventAttribute::from(&v.sequence)); + attributes.push(abci::EventAttribute::from(&v.src_port_id)); + attributes.push(abci::EventAttribute::from(&v.src_channel_id)); + attributes.push(abci::EventAttribute::from(&v.dst_port_id)); + attributes.push(abci::EventAttribute::from(&v.dst_channel_id)); + attributes.append(&mut Vec::::try_from( + &v.acknowledgement, + )?); + attributes.push(abci::EventAttribute::from(&v.dst_connection_id)); Ok(abci::Event { kind: IbcEventType::WriteAck.as_str().to_owned(), @@ -946,22 +948,22 @@ impl AcknowledgePacket { } } -impl TryFrom for abci::Event { +impl TryFrom<&AcknowledgePacket> for abci::Event { type Error = ChannelError; - fn try_from(v: AcknowledgePacket) -> Result { + fn try_from(v: &AcknowledgePacket) -> Result { Ok(abci::Event { kind: IbcEventType::AckPacket.as_str().to_owned(), attributes: vec![ - v.timeout_height.into(), - v.timeout_timestamp.into(), - v.sequence.into(), - v.src_port_id.into(), - v.src_channel_id.into(), - v.dst_port_id.into(), - v.dst_channel_id.into(), - v.channel_ordering.into(), - v.src_connection_id.into(), + abci::EventAttribute::from(&v.timeout_height), + abci::EventAttribute::from(&v.timeout_timestamp), + abci::EventAttribute::from(&v.sequence), + abci::EventAttribute::from(&v.src_port_id), + abci::EventAttribute::from(&v.src_channel_id), + abci::EventAttribute::from(&v.dst_port_id), + abci::EventAttribute::from(&v.dst_channel_id), + abci::EventAttribute::from(&v.channel_ordering), + abci::EventAttribute::from(&v.src_connection_id), ], }) } @@ -1039,21 +1041,21 @@ impl TimeoutPacket { } } -impl TryFrom for abci::Event { +impl TryFrom<&TimeoutPacket> for abci::Event { type Error = ChannelError; - fn try_from(v: TimeoutPacket) -> Result { + fn try_from(v: &TimeoutPacket) -> Result { Ok(abci::Event { kind: IbcEventType::Timeout.as_str().to_owned(), attributes: vec![ - v.timeout_height.into(), - v.timeout_timestamp.into(), - v.sequence.into(), - v.src_port_id.into(), - v.src_channel_id.into(), - v.dst_port_id.into(), - v.dst_channel_id.into(), - v.channel_ordering.into(), + abci::EventAttribute::from(&v.timeout_height), + abci::EventAttribute::from(&v.timeout_timestamp), + abci::EventAttribute::from(&v.sequence), + abci::EventAttribute::from(&v.src_port_id), + abci::EventAttribute::from(&v.src_channel_id), + abci::EventAttribute::from(&v.dst_port_id), + abci::EventAttribute::from(&v.dst_channel_id), + abci::EventAttribute::from(&v.channel_ordering), ], }) } @@ -1099,14 +1101,13 @@ mod tests { let tests: Vec = vec![ Test { kind: IbcEventType::OpenInitChannel, - event: OpenInit::new( + event: AbciEvent::from(&OpenInit::new( port_id.clone(), channel_id.clone(), counterparty_port_id.clone(), connection_id.clone(), version.clone(), - ) - .into(), + )), expected_keys: expected_keys.clone(), expected_values: expected_values .iter() @@ -1116,67 +1117,62 @@ mod tests { }, Test { kind: IbcEventType::OpenTryChannel, - event: OpenTry::new( + event: AbciEvent::from(&OpenTry::new( port_id.clone(), channel_id.clone(), counterparty_port_id.clone(), counterparty_channel_id.clone(), connection_id.clone(), version, - ) - .into(), + )), expected_keys: expected_keys.clone(), expected_values: expected_values.clone(), }, Test { kind: IbcEventType::OpenAckChannel, - event: OpenAck::new( + event: AbciEvent::from(&OpenAck::new( port_id.clone(), channel_id.clone(), counterparty_port_id.clone(), counterparty_channel_id.clone(), connection_id.clone(), - ) - .into(), + )), expected_keys: expected_keys[0..5].to_vec(), expected_values: expected_values[0..5].to_vec(), }, Test { kind: IbcEventType::OpenConfirmChannel, - event: OpenConfirm::new( + event: AbciEvent::from(&OpenConfirm::new( port_id.clone(), channel_id.clone(), counterparty_port_id.clone(), counterparty_channel_id.clone(), connection_id.clone(), - ) - .into(), + )), expected_keys: expected_keys[0..5].to_vec(), expected_values: expected_values[0..5].to_vec(), }, Test { kind: IbcEventType::CloseInitChannel, - event: CloseInit::new( + event: AbciEvent::from(&CloseInit::new( port_id.clone(), channel_id.clone(), counterparty_port_id.clone(), counterparty_channel_id.clone(), connection_id.clone(), - ) - .into(), + )), expected_keys: expected_keys[0..5].to_vec(), expected_values: expected_values[0..5].to_vec(), }, Test { kind: IbcEventType::CloseConfirmChannel, - event: CloseConfirm::new( + event: AbciEvent::from(&CloseConfirm::new( port_id, channel_id, counterparty_port_id, counterparty_channel_id, connection_id, - ) - .into(), + )), expected_keys: expected_keys[0..5].to_vec(), expected_values: expected_values[0..5].to_vec(), }, diff --git a/crates/ibc/src/core/ics04_channel/events/channel_attributes.rs b/crates/ibc/src/core/ics04_channel/events/channel_attributes.rs index e858527bb..95d610b84 100644 --- a/crates/ibc/src/core/ics04_channel/events/channel_attributes.rs +++ b/crates/ibc/src/core/ics04_channel/events/channel_attributes.rs @@ -35,8 +35,8 @@ pub struct PortIdAttribute { pub port_id: PortId, } -impl From for abci::EventAttribute { - fn from(attr: PortIdAttribute) -> Self { +impl From<&PortIdAttribute> for abci::EventAttribute { + fn from(attr: &PortIdAttribute) -> Self { (PORT_ID_ATTRIBUTE_KEY, attr.port_id.as_str()).into() } } @@ -59,8 +59,8 @@ pub struct ChannelIdAttribute { pub channel_id: ChannelId, } -impl From for abci::EventAttribute { - fn from(attr: ChannelIdAttribute) -> Self { +impl From<&ChannelIdAttribute> for abci::EventAttribute { + fn from(attr: &ChannelIdAttribute) -> Self { (CHANNEL_ID_ATTRIBUTE_KEY, attr.channel_id.as_str()).into() } } @@ -82,8 +82,8 @@ pub struct CounterpartyPortIdAttribute { pub counterparty_port_id: PortId, } -impl From for abci::EventAttribute { - fn from(attr: CounterpartyPortIdAttribute) -> Self { +impl From<&CounterpartyPortIdAttribute> for abci::EventAttribute { + fn from(attr: &CounterpartyPortIdAttribute) -> Self { ( COUNTERPARTY_PORT_ID_ATTRIBUTE_KEY, attr.counterparty_port_id.as_str(), @@ -109,8 +109,8 @@ pub struct CounterpartyChannelIdAttribute { pub counterparty_channel_id: ChannelId, } -impl From for abci::EventAttribute { - fn from(attr: CounterpartyChannelIdAttribute) -> Self { +impl From<&CounterpartyChannelIdAttribute> for abci::EventAttribute { + fn from(attr: &CounterpartyChannelIdAttribute) -> Self { ( COUNTERPARTY_CHANNEL_ID_ATTRIBUTE_KEY, attr.counterparty_channel_id.as_str(), @@ -143,8 +143,8 @@ pub struct ConnectionIdAttribute { pub connection_id: ConnectionId, } -impl From for abci::EventAttribute { - fn from(attr: ConnectionIdAttribute) -> Self { +impl From<&ConnectionIdAttribute> for abci::EventAttribute { + fn from(attr: &ConnectionIdAttribute) -> Self { (CONNECTION_ID_ATTRIBUTE_KEY, attr.connection_id.as_str()).into() } } @@ -167,8 +167,8 @@ pub struct VersionAttribute { pub version: Version, } -impl From for abci::EventAttribute { - fn from(attr: VersionAttribute) -> Self { +impl From<&VersionAttribute> for abci::EventAttribute { + fn from(attr: &VersionAttribute) -> Self { (VERSION_ATTRIBUTE_KEY, attr.version.as_str()).into() } } diff --git a/crates/ibc/src/core/ics04_channel/events/packet_attributes.rs b/crates/ibc/src/core/ics04_channel/events/packet_attributes.rs index 319d7aaee..89c833b15 100644 --- a/crates/ibc/src/core/ics04_channel/events/packet_attributes.rs +++ b/crates/ibc/src/core/ics04_channel/events/packet_attributes.rs @@ -50,10 +50,10 @@ pub struct PacketDataAttribute { pub packet_data: Vec, } -impl TryFrom for Vec { +impl TryFrom<&PacketDataAttribute> for Vec { type Error = ChannelError; - fn try_from(attr: PacketDataAttribute) -> Result { + fn try_from(attr: &PacketDataAttribute) -> Result { let tags = vec![ ( PKT_DATA_ATTRIBUTE_KEY, @@ -62,7 +62,7 @@ impl TryFrom for Vec { .into(), ( PKT_DATA_HEX_ATTRIBUTE_KEY, - String::from_utf8(hex::encode(attr.packet_data)).unwrap(), + String::from_utf8(hex::encode(&attr.packet_data)).unwrap(), ) .into(), ]; @@ -89,8 +89,8 @@ pub struct TimeoutHeightAttribute { pub timeout_height: TimeoutHeight, } -impl From for abci::EventAttribute { - fn from(attr: TimeoutHeightAttribute) -> Self { +impl From<&TimeoutHeightAttribute> for abci::EventAttribute { + fn from(attr: &TimeoutHeightAttribute) -> Self { match attr.timeout_height { TimeoutHeight::Never => (PKT_TIMEOUT_HEIGHT_ATTRIBUTE_KEY, "0-0").into(), TimeoutHeight::At(height) => { @@ -118,8 +118,8 @@ pub struct TimeoutTimestampAttribute { pub timeout_timestamp: Timestamp, } -impl From for abci::EventAttribute { - fn from(attr: TimeoutTimestampAttribute) -> Self { +impl From<&TimeoutTimestampAttribute> for abci::EventAttribute { + fn from(attr: &TimeoutTimestampAttribute) -> Self { ( PKT_TIMEOUT_TIMESTAMP_ATTRIBUTE_KEY, attr.timeout_timestamp.nanoseconds().to_string(), @@ -146,8 +146,8 @@ pub struct SequenceAttribute { pub sequence: Sequence, } -impl From for abci::EventAttribute { - fn from(attr: SequenceAttribute) -> Self { +impl From<&SequenceAttribute> for abci::EventAttribute { + fn from(attr: &SequenceAttribute) -> Self { (PKT_SEQ_ATTRIBUTE_KEY, attr.sequence.to_string()).into() } } @@ -170,8 +170,8 @@ pub struct SrcPortIdAttribute { pub src_port_id: PortId, } -impl From for abci::EventAttribute { - fn from(attr: SrcPortIdAttribute) -> Self { +impl From<&SrcPortIdAttribute> for abci::EventAttribute { + fn from(attr: &SrcPortIdAttribute) -> Self { (PKT_SRC_PORT_ATTRIBUTE_KEY, attr.src_port_id.as_str()).into() } } @@ -194,8 +194,8 @@ pub struct SrcChannelIdAttribute { pub src_channel_id: ChannelId, } -impl From for abci::EventAttribute { - fn from(attr: SrcChannelIdAttribute) -> Self { +impl From<&SrcChannelIdAttribute> for abci::EventAttribute { + fn from(attr: &SrcChannelIdAttribute) -> Self { (PKT_SRC_CHANNEL_ATTRIBUTE_KEY, attr.src_channel_id.as_str()).into() } } @@ -218,8 +218,8 @@ pub struct DstPortIdAttribute { pub dst_port_id: PortId, } -impl From for abci::EventAttribute { - fn from(attr: DstPortIdAttribute) -> Self { +impl From<&DstPortIdAttribute> for abci::EventAttribute { + fn from(attr: &DstPortIdAttribute) -> Self { (PKT_DST_PORT_ATTRIBUTE_KEY, attr.dst_port_id.as_str()).into() } } @@ -242,8 +242,8 @@ pub struct DstChannelIdAttribute { pub dst_channel_id: ChannelId, } -impl From for abci::EventAttribute { - fn from(attr: DstChannelIdAttribute) -> Self { +impl From<&DstChannelIdAttribute> for abci::EventAttribute { + fn from(attr: &DstChannelIdAttribute) -> Self { (PKT_DST_CHANNEL_ATTRIBUTE_KEY, attr.dst_channel_id.as_str()).into() } } @@ -266,8 +266,8 @@ pub struct ChannelOrderingAttribute { pub order: Order, } -impl From for abci::EventAttribute { - fn from(attr: ChannelOrderingAttribute) -> Self { +impl From<&ChannelOrderingAttribute> for abci::EventAttribute { + fn from(attr: &ChannelOrderingAttribute) -> Self { (PKT_CHANNEL_ORDERING_ATTRIBUTE_KEY, attr.order.as_str()).into() } } @@ -290,8 +290,8 @@ pub struct PacketConnectionIdAttribute { pub connection_id: ConnectionId, } -impl From for abci::EventAttribute { - fn from(attr: PacketConnectionIdAttribute) -> Self { +impl From<&PacketConnectionIdAttribute> for abci::EventAttribute { + fn from(attr: &PacketConnectionIdAttribute) -> Self { (PKT_CONNECTION_ID_ATTRIBUTE_KEY, attr.connection_id.as_str()).into() } } @@ -314,10 +314,10 @@ pub struct AcknowledgementAttribute { pub acknowledgement: Acknowledgement, } -impl TryFrom for Vec { +impl TryFrom<&AcknowledgementAttribute> for Vec { type Error = ChannelError; - fn try_from(attr: AcknowledgementAttribute) -> Result { + fn try_from(attr: &AcknowledgementAttribute) -> Result { let tags = vec![ ( PKT_ACK_ATTRIBUTE_KEY, @@ -331,7 +331,7 @@ impl TryFrom for Vec { .into(), ( PKT_ACK_HEX_ATTRIBUTE_KEY, - String::from_utf8(hex::encode(attr.acknowledgement)).unwrap(), + String::from_utf8(hex::encode(&attr.acknowledgement)).unwrap(), ) .into(), ]; diff --git a/crates/ibc/src/events.rs b/crates/ibc/src/events.rs index fd7ab9d5e..677ae2aa7 100644 --- a/crates/ibc/src/events.rs +++ b/crates/ibc/src/events.rs @@ -231,10 +231,10 @@ pub enum IbcEvent { AppModule(ModuleEvent), } -impl TryFrom for abci::Event { +impl TryFrom<&IbcEvent> for abci::Event { type Error = Error; - fn try_from(event: IbcEvent) -> Result { + fn try_from(event: &IbcEvent) -> Result { Ok(match event { IbcEvent::CreateClient(event) => event.into(), IbcEvent::UpdateClient(event) => event.into(), @@ -261,14 +261,6 @@ impl TryFrom for abci::Event { } } -impl TryFrom<&IbcEvent> for abci::Event { - type Error = Error; - - fn try_from(event: &IbcEvent) -> Result { - abci::Event::try_from(event.clone()) - } -} - impl IbcEvent { pub fn event_type(&self) -> IbcEventType { match self { @@ -317,17 +309,19 @@ pub struct ModuleEvent { pub attributes: Vec, } -impl TryFrom for abci::Event { +impl TryFrom<&ModuleEvent> for abci::Event { type Error = Error; - fn try_from(event: ModuleEvent) -> Result { + fn try_from(event: &ModuleEvent) -> Result { if IbcEventType::from_str(event.kind.as_str()).is_ok() { - return Err(Error::MalformedModuleEvent { event }); + return Err(Error::MalformedModuleEvent { + event: event.clone(), + }); } - let attributes = event.attributes.into_iter().map(Into::into).collect(); + let attributes = event.attributes.iter().map(Into::into).collect(); Ok(abci::Event { - kind: event.kind, + kind: event.kind.clone(), attributes, }) } @@ -367,9 +361,9 @@ impl From<(K, V)> for ModuleEventAttribute { } } -impl From for abci::EventAttribute { - fn from(attr: ModuleEventAttribute) -> Self { - (attr.key, attr.value).into() +impl From<&ModuleEventAttribute> for abci::EventAttribute { + fn from(attr: &ModuleEventAttribute) -> Self { + (attr.key.clone(), attr.value.clone()).into() } } @@ -400,6 +394,5 @@ pub mod tests { ConnectionId::default(), )); let _ = abci::Event::try_from(&ibc_event); - let _ = abci::Event::try_from(ibc_event); } } From 4315ca6ed3aed969cde9e33638e0e58c2937a60e Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 2 Mar 2023 23:41:02 +0800 Subject: [PATCH 4/5] Update events.rs --- crates/ibc/src/core/ics04_channel/events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ibc/src/core/ics04_channel/events.rs b/crates/ibc/src/core/ics04_channel/events.rs index 4b7fe70df..ab3b264a6 100644 --- a/crates/ibc/src/core/ics04_channel/events.rs +++ b/crates/ibc/src/core/ics04_channel/events.rs @@ -534,7 +534,7 @@ impl From<&ChannelClosed> for abci::Event { abci::EventAttribute::from(&ev.counterparty_port_id), ev.maybe_counterparty_channel_id.as_ref().map_or_else( || (COUNTERPARTY_CHANNEL_ID_ATTRIBUTE_KEY, "").into(), - |c| abci::EventAttribute::from(c), + abci::EventAttribute::from, ), abci::EventAttribute::from(&ev.connection_id), abci::EventAttribute::from(&ev.channel_ordering), From 7b3b2067053b09c5ae663e913cdba4aacfaf61fd Mon Sep 17 00:00:00 2001 From: Davirain Date: Fri, 3 Mar 2023 23:00:24 +0800 Subject: [PATCH 5/5] mvoe to breaking-changes fold --- .../416-conver-ref-ibc_event2abci_event.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/unreleased/{usability => breaking-changes}/416-conver-ref-ibc_event2abci_event.md (100%) diff --git a/.changelog/unreleased/usability/416-conver-ref-ibc_event2abci_event.md b/.changelog/unreleased/breaking-changes/416-conver-ref-ibc_event2abci_event.md similarity index 100% rename from .changelog/unreleased/usability/416-conver-ref-ibc_event2abci_event.md rename to .changelog/unreleased/breaking-changes/416-conver-ref-ibc_event2abci_event.md