From 0b9a4ae6b420d34623775b534f3197a2de024875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Laferri=C3=A8re?= Date: Tue, 11 Oct 2022 11:11:23 -0400 Subject: [PATCH] Make client events compatible with ibc-go v5.0.0 (#138) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * client_id IbcEvent refactor * Remove NewBlock and ChainError events * Refactor update_client event * clippy * Improve CreateClient event type * Improve UpdateClient event type * refactor ClientMisbehaviour event * UpgradeClient event refactor * Remove Client Attributes * Add EventMessage * client handlers emit ibcevent message * Copy relevant ADRs from hermes repository (#147) * Add ADRs * Update docs/architecture/adr-002-error.md Co-authored-by: Shoaib Ahmed Signed-off-by: Philippe Laferrière * Update docs/architecture/adr-003-ics20-implementation.md Co-authored-by: Shoaib Ahmed Signed-off-by: Philippe Laferrière * Update docs/architecture/adr-004-light-client-crates-extraction.md Co-authored-by: Shoaib Ahmed Signed-off-by: Philippe Laferrière * Update docs/architecture/adr-001-handler-implementation.md Co-authored-by: Shoaib Ahmed Signed-off-by: Philippe Laferrière * Update docs/architecture/adr-004-light-client-crates-extraction.md Co-authored-by: Shoaib Ahmed Signed-off-by: Philippe Laferrière Signed-off-by: Philippe Laferrière Co-authored-by: Shoaib Ahmed * Fix CI (#151) * std crate enabled for tests * clippy no-default-features: still use clock feature * patch ibc-proto to not use latest * fix ibc-proto patch properly * Allow deprecated (for crossing hellos fields) * Remove ibc-proto rev for no-std * fix no-std ibc-proto patch * Remove comment * fmt * Fix tests Events will be tested in their own test rather than spread across all tests. * ADR 012: Handlers validation and execution separation (#153) * init adr 12 * Host based API * Add appendices * Add section on event logging * Add .changelog entry * Fix TypedStore associated type (syntax only) * Update docs/architecture/adr-012-handlers-redesign.md Co-authored-by: Philippe Laferrière * validation vs execution clarifications * Move ValidationContext and ExecutionContext methods to appendix * Context clarification * Rework Host-based API section * ValidationContext and ExecutionContext clarification * Move Event logging section + misc * typo * Remove statement. Was mentioned earlier already. * IbcStore blanket implementation * IbcValueForPath rework * Remove IbcSerde from main ADR * TypedStore -> SubStore * diagram * Update docs/architecture/adr-012-handlers-redesign.md Co-authored-by: Sean Chen * Update docs/architecture/adr-012-handlers-redesign.md Co-authored-by: Sean Chen * Update docs/architecture/adr-012-handlers-redesign.md Co-authored-by: Sean Chen * add context for why the validation/execution split is necessary * add trait bounds * Rename ADR012 -> ADR005 * Apply suggestions from code review Change ADR status from Proposed to Accepted Co-authored-by: Philippe Laferrière Signed-off-by: Shoaib Ahmed Signed-off-by: Shoaib Ahmed Co-authored-by: Philippe Laferriere Co-authored-by: Sean Chen * Setup unclog based changelog (#152) * Add filtered changelog with ibc relevant entries * Init unclog * Add unreleased changelog entries * clippy * Remove "message" ibc event * update_client event test * Remove unused function * upgrade client event * changelog * Revert "Remove unused function" This reverts commit 66459cc9a8df01386643d9316110bd22937b4b80. * Remove EMPTY event * Use derive_more::From * Remove Display from IbcEvent * changelog Signed-off-by: Philippe Laferrière Signed-off-by: Shoaib Ahmed Co-authored-by: Shoaib Ahmed Co-authored-by: Sean Chen --- .../144-client-events-ibcgo-compatible.md | 2 + .../144-client-events-ibcgo-compatible.md | 2 + crates/ibc/src/core/ics02_client/events.rs | 326 +++++++++--------- .../ics02_client/handler/create_client.rs | 88 ++--- .../ics02_client/handler/update_client.rs | 90 ++--- .../ics02_client/handler/upgrade_client.rs | 51 ++- .../ibc/src/core/ics03_connection/events.rs | 36 -- crates/ibc/src/core/ics04_channel/events.rs | 118 ------- crates/ibc/src/core/ics26_routing/handler.rs | 4 +- crates/ibc/src/events.rs | 91 +---- 10 files changed, 294 insertions(+), 514 deletions(-) create mode 100644 .changelog/unreleased/breaking-changes/144-client-events-ibcgo-compatible.md create mode 100644 .changelog/unreleased/bug-fixes/144-client-events-ibcgo-compatible.md diff --git a/.changelog/unreleased/breaking-changes/144-client-events-ibcgo-compatible.md b/.changelog/unreleased/breaking-changes/144-client-events-ibcgo-compatible.md new file mode 100644 index 000000000..70a7e7965 --- /dev/null +++ b/.changelog/unreleased/breaking-changes/144-client-events-ibcgo-compatible.md @@ -0,0 +1,2 @@ +- Remove `Display` from `IbcEvent` ([#144](https://github.com/cosmos/ibc-rs/issues/144)). +- Remove `IbcEvent::Empty` ([#144](https://github.com/cosmos/ibc-rs/issues/144)). diff --git a/.changelog/unreleased/bug-fixes/144-client-events-ibcgo-compatible.md b/.changelog/unreleased/bug-fixes/144-client-events-ibcgo-compatible.md new file mode 100644 index 000000000..30c3fcedb --- /dev/null +++ b/.changelog/unreleased/bug-fixes/144-client-events-ibcgo-compatible.md @@ -0,0 +1,2 @@ +- Make client events compatible with ibc-go v5 + ([#144](https://github.com/cosmos/ibc-rs/issues/144)). diff --git a/crates/ibc/src/core/ics02_client/events.rs b/crates/ibc/src/core/ics02_client/events.rs index dd5e03531..4c18ed1b0 100644 --- a/crates/ibc/src/core/ics02_client/events.rs +++ b/crates/ibc/src/core/ics02_client/events.rs @@ -1,15 +1,15 @@ //! Types for the IBC events emitted from Tendermint Websocket by the client module. -use core::fmt::{Display, Error as FmtError, Formatter}; -use serde_derive::{Deserialize, Serialize}; +use derive_more::From; +use ibc_proto::google::protobuf::Any; +use subtle_encoding::hex; use tendermint::abci::tag::Tag; use tendermint::abci::Event as AbciEvent; -use super::header::Header; use crate::core::ics02_client::client_type::ClientType; use crate::core::ics02_client::height::Height; use crate::core::ics24_host::identifier::ClientId; -use crate::events::{IbcEvent, IbcEventType}; +use crate::events::IbcEventType; use crate::prelude::*; /// The content of the `key` field for the attribute containing the client identifier. @@ -21,262 +21,270 @@ pub const CLIENT_TYPE_ATTRIBUTE_KEY: &str = "client_type"; /// The content of the `key` field for the attribute containing the height. pub const CONSENSUS_HEIGHT_ATTRIBUTE_KEY: &str = "consensus_height"; +pub const CONSENSUS_HEIGHTS_ATTRIBUTE_KEY: &str = "consensus_heights"; + /// The content of the `key` field for the header in update client event. pub const HEADER_ATTRIBUTE_KEY: &str = "header"; -/// NewBlock event signals the committing & execution of a new block. -// TODO - find a better place for NewBlock -#[derive(Debug, Serialize, Clone, Copy, PartialEq, Eq)] -pub struct NewBlock { - pub height: Height, +#[derive(Debug, From)] +struct ClientIdAttribute { + client_id: ClientId, } -impl NewBlock { - pub fn new(h: Height) -> NewBlock { - NewBlock { height: h } - } - pub fn set_height(&mut self, height: Height) { - self.height = height; - } - pub fn height(&self) -> Height { - self.height +impl From for Tag { + fn from(attr: ClientIdAttribute) -> Self { + Tag { + key: CLIENT_ID_ATTRIBUTE_KEY.parse().unwrap(), + value: attr.client_id.to_string().parse().unwrap(), + } } } -impl Display for NewBlock { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "NewBlock {{ height: {} }}", self.height) - } +#[derive(Debug, From)] +struct ClientTypeAttribute { + client_type: ClientType, } -impl From for IbcEvent { - fn from(v: NewBlock) -> Self { - IbcEvent::NewBlock(v) +impl From for Tag { + fn from(attr: ClientTypeAttribute) -> Self { + Tag { + key: CLIENT_TYPE_ATTRIBUTE_KEY.parse().unwrap(), + value: attr.client_type.to_string().parse().unwrap(), + } } } -#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Attributes { - pub client_id: ClientId, - pub client_type: ClientType, - pub consensus_height: Height, +#[derive(Debug, From)] +struct ConsensusHeightAttribute { + consensus_height: Height, } -impl Default for Attributes { - fn default() -> Self { - Attributes { - client_id: Default::default(), - client_type: ClientType::Tendermint, - consensus_height: Height::new(0, 1).unwrap(), +impl From for Tag { + fn from(attr: ConsensusHeightAttribute) -> Self { + Tag { + key: CONSENSUS_HEIGHT_ATTRIBUTE_KEY.parse().unwrap(), + value: attr.consensus_height.to_string().parse().unwrap(), } } } -impl Display for Attributes { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!( - f, - "Attributes {{ client_id: {}, client_type: {}, consensus_height: {} }}", - self.client_id, self.client_type, self.consensus_height - ) +#[derive(Debug, From)] +struct ConsensusHeightsAttribute { + consensus_heights: Vec, +} + +impl From for Tag { + fn from(attr: ConsensusHeightsAttribute) -> Self { + let consensus_heights: Vec = attr + .consensus_heights + .into_iter() + .map(|consensus_height| consensus_height.to_string()) + .collect(); + Tag { + key: CONSENSUS_HEIGHTS_ATTRIBUTE_KEY.parse().unwrap(), + value: consensus_heights.join(",").parse().unwrap(), + } } } -/// Convert attributes to Tendermint ABCI tags -/// -/// # Note -/// The parsing of `Key`s and `Value`s never fails, because the -/// `FromStr` instance of `tendermint::abci::tag::{Key, Value}` -/// is infallible, even if it is not represented in the error type. -/// Once tendermint-rs improves the API of the `Key` and `Value` types, -/// we will be able to remove the `.parse().unwrap()` calls. -impl From for Vec { - fn from(attrs: Attributes) -> Self { - let client_id = Tag { - key: CLIENT_ID_ATTRIBUTE_KEY.parse().unwrap(), - value: attrs.client_id.to_string().parse().unwrap(), - }; - let client_type = Tag { - key: CLIENT_TYPE_ATTRIBUTE_KEY.parse().unwrap(), - value: attrs.client_type.as_str().parse().unwrap(), - }; - let consensus_height = Tag { - key: CONSENSUS_HEIGHT_ATTRIBUTE_KEY.parse().unwrap(), - value: attrs.consensus_height.to_string().parse().unwrap(), - }; - vec![client_id, client_type, consensus_height] +#[derive(Debug, From)] +struct HeaderAttribute { + header: Any, +} + +impl From for Tag { + fn from(attr: HeaderAttribute) -> Self { + Tag { + key: HEADER_ATTRIBUTE_KEY.parse().unwrap(), + value: String::from_utf8(hex::encode(attr.header.value)) + .unwrap() + .parse() + .unwrap(), + } } } /// CreateClient event signals the creation of a new on-chain client (IBC client). -#[derive(Debug, Serialize, Clone, PartialEq, Eq)] -pub struct CreateClient(pub Attributes); +#[derive(Debug)] +pub struct CreateClient { + client_id: ClientIdAttribute, + client_type: ClientTypeAttribute, + consensus_height: ConsensusHeightAttribute, +} impl CreateClient { - pub fn client_id(&self) -> &ClientId { - &self.0.client_id + pub fn new(client_id: ClientId, client_type: ClientType, consensus_height: Height) -> Self { + Self { + client_id: ClientIdAttribute::from(client_id), + client_type: ClientTypeAttribute::from(client_type), + consensus_height: ConsensusHeightAttribute::from(consensus_height), + } } -} -impl Display for CreateClient { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "CreateClient {{ {} }}", self.0) + pub fn client_id(&self) -> &ClientId { + &self.client_id.client_id } -} -impl From for CreateClient { - fn from(attrs: Attributes) -> Self { - CreateClient(attrs) + pub fn client_type(&self) -> &ClientType { + &self.client_type.client_type } -} -impl From for IbcEvent { - fn from(v: CreateClient) -> Self { - IbcEvent::CreateClient(v) + pub fn consensus_height(&self) -> &Height { + &self.consensus_height.consensus_height } } impl From for AbciEvent { - fn from(v: CreateClient) -> Self { - let attributes = Vec::::from(v.0); + fn from(c: CreateClient) -> Self { AbciEvent { type_str: IbcEventType::CreateClient.as_str().to_string(), - attributes, + attributes: vec![ + c.client_id.into(), + c.client_type.into(), + c.consensus_height.into(), + ], } } } /// UpdateClient event signals a recent update of an on-chain client (IBC Client). -#[derive(Clone, Debug, PartialEq, Serialize)] +#[derive(Debug)] pub struct UpdateClient { - pub common: Attributes, - pub header: Option>, + client_id: ClientIdAttribute, + client_type: ClientTypeAttribute, + // Deprecated: consensus_height is deprecated and will be removed in a future release. + // Please use consensus_heights instead. + consensus_height: ConsensusHeightAttribute, + consensus_heights: ConsensusHeightsAttribute, + header: HeaderAttribute, } impl UpdateClient { - pub fn client_id(&self) -> &ClientId { - &self.common.client_id + pub fn new( + client_id: ClientId, + client_type: ClientType, + consensus_height: Height, + consensus_heights: Vec, + header: Any, + ) -> Self { + Self { + client_id: ClientIdAttribute::from(client_id), + client_type: ClientTypeAttribute::from(client_type), + consensus_height: ConsensusHeightAttribute::from(consensus_height), + consensus_heights: ConsensusHeightsAttribute::from(consensus_heights), + header: HeaderAttribute::from(header), + } } - pub fn client_type(&self) -> ClientType { - self.common.client_type + pub fn client_id(&self) -> &ClientId { + &self.client_id.client_id } - pub fn consensus_height(&self) -> Height { - self.common.consensus_height + pub fn client_type(&self) -> &ClientType { + &self.client_type.client_type } -} -impl Display for UpdateClient { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - // TODO Display: Check for a solution for Box - write!( - f, - "UpdateClient {{ common: {}, header: None }}", - self.common - ) + pub fn consensus_height(&self) -> &Height { + &self.consensus_height.consensus_height } -} -impl From for UpdateClient { - fn from(attrs: Attributes) -> Self { - UpdateClient { - common: attrs, - header: None, - } + pub fn consensus_heights(&self) -> &[Height] { + self.consensus_heights.consensus_heights.as_ref() } -} -impl From for IbcEvent { - fn from(v: UpdateClient) -> Self { - IbcEvent::UpdateClient(v) + pub fn header(&self) -> &Any { + &self.header.header } } impl From for AbciEvent { - fn from(v: UpdateClient) -> Self { - let mut attributes = Vec::::from(v.common); - if let Some(h) = v.header { - let header = Tag { - key: HEADER_ATTRIBUTE_KEY.parse().unwrap(), - value: h.encode_to_hex_string().parse().unwrap(), - }; - attributes.push(header); - } + fn from(u: UpdateClient) -> Self { AbciEvent { type_str: IbcEventType::UpdateClient.as_str().to_string(), - attributes, + attributes: vec![ + u.client_id.into(), + u.client_type.into(), + u.consensus_height.into(), + u.consensus_heights.into(), + u.header.into(), + ], } } } /// ClientMisbehaviour event signals the update of an on-chain client (IBC Client) with evidence of /// misbehaviour. -#[derive(Debug, Serialize, Clone, PartialEq, Eq)] -pub struct ClientMisbehaviour(pub Attributes); - -impl ClientMisbehaviour { - pub fn client_id(&self) -> &ClientId { - &self.0.client_id - } +#[derive(Debug)] +pub struct ClientMisbehaviour { + client_id: ClientIdAttribute, + client_type: ClientTypeAttribute, } -impl Display for ClientMisbehaviour { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "ClientMisbehaviour {{ {} }}", self.0) +impl ClientMisbehaviour { + pub fn new(client_id: ClientId, client_type: ClientType) -> Self { + Self { + client_id: ClientIdAttribute::from(client_id), + client_type: ClientTypeAttribute::from(client_type), + } } -} -impl From for ClientMisbehaviour { - fn from(attrs: Attributes) -> Self { - ClientMisbehaviour(attrs) + pub fn client_id(&self) -> &ClientId { + &self.client_id.client_id } -} -impl From for IbcEvent { - fn from(v: ClientMisbehaviour) -> Self { - IbcEvent::ClientMisbehaviour(v) + pub fn client_type(&self) -> &ClientType { + &self.client_type.client_type } } impl From for AbciEvent { - fn from(v: ClientMisbehaviour) -> Self { - let attributes = Vec::::from(v.0); + fn from(c: ClientMisbehaviour) -> Self { AbciEvent { type_str: IbcEventType::ClientMisbehaviour.as_str().to_string(), - attributes, + attributes: vec![c.client_id.into(), c.client_type.into()], } } } /// Signals a recent upgrade of an on-chain client (IBC Client). -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] -pub struct UpgradeClient(pub Attributes); +#[derive(Debug)] +pub struct UpgradeClient { + client_id: ClientIdAttribute, + client_type: ClientTypeAttribute, + consensus_height: ConsensusHeightAttribute, +} impl UpgradeClient { + pub fn new(client_id: ClientId, client_type: ClientType, consensus_height: Height) -> Self { + Self { + client_id: ClientIdAttribute::from(client_id), + client_type: ClientTypeAttribute::from(client_type), + consensus_height: ConsensusHeightAttribute::from(consensus_height), + } + } + pub fn client_id(&self) -> &ClientId { - &self.0.client_id + &self.client_id.client_id } -} -impl Display for UpgradeClient { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "UpgradeClient {{ {} }}", self.0) + pub fn client_type(&self) -> &ClientType { + &self.client_type.client_type } -} -impl From for UpgradeClient { - fn from(attrs: Attributes) -> Self { - UpgradeClient(attrs) + pub fn consensus_height(&self) -> &Height { + &self.consensus_height.consensus_height } } impl From for AbciEvent { - fn from(v: UpgradeClient) -> Self { - let attributes = Vec::::from(v.0); + fn from(u: UpgradeClient) -> Self { AbciEvent { type_str: IbcEventType::UpgradeClient.as_str().to_string(), - attributes, + attributes: vec![ + u.client_id.into(), + u.client_type.into(), + u.consensus_height.into(), + ], } } } diff --git a/crates/ibc/src/core/ics02_client/handler/create_client.rs b/crates/ibc/src/core/ics02_client/handler/create_client.rs index 6c7c73b96..d09e4455c 100644 --- a/crates/ibc/src/core/ics02_client/handler/create_client.rs +++ b/crates/ibc/src/core/ics02_client/handler/create_client.rs @@ -7,7 +7,7 @@ use crate::core::ics02_client::client_type::ClientType; use crate::core::ics02_client::consensus_state::ConsensusState; use crate::core::ics02_client::context::ClientReader; use crate::core::ics02_client::error::Error; -use crate::core::ics02_client::events::Attributes; +use crate::core::ics02_client::events::CreateClient; use crate::core::ics02_client::handler::ClientResult; use crate::core::ics02_client::height::Height; use crate::core::ics02_client::msgs::create_client::MsgCreateClient; @@ -50,10 +50,7 @@ pub fn process(ctx: &dyn ClientReader, msg: MsgCreateClient) -> HandlerResult HandlerResult { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); + Ok(HandlerOutput { result, .. }) => { let expected_client_id = ClientId::new(ClientType::Mock, 0).unwrap(); - assert!( - matches!(event, IbcEvent::CreateClient(ref e) if e.client_id() == &expected_client_id) - ); match result { ClientResult::Create(create_result) => { assert_eq!(create_result.client_type, ClientType::Mock); @@ -192,35 +186,26 @@ mod tests { let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone())); match output { - Ok(HandlerOutput { - result, mut events, .. - }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::CreateClient(ref e) if e.client_id() == &expected_client_id) - ); - match result { - ClientResult::Create(create_res) => { - assert_eq!( - create_res.client_type, - create_res.client_state.client_type() - ); - assert_eq!(create_res.client_id, expected_client_id); - assert_eq!( - create_res.client_state.as_ref().clone_into(), - msg.client_state - ); - assert_eq!( - create_res.consensus_state.as_ref().clone_into(), - msg.consensus_state - ); - } - _ => { - panic!("expected result of type ClientResult::CreateResult"); - } + Ok(HandlerOutput { result, .. }) => match result { + ClientResult::Create(create_res) => { + assert_eq!( + create_res.client_type, + create_res.client_state.client_type() + ); + assert_eq!(create_res.client_id, expected_client_id); + assert_eq!( + create_res.client_state.as_ref().clone_into(), + msg.client_state + ); + assert_eq!( + create_res.consensus_state.as_ref().clone_into(), + msg.consensus_state + ); } - } + _ => { + panic!("expected result of type ClientResult::CreateResult"); + } + }, Err(err) => { panic!("unexpected error: {}", err); } @@ -263,15 +248,8 @@ mod tests { let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone())); match output { - Ok(HandlerOutput { - result, mut events, .. - }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); + Ok(HandlerOutput { result, .. }) => { let expected_client_id = ClientId::new(ClientType::Tendermint, 0).unwrap(); - assert!( - matches!(event, IbcEvent::CreateClient(ref e) if e.client_id() == &expected_client_id) - ); match result { ClientResult::Create(create_res) => { assert_eq!(create_res.client_type, ClientType::Tendermint); diff --git a/crates/ibc/src/core/ics02_client/handler/update_client.rs b/crates/ibc/src/core/ics02_client/handler/update_client.rs index 7184b5202..69a0ce4c6 100644 --- a/crates/ibc/src/core/ics02_client/handler/update_client.rs +++ b/crates/ibc/src/core/ics02_client/handler/update_client.rs @@ -6,7 +6,7 @@ use crate::core::ics02_client::client_state::{ClientState, UpdatedState}; use crate::core::ics02_client::consensus_state::ConsensusState; use crate::core::ics02_client::context::ClientReader; use crate::core::ics02_client::error::Error; -use crate::core::ics02_client::events::Attributes; +use crate::core::ics02_client::events::UpdateClient; use crate::core::ics02_client::handler::ClientResult; use crate::core::ics02_client::height::Height; use crate::core::ics02_client::msgs::update_client::MsgUpdateClient; @@ -76,9 +76,12 @@ pub fn process( client_state, consensus_state, } = client_state - .check_header_and_update_state(ctx, client_id.clone(), header) + .check_header_and_update_state(ctx, client_id.clone(), header.clone()) .map_err(|e| Error::header_verification_failure(e.to_string()))?; + let client_type = client_state.client_type(); + let consensus_height = client_state.latest_height(); + let result = ClientResult::Update(Result { client_id: client_id.clone(), client_state, @@ -87,11 +90,13 @@ pub fn process( processed_height: ctx.host_height(), }); - let event_attributes = Attributes { + output.emit(IbcEvent::UpdateClient(UpdateClient::new( client_id, - ..Default::default() - }; - output.emit(IbcEvent::UpdateClient(event_attributes.into())); + client_type, + consensus_height, + vec![consensus_height], + header, + ))); Ok(output.with_result(result)) } @@ -99,6 +104,7 @@ pub fn process( #[cfg(test)] mod tests { use core::str::FromStr; + use ibc_proto::google::protobuf::Any; use test_log::test; use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TmConsensusState; @@ -117,10 +123,10 @@ mod tests { use crate::mock::context::MockContext; use crate::mock::header::MockHeader; use crate::mock::host::{HostBlock, HostType}; - use crate::prelude::*; use crate::test_utils::get_dummy_account_id; use crate::timestamp::Timestamp; use crate::Height; + use crate::{downcast, prelude::*}; #[test] fn test_update_client_ok() { @@ -137,19 +143,14 @@ mod tests { signer, }; - let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone())); + let output = dispatch(&ctx, ClientMsg::UpdateClient(msg)); match output { Ok(HandlerOutput { result, - mut events, + events: _, log, }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id) - ); assert!(log.is_empty()); // Check the result match result { @@ -224,14 +225,9 @@ mod tests { match output { Ok(HandlerOutput { result: _, - mut events, + events: _, log, }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id) - ); assert!(log.is_empty()); } Err(err) => { @@ -279,19 +275,14 @@ mod tests { signer, }; - let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone())); + let output = dispatch(&ctx, ClientMsg::UpdateClient(msg)); match output { Ok(HandlerOutput { result, - mut events, + events: _, log, }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id) - ); assert!(log.is_empty()); // Check the result match result { @@ -348,19 +339,14 @@ mod tests { signer, }; - let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone())); + let output = dispatch(&ctx, ClientMsg::UpdateClient(msg)); match output { Ok(HandlerOutput { result, - mut events, + events: _, log, }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id) - ); assert!(log.is_empty()); // Check the result match result { @@ -428,19 +414,14 @@ mod tests { signer, }; - let output = dispatch(&ctx, ClientMsg::UpdateClient(msg.clone())); + let output = dispatch(&ctx, ClientMsg::UpdateClient(msg)); match output { Ok(HandlerOutput { result, - mut events, + events: _, log, }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::UpdateClient(ref e) if e.client_id() == &msg.client_id) - ); assert!(log.is_empty()); // Check the result match result { @@ -510,4 +491,31 @@ mod tests { }, } } + + #[test] + fn test_update_client_events() { + let client_id = ClientId::default(); + let signer = get_dummy_account_id(); + + let timestamp = Timestamp::now(); + + let ctx = MockContext::default().with_client(&client_id, Height::new(0, 42).unwrap()); + let height = Height::new(0, 46).unwrap(); + let header: Any = MockHeader::new(height).with_timestamp(timestamp).into(); + let msg = MsgUpdateClient { + client_id: client_id.clone(), + header: header.clone(), + signer, + }; + + let output = dispatch(&ctx, ClientMsg::UpdateClient(msg)).unwrap(); + let update_client_event = + downcast!(output.events.first().unwrap() => IbcEvent::UpdateClient).unwrap(); + + assert_eq!(update_client_event.client_id(), &client_id); + assert_eq!(update_client_event.client_type(), &ClientType::Mock); + assert_eq!(update_client_event.consensus_height(), &height); + assert_eq!(update_client_event.consensus_heights(), &vec![height]); + assert_eq!(update_client_event.header(), &header); + } } diff --git a/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs b/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs index c6c0ab9ed..7aa60717d 100644 --- a/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs +++ b/crates/ibc/src/core/ics02_client/handler/upgrade_client.rs @@ -4,7 +4,7 @@ use crate::core::ics02_client::client_state::{ClientState, UpdatedState}; use crate::core::ics02_client::consensus_state::ConsensusState; use crate::core::ics02_client::context::ClientReader; use crate::core::ics02_client::error::Error; -use crate::core::ics02_client::events::Attributes; +use crate::core::ics02_client::events::UpgradeClient; use crate::core::ics02_client::handler::ClientResult; use crate::core::ics02_client::msgs::upgrade_client::MsgUpgradeClient; use crate::core::ics24_host::identifier::ClientId; @@ -56,23 +56,29 @@ pub fn process( // Not implemented yet: https://github.com/informalsystems/ibc-rs/issues/722 // todo!() + let client_type = client_state.client_type(); + let consensus_height = client_state.latest_height(); + let result = ClientResult::Upgrade(Result { client_id: client_id.clone(), client_state, consensus_state, }); - let event_attributes = Attributes { + + output.emit(IbcEvent::UpgradeClient(UpgradeClient::new( client_id, - ..Default::default() - }; + client_type, + consensus_height, + ))); - output.emit(IbcEvent::UpgradeClient(event_attributes.into())); Ok(output.with_result(result)) } #[cfg(test)] mod tests { - use crate::prelude::*; + use crate::core::ics02_client::client_type::ClientType; + use crate::events::IbcEvent; + use crate::{downcast, prelude::*}; use core::str::FromStr; @@ -82,7 +88,6 @@ mod tests { use crate::core::ics02_client::msgs::upgrade_client::MsgUpgradeClient; use crate::core::ics02_client::msgs::ClientMsg; use crate::core::ics24_host::identifier::ClientId; - use crate::events::IbcEvent; use crate::handler::HandlerOutput; use crate::mock::client_state::MockClientState; use crate::mock::consensus_state::MockConsensusState; @@ -113,14 +118,9 @@ mod tests { match output { Ok(HandlerOutput { result, - mut events, + events: _, log, }) => { - assert_eq!(events.len(), 1); - let event = events.pop().unwrap(); - assert!( - matches!(event, IbcEvent::UpgradeClient(ref e) if e.client_id() == &msg.client_id) - ); assert!(log.is_empty()); // Check the result match result { @@ -200,4 +200,29 @@ mod tests { } } } + + #[test] + fn test_upgrade_client_event() { + let client_id = ClientId::default(); + let signer = get_dummy_account_id(); + + let ctx = MockContext::default().with_client(&client_id, Height::new(0, 42).unwrap()); + + let upgrade_height = Height::new(1, 26).unwrap(); + let msg = MsgUpgradeClient { + client_id: client_id.clone(), + client_state: MockClientState::new(MockHeader::new(upgrade_height)).into(), + consensus_state: MockConsensusState::new(MockHeader::new(upgrade_height)).into(), + proof_upgrade_client: Default::default(), + proof_upgrade_consensus_state: Default::default(), + signer, + }; + + let output = dispatch(&ctx, ClientMsg::UpgradeClient(msg)).unwrap(); + let upgrade_client_event = + downcast!(output.events.first().unwrap() => IbcEvent::UpgradeClient).unwrap(); + assert_eq!(upgrade_client_event.client_id(), &client_id); + assert_eq!(upgrade_client_event.client_type(), &ClientType::Mock); + assert_eq!(upgrade_client_event.consensus_height(), &upgrade_height); + } } diff --git a/crates/ibc/src/core/ics03_connection/events.rs b/crates/ibc/src/core/ics03_connection/events.rs index d8ddede11..912b95bef 100644 --- a/crates/ibc/src/core/ics03_connection/events.rs +++ b/crates/ibc/src/core/ics03_connection/events.rs @@ -1,6 +1,5 @@ //! Types for the IBC events emitted from Tendermint Websocket by the connection module. -use core::fmt::{Display, Error as FmtError, Formatter}; use serde_derive::{Deserialize, Serialize}; use tendermint::abci::tag::Tag; use tendermint::abci::Event as AbciEvent; @@ -23,17 +22,6 @@ pub struct Attributes { pub counterparty_client_id: ClientId, } -impl Display for Attributes { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.connection_id, &self.counterparty_connection_id) { - (Some(connection_id), Some(counterparty_connection_id)) => write!(f, "Attributes {{ connection_id: {}, client_id: {}, counterparty_connection_id: {}, counterparty_client_id: {} }}", connection_id, self.client_id, counterparty_connection_id, self.counterparty_client_id), - (Some(connection_id), None) => write!(f, "Attributes {{ connection_id: {}, client_id: {}, counterparty_connection_id: None, counterparty_client_id: {} }}", connection_id, self.client_id, self.counterparty_client_id), - (None, Some(counterparty_connection_id)) => write!(f, "Attributes {{ connection_id: None, client_id: {}, counterparty_connection_id: {}, counterparty_client_id: {} }}", self.client_id, counterparty_connection_id, self.counterparty_client_id), - (None, None) => write!(f, "Attributes {{ connection_id: None, client_id: {}, counterparty_connection_id: None, counterparty_client_id: {} }}", self.client_id, self.counterparty_client_id), - } - } -} - /// Convert attributes to Tendermint ABCI tags /// /// # Note @@ -85,12 +73,6 @@ impl OpenInit { } } -impl Display for OpenInit { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "OpenInit {{ {} }}", self.0) - } -} - impl From for OpenInit { fn from(attrs: Attributes) -> Self { OpenInit(attrs) @@ -125,12 +107,6 @@ impl OpenTry { } } -impl Display for OpenTry { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "OpenTry {{ {} }}", self.0) - } -} - impl From for OpenTry { fn from(attrs: Attributes) -> Self { OpenTry(attrs) @@ -165,12 +141,6 @@ impl OpenAck { } } -impl Display for OpenAck { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "OpenAck {{ {} }}", self.0) - } -} - impl From for OpenAck { fn from(attrs: Attributes) -> Self { OpenAck(attrs) @@ -205,12 +175,6 @@ impl OpenConfirm { } } -impl Display for OpenConfirm { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "OpenConfirm {{ {} }}", self.0) - } -} - impl From for OpenConfirm { fn from(attrs: Attributes) -> Self { OpenConfirm(attrs) diff --git a/crates/ibc/src/core/ics04_channel/events.rs b/crates/ibc/src/core/ics04_channel/events.rs index 2632a09a7..1049b9ab3 100644 --- a/crates/ibc/src/core/ics04_channel/events.rs +++ b/crates/ibc/src/core/ics04_channel/events.rs @@ -1,6 +1,5 @@ //! Types for the IBC events emitted from Tendermint Websocket by the channels module. -use core::fmt::{Display, Error as FmtError, Formatter}; use serde_derive::{Deserialize, Serialize}; use tendermint::abci::tag::Tag; use tendermint::abci::Event as AbciEvent; @@ -10,7 +9,6 @@ use crate::core::ics04_channel::packet::Packet; use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId}; use crate::events::{Error as EventError, IbcEvent, IbcEventType}; use crate::prelude::*; -use crate::utils::pretty::PrettySlice; /// Channel event attribute keys pub const CONNECTION_ID_ATTRIBUTE_KEY: &str = "connection_id"; @@ -48,17 +46,6 @@ impl Attributes { } } -impl Display for Attributes { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.channel_id, &self.counterparty_channel_id) { - (Some(channel_id), Some(counterparty_channel_id)) => write!(f, "Attributes {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (Some(channel_id), None) => write!(f, "Attributes {{ port_id: {}, channel_id: {}, connection_id: None, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, channel_id, self.counterparty_port_id), - (None, Some(counterparty_channel_id)) => write!(f, "Attributes {{ port_id: {}, channel_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (None, None) => write!(f, "Attributes {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - /// Convert attributes to Tendermint ABCI tags /// /// # Note @@ -193,17 +180,6 @@ impl OpenInit { } } -impl Display for OpenInit { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.channel_id, &self.counterparty_channel_id) { - (Some(channel_id), Some(counterparty_channel_id)) => write!(f, "OpenInit {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (Some(channel_id), None) => write!(f, "OpenInit {{ port_id: {}, channel_id: {}, connection_id: None, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, channel_id, self.counterparty_port_id), - (None, Some(counterparty_channel_id)) => write!(f, "OpenInit {{ port_id: {}, channel_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (None, None) => write!(f, "OpenInit {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - impl From for Attributes { fn from(ev: OpenInit) -> Self { Self { @@ -237,17 +213,6 @@ pub struct OpenTry { pub counterparty_channel_id: Option, } -impl Display for OpenTry { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.channel_id, &self.counterparty_channel_id) { - (Some(channel_id), Some(counterparty_channel_id)) => write!(f, "OpenTry {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (Some(channel_id), None) => write!(f, "OpenTry {{ port_id: {}, channel_id: {}, connection_id: None, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, channel_id, self.counterparty_port_id), - (None, Some(counterparty_channel_id)) => write!(f, "OpenTry {{ port_id: {}, channel_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (None, None) => write!(f, "OpenTry {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - impl From for Attributes { fn from(ev: OpenTry) -> Self { Self { @@ -289,17 +254,6 @@ pub struct OpenAck { pub counterparty_port_id: PortId, } -impl Display for OpenAck { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.channel_id, &self.counterparty_channel_id) { - (Some(channel_id), Some(counterparty_channel_id)) => write!(f, "OpenAck {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (Some(channel_id), None) => write!(f, "OpenAck {{ port_id: {}, channel_id: {}, connection_id: None, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, channel_id, self.counterparty_port_id), - (None, Some(counterparty_channel_id)) => write!(f, "OpenAck {{ port_id: {}, channel_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (None, None) => write!(f, "OpenAck {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - impl From for Attributes { fn from(ev: OpenAck) -> Self { Self { @@ -346,17 +300,6 @@ pub struct OpenConfirm { pub counterparty_channel_id: Option, } -impl Display for OpenConfirm { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.channel_id, &self.counterparty_channel_id) { - (Some(channel_id), Some(counterparty_channel_id)) => write!(f, "OpenConfirm {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (Some(channel_id), None) => write!(f, "OpenConfirm {{ port_id: {}, channel_id: {}, connection_id: None, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, channel_id, self.counterparty_port_id), - (None, Some(counterparty_channel_id)) => write!(f, "OpenConfirm {{ port_id: {}, channel_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (None, None) => write!(f, "OpenConfirm {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - impl From for Attributes { fn from(ev: OpenConfirm) -> Self { Self { @@ -399,15 +342,6 @@ pub struct CloseInit { pub counterparty_channel_id: Option, } -impl Display for CloseInit { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match &self.counterparty_channel_id { - Some(counterparty_channel_id) => write!(f, "CloseInit {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - None => write!(f, "CloseInit {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - impl From for Attributes { fn from(ev: CloseInit) -> Self { Self { @@ -476,17 +410,6 @@ pub struct CloseConfirm { pub counterparty_channel_id: Option, } -impl Display for CloseConfirm { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match (&self.channel_id, &self.counterparty_channel_id) { - (Some(channel_id), Some(counterparty_channel_id)) => write!(f, "CloseConfirm {{ port_id: {}, channel_id: {}, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, channel_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (Some(channel_id), None) => write!(f, "CloseConfirm {{ port_id: {}, channel_id: {}, connection_id: None, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, channel_id, self.counterparty_port_id), - (None, Some(counterparty_channel_id)) => write!(f, "CloseConfirm {{ port_id: {}, channel_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: {} }}", self.port_id, self.connection_id, self.counterparty_port_id, counterparty_channel_id), - (None, None) => write!(f, "CloseConfirm {{ port_id: {}, client_id: None, connection_id: {}, counterparty_port_id: {}, counterparty_channel_id: None }}", self.port_id, self.connection_id, self.counterparty_port_id), - } - } -} - impl From for Attributes { fn from(ev: CloseConfirm) -> Self { Self { @@ -581,12 +504,6 @@ impl SendPacket { } } -impl Display for SendPacket { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "SendPacket {{ packet: {} }}", self.packet) - } -} - impl From for IbcEvent { fn from(v: SendPacket) -> Self { IbcEvent::SendPacket(v) @@ -625,12 +542,6 @@ impl ReceivePacket { } } -impl Display for ReceivePacket { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "ReceivePacket {{ packet: {} }}", self.packet) - } -} - impl From for IbcEvent { fn from(v: ReceivePacket) -> Self { IbcEvent::ReceivePacket(v) @@ -671,17 +582,6 @@ impl WriteAcknowledgement { } } -impl Display for WriteAcknowledgement { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!( - f, - "WriteAcknowledgement {{ packet: {}, ack: {} }}", - self.packet, - PrettySlice(&self.ack) - ) - } -} - impl From for IbcEvent { fn from(v: WriteAcknowledgement) -> Self { IbcEvent::WriteAcknowledgement(v) @@ -722,12 +622,6 @@ impl AcknowledgePacket { } } -impl Display for AcknowledgePacket { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "AcknowledgePacket {{ packet: {}}}", self.packet) - } -} - impl From for IbcEvent { fn from(v: AcknowledgePacket) -> Self { IbcEvent::AcknowledgePacket(v) @@ -766,12 +660,6 @@ impl TimeoutPacket { } } -impl Display for TimeoutPacket { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "TimeoutPacket {{ packet: {}}}", self.packet) - } -} - impl From for IbcEvent { fn from(v: TimeoutPacket) -> Self { IbcEvent::TimeoutPacket(v) @@ -810,12 +698,6 @@ impl TimeoutOnClosePacket { } } -impl Display for TimeoutOnClosePacket { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!(f, "TimeoutOnClosePacket {{ packet: {}}}", self.packet) - } -} - impl From for IbcEvent { fn from(v: TimeoutOnClosePacket) -> Self { IbcEvent::TimeoutOnClosePacket(v) diff --git a/crates/ibc/src/core/ics26_routing/handler.rs b/crates/ibc/src/core/ics26_routing/handler.rs index 24c966836..cc0a39a21 100644 --- a/crates/ibc/src/core/ics26_routing/handler.rs +++ b/crates/ibc/src/core/ics26_routing/handler.rs @@ -342,8 +342,8 @@ mod tests { ctx.scope_port_to_module(msg_chan_init.port_id.clone(), transfer_module_id.clone()); // Figure out the ID of the client that was just created. - let mut events = res.unwrap().events; - let client_id_event = events.pop(); + let events = res.unwrap().events; + let client_id_event = events.first(); assert!( client_id_event.is_some(), "There was no event generated for client creation!" diff --git a/crates/ibc/src/events.rs b/crates/ibc/src/events.rs index 8cc96e999..ca9b33718 100644 --- a/crates/ibc/src/events.rs +++ b/crates/ibc/src/events.rs @@ -1,8 +1,6 @@ use crate::prelude::*; -use crate::utils::pretty::PrettySlice; use core::convert::{TryFrom, TryInto}; -use core::fmt::{Display, Error as FmtError, Formatter}; use core::str::FromStr; use flex_error::{define_error, TraceError}; use serde_derive::{Deserialize, Serialize}; @@ -10,7 +8,6 @@ use tendermint::abci::tag::Tag; use tendermint::abci::Event as AbciEvent; use crate::core::ics02_client::error as client_error; -use crate::core::ics02_client::events::NewBlock; use crate::core::ics02_client::events::{self as ClientEvents}; use crate::core::ics03_connection::error as connection_error; use crate::core::ics03_connection::events as ConnectionEvents; @@ -98,9 +95,6 @@ impl WithBlockDataType { } } -const NEW_BLOCK_EVENT: &str = "new_block"; -const EMPTY_EVENT: &str = "empty"; -const CHAIN_ERROR_EVENT: &str = "chain_error"; const APP_MODULE_EVENT: &str = "app_module"; /// Client event types const CREATE_CLIENT_EVENT: &str = "create_client"; @@ -130,7 +124,6 @@ const TIMEOUT_ON_CLOSE_EVENT: &str = "timeout_packet_on_close"; /// Events types #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] pub enum IbcEventType { - NewBlock, CreateClient, UpdateClient, UpgradeClient, @@ -152,14 +145,11 @@ pub enum IbcEventType { Timeout, TimeoutOnClose, AppModule, - Empty, - ChainError, } impl IbcEventType { pub fn as_str(&self) -> &'static str { match *self { - IbcEventType::NewBlock => NEW_BLOCK_EVENT, IbcEventType::CreateClient => CREATE_CLIENT_EVENT, IbcEventType::UpdateClient => UPDATE_CLIENT_EVENT, IbcEventType::UpgradeClient => UPGRADE_CLIENT_EVENT, @@ -181,8 +171,6 @@ impl IbcEventType { IbcEventType::Timeout => TIMEOUT_EVENT, IbcEventType::TimeoutOnClose => TIMEOUT_ON_CLOSE_EVENT, IbcEventType::AppModule => APP_MODULE_EVENT, - IbcEventType::Empty => EMPTY_EVENT, - IbcEventType::ChainError => CHAIN_ERROR_EVENT, } } } @@ -192,7 +180,6 @@ impl FromStr for IbcEventType { fn from_str(s: &str) -> Result { match s { - NEW_BLOCK_EVENT => Ok(IbcEventType::NewBlock), CREATE_CLIENT_EVENT => Ok(IbcEventType::CreateClient), UPDATE_CLIENT_EVENT => Ok(IbcEventType::UpdateClient), UPGRADE_CLIENT_EVENT => Ok(IbcEventType::UpgradeClient), @@ -213,8 +200,6 @@ impl FromStr for IbcEventType { ACK_PACKET_EVENT => Ok(IbcEventType::AckPacket), TIMEOUT_EVENT => Ok(IbcEventType::Timeout), TIMEOUT_ON_CLOSE_EVENT => Ok(IbcEventType::TimeoutOnClose), - EMPTY_EVENT => Ok(IbcEventType::Empty), - CHAIN_ERROR_EVENT => Ok(IbcEventType::ChainError), // from_str() for `APP_MODULE_EVENT` MUST fail because a `ModuleEvent`'s type isn't constant _ => Err(Error::incorrect_event_type(s.to_string())), } @@ -222,10 +207,8 @@ impl FromStr for IbcEventType { } /// Events created by the IBC component of a chain, destined for a relayer. -#[derive(Debug, Clone, Serialize)] +#[derive(Debug)] pub enum IbcEvent { - NewBlock(NewBlock), - CreateClient(ClientEvents::CreateClient), UpdateClient(ClientEvents::UpdateClient), UpgradeClient(ClientEvents::UpgradeClient), @@ -251,44 +234,6 @@ pub enum IbcEvent { TimeoutOnClosePacket(ChannelEvents::TimeoutOnClosePacket), AppModule(ModuleEvent), - - ChainError(String), // Special event, signifying an error on CheckTx or DeliverTx -} - -impl Display for IbcEvent { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - match self { - IbcEvent::NewBlock(ev) => write!(f, "NewBlock({})", ev.height), - - IbcEvent::CreateClient(ev) => write!(f, "CreateClient({})", ev), - IbcEvent::UpdateClient(ev) => write!(f, "UpdateClient({})", ev), - IbcEvent::UpgradeClient(ev) => write!(f, "UpgradeClient({})", ev), - IbcEvent::ClientMisbehaviour(ev) => write!(f, "ClientMisbehaviour({})", ev), - - IbcEvent::OpenInitConnection(ev) => write!(f, "OpenInitConnection({})", ev), - IbcEvent::OpenTryConnection(ev) => write!(f, "OpenTryConnection({})", ev), - IbcEvent::OpenAckConnection(ev) => write!(f, "OpenAckConnection({})", ev), - IbcEvent::OpenConfirmConnection(ev) => write!(f, "OpenConfirmConnection({})", ev), - - IbcEvent::OpenInitChannel(ev) => write!(f, "OpenInitChannel({})", ev), - IbcEvent::OpenTryChannel(ev) => write!(f, "OpenTryChannel({})", ev), - IbcEvent::OpenAckChannel(ev) => write!(f, "OpenAckChannel({})", ev), - IbcEvent::OpenConfirmChannel(ev) => write!(f, "OpenConfirmChannel({})", ev), - IbcEvent::CloseInitChannel(ev) => write!(f, "CloseInitChannel({})", ev), - IbcEvent::CloseConfirmChannel(ev) => write!(f, "CloseConfirmChannel({})", ev), - - IbcEvent::SendPacket(ev) => write!(f, "SendPacket({})", ev), - IbcEvent::ReceivePacket(ev) => write!(f, "ReceivePacket({})", ev), - IbcEvent::WriteAcknowledgement(ev) => write!(f, "WriteAcknowledgement({})", ev), - IbcEvent::AcknowledgePacket(ev) => write!(f, "AcknowledgePacket({})", ev), - IbcEvent::TimeoutPacket(ev) => write!(f, "TimeoutPacket({})", ev), - IbcEvent::TimeoutOnClosePacket(ev) => write!(f, "TimeoutOnClosePacket({})", ev), - - IbcEvent::AppModule(ev) => write!(f, "AppModule({})", ev), - - IbcEvent::ChainError(ev) => write!(f, "ChainError({})", ev), - } - } } impl TryFrom for AbciEvent { @@ -317,24 +262,13 @@ impl TryFrom for AbciEvent { IbcEvent::TimeoutPacket(event) => event.try_into().map_err(Error::channel)?, IbcEvent::TimeoutOnClosePacket(event) => event.try_into().map_err(Error::channel)?, IbcEvent::AppModule(event) => event.try_into()?, - IbcEvent::NewBlock(_) | IbcEvent::ChainError(_) => { - return Err(Error::incorrect_event_type(event.to_string())) - } }) } } impl IbcEvent { - pub fn to_json(&self) -> String { - match serde_json::to_string(self) { - Ok(value) => value, - Err(_) => format!("{:?}", self), // Fallback to debug printing - } - } - pub fn event_type(&self) -> IbcEventType { match self { - IbcEvent::NewBlock(_) => IbcEventType::NewBlock, IbcEvent::CreateClient(_) => IbcEventType::CreateClient, IbcEvent::UpdateClient(_) => IbcEventType::UpdateClient, IbcEvent::ClientMisbehaviour(_) => IbcEventType::ClientMisbehaviour, @@ -356,7 +290,6 @@ impl IbcEvent { IbcEvent::TimeoutPacket(_) => IbcEventType::Timeout, IbcEvent::TimeoutOnClosePacket(_) => IbcEventType::TimeoutOnClose, IbcEvent::AppModule(_) => IbcEventType::AppModule, - IbcEvent::ChainError(_) => IbcEventType::ChainError, } } @@ -407,18 +340,6 @@ pub struct ModuleEvent { pub attributes: Vec, } -impl Display for ModuleEvent { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!( - f, - "ModuleEvent {{ kind: {}, module_name: {}, attributes: {} }}", - self.kind, - self.module_name, - PrettySlice(&self.attributes) - ) - } -} - impl TryFrom for AbciEvent { type Error = Error; @@ -447,16 +368,6 @@ pub struct ModuleEventAttribute { pub value: String, } -impl Display for ModuleEventAttribute { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { - write!( - f, - "ModuleEventAttribute {{ key: {}, value: {} }}", - self.key, self.value - ) - } -} - impl From<(K, V)> for ModuleEventAttribute { fn from((k, v): (K, V)) -> Self { Self {