From 1130107d5ea4842640222617e57e055b0ee0b022 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Thu, 11 Jul 2024 17:08:40 -0600 Subject: [PATCH] add grpc querier for wasm (#2083) (#2084) * add grpc querier encoder for wasm * update changelog * fix comment for GrpcQuerier Co-authored-by: kwt <4344285+kwtalley@users.noreply.github.com> --- CHANGELOG.md | 1 + internal/provwasm/query_plugins.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccf574065..6e17c9429 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features * Create a script for updating links in the spec docs to proto messages [#2068](https://github.com/provenance-io/provenance/pull/2068). +* Add grpc querier for cosmwasm 2.1.0 smart contracts [#2083](https://github.com/provenance-io/provenance/pull/2083). ### Improvements diff --git a/internal/provwasm/query_plugins.go b/internal/provwasm/query_plugins.go index 5ac780df0..f1de46fda 100644 --- a/internal/provwasm/query_plugins.go +++ b/internal/provwasm/query_plugins.go @@ -57,6 +57,7 @@ func QueryPlugins(registry *QuerierRegistry, queryRouter baseapp.GRPCQueryRouter return &wasmkeeper.QueryPlugins{ Custom: customPlugins(registry), Stargate: StargateQuerier(queryRouter, stargateCdc), + Grpc: GrpcQuerier(queryRouter), } } @@ -121,6 +122,31 @@ func StargateQuerier(queryRouter baseapp.GRPCQueryRouter, cdc codec.Codec) func( } } +// GrpcQuerier dispatches whitelisted queries and returns protobuf encoded responses +func GrpcQuerier(queryRouter baseapp.GRPCQueryRouter) func(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error) { + return func(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error) { + _, err := GetWhitelistedQuery(request.Path) + if err != nil { + return nil, err + } + + route := queryRouter.Route(request.Path) + if route == nil { + return nil, wasmvmtypes.UnsupportedRequest{Kind: fmt.Sprintf("No route to query '%s'", request.Path)} + } + + res, err := route(ctx, &abci.RequestQuery{ + Data: request.Data, + Path: request.Path, + }) + if err != nil { + return nil, err + } + + return res, nil + } +} + // ConvertProtoToJsonMarshal unmarshals the given bytes into a proto message and then marshals it to json. // This is done so that clients calling stargate queries do not need to define their own proto unmarshalers, // being able to use response directly by json marshaling, which is supported in cosmwasm.