Skip to content

Commit

Permalink
Change StargateQuery to GrpcQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Jan 26, 2024
1 parent 401557b commit 605beb4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ where the old name was deprecated.
| `GoAPI.CanonicalAddress` | `GoAPI.CanonicalizeAddress` | Perfer verbs for converters |
| `CosmosMsg.Stargate` | `CosmosMsg.Any` | The message has nothing to do with Stargate |
| `StargateMsg` | `AnyMsg` | The message has nothing to do with Stargate |
| `QueryRequest.Stargate` | `QueryRequest.Grpc` | The query has nothing to do with Stargate |
| `StargateQuery` | `GrpcQuery` | The query has nothing to do with Stargate |
| `QueryResponse` | `QueryResult` | Brings consistency with the naming of the other results |
| `VoteMsg.Vote` | `VoteMsg.Option` | Brings consistency with Cosmos SDK naming |

Expand Down
60 changes: 50 additions & 10 deletions types/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"
"fmt"
)

//-------- Queries --------
Expand Down Expand Up @@ -105,10 +106,47 @@ type QueryRequest struct {
IBC *IBCQuery `json:"ibc,omitempty"`
Staking *StakingQuery `json:"staking,omitempty"`
Distribution *DistributionQuery `json:"distribution,omitempty"`
Stargate *StargateQuery `json:"stargate,omitempty"`
Grpc *GrpcQuery `json:"grpc,omitempty"`
Wasm *WasmQuery `json:"wasm,omitempty"`
}

func (m *QueryRequest) UnmarshalJSON(data []byte) error {
// We need a custom unmarshaler to parse both the "stargate" and "any" variants
type InternalQueryRequest struct {
Bank *BankQuery `json:"bank,omitempty"`
Custom json.RawMessage `json:"custom,omitempty"`
IBC *IBCQuery `json:"ibc,omitempty"`
Staking *StakingQuery `json:"staking,omitempty"`
Distribution *DistributionQuery `json:"distribution,omitempty"`
Grpc *GrpcQuery `json:"grpc,omitempty"`
Wasm *WasmQuery `json:"wasm,omitempty"`
Stargate *GrpcQuery `json:"stargate,omitempty"`
}
var tmp InternalQueryRequest
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}

if tmp.Grpc != nil && tmp.Stargate != nil {
return fmt.Errorf("invalid QueryRequest: both 'grpc' and 'stargate' fields are set")
} else if tmp.Grpc == nil && tmp.Stargate != nil {
// Use "Grpc" for both variants
tmp.Grpc = tmp.Stargate
}

*m = QueryRequest{
Bank: tmp.Bank,
Custom: tmp.Custom,
IBC: tmp.IBC,
Staking: tmp.Staking,
Distribution: tmp.Distribution,
Grpc: tmp.Grpc,
Wasm: tmp.Wasm,
}
return nil
}

type BankQuery struct {
Supply *SupplyQuery `json:"supply,omitempty"`
Balance *BalanceQuery `json:"balance,omitempty"`
Expand Down Expand Up @@ -433,16 +471,18 @@ type BondedDenomResponse struct {
Denom string `json:"denom"`
}

// StargateQuery 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.
type StargateQuery struct {
// this is the fully qualified service path used for routing,
// eg. custom/cosmos_sdk.x.bank.v1.Query/QueryBalance
Path string `json:"path"`
// this is the expected protobuf message type (not any), binary encoded
// 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 returned data is protobuf encoded. The protobuf type depends on the query.
type GrpcQuery struct {
// The expected protobuf message type (not [Any](https://protobuf.dev/programming-guides/proto3/#any)), binary encoded
Data []byte `json:"data"`
// The fully qualified endpoint path used for routing.
// It follows the format `/service_path/method_name`,
// eg. "/cosmos.authz.v1beta1.Query/Grants"
Path string `json:"path"`
}

type WasmQuery struct {
Expand Down

0 comments on commit 605beb4

Please sign in to comment.