Skip to content

Commit

Permalink
use env icp value (#475)
Browse files Browse the repository at this point in the history
* change init_event_monitor name to init_event_source

* use env value for icp config
  • Loading branch information
DaviRain-Su authored Jan 3, 2024
1 parent 3f8a7e6 commit ca9d0c9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
5 changes: 0 additions & 5 deletions crates/relayer-cli/src/chain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::collections::HashMap;
use std::fmt::Display;
use std::marker::Send;
use std::path::PathBuf;

use futures::future::join_all;
use http::Uri;
Expand All @@ -20,7 +19,6 @@ use ibc_chain_registry::querier::*;
use ibc_relayer::config::filter::{FilterPattern, PacketFilter};
use ibc_relayer::config::gas_multiplier::GasMultiplier;
use ibc_relayer::config::types::{MaxMsgNum, MaxTxSize, Memo};
use ibc_relayer::config::CanisterIdConfig;
use ibc_relayer::config::{default, AddressType, ChainConfig, EventSourceMode, GasPrice};
use ibc_relayer::keyring::Store;

Expand Down Expand Up @@ -124,9 +122,6 @@ where

Ok(ChainConfig {
id: chain_data.chain_id,
ic_endpoint: String::new(),
canister_id: CanisterIdConfig::default(),
canister_pem: PathBuf::new(),
near_ibc_address: ibc_relayer::config::NearIbcContractAddress::default(),
r#type: default::chain_type(),
rpc_addr: rpc_data.rpc_address,
Expand Down
41 changes: 22 additions & 19 deletions crates/relayer-cli/src/commands/tx/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ibc_relayer_types::core::ics02_client::msgs::create_client::MsgCreateClient;
use ibc_relayer_types::tx_msg::Msg;
use std::sync::Arc;
use std::thread;
use std::{env, path::PathBuf};

use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};
Expand All @@ -19,7 +20,6 @@ use ibc_relayer::chain::requests::{
};
use ibc_relayer::chain::tracking::TrackedMsgs;
use ibc_relayer::client_state::AnyClientState;
use ibc_relayer::config::ChainConfig;
use ibc_relayer::config::Config;
use ibc_relayer::error::Error as RelayerError;
use ibc_relayer::event::IbcEventWithHeight;
Expand Down Expand Up @@ -168,15 +168,12 @@ fn run_create_client(
fn run_only_init_vp(
config: &Config,
src_chain_id: &ChainId,
dst_chain_id: &ChainId,
_dst_chain_id: &ChainId,
clock_drift: Option<humantime::Duration>,
trusting_period: Option<humantime::Duration>,
trust_threshold: Option<TrustThreshold>,
) {
// this config use dst chain config to init vp
let chain_config = config
.find_chain(dst_chain_id)
.expect("not found dst chain Config");
let src_chain = match spawn_chain_runtime(&config, src_chain_id) {
Ok(handle) => handle,
Err(e) => Output::error(e).exit(),
Expand All @@ -190,7 +187,7 @@ fn run_only_init_vp(

let rt = Arc::new(TokioRuntime::new().unwrap());

let vp_client = match VpChain::new(src_chain_id.clone(), chain_config.clone(), rt) {
let vp_client = match VpChain::new(src_chain_id.clone(), rt) {
Ok(client) => client,
Err(e) => Output::error(e).exit(),
};
Expand Down Expand Up @@ -309,24 +306,28 @@ fn build_and_create_client_and_send(
#[derive(Debug, Clone)]
pub struct VpChain {
chain_id: ChainId,
config: ChainConfig,
rt: Arc<TokioRuntime>,
vp_client: VpClient,
signer: String,
canister_id: String,
}

impl VpChain {
pub fn new(
chain_id: ChainId,
chain_config: ChainConfig,
rt: Arc<TokioRuntime>,
) -> Result<Self, RelayerError> {
let canister_pem_path = if chain_config.canister_pem.is_absolute() {
chain_config.canister_pem.clone()
pub fn new(chain_id: ChainId, rt: Arc<TokioRuntime>) -> Result<Self, RelayerError> {
let canister_pem_path: PathBuf = env::var("CANISTER_PEM_PATH")
.map_err(|_| RelayerError::report_error("cann't read cansiter pem path env".into()))?
.into();
let ic_endpoint = env::var("IC_ENDPOINT")
.map_err(|_| RelayerError::report_error("cann't read ic endpoint env".into()))?;
let canister_id = env::var("CANISTER_ID")
.map_err(|_| RelayerError::report_error("cann't read cansiter id env".into()))?;

let canister_pem_path = if canister_pem_path.is_absolute() {
canister_pem_path.clone()
} else {
let current_dir =
std::env::current_dir().map_err(|e| RelayerError::report_error(e.to_string()))?;
current_dir.join(chain_config.canister_pem.clone())
current_dir.join(canister_pem_path.clone())
};

let signer = Secp256k1Identity::from_pem_file(canister_pem_path.clone()).map_err(|e| {
Expand All @@ -349,7 +350,7 @@ impl VpChain {
.to_text();

let vp_client = rt
.block_on(VpClient::new(&chain_config.ic_endpoint, &canister_pem_path))
.block_on(VpClient::new(&ic_endpoint, &canister_pem_path))
.map_err(|e| {
let position = std::panic::Location::caller();
RelayerError::report_error(format!(
Expand All @@ -361,10 +362,10 @@ impl VpChain {

Ok(Self {
chain_id,
config: chain_config.clone(),
vp_client,
rt,
signer: sender,
canister_id,
})
}

Expand All @@ -389,11 +390,13 @@ impl VpChain {
) -> Result<Vec<IbcEventWithHeight>, RelayerError> {
// let mut tracked_msgs = tracked_msgs.clone();
// if tracked_msgs.tracking_id().to_string() != "ft-transfer" {
let canister_id = self.config.canister_id.id.as_str();
let mut msgs: Vec<Any> = Vec::new();
for msg in tracked_msgs.messages() {
let res = self
.block_on(self.vp_client.deliver(canister_id, msg.encode_to_vec()))
.block_on(
self.vp_client
.deliver(&self.canister_id, msg.encode_to_vec()),
)
.map_err(|e| {
let position = std::panic::Location::caller();
RelayerError::report_error(format!(
Expand Down
46 changes: 29 additions & 17 deletions crates/relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use core::{
use futures::future::join_all;
use num_bigint::BigInt;
use prost::Message;
use std::{cmp::Ordering, thread};
use std::env;
use std::{cmp::Ordering, path::PathBuf, thread};

use tokio::runtime::Runtime as TokioRuntime;
use tonic::codegen::http::Uri;
Expand Down Expand Up @@ -154,6 +155,9 @@ pub struct CosmosSdkChain {
account: Option<Account>,

tx_monitor_cmd: Option<TxEventSourceCmd>,

/// config for icp config
canister_id: String,
}

impl CosmosSdkChain {
Expand Down Expand Up @@ -898,16 +902,24 @@ impl ChainEndpoint for CosmosSdkChain {

let tx_config = TxConfig::try_from(&config)?;

let canister_pem_path = if config.canister_pem.is_absolute() {
config.canister_pem.clone()
let canister_pem_path: PathBuf = env::var("CANISTER_PEM_PATH")
.map_err(|_| Error::report_error("cann't read cansiter pem path env".into()))?
.into();
let ic_endpoint = env::var("IC_ENDPOINT")
.map_err(|_| Error::report_error("cann't read ic endpoint env".into()))?;
let canister_id = env::var("CANISTER_ID")
.map_err(|_| Error::report_error("cann't read cansiter id env".into()))?;

let canister_pem_path = if canister_pem_path.is_absolute() {
canister_pem_path.clone()
} else {
let current_dir =
std::env::current_dir().map_err(|e| Error::report_error(e.to_string()))?;
current_dir.join(config.canister_pem.clone())
current_dir.join(canister_pem_path.clone())
};

let vp_client = rt
.block_on(VpClient::new(&config.ic_endpoint, &canister_pem_path))
.block_on(VpClient::new(&ic_endpoint, &canister_pem_path))
.map_err(|e| {
let position = std::panic::Location::caller();
Error::report_error(format!(
Expand All @@ -929,6 +941,7 @@ impl ChainEndpoint for CosmosSdkChain {
tx_config,
account: None,
tx_monitor_cmd: None,
canister_id,
};

Ok(chain)
Expand Down Expand Up @@ -1063,7 +1076,6 @@ impl ChainEndpoint for CosmosSdkChain {
);

use ibc_proto::google::protobuf::Any;
let canister_id = self.config.canister_id.id.clone();

let mut result = vec![];
if tracked_msgs.tracking_id().to_string() != "ft-transfer" {
Expand All @@ -1078,7 +1090,10 @@ impl ChainEndpoint for CosmosSdkChain {

info!("near msg envelope: {near_msg_envelope:?}");
let res = self
.block_on(self.vp_client.deliver(&canister_id, msg.encode_to_vec()))
.block_on(
self.vp_client
.deliver(&self.canister_id, msg.encode_to_vec()),
)
.map_err(|e| {
let position = std::panic::Location::caller();
Error::report_error(format!(
Expand Down Expand Up @@ -1133,7 +1148,6 @@ impl ChainEndpoint for CosmosSdkChain {
);

use ibc_proto::google::protobuf::Any;
let canister_id = self.config.canister_id.id.clone();

let mut result = vec![];
if tracked_msgs.tracking_id().to_string() != "ft-transfer" {
Expand All @@ -1148,7 +1162,10 @@ impl ChainEndpoint for CosmosSdkChain {

info!("near msg envelope: {near_msg_envelope:?}");
let res = self
.block_on(self.vp_client.deliver(&canister_id, msg.encode_to_vec()))
.block_on(
self.vp_client
.deliver(&self.canister_id, msg.encode_to_vec()),
)
.map_err(|e| {
let position = std::panic::Location::caller();
Error::report_error(format!(
Expand Down Expand Up @@ -1352,8 +1369,6 @@ impl ChainEndpoint for CosmosSdkChain {
crate::telemetry!(query, self.id(), "query_client_state");

if matches!(include_proof, IncludeProof::No) {
let canister_id = self.config.canister_id.id.as_str();

let serialized_request = serde_json::to_vec(&request).map_err(|e| {
let position = std::panic::Location::caller();
Error::report_error(format!(
Expand All @@ -1364,7 +1379,7 @@ impl ChainEndpoint for CosmosSdkChain {
let res = self
.block_on(
self.vp_client
.query_client_state(canister_id, serialized_request),
.query_client_state(&self.canister_id, serialized_request),
)
.map_err(|e| {
let position = std::panic::Location::caller();
Expand Down Expand Up @@ -1463,11 +1478,10 @@ impl ChainEndpoint for CosmosSdkChain {
);

crate::telemetry!(query, self.id(), "query_consensus_state_heights");
let canister_id = self.config.canister_id.id.as_str();
inner_query_consensus_state_heights(
self.rt.clone(),
self.vp_client.clone(),
canister_id,
&self.canister_id,
request,
)
}
Expand All @@ -1488,8 +1502,6 @@ impl ChainEndpoint for CosmosSdkChain {
assert!(matches!(include_proof, IncludeProof::No));
assert!(request.client_id.to_string().starts_with("06-solomachine"));

let canister_id = self.config.canister_id.id.as_str();

let serialized_request = serde_json::to_vec(&request).map_err(|e| {
let position = std::panic::Location::caller();
Error::report_error(format!(
Expand All @@ -1501,7 +1513,7 @@ impl ChainEndpoint for CosmosSdkChain {
let res = self
.block_on(
self.vp_client
.query_consensus_state(canister_id, serialized_request),
.query_consensus_state(&self.canister_id, serialized_request),
)
.map_err(|e| {
let position = std::panic::Location::caller();
Expand Down
10 changes: 3 additions & 7 deletions crates/relayer/src/chain/near/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ impl NearIbcContract for NearChain {
}

impl NearChain {
fn init_event_monitor(&self) -> Result<TxMonitorCmd, Error> {
fn init_event_source(&self) -> Result<TxMonitorCmd, Error> {
info!("initializing event monitor");
crate::time!("init_event_monitor");
crate::time!("init_event_source");

let (event_monitor, monitor_tx) = NearEventMonitor::new(
self.config.id.clone(),
Expand All @@ -167,10 +167,6 @@ impl NearChain {
&self.config
}

pub fn canister_id(&self) -> &str {
&self.config.canister_id.id
}

/// Subscribe Events
/// todo near don't have events subscription
pub fn subscribe_ibc_events(&self) -> Result<Vec<IbcRelayerTypeEvent>, Error> {
Expand Down Expand Up @@ -1653,7 +1649,7 @@ impl ChainEndpoint for NearChain {
let tx_monitor_cmd = match &self.tx_monitor_cmd {
Some(tx_monitor_cmd) => tx_monitor_cmd,
None => {
let tx_monitor_cmd = self.init_event_monitor()?;
let tx_monitor_cmd = self.init_event_source()?;
self.tx_monitor_cmd = Some(tx_monitor_cmd);
self.tx_monitor_cmd.as_ref().ok_or(Error::report_error(
"subscribe tx_monitor_cmd is None".to_string(),
Expand Down
9 changes: 0 additions & 9 deletions crates/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,6 @@ pub struct ChainConfig {
/// The chain's network identifier
pub id: ChainId,

#[serde(default = "default::default_canister_id")]
pub canister_id: CanisterIdConfig,

#[serde(default = "default::default_canister_pem_path")]
pub canister_pem: PathBuf,

#[serde(default = "default::default_ic_endpoint")]
pub ic_endpoint: String,

/// The chain type
#[serde(default = "default::near_ibc_contract_address")]
pub near_ibc_address: NearIbcContractAddress,
Expand Down
5 changes: 1 addition & 4 deletions tools/test-framework/src/types/single/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use ibc_relayer::chain::ChainType;
use ibc_relayer::config;
use ibc_relayer::config::compat_mode::CompatMode;
use ibc_relayer::config::gas_multiplier::GasMultiplier;
use ibc_relayer::config::CanisterIdConfig;
use ibc_relayer::config::NearIbcContractAddress;
use ibc_relayer::keyring::Store;
use ibc_relayer_types::core::ics24_host::identifier::ChainId;
Expand Down Expand Up @@ -147,11 +146,9 @@ impl FullNode {
Ok(config::ChainConfig {
id: self.chain_driver.chain_id.clone(),
r#type: ChainType::CosmosSdk,
ic_endpoint: test_config.ic_endpoint.clone(),
canister_pem: test_config.canister_pem.clone(),

near_ibc_address: NearIbcContractAddress::from_str(&test_config.near_ibc_address)
.unwrap(),
canister_id: CanisterIdConfig::from_str(&test_config.canister_id).unwrap(),
rpc_addr: Url::from_str(&self.chain_driver.rpc_address())?,
grpc_addr: Url::from_str(&self.chain_driver.grpc_address())?,
event_source: config::EventSourceMode::Push {
Expand Down

0 comments on commit ca9d0c9

Please sign in to comment.