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 QueryRequest::Grpc #1973

Merged
merged 15 commits into from
Dec 21, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ and this project adheres to
- cosmwasm-std: Make `IbcReceiveResponse::acknowledgement` optional and add
`IbcReceiveResponse::without_ack` constructor. ([#1892])
- cosmwasm-std: Add `std` feature and make it a default feature. ([#1971])
- cosmwasm-std: Add `QueryRequest::Grpc` and deprecate `QueryRequest::Stargate`.
([#1973])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
Expand All @@ -91,6 +93,7 @@ and this project adheres to
[#1949]: https://github.com/CosmWasm/cosmwasm/pull/1949
[#1967]: https://github.com/CosmWasm/cosmwasm/pull/1967
[#1971]: https://github.com/CosmWasm/cosmwasm/pull/1971
[#1973]: https://github.com/CosmWasm/cosmwasm/pull/1973

### Removed

Expand Down
17 changes: 17 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ major releases of `cosmwasm`. Note that you can also view the
+};
```

- If you were using `QueryRequest::Stargate`, you might want to enable the
`cosmwasm_2_0` cargo feature and migrate to `QueryRequest::Grpc` instead.
While the stargate query sometimes returns protobuf encoded data and sometimes
JSON encoded data, depending on the chain, the gRPC query always returns
protobuf encoded data.

```diff
-deps.querier.query(&QueryRequest::Stargate {
- path: "/service.Path/ServiceMethod".to_string(),
- data: Binary::new(b"DATA"),
-})?;
+deps.querier.query(&QueryRequest::Grpc(GrpcQuery {
+ path: "/service.Path/ServiceMethod".to_string(),
+ data: Binary::new(b"DATA"),
+}))?;
```

## 1.4.x -> 1.5.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):
Expand Down
3 changes: 2 additions & 1 deletion contracts/reflect/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@
},
{
"description": "A Stargate query is encoded the same way as abci_query, with path and protobuf encoded request data. The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md). The response is protobuf encoded data directly without a JSON response wrapper. The caller is responsible for compiling the proper protobuf definitions for both requests and responses.",
"deprecated": true,
"type": "object",
"required": [
"stargate"
Expand All @@ -491,7 +492,7 @@
]
},
"path": {
"description": "this is the fully qualified service path used for routing, eg. custom/cosmos_sdk.x.bank.v1.Query/QueryBalance",
"description": "this is the fully qualified service path used for routing, eg. \"/cosmos_sdk.x.bank.v1.Query/QueryBalance\"",
"type": "string"
}
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/reflect/schema/reflect.json
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@
},
{
"description": "A Stargate query is encoded the same way as abci_query, with path and protobuf encoded request data. The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md). The response is protobuf encoded data directly without a JSON response wrapper. The caller is responsible for compiling the proper protobuf definitions for both requests and responses.",
"deprecated": true,
"type": "object",
"required": [
"stargate"
Expand All @@ -1494,7 +1495,7 @@
]
},
"path": {
"description": "this is the fully qualified service path used for routing, eg. custom/cosmos_sdk.x.bank.v1.Query/QueryBalance",
"description": "this is the fully qualified service path used for routing, eg. \"/cosmos_sdk.x.bank.v1.Query/QueryBalance\"",
"type": "string"
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/CAPABILITIES-BUILT-IN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ might define others.
`DistributionQuery::DelegationTotalRewards` and
`DistributionQuery::DelegatorValidators` queries. Only chains running CosmWasm
`1.4.0` or higher support this.
- `cosmwasm_2_0` enables `CosmosMsg::Any`. Only chains running CosmWasm `2.0.0`
or higher support this.
- `cosmwasm_2_0` enables `CosmosMsg::Any` and `QueryRequest::Grpc`. Only chains
running CosmWasm `2.0.0` or higher support this.
2 changes: 1 addition & 1 deletion packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cosmwasm_1_3 = ["cosmwasm_1_2"]
# It requires the host blockchain to run CosmWasm `1.4.0` or higher.
cosmwasm_1_4 = ["cosmwasm_1_3"]
# This enables functionality that is only available on 2.0 chains.
# It adds `CosmosMsg::Any`, replacing `CosmosMsg::Stargate`.
# It adds `CosmosMsg::Any`, replacing `CosmosMsg::Stargate`. It also adds `QueryRequest::Grpc`.
cosmwasm_2_0 = ["cosmwasm_1_4"]

[dependencies]
Expand Down
36 changes: 34 additions & 2 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// needed because the derive macros on QueryRequest use the deprecated `Stargate` variant
#![allow(deprecated)]

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::prelude::*;
#[cfg(feature = "stargate")]
use crate::Binary;
use crate::Empty;

Expand Down Expand Up @@ -53,16 +55,39 @@
/// The response is protobuf encoded data directly without a JSON response wrapper.
/// The caller is responsible for compiling the proper protobuf definitions for both requests and responses.
#[cfg(feature = "stargate")]
#[deprecated = "Please use the GrpcQuery instead"]
Stargate {
/// this is the fully qualified service path used for routing,
/// eg. custom/cosmos_sdk.x.bank.v1.Query/QueryBalance
/// eg. "/cosmos_sdk.x.bank.v1.Query/QueryBalance"
path: String,
/// this is the expected protobuf message type (not any), binary encoded
data: Binary,
},
#[cfg(feature = "stargate")]
Ibc(IbcQuery),
Wasm(WasmQuery),
#[cfg(feature = "cosmwasm_2_0")]
Grpc(GrpcQuery),
}

/// 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.
///
/// The returned data is protobuf encoded. The protobuf type depends on the query.
///
/// To 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).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct GrpcQuery {
/// The fully qualified endpoint path used for routing.
/// It follows the format `/service_path/method_name`,
/// eg. "/cosmos.authz.v1beta1.Query/Grants"
pub path: String,
/// The expected protobuf message type (not [Any](https://protobuf.dev/programming-guides/proto3/#any)), binary encoded
pub data: Binary,
}
chipshort marked this conversation as resolved.
Show resolved Hide resolved

/// A trait that is required to avoid conflicts with other query types like BankQuery and WasmQuery
Expand Down Expand Up @@ -115,6 +140,13 @@
}
}

#[cfg(feature = "cosmwasm_2_0")]
impl<C: CustomQuery> From<GrpcQuery> for QueryRequest<C> {
fn from(msg: GrpcQuery) -> Self {
QueryRequest::Grpc(msg)
}

Check warning on line 147 in packages/std/src/query/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/std/src/query/mod.rs#L145-L147

Added lines #L145 - L147 were not covered by tests
}

#[cfg(feature = "stargate")]
impl<C: CustomQuery> From<IbcQuery> for QueryRequest<C> {
fn from(msg: IbcQuery) -> Self {
Expand Down
5 changes: 5 additions & 0 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,14 @@
}
QueryRequest::Wasm(msg) => self.wasm.query(msg),
#[cfg(feature = "stargate")]
#[allow(deprecated)]
QueryRequest::Stargate { .. } => SystemResult::Err(SystemError::UnsupportedRequest {
kind: "Stargate".to_string(),
}),
#[cfg(feature = "cosmwasm_2_0")]
QueryRequest::Grpc(_) => SystemResult::Err(SystemError::UnsupportedRequest {
kind: "GRPC".to_string(),
}),

Check warning on line 612 in packages/std/src/testing/mock.rs

View check run for this annotation

Codecov / codecov/patch

packages/std/src/testing/mock.rs#L610-L612

Added lines #L610 - L612 were not covered by tests
#[cfg(feature = "stargate")]
QueryRequest::Ibc(msg) => self.ibc.query(msg),
}
Expand Down
Loading