Skip to content

Commit

Permalink
Use CosmosCreateClientOptions instead of ibc-relayer Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ljoss17 committed Nov 28, 2024
1 parent 8770505 commit f30a44c
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use core::time::Duration;
use std::sync::Arc;

use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig;
use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_integration_tests::contexts::binary_channel::setup::CosmosBinaryChannelSetup;
use hermes_cosmos_integration_tests::contexts::bootstrap::CosmosBootstrap;
use hermes_cosmos_integration_tests::init::init_test_runtime;
use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_error::types::Error;
use hermes_ibc_test_suite::tests::transfer::TestIbcTransfer;
use hermes_test_components::setup::traits::run_test::CanRunTest;
use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer_types::core::ics02_client::trust_threshold::TrustThreshold;
use ibc_relayer_types::core::ics24_host::identifier::PortId;

// FIXME: Celestia currently can only be bootstrapped using CosmosBootstrap.
Expand Down Expand Up @@ -53,10 +52,10 @@ fn celestia_integration_tests() -> Result<(), Error> {
dynamic_gas: Some(DynamicGasConfig::default()),
});

let create_client_settings = Settings {
let create_client_settings = CosmosCreateClientOptions {
max_clock_drift: Duration::from_secs(40),
trusting_period: Some(Duration::from_secs(60 * 60)),
trust_threshold: TrustThreshold::ONE_THIRD,
trusting_period: Duration::from_secs(60 * 60),
..Default::default()
};

let setup = CosmosBinaryChannelSetup {
Expand Down
26 changes: 19 additions & 7 deletions crates/cli/cli/src/contexts/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use hermes_cli_components::traits::output::{
use hermes_cli_components::traits::parse::ArgParserComponent;
use hermes_cli_components::traits::types::config::ConfigTypeComponent;
use hermes_cli_framework::output::Output;
use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_integration_tests::contexts::bootstrap::CosmosBootstrap;
use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_cosmos_relayer::contexts::chain::CosmosChain;
Expand All @@ -56,7 +57,6 @@ use hermes_runtime::types::runtime::HermesRuntime;
use hermes_runtime_components::traits::runtime::{
ProvideDefaultRuntimeField, RuntimeGetterComponent, RuntimeTypeComponent,
};
use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::CreateOptions;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ClientId};
Expand Down Expand Up @@ -192,18 +192,30 @@ impl CreateClientOptionsParser<HermesApp, CreateClientArgs, 0, 1> for HermesAppC
args: &CreateClientArgs,
target_chain: &CosmosChain,
counterparty_chain: &CosmosChain,
) -> Result<((), Settings), Error> {
) -> Result<((), CosmosCreateClientOptions), Error> {
let options = CreateOptions {
max_clock_drift: args.clock_drift.map(|d| d.into()),
trusting_period: args.trusting_period.map(|d| d.into()),
trust_threshold: args.trust_threshold,
};

let settings = Settings::for_create_command(
options,
&target_chain.chain_config.clone(),
&counterparty_chain.chain_config.clone(),
);
let max_clock_drift = match options.max_clock_drift {
Some(input) => input,
None => {
target_chain.chain_config.clock_drift
+ counterparty_chain.chain_config.clock_drift
+ counterparty_chain.chain_config.max_block_time
}
};

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

Ok(((), settings))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use hermes_relayer_components::chain::traits::types::create_client::{
HasCreateClientPayloadOptionsType, HasCreateClientPayloadType,
};

use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer_types::clients::ics07_tendermint::client_state::{AllowUpdate, ClientState};
use ibc_relayer_types::clients::ics07_tendermint::consensus_state::ConsensusState;
use ibc_relayer_types::clients::ics07_tendermint::error::Error as TendermintClientError;
Expand All @@ -27,7 +26,7 @@ use tendermint_rpc::Error as TendermintRpcError;

use crate::traits::rpc_client::HasRpcClient;
use crate::traits::unbonding_period::CanQueryUnbondingPeriod;
use crate::types::payloads::client::CosmosCreateClientPayload;
use crate::types::payloads::client::{CosmosCreateClientOptions, CosmosCreateClientPayload};
use crate::types::status::ChainStatus;
use crate::types::tendermint::TendermintClientState;

Expand All @@ -37,39 +36,42 @@ impl<Chain, Counterparty> CreateClientPayloadBuilder<Chain, Counterparty>
for BuildCosmosCreateClientPayload
where
Chain: HasRpcClient
+ HasCreateClientPayloadOptionsType<Counterparty, CreateClientPayloadOptions = Settings>
+ HasCreateClientPayloadType<Counterparty, CreateClientPayload = CosmosCreateClientPayload>
+ HasCreateClientPayloadOptionsType<
Counterparty,
CreateClientPayloadOptions = CosmosCreateClientOptions,
> + HasCreateClientPayloadType<Counterparty, CreateClientPayload = CosmosCreateClientPayload>
+ CanQueryUnbondingPeriod
+ HasChainId<ChainId = ChainId>
+ CanQueryChainHeight<Height = Height>
+ CanQueryChainStatus<ChainStatus = ChainStatus>
+ CanRaiseError<TendermintClientError>
+ CanRaiseError<TendermintError>
+ CanRaiseError<TendermintRpcError>
+ CanRaiseError<&'static str>
+ CanRaiseError<String>
+ CanRaiseError<HermesError>,
{
async fn build_create_client_payload(
chain: &Chain,
create_client_options: &Settings,
create_client_options: &Chain::CreateClientPayloadOptions,
) -> Result<CosmosCreateClientPayload, Chain::Error> {
let latest_height = chain.query_chain_height().await?;

let unbonding_period = chain.query_unbonding_period().await?;

// TODO: Should we use a value for `trusting_period` in the config if it is not
// found in the client settings?
// And if both are missing, should we default to another value?
// Current behaviour is to default to 2/3 of unbonding period if the
// trusting period is missing from the client settings.
let trusting_period = create_client_options
.trusting_period
.unwrap_or(2 * unbonding_period / 3);
let trusting_period = create_client_options.trusting_period;
let trust_threshold = create_client_options
.trust_threshold
.try_into()
.map_err(|e| {
Chain::raise_error(format!(
"failed to convert `Fraction` to `TrustThreshold` for trust_threshold: {e}"
))
})?;

#[allow(deprecated)]
let client_state = ClientState::new(
chain.chain_id().clone(),
create_client_options.trust_threshold,
trust_threshold,
trusting_period,
unbonding_period,
create_client_options.max_clock_drift,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use cgp::prelude::Async;
use hermes_relayer_components::chain::traits::types::create_client::{
ProvideCreateClientMessageOptionsType, ProvideCreateClientPayloadOptionsType,
};
use ibc_relayer::chain::cosmos::client::Settings;

use crate::types::payloads::client::CosmosCreateClientOptions;

pub struct ProvideCosmosCreateClientSettings;

Expand All @@ -11,7 +12,7 @@ impl<Chain, Counterparty> ProvideCreateClientPayloadOptionsType<Chain, Counterpa
where
Chain: Async,
{
type CreateClientPayloadOptions = Settings;
type CreateClientPayloadOptions = CosmosCreateClientOptions;
}

pub struct ProvideNoCreateClientMessageOptionsType;
Expand Down
23 changes: 23 additions & 0 deletions crates/cosmos/cosmos-chain-components/src/types/payloads/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use cgp::prelude::*;
use core::time::Duration;

use ibc_client_tendermint::types::proto::v1::Fraction;

use crate::types::tendermint::{TendermintClientState, TendermintConsensusState, TendermintHeader};

Expand All @@ -12,3 +15,23 @@ pub struct CosmosCreateClientPayload {
pub client_state: TendermintClientState,
pub consensus_state: TendermintConsensusState,
}

#[derive(Clone, Debug)]
pub struct CosmosCreateClientOptions {
pub max_clock_drift: Duration,
pub trusting_period: Duration,
pub trust_threshold: Fraction,
}

impl Default for CosmosCreateClientOptions {
fn default() -> Self {
Self {
max_clock_drift: Default::default(),
trusting_period: Duration::from_secs(40),
trust_threshold: Fraction {
numerator: 2,
denominator: 3,
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cgp::core::error::{ErrorRaiserComponent, ErrorTypeComponent};
use cgp::prelude::*;
use hermes_cosmos_chain_components::types::channel::CosmosInitChannelOptions;
use hermes_cosmos_chain_components::types::connection::CosmosInitConnectionOptions;
use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_relayer::contexts::birelay::CosmosBiRelay;
use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_cosmos_relayer::contexts::chain::CosmosChain;
Expand All @@ -26,7 +27,6 @@ use hermes_test_components::setup::traits::drivers::binary_channel::BinaryChanne
use hermes_test_components::setup::traits::init_channel_options_at::ProvideInitChannelOptionsAt;
use hermes_test_components::setup::traits::init_connection_options_at::ProvideInitConnectionOptionsAt;
use hermes_test_components::setup::traits::port_id_at::ProvidePortIdAt;
use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};

use crate::contexts::binary_channel::test_driver::CosmosBinaryChannelTestDriver;
Expand All @@ -41,7 +41,7 @@ use crate::contexts::relay_driver::CosmosRelayDriver;
pub struct CosmosBinaryChannelSetup {
pub bootstrap_a: Arc<CosmosBootstrap>,
pub bootstrap_b: Arc<CosmosBootstrap>,
pub create_client_settings: Settings,
pub create_client_settings: CosmosCreateClientOptions,
pub init_connection_options: CosmosInitConnectionOptions,
pub init_channel_options: CosmosInitChannelOptions,
pub port_id: PortId,
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<const I: usize, const J: usize> ProvideCreateClientOptionsAt<CosmosBinaryCh
fn create_client_payload_options(
setup: &CosmosBinaryChannelSetup,
_index: Twindex<I, J>,
) -> &Settings {
) -> &CosmosCreateClientOptions {
&setup.create_client_settings
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cgp::core::error::{ErrorRaiserComponent, ErrorTypeComponent};
use cgp::prelude::*;
use hermes_cosmos_chain_components::types::channel::CosmosInitChannelOptions;
use hermes_cosmos_chain_components::types::connection::CosmosInitConnectionOptions;
use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_relayer::contexts::birelay::CosmosBiRelay;
use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_cosmos_relayer::contexts::chain::CosmosChain;
Expand All @@ -26,7 +27,6 @@ use hermes_test_components::setup::traits::drivers::binary_channel::BinaryChanne
use hermes_test_components::setup::traits::init_channel_options_at::ProvideInitChannelOptionsAt;
use hermes_test_components::setup::traits::init_connection_options_at::ProvideInitConnectionOptionsAt;
use hermes_test_components::setup::traits::port_id_at::ProvidePortIdAt;
use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};

use crate::contexts::binary_channel::test_driver::CosmosBinaryChannelTestDriver;
Expand All @@ -41,7 +41,7 @@ use crate::contexts::relay_driver::CosmosRelayDriver;
pub struct LegacyCosmosBinaryChannelSetup {
pub bootstrap_a: Arc<LegacyCosmosBootstrap>,
pub bootstrap_b: Arc<LegacyCosmosBootstrap>,
pub create_client_settings: Settings,
pub create_client_settings: CosmosCreateClientOptions,
pub init_connection_options: CosmosInitConnectionOptions,
pub init_channel_options: CosmosInitChannelOptions,
pub port_id: PortId,
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<const I: usize, const J: usize>
fn create_client_payload_options(
setup: &LegacyCosmosBinaryChannelSetup,
_index: Twindex<I, J>,
) -> &Settings {
) -> &CosmosCreateClientOptions {
&setup.create_client_settings
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use core::time::Duration;
use std::sync::Arc;

use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig;
use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_integration_tests::contexts::binary_channel::setup::CosmosBinaryChannelSetup;
use hermes_cosmos_integration_tests::contexts::bootstrap::CosmosBootstrap;
use hermes_cosmos_integration_tests::init::init_test_runtime;
use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_error::types::Error;
use hermes_ibc_test_suite::tests::transfer::TestIbcTransfer;
use hermes_test_components::setup::traits::run_test::CanRunTest;
use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer_types::core::ics02_client::trust_threshold::TrustThreshold;
use ibc_proto::ibc::lightclients::tendermint::v1::Fraction;
use ibc_relayer_types::core::ics24_host::identifier::PortId;

#[test]
Expand All @@ -36,10 +36,13 @@ fn cosmos_integration_tests() -> Result<(), Error> {
dynamic_gas: Some(DynamicGasConfig::default()),
});

let create_client_settings = Settings {
let create_client_settings = CosmosCreateClientOptions {
max_clock_drift: Duration::from_secs(40),
trusting_period: None,
trust_threshold: TrustThreshold::ONE_THIRD,
trust_threshold: Fraction {
numerator: 1,
denominator: 2,
},
..Default::default()
};

let setup = CosmosBinaryChannelSetup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use core::time::Duration;
use std::sync::Arc;

use hermes_cosmos_chain_components::types::payloads::client::CosmosCreateClientOptions;
use hermes_cosmos_integration_tests::contexts::binary_channel::setup_legacy::LegacyCosmosBinaryChannelSetup;
use hermes_cosmos_integration_tests::contexts::bootstrap_legacy::LegacyCosmosBootstrap;
use hermes_cosmos_integration_tests::init::init_test_runtime;
use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_error::types::Error;
use hermes_ibc_test_suite::tests::transfer::TestIbcTransfer;
use hermes_test_components::setup::traits::run_test::CanRunTest;
use ibc_relayer::chain::cosmos::client::Settings;
use ibc_relayer_types::core::ics02_client::trust_threshold::TrustThreshold;
use ibc_proto::ibc::lightclients::tendermint::v1::Fraction;
use ibc_relayer_types::core::ics24_host::identifier::PortId;

#[test]
Expand Down Expand Up @@ -39,10 +39,13 @@ fn cosmos_integration_tests_legacy() -> Result<(), Error> {
comet_config_modifier: Box::new(|_| Ok(())),
});

let create_client_settings = Settings {
let create_client_settings = CosmosCreateClientOptions {
max_clock_drift: Duration::from_secs(40),
trusting_period: None,
trust_threshold: TrustThreshold::ONE_THIRD,
trust_threshold: Fraction {
numerator: 1,
denominator: 2,
},
..Default::default()
};

let setup = LegacyCosmosBinaryChannelSetup {
Expand Down
9 changes: 5 additions & 4 deletions crates/cosmos/cosmos-relayer/src/contexts/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use hermes_cosmos_chain_components::traits::unbonding_period::CanQueryUnbondingP
use hermes_cosmos_chain_components::types::config::gas::gas_config::GasConfig;
use hermes_cosmos_chain_components::types::config::tx_config::TxConfig;
use hermes_cosmos_chain_components::types::payloads::client::{
CosmosCreateClientPayload, CosmosUpdateClientPayload,
CosmosCreateClientOptions, CosmosCreateClientPayload, CosmosUpdateClientPayload,
};
use hermes_relayer_components::chain::traits::payload_builders::create_client::CanBuildCreateClientPayload;
use hermes_relayer_components::chain::traits::types::create_client::{
HasCreateClientPayloadOptionsType, HasCreateClientPayloadType,
};
use hermes_relayer_components::chain::traits::types::update_client::HasUpdateClientPayloadType;
use ibc_relayer::chain::cosmos::client::Settings;

use cgp::core::error::{ErrorRaiserComponent, ErrorTypeComponent};
use cgp::prelude::*;
Expand Down Expand Up @@ -382,8 +381,10 @@ pub trait CanUseCosmosChain:
+ HasEventType<Event = Arc<AbciEvent>>
+ HasCreateClientPayloadType<CosmosChain, CreateClientPayload = CosmosCreateClientPayload>
+ HasUpdateClientPayloadType<CosmosChain, UpdateClientPayload = CosmosUpdateClientPayload>
+ HasCreateClientPayloadOptionsType<CosmosChain, CreateClientPayloadOptions = Settings>
+ CanQueryBalance
+ HasCreateClientPayloadOptionsType<
CosmosChain,
CreateClientPayloadOptions = CosmosCreateClientOptions,
> + CanQueryBalance
+ CanIbcTransferToken<CosmosChain>
+ CanConvertGasToFee
+ CanQueryEipBaseFee
Expand Down
Loading

0 comments on commit f30a44c

Please sign in to comment.