Skip to content

Commit

Permalink
Abstract components to query connection end (#167)
Browse files Browse the repository at this point in the history
* Abstract connection end query

* Formatting

* Improve error handling in connection end query CLI

---------

Co-authored-by: Romain Ruetschi <[email protected]>
Co-authored-by: Soares Chen <[email protected]>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent 248fa3e commit 7b3720c
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 43 deletions.
1 change: 0 additions & 1 deletion crates/cli/cli/src/commands/query/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use hermes_cosmos_client_components::traits::chain_handle::HasBlockingChainHandl
use hermes_cosmos_relayer::contexts::builder::CosmosBuilder;
use hermes_cosmos_relayer::types::error::BaseError;
use hermes_relayer_components::chain::traits::queries::chain_status::CanQueryChainHeight;

use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::{
IncludeProof, PageRequest, QueryChannelsRequest, QueryClientStateRequest,
Expand Down
62 changes: 21 additions & 41 deletions crates/cli/cli/src/commands/query/connection/end.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use hermes_cli_framework::output::Output;
use oneline_eyre::eyre::eyre;
use tracing::error;
use tracing::info;

use hermes_cli_framework::command::CommandRunner;
use hermes_cosmos_client_components::traits::chain_handle::HasBlockingChainHandle;
use hermes_cli_framework::output::Output;
use hermes_cosmos_relayer::contexts::builder::CosmosBuilder;
use hermes_cosmos_relayer::contexts::chain::CosmosChain;
use hermes_cosmos_relayer::types::error::BaseError as RelayerError;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::QueryConnectionRequest;
use ibc_relayer::chain::requests::{IncludeProof, QueryHeight};
use hermes_relayer_components::chain::traits::queries::connection_end::CanQueryConnectionEnd;
use hermes_relayer_components::chain::traits::types::chain_id::HasChainId;
use ibc_relayer_types::core::ics03_connection::connection::State;
use ibc_relayer_types::core::ics24_host::identifier::ChainId;
use ibc_relayer_types::core::ics24_host::identifier::ConnectionId;
Expand Down Expand Up @@ -49,43 +46,26 @@ pub struct QueryConnectionEnd {
impl CommandRunner<CosmosBuilder> for QueryConnectionEnd {
async fn run(&self, builder: &CosmosBuilder) -> Result<Output> {
let chain = builder.build_chain(&self.chain_id).await?;
let chain_id = self.chain_id.clone();
let connection_id = self.connection_id.clone();
let height = self.height;

let connection_end = chain
.with_blocking_chain_handle(move |chain_handle| {
let query_height = if let Some(height) = height {
let specified_height = Height::new(chain_handle.id().version(), height)
.map_err(|e| RelayerError::generic(eyre!("Failed to create Height with revision number `{}` and revision height `{height}`. Error: {e}", chain_handle.id().version())))?;
let height = self.height.map(|h|
Height::new(chain.chain_id().version(), h)
.map_err(|e| RelayerError::generic(eyre!("Failed to create Height with revision number `{}` and revision height `{h}`. Error: {e}", chain.chain_id().version())))
).transpose()?;

QueryHeight::Specific(specified_height)
} else {
QueryHeight::Latest
};
let (connection_end, _) = chain_handle
.query_connection(
QueryConnectionRequest {
connection_id: connection_id.clone(),
height: query_height,
},
IncludeProof::No,
)
.map_err(|e| RelayerError::generic(eyre!("Failed to query connection with id `{connection_id}`. Error: {e}")))?;

if connection_end.state_matches(&State::Uninitialized) {
error!("Connection '{connection_id}' does not exist")
} else {
info!(
"Successfully queried connection end on chain `{}`",
chain_id,
);
}

Ok(connection_end)
})
let connection_end =
<CosmosChain as CanQueryConnectionEnd<CosmosChain>>::query_connection_end(
&chain,
&self.connection_id,
height.as_ref(),
)
.await?;

if connection_end.state_matches(&State::Uninitialized) {
return Ok(Output::error(format!(
"Connection '{}' does not exist",
self.connection_id
)));
}

Ok(Output::success(connection_end))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use hermes_relayer_components::chain::traits::queries::block::BlockQuerierCompon
use hermes_relayer_components::chain::traits::queries::chain_status::ChainStatusQuerierComponent;
use hermes_relayer_components::chain::traits::queries::client_state::ClientStateQuerierComponent;
use hermes_relayer_components::chain::traits::queries::client_state::ClientStatesQuerierComponent;
use hermes_relayer_components::chain::traits::queries::connection_end::ConnectionEndQuerierComponent;
use hermes_relayer_components::chain::traits::queries::consensus_state_height::ConsensusStateHeightQuerierComponent;
use hermes_relayer_components::chain::traits::queries::counterparty_chain_id::CounterpartyChainIdQuerierComponent;
use hermes_relayer_components::chain::traits::queries::packet_acknowledgements::PacketAcknowledgementsQuerierComponent;
Expand All @@ -43,7 +44,8 @@ use hermes_relayer_components::chain::traits::types::client_state::{
ClientStateDecoderComponent, ClientStateTypeComponent, ClientStatesDecoderComponent,
};
use hermes_relayer_components::chain::traits::types::connection::{
ConnectionHandshakePayloadTypeComponent, InitConnectionOptionsTypeComponent,
ConnectionEndTypeComponent, ConnectionHandshakePayloadTypeComponent,
InitConnectionOptionsTypeComponent,
};
use hermes_relayer_components::chain::traits::types::consensus_state::ConsensusStateTypeComponent;
use hermes_relayer_components::chain::traits::types::create_client::{
Expand Down Expand Up @@ -86,6 +88,7 @@ use crate::impls::queries::block::QueryCometBlock;
use crate::impls::queries::chain_id::QueryChainIdWithChainHandle;
use crate::impls::queries::chain_status::QueryChainStatusWithChainHandle;
use crate::impls::queries::client_state::QueryCosmosClientStateFromAbci;
use crate::impls::queries::connection_end::QueryCosmosConnectionEndFromChainHandle;
use crate::impls::queries::consensus_state_height::QueryConsensusStateHeightFromChainHandle;
use crate::impls::queries::packet_acknowledgements::QueryCosmosPacketAcknowledgements;
use crate::impls::queries::packet_commitments::QueryCosmosPacketCommitments;
Expand Down Expand Up @@ -117,6 +120,7 @@ delegate_components! {
MessageTypeComponent,
EventTypeComponent,
IbcChainTypesComponent,
ConnectionEndTypeComponent,
IbcPacketTypesProviderComponent,
ChainStatusTypeComponent,
BlockTypeComponent,
Expand Down Expand Up @@ -212,5 +216,7 @@ delegate_components! {
QueryCometBlock,
AbciQuerierComponent:
QueryAbci,
ConnectionEndQuerierComponent:
QueryCosmosConnectionEndFromChainHandle,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use cgp_core::HasErrorType;
use hermes_relayer_components::chain::traits::queries::connection_end::ConnectionEndQuerier;
use hermes_relayer_components::chain::traits::types::connection::HasConnectionEndType;
use hermes_relayer_components::chain::traits::types::ibc::HasIbcChainTypes;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::IncludeProof;
use ibc_relayer::chain::requests::QueryConnectionRequest;
use ibc_relayer::chain::requests::QueryHeight;
use ibc_relayer_types::core::ics03_connection::connection::ConnectionEnd;
use ibc_relayer_types::core::ics24_host::identifier::ConnectionId;
use ibc_relayer_types::Height;

use crate::traits::chain_handle::HasBlockingChainHandle;

pub struct QueryCosmosConnectionEndFromChainHandle;

impl<Chain, Counterparty> ConnectionEndQuerier<Chain, Counterparty>
for QueryCosmosConnectionEndFromChainHandle
where
Chain: HasConnectionEndType<Counterparty, ConnectionEnd = ConnectionEnd>
+ HasIbcChainTypes<Counterparty, Height = Height, ConnectionId = ConnectionId>
+ HasBlockingChainHandle
+ HasErrorType,
{
async fn query_connection_end(
chain: &Chain,
connection_id: &Chain::ConnectionId,
height: Option<&Chain::Height>,
) -> Result<Chain::ConnectionEnd, Chain::Error> {
let connection_id = connection_id.clone();
let height = height.cloned();
chain
.with_blocking_chain_handle(move |chain_handle| {
let query_height = if let Some(height) = height {
QueryHeight::Specific(height)
} else {
QueryHeight::Latest
};
let (connection_end, _) = chain_handle
.query_connection(
QueryConnectionRequest {
connection_id: connection_id.clone(),
height: query_height,
},
IncludeProof::No,
)
.map_err(Chain::raise_error)?;

Ok(connection_end)
})
.await
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod block;
pub mod chain_id;
pub mod chain_status;
pub mod client_state;
pub mod connection_end;
pub mod consensus_state;
pub mod consensus_state_height;
pub mod packet_acknowledgements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use hermes_relayer_components::chain::traits::types::block::{
};
use hermes_relayer_components::chain::traits::types::chain::HasChainTypes;
use hermes_relayer_components::chain::traits::types::chain_id::{HasChainId, ProvideChainIdType};
use hermes_relayer_components::chain::traits::types::connection::ProvideConnectionEndType;
use hermes_relayer_components::chain::traits::types::event::ProvideEventType;
use hermes_relayer_components::chain::traits::types::height::{
GenesisHeightGetter, HasHeightType, HeightIncrementer, ProvideHeightType,
Expand All @@ -19,6 +20,7 @@ use hermes_relayer_components::chain::traits::types::timestamp::{
HasTimestampType, ProvideTimestampType,
};
use ibc_relayer::chain::endpoint::ChainStatus;
use ibc_relayer_types::core::ics03_connection::connection::ConnectionEnd;
use ibc_relayer_types::core::ics04_channel::packet::{Packet, Sequence};
use ibc_relayer_types::core::ics24_host::identifier::{
ChainId, ChannelId, ClientId, ConnectionId, PortId,
Expand Down Expand Up @@ -149,3 +151,10 @@ where
&block_id.hash
}
}

impl<Chain, Counterparty> ProvideConnectionEndType<Chain, Counterparty> for ProvideCosmosChainTypes
where
Chain: Async,
{
type ConnectionEnd = ConnectionEnd;
}
4 changes: 4 additions & 0 deletions crates/cosmos/cosmos-relayer/src/chain/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use hermes_relayer_components::chain::traits::queries::block::BlockQuerierCompon
use hermes_relayer_components::chain::traits::queries::chain_status::ChainStatusQuerierComponent;
use hermes_relayer_components::chain::traits::queries::client_state::ClientStateQuerierComponent;
use hermes_relayer_components::chain::traits::queries::client_state::ClientStatesQuerierComponent;
use hermes_relayer_components::chain::traits::queries::connection_end::ConnectionEndQuerierComponent;
use hermes_relayer_components::chain::traits::queries::consensus_state::ConsensusStateQuerierComponent;
use hermes_relayer_components::chain::traits::queries::consensus_state_height::ConsensusStateHeightQuerierComponent;
use hermes_relayer_components::chain::traits::queries::counterparty_chain_id::CounterpartyChainIdQuerierComponent;
Expand All @@ -46,6 +47,7 @@ use hermes_relayer_components::chain::traits::types::channel::InitChannelOptions
use hermes_relayer_components::chain::traits::types::client_state::ClientStateDecoderComponent;
use hermes_relayer_components::chain::traits::types::client_state::ClientStateTypeComponent;
use hermes_relayer_components::chain::traits::types::client_state::ClientStatesDecoderComponent;
use hermes_relayer_components::chain::traits::types::connection::ConnectionEndTypeComponent;
use hermes_relayer_components::chain::traits::types::connection::ConnectionHandshakePayloadTypeComponent;
use hermes_relayer_components::chain::traits::types::connection::InitConnectionOptionsTypeComponent;
use hermes_relayer_components::chain::traits::types::consensus_state::ConsensusStateTypeComponent;
Expand Down Expand Up @@ -118,6 +120,8 @@ delegate_components! {
ClientStatesDecoderComponent,
ConsensusStateTypeComponent,
IbcChainTypesComponent,
ConnectionEndQuerierComponent,
ConnectionEndTypeComponent,
IbcPacketTypesProviderComponent,
ChainStatusTypeComponent,
BlockTypeComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use cgp_core::prelude::*;

use crate::chain::traits::types::connection::HasConnectionEndType;
use crate::chain::traits::types::ibc::HasIbcChainTypes;

#[derive_component(ConnectionEndQuerierComponent, ConnectionEndQuerier<Chain>)]
#[async_trait]
pub trait CanQueryConnectionEnd<Counterparty>:
HasConnectionEndType<Counterparty> + HasIbcChainTypes<Counterparty> + HasErrorType
{
async fn query_connection_end(
&self,
connection_id: &Self::ConnectionId,
height: Option<&Self::Height>,
) -> Result<Self::ConnectionEnd, Self::Error>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod block;
pub mod chain_status;
pub mod client_state;
pub mod client_status;
pub mod connection_end;
pub mod consensus_state;
pub mod consensus_state_height;
pub mod counterparty_chain_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ pub trait HasConnectionHandshakePayloadTypes<Counterparty>: Async {

type ConnectionOpenConfirmPayload: Async;
}

#[derive_component(ConnectionEndTypeComponent, ProvideConnectionEndType<Chain>)]
pub trait HasConnectionEndType<Counterparty>: Async {
type ConnectionEnd: Async;
}

0 comments on commit 7b3720c

Please sign in to comment.