From 80ef98f3a81d4eae862eccf7526f3b27396e1134 Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Wed, 18 Dec 2024 16:44:49 +0000 Subject: [PATCH] Implement CanBootstrapChannel --- .../src/components/chain.rs | 14 ++++- .../src/components/types.rs | 6 +- .../src/impls/events/channel.rs | 56 +++++++++++++++++++ .../src/impls/events/connection_id.rs | 6 +- .../src/impls/events/mod.rs | 1 + .../src/contexts/chain.rs | 2 + .../src/contexts/cosmos_to_starknet_relay.rs | 8 +++ .../starknet-relayer/src/impls/error.rs | 6 ++ 8 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 relayer/crates/starknet-chain-components/src/impls/events/channel.rs diff --git a/relayer/crates/starknet-chain-components/src/components/chain.rs b/relayer/crates/starknet-chain-components/src/components/chain.rs index d08f6d93..69147b80 100644 --- a/relayer/crates/starknet-chain-components/src/components/chain.rs +++ b/relayer/crates/starknet-chain-components/src/components/chain.rs @@ -48,7 +48,8 @@ use crate::impls::contract::deploy::DeployStarknetContract; use crate::impls::contract::invoke::InvokeStarknetContract; use crate::impls::contract::message::BuildInvokeContractCall; use crate::impls::counterparty_message_height::GetCounterpartyCosmosHeightFromStarknetMessage; -use crate::impls::events::connection_id::UseStarknetConnectionHandshakeEvents; +use crate::impls::events::channel::UseStarknetChannelEvents; +use crate::impls::events::connection_id::UseStarknetConnectionEvents; use crate::impls::events::create_client::UseStarknetCreateClientEvent; use crate::impls::messages::channel::BuildStarknetChannelHandshakeMessages; use crate::impls::messages::connection::BuildStarknetConnectionHandshakeMessages; @@ -142,8 +143,9 @@ cgp_preset! { ContractClassHashTypeComponent, ]: ProvideStarknetContractTypes, + // FIXME: we may have to define our own chain types, + // or implement Cairo encoding for the Cosmos types [ - ChannelIdTypeComponent, PortIdTypeComponent, SequenceTypeComponent, OutgoingPacketTypeComponent, @@ -154,6 +156,7 @@ cgp_preset! { [ ClientIdTypeComponent, ConnectionIdTypeComponent, + ChannelIdTypeComponent, ConnectionEndTypeComponent, ChannelEndTypeComponent, ]: @@ -221,7 +224,12 @@ cgp_preset! { ConnectionOpenInitEventComponent, ConnectionOpenTryEventComponent, ]: - UseStarknetConnectionHandshakeEvents, + UseStarknetConnectionEvents, + [ + ChannelOpenInitEventComponent, + ChannelOpenTryEventComponent, + ]: + UseStarknetChannelEvents, CreateClientMessageOptionsTypeComponent: ProvideNoCreateClientMessageOptionsType, CreateClientPayloadBuilderComponent: diff --git a/relayer/crates/starknet-chain-components/src/components/types.rs b/relayer/crates/starknet-chain-components/src/components/types.rs index 118396d0..6726a0ac 100644 --- a/relayer/crates/starknet-chain-components/src/components/types.rs +++ b/relayer/crates/starknet-chain-components/src/components/types.rs @@ -1,10 +1,11 @@ use cgp::prelude::*; use hermes_cosmos_chain_components::components::client::{ - ChannelEndTypeComponent, ClientIdTypeComponent, ConnectionEndTypeComponent, - ConnectionIdTypeComponent, + ChannelEndTypeComponent, ChannelIdTypeComponent, ClientIdTypeComponent, + ConnectionEndTypeComponent, ConnectionIdTypeComponent, }; use crate::types::channel_end::ChannelEnd; +use crate::types::channel_id::ChannelId; use crate::types::client_id::ClientId; use crate::types::connection_id::{ConnectionEnd, ConnectionId}; @@ -12,6 +13,7 @@ cgp_preset! { StarknetChainTypes { ClientIdTypeComponent: ClientId, ConnectionIdTypeComponent: ConnectionId, + ChannelIdTypeComponent: ChannelId, ConnectionEndTypeComponent: ConnectionEnd, ChannelEndTypeComponent: ChannelEnd, } diff --git a/relayer/crates/starknet-chain-components/src/impls/events/channel.rs b/relayer/crates/starknet-chain-components/src/impls/events/channel.rs new file mode 100644 index 00000000..988d20d7 --- /dev/null +++ b/relayer/crates/starknet-chain-components/src/impls/events/channel.rs @@ -0,0 +1,56 @@ +use hermes_cairo_encoding_components::strategy::ViaCairo; +use hermes_cairo_encoding_components::types::as_felt::AsFelt; +use hermes_chain_components::traits::types::ibc::HasChannelIdType; +use hermes_chain_components::traits::types::ibc_events::channel::{ + ProvideChannelOpenInitEvent, ProvideChannelOpenTryEvent, +}; +use hermes_chain_type_components::traits::types::message_response::HasMessageResponseType; +use hermes_encoding_components::traits::decode::CanDecode; +use hermes_encoding_components::traits::has_encoding::HasDefaultEncoding; +use hermes_encoding_components::traits::types::encoded::HasEncodedType; +use starknet::core::types::Felt; + +use crate::types::channel_id::ChannelId; +use crate::types::message_response::StarknetMessageResponse; + +pub struct UseStarknetChannelEvents; + +impl ProvideChannelOpenInitEvent + for UseStarknetChannelEvents +where + Chain: HasMessageResponseType + + HasChannelIdType + + HasDefaultEncoding, + Encoding: HasEncodedType> + CanDecode, +{ + type ChannelOpenInitEvent = ChannelId; + + fn try_extract_channel_open_init_event( + response: &StarknetMessageResponse, + ) -> Option { + Chain::default_encoding().decode(&response.result).ok() + } + + fn channel_open_init_event_channel_id(channel_id: &ChannelId) -> &ChannelId { + channel_id + } +} + +impl ProvideChannelOpenTryEvent + for UseStarknetChannelEvents +where + Chain: HasMessageResponseType + + HasChannelIdType + + HasDefaultEncoding, + Encoding: HasEncodedType> + CanDecode, +{ + type ChannelOpenTryEvent = ChannelId; + + fn try_extract_channel_open_try_event(response: &StarknetMessageResponse) -> Option { + Chain::default_encoding().decode(&response.result).ok() + } + + fn channel_open_try_event_channel_id(channel_id: &ChannelId) -> &ChannelId { + channel_id + } +} diff --git a/relayer/crates/starknet-chain-components/src/impls/events/connection_id.rs b/relayer/crates/starknet-chain-components/src/impls/events/connection_id.rs index 971ea62a..1bb03794 100644 --- a/relayer/crates/starknet-chain-components/src/impls/events/connection_id.rs +++ b/relayer/crates/starknet-chain-components/src/impls/events/connection_id.rs @@ -13,10 +13,10 @@ use starknet::core::types::Felt; use crate::types::connection_id::ConnectionId; use crate::types::message_response::StarknetMessageResponse; -pub struct UseStarknetConnectionHandshakeEvents; +pub struct UseStarknetConnectionEvents; impl ProvideConnectionOpenInitEvent - for UseStarknetConnectionHandshakeEvents + for UseStarknetConnectionEvents where Chain: HasMessageResponseType + HasConnectionIdType @@ -37,7 +37,7 @@ where } impl ProvideConnectionOpenTryEvent - for UseStarknetConnectionHandshakeEvents + for UseStarknetConnectionEvents where Chain: HasMessageResponseType + HasConnectionIdType diff --git a/relayer/crates/starknet-chain-components/src/impls/events/mod.rs b/relayer/crates/starknet-chain-components/src/impls/events/mod.rs index 10c3ef87..8fde4044 100644 --- a/relayer/crates/starknet-chain-components/src/impls/events/mod.rs +++ b/relayer/crates/starknet-chain-components/src/impls/events/mod.rs @@ -1,2 +1,3 @@ +pub mod channel; pub mod connection_id; pub mod create_client; diff --git a/relayer/crates/starknet-chain-context/src/contexts/chain.rs b/relayer/crates/starknet-chain-context/src/contexts/chain.rs index 0e1e4523..ea4e0db3 100644 --- a/relayer/crates/starknet-chain-context/src/contexts/chain.rs +++ b/relayer/crates/starknet-chain-context/src/contexts/chain.rs @@ -80,6 +80,7 @@ use hermes_relayer_components::chain::traits::types::event::HasEventType; use hermes_relayer_components::chain::traits::types::ibc::{ HasClientIdType, HasConnectionIdType, HasCounterpartyMessageHeight, }; +use hermes_relayer_components::chain::traits::types::ibc_events::channel::HasChannelOpenTryEvent; use hermes_relayer_components::chain::traits::types::ibc_events::connection::HasConnectionOpenTryEvent; use hermes_relayer_components::chain::traits::types::packet::HasOutgoingPacketType; use hermes_relayer_components::chain::traits::types::update_client::HasUpdateClientPayloadType; @@ -301,6 +302,7 @@ pub trait CanUseStarknetChain: + CanBuildChannelOpenAckMessage + CanBuildChannelOpenConfirmMessage + HasConnectionOpenTryEvent + + HasChannelOpenTryEvent + CanQueryContractAddress + CanQueryContractAddress { diff --git a/relayer/crates/starknet-relayer/src/contexts/cosmos_to_starknet_relay.rs b/relayer/crates/starknet-relayer/src/contexts/cosmos_to_starknet_relay.rs index 436e204e..788ff06b 100644 --- a/relayer/crates/starknet-relayer/src/contexts/cosmos_to_starknet_relay.rs +++ b/relayer/crates/starknet-relayer/src/contexts/cosmos_to_starknet_relay.rs @@ -9,11 +9,15 @@ use hermes_relayer_components::multi::traits::chain_at::{ }; use hermes_relayer_components::multi::traits::client_id_at::ClientIdAtGetterComponent; use hermes_relayer_components::multi::types::tags::{Dst, Src}; +use hermes_relayer_components::relay::impls::channel::bootstrap::CanBootstrapChannel; use hermes_relayer_components::relay::impls::connection::bootstrap::CanBootstrapConnection; use hermes_relayer_components::relay::impls::selector::SelectRelayAToB; use hermes_relayer_components::relay::traits::chains::{ CanRaiseRelayChainErrors, HasRelayChains, HasRelayClientIds, }; +use hermes_relayer_components::relay::traits::channel::open_ack::CanRelayChannelOpenAck; +use hermes_relayer_components::relay::traits::channel::open_confirm::CanRelayChannelOpenConfirm; +use hermes_relayer_components::relay::traits::channel::open_try::CanRelayChannelOpenTry; use hermes_relayer_components::relay::traits::client_creator::CanCreateClient; use hermes_relayer_components::relay::traits::connection::open_ack::CanRelayConnectionOpenAck; use hermes_relayer_components::relay::traits::connection::open_confirm::CanRelayConnectionOpenConfirm; @@ -139,6 +143,10 @@ pub trait CanUseCosmosToStarknetRelay: + CanRelayConnectionOpenAck + CanRelayConnectionOpenConfirm + CanBootstrapConnection + + CanRelayChannelOpenTry + + CanRelayChannelOpenAck + + CanRelayChannelOpenConfirm + + CanBootstrapChannel { } diff --git a/relayer/crates/starknet-relayer/src/impls/error.rs b/relayer/crates/starknet-relayer/src/impls/error.rs index 584a9a34..56e0015e 100644 --- a/relayer/crates/starknet-relayer/src/impls/error.rs +++ b/relayer/crates/starknet-relayer/src/impls/error.rs @@ -14,6 +14,8 @@ use hermes_error::traits::wrap::WrapError; use hermes_error::types::Error; use hermes_relayer_components::chain::traits::send_message::EmptyMessageResponse; use hermes_relayer_components::chain::traits::types::chain_id::HasChainIdType; +use hermes_relayer_components::relay::impls::channel::open_init::MissingChannelInitEventError; +use hermes_relayer_components::relay::impls::channel::open_try::MissingChannelTryEventError; use hermes_relayer_components::relay::impls::connection::open_init::MissingConnectionInitEventError; use hermes_relayer_components::relay::impls::connection::open_try::MissingConnectionTryEventError; use hermes_relayer_components::relay::impls::create_client::MissingCreateClientEventError; @@ -45,6 +47,10 @@ delegate_components! { MissingConnectionInitEventError<'a, Relay>, <'a, Relay: HasRelayChains> MissingConnectionTryEventError<'a, Relay>, + <'a, Relay> + MissingChannelInitEventError<'a, Relay>, + <'a, Relay: HasRelayChains> + MissingChannelTryEventError<'a, Relay>, ]: DebugError, [