Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic create connection cli #524

Merged
merged 9 commits into from
Jan 16, 2025
145 changes: 145 additions & 0 deletions crates/cli/cli-components/src/impls/commands/connection/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use core::fmt::Display;
use core::marker::PhantomData;

use cgp::core::field::Index;
use cgp::prelude::*;
use hermes_logging_components::traits::has_logger::HasLogger;
use hermes_logging_components::traits::logger::CanLog;
use hermes_logging_components::types::level::LevelInfo;
use hermes_relayer_components::build::traits::builders::relay_builder::CanBuildRelay;
use hermes_relayer_components::chain::traits::types::chain_id::HasChainIdType;
use hermes_relayer_components::chain::traits::types::connection::HasInitConnectionOptionsType;
use hermes_relayer_components::chain::traits::types::ibc::HasClientIdType;
use hermes_relayer_components::multi::traits::chain_at::HasChainTypeAt;
use hermes_relayer_components::multi::traits::relay_at::HasRelayTypeAt;
use hermes_relayer_components::relay::impls::connection::bootstrap::CanBootstrapConnection;
use hermes_relayer_components::relay::traits::chains::HasRelayChains;

use crate::traits::build::CanLoadBuilder;
use crate::traits::command::CommandRunner;
use crate::traits::output::CanProduceOutput;
use crate::traits::parse::CanParseArg;

pub struct RunCreateConnectionCommand;

#[derive(Debug, clap::Parser, HasField)]
pub struct CreateConnectionArgs {
#[clap(
long = "target-chain-id",
required = true,
value_name = "TARGET_CHAIN_ID",
help_heading = "REQUIRED"
)]
target_chain_id: String,

#[clap(
long = "target-client-id",
required = true,
value_name = "TARGET_CLIENT_ID",
help_heading = "REQUIRED"
)]
target_client_id: String,

#[clap(
long = "counterparty-chain-id",
required = true,
value_name = "COUNTERPARTY_CHAIN_ID",
help_heading = "REQUIRED"
)]
counterparty_chain_id: String,

#[clap(
long = "counterparty-client-id",
required = true,
value_name = "COUNTERPARTY_CLIENT_ID",
help_heading = "REQUIRED"
)]
counterparty_client_id: String,
}

impl<App, Args, Builder, Chain, Counterparty, Relay> CommandRunner<App, Args>
for RunCreateConnectionCommand
where
App: CanLoadBuilder<Builder = Builder>
+ HasLogger
+ CanProduceOutput<&'static str>
+ CanRaiseError<Builder::Error>
+ CanRaiseError<Relay::Error>
+ CanParseArg<Args, symbol!("target_chain_id"), Parsed = Chain::ChainId>
+ CanParseArg<Args, symbol!("counterparty_chain_id"), Parsed = Counterparty::ChainId>
+ CanParseArg<Args, symbol!("target_client_id"), Parsed = Chain::ClientId>
+ CanParseArg<Args, symbol!("counterparty_client_id"), Parsed = Counterparty::ClientId>,
App::Logger: CanLog<LevelInfo>,
Builder: CanBuildRelay<Index<0>, Index<1>, Relay = Relay>
seanchen1991 marked this conversation as resolved.
Show resolved Hide resolved
+ HasChainTypeAt<Index<0>, Chain = Chain>
+ HasChainTypeAt<Index<1>, Chain = Counterparty>
+ HasRelayTypeAt<Index<0>, Index<1>>,
Chain:
HasChainIdType + HasClientIdType<Counterparty> + HasInitConnectionOptionsType<Counterparty>,
Chain::InitConnectionOptions: Default,
Chain::ChainId: Display,
Chain::ClientId: Display,
Counterparty::ChainId: Display,
Counterparty::ClientId: Display,
Counterparty: HasChainIdType + HasClientIdType<Chain>,
Relay: CanBootstrapConnection + HasRelayChains<SrcChain = Chain, DstChain = Counterparty>,
Args: Async,
{
async fn run_command(app: &App, args: &Args) -> Result<App::Output, App::Error> {
let logger = app.logger();
let builder = app.load_builder().await?;

let target_chain_id = app.parse_arg(args, PhantomData::<symbol!("target_chain_id")>)?;
let target_client_id = app.parse_arg(args, PhantomData::<symbol!("target_client_id")>)?;
let counterparty_chain_id =
app.parse_arg(args, PhantomData::<symbol!("counterparty_chain_id")>)?;
let counterparty_client_id =
app.parse_arg(args, PhantomData::<symbol!("counterparty_client_id")>)?;

logger
.log(
&format!(
"Creating connection between {}:{} and {}:{}...",
target_chain_id,
target_client_id,
counterparty_chain_id,
counterparty_client_id
),
&LevelInfo,
)
.await;

let relay = builder
.build_relay(
PhantomData::<(Index<0>, Index<1>)>,
&target_chain_id,
&counterparty_chain_id,
&target_client_id,
&counterparty_client_id,
)
.await
.map_err(App::raise_error)?;

let (target_connection_id, counterparty_connection_id) = relay
.bootstrap_connection(&Default::default())
.await
.map_err(App::raise_error)?;

logger
.log(
&format!(
"Connection {}:{} successfully created between {}:{} and {}:{}",
target_connection_id,
counterparty_connection_id,
target_chain_id,
target_client_id,
counterparty_chain_id,
counterparty_client_id,
),
&LevelInfo,
)
.await;

Ok(app.produce_output("Done"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod create;
1 change: 1 addition & 0 deletions crates/cli/cli-components/src/impls/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod bootstrap;
pub mod client;
pub mod connection;
pub mod queries;
pub mod start;
101 changes: 0 additions & 101 deletions crates/cli/cli/src/commands/connection/create.rs

This file was deleted.

11 changes: 7 additions & 4 deletions crates/cli/cli/src/commands/connection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
mod create;
pub use create::ConnectionCreate;
// mod create;
// pub use create::ConnectionCreate;

use hermes_cli_components::impls::commands::connection::create::CreateConnectionArgs;
use hermes_cli_components::traits::command::CanRunCommand;
use hermes_cli_framework::command::CommandRunner;
use hermes_cli_framework::output::Output;

Expand All @@ -9,13 +12,13 @@ use crate::Result;
#[derive(Debug, clap::Subcommand)]
pub enum ConnectionCommands {
/// Create a new connection
Create(ConnectionCreate),
Create(CreateConnectionArgs),
}

impl CommandRunner<HermesApp> for ConnectionCommands {
async fn run(&self, app: &HermesApp) -> Result<Output> {
match self {
Self::Create(cmd) => cmd.run(app).await,
Self::Create(cmd) => app.run_command(cmd).await,
}
}
}
10 changes: 10 additions & 0 deletions crates/cli/cli/src/contexts/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use hermes_cli_components::impls::commands::client::create::{
use hermes_cli_components::impls::commands::client::update::{
RunUpdateClientCommand, UpdateClientArgs,
};
use hermes_cli_components::impls::commands::connection::create::{
CreateConnectionArgs, RunCreateConnectionCommand,
};
use hermes_cli_components::impls::commands::queries::balance::{
QueryBalanceArgs, RunQueryBalanceCommand,
};
Expand Down Expand Up @@ -198,6 +201,11 @@ delegate_components! {
(CreateClientArgs, symbol!("target_chain_id")): ParseFromString<ChainId>,
(CreateClientArgs, symbol!("counterparty_chain_id")): ParseFromString<ChainId>,

(CreateConnectionArgs, symbol!("target_chain_id")): ParseFromString<ChainId>,
(CreateConnectionArgs, symbol!("target_client_id")): ParseFromString<ClientId>,
(CreateConnectionArgs, symbol!("counterparty_chain_id")): ParseFromString<ChainId>,
(CreateConnectionArgs, symbol!("counterparty_client_id")): ParseFromString<ClientId>,

(UpdateClientArgs, symbol!("host_chain_id")): ParseFromString<ChainId>,
(UpdateClientArgs, symbol!("client_id")): ParseFromString<ClientId>,
(UpdateClientArgs, symbol!("counterparty_client_id")): ParseFromString<ClientId>,
Expand All @@ -218,6 +226,7 @@ delegate_components! {
QueryConsensusStateArgs: RunQueryConsensusStateCommand,

CreateClientArgs: RunCreateClientCommand,
CreateConnectionArgs: RunCreateConnectionCommand,
UpdateClientArgs: RunUpdateClientCommand,

BootstrapSubCommand: RunBootstrapSubCommand,
Expand Down Expand Up @@ -297,6 +306,7 @@ pub trait CanUseHermesApp:
+ CanRunCommand<QueryChainStatusArgs>
+ CanRunCommand<QueryBalanceArgs>
+ CanRunCommand<CreateClientArgs>
+ CanRunCommand<CreateConnectionArgs>
+ CanRunCommand<UpdateClientArgs>
+ CanRunCommand<BootstrapChainArgs>
+ CanRunCommand<QueryChannelSubCommand>
Expand Down
Loading