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

Implement create client CLI #227

Merged
merged 9 commits into from
Jan 24, 2025
372 changes: 305 additions & 67 deletions relayer/Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ hermes-wasm-client-components = { version = "0.1.0" }
hermes-wasm-test-components = { version = "0.1.0" }
hermes-wasm-encoding-components = { version = "0.1.0" }
hermes-cli-components = { version = "0.1.0" }
hermes-cli = { version = "0.1.0" }
hermes-test-components = { version = "0.1.0" }
hermes-cairo-encoding-components = { version = "0.1.0" }
hermes-protobuf-encoding-components = { version = "0.1.0" }
Expand Down Expand Up @@ -163,3 +164,4 @@ hermes-wasm-test-components = { git = "https://github.com/informalsyste
hermes-wasm-client-components = { git = "https://github.com/informalsystems/hermes-sdk.git" }
hermes-wasm-encoding-components = { git = "https://github.com/informalsystems/hermes-sdk.git" }
hermes-cli-components = { git = "https://github.com/informalsystems/hermes-sdk.git" }
hermes-cli = { git = "https://github.com/informalsystems/hermes-sdk.git" }
10 changes: 9 additions & 1 deletion relayer/crates/starknet-chain-context/src/contexts/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ use hermes_relayer_components::chain::traits::types::connection::{
HasInitConnectionOptionsType,
};
use hermes_relayer_components::chain::traits::types::consensus_state::HasConsensusStateType;
use hermes_relayer_components::chain::traits::types::create_client::HasCreateClientEvent;
use hermes_relayer_components::chain::traits::types::create_client::{
HasCreateClientEvent, HasCreateClientMessageOptionsType, HasCreateClientPayloadOptionsType,
};
use hermes_relayer_components::chain::traits::types::event::HasEventType;
use hermes_relayer_components::chain::traits::types::ibc::{
HasChannelIdType, HasClientIdType, HasConnectionIdType, HasCounterpartyMessageHeight,
Expand Down Expand Up @@ -152,6 +154,7 @@ use hermes_starknet_chain_components::types::cosmos::consensus_state::CometConse
use hermes_starknet_chain_components::types::cosmos::update::CometUpdateHeader;
use hermes_starknet_chain_components::types::event::StarknetEvent;
use hermes_starknet_chain_components::types::message_response::StarknetMessageResponse;
use hermes_starknet_chain_components::types::payloads::client::StarknetCreateClientPayloadOptions;
use hermes_starknet_test_components::impls::types::wallet::ProvideStarknetWalletType;
use hermes_test_components::chain::traits::queries::balance::CanQueryBalance;
use hermes_test_components::chain::traits::types::address::HasAddressType;
Expand Down Expand Up @@ -334,6 +337,11 @@ pub trait CanUseStarknetChain:
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
+ HasCreateClientEvent<CosmosChain>
+ CanBuildCreateClientPayload<CosmosChain>
+ HasCreateClientMessageOptionsType<CosmosChain, CreateClientMessageOptions = ()>
+ HasCreateClientPayloadOptionsType<
CosmosChain,
CreateClientPayloadOptions = StarknetCreateClientPayloadOptions,
> + CanBuildCreateClientPayload<CosmosChain>
+ CanBuildCreateClientMessage<CosmosChain>
+ CanBuildUpdateClientPayload<CosmosChain>
+ CanBuildUpdateClientMessage<CosmosChain>
Expand Down
2 changes: 2 additions & 0 deletions relayer/crates/starknet-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cgp = { workspace = true }
cgp-error-eyre = { workspace = true }
hermes-any-counterparty = { workspace = true }
hermes-cli-components = { workspace = true }
hermes-cli = { workspace = true }
hermes-error = { workspace = true }
hermes-runtime = { workspace = true }
hermes-runtime-components = { workspace = true }
Expand All @@ -30,6 +31,7 @@ hermes-cosmos-relayer = { workspace = true }
hermes-cosmos-integration-tests = { workspace = true }
hermes-cosmos-chain-components = { workspace = true }
hermes-starknet-chain-components = { workspace = true }
hermes-starknet-chain-context = { workspace = true }
hermes-starknet-relayer = { workspace = true }
hermes-starknet-integration-tests = { workspace = true }
hermes-starknet-test-components = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions relayer/crates/starknet-cli/bin/hermes_starknet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![recursion_limit = "256"]

use std::path::PathBuf;
use std::sync::Arc;

Expand Down
1 change: 1 addition & 0 deletions relayer/crates/starknet-cli/src/commands/create/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod subcommand;
23 changes: 23 additions & 0 deletions relayer/crates/starknet-cli/src/commands/create/subcommand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use hermes_cli::commands::client::create::CreateClientArgs;
use hermes_cli_components::traits::command::{CanRunCommand, CommandRunner};

#[derive(Debug, clap::Subcommand)]
pub enum CreateSubCommand {
Client(CreateClientArgs),
}

pub struct RunCreateSubCommand;

impl<App> CommandRunner<App, CreateSubCommand> for RunCreateSubCommand
where
App: CanRunCommand<CreateClientArgs>,
{
async fn run_command(
app: &App,
subcommand: &CreateSubCommand,
) -> Result<App::Output, App::Error> {
match subcommand {
CreateSubCommand::Client(args) => app.run_command(args).await,
}
}
}
3 changes: 2 additions & 1 deletion relayer/crates/starknet-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod client;
pub mod create;
pub mod query;
pub mod update;
1 change: 1 addition & 0 deletions relayer/crates/starknet-cli/src/commands/update/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod subcommand;
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ use hermes_cli_components::impls::commands::client::update::UpdateClientArgs;
use hermes_cli_components::traits::command::{CanRunCommand, CommandRunner};

#[derive(Debug, clap::Subcommand)]
pub enum ClientSubCommand {
UpdateClient(UpdateClientArgs),
pub enum UpdateSubCommand {
Client(UpdateClientArgs),
}

pub struct RunClientSubCommand;
pub struct RunUpdateSubCommand;

impl<App> CommandRunner<App, ClientSubCommand> for RunClientSubCommand
impl<App> CommandRunner<App, UpdateSubCommand> for RunUpdateSubCommand
where
App: CanRunCommand<UpdateClientArgs>,
{
async fn run_command(
app: &App,
subcommand: &ClientSubCommand,
subcommand: &UpdateSubCommand,
) -> Result<App::Output, App::Error> {
match subcommand {
ClientSubCommand::UpdateClient(args) => app.run_command(args).await,
UpdateSubCommand::Client(args) => app.run_command(args).await,
}
}
}
70 changes: 66 additions & 4 deletions relayer/crates/starknet-cli/src/contexts/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use std::path::PathBuf;

use cgp::core::component::UseDelegate;
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeComponent};
use cgp::core::field::WithField;
use cgp::core::field::{Index, WithField};
use cgp::core::types::WithType;
use cgp::prelude::*;
use hermes_cli::commands::client::create::CreateClientArgs;
use hermes_cli_components::impls::commands::bootstrap::chain::RunBootstrapChainCommand;
use hermes_cli_components::impls::commands::client::create::{
CreateClientOptionsParser, RunCreateClientCommand,
};
use hermes_cli_components::impls::commands::client::update::{
RunUpdateClientCommand, UpdateClientArgs,
};
Expand Down Expand Up @@ -43,6 +47,7 @@ use hermes_cli_components::traits::output::{
};
use hermes_cli_components::traits::parse::ArgParserComponent;
use hermes_cli_components::traits::types::config::ConfigTypeComponent;
use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_relayer::contexts::chain::CosmosChain;
use hermes_error::traits::wrap::CanWrapError;
use hermes_error::types::HermesError;
Expand All @@ -59,6 +64,8 @@ use hermes_starknet_chain_components::impls::types::config::{
StarknetChainConfig, StarknetRelayerConfig,
};
use hermes_starknet_chain_components::types::client_id::ClientId;
use hermes_starknet_chain_components::types::payloads::client::StarknetCreateClientPayloadOptions;
use hermes_starknet_chain_context::contexts::chain::StarknetChain;
use hermes_starknet_integration_tests::contexts::bootstrap::StarknetBootstrap;
use hermes_starknet_integration_tests::contexts::chain_driver::StarknetChainDriver;
use hermes_starknet_relayer::contexts::builder::StarknetBuilder;
Expand All @@ -68,8 +75,9 @@ use ibc::core::host::types::identifiers::{ChainId, ClientId as CosmosClientId};
use starknet::core::types::Felt;
use toml::to_string_pretty;

use crate::commands::client::subcommand::{ClientSubCommand, RunClientSubCommand};
use crate::commands::create::subcommand::{CreateSubCommand, RunCreateSubCommand};
use crate::commands::query::subcommand::{QuerySubCommand, RunQuerySubCommand};
use crate::commands::update::subcommand::{RunUpdateSubCommand, UpdateSubCommand};
use crate::impls::bootstrap::starknet_chain::{BootstrapStarknetChainArgs, LoadStarknetBootstrap};
use crate::impls::bootstrap::subcommand::{BootstrapSubCommand, RunBootstrapSubCommand};
use crate::impls::build::LoadStarknetBuilder;
Expand Down Expand Up @@ -156,6 +164,9 @@ delegate_components! {
(UpdateClientArgs, symbol!("client_id")): ParseFromString<ClientId>,
(UpdateClientArgs, symbol!("counterparty_client_id")): ParseFromString<CosmosClientId>,
(UpdateClientArgs, symbol!("target_height")): ParseFromOptionalString<Height>,

(CreateClientArgs, symbol!("target_chain_id")): ParseFromString<ChainId>,
(CreateClientArgs, symbol!("counterparty_chain_id")): ParseFromString<ChainId>,
}
}

Expand All @@ -170,8 +181,11 @@ delegate_components! {
QueryChainStatusArgs: RunQueryChainStatusCommand,
QueryBalanceArgs: RunQueryBalanceCommand,

ClientSubCommand: RunClientSubCommand,
CreateSubCommand: RunCreateSubCommand,
UpdateSubCommand: RunUpdateSubCommand,

UpdateClientArgs: RunUpdateClientCommand,
CreateClientArgs: RunCreateClientCommand,

BootstrapStarknetChainArgs: RunBootstrapChainCommand<UpdateStarknetConfig>,
}
Expand Down Expand Up @@ -218,6 +232,52 @@ impl ConfigUpdater<StarknetChainDriver, StarknetRelayerConfig> for UpdateStarkne
}
}

impl CreateClientOptionsParser<StarknetApp, CreateClientArgs, Index<0>, Index<1>>
for StarknetAppComponents
{
async fn parse_create_client_options(
_app: &StarknetApp,
args: &CreateClientArgs,
_target_chain: &StarknetChain,
counterparty_chain: &CosmosChain,
) -> Result<((), CosmosCreateClientOptions), HermesError> {
let max_clock_drift = match args.clock_drift.map(|d| d.into()) {
Some(input) => input,
None => {
counterparty_chain.chain_config.clock_drift
+ counterparty_chain.chain_config.max_block_time
}
};

let settings = CosmosCreateClientOptions {
max_clock_drift,
trusting_period: args.trusting_period.map(|d| d.into()).unwrap_or_default(),
trust_threshold: args
.trust_threshold
.map(|threshold| threshold.into())
.unwrap_or_default(),
};

Ok(((), settings))
}
}

// TODO(seanchen1991): Implement Cosmos-to-Starknet client creation
pub struct CreateCosmosClientOnStarknetArgs;

impl CreateClientOptionsParser<StarknetApp, CreateCosmosClientOnStarknetArgs, Index<1>, Index<0>>
for StarknetAppComponents
{
async fn parse_create_client_options(
_app: &StarknetApp,
_args: &CreateCosmosClientOnStarknetArgs,
_target_chain: &CosmosChain,
_counterparty_chain: &StarknetChain,
) -> Result<((), StarknetCreateClientPayloadOptions), HermesError> {
todo!()
}
}

pub trait CanUseStarknetApp:
HasRuntime
+ HasLogger
Expand All @@ -235,8 +295,10 @@ pub trait CanUseStarknetApp:
+ CanRunCommand<QueryClientStateArgs>
+ CanRunCommand<QueryConsensusStateArgs>
+ CanRunCommand<QueryBalanceArgs>
+ CanRunCommand<ClientSubCommand>
+ CanRunCommand<CreateSubCommand>
+ CanRunCommand<UpdateSubCommand>
+ CanRunCommand<UpdateClientArgs>
+ CanRunCommand<CreateClientArgs>
{
}

Expand Down
13 changes: 12 additions & 1 deletion relayer/crates/starknet-cli/src/impls/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use hermes_cli_components::traits::command::{CanRunCommand, CommandRunner};

use crate::commands::create::subcommand::CreateSubCommand;
use crate::commands::query::subcommand::QuerySubCommand;
use crate::commands::update::subcommand::UpdateSubCommand;
use crate::impls::bootstrap::subcommand::BootstrapSubCommand;

#[derive(Debug, clap::Subcommand)]
Expand All @@ -9,13 +11,20 @@ pub enum AllSubCommands {
Bootstrap(BootstrapSubCommand),
#[clap(subcommand)]
Query(QuerySubCommand),
#[clap(subcommand)]
Create(CreateSubCommand),
#[clap(subcommand)]
Update(UpdateSubCommand),
}

pub struct RunAllSubCommand;

impl<App> CommandRunner<App, AllSubCommands> for RunAllSubCommand
where
App: CanRunCommand<BootstrapSubCommand> + CanRunCommand<QuerySubCommand>,
App: CanRunCommand<BootstrapSubCommand>
+ CanRunCommand<QuerySubCommand>
+ CanRunCommand<CreateSubCommand>
+ CanRunCommand<UpdateSubCommand>,
{
async fn run_command(
app: &App,
Expand All @@ -24,6 +33,8 @@ where
match subcommand {
AllSubCommands::Bootstrap(args) => app.run_command(args).await,
AllSubCommands::Query(args) => app.run_command(args).await,
AllSubCommands::Create(args) => app.run_command(args).await,
AllSubCommands::Update(args) => app.run_command(args).await,
}
}
}
Loading