Skip to content

Commit

Permalink
Remove OFA relay build_update_client method and dependency to Foreign…
Browse files Browse the repository at this point in the history
…Client
  • Loading branch information
soareschen committed Jul 14, 2023
1 parent a1595ab commit 2deffeb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,12 @@ use ibc_relayer_components::relay::traits::messages::update_client::{
CanBuildUpdateClientMessage, UpdateClientMessageBuilder,
};
use ibc_relayer_components::relay::traits::target::ChainTarget;
use ibc_relayer_components::relay::traits::target::{DestinationTarget, SourceTarget};

use crate::one_for_all::components;
use crate::one_for_all::traits::chain::OfaChain;
use crate::one_for_all::traits::relay::OfaRelay;
use crate::one_for_all::types::relay::OfaRelayWrapper;
use crate::std_prelude::*;

pub struct BuildUpdateClientMessageFromOfa;

#[async_trait]
impl<Relay, SrcChain> UpdateClientMessageBuilder<OfaRelayWrapper<Relay>, SourceTarget>
for BuildUpdateClientMessageFromOfa
where
Relay: OfaRelay<SrcChain = SrcChain>,
SrcChain: OfaChain,
{
async fn build_update_client_messages(
context: &OfaRelayWrapper<Relay>,
_target: SourceTarget,
height: &<Relay::DstChain as OfaChain>::Height,
) -> Result<Vec<SrcChain::Message>, Relay::Error> {
let messages = context
.relay
.build_src_update_client_messages(height)
.await?;

Ok(messages)
}
}

#[async_trait]
impl<Relay, DstChain> UpdateClientMessageBuilder<OfaRelayWrapper<Relay>, DestinationTarget>
for BuildUpdateClientMessageFromOfa
where
Relay: OfaRelay<DstChain = DstChain>,
DstChain: OfaChain,
{
async fn build_update_client_messages(
context: &OfaRelayWrapper<Relay>,
_target: DestinationTarget,
height: &<Relay::SrcChain as OfaChain>::Height,
) -> Result<Vec<DstChain::Message>, Relay::Error> {
let messages = context
.relay
.build_dst_update_client_messages(height)
.await?;

Ok(messages)
}
}

#[async_trait]
impl<Relay, Target> CanBuildUpdateClientMessage<Target> for OfaRelayWrapper<Relay>
where
Expand Down
10 changes: 0 additions & 10 deletions crates/relayer-all-in-one/src/one_for_all/traits/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ pub trait OfaRelay: Async {

fn dst_chain(&self) -> &OfaChainWrapper<Self::DstChain>;

async fn build_src_update_client_messages(
&self,
height: &<Self::DstChain as OfaChain>::Height,
) -> Result<Vec<<Self::SrcChain as OfaChain>::Message>, Self::Error>;

async fn build_dst_update_client_messages(
&self,
height: &<Self::SrcChain as OfaChain>::Height,
) -> Result<Vec<<Self::DstChain as OfaChain>::Message>, Self::Error>;

async fn try_acquire_packet_lock<'a>(
&'a self,
packet: &'a Self::Packet,
Expand Down
17 changes: 2 additions & 15 deletions crates/relayer-cosmos/src/contexts/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use ibc_relayer::chain::handle::BaseChainHandle;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::config::filter::PacketFilter;
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::ForeignClient;
use ibc_relayer::keyring::AnySigningKeyPair;
use ibc_relayer::keyring::Secp256k1KeyPair;
use ibc_relayer::spawn::spawn_chain_runtime;
Expand Down Expand Up @@ -133,24 +132,12 @@ impl CosmosBuilder {
src_batch_sender: CosmosBatchSender,
dst_batch_sender: CosmosBatchSender,
) -> Result<CosmosRelay<BaseChainHandle, BaseChainHandle>, Error> {
let client_src_to_dst = ForeignClient::restore(
dst_client_id.clone(),
dst_chain.chain.handle.clone(),
src_chain.chain.handle.clone(),
);

let client_dst_to_src = ForeignClient::restore(
src_client_id.clone(),
src_chain.chain.handle.clone(),
dst_chain.chain.handle.clone(),
);

let relay = CosmosRelay::new(
self.runtime.clone(),
src_chain,
dst_chain,
client_src_to_dst,
client_dst_to_src,
src_client_id.clone(),
dst_client_id.clone(),
self.packet_filter.clone(),
src_batch_sender,
dst_batch_sender,
Expand Down
15 changes: 7 additions & 8 deletions crates/relayer-cosmos/src/contexts/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use alloc::sync::Arc;
use futures::lock::Mutex;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::config::filter::PacketFilter;
use ibc_relayer::foreign_client::ForeignClient;
use ibc_relayer_all_in_one::one_for_all::types::chain::OfaChainWrapper;
use ibc_relayer_all_in_one::one_for_all::types::runtime::OfaRuntimeWrapper;
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;
use ibc_relayer_types::core::ics04_channel::packet::Sequence;
use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, PortId};
use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, ClientId, PortId};

use crate::contexts::chain::CosmosChain;
use crate::types::batch::CosmosBatchSender;
Expand All @@ -22,8 +21,8 @@ where
pub runtime: OfaRuntimeWrapper<TokioRuntimeContext>,
pub src_chain: OfaChainWrapper<CosmosChain<SrcChain>>,
pub dst_chain: OfaChainWrapper<CosmosChain<DstChain>>,
pub src_to_dst_client: ForeignClient<DstChain, SrcChain>,
pub dst_to_src_client: ForeignClient<SrcChain, DstChain>,
pub src_client_id: ClientId,
pub dst_client_id: ClientId,
pub packet_filter: PacketFilter,
pub packet_lock_mutex: Arc<Mutex<HashSet<(ChannelId, PortId, ChannelId, PortId, Sequence)>>>,
pub src_chain_message_batch_sender: CosmosBatchSender,
Expand All @@ -39,8 +38,8 @@ where
runtime: OfaRuntimeWrapper<TokioRuntimeContext>,
src_chain: OfaChainWrapper<CosmosChain<SrcChain>>,
dst_chain: OfaChainWrapper<CosmosChain<DstChain>>,
src_to_dst_client: ForeignClient<DstChain, SrcChain>,
dst_to_src_client: ForeignClient<SrcChain, DstChain>,
src_client_id: ClientId,
dst_client_id: ClientId,
packet_filter: PacketFilter,
src_chain_message_batch_sender: CosmosBatchSender,
dst_chain_message_batch_sender: CosmosBatchSender,
Expand All @@ -49,8 +48,8 @@ where
runtime,
src_chain,
dst_chain,
src_to_dst_client,
dst_to_src_client,
src_client_id,
dst_client_id,
packet_filter,
src_chain_message_batch_sender,
dst_chain_message_batch_sender,
Expand Down
64 changes: 2 additions & 62 deletions crates/relayer-cosmos/src/impls/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use async_trait::async_trait;
use eyre::eyre;
use futures::channel::oneshot::{channel, Sender};
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::foreign_client::ForeignClient;
use ibc_relayer_all_in_one::one_for_all::traits::chain::OfaChain;
use ibc_relayer_all_in_one::one_for_all::traits::relay::OfaRelay;
use ibc_relayer_all_in_one::one_for_all::types::chain::OfaChainWrapper;
Expand All @@ -12,14 +11,11 @@ use ibc_relayer_runtime::tokio::error::Error as TokioError;
use ibc_relayer_runtime::tokio::logger::tracing::TracingLogger;
use ibc_relayer_types::core::ics04_channel::packet::Packet;
use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId};
use ibc_relayer_types::tx_msg::Msg;
use ibc_relayer_types::Height;

use crate::contexts::chain::CosmosChain;
use crate::contexts::relay::CosmosRelay;
use crate::types::batch::CosmosBatchSender;
use crate::types::error::{BaseError, Error};
use crate::types::message::CosmosIbcMessage;

pub struct PacketLock {
pub release_sender: Option<Sender<()>>,
Expand Down Expand Up @@ -74,11 +70,11 @@ where
}

fn src_client_id(&self) -> &ClientId {
&self.dst_to_src_client.id
&self.src_client_id
}

fn dst_client_id(&self) -> &ClientId {
&self.src_to_dst_client.id
&self.dst_client_id
}

fn src_chain(&self) -> &OfaChainWrapper<Self::SrcChain> {
Expand All @@ -89,36 +85,6 @@ where
&self.dst_chain
}

async fn build_src_update_client_messages(
&self,
height: &Height,
) -> Result<Vec<CosmosIbcMessage>, Self::Error> {
let height = *height;
let client = self.dst_to_src_client.clone();

self.runtime
.runtime
.runtime
.spawn_blocking(move || build_update_client_messages(&client, height))
.await
.map_err(BaseError::join)?
}

async fn build_dst_update_client_messages(
&self,
height: &Height,
) -> Result<Vec<CosmosIbcMessage>, Self::Error> {
let height = *height;
let client = self.src_to_dst_client.clone();

self.runtime
.runtime
.runtime
.spawn_blocking(move || build_update_client_messages(&client, height))
.await
.map_err(BaseError::join)?
}

async fn try_acquire_packet_lock<'a>(&'a self, packet: &'a Packet) -> Option<PacketLock> {
let packet_key = (
packet.source_channel.clone(),
Expand Down Expand Up @@ -218,29 +184,3 @@ where
&self.dst_chain_message_batch_sender
}
}

fn build_update_client_messages<DstChain, SrcChain>(
foreign_client: &ForeignClient<DstChain, SrcChain>,
height: Height,
) -> Result<Vec<CosmosIbcMessage>, Error>
where
SrcChain: ChainHandle,
DstChain: ChainHandle,
{
let messages = foreign_client
.build_update_client_with_trusted(height, None)
.map_err(BaseError::foreign_client)?;

let ibc_messages = messages
.into_iter()
.map(|update_message| {
CosmosIbcMessage::new(Some(height), move |signer| {
let mut update_message = update_message.clone();
update_message.signer = signer.clone();
Ok(update_message.to_any())
})
})
.collect();

Ok(ibc_messages)
}

0 comments on commit 2deffeb

Please sign in to comment.