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

Streamline Stargate/gRPC querier interfaces #2093

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 23 additions & 11 deletions x/wasm/keeper/query_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@

type CustomQuerier func(ctx sdk.Context, request json.RawMessage) ([]byte, error)

type (
stargateQuerierFn func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error)
grpcQuerierFn func(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error)
)

type QueryPlugins struct {
Bank func(ctx sdk.Context, request *wasmvmtypes.BankQuery) ([]byte, error)
Custom CustomQuerier
IBC func(ctx sdk.Context, caller sdk.AccAddress, request *wasmvmtypes.IBCQuery) ([]byte, error)
Staking func(ctx sdk.Context, request *wasmvmtypes.StakingQuery) ([]byte, error)
Stargate func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error)
Grpc func(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error)
Stargate stargateQuerierFn
Grpc grpcQuerierFn
Wasm func(ctx sdk.Context, request *wasmvmtypes.WasmQuery) ([]byte, error)
Distribution func(ctx sdk.Context, request *wasmvmtypes.DistributionQuery) ([]byte, error)
}
Expand Down Expand Up @@ -121,7 +126,7 @@
Custom: NoCustomQuerier,
IBC: IBCQuerier(wasm, channelKeeper),
Staking: StakingQuerier(staking, distKeeper),
Stargate: RejectStargateQuerier(),
Stargate: RejectStargateQuerier,
Grpc: RejectGrpcQuerier,
Wasm: WasmQuerier(wasm),
Distribution: DistributionQuerier(distKeeper),
Expand Down Expand Up @@ -332,10 +337,15 @@
}
}

// RejectGrpcQuerier is a querier that rejects all gRPC queries.
//
// Use AcceptListGrpcQuerier instead to create a list of accepted query types.
func RejectGrpcQuerier(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error) {
return nil, wasmvmtypes.UnsupportedRequest{Kind: "gRPC queries are disabled"}
return nil, wasmvmtypes.UnsupportedRequest{Kind: "gRPC queries are disabled on this chain"}

Check warning on line 344 in x/wasm/keeper/query_plugins.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/query_plugins.go#L344

Added line #L344 was not covered by tests
}

var _ grpcQuerierFn = RejectGrpcQuerier // just a type check

// AcceptListGrpcQuerier supports a preconfigured set of gRPC queries only.
// All arguments must be non nil.
//
Expand All @@ -344,7 +354,7 @@
//
// These queries can be set via WithQueryPlugins option in the wasm keeper constructor:
// WithQueryPlugins(&QueryPlugins{Grpc: AcceptListGrpcQuerier(acceptList, queryRouter, codec)})
func AcceptListGrpcQuerier(acceptList AcceptedQueries, queryRouter GRPCQueryRouter, codec codec.Codec) func(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error) {
func AcceptListGrpcQuerier(acceptList AcceptedQueries, queryRouter GRPCQueryRouter, codec codec.Codec) grpcQuerierFn {

Check warning on line 357 in x/wasm/keeper/query_plugins.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/query_plugins.go#L357

Added line #L357 was not covered by tests
return func(ctx sdk.Context, request *wasmvmtypes.GrpcQuery) (proto.Message, error) {
protoResponseFn, accepted := acceptList[request.Path]
if !accepted {
Expand Down Expand Up @@ -375,13 +385,15 @@
}
}

// RejectStargateQuerier rejects all stargate queries
func RejectStargateQuerier() func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
return func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
return nil, wasmvmtypes.UnsupportedRequest{Kind: "Stargate queries are disabled"}
}
// RejectStargateQuerier is a querier that rejects all stargate queries.
//
// Use AcceptListStargateQuerier instead to create a list of accepted query types.
func RejectStargateQuerier(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is breaking since it changes the signature of a public function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's breaking. I would assume that most people don't use the RejectStargateQuerier directly, since it's the default anyways, but not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github only finds copies, no usage: https://github.com/search?q=RejectStargateQuerier&type=code
So probably a good change to include in 0.55

return nil, wasmvmtypes.UnsupportedRequest{Kind: "Stargate queries are disabled on this chain"}

Check warning on line 392 in x/wasm/keeper/query_plugins.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/query_plugins.go#L391-L392

Added lines #L391 - L392 were not covered by tests
}

var _ stargateQuerierFn = RejectStargateQuerier // just a type check
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice


// AcceptedQueries defines accepted Stargate or gRPC queries as a map where the key is the query path
// and the value is a function returning a proto.Message.
//
Expand All @@ -400,7 +412,7 @@
//
// These queries can be set via WithQueryPlugins option in the wasm keeper constructor:
// WithQueryPlugins(&QueryPlugins{Stargate: AcceptListStargateQuerier(acceptList, queryRouter, codec)})
func AcceptListStargateQuerier(acceptList AcceptedQueries, queryRouter GRPCQueryRouter, codec codec.Codec) func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
func AcceptListStargateQuerier(acceptList AcceptedQueries, queryRouter GRPCQueryRouter, codec codec.Codec) stargateQuerierFn {
return func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
protoResponseFn, accepted := acceptList[request.Path]
if !accepted {
Expand Down
Loading