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

Fix panic when querying connections filtered by counterparty chain #3381

Merged
merged 3 commits into from
Jun 5, 2023
Merged
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: 3 additions & 0 deletions .changelog/unreleased/bug-fixes/3381-panic-query-conns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Fix panic which can occur when querying connections filtered
by counterparty chain using `hermes query connections`
([\#3381](https://github.com/informalsystems/hermes/issues/3381))
69 changes: 31 additions & 38 deletions crates/relayer-cli/src/commands/query/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,37 @@ impl Runnable for QueryConnectionsCmd {
let chain = spawn_chain_runtime(&config, &self.chain_id)
.unwrap_or_else(exit_with_unrecoverable_error);

let res = chain.query_connections(QueryConnectionsRequest {
pagination: Some(PageRequest::all()),
});

let connections = match res {
Ok(connections) => {
// 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();

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!(
"An error occurred trying to query connections: {e}"
))
.exit(),
};
let mut connections = chain
.query_connections(QueryConnectionsRequest {
pagination: Some(PageRequest::all()),
})
.unwrap_or_else(|e| {
Output::error(format!(
"An error occurred trying to query connections: {e}"
))
.exit()
});

// Check the counterparty chain id only if filtering is required.
if let Some(counterparty_filter_id) = self.counterparty_chain_id.clone() {
connections.retain(|connection| {
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();

counterparty_chain_id == counterparty_filter_id
});
}

if self.verbose {
Output::success(connections).exit()
Expand Down