Skip to content

Commit

Permalink
Implement UpdateClient chain components and remove depdency to `For…
Browse files Browse the repository at this point in the history
…eignClient` (#3480)

* Finish abstract implementation of UpdateClient

* Implement Cosmos methods for building UpdateClient message

* Implement find_consensus_state_height_before in Cosmos chain

* New UpdateClient component is now working

* Remove OFA relay build_update_client method and dependency to ForeignClient
  • Loading branch information
soareschen authored Jul 14, 2023
1 parent cc1c1a8 commit c7763f5
Show file tree
Hide file tree
Showing 21 changed files with 459 additions and 185 deletions.
5 changes: 2 additions & 3 deletions crates/relayer-all-in-one/src/one_for_all/components.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ibc_relayer_components::relay::impls::client::update::BuildUpdateClientMessages;
use ibc_relayer_components::relay::impls::message_senders::chain_sender::SendIbcMessagesToChain;
use ibc_relayer_components::relay::impls::message_senders::update_client::SendIbcMessagesWithUpdateClient;
use ibc_relayer_components::relay::impls::messages::skip_update_client::SkipUpdateClient;
Expand All @@ -20,15 +21,13 @@ use ibc_relayer_components_extra::telemetry::impls::status::ChainStatusTelemetry

use crate::one_for_all::impls::chain::queries::consensus_state::SendConsensusStateQueryToOfa;
use crate::one_for_all::impls::chain::queries::status::SendChainStatusQueryToOfa;
use crate::one_for_all::impls::relay::message_builders::update_client::BuildUpdateClientMessageFromOfa;
use crate::one_for_all::impls::relay::packet_filter::FilterPacketFromOfa;

pub type ChainStatusQuerier = ChainStatusTelemetryQuerier<SendChainStatusQueryToOfa>;

pub type ConsensusStateQuerier = ConsensusStateTelemetryQuerier<SendConsensusStateQueryToOfa>;

pub type UpdateClientMessageBuilder =
SkipUpdateClient<WaitUpdateClient<BuildUpdateClientMessageFromOfa>>;
pub type UpdateClientMessageBuilder = SkipUpdateClient<WaitUpdateClient<BuildUpdateClientMessages>>;

pub type AckPacketRelayer = BaseAckPacketRelayer;

Expand Down
109 changes: 109 additions & 0 deletions crates/relayer-all-in-one/src/one_for_all/impls/chain/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use async_trait::async_trait;
use ibc_relayer_components::chain::traits::client::client_state::CanQueryClientState;
use ibc_relayer_components::chain::traits::client::consensus_state::CanFindConsensusStateHeight;
use ibc_relayer_components::chain::traits::client::create::{
CanBuildCreateClientMessage, CanBuildCreateClientPayload, HasCreateClientEvent,
HasCreateClientOptions, HasCreateClientPayload,
};
use ibc_relayer_components::chain::traits::client::update::{
CanBuildUpdateClientMessage, CanBuildUpdateClientPayload, HasUpdateClientPayload,
};
use ibc_relayer_components::chain::traits::types::client_state::{
HasClientStateFields, HasClientStateType,
};

use crate::one_for_all::traits::chain::OfaIbcChain;
use crate::one_for_all::types::chain::OfaChainWrapper;
Expand Down Expand Up @@ -76,3 +84,104 @@ where
.await
}
}

#[async_trait]
impl<Chain, Counterparty> HasClientStateType<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
type ClientState = Chain::ClientState;
}

#[async_trait]
impl<Chain, Counterparty> HasUpdateClientPayload<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
type UpdateClientPayload = Chain::UpdateClientPayload;
}

#[async_trait]
impl<Chain, Counterparty> CanBuildUpdateClientPayload<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
async fn build_update_client_payload(
&self,
trusted_height: &Self::Height,
target_height: &Self::Height,
client_state: Self::ClientState,
) -> Result<Self::UpdateClientPayload, Self::Error> {
self.chain
.build_update_client_payload(trusted_height, target_height, client_state)
.await
}
}

#[async_trait]
impl<Chain, Counterparty> CanBuildUpdateClientMessage<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
async fn build_update_client_message(
&self,
client_id: &Self::ClientId,
payload: Counterparty::UpdateClientPayload,
) -> Result<Vec<Self::Message>, Self::Error> {
self.chain
.build_update_client_message(client_id, payload)
.await
}
}

#[async_trait]
impl<Chain, Counterparty> CanFindConsensusStateHeight<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
async fn find_consensus_state_height_before(
&self,
client_id: &Self::ClientId,
target_height: &Counterparty::Height,
) -> Result<Counterparty::Height, Self::Error> {
self.chain
.find_consensus_state_height_before(client_id, target_height)
.await
}
}

impl<Chain, Counterparty> HasClientStateFields<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
fn client_state_latest_height(client_state: &Self::ClientState) -> &Self::Height {
Chain::client_state_latest_height(client_state)
}
}

#[async_trait]
impl<Chain, Counterparty> CanQueryClientState<OfaChainWrapper<Counterparty>>
for OfaChainWrapper<Chain>
where
Chain: OfaIbcChain<Counterparty>,
Counterparty: OfaIbcChain<Chain>,
{
async fn query_client_state(
&self,
client_id: &Self::ClientId,
) -> Result<Counterparty::ClientState, Self::Error> {
self.chain.query_client_state(client_id).await
}
}
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
30 changes: 30 additions & 0 deletions crates/relayer-all-in-one/src/one_for_all/traits/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,16 @@ where
*/
type OutgoingPacket: Async;

type ClientState: Async;

type CreateClientPayloadOptions: Async;

type CreateClientPayload: Async;

type CreateClientEvent: Async;

type UpdateClientPayload: Async;

type ConnectionDetails: Async;

type ConnectionVersion: Eq + Default + Async;
Expand Down Expand Up @@ -237,6 +241,8 @@ where
fn outgoing_packet_timeout_timestamp(packet: &Self::OutgoingPacket)
-> &Counterparty::Timestamp;

fn client_state_latest_height(client_state: &Self::ClientState) -> &Self::Height;

fn log_incoming_packet<'a>(
event: &'a Self::IncomingPacket,
) -> <Self::Logger as BaseLogger>::LogValue<'a>;
Expand Down Expand Up @@ -292,6 +298,11 @@ where
port_id: &Self::PortId,
) -> Result<Counterparty::ChainId, Self::Error>;

async fn query_client_state(
&self,
client_id: &Self::ClientId,
) -> Result<Counterparty::ClientState, Self::Error>;

async fn query_consensus_state(
&self,
client_id: &Self::ClientId,
Expand Down Expand Up @@ -339,6 +350,25 @@ where
counterparty_payload: Counterparty::CreateClientPayload,
) -> Result<Self::Message, Self::Error>;

async fn build_update_client_payload(
&self,
trusted_height: &Self::Height,
target_height: &Self::Height,
client_state: Self::ClientState,
) -> Result<Self::UpdateClientPayload, Self::Error>;

async fn build_update_client_message(
&self,
client_id: &Self::ClientId,
payload: Counterparty::UpdateClientPayload,
) -> Result<Vec<Self::Message>, Self::Error>;

async fn find_consensus_state_height_before(
&self,
client_id: &Self::ClientId,
target_height: &Counterparty::Height,
) -> Result<Counterparty::Height, Self::Error>;

async fn build_connection_open_init_payload(
&self,
) -> Result<Self::ConnectionOpenInitPayload, Self::Error>;
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
16 changes: 10 additions & 6 deletions crates/relayer-components/src/chain/traits/client/client_state.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use async_trait::async_trait;

use crate::chain::traits::types::client_state::HasClientStateSettingsType;
use crate::chain::traits::types::client_state::HasClientStateType;
use crate::chain::traits::types::ibc::HasIbcChainTypes;
use crate::core::traits::error::HasErrorType;
use crate::std_prelude::*;

#[async_trait]
pub trait CanBuildClientState<Counterparty>: HasClientStateSettingsType<Counterparty> {
async fn build_client_state(
pub trait CanQueryClientState<Counterparty>: HasIbcChainTypes<Counterparty> + HasErrorType
where
Counterparty: HasClientStateType<Self>,
{
async fn query_client_state(
&self,
height: &Self::Height,
settings: &Self::ClientStateSettings,
) -> Self::ClientState;
client_id: &Self::ClientId,
) -> Result<Counterparty::ClientState, Self::Error>;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use async_trait::async_trait;

use crate::chain::traits::types::client_state::HasClientStateType;
use crate::chain::traits::types::consensus_state::HasConsensusStateType;
use crate::chain::traits::types::height::HasHeightType;
use crate::chain::traits::types::ibc::HasIbcChainTypes;
use crate::core::traits::error::HasErrorType;
use crate::std_prelude::*;

#[async_trait]
pub trait CanBuildConsensusState<Counterparty>:
HasConsensusStateType<Counterparty> + HasClientStateType<Counterparty> + HasErrorType
pub trait CanFindConsensusStateHeight<Counterparty>:
HasIbcChainTypes<Counterparty> + HasErrorType
where
Counterparty: HasHeightType,
{
async fn build_consensus_state(
trusted_height: &Self::Height,
target_height: &Self::Height,
client_state: &Self::ClientState,
) -> Result<Self::ConsensusState, Self::Error>;
/**
Query the chain to find a consensus state that has a height that is
less than or equal the target height. This is needed as a base trusted
height to build the headers for UpdateClient.
Invariant: the returned height must be less than or equal to the given
target height.
*/
async fn find_consensus_state_height_before(
&self,
client_id: &Self::ClientId,
target_height: &Counterparty::Height,
) -> Result<Counterparty::Height, Self::Error>;
}
1 change: 1 addition & 0 deletions crates/relayer-components/src/chain/traits/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod client_state;
pub mod consensus_state;
pub mod create;
pub mod update;
36 changes: 36 additions & 0 deletions crates/relayer-components/src/chain/traits/client/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use async_trait::async_trait;

use crate::chain::traits::types::client_state::HasClientStateType;
use crate::chain::traits::types::ibc::HasIbcChainTypes;
use crate::core::traits::error::HasErrorType;
use crate::core::traits::sync::Async;
use crate::std_prelude::*;

pub trait HasUpdateClientPayload<Counterparty>: HasIbcChainTypes<Counterparty> {
type UpdateClientPayload: Async;
}

#[async_trait]
pub trait CanBuildUpdateClientPayload<Counterparty>:
HasUpdateClientPayload<Counterparty> + HasClientStateType<Counterparty> + HasErrorType
{
async fn build_update_client_payload(
&self,
trusted_height: &Self::Height,
target_height: &Self::Height,
client_state: Self::ClientState,
) -> Result<Self::UpdateClientPayload, Self::Error>;
}

#[async_trait]
pub trait CanBuildUpdateClientMessage<Counterparty>:
HasIbcChainTypes<Counterparty> + HasErrorType
where
Counterparty: HasUpdateClientPayload<Self>,
{
async fn build_update_client_message(
&self,
client_id: &Self::ClientId,
payload: Counterparty::UpdateClientPayload,
) -> Result<Vec<Self::Message>, Self::Error>;
}
Loading

0 comments on commit c7763f5

Please sign in to comment.