Skip to content

Commit 84743a2

Browse files
authored
Add height parameter to query commitments (#4194)
* Add height parameter to query commitments * Cleanup * Update guide templates
1 parent 0f912e7 commit 84743a2

File tree

6 files changed

+70
-19
lines changed

6 files changed

+70
-19
lines changed

crates/relayer-cli/src/commands/query/packet/commitments.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use abscissa_core::clap::Parser;
22

33
use ibc_relayer::chain::counterparty::commitments_on_chain;
4-
use ibc_relayer::chain::requests::Paginate;
4+
use ibc_relayer::chain::handle::ChainHandle;
5+
use ibc_relayer::chain::requests::{Paginate, QueryHeight};
6+
use ibc_relayer_types::core::ics02_client::height::Height;
57
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
68

79
use crate::cli_utils::spawn_chain_runtime;
8-
use crate::conclude::Output;
10+
use crate::conclude::{exit_with_unrecoverable_error, Output};
911
use crate::error::Error;
1012
use crate::prelude::*;
1113

@@ -40,6 +42,13 @@ pub struct QueryPacketCommitmentsCmd {
4042
help = "Identifier of the channel to query"
4143
)]
4244
channel_id: ChannelId,
45+
46+
#[clap(
47+
long = "height",
48+
value_name = "HEIGHT",
49+
help = "Height of the state to query. Leave unspecified for latest height."
50+
)]
51+
height: Option<u64>,
4352
}
4453

4554
impl QueryPacketCommitmentsCmd {
@@ -48,12 +57,25 @@ impl QueryPacketCommitmentsCmd {
4857

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

51-
commitments_on_chain(&chain, &self.port_id, &self.channel_id, Paginate::All)
52-
.map_err(Error::supervisor)
53-
.map(|(seqs_vec, height)| PacketSeqs {
54-
height,
55-
seqs: seqs_vec,
56-
})
60+
let query_height = self.height.map_or(QueryHeight::Latest, |revision_height| {
61+
QueryHeight::Specific(
62+
Height::new(chain.id().version(), revision_height)
63+
.unwrap_or_else(exit_with_unrecoverable_error),
64+
)
65+
});
66+
67+
commitments_on_chain(
68+
&chain,
69+
&query_height,
70+
&self.port_id,
71+
&self.channel_id,
72+
Paginate::All,
73+
)
74+
.map_err(Error::supervisor)
75+
.map(|(seqs_vec, height)| PacketSeqs {
76+
height,
77+
seqs: seqs_vec,
78+
})
5779
}
5880
}
5981

@@ -85,7 +107,8 @@ mod tests {
85107
QueryPacketCommitmentsCmd {
86108
chain_id: ChainId::from_string("chain_id"),
87109
port_id: PortId::from_str("port_id").unwrap(),
88-
channel_id: ChannelId::from_str("channel-07").unwrap()
110+
channel_id: ChannelId::from_str("channel-07").unwrap(),
111+
height: None,
89112
},
90113
QueryPacketCommitmentsCmd::parse_from([
91114
"test",
@@ -105,7 +128,8 @@ mod tests {
105128
QueryPacketCommitmentsCmd {
106129
chain_id: ChainId::from_string("chain_id"),
107130
port_id: PortId::from_str("port_id").unwrap(),
108-
channel_id: ChannelId::from_str("channel-07").unwrap()
131+
channel_id: ChannelId::from_str("channel-07").unwrap(),
132+
height: None,
109133
},
110134
QueryPacketCommitmentsCmd::parse_from([
111135
"test",

crates/relayer/src/chain/cosmos.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,8 @@ impl ChainEndpoint for CosmosSdkChain {
18631863
)
18641864
})?;
18651865

1866+
let height_param = AsciiMetadataValue::try_from(request.query_height)?;
1867+
18661868
if request.pagination.is_enabled() {
18671869
let mut results = Vec::new();
18681870
let mut page_key = Vec::new();
@@ -1890,6 +1892,10 @@ impl ChainEndpoint for CosmosSdkChain {
18901892
// TODO: This should either be configurable or inferred from the pagination
18911893
tonic_request.set_timeout(Duration::from_secs(10));
18921894

1895+
tonic_request
1896+
.metadata_mut()
1897+
.insert("x-cosmos-block-height", height_param.clone());
1898+
18931899
let response = self.rt.block_on(async {
18941900
client
18951901
.packet_commitments(tonic_request)
@@ -1946,9 +1952,14 @@ impl ChainEndpoint for CosmosSdkChain {
19461952

19471953
Ok((commitment_sequences, height))
19481954
} else {
1949-
let request = tonic::Request::new(request.into());
1955+
let mut tonic_request = tonic::Request::new(request.clone().into());
1956+
1957+
tonic_request
1958+
.metadata_mut()
1959+
.insert("x-cosmos-block-height", height_param);
1960+
19501961
let response = self
1951-
.block_on(client.packet_commitments(request))
1962+
.block_on(client.packet_commitments(tonic_request))
19521963
.map_err(|e| Error::grpc_status(e, "query_packet_commitments".to_owned()))?
19531964
.into_inner();
19541965

crates/relayer/src/chain/counterparty.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,15 @@ pub fn check_channel_counterparty(
379379
/// - received on counterparty chain but not yet acknowledged by this chain,
380380
pub fn commitments_on_chain(
381381
chain: &impl ChainHandle,
382+
query_height: &QueryHeight,
382383
port_id: &PortId,
383384
channel_id: &ChannelId,
384385
paginate: Paginate,
385386
) -> Result<(Vec<Sequence>, Height), Error> {
386387
// get the packet commitments on the counterparty/ source chain
387388
let (mut commit_sequences, response_height) = chain
388389
.query_packet_commitments(QueryPacketCommitmentsRequest {
390+
query_height: *query_height,
389391
port_id: port_id.clone(),
390392
channel_id: channel_id.clone(),
391393
pagination: paginate,
@@ -506,6 +508,7 @@ pub fn unreceived_packets(
506508
) -> Result<(Vec<Sequence>, Height), Error> {
507509
let (commit_sequences, h) = commitments_on_chain(
508510
counterparty_chain,
511+
&QueryHeight::Latest,
509512
&path.counterparty_port_id,
510513
&path.counterparty_channel_id,
511514
paginate,
@@ -543,6 +546,7 @@ pub fn acknowledgements_on_chain(
543546

544547
let (commitments_on_counterparty, _) = commitments_on_chain(
545548
counterparty_chain,
549+
&QueryHeight::Latest,
546550
&counterparty.port_id,
547551
counterparty_channel_id,
548552
Paginate::All,
@@ -594,8 +598,13 @@ pub fn unreceived_acknowledgements(
594598
path: &PathIdentifiers,
595599
pagination: Paginate,
596600
) -> Result<Option<(Vec<Sequence>, Height)>, Error> {
597-
let (commitments_on_src, _) =
598-
commitments_on_chain(chain, &path.port_id, &path.channel_id, pagination)?;
601+
let (commitments_on_src, _) = commitments_on_chain(
602+
chain,
603+
&QueryHeight::Latest,
604+
&path.port_id,
605+
&path.channel_id,
606+
pagination,
607+
)?;
599608

600609
let acks_and_height_on_counterparty = packet_acknowledgements(
601610
counterparty_chain,
@@ -642,8 +651,13 @@ pub fn pending_packet_summary(
642651
.as_ref()
643652
.ok_or_else(Error::missing_counterparty_channel_id)?;
644653

645-
let (commitments_on_src, _) =
646-
commitments_on_chain(chain, &channel.port_id, &channel.channel_id, pagination)?;
654+
let (commitments_on_src, _) = commitments_on_chain(
655+
chain,
656+
&QueryHeight::Latest,
657+
&channel.port_id,
658+
&channel.channel_id,
659+
pagination,
660+
)?;
647661

648662
let unreceived = unreceived_packets_sequences(
649663
counterparty_chain,

crates/relayer/src/chain/requests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ pub struct QueryPacketCommitmentRequest {
338338
/// gRPC query to fetch the packet commitment hashes associated with the specified channel.
339339
#[derive(Clone, Debug, Serialize, Deserialize)]
340340
pub struct QueryPacketCommitmentsRequest {
341+
pub query_height: QueryHeight,
341342
pub port_id: PortId,
342343
pub channel_id: ChannelId,
343344
pub pagination: Paginate,
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[#BINARY hermes]][[#GLOBALOPTIONS]] query packet commitments --chain [[#CHAIN_ID]] --port [[#PORT_ID]] --channel [[#CHANNEL_ID]]
1+
[[#BINARY hermes]][[#GLOBALOPTIONS]] query packet commitments[[#OPTIONS]] --chain [[#CHAIN_ID]] --port [[#PORT_ID]] --channel [[#CHANNEL_ID]]

guide/src/templates/help_templates/query/packet/commitments.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ DESCRIPTION:
22
Query packet commitments
33

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

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

1011
REQUIRED:
1112
--chain <CHAIN_ID> Identifier of the chain to query

0 commit comments

Comments
 (0)