From c25e5344c406aa61720b58a49b352a089eb5b3cf Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Thu, 7 Jan 2021 14:22:30 +0200 Subject: [PATCH 01/12] Implemented the 'query connection channels' command --- relayer-cli/src/commands/query.rs | 4 + relayer-cli/src/commands/query/connection.rs | 78 ++++++++++++++++++++ relayer/src/chain.rs | 10 ++- relayer/src/chain/cosmos.rs | 36 +++++++-- relayer/src/chain/mock.rs | 13 +++- 5 files changed, 131 insertions(+), 10 deletions(-) diff --git a/relayer-cli/src/commands/query.rs b/relayer-cli/src/commands/query.rs index e6147b4ba7..5888eb9da5 100644 --- a/relayer-cli/src/commands/query.rs +++ b/relayer-cli/src/commands/query.rs @@ -47,6 +47,10 @@ pub enum QueryConnectionCmds { /// The `query connection end` subcommand #[options(help = "query connection end")] End(connection::QueryConnectionEndCmd), + + /// The `query connection channels` subcommand + #[options(help = "query connection channels")] + Channels(connection::QueryConnectionChannelsCmd), } #[derive(Command, Debug, Options, Runnable)] diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index db330286bd..50db03947b 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -13,6 +13,7 @@ use relayer::config::{ChainConfig, Config}; use crate::error::{Error, Kind}; use crate::prelude::*; +use ibc_proto::ibc::core::channel::v1::QueryConnectionChannelsRequest; #[derive(Clone, Command, Debug, Options)] pub struct QueryConnectionEndCmd { @@ -98,6 +99,83 @@ impl Runnable for QueryConnectionEndCmd { } } +/// Command for querying the channel identifiers associated with a connection. +/// Sample invocation: +/// `cargo run --bin relayer -- -c simple_config.toml query connection channels ibc-0 connection-0` +#[derive(Clone, Command, Debug, Options)] +pub struct QueryConnectionChannelsCmd { + #[options(free, help = "identifier of the chain to query")] + chain_id: Option, + + #[options(free, help = "identifier of the connection to query")] + connection_id: Option, +} + +#[derive(Debug)] +struct QueryConnectionChannelsOptions { + connection_id: ConnectionId, +} + +impl QueryConnectionChannelsCmd { + fn validate_options( + &self, + config: &Config, + ) -> Result<(ChainConfig, QueryConnectionChannelsOptions), String> { + let chain_id = self + .chain_id + .clone() + .ok_or_else(|| "no chain chain identifier provided".to_string())?; + let chain_config = config + .chains + .iter() + .find(|c| c.id == chain_id) + .ok_or_else(|| "missing chain configuration for the given chain id".to_string())?; + + let connection_id = self + .connection_id + .as_ref() + .ok_or_else(|| "no connection identifier was provided".to_string())? + .parse() + .map_err(|err: ValidationError| err.to_string())?; + + let opts = QueryConnectionChannelsOptions { connection_id }; + + Ok((chain_config.clone(), opts)) + } +} + +impl Runnable for QueryConnectionChannelsCmd { + fn run(&self) { + let config = app_config(); + + let (chain_config, opts) = match self.validate_options(&config) { + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + Ok(result) => result, + }; + status_info!("Options", "{:?}", opts); + + let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); + let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap(); + + let req = QueryConnectionChannelsRequest { + connection: opts.connection_id.to_string(), + pagination: None, + }; + + let res: Result<_, Error> = chain + .query_connection_channels(req) + .map_err(|e| Kind::Query.context(e).into()); + + match res { + Ok(cs) => status_info!("Result for connection channels query: ", "{:?}", cs), + Err(e) => status_info!("Error encountered on channel end query:", "{}", e), + } + } +} + #[cfg(test)] mod tests { use crate::commands::query::connection::QueryConnectionEndCmd; diff --git a/relayer/src/chain.rs b/relayer/src/chain.rs index 592ccf3dc2..1f320f6cdd 100644 --- a/relayer/src/chain.rs +++ b/relayer/src/chain.rs @@ -23,8 +23,8 @@ use tendermint::account::Id as AccountId; use tendermint::block::Height; use ibc_proto::ibc::core::channel::v1::{ - PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, - QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryConnectionChannelsRequest, QueryPacketAcknowledgementsRequest, + QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use ibc_proto::ibc::core::commitment::v1::MerkleProof; @@ -337,6 +337,12 @@ pub trait Chain: Sized { request: QueryUnreceivedAcksRequest, ) -> Result, Error>; + /// Performs a query to retrieve the identifiers of all channels associated with a connection. + fn query_connection_channels( + &self, + request: QueryConnectionChannelsRequest, + ) -> Result, Error>; + fn build_packet_proofs( &self, packet_type: PacketMsgType, diff --git a/relayer/src/chain/cosmos.rs b/relayer/src/chain/cosmos.rs index aef18dc56f..38d41a3d7f 100644 --- a/relayer/src/chain/cosmos.rs +++ b/relayer/src/chain/cosmos.rs @@ -36,8 +36,8 @@ use ibc_proto::cosmos::tx::v1beta1::{AuthInfo, Fee, ModeInfo, SignDoc, SignerInf // Support for GRPC use ibc_proto::cosmos::auth::v1beta1::{BaseAccount, QueryAccountRequest}; use ibc_proto::ibc::core::channel::v1::{ - PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, - QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryConnectionChannelsRequest, QueryPacketAcknowledgementsRequest, + QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use ibc_proto::ibc::core::commitment::v1::MerkleProof; @@ -51,7 +51,7 @@ use ibc::ics07_tendermint::header::Header as TMHeader; use ibc::ics23_commitment::commitment::CommitmentPrefix; use ibc::ics23_commitment::merkle::convert_tm_to_ics_merkle_proof; -use ibc::ics24_host::identifier::{ChainId, ClientId}; +use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId}; use ibc::ics24_host::Path::ClientConsensusState as ClientConsensusPath; use ibc::ics24_host::Path::ClientState as ClientStatePath; use ibc::ics24_host::{Path, IBC_QUERY_PATH}; @@ -516,7 +516,6 @@ impl Chain for CosmosSDKChain { Ok(key) } /// Queries the packet commitment hashes associated with a channel. - /// TODO - move to the chain trait fn query_packet_commitments( &self, request: QueryPacketCommitmentsRequest, @@ -604,7 +603,6 @@ impl Chain for CosmosSDKChain { } /// Queries the packet commitment hashes associated with a channel. - /// TODO - move the chain trait fn query_unreceived_acknowledgements( &self, request: QueryUnreceivedAcksRequest, @@ -655,6 +653,34 @@ impl Chain for CosmosSDKChain { } Ok(result) } + + fn query_connection_channels( + &self, + request: QueryConnectionChannelsRequest, + ) -> Result, Error> { + let grpc_addr = + Uri::from_str(&self.config().grpc_addr).map_err(|e| Kind::Grpc.context(e))?; + let mut client = self + .block_on( + ibc_proto::ibc::core::channel::v1::query_client::QueryClient::connect(grpc_addr), + )? + .map_err(|e| Kind::Grpc.context(e))?; + + let request = tonic::Request::new(request); + + let response = self + .block_on(client.connection_channels(request))? + .map_err(|e| Kind::Grpc.context(e))? + .into_inner(); + + let vec_ids = response + .channels + .iter() + .map(|ic| ChannelId::from_str(ic.channel_id.as_str()).unwrap()) + .collect(); + + Ok(vec_ids) + } } fn packet_query(request: &QueryPacketEventDataRequest, seq: &Sequence) -> Result { diff --git a/relayer/src/chain/mock.rs b/relayer/src/chain/mock.rs index 00bb753feb..9b216f9e9d 100644 --- a/relayer/src/chain/mock.rs +++ b/relayer/src/chain/mock.rs @@ -11,8 +11,8 @@ use tendermint::account::Id; use tendermint_testgen::light_block::TMLightBlock; use ibc_proto::ibc::core::channel::v1::{ - PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, - QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryConnectionChannelsRequest, QueryPacketAcknowledgementsRequest, + QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use ibc_proto::ibc::core::commitment::v1::MerkleProof; @@ -25,7 +25,7 @@ use ibc::ics07_tendermint::consensus_state::ConsensusState as TendermintConsensu use ibc::ics07_tendermint::header::Header as TendermintHeader; use ibc::ics18_relayer::context::ICS18Context; use ibc::ics23_commitment::commitment::CommitmentPrefix; -use ibc::ics24_host::identifier::{ChainId, ClientId}; +use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId}; use ibc::ics24_host::Path; use ibc::mock::context::MockContext; use ibc::mock::host::HostType; @@ -228,6 +228,13 @@ impl Chain for MockChain { unimplemented!() } + fn query_connection_channels( + &self, + _request: QueryConnectionChannelsRequest, + ) -> Result, Error> { + unimplemented!() + } + fn query_txs(&self, _request: QueryPacketEventDataRequest) -> Result, Error> { unimplemented!() } From 0c72d93599fc9d6e1c95921e8114b0a8cfd44121 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Thu, 7 Jan 2021 14:32:58 +0200 Subject: [PATCH 02/12] Avoid calls to unwrap --- relayer/src/chain/cosmos.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/relayer/src/chain/cosmos.rs b/relayer/src/chain/cosmos.rs index 38d41a3d7f..9a47666571 100644 --- a/relayer/src/chain/cosmos.rs +++ b/relayer/src/chain/cosmos.rs @@ -547,7 +547,6 @@ impl Chain for CosmosSDKChain { } /// Queries the packet commitment hashes associated with a channel. - /// TODO - move the chain trait fn query_unreceived_packets( &self, request: QueryUnreceivedPacketsRequest, @@ -571,7 +570,6 @@ impl Chain for CosmosSDKChain { } /// Queries the packet acknowledgment hashes associated with a channel. - /// TODO - move to the chain trait fn query_packet_acknowledgements( &self, request: QueryPacketAcknowledgementsRequest, @@ -676,7 +674,7 @@ impl Chain for CosmosSDKChain { let vec_ids = response .channels .iter() - .map(|ic| ChannelId::from_str(ic.channel_id.as_str()).unwrap()) + .filter_map(|ic| ChannelId::from_str(ic.channel_id.as_str()).ok()) .collect(); Ok(vec_ids) From 6975f13de441462e5730690fb3a48573620fb164 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Thu, 7 Jan 2021 14:35:07 +0200 Subject: [PATCH 03/12] Updated the changelog --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3375040a77..8494df6dad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ ## Unreleased Changes +### FEATURES + +- [relayer-cli] + - Implement command to query the channels associated with a connection ([#505]) + +### IMPROVEMENTS + + +[#505]: https://github.com/informalsystems/ibc-rs/issues/505 + ## v0.0.6 *December 23, 2020* From b43d098d5423089f7576361781de0f1fbe202a7a Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Thu, 7 Jan 2021 14:42:07 +0200 Subject: [PATCH 04/12] Added basic CLI test --- relayer-cli/src/commands/query/connection.rs | 94 +++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index 50db03947b..b1880e0c99 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -178,7 +178,7 @@ impl Runnable for QueryConnectionChannelsCmd { #[cfg(test)] mod tests { - use crate::commands::query::connection::QueryConnectionEndCmd; + use crate::commands::query::connection::{QueryConnectionEndCmd, QueryConnectionChannelsCmd}; use relayer::config::parse; #[test] @@ -274,4 +274,96 @@ mod tests { } } } + + #[test] + fn parse_query_connection_channels_parameters() { + let default_params = QueryConnectionChannelsCmd { + chain_id: Some("ibc-0".to_string().parse().unwrap()), + connection_id: Some("ibconeconnection".to_string().parse().unwrap()), + }; + + struct Test { + name: String, + params: QueryConnectionChannelsCmd, + want_pass: bool, + } + + let tests: Vec = vec![ + Test { + name: "Good parameters".to_string(), + params: default_params.clone(), + want_pass: true, + }, + Test { + name: "No chain specified".to_string(), + params: QueryConnectionChannelsCmd { + chain_id: None, + ..default_params.clone() + }, + want_pass: false, + }, + Test { + name: "Chain not configured".to_string(), + params: QueryConnectionChannelsCmd { + chain_id: Some("ibc007".to_string().parse().unwrap()), + ..default_params.clone() + }, + want_pass: false, + }, + Test { + name: "No connection id specified".to_string(), + params: QueryConnectionChannelsCmd { + connection_id: None, + ..default_params.clone() + }, + want_pass: false, + }, + Test { + name: "Bad connection, non-alpha".to_string(), + params: QueryConnectionChannelsCmd { + connection_id: Some("connection-0^".to_string()), + ..default_params.clone() + }, + want_pass: false, + }, + Test { + name: "Bad connection, name too short".to_string(), + params: QueryConnectionChannelsCmd { + connection_id: Some("connshort".to_string()), + ..default_params + }, + want_pass: false, + }, + ] + .into_iter() + .collect(); + + let path = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/fixtures/two_chains.toml" + ); + + let config = parse(path).unwrap(); + + for test in tests { + let res = test.params.validate_options(&config); + + match res { + Ok(_res) => { + assert!( + test.want_pass, + "validate_options should have failed for test {}", + test.name + ); + } + Err(err) => { + assert!( + !test.want_pass, + "validate_options failed for test {}, \nerr {}", + test.name, err + ); + } + } + } + } } From 4bf44ac7b6bcc17d56712d2202217e16bd44f1fc Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Thu, 7 Jan 2021 15:34:47 +0200 Subject: [PATCH 05/12] Fix FMT --- relayer-cli/src/commands/query/connection.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index b1880e0c99..3e892e5512 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -178,7 +178,7 @@ impl Runnable for QueryConnectionChannelsCmd { #[cfg(test)] mod tests { - use crate::commands::query::connection::{QueryConnectionEndCmd, QueryConnectionChannelsCmd}; + use crate::commands::query::connection::{QueryConnectionChannelsCmd, QueryConnectionEndCmd}; use relayer::config::parse; #[test] @@ -335,12 +335,12 @@ mod tests { want_pass: false, }, ] - .into_iter() - .collect(); + .into_iter() + .collect(); let path = concat!( - env!("CARGO_MANIFEST_DIR"), - "/tests/fixtures/two_chains.toml" + env!("CARGO_MANIFEST_DIR"), + "/tests/fixtures/two_chains.toml" ); let config = parse(path).unwrap(); From 90b885faba374f03244a38352de1c65181994461 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Fri, 8 Jan 2021 15:34:41 +0200 Subject: [PATCH 06/12] Added JSON output option --- relayer-cli/src/commands/query/connection.rs | 32 ++++++--- relayer-cli/src/conclude.rs | 75 ++++++++++++++++++++ relayer-cli/src/lib.rs | 1 + 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 relayer-cli/src/conclude.rs diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index 3e892e5512..b0425d73a9 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -7,13 +7,13 @@ use ibc::ics03_connection::connection::ConnectionEnd; use ibc::ics24_host::error::ValidationError; use ibc::ics24_host::identifier::ChainId; use ibc::ics24_host::identifier::ConnectionId; - +use ibc_proto::ibc::core::channel::v1::QueryConnectionChannelsRequest; use relayer::chain::{Chain, CosmosSDKChain}; use relayer::config::{ChainConfig, Config}; +use crate::conclude::{on_exit, CommandOutput, CommandStatus}; use crate::error::{Error, Kind}; use crate::prelude::*; -use ibc_proto::ibc::core::channel::v1::QueryConnectionChannelsRequest; #[derive(Clone, Command, Debug, Options)] pub struct QueryConnectionEndCmd { @@ -109,6 +109,9 @@ pub struct QueryConnectionChannelsCmd { #[options(free, help = "identifier of the connection to query")] connection_id: Option, + + #[options(help = "enable output in JSON format")] + json: bool, } #[derive(Debug)] @@ -150,12 +153,16 @@ impl Runnable for QueryConnectionChannelsCmd { let (chain_config, opts) = match self.validate_options(&config) { Err(err) => { - status_err!("invalid options: {}", err); - return; + return on_exit( + self.json, + CommandOutput::new(CommandStatus::Error).with_msg(err), + ); } Ok(result) => result, }; - status_info!("Options", "{:?}", opts); + if !self.json { + status_info!("Options", "{:?}", opts); + } let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap(); @@ -170,17 +177,25 @@ impl Runnable for QueryConnectionChannelsCmd { .map_err(|e| Kind::Query.context(e).into()); match res { - Ok(cs) => status_info!("Result for connection channels query: ", "{:?}", cs), - Err(e) => status_info!("Error encountered on channel end query:", "{}", e), + Ok(cs) => on_exit( + self.json, + CommandOutput::new(CommandStatus::Success).with_msg(format!("{:?}", cs)), + ), + Err(e) => on_exit( + self.json, + CommandOutput::new(CommandStatus::Error) + .with_msg(format!("error encountered: {:?}", e)), + ), } } } #[cfg(test)] mod tests { - use crate::commands::query::connection::{QueryConnectionChannelsCmd, QueryConnectionEndCmd}; use relayer::config::parse; + use crate::commands::query::connection::{QueryConnectionChannelsCmd, QueryConnectionEndCmd}; + #[test] fn parse_connection_query_end_parameters() { let default_params = QueryConnectionEndCmd { @@ -280,6 +295,7 @@ mod tests { let default_params = QueryConnectionChannelsCmd { chain_id: Some("ibc-0".to_string().parse().unwrap()), connection_id: Some("ibconeconnection".to_string().parse().unwrap()), + json: false, }; struct Test { diff --git a/relayer-cli/src/conclude.rs b/relayer-cli/src/conclude.rs new file mode 100644 index 0000000000..979b9aa882 --- /dev/null +++ b/relayer-cli/src/conclude.rs @@ -0,0 +1,75 @@ +#![allow(dead_code)] +//! Handles the return message (command output) and return code from a CLI command. +//! Most of the code here is temporary/experimental. +//! https://github.com/informalsystems/ibc-rs/issues/500 will introduce a more permanent solution. + +use std::collections::HashMap; + +use serde::Serialize; + +pub fn on_exit(json: bool, out: CommandOutput) { + // Handle the output message + if json { + println!("{}", serde_json::to_string(&out).unwrap()); + } else { + println!("status={:?}\nmsg={:?}", out.status, out.msg); + } + + // The return code + if out.status == CommandStatus::Error { + std::process::exit(1); + } else { + std::process::exit(0); + } +} + +/// A CLI output with support for JSON serialization. +#[derive(Serialize, Debug)] +pub struct CommandOutput { + /// The return status + pub status: CommandStatus, + + /// An arbitrary message accompanying the output + pub msg: Option, + + /// Additional arbitrary key/values that may be present in the output + /// TODO: at the moment this is not used! + #[serde(flatten)] + pub extra: HashMap, +} + +impl CommandOutput { + pub fn new(status: CommandStatus) -> Self { + CommandOutput { + status, + msg: None, + extra: Default::default(), + } + } + + pub fn with_msg(self, msg: String) -> Self { + CommandOutput { + msg: Some(msg), + ..self + } + } + + pub fn with_extra(self, k: String, v: String) -> Self { + let mut extra = self.extra.clone(); + extra.insert(k, v); + + CommandOutput { extra, ..self } + } +} + +#[derive(Serialize, Debug, PartialEq, Eq)] +pub enum CommandStatus { + #[serde(rename(serialize = "success"))] + Success, + + #[serde(rename(serialize = "info"))] + Info, + + #[serde(rename(serialize = "error"))] + Error, +} diff --git a/relayer-cli/src/lib.rs b/relayer-cli/src/lib.rs index 6a91c6fee7..0e13022e2b 100644 --- a/relayer-cli/src/lib.rs +++ b/relayer-cli/src/lib.rs @@ -18,6 +18,7 @@ pub mod application; pub mod commands; +mod conclude; pub mod config; pub mod error; pub mod prelude; From fb2e270183b82efd7a59d94abda3bc5f43093216 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Tue, 12 Jan 2021 11:55:28 +0200 Subject: [PATCH 07/12] Overrriding default tracing cfg [WIP] --- relayer-cli/src/application.rs | 21 +++++++++++++++++--- relayer-cli/src/commands/query/connection.rs | 3 +++ relayer-cli/src/lib.rs | 1 + 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/relayer-cli/src/application.rs b/relayer-cli/src/application.rs index 25cbc278e8..933316aea3 100644 --- a/relayer-cli/src/application.rs +++ b/relayer-cli/src/application.rs @@ -1,11 +1,13 @@ //! Cli Abscissa Application -use crate::{commands::CliCmd, config::Config}; use abscissa_core::{ application::{self, AppCell}, - config, trace, Application, EntryPoint, FrameworkError, StandardPaths, + component::Component, + Application, config, EntryPoint, FrameworkError, StandardPaths, trace, }; +use crate::{commands::CliCmd, config::Config, components::Tracing}; + /// Application state pub static APPLICATION: AppCell = AppCell::new(); @@ -76,13 +78,26 @@ impl Application for CliApp { &mut self.state } + fn framework_components( + &mut self, + command: &Self::Cmd, + ) -> Result>>, FrameworkError> { + Ok(vec![]) + } + /// Register all components used by this application. /// /// If you would like to add additional components to your application /// beyond the default ones provided by the framework, this is the place /// to do so. fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> { - let components = self.framework_components(command)?; + let mut components = self.framework_components(command)?; + + tracing::info!("pre-push"); + let c = Tracing::new()?; + components.push(Box::new(c)); + tracing::info!("post-push"); + self.state.components.register(components) } diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index b0425d73a9..e0b9166d4a 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -150,6 +150,9 @@ impl QueryConnectionChannelsCmd { impl Runnable for QueryConnectionChannelsCmd { fn run(&self) { let config = app_config(); + tracing::debug!("post-push ^2"); + tracing::info!("post-push ^2"); + tracing::error!("ERROROROOROR"); let (chain_config, opts) = match self.validate_options(&config) { Err(err) => { diff --git a/relayer-cli/src/lib.rs b/relayer-cli/src/lib.rs index 0e13022e2b..42a5d9bbce 100644 --- a/relayer-cli/src/lib.rs +++ b/relayer-cli/src/lib.rs @@ -22,3 +22,4 @@ mod conclude; pub mod config; pub mod error; pub mod prelude; +mod components; From 6e7b46c164b7c4aa501973a81c0f720f5e8e7e66 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Tue, 12 Jan 2021 12:02:54 +0200 Subject: [PATCH 08/12] Revert "Overrriding default tracing cfg [WIP]" This reverts commit fb2e270183b82efd7a59d94abda3bc5f43093216. --- relayer-cli/src/application.rs | 21 +++----------------- relayer-cli/src/commands/query/connection.rs | 3 --- relayer-cli/src/lib.rs | 1 - 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/relayer-cli/src/application.rs b/relayer-cli/src/application.rs index 933316aea3..25cbc278e8 100644 --- a/relayer-cli/src/application.rs +++ b/relayer-cli/src/application.rs @@ -1,13 +1,11 @@ //! Cli Abscissa Application +use crate::{commands::CliCmd, config::Config}; use abscissa_core::{ application::{self, AppCell}, - component::Component, - Application, config, EntryPoint, FrameworkError, StandardPaths, trace, + config, trace, Application, EntryPoint, FrameworkError, StandardPaths, }; -use crate::{commands::CliCmd, config::Config, components::Tracing}; - /// Application state pub static APPLICATION: AppCell = AppCell::new(); @@ -78,26 +76,13 @@ impl Application for CliApp { &mut self.state } - fn framework_components( - &mut self, - command: &Self::Cmd, - ) -> Result>>, FrameworkError> { - Ok(vec![]) - } - /// Register all components used by this application. /// /// If you would like to add additional components to your application /// beyond the default ones provided by the framework, this is the place /// to do so. fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> { - let mut components = self.framework_components(command)?; - - tracing::info!("pre-push"); - let c = Tracing::new()?; - components.push(Box::new(c)); - tracing::info!("post-push"); - + let components = self.framework_components(command)?; self.state.components.register(components) } diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index e0b9166d4a..b0425d73a9 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -150,9 +150,6 @@ impl QueryConnectionChannelsCmd { impl Runnable for QueryConnectionChannelsCmd { fn run(&self) { let config = app_config(); - tracing::debug!("post-push ^2"); - tracing::info!("post-push ^2"); - tracing::error!("ERROROROOROR"); let (chain_config, opts) = match self.validate_options(&config) { Err(err) => { diff --git a/relayer-cli/src/lib.rs b/relayer-cli/src/lib.rs index 42a5d9bbce..0e13022e2b 100644 --- a/relayer-cli/src/lib.rs +++ b/relayer-cli/src/lib.rs @@ -22,4 +22,3 @@ mod conclude; pub mod config; pub mod error; pub mod prelude; -mod components; From 240848224a8491ddaf248dbc892264ba3cf79dc2 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Tue, 12 Jan 2021 17:56:21 +0200 Subject: [PATCH 09/12] Simplified output, removed JSON --- relayer-cli/src/commands/query/connection.rs | 26 ++----- relayer-cli/src/conclude.rs | 75 -------------------- relayer-cli/src/lib.rs | 1 - 3 files changed, 5 insertions(+), 97 deletions(-) delete mode 100644 relayer-cli/src/conclude.rs diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index b0425d73a9..9733be5179 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -11,7 +11,6 @@ use ibc_proto::ibc::core::channel::v1::QueryConnectionChannelsRequest; use relayer::chain::{Chain, CosmosSDKChain}; use relayer::config::{ChainConfig, Config}; -use crate::conclude::{on_exit, CommandOutput, CommandStatus}; use crate::error::{Error, Kind}; use crate::prelude::*; @@ -109,9 +108,6 @@ pub struct QueryConnectionChannelsCmd { #[options(free, help = "identifier of the connection to query")] connection_id: Option, - - #[options(help = "enable output in JSON format")] - json: bool, } #[derive(Debug)] @@ -153,16 +149,12 @@ impl Runnable for QueryConnectionChannelsCmd { let (chain_config, opts) = match self.validate_options(&config) { Err(err) => { - return on_exit( - self.json, - CommandOutput::new(CommandStatus::Error).with_msg(err), - ); + status_err!("invalid options: {}", err); + return; } Ok(result) => result, }; - if !self.json { - status_info!("Options", "{:?}", opts); - } + status_info!("Options", "{:?}", opts); let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap(); @@ -177,15 +169,8 @@ impl Runnable for QueryConnectionChannelsCmd { .map_err(|e| Kind::Query.context(e).into()); match res { - Ok(cs) => on_exit( - self.json, - CommandOutput::new(CommandStatus::Success).with_msg(format!("{:?}", cs)), - ), - Err(e) => on_exit( - self.json, - CommandOutput::new(CommandStatus::Error) - .with_msg(format!("error encountered: {:?}", e)), - ), + Ok(cs) => status_info!("connection query result: ", "{:?}", cs), + Err(e) => status_info!("connection query error", "{}", e), } } } @@ -295,7 +280,6 @@ mod tests { let default_params = QueryConnectionChannelsCmd { chain_id: Some("ibc-0".to_string().parse().unwrap()), connection_id: Some("ibconeconnection".to_string().parse().unwrap()), - json: false, }; struct Test { diff --git a/relayer-cli/src/conclude.rs b/relayer-cli/src/conclude.rs deleted file mode 100644 index 979b9aa882..0000000000 --- a/relayer-cli/src/conclude.rs +++ /dev/null @@ -1,75 +0,0 @@ -#![allow(dead_code)] -//! Handles the return message (command output) and return code from a CLI command. -//! Most of the code here is temporary/experimental. -//! https://github.com/informalsystems/ibc-rs/issues/500 will introduce a more permanent solution. - -use std::collections::HashMap; - -use serde::Serialize; - -pub fn on_exit(json: bool, out: CommandOutput) { - // Handle the output message - if json { - println!("{}", serde_json::to_string(&out).unwrap()); - } else { - println!("status={:?}\nmsg={:?}", out.status, out.msg); - } - - // The return code - if out.status == CommandStatus::Error { - std::process::exit(1); - } else { - std::process::exit(0); - } -} - -/// A CLI output with support for JSON serialization. -#[derive(Serialize, Debug)] -pub struct CommandOutput { - /// The return status - pub status: CommandStatus, - - /// An arbitrary message accompanying the output - pub msg: Option, - - /// Additional arbitrary key/values that may be present in the output - /// TODO: at the moment this is not used! - #[serde(flatten)] - pub extra: HashMap, -} - -impl CommandOutput { - pub fn new(status: CommandStatus) -> Self { - CommandOutput { - status, - msg: None, - extra: Default::default(), - } - } - - pub fn with_msg(self, msg: String) -> Self { - CommandOutput { - msg: Some(msg), - ..self - } - } - - pub fn with_extra(self, k: String, v: String) -> Self { - let mut extra = self.extra.clone(); - extra.insert(k, v); - - CommandOutput { extra, ..self } - } -} - -#[derive(Serialize, Debug, PartialEq, Eq)] -pub enum CommandStatus { - #[serde(rename(serialize = "success"))] - Success, - - #[serde(rename(serialize = "info"))] - Info, - - #[serde(rename(serialize = "error"))] - Error, -} diff --git a/relayer-cli/src/lib.rs b/relayer-cli/src/lib.rs index 0e13022e2b..6a91c6fee7 100644 --- a/relayer-cli/src/lib.rs +++ b/relayer-cli/src/lib.rs @@ -18,7 +18,6 @@ pub mod application; pub mod commands; -mod conclude; pub mod config; pub mod error; pub mod prelude; From cbb9549344c2d99a9798952cc4ae016413a24a69 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Tue, 12 Jan 2021 17:58:33 +0200 Subject: [PATCH 10/12] Fixed output text --- relayer-cli/src/commands/query/connection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index 9733be5179..2fde384ee2 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -169,8 +169,8 @@ impl Runnable for QueryConnectionChannelsCmd { .map_err(|e| Kind::Query.context(e).into()); match res { - Ok(cs) => status_info!("connection query result: ", "{:?}", cs), - Err(e) => status_info!("connection query error", "{}", e), + Ok(cs) => status_info!("connection channels query result: ", "{:?}", cs), + Err(e) => status_info!("connection channels query error", "{}", e), } } } From ea129d390229e4e3fe0403c880174f33e38d1e5e Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Wed, 13 Jan 2021 15:39:10 +0200 Subject: [PATCH 11/12] Using find_chain method --- relayer-cli/src/commands/query/connection.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index 8176abde35..87cafd65ce 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -123,9 +123,7 @@ impl QueryConnectionChannelsCmd { .clone() .ok_or_else(|| "no chain chain identifier provided".to_string())?; let chain_config = config - .chains - .iter() - .find(|c| c.id == chain_id) + .find_chain(&chain_id) .ok_or_else(|| "missing chain configuration for the given chain id".to_string())?; let connection_id = self From c51b8141eaf468ce4781a26eadd25e6346792401 Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Wed, 13 Jan 2021 15:46:46 +0200 Subject: [PATCH 12/12] Added TODO addressing Vitor's comment --- relayer/src/chain/cosmos.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/relayer/src/chain/cosmos.rs b/relayer/src/chain/cosmos.rs index 9a47666571..54bf2844ca 100644 --- a/relayer/src/chain/cosmos.rs +++ b/relayer/src/chain/cosmos.rs @@ -671,6 +671,9 @@ impl Chain for CosmosSDKChain { .map_err(|e| Kind::Grpc.context(e))? .into_inner(); + // TODO: add warnings for any identifiers that fail to parse (below). + // https://github.com/informalsystems/ibc-rs/pull/506#discussion_r555945560 + let vec_ids = response .channels .iter()