Skip to content

Commit

Permalink
New optional flags for the command query connections (informalsyste…
Browse files Browse the repository at this point in the history
…ms#2391)

* Added 'verbose' and 'counterparty-chain' flags to 'query connections' command

* Updated Hermes guide with new optional flags for 'query connections' command

* Added changelog entry

* Updated ADR 010 Status to reflect new PR for 'query connections' optional flags

* Fixed date error in ADR 010

* Fixed changelog entry
  • Loading branch information
ljoss17 authored Jul 13, 2022
1 parent cf1eab3 commit 2cd91a0
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- New optional flags `--counterparty-chain` and `--verbose` for the command
`query connections` ([#2310](https://github.com/informalsystems/ibc-rs/issues/2310))
4 changes: 4 additions & 0 deletions docs/architecture/adr-010-unified-cli-arguments-hermes.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ __08.07.22__

* Created a new PR, [#2384](https://github.com/informalsystems/ibc-rs/pull/2384), to add the optional flag for the `upgrade clients` command, issue [#2311](https://github.com/informalsystems/ibc-rs/issues/2311)

__11.07.22__

* Created a new PR, [#2391](https://github.com/informalsystems/ibc-rs/pull/2391), to add the optional flags for the `query connections` command, issue [#2310](https://github.com/informalsystems/ibc-rs/issues/2310)

## Consequences

### Positive
Expand Down
7 changes: 7 additions & 0 deletions guide/src/commands/queries/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ USAGE:
DESCRIPTION:
Query the identifiers of all connections on a chain

OPTIONS:
--counterparty-chain <COUNTERPARTY_CHAIN_ID>
Filter the query response by the counterparty chain

--verbose
Enable verbose output, displaying the client for each connection in the response

REQUIRED:
--chain <CHAIN_ID> Identifier of the chain to query
```
Expand Down
103 changes: 93 additions & 10 deletions relayer-cli/src/commands/query/connections.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use abscissa_core::clap::Parser;
use abscissa_core::Runnable;

use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics24_host::identifier::{ChainId, ConnectionId};
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::{PageRequest, QueryConnectionsRequest};
use ibc_relayer::chain::requests::{
IncludeProof, PageRequest, QueryClientStateRequest, QueryConnectionsRequest, QueryHeight,
};

use crate::cli_utils::spawn_chain_runtime;
use crate::conclude::{exit_with_unrecoverable_error, Output};
Expand All @@ -19,6 +22,19 @@ pub struct QueryConnectionsCmd {
help = "Identifier of the chain to query"
)]
chain_id: ChainId,

#[clap(
long = "counterparty-chain",
value_name = "COUNTERPARTY_CHAIN_ID",
help = "Filter the query response by the counterparty chain"
)]
counterparty_chain_id: Option<ChainId>,

#[clap(
long = "verbose",
help = "Enable verbose output, displaying the client for each connection in the response"
)]
verbose: bool,
}

// hermes query connections ibc-0
Expand All @@ -35,16 +51,51 @@ impl Runnable for QueryConnectionsCmd {
pagination: Some(PageRequest::all()),
});

match res {
let connections = match res {
Ok(connections) => {
let ids: Vec<ConnectionId> = connections
.into_iter()
.map(|identified_connection| identified_connection.connection_id)
.collect();
// Check the counterparty chain id only if filtering is required.
if let Some(counterparty_filter_id) = self.counterparty_chain_id.clone() {
let mut output = connections.clone();

Output::success(ids).exit()
for (id, connection) in connections.into_iter().enumerate() {
let client_id = connection.end().client_id().to_owned();
let chain_height = chain.query_latest_height();
let (client_state, _) = chain
.query_client_state(
QueryClientStateRequest {
client_id,
height: QueryHeight::Specific(chain_height.unwrap()),
},
IncludeProof::No,
)
.unwrap();
let counterparty_chain_id = client_state.chain_id();

if counterparty_chain_id != counterparty_filter_id {
output.remove(id);
}
}
output
} else {
connections
}
}
Err(e) => Output::error(format!("{}", e)).exit(),
Err(e) => Output::error(format!(
"An error occurred trying to query connections: {}",
e
))
.exit(),
};

if self.verbose {
Output::success(connections).exit()
} else {
let ids: Vec<ConnectionId> = connections
.into_iter()
.map(|identified_connection| identified_connection.connection_id)
.collect();

Output::success(ids).exit()
}
}
}
Expand All @@ -57,15 +108,47 @@ mod tests {
use ibc::core::ics24_host::identifier::ChainId;

#[test]
fn test_query_connections() {
fn test_query_connections_required_only() {
assert_eq!(
QueryConnectionsCmd {
chain_id: ChainId::from_string("chain_id")
chain_id: ChainId::from_string("chain_id"),
counterparty_chain_id: None,
verbose: false,
},
QueryConnectionsCmd::parse_from(&["test", "--chain", "chain_id"])
)
}

#[test]
fn test_query_connections_counterparty_chain() {
assert_eq!(
QueryConnectionsCmd {
chain_id: ChainId::from_string("chain_id"),
counterparty_chain_id: Some(ChainId::from_string("chain_counterparty_id")),
verbose: false,
},
QueryConnectionsCmd::parse_from(&[
"test",
"--chain",
"chain_id",
"--counterparty-chain",
"chain_counterparty_id"
])
)
}

#[test]
fn test_query_connections_verbose() {
assert_eq!(
QueryConnectionsCmd {
chain_id: ChainId::from_string("chain_id"),
counterparty_chain_id: None,
verbose: true,
},
QueryConnectionsCmd::parse_from(&["test", "--chain", "chain_id", "--verbose"])
)
}

#[test]
fn test_query_connections_no_chain() {
assert!(QueryConnectionsCmd::try_parse_from(&["test"]).is_err())
Expand Down

0 comments on commit 2cd91a0

Please sign in to comment.