Skip to content

Commit

Permalink
Perform packet filter on chain context instead of on relay context (#497
Browse files Browse the repository at this point in the history
)

* Rename CanFilterPackets to CanFilterRelayPackets

* Add back HasIncomingPacketType alias

* Add HasRelayPacketType

* Use HasRelayPacketType in CanFilterRelayPacket

* Implement OutgoingPacketFilter for FilterPacketWithConfig

* Add HasCounterparty and CanUseCounterparty helper

* Implement IncomingPacketFilter for FilterPacketWithConfig

* Add packet_filter field to CosmosChain

* Implement CanFilterPacket for CosmosChain

* Simplify trait bounds of counterparty

* Add FilterRelayPacketWithChains

* Remove packet_filter field from CosmosRelay

* Use FilterRelayPacketWithChains in WasmRelayer

* Remove relay impl of FilterPacketWithConfig

* Simplify delegation of extra relay components

* Implement RelayPacketFilter in DefaultRelayComponents

* Rename default/extra-RelayComponents to -RelayPreset
  • Loading branch information
soareschen authored Dec 11, 2024
1 parent 01813c1 commit be7d07f
Show file tree
Hide file tree
Showing 46 changed files with 372 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ use alloc::vec::Vec;

use cgp::core::component::UseDelegate;
use cgp::prelude::*;
use hermes_chain_type_components::traits::types::counterparty::CanUseCounterparty;
use hermes_chain_type_components::traits::types::ibc::client_id::HasClientIdType;
use hermes_chain_type_components::traits::types::message::HasMessageType;

use crate::traits::types::update_client::HasUpdateClientPayloadType;
use crate::traits::types::update_client::{HasUpdateClientPayloadType, UpdateClientPayloadOf};

#[cgp_component {
provider: UpdateClientMessageBuilder,
context: Chain,
}]
#[async_trait]
pub trait CanBuildUpdateClientMessage<Counterparty>:
HasClientIdType<Counterparty> + HasMessageType + HasErrorType
where
Counterparty: HasUpdateClientPayloadType<Self>,
HasClientIdType<Counterparty>
+ CanUseCounterparty<Counterparty, Counterparty: HasUpdateClientPayloadType<Self>>
+ HasMessageType
+ HasErrorType
{
async fn build_update_client_message(
&self,
client_id: &Self::ClientId,
payload: Counterparty::UpdateClientPayload,
payload: UpdateClientPayloadOf<Counterparty, Self>,
) -> Result<Vec<Self::Message>, Self::Error>;
}

Expand Down
21 changes: 14 additions & 7 deletions crates/chain/chain-components/src/traits/packet/fields.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use cgp::prelude::*;
use hermes_chain_type_components::traits::types::counterparty::CanUseCounterparty;
use hermes_chain_type_components::traits::types::height::HasHeightType;
use hermes_chain_type_components::traits::types::ibc::channel_id::HasChannelIdType;
use hermes_chain_type_components::traits::types::ibc::packet::HasOutgoingPacketType;
use hermes_chain_type_components::traits::types::ibc::port_id::HasPortIdType;
use hermes_chain_type_components::traits::types::ibc::sequence::HasSequenceType;
use hermes_chain_type_components::traits::types::timeout::HasTimeoutType;
use hermes_chain_type_components::traits::types::timeout::{HasTimeoutType, TimeoutOf};

use crate::types::aliases::{ChannelIdOf, HeightOf, PortIdOf};

#[cgp_component {
provider: OutgoingPacketFieldsReader,
Expand All @@ -15,24 +18,28 @@ pub trait CanReadOutgoingPacketFields<Counterparty>:
+ HasChannelIdType<Counterparty>
+ HasPortIdType<Counterparty>
+ HasSequenceType<Counterparty>
where
Counterparty: HasHeightType + HasTimeoutType + HasChannelIdType<Self> + HasPortIdType<Self>,
+ CanUseCounterparty<
Counterparty,
Counterparty: HasHeightType + HasTimeoutType + HasChannelIdType<Self> + HasPortIdType<Self>,
>
{
fn outgoing_packet_src_channel_id(packet: &Self::OutgoingPacket) -> &Self::ChannelId;

fn outgoing_packet_dst_channel_id(packet: &Self::OutgoingPacket) -> &Counterparty::ChannelId;
fn outgoing_packet_dst_channel_id(
packet: &Self::OutgoingPacket,
) -> &ChannelIdOf<Counterparty, Self>;

fn outgoing_packet_src_port(packet: &Self::OutgoingPacket) -> &Self::PortId;

fn outgoing_packet_dst_port(packet: &Self::OutgoingPacket) -> &Counterparty::PortId;
fn outgoing_packet_dst_port(packet: &Self::OutgoingPacket) -> &PortIdOf<Counterparty, Self>;

fn outgoing_packet_sequence(packet: &Self::OutgoingPacket) -> &Self::Sequence;

fn outgoing_packet_timeout_height(
packet: &Self::OutgoingPacket,
) -> Option<Counterparty::Height>;
) -> Option<HeightOf<Counterparty>>;

fn outgoing_packet_timeout_timestamp(
packet: &Self::OutgoingPacket,
) -> Option<Counterparty::Timeout>;
) -> Option<TimeoutOf<Counterparty>>;
}
32 changes: 32 additions & 0 deletions crates/chain/chain-components/src/traits/packet/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use cgp::prelude::*;
use hermes_chain_type_components::traits::types::ibc::packet::{
HasIncomingPacketType, HasOutgoingPacketType,
};

#[cgp_component {
context: Chain,
provider: OutgoingPacketFilter,
}]
#[async_trait]
pub trait CanFilterOutgoingPacket<Counterparty>:
HasOutgoingPacketType<Counterparty> + HasErrorType
{
async fn should_relay_outgoing_packet(
&self,
packet: &Self::OutgoingPacket,
) -> Result<bool, Self::Error>;
}

#[cgp_component {
context: Chain,
provider: IncomingPacketFilter,
}]
#[async_trait]
pub trait CanFilterIncomingPacket<Counterparty>:
HasIncomingPacketType<Counterparty> + HasErrorType
{
async fn should_relay_incoming_packet(
&self,
packet: &Self::IncomingPacket,
) -> Result<bool, Self::Error>;
}
1 change: 1 addition & 0 deletions crates/chain/chain-components/src/traits/packet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod fields;
pub mod filter;
pub mod from_write_ack;
11 changes: 7 additions & 4 deletions crates/chain/chain-components/src/traits/queries/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use core::marker::PhantomData;

use cgp::core::component::UseDelegate;
use cgp::prelude::*;
use hermes_chain_type_components::traits::types::counterparty::CanUseCounterparty;
use hermes_chain_type_components::traits::types::height::HasHeightType;
use hermes_chain_type_components::traits::types::ibc::client_id::HasClientIdType;
use hermes_chain_type_components::traits::types::ibc::client_state::ClientStateOf;

use crate::traits::queries::chain_status::CanQueryChainStatus;
use crate::traits::types::client_state::{HasClientStateType, HasRawClientStateType};
Expand All @@ -17,16 +19,17 @@ use crate::traits::types::proof::HasCommitmentProofType;
}]
#[async_trait]
pub trait CanQueryClientState<Counterparty>:
HasClientIdType<Counterparty> + HasHeightType + HasErrorType
where
Counterparty: HasClientStateType<Self>,
HasClientIdType<Counterparty>
+ CanUseCounterparty<Counterparty, Counterparty: HasClientStateType<Self>>
+ HasHeightType
+ HasErrorType
{
async fn query_client_state(
&self,
tag: PhantomData<Counterparty>,
client_id: &Self::ClientId,
height: &Self::Height,
) -> Result<Counterparty::ClientState, Self::Error>;
) -> Result<ClientStateOf<Counterparty, Self>, Self::Error>;
}

#[cgp_component {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@ use core::marker::PhantomData;

use cgp::core::component::UseDelegate;
use cgp::prelude::*;
use hermes_chain_type_components::traits::types::counterparty::CanUseCounterparty;
use hermes_chain_type_components::traits::types::ibc::client_id::HasClientIdType;
use hermes_chain_type_components::traits::types::ibc::consensus_state::ConsensusStateOf;

use super::chain_status::CanQueryChainStatus;
use crate::traits::types::consensus_state::{HasConsensusStateType, HasRawConsensusStateType};
use crate::traits::types::height::HasHeightType;
use crate::traits::types::proof::HasCommitmentProofType;
use crate::types::aliases::HeightOf;

#[cgp_component {
provider: ConsensusStateQuerier,
context: Chain,
}]
#[async_trait]
pub trait CanQueryConsensusState<Counterparty>:
HasClientIdType<Counterparty> + HasHeightType + HasErrorType
where
Counterparty: HasConsensusStateType<Self> + HasHeightType,
HasClientIdType<Counterparty>
+ CanUseCounterparty<Counterparty, Counterparty: HasConsensusStateType<Self> + HasHeightType>
+ HasHeightType
+ HasErrorType
{
async fn query_consensus_state(
&self,
tag: PhantomData<Counterparty>,
client_id: &Self::ClientId,
consensus_height: &Counterparty::Height,
consensus_height: &HeightOf<Counterparty>,
query_height: &Self::Height,
) -> Result<Counterparty::ConsensusState, Self::Error>;
) -> Result<ConsensusStateOf<Counterparty, Self>, Self::Error>;
}

#[cgp_component {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub trait HasUpdateClientPayloadType<Counterparty>: Async {
type UpdateClientPayload: Async;
}

pub type UpdateClientPayloadOf<Chain, Counterparty> =
<Chain as HasUpdateClientPayloadType<Counterparty>>::UpdateClientPayload;

impl<Chain, Counterparty, Components, Delegate> ProvideUpdateClientPayloadType<Chain, Counterparty>
for UseDelegate<Components>
where
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub trait HasCounterparty<Counterparty> {
type Counterparty;
}

impl<Chain, Counterparty> HasCounterparty<Counterparty> for Chain {
type Counterparty = Counterparty;
}

pub trait CanUseCounterparty<Counterparty>:
HasCounterparty<Counterparty, Counterparty = Counterparty>
{
}

impl<Chain, Counterparty> CanUseCounterparty<Counterparty> for Chain {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub trait HasClientStateType<Counterparty>: Async {
type ClientState: Async;
}

pub type ClientStateOf<Chain, Counterparty> =
<Chain as HasClientStateType<Counterparty>>::ClientState;

impl<Chain, Counterparty, Components, Delegate> ProvideClientStateType<Chain, Counterparty>
for UseDelegate<Components>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub trait HasConsensusStateType<Counterparty>: Async {
type ConsensusState: Async;
}

pub type ConsensusStateOf<Chain, Counterparty> =
<Chain as HasConsensusStateType<Counterparty>>::ConsensusState;

impl<Chain, Counterparty, Components, Delegate> ProvideConsensusStateType<Chain, Counterparty>
for UseDelegate<Components>
where
Expand Down
21 changes: 21 additions & 0 deletions crates/chain/chain-type-components/src/traits/types/ibc/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use cgp::prelude::*;

use crate::traits::types::counterparty::CanUseCounterparty;

#[cgp_component {
name: OutgoingPacketTypeComponent,
provider: ProvideOutgoingPacketType,
Expand All @@ -18,3 +20,22 @@ pub trait HasOutgoingPacketType<Counterparty>: Async {
*/
type OutgoingPacket: Async;
}

pub trait HasIncomingPacketType<Counterparty>:
Sized
+ Async
+ CanUseCounterparty<
Counterparty,
Counterparty: HasOutgoingPacketType<Self, OutgoingPacket = Self::IncomingPacket>,
>
{
type IncomingPacket: Async;
}

impl<Chain, Counterparty> HasIncomingPacketType<Counterparty> for Chain
where
Chain: Async,
Counterparty: HasOutgoingPacketType<Chain>,
{
type IncomingPacket = Counterparty::OutgoingPacket;
}
1 change: 1 addition & 0 deletions crates/chain/chain-type-components/src/traits/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod amount;
pub mod chain_id;
pub mod commitment_prefix;
pub mod commitment_proof;
pub mod counterparty;
pub mod denom;
pub mod event;
pub mod height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pub trait HasTimeoutType: HasTimeType {

fn has_timed_out(time: &Self::Time, timeout: &Self::Timeout) -> bool;
}

pub type TimeoutOf<Chain> = <Chain as HasTimeoutType>::Timeout;
10 changes: 9 additions & 1 deletion crates/cosmos/cosmos-chain-components/src/components/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub use hermes_relayer_components::chain::traits::message_builders::receive_pack
pub use hermes_relayer_components::chain::traits::message_builders::timeout_unordered_packet::TimeoutUnorderedPacketMessageBuilderComponent;
pub use hermes_relayer_components::chain::traits::message_builders::update_client::UpdateClientMessageBuilderComponent;
pub use hermes_relayer_components::chain::traits::packet::fields::OutgoingPacketFieldsReaderComponent;
pub use hermes_relayer_components::chain::traits::packet::filter::{
IncomingPacketFilterComponent, OutgoingPacketFilterComponent,
};
pub use hermes_relayer_components::chain::traits::packet::from_write_ack::PacketFromWriteAckBuilderComponent;
pub use hermes_relayer_components::chain::traits::payload_builders::ack_packet::AckPacketPayloadBuilderComponent;
pub use hermes_relayer_components::chain::traits::payload_builders::channel_handshake::{
Expand Down Expand Up @@ -163,6 +166,7 @@ use crate::impls::queries::send_packets::QuerySendPacketsConcurrently;
use crate::impls::queries::unreceived_acks::QueryUnreceivedCosmosAcksSequences;
use crate::impls::queries::unreceived_packet::QueryUnreceivedCosmosPacketSequences;
use crate::impls::queries::write_ack_event::QueryCosmosWriteAckEvent;
use crate::impls::relay::packet_filter::FilterPacketWithConfig;
use crate::impls::types::chain::ProvideCosmosChainTypes;
use crate::impls::types::client_state::ProvideAnyRawClientState;
use crate::impls::types::consensus_state::ProvideAnyRawConsensusState;
Expand Down Expand Up @@ -332,7 +336,11 @@ cgp_preset! {
ChannelEndWithProofsQuerierComponent,
]:
QueryCosmosChannelEndFromAbci,

[
OutgoingPacketFilterComponent,
IncomingPacketFilterComponent,
]:
FilterPacketWithConfig<symbol!("packet_filter")>,
[
ClientStateTypeComponent,
ClientStateFieldsComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
use core::marker::PhantomData;

use cgp::prelude::HasField;
use cgp::core::Async;
use cgp::prelude::{HasErrorType, HasField};
use hermes_relayer_components::chain::traits::packet::fields::CanReadOutgoingPacketFields;
use hermes_relayer_components::chain::traits::packet::filter::{
IncomingPacketFilter, OutgoingPacketFilter,
};
use hermes_relayer_components::chain::traits::types::ibc::{HasChannelIdType, HasPortIdType};
use hermes_relayer_components::relay::traits::chains::{HasRelayChainTypes, PacketOf};
use hermes_relayer_components::relay::traits::packet_filter::PacketFilter;
use ibc::core::host::types::identifiers::{ChannelId, PortId};

use crate::types::messages::packet::packet_filter::PacketFilterConfig;

pub struct FilterPacketWithConfig<Tag>(pub PhantomData<Tag>);

impl<Relay, Tag, SrcChain, DstChain> PacketFilter<Relay> for FilterPacketWithConfig<Tag>
impl<Chain, Counterparty, Tag> OutgoingPacketFilter<Chain, Counterparty>
for FilterPacketWithConfig<Tag>
where
Relay: HasRelayChainTypes<SrcChain = SrcChain, DstChain = DstChain>
+ HasField<Tag, Value = PacketFilterConfig>,
SrcChain: CanReadOutgoingPacketFields<DstChain>
+ HasPortIdType<DstChain, PortId = PortId>
+ HasChannelIdType<DstChain, ChannelId = ChannelId>,
Chain: CanReadOutgoingPacketFields<Counterparty>
+ HasPortIdType<Counterparty, PortId = PortId>
+ HasChannelIdType<Counterparty, ChannelId = ChannelId>
+ HasField<Tag, Value = PacketFilterConfig>
+ HasErrorType,
{
async fn should_relay_packet(
relay: &Relay,
packet: &PacketOf<Relay>,
) -> Result<bool, Relay::Error> {
Ok(relay.get_field(PhantomData).is_allowed(
SrcChain::outgoing_packet_src_port(packet),
SrcChain::outgoing_packet_src_channel_id(packet),
async fn should_relay_outgoing_packet(
chain: &Chain,
packet: &Chain::OutgoingPacket,
) -> Result<bool, Chain::Error> {
Ok(chain.get_field(PhantomData).is_allowed(
Chain::outgoing_packet_src_port(packet),
Chain::outgoing_packet_src_channel_id(packet),
))
}
}

impl<Chain, Counterparty, Tag> IncomingPacketFilter<Chain, Counterparty>
for FilterPacketWithConfig<Tag>
where
Chain: Async
+ HasField<Tag, Value = PacketFilterConfig>
+ HasPortIdType<Counterparty, PortId = PortId>
+ HasChannelIdType<Counterparty, ChannelId = ChannelId>
+ HasErrorType,
Counterparty: CanReadOutgoingPacketFields<Chain>,
{
async fn should_relay_incoming_packet(
chain: &Chain,
packet: &Counterparty::OutgoingPacket,
) -> Result<bool, Chain::Error> {
Ok(chain.get_field(PhantomData).is_allowed(
Counterparty::outgoing_packet_dst_port(packet),
Counterparty::outgoing_packet_dst_channel_id(packet),
))
}
}
Loading

0 comments on commit be7d07f

Please sign in to comment.