Skip to content

Commit

Permalink
Wait for chain to start up in Cosmos Bootstrap (#492)
Browse files Browse the repository at this point in the history
* Add CanWaitChainStartup trait to ChainDriver

* Simplify wiring of LegacyCosmosSdkBootstrapComponents

* Implement CanWaitChainStartup for CosmosChainDriver

* Call chain_driver.wait_chain_startup() inside bootstrap

* Revert "Call chain_driver.wait_chain_startup() inside bootstrap"

This reverts commit 4a0e246.

* Use BuildAndWaitChainDriver inside Cosmos bootstrap
  • Loading branch information
soareschen authored Dec 9, 2024
1 parent ec5f5b0 commit e7bbfad
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,7 +129,7 @@ delegate_components! {
ChainBuilderWithNodeConfigComponent:
BuildCosmosChainWithNodeConfig,
ChainDriverBuilderComponent:
BuildCosmosChainDriver,
BuildAndWaitChainDriver<BuildCosmosChainDriver>,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,6 +127,6 @@ delegate_components! {
ChainBuilderWithNodeConfigComponent:
BuildCosmosChainWithNodeConfig,
ChainDriverBuilderComponent:
BuildCosmosChainDriver,
BuildAndWaitChainDriver<BuildCosmosChainDriver>,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +70,7 @@ delegate_components! {
ProvideHermesRuntime,
[
RandomAmountGeneratorComponent,
ChainStartupWaiterComponent,
ProposalIdTypeComponent,
ProposalStatusTypeComponent,
]:
Expand Down Expand Up @@ -200,3 +204,7 @@ impl ConfigUpdater<CosmosChainDriver, Config> for CosmosChainDriverComponents {
Ok(chain_config_str)
}
}

pub trait CanUseCosmosChainDriver: CanWaitChainStartup {}

impl CanUseCosmosChainDriver for CosmosChainDriver {}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GetStdOutOrElseStdErr>,
with_cosmos_sdk_bootstrap_components! {
[
GenesisAccountAdderComponent,
GenesisValidatorAdderComponent,
GenesisTransactionsCollectorComponent,
WalletInitializerComponent,
],
| Components | {
cgp_preset! {
LegacyCosmosSdkBootstrapComponents {
GenesisAccountAdderComponent: LegacyAddCosmosGenesisAccount,
GenesisValidatorAdderComponent: LegacyAddCosmosGenesisValidator,
GenesisTransactionsCollectorComponent: LegacyCollectCosmosGentxs,
WalletInitializerComponent: InitCosmosTestWallet<GetStdOutOrElseStdErr>,

// Components that are the same as `CosmosSdkBootstrapComponents`
[
ChainNodeConfigTypeComponent,
ChainGenesisConfigTypeComponent,
WalletConfigTypeComponent,
WalletConfigFieldsComponent,
ChainIdGeneratorComponent,
ChainHomeDirInitializerComponent,
ChainDataInitializerComponent,
GenesisDenomGetterComponent,
WalletHdPathComponent,
ChainNodeConfigInitializerComponent,
ChainGenesisConfigInitializerComponent,
GenesisWalletAdderComponent,
ChainFullNodeStarterComponent,
ChainBootstrapperComponent,
]:
CosmosSdkBootstrapComponents,
Components: CosmosSdkBootstrapComponents,
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<InBuilder>(pub PhantomData<InBuilder>);

impl<Bootstrap, ChainDriver, InBuilder> ChainDriverBuilder<Bootstrap>
for BuildAndWaitChainDriver<InBuilder>
where
Bootstrap: HasRuntimeType<Runtime: HasChildProcessType>
+ HasChainType<Chain: HasWalletType>
+ HasChainDriverType<ChainDriver = ChainDriver>
+ HasChainGenesisConfigType
+ HasChainNodeConfigType
+ CanRaiseError<ChainDriver::Error>,
InBuilder: ChainDriverBuilder<Bootstrap>,
ChainDriver: CanWaitChainStartup,
{
async fn build_chain_driver(
bootstrap: &Bootstrap,
genesis_config: Bootstrap::ChainGenesisConfig,
chain_node_config: Bootstrap::ChainNodeConfig,
wallets: BTreeMap<String, Wallet<Bootstrap::Chain>>,
chain_process: ChildProcessOf<Bootstrap::Runtime>,
) -> Result<ChainDriver, Bootstrap::Error> {
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod bootstrap_chain;
pub mod build_wait;
pub mod start_chain;
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ use crate::bootstrap::traits::types::genesis_config::HasChainGenesisConfigType;
}]
#[async_trait]
pub trait CanBuildChainDriver:
HasRuntimeType
+ HasChainDriverType
HasRuntimeType<Runtime: HasChildProcessType>
+ HasChainDriverType<Chain: HasWalletType>
+ HasChainGenesisConfigType
+ HasChainNodeConfigType
+ HasErrorType
where
Self::Runtime: HasChildProcessType,
Self::Chain: HasWalletType,
{
async fn build_chain_driver(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime: HasChildProcessType + HasFilePathType>
+ HasErrorType
{
async fn start_chain_full_node(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,5 +11,7 @@ delegate_components! {
CosmosChainDriverComponents {
RandomAmountGeneratorComponent:
GenerateRandomAmount,
ChainStartupWaiterComponent:
WaitChainReachHeight<1>,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +116,7 @@ delegate_components! {
ChainBuilderWithNodeConfigComponent:
BuildCosmosChainWithNodeConfig,
ChainDriverBuilderComponent:
BuildChainDriverAndInitWasmClient<BuildCosmosChainDriver>,
BuildChainDriverAndInitWasmClient<BuildAndWaitChainDriver<BuildCosmosChainDriver>>,
CosmosGenesisConfigModifierComponent:
ModifyWasmGenesisConfig<NoModifyGenesisConfig>,
CometConfigModifierComponent:
Expand Down
1 change: 1 addition & 0 deletions crates/test/test-components/src/chain_driver/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod wait;
49 changes: 49 additions & 0 deletions crates/test/test-components/src/chain_driver/impls/wait.rs
Original file line number Diff line number Diff line change
@@ -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<const H: u64>;

impl<ChainDriver, Chain, const H: u64> ChainStartupWaiter<ChainDriver> for WaitChainReachHeight<H>
where
ChainDriver:
HasChain<Chain = Chain> + HasRuntime<Runtime: CanSleep> + 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<ChainDriver> ChainStartupWaiter<ChainDriver> for NoWaitChainStartup
where
ChainDriver: Async + HasErrorType,
{
async fn wait_chain_startup(_chain_driver: &ChainDriver) -> Result<(), ChainDriver::Error> {
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/test/test-components/src/chain_driver/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod impls;
pub mod traits;
1 change: 1 addition & 0 deletions crates/test/test-components/src/chain_driver/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod chain_process;
pub mod config;
pub mod fields;
pub mod types;
pub mod wait;
10 changes: 10 additions & 0 deletions crates/test/test-components/src/chain_driver/traits/wait.rs
Original file line number Diff line number Diff line change
@@ -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>;
}

0 comments on commit e7bbfad

Please sign in to comment.