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

Update/add hermes relayer #427

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions cw-orch-daemon/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@
}
}

pub fn options(&self) -> &SenderOptions {
&self.options
}

Check warning on line 223 in cw-orch-daemon/src/sender.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch-daemon/src/sender.rs#L221-L223

Added lines #L221 - L223 were not covered by tests

fn cosmos_private_key(&self) -> SigningKey {
SigningKey::from_slice(&self.private_key.raw_key()).unwrap()
}
Expand Down
39 changes: 39 additions & 0 deletions packages/interchain/hermes-relayer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "hermes-relayer"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
anyhow.workspace = true
cosmwasm-std.workspace = true
cw-orch-interchain-core = { version = "0.3.3", path = "../interchain-core" }
cw-orch-interchain-daemon = { version = "0.3.2", path = "../interchain-daemon" }
cw-orch-networks = { version = "0.22.2", path = "../../cw-orch-networks" }
cw-orch-core = { version = "1.1.1", path = "../../cw-orch-core" }
hdpath = "0.6.3"
ibc-relayer = "0.28.0"
ibc-relayer-cli = "1.9.0"
ibc-relayer-types = { version = "0.28.0" }
old-ibc-relayer-types = { package = "ibc-relayer-types", version = "0.25.0" }
tokio.workspace = true
cw-orch-daemon = { version = "0.23.5", path = "../../../cw-orch-daemon" }
log.workspace = true
cosmrs.workspace = true
futures = "0.3.30"
tonic.workspace = true
cw-orch-traits = { version = "0.22.0", path = "../../cw-orch-traits" }
futures-util = "0.3.30"
async-recursion = "1.1.1"
serde_json.workspace = true

[dev-dependencies]
cw-orch = { path = "../../../cw-orch", features = ["daemon"] }
cw-orch-interchain = { path = "../../../cw-orch-interchain", features = [
"daemon",
] }
dotenv = "0.15.0"
ibc-proto = "0.32.0"
pretty_env_logger = "0.5.0"
90 changes: 90 additions & 0 deletions packages/interchain/hermes-relayer/examples/pion-xion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use cosmrs::Any;
use cw_orch::prelude::*;
use cw_orch::{
daemon::networks::{PION_1, XION_TESTNET_1},
tokio::runtime::Runtime,
};
use cw_orch_interchain::prelude::*;
use cw_orch_traits::Stargate;
use hermes_relayer::core::HermesRelayer;
use old_ibc_relayer_types::core::ics24_host::identifier::PortId;
use old_ibc_relayer_types::tx_msg::Msg;
use old_ibc_relayer_types::{
applications::transfer::msgs::transfer::MsgTransfer,
core::ics04_channel::timeout::TimeoutHeight, timestamp::Timestamp,
};

pub fn main() -> cw_orch::anyhow::Result<()> {
dotenv::dotenv()?;
pretty_env_logger::init();
let rt = Runtime::new()?;

let relayer = HermesRelayer::new(
rt.handle(),
vec![
(
PION_1,
None,
true,
"https://rpc-falcron.pion-1.ntrn.tech/".to_string(),
),
(
XION_TESTNET_1,
None,
false,
"https://xion-testnet-rpc.polkachu.com".to_string(),
),
],
vec![(
(
XION_TESTNET_1.chain_id.to_string(),
PION_1.chain_id.to_string(),
),
"connection-63".to_string(),
)]
.into_iter()
.collect(),
)?;

let channel = relayer.create_channel(
"xion-testnet-1",
"pion-1",
&PortId::transfer(),
&PortId::transfer(),
"ics20-1",
None,
)?;

let xion = relayer.chain("xion-testnet-1")?;
let pion = relayer.chain("pion-1")?;

let msg = MsgTransfer {
source_port: PortId::transfer(),
source_channel: channel
.interchain_channel
.get_chain("xion-testnet-1")?
.channel
.unwrap(),
token: ibc_proto::cosmos::base::v1beta1::Coin {
denom: "uxion".to_string(),
amount: "1987".to_string(),
},

sender: xion.sender().to_string().parse().unwrap(),
receiver: pion.sender().to_string().parse().unwrap(),
timeout_height: TimeoutHeight::Never,
timeout_timestamp: Timestamp::from_nanoseconds(1_800_000_000_000_000_000)?,
memo: None,
};
let response = xion.commit_any::<u64>(
vec![Any {
type_url: msg.type_url(),
value: msg.to_any().value,
}],
None,
)?;

relayer.check_ibc("xion-testnet-1", response)?;

Ok(())
}
105 changes: 105 additions & 0 deletions packages/interchain/hermes-relayer/src/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::core::HermesRelayer;
use cosmwasm_std::IbcOrder;
use cw_orch_interchain_core::env::ChainId;
use cw_orch_interchain_daemon::ChannelCreator;
use cw_orch_interchain_daemon::InterchainDaemonError;
use ibc_relayer::chain::requests::{IncludeProof, QueryClientStateRequest, QueryConnectionRequest};
use ibc_relayer::chain::{handle::ChainHandle, requests::QueryHeight};
use ibc_relayer::channel::Channel;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;
use ibc_relayer_cli::cli_utils::spawn_chain_runtime;
use ibc_relayer_types::core::ics03_connection::connection::IdentifiedConnectionEnd;
use ibc_relayer_types::core::ics04_channel::channel::Ordering;
use ibc_relayer_types::core::ics24_host::identifier::{self};

impl ChannelCreator for HermesRelayer {
fn create_ibc_channel(
&self,
src_chain: ChainId,
dst_chain: ChainId,
src_port: &old_ibc_relayer_types::core::ics24_host::identifier::PortId,
dst_port: &old_ibc_relayer_types::core::ics24_host::identifier::PortId,
version: &str,
order: Option<IbcOrder>,
) -> Result<String, InterchainDaemonError> {
let src_connection = self
.connection_ids
.get(&(src_chain.to_string(), dst_chain.to_string()))
.unwrap();

let config = self.duplex_config(src_chain, dst_chain);

// Validate & spawn runtime for side a.
let chain_a =
spawn_chain_runtime(&config, &identifier::ChainId::from_string(src_chain)).unwrap();

self.add_key(&chain_a);
// Query the connection end.
let (conn_end, _) = chain_a
.query_connection(
QueryConnectionRequest {
connection_id: src_connection.parse().unwrap(),
height: QueryHeight::Latest,
},
IncludeProof::No,
)
.unwrap();

// Query the client state, obtain the identifier of chain b.
let chain_b = chain_a
.query_client_state(
QueryClientStateRequest {
client_id: conn_end.client_id().clone(),
height: QueryHeight::Latest,
},
IncludeProof::No,
)
.map(|(cs, _)| cs.chain_id())
.unwrap();

// Spawn the runtime for side b.
let chain_b = spawn_chain_runtime(&config, &chain_b).unwrap();
self.add_key(&chain_b);

// Create the foreign client handles.
let client_a =
ForeignClient::find(chain_b.clone(), chain_a.clone(), conn_end.client_id()).unwrap();

let client_b =
ForeignClient::find(chain_a, chain_b, conn_end.counterparty().client_id()).unwrap();

let identified_end =
IdentifiedConnectionEnd::new(src_connection.parse().unwrap(), conn_end);

let connection = Connection::find(client_a, client_b, &identified_end).unwrap();

Channel::new(
connection,
cosmwasm_to_hermes_order(order),
src_port.to_string().parse().unwrap(),
dst_port.to_string().parse().unwrap(),
Some(version.to_string().into()),
)
.unwrap();

Ok(src_connection.to_string())
}

Check warning on line 87 in packages/interchain/hermes-relayer/src/channel.rs

View check run for this annotation

Codecov / codecov/patch

packages/interchain/hermes-relayer/src/channel.rs#L17-L87

Added lines #L17 - L87 were not covered by tests

fn interchain_env(&self) -> cw_orch_interchain_daemon::DaemonInterchainEnv<Self> {
unimplemented!("
The Hermes Relayer is a channel creator as well as an Interchain env.
You don't need to use this function, you can simply await packets directly on this structure"
)

Check warning on line 93 in packages/interchain/hermes-relayer/src/channel.rs

View check run for this annotation

Codecov / codecov/patch

packages/interchain/hermes-relayer/src/channel.rs#L89-L93

Added lines #L89 - L93 were not covered by tests
}
}

fn cosmwasm_to_hermes_order(order: Option<IbcOrder>) -> Ordering {
match order {
Some(order) => match order {
IbcOrder::Unordered => Ordering::Unordered,
IbcOrder::Ordered => Ordering::Ordered,

Check warning on line 101 in packages/interchain/hermes-relayer/src/channel.rs

View check run for this annotation

Codecov / codecov/patch

packages/interchain/hermes-relayer/src/channel.rs#L97-L101

Added lines #L97 - L101 were not covered by tests
},
None => Ordering::Unordered,

Check warning on line 103 in packages/interchain/hermes-relayer/src/channel.rs

View check run for this annotation

Codecov / codecov/patch

packages/interchain/hermes-relayer/src/channel.rs#L103

Added line #L103 was not covered by tests
}
}

Check warning on line 105 in packages/interchain/hermes-relayer/src/channel.rs

View check run for this annotation

Codecov / codecov/patch

packages/interchain/hermes-relayer/src/channel.rs#L105

Added line #L105 was not covered by tests
65 changes: 65 additions & 0 deletions packages/interchain/hermes-relayer/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::time::Duration;

use cw_orch_core::environment::ChainInfoOwned;
use ibc_relayer::chain::cosmos::config::CosmosSdkConfig;
use ibc_relayer::config::gas_multiplier::GasMultiplier;
use ibc_relayer::config::types::{MaxMsgNum, MaxTxSize, Memo};
use ibc_relayer::config::{AddressType, ChainConfig, EventSourceMode, GasPrice, RefreshRate};
use ibc_relayer::keyring::Store;
use ibc_relayer_types::core::ics02_client::trust_threshold::TrustThreshold;
use ibc_relayer_types::core::ics24_host::identifier::{self};

pub const KEY_NAME: &str = "relayer";

pub fn chain_config(
chain: &str,
rpc_url: &str,
chain_data: &ChainInfoOwned,
is_consumer_chain: bool,
) -> ChainConfig {
ChainConfig::CosmosSdk(CosmosSdkConfig {
id: identifier::ChainId::from_string(chain),

rpc_addr: rpc_url.parse().unwrap(),
grpc_addr: chain_data.grpc_urls[0].parse().unwrap(),
event_source: EventSourceMode::Pull {
interval: Duration::from_secs(4),
max_retries: 4,
},
rpc_timeout: Duration::from_secs(10),
trusted_node: false,
account_prefix: chain_data.network_info.pub_address_prefix.to_string(),
key_name: KEY_NAME.to_string(),
key_store_type: Store::Memory,
key_store_folder: None,
store_prefix: "ibc".to_string(),
default_gas: Some(100000),
max_gas: Some(2000000),
genesis_restart: None,
gas_adjustment: None,
gas_multiplier: Some(GasMultiplier::new(1.3).unwrap()),
fee_granter: None,
max_msg_num: MaxMsgNum::new(30).unwrap(),
max_tx_size: MaxTxSize::new(180000).unwrap(),
max_grpc_decoding_size: 33554432u64.into(),
clock_drift: Duration::from_secs(5),
max_block_time: Duration::from_secs(30),
trusting_period: None,
ccv_consumer_chain: is_consumer_chain,
memo_prefix: Memo::new("").unwrap(),
sequential_batch_tx: false,
proof_specs: None,
trust_threshold: TrustThreshold::new(1, 3).unwrap(),
gas_price: GasPrice::new(chain_data.gas_price, chain_data.gas_denom.to_string()),
packet_filter: Default::default(),
address_type: AddressType::Cosmos,
extension_options: Default::default(),
query_packets_chunk_size: 10,
client_refresh_rate: RefreshRate::new(5, 1),
memo_overwrite: Default::default(),
dynamic_gas_price: Default::default(),
compat_mode: Default::default(),
clear_interval: Default::default(),
excluded_sequences: Default::default(),
})
}

Check warning on line 65 in packages/interchain/hermes-relayer/src/config.rs

View check run for this annotation

Codecov / codecov/patch

packages/interchain/hermes-relayer/src/config.rs#L14-L65

Added lines #L14 - L65 were not covered by tests
Loading
Loading