Skip to content

Commit

Permalink
Generic create connection cli (#524)
Browse files Browse the repository at this point in the history
* Scaffold generic create connection command

* First stab at generic create connection command

* Call Chain::InitConnectionOptions correctly

* Cargo fmt

* Hook up generic create connection cli command

* Delete cosmos-specific create connection cli

* Addd some additional bounds to CommandRunner impl

* Fix compilation error

---------

Co-authored-by: Soares Chen <[email protected]>
  • Loading branch information
seanchen1991 and soareschen authored Jan 16, 2025
1 parent 9dd5e35 commit ab0513f
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 114 deletions.
146 changes: 146 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,146 @@
use core::fmt::Display;
use core::marker::PhantomData;

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::multi::types::index::Index;
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, HasOutputType};
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: HasOutputType + HasErrorType,
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: HasChainTypeAt<Index<0>, Chain = Chain>
+ HasChainTypeAt<Index<1>, Chain = Counterparty>
+ CanBuildRelay<Index<0>, Index<1>, Relay = Relay>
+ HasRelayTypeAt<Index<0>, Index<1>>,
Chain:
HasChainIdType + HasClientIdType<Counterparty> + HasInitConnectionOptionsType<Counterparty>,
Counterparty: HasChainIdType + HasClientIdType<Chain>,
Chain::InitConnectionOptions: Default,
Chain::ChainId: Display,
Chain::ClientId: Display,
Counterparty::ChainId: Display,
Counterparty::ClientId: Display,
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,
}
}
}
18 changes: 11 additions & 7 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 @@ -63,9 +66,7 @@ use hermes_cli_components::traits::bootstrap::{BootstrapLoaderComponent, Bootstr
use hermes_cli_components::traits::build::{
BuilderLoaderComponent, BuilderTypeComponent, CanLoadBuilder,
};
use hermes_cli_components::traits::command::{
CanRunCommand, CommandRunner, CommandRunnerComponent,
};
use hermes_cli_components::traits::command::{CanRunCommand, CommandRunnerComponent};
use hermes_cli_components::traits::config::config_path::ConfigPathGetterComponent;
use hermes_cli_components::traits::config::load_config::{CanLoadConfig, ConfigLoaderComponent};
use hermes_cli_components::traits::config::write_config::{CanWriteConfig, ConfigWriterComponent};
Expand Down Expand Up @@ -198,6 +199,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 +224,7 @@ delegate_components! {
QueryConsensusStateArgs: RunQueryConsensusStateCommand,

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

BootstrapSubCommand: RunBootstrapSubCommand,
Expand Down Expand Up @@ -297,6 +304,7 @@ pub trait CanUseHermesApp:
+ CanRunCommand<QueryChainStatusArgs>
+ CanRunCommand<QueryBalanceArgs>
+ CanRunCommand<CreateClientArgs>
+ CanRunCommand<CreateConnectionArgs>
+ CanRunCommand<UpdateClientArgs>
+ CanRunCommand<BootstrapChainArgs>
+ CanRunCommand<QueryChannelSubCommand>
Expand All @@ -309,7 +317,3 @@ pub trait CanUseHermesApp:
}

impl CanUseHermesApp for HermesApp {}

pub trait CanRunQueryClientsCommand: CommandRunner<HermesApp, QueryClientsArgs> {}

impl CanRunQueryClientsCommand for RunQueryClientsCommand {}
14 changes: 12 additions & 2 deletions crates/cosmos/cosmos-relayer/src/contexts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use hermes_relayer_components::build::traits::builders::birelay_from_relay_build
use hermes_relayer_components::build::traits::builders::chain_builder::ChainBuilder;
use hermes_relayer_components::build::traits::cache::{HasChainCache, HasRelayCache};
use hermes_relayer_components::multi::traits::birelay_at::BiRelayTypeAtComponent;
use hermes_relayer_components::multi::traits::chain_at::ChainTypeAtComponent;
use hermes_relayer_components::multi::traits::relay_at::RelayTypeAtComponent;
use hermes_relayer_components::multi::traits::chain_at::{ChainTypeAtComponent, HasChainTypeAt};
use hermes_relayer_components::multi::traits::relay_at::{HasRelayTypeAt, RelayTypeAtComponent};
use hermes_relayer_components::multi::types::index::Index;
use hermes_relayer_components::multi::types::tags::{Dst, Src};
use hermes_relayer_components::relay::traits::target::SourceTarget;
Expand Down Expand Up @@ -386,3 +386,13 @@ impl HasBatchConfig for CosmosBuilder {
&self.batch_config
}
}

pub trait CanUseCosmosBuilder:
HasChainTypeAt<Index<0>, Chain = CosmosChain>
+ HasChainTypeAt<Index<1>, Chain = CosmosChain>
+ HasRelayTypeAt<Index<0>, Index<1>, Relay = CosmosRelay>
+ HasRelayTypeAt<Index<1>, Index<0>, Relay = CosmosRelay>
{
}

impl CanUseCosmosBuilder for CosmosBuilder {}

0 comments on commit ab0513f

Please sign in to comment.