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

Use env variables to bootstrap CosmosBootstrap #476

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions crates/cosmos/cosmos-integration-tests/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use alloc::sync::Arc;
use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig;
use hermes_error::types::Error;
use serde_json::Value as JsonValue;
use std::env;
use toml::Value as TomlValue;

use hermes_cosmos_relayer::contexts::build::CosmosBuilder;
use hermes_runtime::types::runtime::HermesRuntime;
use tokio::runtime::Builder;
use tracing::info;
Expand All @@ -8,6 +14,8 @@ use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, EnvFilter};

use crate::contexts::bootstrap::CosmosBootstrap;

pub fn init_test_runtime() -> HermesRuntime {
let _ = stable_eyre::install();

Expand All @@ -28,3 +36,54 @@ pub fn init_test_runtime() -> HermesRuntime {

runtime
}

pub fn init_bootstrap(
chain_id: usize,
runtime: HermesRuntime,
should_randomize_identifiers: bool,
chain_store_dir: &str,
transfer_denom_prefix: String,
genesis_modifier: impl Fn(&mut JsonValue) -> Result<(), Error> + Send + Sync + 'static,
comet_config_modifier: impl Fn(&mut TomlValue) -> Result<(), Error> + Send + Sync + 'static,
dynamic_gas: Option<DynamicGasConfig>,
) -> CosmosBootstrap {
let cosmos_builder = Arc::new(CosmosBuilder::new_with_default(runtime.clone()));

let chain_command_path =
env::var("CHAIN_COMMAND_PATHS").unwrap_or_else(|_| "gaiad".to_string());
let chain_command_paths: Vec<String> = parse_chain_command_paths(chain_command_path);

let account_prefix = env::var("ACCOUNT_PREFIXES").unwrap_or_else(|_| "cosmos".to_string());
let account_prefixes = parse_chain_command_paths(account_prefix);

let staking_denom_prefix = env::var("NATIVE_TOKENS").unwrap_or_else(|_| "stake".to_string());
let staking_denom_prefixes = parse_chain_command_paths(staking_denom_prefix);

CosmosBootstrap {
runtime,
cosmos_builder,
should_randomize_identifiers,
chain_store_dir: chain_store_dir.into(),
chain_command_path: chain_command_paths[chain_id % chain_command_paths.len()]
.as_str()
.into(),
account_prefix: account_prefixes[chain_id % account_prefixes.len()]
.as_str()
.into(),
staking_denom_prefix: staking_denom_prefixes[chain_id % staking_denom_prefixes.len()]
.as_str()
.into(),
transfer_denom_prefix,
genesis_config_modifier: Box::new(genesis_modifier),
comet_config_modifier: Box::new(comet_config_modifier),
dynamic_gas,
}
}

fn parse_chain_command_paths(chain_command_path: String) -> Vec<String> {
let patterns: Vec<String> = chain_command_path
.split(',')
.map(|chain_binary| chain_binary.to_string())
.collect();
patterns
}
1 change: 1 addition & 0 deletions crates/cosmos/cosmos-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]

extern crate alloc;

Expand Down
30 changes: 11 additions & 19 deletions crates/cosmos/cosmos-integration-tests/tests/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
use std::sync::Arc;

use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig;
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_cosmos_integration_tests::init::{init_bootstrap, init_test_runtime};
use hermes_error::types::Error;
use hermes_test_components::bootstrap::traits::chain::CanBootstrapChain;

#[test]
fn test_cosmos_bootstrap() -> Result<(), Error> {
let runtime = init_test_runtime();

let builder = Arc::new(CosmosBuilder::new_with_default(runtime.clone()));

// TODO: load parameters from environment variables
let bootstrap = Arc::new(CosmosBootstrap {
runtime: runtime.clone(),
cosmos_builder: builder,
should_randomize_identifiers: true,
chain_store_dir: "./test-data".into(),
chain_command_path: "gaiad".into(),
account_prefix: "cosmos".into(),
staking_denom_prefix: "stake".into(),
transfer_denom_prefix: "coin".into(),
genesis_config_modifier: Box::new(|_| Ok(())),
comet_config_modifier: Box::new(|_| Ok(())),
dynamic_gas: Some(DynamicGasConfig::default()),
});
let bootstrap = Arc::new(init_bootstrap(
0,
runtime.clone(),
true,
"./test-data",
"coin".into(),
|_| Ok(()),
|_| Ok(()),
Some(DynamicGasConfig::default()),
));

runtime.runtime.clone().block_on(async move {
let _chain_driver = bootstrap.bootstrap_chain("chain-1").await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use std::sync::Arc;

use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig;
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_cosmos_integration_tests::init::{init_bootstrap, init_test_runtime};
use hermes_error::types::Error;
use hermes_ibc_test_suite::tests::transfer::TestIbcTransfer;
use hermes_test_components::setup::traits::run_test::CanRunTest;
Expand All @@ -19,22 +17,27 @@ use ibc_relayer_types::core::ics24_host::identifier::PortId;
fn cosmos_integration_tests() -> Result<(), Error> {
let runtime = init_test_runtime();

let builder = Arc::new(CosmosBuilder::new_with_default(runtime.clone()));

// TODO: load parameters from environment variables
let bootstrap = Arc::new(CosmosBootstrap {
runtime: runtime.clone(),
cosmos_builder: builder,
should_randomize_identifiers: true,
chain_store_dir: "./test-data".into(),
chain_command_path: "gaiad".into(),
account_prefix: "cosmos".into(),
staking_denom_prefix: "stake".into(),
transfer_denom_prefix: "coin".into(),
genesis_config_modifier: Box::new(|_| Ok(())),
comet_config_modifier: Box::new(|_| Ok(())),
dynamic_gas: Some(DynamicGasConfig::default()),
});
let bootstrap_chain_0 = Arc::new(init_bootstrap(
0,
runtime.clone(),
true,
"./test-data",
"coin".into(),
|_| Ok(()),
|_| Ok(()),
Some(DynamicGasConfig::default()),
));

let bootstrap_chain_1 = Arc::new(init_bootstrap(
1,
runtime.clone(),
true,
"./test-data",
"coin".into(),
|_| Ok(()),
|_| Ok(()),
Some(DynamicGasConfig::default()),
));

let create_client_settings = Settings {
max_clock_drift: Duration::from_secs(40),
Expand All @@ -43,8 +46,8 @@ fn cosmos_integration_tests() -> Result<(), Error> {
};

let setup = CosmosBinaryChannelSetup {
bootstrap_a: bootstrap.clone(),
bootstrap_b: bootstrap,
bootstrap_a: bootstrap_chain_0,
bootstrap_b: bootstrap_chain_1,
create_client_settings,
init_connection_options: Default::default(),
init_channel_options: Default::default(),
Expand Down
Loading