Skip to content

Commit

Permalink
Implement ADR 009: ChainEndpoint and ChainHandle methods standard…
Browse files Browse the repository at this point in the history
…ization (informalsystems#2200)

* QueryChannelClientStateRequest domain type

* QueryChannelClientStateRequest domain type

* QueryChannelRequest domain type

* QueryChannelClientStateRequest: remove unused Protobuf

* clippy

* PageRequest domain type

* QueryClientStatesRequest domain type

* QueryClientStateRequest domain type

* QueryConsensusStatesRequest domain type

* bunch of domain types

* bunch of domain types

* rest of domain types

* changelog

* Update relayer-cli/src/commands/misbehaviour.rs

Co-authored-by: Romain Ruetschi <[email protected]>

* remove extraneous `request` vars

* Use MerkleProof domain type

* Remove `PacketState` return from queries

* Use existing Sequence domain type

Co-authored-by: Romain Ruetschi <[email protected]>
  • Loading branch information
plafer and romac authored May 19, 2022
1 parent cfa7890 commit 3add5d3
Show file tree
Hide file tree
Showing 45 changed files with 1,320 additions and 793 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Implement ADR 9: add domain type for request messages that are passed to query
functions ([#2192](https://github.com/informalsystems/ibc-rs/issues/2192))
11 changes: 9 additions & 2 deletions relayer-cli/src/commands/create/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ibc::core::ics04_channel::Version;
use ibc::core::ics24_host::identifier::{ChainId, ConnectionId, PortId};
use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::{QueryClientStateRequest, QueryConnectionRequest};
use ibc_relayer::channel::Channel;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;
Expand Down Expand Up @@ -187,12 +188,18 @@ impl CreateChannelCommand {
// Query the connection end.
let height = Height::new(chain_a.id().version(), 0);
let conn_end = chain_a
.query_connection(connection_a, height)
.query_connection(QueryConnectionRequest {
connection_id: connection_a.clone(),
height,
})
.unwrap_or_else(exit_with_unrecoverable_error);

// Query the client state, obtain the identifier of chain b.
let chain_b = chain_a
.query_client_state(conn_end.client_id(), height)
.query_client_state(QueryClientStateRequest {
client_id: conn_end.client_id().clone(),
height,
})
.map(|cs| cs.chain_id())
.unwrap_or_else(exit_with_unrecoverable_error);

Expand Down
6 changes: 5 additions & 1 deletion relayer-cli/src/commands/create/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics24_host::identifier::{ChainId, ClientId};
use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::QueryClientStateRequest;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;

Expand Down Expand Up @@ -114,7 +115,10 @@ impl CreateConnectionCommand {

// Query client state. Extract the target chain (chain_id which this client is verifying).
let height = Height::new(chain_a.id().version(), 0);
let chain_b_id = match chain_a.query_client_state(client_a_id, height) {
let chain_b_id = match chain_a.query_client_state(QueryClientStateRequest {
client_id: client_a_id.clone(),
height,
}) {
Ok(cs) => cs.chain_id(),
Err(e) => Output::error(format!(
"failed while querying client '{}' on chain '{}' with error: {}",
Expand Down
6 changes: 5 additions & 1 deletion relayer-cli/src/commands/misbehaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ibc::core::ics02_client::height::Height;
use ibc::core::ics24_host::identifier::{ChainId, ClientId};
use ibc::events::IbcEvent;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::QueryClientStateRequest;
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::{ForeignClient, MisbehaviourResults};
use std::ops::Deref;
Expand Down Expand Up @@ -99,7 +100,10 @@ fn misbehaviour_handling<Chain: ChainHandle>(
update: Option<UpdateClient>,
) -> Result<(), Box<dyn std::error::Error>> {
let client_state = chain
.query_client_state(&client_id, Height::zero())
.query_client_state(QueryClientStateRequest {
client_id: client_id.clone(),
height: Height::zero(),
})
.map_err(|e| format!("could not query client state for {}: {}", client_id, e))?;

if client_state.is_frozen() {
Expand Down
8 changes: 6 additions & 2 deletions relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tokio::runtime::Runtime as TokioRuntime;

use ibc::core::ics24_host::identifier::ChainId;
use ibc::core::ics24_host::identifier::{ChannelId, PortId};
use ibc_relayer::chain::requests::QueryChannelRequest;
use ibc_relayer::chain::{ChainEndpoint, CosmosSdkChain};

use crate::conclude::{exit_with_unrecoverable_error, Output};
Expand Down Expand Up @@ -46,8 +47,11 @@ impl Runnable for QueryChannelEndCmd {
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt)
.unwrap_or_else(exit_with_unrecoverable_error);

let height = ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64));
let res = chain.query_channel(&self.port_id, &self.channel_id, height);
let res = chain.query_channel(QueryChannelRequest {
port_id: self.port_id.clone(),
channel_id: self.channel_id,
height: ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64)),
});
match res {
Ok(channel_end) => {
if channel_end.state_matches(&State::Uninitialized) {
Expand Down
47 changes: 33 additions & 14 deletions relayer-cli/src/commands/query/channel_ends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use ibc::core::ics24_host::identifier::ChainId;
use ibc::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId};
use ibc::Height;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::chain::requests::{
QueryChannelRequest, QueryClientStateRequest, QueryConnectionRequest,
};
use ibc_relayer::registry::Registry;

use crate::conclude::Output;
Expand Down Expand Up @@ -78,7 +81,11 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
None => chain.query_latest_height()?,
};

let channel_end = chain.query_channel(&port_id, &channel_id, chain_height)?;
let channel_end = chain.query_channel(QueryChannelRequest {
port_id: port_id.clone(),
channel_id,
height: chain_height,
})?;
if channel_end.state_matches(&State::Uninitialized) {
return Err(format!(
"{}/{} on chain {} @ {:?} is uninitialized",
Expand All @@ -98,11 +105,17 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
})?
.clone();

let connection_end = chain.query_connection(&connection_id, chain_height)?;
let connection_end = chain.query_connection(QueryConnectionRequest {
connection_id: connection_id.clone(),
height: chain_height,
})?;

let client_id = connection_end.client_id().clone();

let client_state = chain.query_client_state(&client_id, chain_height)?;
let client_state = chain.query_client_state(QueryClientStateRequest {
client_id: client_id.clone(),
height: chain_height,
})?;

let channel_counterparty = channel_end.counterparty().clone();
let connection_counterparty = connection_end.counterparty().clone();
Expand Down Expand Up @@ -132,17 +145,23 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
let counterparty_chain = registry.get_or_spawn(&counterparty_chain_id)?;
let counterparty_chain_height = counterparty_chain.query_latest_height()?;

let counterparty_connection_end = counterparty_chain
.query_connection(&counterparty_connection_id, counterparty_chain_height)?;

let counterparty_client_state = counterparty_chain
.query_client_state(&counterparty_client_id, counterparty_chain_height)?;

let counterparty_channel_end = counterparty_chain.query_channel(
&counterparty_port_id,
&counterparty_channel_id,
counterparty_chain_height,
)?;
let counterparty_connection_end =
counterparty_chain.query_connection(QueryConnectionRequest {
connection_id: counterparty_connection_id.clone(),
height: counterparty_chain_height,
})?;

let counterparty_client_state =
counterparty_chain.query_client_state(QueryClientStateRequest {
client_id: counterparty_client_id.clone(),
height: counterparty_chain_height,
})?;

let counterparty_channel_end = counterparty_chain.query_channel(QueryChannelRequest {
port_id: counterparty_port_id.clone(),
channel_id: counterparty_channel_id,
height: counterparty_chain_height,
})?;

if cmd.verbose {
let res = ChannelEnds {
Expand Down
51 changes: 32 additions & 19 deletions relayer-cli/src/commands/query/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics04_channel::channel::{ChannelEnd, State};
use ibc::core::ics24_host::identifier::{ChainId, ChannelId, ConnectionId, PortChannelId, PortId};
use ibc::Height;
use ibc_proto::ibc::core::channel::v1::QueryChannelsRequest;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::chain::requests::{
PageRequest, QueryChannelRequest, QueryChannelsRequest, QueryClientStateRequest,
QueryConnectionRequest,
};
use ibc_relayer::registry::Registry;

use crate::commands::query::channel_ends::ChannelEnds;
Expand Down Expand Up @@ -54,11 +57,9 @@ fn run_query_channels<Chain: ChainHandle>(
let chain = registry.get_or_spawn(&cmd.chain_id)?;
let chain_height = chain.query_latest_height()?;

let req = QueryChannelsRequest {
pagination: ibc_proto::cosmos::base::query::pagination::all(),
};

let identified_channels = chain.query_channels(req)?;
let identified_channels = chain.query_channels(QueryChannelsRequest {
pagination: Some(PageRequest::all()),
})?;

for identified_channel in identified_channels {
let port_id = identified_channel.port_id;
Expand Down Expand Up @@ -125,9 +126,15 @@ fn query_channel_ends<Chain: ChainHandle>(
channel_id: ChannelId,
chain_height: Height,
) -> Result<ChannelEnds, Box<dyn std::error::Error>> {
let connection_end = chain.query_connection(&connection_id, chain_height)?;
let connection_end = chain.query_connection(QueryConnectionRequest {
connection_id: connection_id.clone(),
height: chain_height,
})?;
let client_id = connection_end.client_id().clone();
let client_state = chain.query_client_state(&client_id, chain_height)?;
let client_state = chain.query_client_state(QueryClientStateRequest {
client_id,
height: chain_height,
})?;
let counterparty_chain_id = client_state.chain_id();

if let Some(dst_chain_id) = destination_chain {
Expand Down Expand Up @@ -166,17 +173,23 @@ fn query_channel_ends<Chain: ChainHandle>(
let counterparty_chain = registry.get_or_spawn(&counterparty_chain_id)?;
let counterparty_chain_height = counterparty_chain.query_latest_height()?;

let counterparty_connection_end = counterparty_chain
.query_connection(&counterparty_connection_id, counterparty_chain_height)?;

let counterparty_client_state = counterparty_chain
.query_client_state(&counterparty_client_id, counterparty_chain_height)?;

let counterparty_channel_end = counterparty_chain.query_channel(
&counterparty_port_id,
&counterparty_channel_id,
counterparty_chain_height,
)?;
let counterparty_connection_end =
counterparty_chain.query_connection(QueryConnectionRequest {
connection_id: counterparty_connection_id,
height: counterparty_chain_height,
})?;

let counterparty_client_state =
counterparty_chain.query_client_state(QueryClientStateRequest {
client_id: counterparty_client_id,
height: counterparty_chain_height,
})?;

let counterparty_channel_end = counterparty_chain.query_channel(QueryChannelRequest {
port_id: counterparty_port_id,
channel_id: counterparty_channel_id,
height: counterparty_chain_height,
})?;

Ok(ChannelEnds {
channel_end,
Expand Down
40 changes: 26 additions & 14 deletions relayer-cli/src/commands/query/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use alloc::sync::Arc;

use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};
use ibc_relayer::chain::requests::{
PageRequest, QueryClientConnectionsRequest, QueryClientStateRequest,
QueryConsensusStateRequest, QueryConsensusStatesRequest,
};
use tokio::runtime::Runtime as TokioRuntime;
use tracing::debug;

Expand All @@ -12,8 +16,6 @@ use ibc::core::ics24_host::identifier::ClientId;
use ibc::events::WithBlockDataType;
use ibc::query::QueryTxRequest;
use ibc::Height;
use ibc_proto::ibc::core::client::v1::QueryConsensusStatesRequest;
use ibc_proto::ibc::core::connection::v1::QueryClientConnectionsRequest;
use ibc_relayer::chain::ChainEndpoint;
use ibc_relayer::chain::CosmosSdkChain;

Expand Down Expand Up @@ -53,7 +55,10 @@ impl Runnable for QueryClientStateCmd {
.unwrap_or_else(exit_with_unrecoverable_error);
let height = ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64));

match chain.query_client_state(&self.client_id, height) {
match chain.query_client_state(QueryClientStateRequest {
client_id: self.client_id.clone(),
height,
}) {
Ok(cs) => Output::success(cs).exit(),
Err(e) => Output::error(format!("{}", e)).exit(),
}
Expand Down Expand Up @@ -108,7 +113,10 @@ impl Runnable for QueryClientConsensusCmd {
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt)
.unwrap_or_else(exit_with_unrecoverable_error);

let counterparty_chain = match chain.query_client_state(&self.client_id, Height::zero()) {
let counterparty_chain = match chain.query_client_state(QueryClientStateRequest {
client_id: self.client_id.clone(),
height: Height::zero(),
}) {
Ok(cs) => cs.chain_id(),
Err(e) => Output::error(format!(
"failed while querying client '{}' on chain '{}' with error: {}",
Expand All @@ -122,8 +130,11 @@ impl Runnable for QueryClientConsensusCmd {
let height = ibc::Height::new(chain.id().version(), self.height.unwrap_or(0_u64));
let consensus_height = ibc::Height::new(counterparty_chain.version(), cs_height);

let res =
chain.query_consensus_state(self.client_id.clone(), consensus_height, height);
let res = chain.query_consensus_state(QueryConsensusStateRequest {
client_id: self.client_id.clone(),
consensus_height,
query_height: height,
});

match res {
Ok(cs) => Output::success(cs).exit(),
Expand All @@ -132,8 +143,8 @@ impl Runnable for QueryClientConsensusCmd {
}
None => {
let res = chain.query_consensus_states(QueryConsensusStatesRequest {
client_id: self.client_id.to_string(),
pagination: ibc_proto::cosmos::base::query::pagination::all(),
client_id: self.client_id.clone(),
pagination: Some(PageRequest::all()),
});

match res {
Expand Down Expand Up @@ -188,7 +199,10 @@ impl Runnable for QueryClientHeaderCmd {
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt)
.unwrap_or_else(exit_with_unrecoverable_error);

let counterparty_chain = match chain.query_client_state(&self.client_id, Height::zero()) {
let counterparty_chain = match chain.query_client_state(QueryClientStateRequest {
client_id: self.client_id.clone(),
height: Height::zero(),
}) {
Ok(cs) => cs.chain_id(),
Err(e) => Output::error(format!(
"failed while querying client '{}' on chain '{}' with error: {}",
Expand Down Expand Up @@ -252,11 +266,9 @@ impl Runnable for QueryClientConnectionsCmd {
let chain = CosmosSdkChain::bootstrap(chain_config.clone(), rt)
.unwrap_or_else(exit_with_unrecoverable_error);

let req = QueryClientConnectionsRequest {
client_id: self.client_id.to_string(),
};

let res = chain.query_client_connections(req);
let res = chain.query_client_connections(QueryClientConnectionsRequest {
client_id: self.client_id.clone(),
});

match res {
Ok(ce) => Output::success(ce).exit(),
Expand Down
4 changes: 2 additions & 2 deletions relayer-cli/src/commands/query/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::runtime::Runtime as TokioRuntime;

use ibc::core::ics02_client::client_state::ClientState;
use ibc::core::ics24_host::identifier::{ChainId, ClientId};
use ibc_proto::ibc::core::client::v1::QueryClientStatesRequest;
use ibc_relayer::chain::requests::{PageRequest, QueryClientStatesRequest};
use ibc_relayer::chain::{ChainEndpoint, CosmosSdkChain};

use crate::conclude::{exit_with_unrecoverable_error, Output};
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Runnable for QueryAllClientsCmd {
.unwrap_or_else(exit_with_unrecoverable_error);

let req = QueryClientStatesRequest {
pagination: ibc_proto::cosmos::base::query::pagination::all(),
pagination: Some(PageRequest::all()),
};

let res: Result<_, Error> = chain.query_clients(req).map_err(Error::relayer);
Expand Down
Loading

0 comments on commit 3add5d3

Please sign in to comment.