Skip to content

Commit

Permalink
Implement basic IBC channel handshake in relayer-next (#3463)
Browse files Browse the repository at this point in the history
* Implement channel Init step

* Implement Channel handshake TRY step for relayer-next

* Implement channel ack step for relayer-next

* Fix merge error

* Implement channel handshake confirm step for relayer-next

* Implement channel handshake for relayer-next

* Add docstring to channel handshake steps implementation

* Minor touch up

---------

Co-authored-by: Soares Chen <[email protected]>
  • Loading branch information
ljoss17 and soareschen authored Jul 10, 2023
1 parent 612ed64 commit 6251577
Show file tree
Hide file tree
Showing 36 changed files with 1,599 additions and 17 deletions.
9 changes: 8 additions & 1 deletion crates/relayer-all-in-one/src/all_for_one/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use ibc_relayer_components::chain::traits::client::create::HasCreateClientOption
use ibc_relayer_components::chain::traits::queries::consensus_state::CanQueryConsensusState;
use ibc_relayer_components::chain::traits::queries::received_packet::CanQueryReceivedPacket;
use ibc_relayer_components::chain::traits::queries::status::CanQueryChainStatus;
use ibc_relayer_components::chain::traits::types::channel::{
HasChannelHandshakePayloads, HasInitChannelOptionsType,
};
use ibc_relayer_components::chain::traits::types::connection::{
HasConnectionHandshakePayloads, HasInitConnectionOptionsType,
};
Expand All @@ -27,6 +30,8 @@ pub trait AfoChain<Counterparty>:
+ HasCreateClientOptions<Counterparty>
+ HasInitConnectionOptionsType<Counterparty>
+ HasConnectionHandshakePayloads<Counterparty>
+ HasInitChannelOptionsType<Counterparty>
+ HasChannelHandshakePayloads<Counterparty>
where
Counterparty: AfoCounterpartyChain<Self>,
{
Expand Down Expand Up @@ -59,7 +64,9 @@ where
+ CanQueryReceivedPacket<Counterparty>
+ HasCreateClientOptions<Counterparty>
+ HasInitConnectionOptionsType<Counterparty>
+ HasConnectionHandshakePayloads<Counterparty>,
+ HasConnectionHandshakePayloads<Counterparty>
+ HasInitChannelOptionsType<Counterparty>
+ HasChannelHandshakePayloads<Counterparty>,
{
}

Expand Down
6 changes: 6 additions & 0 deletions crates/relayer-all-in-one/src/all_for_one/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use ibc_relayer_components::logger::traits::level::HasLoggerWithBaseLevels;
use ibc_relayer_components::relay::impls::client::create::CanCreateClient;
use ibc_relayer_components::relay::traits::auto_relayer::CanAutoRelay;
use ibc_relayer_components::relay::traits::chains::HasRelayChains;
use ibc_relayer_components::relay::traits::channel::open_handshake::CanRelayChannelOpenHandshake;
use ibc_relayer_components::relay::traits::channel::open_init::CanInitChannel;
use ibc_relayer_components::relay::traits::connection::open_handshake::CanRelayConnectionOpenHandshake;
use ibc_relayer_components::relay::traits::connection::open_init::CanInitConnection;
use ibc_relayer_components::relay::traits::event_relayer::CanRelayEvent;
Expand Down Expand Up @@ -47,6 +49,8 @@ pub trait AfoRelay:
+ CanCreateClient<DestinationTarget>
+ CanInitConnection
+ CanRelayConnectionOpenHandshake
+ CanInitChannel
+ CanRelayChannelOpenHandshake
+ SupportsPacketRetry
{
type AfoSrcChain: AfoChain<Self::AfoDstChain>;
Expand Down Expand Up @@ -85,6 +89,8 @@ where
+ CanCreateClient<DestinationTarget>
+ CanInitConnection
+ CanRelayConnectionOpenHandshake
+ CanInitChannel
+ CanRelayChannelOpenHandshake
+ SupportsPacketRetry,
{
type AfoSrcChain = SrcChain;
Expand Down
182 changes: 182 additions & 0 deletions crates/relayer-all-in-one/src/one_for_all/impls/chain/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use async_trait::async_trait;

use ibc_relayer_components::chain::traits::message_builders::channel::{
CanBuildChannelHandshakeMessages, CanBuildChannelHandshakePayloads,
};
use ibc_relayer_components::chain::traits::types::channel::{
HasChannelHandshakePayloads, HasInitChannelOptionsType,
};
use ibc_relayer_components::chain::traits::types::ibc_events::channel::{
HasChannelOpenInitEvent, HasChannelOpenTryEvent,
};

use crate::one_for_all::traits::chain::OfaIbcChain;
use crate::one_for_all::types::chain::OfaChainWrapper;
use crate::std_prelude::*;

impl<Chain, Counterparty> HasChannelHandshakePayloads<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
type ChannelOpenTryPayload = Chain::ChannelOpenTryPayload;

type ChannelOpenAckPayload = Chain::ChannelOpenAckPayload;

type ChannelOpenConfirmPayload = Chain::ChannelOpenConfirmPayload;
}

impl<Chain, Counterparty> HasInitChannelOptionsType<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
type InitChannelOptions = Chain::InitChannelOptions;
}

impl<Chain, Counterparty> HasChannelOpenInitEvent<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
type ChannelOpenInitEvent = Chain::ChannelOpenInitEvent;

fn try_extract_channel_open_init_event(
event: Chain::Event,
) -> Option<Chain::ChannelOpenInitEvent> {
Chain::try_extract_channel_open_init_event(event)
}

fn channel_open_init_event_channel_id(
event: &Chain::ChannelOpenInitEvent,
) -> &Chain::ChannelId {
Chain::channel_open_init_event_channel_id(event)
}
}

impl<Chain, Counterparty> HasChannelOpenTryEvent<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
type ChannelOpenTryEvent = Chain::ChannelOpenTryEvent;

fn try_extract_channel_open_try_event(
event: Chain::Event,
) -> Option<Chain::ChannelOpenTryEvent> {
Chain::try_extract_channel_open_try_event(event)
}

fn channel_open_try_event_channel_id(event: &Chain::ChannelOpenTryEvent) -> &Chain::ChannelId {
Chain::channel_open_try_event_channel_id(event)
}
}

#[async_trait]
impl<Chain, Counterparty> CanBuildChannelHandshakePayloads<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
async fn build_channel_open_try_payload(
&self,
height: &Self::Height,
port_id: &Self::PortId,
channel_id: &Self::ChannelId,
) -> Result<Self::ChannelOpenTryPayload, Self::Error> {
self.chain
.build_channel_open_try_payload(height, port_id, channel_id)
.await
}

async fn build_channel_open_ack_payload(
&self,
height: &Self::Height,
port_id: &Self::PortId,
channel_id: &Self::ChannelId,
) -> Result<Self::ChannelOpenAckPayload, Self::Error> {
self.chain
.build_channel_open_ack_payload(height, port_id, channel_id)
.await
}

async fn build_channel_open_confirm_payload(
&self,
height: &Self::Height,
port_id: &Self::PortId,
channel_id: &Self::ChannelId,
) -> Result<Self::ChannelOpenConfirmPayload, Self::Error> {
self.chain
.build_channel_open_confirm_payload(height, port_id, channel_id)
.await
}
}

#[async_trait]
impl<Chain, Counterparty> CanBuildChannelHandshakeMessages<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
async fn build_channel_open_init_message(
&self,
port_id: &Self::PortId,
counterparty_port_id: &Counterparty::PortId,
init_channel_options: &Self::InitChannelOptions,
) -> Result<Self::Message, Self::Error> {
self.chain
.build_channel_open_init_message(port_id, counterparty_port_id, init_channel_options)
.await
}

async fn build_channel_open_try_message(
&self,
dst_port_id: &Self::PortId,
src_port_id: &Counterparty::PortId,
src_channel_id: &Counterparty::ChannelId,
counterparty_payload: Counterparty::ChannelOpenTryPayload,
) -> Result<Self::Message, Self::Error> {
self.chain
.build_channel_open_try_message(
dst_port_id,
src_port_id,
src_channel_id,
counterparty_payload,
)
.await
}

async fn build_channel_open_ack_message(
&self,
port_id: &Self::PortId,
channel_id: &Self::ChannelId,
counterparty_channel_id: &Counterparty::ChannelId,
counterparty_payload: Counterparty::ChannelOpenAckPayload,
) -> Result<Self::Message, Self::Error> {
self.chain
.build_channel_open_ack_message(
port_id,
channel_id,
counterparty_channel_id,
counterparty_payload,
)
.await
}

async fn build_channel_open_confirm_message(
&self,
port_id: &Self::PortId,
channel_id: &Self::ChannelId,
counterparty_payload: Counterparty::ChannelOpenConfirmPayload,
) -> Result<Self::Message, Self::Error> {
self.chain
.build_channel_open_confirm_message(port_id, channel_id, counterparty_payload)
.await
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod channel;
pub mod client;
pub mod connection;
pub mod error;
Expand Down
149 changes: 149 additions & 0 deletions crates/relayer-all-in-one/src/one_for_all/impls/relay/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use async_trait::async_trait;

use ibc_relayer_components::relay::impls::channel::open_ack::RelayChannelOpenAck;
use ibc_relayer_components::relay::impls::channel::open_confirm::RelayChannelOpenConfirm;
use ibc_relayer_components::relay::impls::channel::open_handshake::RelayChannelOpenHandshake;
use ibc_relayer_components::relay::impls::channel::open_init::{
InitializeChannel, InjectMissingChannelInitEventError,
};
use ibc_relayer_components::relay::impls::channel::open_try::{
InjectMissingChannelTryEventError, RelayChannelOpenTry,
};
use ibc_relayer_components::relay::traits::channel::open_ack::{
CanRelayChannelOpenAck, ChannelOpenAckRelayer,
};
use ibc_relayer_components::relay::traits::channel::open_confirm::{
CanRelayChannelOpenConfirm, ChannelOpenConfirmRelayer,
};
use ibc_relayer_components::relay::traits::channel::open_handshake::{
CanRelayChannelOpenHandshake, ChannelOpenHandshakeRelayer,
};
use ibc_relayer_components::relay::traits::channel::open_init::{
CanInitChannel, ChannelInitializer,
};
use ibc_relayer_components::relay::traits::channel::open_try::{
CanRelayChannelOpenTry, ChannelOpenTryRelayer,
};

use crate::one_for_all::traits::chain::{OfaChain, OfaIbcChain};
use crate::one_for_all::{traits::relay::OfaRelay, types::relay::OfaRelayWrapper};
use crate::std_prelude::*;

impl<Relay> InjectMissingChannelInitEventError for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
fn missing_channel_init_event_error(&self) -> Relay::Error {
self.relay.missing_channel_init_event_error()
}
}

impl<Relay> InjectMissingChannelTryEventError for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
fn missing_channel_try_event_error(
&self,
src_channel_id: &<Relay::SrcChain as OfaChain>::ChannelId,
) -> Relay::Error {
self.relay.missing_channel_try_event_error(src_channel_id)
}
}

#[async_trait]
impl<Relay> CanInitChannel for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
async fn init_channel(
&self,
src_port_id: &<Relay::SrcChain as OfaChain>::PortId,
dst_port_id: &<Relay::DstChain as OfaChain>::PortId,
init_channel_options: &<Relay::SrcChain as OfaIbcChain<Relay::DstChain>>::InitChannelOptions,
) -> Result<<Relay::SrcChain as OfaChain>::ChannelId, Self::Error> {
InitializeChannel::init_channel(self, src_port_id, dst_port_id, init_channel_options).await
}
}

#[async_trait]
impl<Relay> CanRelayChannelOpenTry for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
async fn relay_channel_open_try(
&self,
dst_port_id: &<Relay::DstChain as OfaChain>::PortId,
src_port_id: &<Relay::SrcChain as OfaChain>::PortId,
src_channel_id: &<Relay::SrcChain as OfaChain>::ChannelId,
) -> Result<<Relay::DstChain as OfaChain>::ChannelId, Self::Error> {
RelayChannelOpenTry::relay_channel_open_try(self, dst_port_id, src_port_id, src_channel_id)
.await
}
}

#[async_trait]
impl<Relay> CanRelayChannelOpenAck for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
async fn relay_channel_open_ack(
&self,
src_port_id: &<Relay::SrcChain as OfaChain>::PortId,
src_channel_id: &<Relay::SrcChain as OfaChain>::ChannelId,
dst_port_id: &<Relay::DstChain as OfaChain>::PortId,
dst_channel_id: &<Relay::DstChain as OfaChain>::ChannelId,
) -> Result<(), Self::Error> {
RelayChannelOpenAck::relay_channel_open_ack(
self,
src_port_id,
src_channel_id,
dst_port_id,
dst_channel_id,
)
.await
}
}

#[async_trait]
impl<Relay> CanRelayChannelOpenConfirm for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
async fn relay_channel_open_confirm(
&self,
dst_port_id: &<Relay::DstChain as OfaChain>::PortId,
dst_channel_id: &<Relay::DstChain as OfaChain>::ChannelId,
src_port_id: &<Relay::SrcChain as OfaChain>::PortId,
src_channel_id: &<Relay::SrcChain as OfaChain>::ChannelId,
) -> Result<(), Self::Error> {
RelayChannelOpenConfirm::relay_channel_open_confirm(
self,
dst_port_id,
dst_channel_id,
src_port_id,
src_channel_id,
)
.await
}
}

#[async_trait]
impl<Relay> CanRelayChannelOpenHandshake for OfaRelayWrapper<Relay>
where
Relay: OfaRelay,
{
async fn relay_channel_open_handshake(
&self,
src_channel_id: &<Relay::SrcChain as OfaChain>::ChannelId,
src_port_id: &<Relay::SrcChain as OfaChain>::PortId,
dst_port_id: &<Relay::DstChain as OfaChain>::PortId,
) -> Result<<Relay::DstChain as OfaChain>::ChannelId, Self::Error> {
RelayChannelOpenHandshake::relay_channel_open_handshake(
self,
src_channel_id,
src_port_id,
dst_port_id,
)
.await
}
}
Loading

0 comments on commit 6251577

Please sign in to comment.