Skip to content

Commit

Permalink
Fix a bug in query_next_sequence_receive, where if we didnt ask for…
Browse files Browse the repository at this point in the history
… a proof, then the query height was ignored (#3662)
  • Loading branch information
romac authored Oct 21, 2023
1 parent 9a7ef31 commit 7e2b14c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
58 changes: 21 additions & 37 deletions crates/relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,48 +1958,32 @@ impl ChainEndpoint for CosmosSdkChain {
);
crate::telemetry!(query, self.id(), "query_next_sequence_receive");

match include_proof {
IncludeProof::Yes => {
let res = self.query(
SeqRecvsPath(request.port_id, request.channel_id),
request.height,
true,
)?;
let prove = include_proof.to_bool();

// Note: We expect the return to be a u64 encoded in big-endian. Refer to ibc-go:
// https://github.com/cosmos/ibc-go/blob/25767f6bdb5bab2c2a116b41d92d753c93e18121/modules/core/04-channel/client/utils/utils.go#L191
if res.value.len() != 8 {
return Err(Error::query("next_sequence_receive".into()));
}
let seq: Sequence = Bytes::from(res.value).get_u64().into();

let proof = res.proof.ok_or_else(Error::empty_response_proof)?;

Ok((seq, Some(proof)))
}
IncludeProof::No => {
let mut client = self
.block_on(
ibc_proto::ibc::core::channel::v1::query_client::QueryClient::connect(
self.grpc_addr.clone(),
),
)
.map_err(Error::grpc_transport)?;
let res = self.query(
SeqRecvsPath(request.port_id, request.channel_id),
request.height,
true,
)?;

client = client.max_decoding_message_size(
self.config().max_grpc_decoding_size.get_bytes() as usize,
);
// Note: We expect the return to be a u64 encoded in big-endian. Refer to ibc-go:
// https://github.com/cosmos/ibc-go/blob/25767f6bdb5bab2c2a116b41d92d753c93e18121/modules/core/04-channel/client/utils/utils.go#L191
if res.value.len() != 8 {
return Err(Error::query(format!(
"next_sequence_receive: expected a u64 but got {} bytes of data",
res.value.len()
)));
}

let request = tonic::Request::new(request.into());
let seq: Sequence = Bytes::from(res.value).get_u64().into();

let response = self
.block_on(client.next_sequence_receive(request))
.map_err(|e| Error::grpc_status(e, "query_next_sequence_receive".to_owned()))?
.into_inner();
let proof = if prove {
Some(res.proof.ok_or_else(Error::empty_response_proof)?)
} else {
None
};

Ok((Sequence::from(response.next_sequence_receive), None))
}
}
Ok((seq, proof))
}

/// This function queries transactions for events matching certain criteria.
Expand Down
8 changes: 7 additions & 1 deletion crates/relayer/src/chain/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ impl Display for QueryHeight {

/// Defines a type to be used in select requests to specify whether or not a proof should be
/// returned along with the response.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum IncludeProof {
Yes,
No,
}

impl IncludeProof {
pub fn to_bool(&self) -> bool {
*self == IncludeProof::Yes
}
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PageRequest {
/// key is a value returned in PageResponse.next_key to begin
Expand Down

0 comments on commit 7e2b14c

Please sign in to comment.