From 84743a21c3be533c4b362276a6f213bf6cf27454 Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Sat, 28 Sep 2024 04:29:04 -0400 Subject: [PATCH] Add height parameter to query commitments (#4194) * Add height parameter to query commitments * Cleanup * Update guide templates --- .../src/commands/query/packet/commitments.rs | 44 ++++++++++++++----- crates/relayer/src/chain/cosmos.rs | 15 ++++++- crates/relayer/src/chain/counterparty.rs | 22 ++++++++-- crates/relayer/src/chain/requests.rs | 1 + .../hermes/query/packet/commitments_1.md | 2 +- .../query/packet/commitments.md | 5 ++- 6 files changed, 70 insertions(+), 19 deletions(-) diff --git a/crates/relayer-cli/src/commands/query/packet/commitments.rs b/crates/relayer-cli/src/commands/query/packet/commitments.rs index 725b5de562..836df55b27 100644 --- a/crates/relayer-cli/src/commands/query/packet/commitments.rs +++ b/crates/relayer-cli/src/commands/query/packet/commitments.rs @@ -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::*; @@ -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, } impl QueryPacketCommitmentsCmd { @@ -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, + }) } } @@ -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", @@ -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", diff --git a/crates/relayer/src/chain/cosmos.rs b/crates/relayer/src/chain/cosmos.rs index cc5b809cad..5e53f57fdf 100644 --- a/crates/relayer/src/chain/cosmos.rs +++ b/crates/relayer/src/chain/cosmos.rs @@ -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(); @@ -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) @@ -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(); diff --git a/crates/relayer/src/chain/counterparty.rs b/crates/relayer/src/chain/counterparty.rs index ffd4a42c9c..118c118a62 100644 --- a/crates/relayer/src/chain/counterparty.rs +++ b/crates/relayer/src/chain/counterparty.rs @@ -379,6 +379,7 @@ 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, @@ -386,6 +387,7 @@ pub fn commitments_on_chain( // 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, @@ -506,6 +508,7 @@ pub fn unreceived_packets( ) -> Result<(Vec, Height), Error> { let (commit_sequences, h) = commitments_on_chain( counterparty_chain, + &QueryHeight::Latest, &path.counterparty_port_id, &path.counterparty_channel_id, paginate, @@ -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, @@ -594,8 +598,13 @@ pub fn unreceived_acknowledgements( path: &PathIdentifiers, pagination: Paginate, ) -> Result, 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, @@ -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, diff --git a/crates/relayer/src/chain/requests.rs b/crates/relayer/src/chain/requests.rs index d86770e7f7..80162ee6e8 100644 --- a/crates/relayer/src/chain/requests.rs +++ b/crates/relayer/src/chain/requests.rs @@ -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, diff --git a/guide/src/templates/commands/hermes/query/packet/commitments_1.md b/guide/src/templates/commands/hermes/query/packet/commitments_1.md index 57b807de52..32d0ca5476 100644 --- a/guide/src/templates/commands/hermes/query/packet/commitments_1.md +++ b/guide/src/templates/commands/hermes/query/packet/commitments_1.md @@ -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]] diff --git a/guide/src/templates/help_templates/query/packet/commitments.md b/guide/src/templates/help_templates/query/packet/commitments.md index b12e02d44f..33d211e2bf 100644 --- a/guide/src/templates/help_templates/query/packet/commitments.md +++ b/guide/src/templates/help_templates/query/packet/commitments.md @@ -2,10 +2,11 @@ DESCRIPTION: Query packet commitments USAGE: - hermes query packet commitments --chain --port --channel + hermes query packet commitments [OPTIONS] --chain --port --channel OPTIONS: - -h, --help Print help information + -h, --help Print help information + --height Height of the state to query. Leave unspecified for latest height. REQUIRED: --chain Identifier of the chain to query