Skip to content

Update/add hermes relayer #427

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ speculoos = "0.11.0"
log = "0.4.22"

# Interchain
ibc-relayer-types = { version = "0.29.2" }
ibc-relayer = { version = "0.29.3" }
ibc-relayer-types = { version = "0.29.3" }
ibc-chain-registry = { version = "0.29.2" }
40 changes: 40 additions & 0 deletions packages/interchain/hermes-relayer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[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 = { workspace = true }
cw-orch-interchain-daemon = { workspace = true }
cw-orch-networks = { workspace = true }
cw-orch-core = { workspace = true }
cw-orch-traits = { workspace = true }
cw-orch-daemon = { workspace = true }


hdpath = "0.6.3"
ibc-relayer = { workspace = true }
ibc-relayer-cli = "1.9.0"
ibc-relayer-types = { workspace = true }
tokio.workspace = true
log.workspace = true
cosmrs.workspace = true
futures = "0.3.30"
tonic.workspace = true
futures-util = "0.3.30"
async-recursion = "1.1.1"
serde_json.workspace = true
prost-types = { workspace = true }

[dev-dependencies]
cw-orch = { workspace = true, features = ["daemon"] }
cw-orch-interchain = { workspace = true, features = ["daemon"] }
dotenv = "0.15.0"
ibc-proto = "0.47.0"
pretty_env_logger = "0.5.0"
89 changes: 89 additions & 0 deletions packages/interchain/hermes-relayer/examples/pion-xion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 ibc_relayer_types::core::ics24_host::identifier::PortId;
use ibc_relayer_types::tx_msg::Msg;
use 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.get_chain("xion-testnet-1")?;
let pion = relayer.get_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_addr().to_string().parse().unwrap(),
receiver: pion.sender_addr().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![prost_types::Any {
type_url: msg.type_url(),
value: msg.to_any().value,
}],
None,
)?;

relayer.await_and_check_packets("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: &ibc_relayer_types::core::ics24_host::identifier::PortId,
dst_port: &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())
}

fn interchain_env(&self) -> cw_orch_interchain_daemon::DaemonInterchain<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"
)
}
}

fn cosmwasm_to_hermes_order(order: Option<IbcOrder>) -> Ordering {
match order {
Some(order) => match order {
IbcOrder::Unordered => Ordering::Unordered,
IbcOrder::Ordered => Ordering::Ordered,
},
None => Ordering::Unordered,
}
}
66 changes: 66 additions & 0 deletions packages/interchain/hermes-relayer/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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(),
allow_ccq: true,
})
}
Loading
Loading