Skip to content

Commit

Permalink
Added CLI to query for all connections (informalsystems#565)
Browse files Browse the repository at this point in the history
* Added query for informalsystems#553 and upd changelog.

* Moved the 'raw' ConnectionIds file from ICS02 to ICS03

* Optimized imports à la intellij

* Cargo fmt

Co-authored-by: Anca Zamfir <zamfiranca@gmail.com>
  • Loading branch information
adizere and ancazamfir authored Jan 28, 2021
1 parent 2c88f52 commit 349750d
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- JSON output for queries and txs ([#500])
- Implement commands for channel close init and confirm ([#538])
- Implement command to perform the handshake for a new channel ([#557])
- Query all connections command ([#553])
- Query all channels command ([#568])

- [relayer]
Expand Down Expand Up @@ -71,6 +72,7 @@
[#538]: https://github.com/informalsystems/ibc-rs/issues/538
[#540]: https://github.com/informalsystems/ibc-rs/issues/540
[#554]: https://github.com/informalsystems/ibc-rs/issues/554
[#553]: https://github.com/informalsystems/ibc-rs/issues/553
[#557]: https://github.com/informalsystems/ibc-rs/issues/557
[#563]: https://github.com/informalsystems/ibc-rs/issues/563
[#568]: https://github.com/informalsystems/ibc-rs/issues/568
Expand Down
5 changes: 5 additions & 0 deletions relayer-cli/src/commands/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod channel;
mod channels;
mod client;
mod connection;
mod connections;
mod packet;

/// `query` subcommand
Expand All @@ -20,6 +21,10 @@ pub enum QueryCmd {
#[options(help = "query information about connection(s)")]
Connection(QueryConnectionCmds),

/// The `query connections` subcommand
#[options(help = "query the identifiers of all connection on a chain")]
Connections(connections::QueryConnectionsCmd),

/// The `query channel` subcommand
#[options(help = "query information about channel(s)")]
Channel(QueryChannelCmds),
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::runtime::Runtime as TokioRuntime;
use tracing::info;

use ibc::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use ibc::ics02_client::raw::ConnectionIds as ConnectionIDs;
use ibc::ics03_connection::raw::ConnectionIds as ConnectionIDs;
use ibc::ics24_host::error::ValidationError;
use ibc::ics24_host::identifier::ChainId;
use ibc::ics24_host::identifier::ClientId;
Expand Down
55 changes: 55 additions & 0 deletions relayer-cli/src/commands/query/connections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::sync::Arc;

use abscissa_core::{Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;

use ibc::ics24_host::identifier::ChainId;
use ibc_proto::ibc::core::connection::v1::QueryConnectionsRequest;
use relayer::chain::{Chain, CosmosSDKChain};
use relayer::config::{ChainConfig, Config};

use crate::conclude::Output;
use crate::prelude::*;

#[derive(Clone, Command, Debug, Options)]
pub struct QueryConnectionsCmd {
#[options(free, required, help = "identifier of the chain to query")]
chain_id: ChainId,
}

impl QueryConnectionsCmd {
fn validate_options(&self, config: &Config) -> Result<ChainConfig, String> {
let chain_config = config
.find_chain(&self.chain_id)
.ok_or_else(|| format!("chain '{}' not found in configuration file", self.chain_id))?;

Ok(chain_config.clone())
}
}

// rrly -c config.toml query connections ibc-0
impl Runnable for QueryConnectionsCmd {
fn run(&self) {
let config = app_config();

let chain_config = match self.validate_options(&config) {
Err(err) => {
return Output::error(err).exit();
}
Ok(result) => result,
};
info!("Options {:?}", chain_config);

let rt = Arc::new(TokioRuntime::new().unwrap());
let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap();

let req = QueryConnectionsRequest { pagination: None };

let res = chain.query_connections(req);

match res {
Ok(ce) => Output::success(ce).exit(),
Err(e) => Output::error(format!("{}", e)).exit(),
}
}
}
7 changes: 4 additions & 3 deletions relayer-cli/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
use std::str::FromStr;
use std::sync::Arc;

use ibc::ics02_client::raw::ConnectionIds as DomainTypeClientConnections;
use tendermint::net::Address;
use tendermint_proto::Protobuf;

use ibc::ics03_connection::raw::ConnectionIds as DomainTypeClientConnections;
use ibc::ics04_channel::channel::{ChannelEnd, Order, State as ChannelState};
use ibc::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId};
use ibc::ics24_host::Path::{ChannelEnds, ClientConnections};
use relayer::chain::{Chain, CosmosSDKChain};
use relayer::config::{default, ChainConfig, Config};
use tendermint::net::Address;
use tendermint_proto::Protobuf;

/// Configuration that connects to the informaldev/simd DockerHub image running on localhost.
fn simd_config() -> Config {
Expand Down
5 changes: 5 additions & 0 deletions relayer/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ibc::events::IBCEvent;
use ibc::ics02_client::header::Header;
use ibc::ics02_client::state::{ClientState, ConsensusState};
use ibc::ics03_connection::connection::{ConnectionEnd, State};
use ibc::ics03_connection::raw::ConnectionIds;
use ibc::ics03_connection::version::{get_compatible_versions, Version};
use ibc::ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest};
use ibc::ics04_channel::packet::{PacketMsgType, Sequence};
Expand All @@ -27,6 +28,7 @@ use ibc_proto::ibc::core::channel::v1::{
QueryUnreceivedPacketsRequest,
};
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use ibc_proto::ibc::core::connection::v1::QueryConnectionsRequest;

use crate::config::ChainConfig;
use crate::connection::ConnectionMsgType;
Expand Down Expand Up @@ -361,6 +363,9 @@ pub trait Chain: Sized {
request: QueryConnectionChannelsRequest,
) -> Result<Vec<ChannelId>, Error>;

/// Performs a query to retrieve the identifiers of all connections.
fn query_connections(&self, request: QueryConnectionsRequest) -> Result<ConnectionIds, Error>;

fn query_channels(&self, request: QueryChannelsRequest) -> Result<Vec<ChannelId>, Error>;

fn build_packet_proofs(
Expand Down
34 changes: 33 additions & 1 deletion relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ use tonic::codegen::http::Uri;
use ibc::downcast;
use ibc::events::{from_tx_response_event, IBCEvent};
use ibc::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use ibc::ics03_connection::raw::ConnectionIds;
use ibc::ics04_channel::channel::QueryPacketEventDataRequest;
use ibc::ics04_channel::packet::Sequence;
use ibc::ics07_tendermint::client_state::ClientState;
use ibc::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState;
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, ChannelId, ClientId};
use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId};
use ibc::ics24_host::Path::ClientConsensusState as ClientConsensusPath;
use ibc::ics24_host::Path::ClientState as ClientStatePath;
use ibc::ics24_host::{Path, IBC_QUERY_PATH};
Expand All @@ -45,6 +46,7 @@ use ibc_proto::ibc::core::channel::v1::{
QueryUnreceivedPacketsRequest,
};
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use ibc_proto::ibc::core::connection::v1::QueryConnectionsRequest;

use crate::chain::QueryResponse;
use crate::config::ChainConfig;
Expand Down Expand Up @@ -725,6 +727,36 @@ impl Chain for CosmosSDKChain {
Ok(vec_ids)
}

fn query_connections(&self, request: QueryConnectionsRequest) -> Result<ConnectionIds, Error> {
crate::time!("query_connections");

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::connection::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.connections(request))
.map_err(|e| Kind::Grpc.context(e))?
.into_inner();

// TODO: add warnings for any identifiers that fail to parse (below).
// similar to the parsing in `query_connection_channels`.

let ids = response
.connections
.iter()
.filter_map(|ic| ConnectionId::from_str(ic.id.as_str()).ok())
.collect();

Ok(ids)
}

fn query_channels(&self, request: QueryChannelsRequest) -> Result<Vec<ChannelId>, Error> {
crate::time!("query_connections");

Expand Down
22 changes: 13 additions & 9 deletions relayer/src/chain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ use std::time::Duration;

use crossbeam_channel as channel;
use prost_types::Any;
use tokio::runtime::Runtime;

use tendermint::account::Id;
use tendermint_testgen::light_block::TMLightBlock;

use ibc_proto::ibc::core::channel::v1::{
PacketState, QueryChannelsRequest, QueryConnectionChannelsRequest,
QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest,
QueryUnreceivedPacketsRequest,
};
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use tokio::runtime::Runtime;

use ibc::downcast;
use ibc::events::IBCEvent;
use ibc::ics02_client::client_def::AnyClientState;
use ibc::ics03_connection::raw::ConnectionIds;
use ibc::ics04_channel::channel::QueryPacketEventDataRequest;
use ibc::ics07_tendermint::client_state::ClientState as TendermintClientState;
use ibc::ics07_tendermint::consensus_state::ConsensusState as TendermintConsensusState;
Expand All @@ -32,6 +25,13 @@ use ibc::mock::context::MockContext;
use ibc::mock::host::HostType;
use ibc::test_utils::get_dummy_account_id;
use ibc::Height;
use ibc_proto::ibc::core::channel::v1::{
PacketState, QueryChannelsRequest, QueryConnectionChannelsRequest,
QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, QueryUnreceivedAcksRequest,
QueryUnreceivedPacketsRequest,
};
use ibc_proto::ibc::core::commitment::v1::MerkleProof;
use ibc_proto::ibc::core::connection::v1::QueryConnectionsRequest;

use crate::chain::{Chain, QueryResponse};
use crate::config::ChainConfig;
Expand Down Expand Up @@ -237,6 +237,10 @@ impl Chain for MockChain {
unimplemented!()
}

fn query_connections(&self, _request: QueryConnectionsRequest) -> Result<ConnectionIds, Error> {
unimplemented!()
}

fn query_txs(&self, _request: QueryPacketEventDataRequest) -> Result<Vec<IBCEvent>, Error> {
unimplemented!()
}
Expand Down

0 comments on commit 349750d

Please sign in to comment.