diff --git a/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap.rs b/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap.rs index 3f9af0818..c5939cb79 100644 --- a/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap.rs +++ b/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap.rs @@ -11,6 +11,7 @@ use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::Dyna use hermes_cosmos_relayer::contexts::build::CosmosBuilder; use hermes_cosmos_relayer::contexts::chain::CosmosChain; use hermes_cosmos_test_components::bootstrap::components::cosmos_sdk::*; +use hermes_cosmos_test_components::bootstrap::impls::chain::build_wait::BuildAndWaitChainDriver; use hermes_cosmos_test_components::bootstrap::impls::generator::wallet_config::GenerateStandardWalletConfig; use hermes_cosmos_test_components::bootstrap::impls::modifiers::no_modify_cosmos_sdk_config::NoModifyCosmosSdkConfig; use hermes_cosmos_test_components::bootstrap::traits::chain::build_chain_driver::ChainDriverBuilderComponent; @@ -128,7 +129,7 @@ delegate_components! { ChainBuilderWithNodeConfigComponent: BuildCosmosChainWithNodeConfig, ChainDriverBuilderComponent: - BuildCosmosChainDriver, + BuildAndWaitChainDriver, } } diff --git a/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap_legacy.rs b/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap_legacy.rs index 535391c24..72865085d 100644 --- a/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap_legacy.rs +++ b/crates/cosmos/cosmos-integration-tests/src/contexts/bootstrap_legacy.rs @@ -10,6 +10,7 @@ use cgp::prelude::*; use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig; use hermes_cosmos_relayer::contexts::build::CosmosBuilder; use hermes_cosmos_test_components::bootstrap::components::cosmos_sdk_legacy::*; +use hermes_cosmos_test_components::bootstrap::impls::chain::build_wait::BuildAndWaitChainDriver; use hermes_cosmos_test_components::bootstrap::impls::generator::wallet_config::GenerateStandardWalletConfig; use hermes_cosmos_test_components::bootstrap::impls::modifiers::no_modify_cosmos_sdk_config::NoModifyCosmosSdkConfig; use hermes_cosmos_test_components::bootstrap::traits::chain::build_chain_driver::ChainDriverBuilderComponent; @@ -126,6 +127,6 @@ delegate_components! { ChainBuilderWithNodeConfigComponent: BuildCosmosChainWithNodeConfig, ChainDriverBuilderComponent: - BuildCosmosChainDriver, + BuildAndWaitChainDriver, } } diff --git a/crates/cosmos/cosmos-integration-tests/src/contexts/chain_driver.rs b/crates/cosmos/cosmos-integration-tests/src/contexts/chain_driver.rs index fd521d8dc..cf3bf97d3 100644 --- a/crates/cosmos/cosmos-integration-tests/src/contexts/chain_driver.rs +++ b/crates/cosmos/cosmos-integration-tests/src/contexts/chain_driver.rs @@ -33,6 +33,9 @@ use hermes_test_components::chain_driver::traits::fields::wallet::{ RelayerWallet, UserWallet, ValidatorWallet, WalletGetterAt, WalletsGetter, }; use hermes_test_components::chain_driver::traits::types::chain::{ChainGetter, ProvideChainType}; +use hermes_test_components::chain_driver::traits::wait::{ + CanWaitChainStartup, ChainStartupWaiterComponent, +}; use ibc_relayer::config::Config; use tokio::process::Child; use toml::to_string_pretty; @@ -67,6 +70,7 @@ delegate_components! { ProvideHermesRuntime, [ RandomAmountGeneratorComponent, + ChainStartupWaiterComponent, ProposalIdTypeComponent, ProposalStatusTypeComponent, ]: @@ -200,3 +204,7 @@ impl ConfigUpdater for CosmosChainDriverComponents { Ok(chain_config_str) } } + +pub trait CanUseCosmosChainDriver: CanWaitChainStartup {} + +impl CanUseCosmosChainDriver for CosmosChainDriver {} diff --git a/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk.rs b/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk.rs index 69a6813ce..b55207cb3 100644 --- a/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk.rs +++ b/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk.rs @@ -80,7 +80,6 @@ use crate::bootstrap::types::chain_node_config::CosmosChainNodeConfig; use crate::bootstrap::types::genesis_config::CosmosGenesisConfig; use crate::chain::types::wallet::CosmosTestWallet; -// Components that will be swapped in `LegacyCosmosSdkBootstrapComponents` cgp_preset! { CosmosSdkBootstrapComponents { GenesisAccountAdderComponent: AddCosmosGenesisAccount, diff --git a/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk_legacy.rs b/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk_legacy.rs index 4817e75df..55a18b0cc 100644 --- a/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk_legacy.rs +++ b/crates/cosmos/cosmos-test-components/src/bootstrap/components/cosmos_sdk_legacy.rs @@ -25,7 +25,7 @@ use hermes_test_components::driver::traits::types::chain_driver::ProvideChainDri use ibc::core::host::types::identifiers::ChainId; use ibc_relayer::keyring::errors::Error as KeyringError; -use crate::bootstrap::components::cosmos_sdk::CosmosSdkBootstrapComponents; +use crate::bootstrap::components::cosmos_sdk::{CosmosSdkBootstrapComponents, *}; use crate::bootstrap::impls::genesis_legacy::add_genesis_account::LegacyAddCosmosGenesisAccount; use crate::bootstrap::impls::genesis_legacy::add_genesis_validator::LegacyAddCosmosGenesisValidator; use crate::bootstrap::impls::genesis_legacy::collect_gentxs::LegacyCollectCosmosGentxs; @@ -70,31 +70,24 @@ use crate::bootstrap::types::chain_node_config::CosmosChainNodeConfig; use crate::bootstrap::types::genesis_config::CosmosGenesisConfig; use crate::chain::types::wallet::CosmosTestWallet; -cgp_preset! { - LegacyCosmosSdkBootstrapComponents { - GenesisAccountAdderComponent: LegacyAddCosmosGenesisAccount, - GenesisValidatorAdderComponent: LegacyAddCosmosGenesisValidator, - GenesisTransactionsCollectorComponent: LegacyCollectCosmosGentxs, - WalletInitializerComponent: InitCosmosTestWallet, +with_cosmos_sdk_bootstrap_components! { + [ + GenesisAccountAdderComponent, + GenesisValidatorAdderComponent, + GenesisTransactionsCollectorComponent, + WalletInitializerComponent, + ], + | Components | { + cgp_preset! { + LegacyCosmosSdkBootstrapComponents { + GenesisAccountAdderComponent: LegacyAddCosmosGenesisAccount, + GenesisValidatorAdderComponent: LegacyAddCosmosGenesisValidator, + GenesisTransactionsCollectorComponent: LegacyCollectCosmosGentxs, + WalletInitializerComponent: InitCosmosTestWallet, - // Components that are the same as `CosmosSdkBootstrapComponents` - [ - ChainNodeConfigTypeComponent, - ChainGenesisConfigTypeComponent, - WalletConfigTypeComponent, - WalletConfigFieldsComponent, - ChainIdGeneratorComponent, - ChainHomeDirInitializerComponent, - ChainDataInitializerComponent, - GenesisDenomGetterComponent, - WalletHdPathComponent, - ChainNodeConfigInitializerComponent, - ChainGenesisConfigInitializerComponent, - GenesisWalletAdderComponent, - ChainFullNodeStarterComponent, - ChainBootstrapperComponent, - ]: - CosmosSdkBootstrapComponents, + Components: CosmosSdkBootstrapComponents, + } + } } } diff --git a/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/build_wait.rs b/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/build_wait.rs new file mode 100644 index 000000000..f5408aefe --- /dev/null +++ b/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/build_wait.rs @@ -0,0 +1,53 @@ +use core::marker::PhantomData; +use std::collections::BTreeMap; + +use cgp::prelude::CanRaiseError; +use hermes_runtime_components::traits::os::child_process::{ChildProcessOf, HasChildProcessType}; +use hermes_runtime_components::traits::runtime::HasRuntimeType; +use hermes_test_components::chain::traits::types::wallet::{HasWalletType, Wallet}; +use hermes_test_components::chain_driver::traits::types::chain::HasChainType; +use hermes_test_components::chain_driver::traits::wait::CanWaitChainStartup; +use hermes_test_components::driver::traits::types::chain_driver::HasChainDriverType; + +use crate::bootstrap::traits::chain::build_chain_driver::ChainDriverBuilder; +use crate::bootstrap::traits::types::chain_node_config::HasChainNodeConfigType; +use crate::bootstrap::traits::types::genesis_config::HasChainGenesisConfigType; + +pub struct BuildAndWaitChainDriver(pub PhantomData); + +impl ChainDriverBuilder + for BuildAndWaitChainDriver +where + Bootstrap: HasRuntimeType + + HasChainType + + HasChainDriverType + + HasChainGenesisConfigType + + HasChainNodeConfigType + + CanRaiseError, + InBuilder: ChainDriverBuilder, + ChainDriver: CanWaitChainStartup, +{ + async fn build_chain_driver( + bootstrap: &Bootstrap, + genesis_config: Bootstrap::ChainGenesisConfig, + chain_node_config: Bootstrap::ChainNodeConfig, + wallets: BTreeMap>, + chain_process: ChildProcessOf, + ) -> Result { + let chain_driver = InBuilder::build_chain_driver( + bootstrap, + genesis_config, + chain_node_config, + wallets, + chain_process, + ) + .await?; + + chain_driver + .wait_chain_startup() + .await + .map_err(Bootstrap::raise_error)?; + + Ok(chain_driver) + } +} diff --git a/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/mod.rs b/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/mod.rs index 4e4d493e4..a526ad38e 100644 --- a/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/mod.rs +++ b/crates/cosmos/cosmos-test-components/src/bootstrap/impls/chain/mod.rs @@ -1,2 +1,3 @@ pub mod bootstrap_chain; +pub mod build_wait; pub mod start_chain; diff --git a/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/build_chain_driver.rs b/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/build_chain_driver.rs index 06dea9bd8..24f60dac5 100644 --- a/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/build_chain_driver.rs +++ b/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/build_chain_driver.rs @@ -15,14 +15,11 @@ use crate::bootstrap::traits::types::genesis_config::HasChainGenesisConfigType; }] #[async_trait] pub trait CanBuildChainDriver: - HasRuntimeType - + HasChainDriverType + HasRuntimeType + + HasChainDriverType + HasChainGenesisConfigType + HasChainNodeConfigType + HasErrorType -where - Self::Runtime: HasChildProcessType, - Self::Chain: HasWalletType, { async fn build_chain_driver( &self, diff --git a/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/start_chain.rs b/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/start_chain.rs index 8ce05a7af..20f754cfd 100644 --- a/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/start_chain.rs +++ b/crates/cosmos/cosmos-test-components/src/bootstrap/traits/chain/start_chain.rs @@ -12,9 +12,10 @@ use crate::bootstrap::traits::types::genesis_config::HasChainGenesisConfigType; }] #[async_trait] pub trait CanStartChainFullNode: - HasChainNodeConfigType + HasChainGenesisConfigType + HasRuntime + HasErrorType -where - Self::Runtime: HasChildProcessType + HasFilePathType, + HasChainNodeConfigType + + HasChainGenesisConfigType + + HasRuntime + + HasErrorType { async fn start_chain_full_node( &self, diff --git a/crates/cosmos/cosmos-test-components/src/chain_driver/components.rs b/crates/cosmos/cosmos-test-components/src/chain_driver/components.rs index 9f1c1b1a9..12c6a9a95 100644 --- a/crates/cosmos/cosmos-test-components/src/chain_driver/components.rs +++ b/crates/cosmos/cosmos-test-components/src/chain_driver/components.rs @@ -1,5 +1,7 @@ use cgp::prelude::*; +use hermes_test_components::chain_driver::impls::wait::WaitChainReachHeight; use hermes_test_components::chain_driver::traits::fields::amount::RandomAmountGeneratorComponent; +use hermes_test_components::chain_driver::traits::wait::ChainStartupWaiterComponent; use crate::chain_driver::impls::amount::GenerateRandomAmount; @@ -9,5 +11,7 @@ delegate_components! { CosmosChainDriverComponents { RandomAmountGeneratorComponent: GenerateRandomAmount, + ChainStartupWaiterComponent: + WaitChainReachHeight<1>, } } diff --git a/crates/cosmos/cosmos-wasm-relayer/src/context/cosmos_bootstrap.rs b/crates/cosmos/cosmos-wasm-relayer/src/context/cosmos_bootstrap.rs index fb96bddcf..31d4ec210 100644 --- a/crates/cosmos/cosmos-wasm-relayer/src/context/cosmos_bootstrap.rs +++ b/crates/cosmos/cosmos-wasm-relayer/src/context/cosmos_bootstrap.rs @@ -18,6 +18,7 @@ use hermes_cosmos_integration_tests::traits::bootstrap::cosmos_builder::CosmosBu use hermes_cosmos_integration_tests::traits::bootstrap::relayer_chain_config::RelayerChainConfigBuilderComponent; use hermes_cosmos_relayer::contexts::build::CosmosBuilder; use hermes_cosmos_test_components::bootstrap::components::cosmos_sdk::*; +use hermes_cosmos_test_components::bootstrap::impls::chain::build_wait::BuildAndWaitChainDriver; use hermes_cosmos_test_components::bootstrap::impls::generator::wallet_config::GenerateStandardWalletConfig; use hermes_cosmos_test_components::bootstrap::impls::modifiers::no_modify_comet_config::NoModifyCometConfig; use hermes_cosmos_test_components::bootstrap::impls::modifiers::no_modify_cosmos_sdk_config::NoModifyCosmosSdkConfig; @@ -115,7 +116,7 @@ delegate_components! { ChainBuilderWithNodeConfigComponent: BuildCosmosChainWithNodeConfig, ChainDriverBuilderComponent: - BuildChainDriverAndInitWasmClient, + BuildChainDriverAndInitWasmClient>, CosmosGenesisConfigModifierComponent: ModifyWasmGenesisConfig, CometConfigModifierComponent: diff --git a/crates/test/test-components/src/chain_driver/impls/mod.rs b/crates/test/test-components/src/chain_driver/impls/mod.rs new file mode 100644 index 000000000..773f6d7cf --- /dev/null +++ b/crates/test/test-components/src/chain_driver/impls/mod.rs @@ -0,0 +1 @@ +pub mod wait; diff --git a/crates/test/test-components/src/chain_driver/impls/wait.rs b/crates/test/test-components/src/chain_driver/impls/wait.rs new file mode 100644 index 000000000..f5fb03317 --- /dev/null +++ b/crates/test/test-components/src/chain_driver/impls/wait.rs @@ -0,0 +1,49 @@ +use core::time::Duration; + +use cgp::prelude::*; +use hermes_relayer_components::chain::traits::queries::chain_status::CanQueryChainHeight; +use hermes_relayer_components::chain::traits::types::height::HasHeightFields; +use hermes_runtime_components::traits::runtime::HasRuntime; +use hermes_runtime_components::traits::sleep::CanSleep; + +use crate::chain_driver::traits::types::chain::HasChain; +use crate::chain_driver::traits::wait::ChainStartupWaiter; + +pub struct WaitChainReachHeight; + +impl ChainStartupWaiter for WaitChainReachHeight +where + ChainDriver: + HasChain + HasRuntime + CanRaiseError<&'static str>, + Chain: CanQueryChainHeight + HasHeightFields, +{ + async fn wait_chain_startup(chain_driver: &ChainDriver) -> Result<(), ChainDriver::Error> { + let runtime = chain_driver.runtime(); + let chain = chain_driver.chain(); + + for _ in 0..10 { + if let Ok(height) = chain.query_chain_height().await { + if Chain::revision_height(&height) >= H { + return Ok(()); + } + } + + runtime.sleep(Duration::from_millis(500)).await; + } + + Err(ChainDriver::raise_error( + "chain did not progress to target height within 5 seconds", + )) + } +} + +pub struct NoWaitChainStartup; + +impl ChainStartupWaiter for NoWaitChainStartup +where + ChainDriver: Async + HasErrorType, +{ + async fn wait_chain_startup(_chain_driver: &ChainDriver) -> Result<(), ChainDriver::Error> { + Ok(()) + } +} diff --git a/crates/test/test-components/src/chain_driver/mod.rs b/crates/test/test-components/src/chain_driver/mod.rs index f6ac8fc7b..d44cc4a18 100644 --- a/crates/test/test-components/src/chain_driver/mod.rs +++ b/crates/test/test-components/src/chain_driver/mod.rs @@ -1 +1,2 @@ +pub mod impls; pub mod traits; diff --git a/crates/test/test-components/src/chain_driver/traits/mod.rs b/crates/test/test-components/src/chain_driver/traits/mod.rs index 8552bfc1a..9b17a97bc 100644 --- a/crates/test/test-components/src/chain_driver/traits/mod.rs +++ b/crates/test/test-components/src/chain_driver/traits/mod.rs @@ -2,3 +2,4 @@ pub mod chain_process; pub mod config; pub mod fields; pub mod types; +pub mod wait; diff --git a/crates/test/test-components/src/chain_driver/traits/wait.rs b/crates/test/test-components/src/chain_driver/traits/wait.rs new file mode 100644 index 000000000..411cd2c67 --- /dev/null +++ b/crates/test/test-components/src/chain_driver/traits/wait.rs @@ -0,0 +1,10 @@ +use cgp::prelude::*; + +#[cgp_component { + context: ChainDriver, + provider: ChainStartupWaiter, +}] +#[async_trait] +pub trait CanWaitChainStartup: Async + HasErrorType { + async fn wait_chain_startup(&self) -> Result<(), Self::Error>; +}