Skip to content

Commit

Permalink
Add height parameter to query commitments (#4194)
Browse files Browse the repository at this point in the history
* Add height parameter to query commitments

* Cleanup

* Update guide templates
  • Loading branch information
ancazamfir authored Sep 28, 2024
1 parent 0f912e7 commit 84743a2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
44 changes: 34 additions & 10 deletions crates/relayer-cli/src/commands/query/packet/commitments.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use abscissa_core::clap::Parser;

use ibc_relayer::chain::counterparty::commitments_on_chain;
use ibc_relayer::chain::requests::Paginate;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::{Paginate, QueryHeight};
use ibc_relayer_types::core::ics02_client::height::Height;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ChannelId, PortId};

use crate::cli_utils::spawn_chain_runtime;
use crate::conclude::Output;
use crate::conclude::{exit_with_unrecoverable_error, Output};
use crate::error::Error;
use crate::prelude::*;

Expand Down Expand Up @@ -40,6 +42,13 @@ pub struct QueryPacketCommitmentsCmd {
help = "Identifier of the channel to query"
)]
channel_id: ChannelId,

#[clap(
long = "height",
value_name = "HEIGHT",
help = "Height of the state to query. Leave unspecified for latest height."
)]
height: Option<u64>,
}

impl QueryPacketCommitmentsCmd {
Expand All @@ -48,12 +57,25 @@ impl QueryPacketCommitmentsCmd {

let chain = spawn_chain_runtime(&config, &self.chain_id)?;

commitments_on_chain(&chain, &self.port_id, &self.channel_id, Paginate::All)
.map_err(Error::supervisor)
.map(|(seqs_vec, height)| PacketSeqs {
height,
seqs: seqs_vec,
})
let query_height = self.height.map_or(QueryHeight::Latest, |revision_height| {
QueryHeight::Specific(
Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
});

commitments_on_chain(
&chain,
&query_height,
&self.port_id,
&self.channel_id,
Paginate::All,
)
.map_err(Error::supervisor)
.map(|(seqs_vec, height)| PacketSeqs {
height,
seqs: seqs_vec,
})
}
}

Expand Down Expand Up @@ -85,7 +107,8 @@ mod tests {
QueryPacketCommitmentsCmd {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap()
channel_id: ChannelId::from_str("channel-07").unwrap(),
height: None,
},
QueryPacketCommitmentsCmd::parse_from([
"test",
Expand All @@ -105,7 +128,8 @@ mod tests {
QueryPacketCommitmentsCmd {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap()
channel_id: ChannelId::from_str("channel-07").unwrap(),
height: None,
},
QueryPacketCommitmentsCmd::parse_from([
"test",
Expand Down
15 changes: 13 additions & 2 deletions crates/relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,8 @@ impl ChainEndpoint for CosmosSdkChain {
)
})?;

let height_param = AsciiMetadataValue::try_from(request.query_height)?;

if request.pagination.is_enabled() {
let mut results = Vec::new();
let mut page_key = Vec::new();
Expand Down Expand Up @@ -1890,6 +1892,10 @@ impl ChainEndpoint for CosmosSdkChain {
// TODO: This should either be configurable or inferred from the pagination
tonic_request.set_timeout(Duration::from_secs(10));

tonic_request
.metadata_mut()
.insert("x-cosmos-block-height", height_param.clone());

let response = self.rt.block_on(async {
client
.packet_commitments(tonic_request)
Expand Down Expand Up @@ -1946,9 +1952,14 @@ impl ChainEndpoint for CosmosSdkChain {

Ok((commitment_sequences, height))
} else {
let request = tonic::Request::new(request.into());
let mut tonic_request = tonic::Request::new(request.clone().into());

tonic_request
.metadata_mut()
.insert("x-cosmos-block-height", height_param);

let response = self
.block_on(client.packet_commitments(request))
.block_on(client.packet_commitments(tonic_request))
.map_err(|e| Error::grpc_status(e, "query_packet_commitments".to_owned()))?
.into_inner();

Expand Down
22 changes: 18 additions & 4 deletions crates/relayer/src/chain/counterparty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,15 @@ pub fn check_channel_counterparty(
/// - received on counterparty chain but not yet acknowledged by this chain,
pub fn commitments_on_chain(
chain: &impl ChainHandle,
query_height: &QueryHeight,
port_id: &PortId,
channel_id: &ChannelId,
paginate: Paginate,
) -> Result<(Vec<Sequence>, Height), Error> {
// get the packet commitments on the counterparty/ source chain
let (mut commit_sequences, response_height) = chain
.query_packet_commitments(QueryPacketCommitmentsRequest {
query_height: *query_height,
port_id: port_id.clone(),
channel_id: channel_id.clone(),
pagination: paginate,
Expand Down Expand Up @@ -506,6 +508,7 @@ pub fn unreceived_packets(
) -> Result<(Vec<Sequence>, Height), Error> {
let (commit_sequences, h) = commitments_on_chain(
counterparty_chain,
&QueryHeight::Latest,
&path.counterparty_port_id,
&path.counterparty_channel_id,
paginate,
Expand Down Expand Up @@ -543,6 +546,7 @@ pub fn acknowledgements_on_chain(

let (commitments_on_counterparty, _) = commitments_on_chain(
counterparty_chain,
&QueryHeight::Latest,
&counterparty.port_id,
counterparty_channel_id,
Paginate::All,
Expand Down Expand Up @@ -594,8 +598,13 @@ pub fn unreceived_acknowledgements(
path: &PathIdentifiers,
pagination: Paginate,
) -> Result<Option<(Vec<Sequence>, Height)>, Error> {
let (commitments_on_src, _) =
commitments_on_chain(chain, &path.port_id, &path.channel_id, pagination)?;
let (commitments_on_src, _) = commitments_on_chain(
chain,
&QueryHeight::Latest,
&path.port_id,
&path.channel_id,
pagination,
)?;

let acks_and_height_on_counterparty = packet_acknowledgements(
counterparty_chain,
Expand Down Expand Up @@ -642,8 +651,13 @@ pub fn pending_packet_summary(
.as_ref()
.ok_or_else(Error::missing_counterparty_channel_id)?;

let (commitments_on_src, _) =
commitments_on_chain(chain, &channel.port_id, &channel.channel_id, pagination)?;
let (commitments_on_src, _) = commitments_on_chain(
chain,
&QueryHeight::Latest,
&channel.port_id,
&channel.channel_id,
pagination,
)?;

let unreceived = unreceived_packets_sequences(
counterparty_chain,
Expand Down
1 change: 1 addition & 0 deletions crates/relayer/src/chain/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ pub struct QueryPacketCommitmentRequest {
/// gRPC query to fetch the packet commitment hashes associated with the specified channel.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct QueryPacketCommitmentsRequest {
pub query_height: QueryHeight,
pub port_id: PortId,
pub channel_id: ChannelId,
pub pagination: Paginate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[[#BINARY hermes]][[#GLOBALOPTIONS]] query packet commitments --chain [[#CHAIN_ID]] --port [[#PORT_ID]] --channel [[#CHANNEL_ID]]
[[#BINARY hermes]][[#GLOBALOPTIONS]] query packet commitments[[#OPTIONS]] --chain [[#CHAIN_ID]] --port [[#PORT_ID]] --channel [[#CHANNEL_ID]]
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ DESCRIPTION:
Query packet commitments

USAGE:
hermes query packet commitments --chain <CHAIN_ID> --port <PORT_ID> --channel <CHANNEL_ID>
hermes query packet commitments [OPTIONS] --chain <CHAIN_ID> --port <PORT_ID> --channel <CHANNEL_ID>

OPTIONS:
-h, --help Print help information
-h, --help Print help information
--height <HEIGHT> Height of the state to query. Leave unspecified for latest height.

REQUIRED:
--chain <CHAIN_ID> Identifier of the chain to query
Expand Down

0 comments on commit 84743a2

Please sign in to comment.