Skip to content

Commit

Permalink
Consolidate ChainEndpoint/Handle proven queries (#2226)
Browse files Browse the repository at this point in the history
* Remove TODO

* query_client_state consolidation

* query_connection consolidation

* query_channel consolidation

* query_packet_commitment function

* query_packet_acknowledgement

* query_packet_receipt

* query_next_sequence_receive

* use request instead of build_packet_proofs

where appropriate

* build_packet_proofs now only returns proofs

* build_packet_proofs no longer uses proven_packet

* proven_packet removed

* query_consensus_state

* remove proven_client_consensus

* cleanup

* docstrings

* changelog

* fix compilation errors

* mock consensus_state query

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
plafer and romac authored Jun 6, 2022
1 parent 08b7f98 commit b5b165b
Show file tree
Hide file tree
Showing 41 changed files with 1,841 additions and 1,202 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Consolidate ChainEndpoint::proven_* methods with their corresponding query_*()
form ([#2223](https://github.com/informalsystems/ibc-rs/issues/2223))
20 changes: 0 additions & 20 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,6 @@ pub mod cosmos {
pub mod v1beta1 {
include_proto!("cosmos.base.query.v1beta1.rs");
}

// TODO (BEFORE MERGING PR): Remove
pub mod pagination {
use super::v1beta1::PageRequest;

pub fn all() -> Option<PageRequest> {
Some(PageRequest {
limit: u64::MAX,
..Default::default()
})
}

pub fn latest_limited(limit: u64) -> Option<PageRequest> {
Some(PageRequest {
limit,
reverse: true,
..Default::default()
})
}
}
}
pub mod reflection {
pub mod v1beta1 {
Expand Down
28 changes: 17 additions & 11 deletions relayer-cli/src/commands/create/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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::chain::requests::{IncludeProof, QueryClientStateRequest, QueryConnectionRequest};
use ibc_relayer::channel::Channel;
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;
Expand Down Expand Up @@ -187,20 +187,26 @@ impl CreateChannelCommand {

// Query the connection end.
let height = Height::new(chain_a.id().version(), 0);
let conn_end = chain_a
.query_connection(QueryConnectionRequest {
connection_id: connection_a.clone(),
height,
})
let (conn_end, _) = chain_a
.query_connection(
QueryConnectionRequest {
connection_id: connection_a.clone(),
height,
},
IncludeProof::No,
)
.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(QueryClientStateRequest {
client_id: conn_end.client_id().clone(),
height,
})
.map(|cs| cs.chain_id())
.query_client_state(
QueryClientStateRequest {
client_id: conn_end.client_id().clone(),
height,
},
IncludeProof::No,
)
.map(|(cs, _)| cs.chain_id())
.unwrap_or_else(exit_with_unrecoverable_error);

// Spawn the runtime for side b.
Expand Down
15 changes: 9 additions & 6 deletions relayer-cli/src/commands/create/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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::chain::requests::{IncludeProof, QueryClientStateRequest};
use ibc_relayer::connection::Connection;
use ibc_relayer::foreign_client::ForeignClient;

Expand Down Expand Up @@ -115,11 +115,14 @@ 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(QueryClientStateRequest {
client_id: client_a_id.clone(),
height,
}) {
Ok(cs) => cs.chain_id(),
let chain_b_id = match chain_a.query_client_state(
QueryClientStateRequest {
client_id: client_a_id.clone(),
height,
},
IncludeProof::No,
) {
Ok((cs, _)) => cs.chain_id(),
Err(e) => Output::error(format!(
"failed while querying client '{}' on chain '{}' with error: {}",
client_a_id, self.chain_a_id, e
Expand Down
15 changes: 9 additions & 6 deletions relayer-cli/src/commands/misbehaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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::chain::requests::{IncludeProof, QueryClientStateRequest};
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::{ForeignClient, MisbehaviourResults};
use std::ops::Deref;
Expand Down Expand Up @@ -99,11 +99,14 @@ fn misbehaviour_handling<Chain: ChainHandle>(
client_id: ClientId,
update: Option<UpdateClient>,
) -> Result<(), Box<dyn std::error::Error>> {
let client_state = chain
.query_client_state(QueryClientStateRequest {
client_id: client_id.clone(),
height: Height::zero(),
})
let (client_state, _) = chain
.query_client_state(
QueryClientStateRequest {
client_id: client_id.clone(),
height: Height::zero(),
},
IncludeProof::No,
)
.map_err(|e| format!("could not query client state for {}: {}", client_id, e))?;

if client_state.is_frozen() {
Expand Down
17 changes: 10 additions & 7 deletions relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ibc_relayer::chain::handle::ChainHandle;

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::requests::{IncludeProof, QueryChannelRequest};

use crate::cli_utils::spawn_chain_runtime;
use crate::conclude::{exit_with_unrecoverable_error, Output};
Expand Down Expand Up @@ -35,13 +35,16 @@ impl Runnable for QueryChannelEndCmd {
let chain = spawn_chain_runtime(&config, &self.chain_id)
.unwrap_or_else(exit_with_unrecoverable_error);

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)),
});
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)),
},
IncludeProof::No,
);
match res {
Ok(channel_end) => {
Ok((channel_end, _)) => {
if channel_end.state_matches(&State::Uninitialized) {
Output::error(format!(
"port '{}' & channel '{}' does not exist",
Expand Down
68 changes: 42 additions & 26 deletions relayer-cli/src/commands/query/channel_ends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ibc::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortI
use ibc::Height;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::chain::requests::{
QueryChannelRequest, QueryClientStateRequest, QueryConnectionRequest,
IncludeProof, QueryChannelRequest, QueryClientStateRequest, QueryConnectionRequest,
};
use ibc_relayer::registry::Registry;

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

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

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

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

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

let channel_counterparty = channel_end.counterparty().clone();
let connection_counterparty = connection_end.counterparty().clone();
Expand Down Expand Up @@ -145,23 +154,30 @@ 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(QueryConnectionRequest {
let (counterparty_connection_end, _) = counterparty_chain.query_connection(
QueryConnectionRequest {
connection_id: counterparty_connection_id.clone(),
height: counterparty_chain_height,
})?;
},
IncludeProof::No,
)?;

let counterparty_client_state =
counterparty_chain.query_client_state(QueryClientStateRequest {
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,
})?;
},
IncludeProof::No,
)?;

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

if cmd.verbose {
let res = ChannelEnds {
Expand Down
55 changes: 34 additions & 21 deletions relayer-cli/src/commands/query/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ibc::core::ics24_host::identifier::{ChainId, ChannelId, ConnectionId, PortCh
use ibc::Height;
use ibc_relayer::chain::handle::{BaseChainHandle, ChainHandle};
use ibc_relayer::chain::requests::{
PageRequest, QueryChannelRequest, QueryChannelsRequest, QueryClientStateRequest,
IncludeProof, PageRequest, QueryChannelRequest, QueryChannelsRequest, QueryClientStateRequest,
QueryConnectionRequest,
};
use ibc_relayer::registry::Registry;
Expand Down Expand Up @@ -126,15 +126,21 @@ 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(QueryConnectionRequest {
connection_id: connection_id.clone(),
height: chain_height,
})?;
let (connection_end, _) = chain.query_connection(
QueryConnectionRequest {
connection_id: connection_id.clone(),
height: chain_height,
},
IncludeProof::No,
)?;
let client_id = connection_end.client_id().clone();
let client_state = chain.query_client_state(QueryClientStateRequest {
client_id,
height: chain_height,
})?;
let (client_state, _) = chain.query_client_state(
QueryClientStateRequest {
client_id,
height: chain_height,
},
IncludeProof::No,
)?;
let counterparty_chain_id = client_state.chain_id();

if let Some(dst_chain_id) = destination_chain {
Expand Down Expand Up @@ -173,23 +179,30 @@ 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(QueryConnectionRequest {
let (counterparty_connection_end, _) = counterparty_chain.query_connection(
QueryConnectionRequest {
connection_id: counterparty_connection_id,
height: counterparty_chain_height,
})?;
},
IncludeProof::No,
)?;

let counterparty_client_state =
counterparty_chain.query_client_state(QueryClientStateRequest {
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,
})?;
},
IncludeProof::No,
)?;

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

Ok(ChannelEnds {
channel_end,
Expand Down
Loading

0 comments on commit b5b165b

Please sign in to comment.