From 43c931b783222b4d6ecfc3c13b2705e847230c72 Mon Sep 17 00:00:00 2001 From: Amidamaru Date: Thu, 15 Aug 2024 16:34:58 +0700 Subject: [PATCH] Implement generic CLI query connection end --- .../src/impls/commands/queries/connection.rs | 24 +++++ .../impls/commands/queries/connection_end.rs | 89 +++++++++++++++++++ .../src/impls/commands/queries/mod.rs | 2 + crates/cli/cli/src/commands/query/mod.rs | 7 +- 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 crates/cli/cli-components/src/impls/commands/queries/connection.rs create mode 100644 crates/cli/cli-components/src/impls/commands/queries/connection_end.rs diff --git a/crates/cli/cli-components/src/impls/commands/queries/connection.rs b/crates/cli/cli-components/src/impls/commands/queries/connection.rs new file mode 100644 index 000000000..8b6f6bcfb --- /dev/null +++ b/crates/cli/cli-components/src/impls/commands/queries/connection.rs @@ -0,0 +1,24 @@ +use crate::impls::commands::queries::connection_end::QueryConnectionEndArgs; +use crate::traits::command::{CanRunCommand, CommandRunner}; + +pub struct RunQueryConnectionSubCommand; + +#[derive(Debug, clap::Subcommand)] +pub enum QueryConnectionSubCommand { + /// Query the connection end + End(QueryConnectionEndArgs), +} + +impl CommandRunner for RunQueryConnectionSubCommand +where + App: CanRunCommand, +{ + async fn run_command( + app: &App, + subcommand: &QueryConnectionSubCommand, + ) -> Result { + match subcommand { + QueryConnectionSubCommand::End(args) => app.run_command(args).await, + } + } +} diff --git a/crates/cli/cli-components/src/impls/commands/queries/connection_end.rs b/crates/cli/cli-components/src/impls/commands/queries/connection_end.rs new file mode 100644 index 000000000..a20674d9d --- /dev/null +++ b/crates/cli/cli-components/src/impls/commands/queries/connection_end.rs @@ -0,0 +1,89 @@ +use core::marker::PhantomData; + +use cgp_core::prelude::*; +use hermes_relayer_components::build::traits::builders::chain_builder::CanBuildChain; +use hermes_relayer_components::chain::traits::queries::chain_status::CanQueryChainHeight; +use hermes_relayer_components::chain::traits::queries::connection_end::CanQueryConnectionEnd; +use hermes_relayer_components::chain::traits::types::chain_id::HasChainId; +use hermes_relayer_components::chain::traits::types::connection::HasConnectionEndType; +use hermes_relayer_components::chain::traits::types::height::HasHeightType; +use hermes_relayer_components::multi::traits::chain_at::HasChainTypeAt; +use hermes_relayer_components::multi::types::index::Index; + +use crate::traits::build::CanLoadBuilder; +use crate::traits::command::CommandRunner; +use crate::traits::output::CanProduceOutput; +use crate::traits::parse::CanParseArg; + +pub struct RunQueryConnectionEndCommand; + +#[derive(Debug, clap::Parser, HasField)] +pub struct QueryConnectionEndArgs { + #[clap( + long = "chain", + required = true, + value_name = "CHAIN_ID", + help_heading = "REQUIRED", + help = "Identifier of the chain to query" + )] + chain_id: String, + + #[clap( + long = "connection", + visible_alias = "conn", + required = true, + value_name = "CONNECTION_ID", + help_heading = "REQUIRED", + help = "Identifier of the connection to query" + )] + connection_id: String, + + #[clap( + long = "height", + value_name = "HEIGHT", + help = "Height of the state to query. Leave unspecified for latest height." + )] + height: Option, +} + +impl CommandRunner + for RunQueryConnectionEndCommand +where + App: CanLoadBuilder + + CanProduceOutput + + CanParseArg + + CanParseArg + + CanParseArg> + + CanRaiseError + + CanRaiseError + + CanRaiseError, + Build: CanBuildChain<0, Chain = Chain> + HasChainTypeAt<1, Chain = Counterparty>, + Chain: CanQueryChainHeight + CanQueryConnectionEnd, + Counterparty: HasHeightType + HasChainId + HasConnectionEndType, + Args: Async, +{ + async fn run_command(app: &App, args: &Args) -> Result { + let chain_id = app.parse_arg(args, PhantomData::)?; + let connection_id = app.parse_arg(args, PhantomData::)?; + let m_query_height = app.parse_arg(args, PhantomData::)?; + + let builder = app.load_builder().await?; + + let chain = builder + .build_chain(Index::<0>, &chain_id) + .await + .map_err(App::raise_error)?; + + let query_height = match m_query_height { + Some(query_height) => query_height, + None => chain.query_chain_height().await.map_err(App::raise_error)?, + }; + + let connection_end = chain + .query_connection_end(&connection_id, &query_height) + .await + .map_err(App::raise_error)?; + + Ok(app.produce_output(connection_end)) + } +} diff --git a/crates/cli/cli-components/src/impls/commands/queries/mod.rs b/crates/cli/cli-components/src/impls/commands/queries/mod.rs index 3ca5ea222..445c82920 100644 --- a/crates/cli/cli-components/src/impls/commands/queries/mod.rs +++ b/crates/cli/cli-components/src/impls/commands/queries/mod.rs @@ -1,4 +1,6 @@ pub mod client; pub mod client_state; pub mod client_status; +pub mod connection; +pub mod connection_end; pub mod consensus_state; diff --git a/crates/cli/cli/src/commands/query/mod.rs b/crates/cli/cli/src/commands/query/mod.rs index 7294df484..c66ab8cf5 100644 --- a/crates/cli/cli/src/commands/query/mod.rs +++ b/crates/cli/cli/src/commands/query/mod.rs @@ -1,6 +1,3 @@ -mod connection; -pub use connection::QueryConnection; - mod clients; pub use clients::QueryClients; @@ -41,7 +38,7 @@ pub enum QueryCommands { /// Query connection information #[clap(subcommand)] - Connection(QueryConnection), + Connection(QueryConnectionSubCommand), /// Query channel information #[clap(subcommand)] @@ -57,7 +54,7 @@ impl CommandRunner for QueryCommands { match self { Self::Client(cmd) => app.run_command(cmd).await, Self::Clients(cmd) => cmd.run(app).await, - Self::Connection(cmd) => cmd.run(app).await, + Self::Connection(cmd) => app.run_command(cmd).await, Self::Connections(cmd) => cmd.run(app).await, Self::Channels(cmd) => cmd.run(app).await, Self::Channel(cmd) => cmd.run(app).await,