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

Add query_grpc and document gRPC querying #2120

Merged
merged 3 commits into from
Apr 19, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to
are like the `Add`/`Sub` implementations but `const`. ([#2098], [#2107])
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
`Uint64::strict_add`/`::strict_sub` and document overflows. ([#2098], [#2107])
- cosmwasm-std: Add `QuerierWrapper::query_grpc` helper for gRPC queries.
([#2120])

[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
[#2057]: https://github.com/CosmWasm/cosmwasm/pull/2057
Expand All @@ -37,6 +39,7 @@ and this project adheres to
[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098
[#2099]: https://github.com/CosmWasm/cosmwasm/pull/2099
[#2107]: https://github.com/CosmWasm/cosmwasm/pull/2107
[#2120]: https://github.com/CosmWasm/cosmwasm/pull/2120

### Changed

Expand Down
2 changes: 1 addition & 1 deletion contracts/reflect/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
]
},
"GrpcQuery": {
"description": "Queries the chain using a grpc query. This allows to query information that is not exposed in our API. The chain needs to allowlist the supported queries. The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.\n\nThe returned data is protobuf encoded. The protobuf type depends on the query.\n\nTo find the path, as well as the request and response types, you can query the chain's gRPC endpoint using a tool like [grpcurl](https://github.com/fullstorydev/grpcurl).",
"description": "Queries the chain using a grpc query. This allows to query information that is not exposed in our API. The chain needs to allowlist the supported queries. The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.\n\nThe returned data is protobuf encoded. The protobuf type depends on the query. Because of this, using it with the [`query`](crate::QuerierWrapper::query) function will result in a deserialization error. Use [`raw_query`](crate::Querier::raw_query) or [`query_grpc`](crate::QuerierWrapper::query_grpc) instead.\n\nTo find the path, as well as the request and response types, you can query the chain's gRPC endpoint using a tool like [grpcurl](https://github.com/fullstorydev/grpcurl).",
"type": "object",
"required": [
"data",
Expand Down
2 changes: 1 addition & 1 deletion contracts/reflect/schema/reflect.json
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@
]
},
"GrpcQuery": {
"description": "Queries the chain using a grpc query. This allows to query information that is not exposed in our API. The chain needs to allowlist the supported queries. The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.\n\nThe returned data is protobuf encoded. The protobuf type depends on the query.\n\nTo find the path, as well as the request and response types, you can query the chain's gRPC endpoint using a tool like [grpcurl](https://github.com/fullstorydev/grpcurl).",
"description": "Queries the chain using a grpc query. This allows to query information that is not exposed in our API. The chain needs to allowlist the supported queries. The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.\n\nThe returned data is protobuf encoded. The protobuf type depends on the query. Because of this, using it with the [`query`](crate::QuerierWrapper::query) function will result in a deserialization error. Use [`raw_query`](crate::Querier::raw_query) or [`query_grpc`](crate::QuerierWrapper::query_grpc) instead.\n\nTo find the path, as well as the request and response types, you can query the chain's gRPC endpoint using a tool like [grpcurl](https://github.com/fullstorydev/grpcurl).",
"type": "object",
"required": [
"data",
Expand Down
4 changes: 4 additions & 0 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub enum QueryRequest<C> {
/// The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.
///
/// The returned data is protobuf encoded. The protobuf type depends on the query.
/// Because of this, using it with the [`query`](crate::QuerierWrapper::query) function will result
/// in a deserialization error.
/// Use [`raw_query`](crate::Querier::raw_query) or [`query_grpc`](crate::QuerierWrapper::query_grpc)
/// instead.
///
/// To find the path, as well as the request and response types,
/// you can query the chain's gRPC endpoint using a tool like
Expand Down
16 changes: 15 additions & 1 deletion packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
/// one level. Only use this if you don't need to check the SystemError
/// eg. If you don't differentiate between contract missing and contract returned error
pub fn query<U: DeserializeOwned>(&self, request: &QueryRequest<C>) -> StdResult<U> {
self.query_raw(request).and_then(|raw| from_json(raw))
}

/// Internal helper to avoid code duplication.
/// Performs a query and returns the binary result without deserializing it,
/// wrapping any errors that may occur into `StdError`.
fn query_raw(&self, request: &QueryRequest<C>) -> StdResult<Binary> {
let raw = to_json_vec(request).map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {serialize_err}"))
})?;
Expand All @@ -278,7 +285,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(
format!("Querier contract error: {contract_err}"),
)),
SystemResult::Ok(ContractResult::Ok(value)) => from_json(value),
SystemResult::Ok(ContractResult::Ok(value)) => Ok(value),
}
}

Expand Down Expand Up @@ -395,6 +402,13 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
Ok(res.validators)
}

/// See [`GrpcQuery`](crate::GrpcQuery) for more information.
#[cfg(feature = "cosmwasm_2_0")]
pub fn query_grpc(&self, path: String, data: Binary) -> StdResult<Binary> {
use crate::GrpcQuery;
self.query_raw(&QueryRequest::Grpc(GrpcQuery { path, data }))
}

/// Queries another wasm contract. You should know a priori the proper types for T and U
/// (response and request) based on the contract API
pub fn query_wasm_smart<T: DeserializeOwned>(
Expand Down