diff --git a/.changelog/unreleased/bug-fixes/1154-use-query-height-in-query-helper-functions.md b/.changelog/unreleased/bug-fixes/1154-use-query-height-in-query-helper-functions.md new file mode 100644 index 000000000..cf1e2bde2 --- /dev/null +++ b/.changelog/unreleased/bug-fixes/1154-use-query-height-in-query-helper-functions.md @@ -0,0 +1,2 @@ +- [ibc-query] Update standalone query functions to use the specified query + height ([\#1154](https://github.com/cosmos/ibc-rs/issues/1154)) diff --git a/ibc-query/src/core/channel/query.rs b/ibc-query/src/core/channel/query.rs index ff39fec72..19875c78f 100644 --- a/ibc-query/src/core/channel/query.rs +++ b/ibc-query/src/core/channel/query.rs @@ -39,20 +39,20 @@ where let channel_end = ibc_ctx.channel_end(&channel_end_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; + let proof = ibc_ctx - .get_proof(current_height, &Path::ChannelEnd(channel_end_path.clone())) + .get_proof(proof_height, &Path::ChannelEnd(channel_end_path.clone())) .ok_or_else(|| { QueryError::proof_not_found(format!( "Proof not found for channel end path {channel_end_path:?}" )) })?; - Ok(QueryChannelResponse::new( - channel_end, - proof, - current_height, - )) + Ok(QueryChannelResponse::new(channel_end, proof, proof_height)) } /// Queries for all existing IBC channels and returns the corresponding channel ends @@ -129,11 +129,14 @@ where let client_state = client_val_ctx.client_state(connection_end.client_id())?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::ClientState(ClientStatePath::new(connection_end.client_id().clone())), ) .ok_or_else(|| { @@ -146,7 +149,7 @@ where Ok(QueryChannelClientStateResponse::new( IdentifiedClientState::new(connection_end.client_id().clone(), client_state.into()), proof, - current_height, + proof_height, )) } @@ -184,11 +187,14 @@ where let consensus_state = client_val_ctx.consensus_state(&consensus_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::ClientConsensusState(consensus_path.clone()), ) .ok_or_else(|| { @@ -201,7 +207,7 @@ where consensus_state.into(), connection_end.client_id().clone(), proof, - current_height, + proof_height, )) } @@ -219,10 +225,13 @@ where let packet_commitment_data = ibc_ctx.get_packet_commitment(&commitment_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx - .get_proof(current_height, &Path::Commitment(commitment_path.clone())) + .get_proof(proof_height, &Path::Commitment(commitment_path.clone())) .ok_or_else(|| { QueryError::proof_not_found(format!( "Proof not found for packet commitment path: {commitment_path:?}" @@ -232,7 +241,7 @@ where Ok(QueryPacketCommitmentResponse::new( packet_commitment_data, proof, - current_height, + proof_height, )) } @@ -274,10 +283,13 @@ where // Unreceived packets are not stored let packet_receipt_data = ibc_ctx.get_packet_receipt(&receipt_path); - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx - .get_proof(current_height, &Path::Receipt(receipt_path.clone())) + .get_proof(proof_height, &Path::Receipt(receipt_path.clone())) .ok_or_else(|| { QueryError::proof_not_found(format!( "Proof not found for packet receipt path: {receipt_path:?}" @@ -287,7 +299,7 @@ where Ok(QueryPacketReceiptResponse::new( packet_receipt_data.is_ok(), proof, - current_height, + proof_height, )) } @@ -305,10 +317,13 @@ where let packet_acknowledgement_data = ibc_ctx.get_packet_acknowledgement(&acknowledgement_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx - .get_proof(current_height, &Path::Ack(acknowledgement_path.clone())) + .get_proof(proof_height, &Path::Ack(acknowledgement_path.clone())) .ok_or_else(|| { QueryError::proof_not_found(format!( "Proof not found for packet acknowledgement path: {acknowledgement_path:?}" @@ -318,7 +333,7 @@ where Ok(QueryPacketAcknowledgementResponse::new( packet_acknowledgement_data, proof, - current_height, + proof_height, )) } @@ -408,10 +423,13 @@ where let next_sequence_send = ibc_ctx.get_next_sequence_send(&next_seq_send_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx - .get_proof(current_height, &Path::SeqSend(next_seq_send_path)) + .get_proof(proof_height, &Path::SeqSend(next_seq_send_path)) .ok_or_else(|| { QueryError::proof_not_found(format!( "Next sequence send proof not found for channel {}", @@ -422,7 +440,7 @@ where Ok(QueryNextSequenceSendResponse::new( next_sequence_send, proof, - current_height, + proof_height, )) } @@ -438,10 +456,13 @@ where let next_sequence_recv = ibc_ctx.get_next_sequence_recv(&next_seq_recv_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx - .get_proof(current_height, &Path::SeqRecv(next_seq_recv_path)) + .get_proof(proof_height, &Path::SeqRecv(next_seq_recv_path)) .ok_or_else(|| { QueryError::proof_not_found(format!( "Next sequence receive proof not found for channel {}", @@ -452,6 +473,6 @@ where Ok(QueryNextSequenceReceiveResponse::new( next_sequence_recv, proof, - current_height, + proof_height, )) } diff --git a/ibc-query/src/core/client/query.rs b/ibc-query/src/core/client/query.rs index c7e98170a..ab9ee05c2 100644 --- a/ibc-query/src/core/client/query.rs +++ b/ibc-query/src/core/client/query.rs @@ -38,11 +38,14 @@ where let client_state = client_val_ctx.client_state(&client_id)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::ClientState(ClientStatePath::new(client_id.clone())), ) .ok_or_else(|| { @@ -54,7 +57,7 @@ where Ok(QueryClientStateResponse::new( client_state.into(), proof, - current_height, + proof_height, )) } @@ -111,11 +114,14 @@ where })? }; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::ClientConsensusState(ClientConsensusStatePath::new( client_id.clone(), height.revision_number(), @@ -131,7 +137,7 @@ where Ok(QueryConsensusStateResponse::new( consensus_state.into(), proof, - current_height, + proof_height, )) } diff --git a/ibc-query/src/core/connection/query.rs b/ibc-query/src/core/connection/query.rs index 419c45417..d156c4c1c 100644 --- a/ibc-query/src/core/connection/query.rs +++ b/ibc-query/src/core/connection/query.rs @@ -30,11 +30,14 @@ where { let connection_end = ibc_ctx.connection_end(&request.connection_id)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::Connection(ConnectionPath::new(&request.connection_id)), ) .ok_or_else(|| { @@ -47,7 +50,7 @@ where Ok(QueryConnectionResponse::new( connection_end, proof, - current_height, + proof_height, )) } @@ -78,11 +81,14 @@ where { let connections = ibc_ctx.client_connection_ends(&request.client_id)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof: Proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::ClientConnection(ClientConnectionPath::new(request.client_id.clone())), ) .ok_or_else(|| { @@ -95,7 +101,7 @@ where Ok(QueryClientConnectionsResponse::new( connections, proof, - current_height, + proof_height, )) } @@ -113,11 +119,14 @@ where let client_state = client_val_ctx.client_state(connection_end.client_id())?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx .get_proof( - current_height, + proof_height, &Path::ClientState(ClientStatePath::new(connection_end.client_id().clone())), ) .ok_or_else(|| { @@ -130,7 +139,7 @@ where Ok(QueryConnectionClientStateResponse::new( IdentifiedClientState::new(connection_end.client_id().clone(), client_state.into()), proof, - current_height, + proof_height, )) } @@ -155,10 +164,13 @@ where let consensus_state = client_val_ctx.consensus_state(&consensus_path)?; - let current_height = ibc_ctx.host_height()?; + let proof_height = match request.query_height { + Some(height) => height, + None => ibc_ctx.host_height()?, + }; let proof = ibc_ctx - .get_proof(current_height, &Path::ClientConsensusState(consensus_path)) + .get_proof(proof_height, &Path::ClientConsensusState(consensus_path)) .ok_or_else(|| { QueryError::proof_not_found(format!( "Proof not found for consensus state path: {:?}", @@ -170,7 +182,7 @@ where consensus_state.into(), connection_end.client_id().clone(), proof, - current_height, + proof_height, )) } diff --git a/ibc-query/src/core/connection/types/request.rs b/ibc-query/src/core/connection/types/request.rs index e62262adf..cf0f5a6f9 100644 --- a/ibc-query/src/core/connection/types/request.rs +++ b/ibc-query/src/core/connection/types/request.rs @@ -59,6 +59,7 @@ impl From for QueryConnectionsRequest { #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct QueryClientConnectionsRequest { pub client_id: ClientId, + pub query_height: Option, } impl TryFrom for QueryClientConnectionsRequest { @@ -67,6 +68,7 @@ impl TryFrom for QueryClientConnectionsRequest fn try_from(request: RawQueryClientConnectionsRequest) -> Result { Ok(Self { client_id: request.client_id.parse()?, + query_height: None, }) } }