Skip to content

Commit

Permalink
fix: update standalone query functions to use the specified `query_he…
Browse files Browse the repository at this point in the history
…ight` (#1155)

* fix: use query_height in query functions

* chore: add changelog

* nit
  • Loading branch information
Farhad-Shabani authored Apr 8, 2024
1 parent 1f581fa commit 91bee67
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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))
77 changes: 49 additions & 28 deletions ibc-query/src/core/channel/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(|| {
Expand All @@ -146,7 +149,7 @@ where
Ok(QueryChannelClientStateResponse::new(
IdentifiedClientState::new(connection_end.client_id().clone(), client_state.into()),
proof,
current_height,
proof_height,
))
}

Expand Down Expand Up @@ -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(|| {
Expand All @@ -201,7 +207,7 @@ where
consensus_state.into(),
connection_end.client_id().clone(),
proof,
current_height,
proof_height,
))
}

Expand All @@ -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:?}"
Expand All @@ -232,7 +241,7 @@ where
Ok(QueryPacketCommitmentResponse::new(
packet_commitment_data,
proof,
current_height,
proof_height,
))
}

Expand Down Expand Up @@ -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:?}"
Expand All @@ -287,7 +299,7 @@ where
Ok(QueryPacketReceiptResponse::new(
packet_receipt_data.is_ok(),
proof,
current_height,
proof_height,
))
}

Expand All @@ -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:?}"
Expand All @@ -318,7 +333,7 @@ where
Ok(QueryPacketAcknowledgementResponse::new(
packet_acknowledgement_data,
proof,
current_height,
proof_height,
))
}

Expand Down Expand Up @@ -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 {}",
Expand All @@ -422,7 +440,7 @@ where
Ok(QueryNextSequenceSendResponse::new(
next_sequence_send,
proof,
current_height,
proof_height,
))
}

Expand All @@ -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 {}",
Expand All @@ -452,6 +473,6 @@ where
Ok(QueryNextSequenceReceiveResponse::new(
next_sequence_recv,
proof,
current_height,
proof_height,
))
}
18 changes: 12 additions & 6 deletions ibc-query/src/core/client/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand All @@ -54,7 +57,7 @@ where
Ok(QueryClientStateResponse::new(
client_state.into(),
proof,
current_height,
proof_height,
))
}

Expand Down Expand Up @@ -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(),
Expand All @@ -131,7 +137,7 @@ where
Ok(QueryConsensusStateResponse::new(
consensus_state.into(),
proof,
current_height,
proof_height,
))
}

Expand Down
36 changes: 24 additions & 12 deletions ibc-query/src/core/connection/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand All @@ -47,7 +50,7 @@ where
Ok(QueryConnectionResponse::new(
connection_end,
proof,
current_height,
proof_height,
))
}

Expand Down Expand Up @@ -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(|| {
Expand All @@ -95,7 +101,7 @@ where
Ok(QueryClientConnectionsResponse::new(
connections,
proof,
current_height,
proof_height,
))
}

Expand All @@ -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(|| {
Expand All @@ -130,7 +139,7 @@ where
Ok(QueryConnectionClientStateResponse::new(
IdentifiedClientState::new(connection_end.client_id().clone(), client_state.into()),
proof,
current_height,
proof_height,
))
}

Expand All @@ -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: {:?}",
Expand All @@ -170,7 +182,7 @@ where
consensus_state.into(),
connection_end.client_id().clone(),
proof,
current_height,
proof_height,
))
}

Expand Down
2 changes: 2 additions & 0 deletions ibc-query/src/core/connection/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl From<RawQueryConnectionsRequest> for QueryConnectionsRequest {
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct QueryClientConnectionsRequest {
pub client_id: ClientId,
pub query_height: Option<Height>,
}

impl TryFrom<RawQueryClientConnectionsRequest> for QueryClientConnectionsRequest {
Expand All @@ -67,6 +68,7 @@ impl TryFrom<RawQueryClientConnectionsRequest> for QueryClientConnectionsRequest
fn try_from(request: RawQueryClientConnectionsRequest) -> Result<Self, Self::Error> {
Ok(Self {
client_id: request.client_id.parse()?,
query_height: None,
})
}
}
Expand Down

0 comments on commit 91bee67

Please sign in to comment.