Skip to content

Commit

Permalink
Remove BaseChainHandle from BaseCosmosChain (#483)
Browse files Browse the repository at this point in the history
* Remove BaseChainHandle from BaseCosmosChain

* Remove query balance CLI as it depends on ChainHandle

* Fix keypair get/add for Cosmos chains
  • Loading branch information
ljoss17 authored Dec 2, 2024
1 parent a70ba95 commit a465137
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 176 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ cgp-core = { version = "0.1.0" }
cgp-error-eyre = { version = "0.1.0" }
cgp-component-macro = { version = "0.1.0" }
clap = { version = "4.5.20" }
dirs-next = { version = "2.0.0" }
oneline-eyre = { version = "0.1.0" }
prost = { version = "0.13.3" }
prost-types = { version = "0.13.3" }
Expand Down
131 changes: 0 additions & 131 deletions crates/cli/cli/src/commands/keys/balance.rs

This file was deleted.

6 changes: 0 additions & 6 deletions crates/cli/cli/src/commands/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub use list::KeysListCmd;
mod delete;
pub use delete::KeysDeleteCmd;

mod balance;
pub use balance::KeysBalanceCmd;
use hermes_cli_framework::command::CommandRunner;
use hermes_cli_framework::output::Output;

Expand All @@ -26,9 +24,6 @@ pub enum KeysCmd {

/// Delete key(s) from a configured chain
Delete(KeysDeleteCmd),

/// Retrieve the balance for a key from a configured chain
Balance(KeysBalanceCmd),
}

impl CommandRunner<HermesApp> for KeysCmd {
Expand All @@ -37,7 +32,6 @@ impl CommandRunner<HermesApp> for KeysCmd {
Self::Add(cmd) => cmd.run(app).await,
Self::List(cmd) => cmd.run(app).await,
Self::Delete(cmd) => cmd.run(app).await,
Self::Balance(cmd) => cmd.run(app).await,
}
}
}
2 changes: 2 additions & 0 deletions crates/cosmos/cosmos-relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tendermint = { workspace = true, features = ["secp2
tendermint-proto = { workspace = true }
tendermint-rpc = { workspace = true, features = ["http-client", "websocket-client"] }

dirs-next = { workspace = true }
eyre = { workspace = true }
prost = { workspace = true }
prost-types = { workspace = true }
Expand All @@ -53,6 +54,7 @@ tokio = { workspace = true }
tonic = { workspace = true, features = ["tls", "tls-roots"] }
serde = { workspace = true }
serde_derive = { workspace = true }
serde_json = { workspace = true }
subtle-encoding = { workspace = true }
itertools = { workspace = true }
futures = { workspace = true }
Expand Down
63 changes: 28 additions & 35 deletions crates/cosmos/cosmos-relayer/src/contexts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cgp::prelude::*;
use eyre::eyre;
use futures::lock::Mutex;
use std::collections::HashMap;
use std::fs::{self, File};

use hermes_cosmos_chain_components::types::config::tx_config::TxConfig;
use hermes_error::types::Error;
Expand All @@ -25,15 +26,14 @@ use hermes_runtime_components::traits::runtime::{
ProvideDefaultRuntimeField, RuntimeGetterComponent, RuntimeTypeComponent,
};
use ibc_relayer::chain::cosmos::config::CosmosSdkConfig;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::config::filter::PacketFilter;
use ibc_relayer::config::ChainConfig;
use ibc_relayer::keyring::{AnySigningKeyPair, Secp256k1KeyPair};
use ibc_relayer::spawn::{spawn_chain_runtime_with_config, SpawnError};
use ibc_relayer::keyring::{
AnySigningKeyPair, Secp256k1KeyPair, KEYSTORE_DEFAULT_FOLDER, KEYSTORE_FILE_EXTENSION,
};
use ibc_relayer::spawn::SpawnError;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ClientId};
use tendermint_rpc::client::CompatMode;
use tendermint_rpc::{Client, HttpClient};
use tokio::task;

use crate::contexts::birelay::CosmosBiRelay;
use crate::contexts::chain::CosmosChain;
Expand Down Expand Up @@ -168,19 +168,7 @@ impl CosmosBuilder {
chain_config: CosmosSdkConfig,
m_keypair: Option<&Secp256k1KeyPair>,
) -> Result<CosmosChain, Error> {
let runtime = self.runtime.runtime.clone();
let chain_id = chain_config.id.clone();

let (handle, key) = task::block_in_place(|| -> Result<_, Error> {
let handle = spawn_chain_runtime_with_config::<BaseChainHandle>(
ChainConfig::CosmosSdk(chain_config.clone()),
runtime,
)?;

let key = get_keypair(&chain_id, &handle, m_keypair)?;

Ok((handle, key))
})?;
let key = get_keypair(&chain_config, m_keypair)?;

let event_source_mode = chain_config.event_source.clone();

Expand All @@ -199,7 +187,6 @@ impl CosmosBuilder {
rpc_client.set_compat_mode(compat_mode);

let context = CosmosChain::new(
handle,
chain_config,
tx_config,
rpc_client,
Expand Down Expand Up @@ -238,30 +225,36 @@ impl CosmosBuilder {
}

pub fn get_keypair(
chain_id: &ChainId,
handle: &BaseChainHandle,
chain_config: &CosmosSdkConfig,
m_keypair: Option<&Secp256k1KeyPair>,
) -> Result<Secp256k1KeyPair, Error> {
if let Some(keypair) = m_keypair {
let ChainConfig::CosmosSdk(chain_config) = handle.config()?;
let ks_folder = &chain_config.key_store_folder;

let ks_folder = match ks_folder {
Some(folder) => folder.to_owned(),
None => {
let home =
dirs_next::home_dir().ok_or_else(|| eyre!("failed to retrieve home directory"))?;
home.join(KEYSTORE_DEFAULT_FOLDER)
}
};
// Create hermes_keyring folder if it does not exist
fs::create_dir_all(&ks_folder)?;

// try add the key to the chain handle, in case if it is only in the key map,
// as for the case of integration tests.
let _ = handle.add_key(
chain_config.key_name,
AnySigningKeyPair::Secp256k1(keypair.clone()),
);
let mut filename = ks_folder.join(chain_config.key_name.clone());
filename.set_extension(KEYSTORE_FILE_EXTENSION);

let file = File::create(filename.clone())?;

return Ok(keypair.clone());
if let Some(keypair) = m_keypair {
serde_json::to_writer_pretty(file, &AnySigningKeyPair::Secp256k1(keypair.clone()))?;
}

let keypair = handle.get_key()?;
let file = File::open(&filename)?;

let AnySigningKeyPair::Secp256k1(key) = keypair else {
return Err(eyre!("no Secp256k1 key pair for chain {}", chain_id).into());
};
let key_entry = serde_json::from_reader(file)?;

Ok(key)
Ok(key_entry)
}

impl ChainBuilder<CosmosBuilder, 0> for CosmosBaseBuildComponents {
Expand Down
4 changes: 0 additions & 4 deletions crates/cosmos/cosmos-relayer/src/contexts/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ use ibc::core::channel::types::channel::ChannelEnd;
use ibc_proto::cosmos::tx::v1beta1::Fee;
use ibc_relayer::chain::cosmos::config::CosmosSdkConfig;
use ibc_relayer::chain::cosmos::types::account::Account;
use ibc_relayer::chain::handle::BaseChainHandle;
use ibc_relayer::config::EventSourceMode;
use ibc_relayer::event::source::queries::all as all_queries;
use ibc_relayer::keyring::Secp256k1KeyPair;
Expand All @@ -125,7 +124,6 @@ pub struct CosmosChain {

#[derive(HasField)]
pub struct BaseCosmosChain {
pub handle: BaseChainHandle,
pub chain_config: CosmosSdkConfig,
pub chain_id: ChainId,
pub compat_mode: CompatMode,
Expand Down Expand Up @@ -267,7 +265,6 @@ impl IbcCommitmentPrefixGetter<CosmosChain> for CosmosChainContextComponents {

impl CosmosChain {
pub fn new(
handle: BaseChainHandle,
chain_config: CosmosSdkConfig,
tx_config: TxConfig,
rpc_client: HttpClient,
Expand Down Expand Up @@ -297,7 +294,6 @@ impl CosmosChain {

let chain = Self {
base_chain: Arc::new(BaseCosmosChain {
handle,
chain_config,
chain_id,
compat_mode,
Expand Down

0 comments on commit a465137

Please sign in to comment.