Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup query_next_sequence_receive #3662

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions crates/relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1961,48 +1961,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
Loading