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

Enable deterministic Cosmwasm stargate queries #2190

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
supportedFeatures := "iterator,staking,stargate,osmosis"

wasmOpts = append(owasm.RegisterCustomPlugins(appKeepers.GAMMKeeper, appKeepers.BankKeeper, appKeepers.TokenFactoryKeeper), wasmOpts...)
wasmOpts = append(owasm.RegisterStargateQueries(*bApp.GRPCQueryRouter()), wasmOpts...)

wasmKeeper := wasm.NewKeeper(
appCodec,
Expand Down
59 changes: 59 additions & 0 deletions wasmbinding/query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,46 @@ import (
"fmt"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
proto "github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/osmosis-labs/osmosis/v10/wasmbinding/bindings"
)

func StarGateQuerier(queryRouter baseapp.GRPCQueryRouter) func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
mattverse marked this conversation as resolved.
Show resolved Hide resolved
return func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
binding, whitelisted := StargateWhitelist.Load(request.Path)
Copy link
Member

Choose a reason for hiding this comment

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

Just want to be extra-sure, this path is the /cosmos.auth.v1beta1.Query/Account path, NOT the http path in the proto files right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, they are defined in the whitelist file

if !whitelisted {
return nil, wasmvmtypes.UnsupportedRequest{Kind: fmt.Sprintf("'%s' path is not allowed from the contract", request.Path)}
}

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,
})

mattverse marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

// normalize response to ensure backward compatibility
bz, err := NormalizeReponses(binding, res.Value)
if err != nil {
return nil, err
}

return bz, nil
}
}

// CustomQuerier dispatches custom CosmWasm bindings queries.
func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
Expand Down Expand Up @@ -110,6 +144,31 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag
}
}

func NormalizeReponses(binding interface{}, bz []byte) ([]byte, error) {
mattverse marked this conversation as resolved.
Show resolved Hide resolved
// all values are proto message
message, ok := binding.(proto.Message)
if !ok {
return nil, wasmvmtypes.Unknown{}
}

// unmarshal binary into stargate response data structure
err := proto.Unmarshal(bz, message)
if err != nil {
return nil, wasmvmtypes.Unknown{}
}

// build new deterministic response
bz, err = proto.Marshal(message)
if err != nil {
return nil, wasmvmtypes.Unknown{}
}

// clear proto message
message.Reset()
mattverse marked this conversation as resolved.
Show resolved Hide resolved

return bz, nil
}

// ConvertSdkCoinsToWasmCoins converts sdk type coins to wasm vm type coins
func ConvertSdkCoinsToWasmCoins(coins []sdk.Coin) wasmvmtypes.Coins {
var toSend wasmvmtypes.Coins
Expand Down
22 changes: 22 additions & 0 deletions wasmbinding/stargate_whitelist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package wasmbinding

import (
"sync"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

epochtypes "github.com/osmosis-labs/osmosis/v10/x/epochs/types"
)

// StargateLayerWhitelist keeps whitelist and its deterministic
// response binding for stargate queries.
//
// The query can be multi-thread, so we have to use
// thread safe sync.Map instead map[string]bool.
var StargateWhitelist sync.Map

func init() {
StargateWhitelist.Store("/cosmos.auth.v1beta1.Query/Account", authtypes.QueryAccountResponse{})

StargateWhitelist.Store("/osmosis.epochs.v1beta1.Query/EpochInfos", epochtypes.QueryEpochsInfoRequest{})
}
13 changes: 13 additions & 0 deletions wasmbinding/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package wasmbinding

import (
"github.com/CosmWasm/wasmd/x/wasm"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/cosmos/cosmos-sdk/baseapp"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

gammkeeper "github.com/osmosis-labs/osmosis/v10/x/gamm/keeper"
Expand All @@ -28,3 +31,13 @@ func RegisterCustomPlugins(
messengerDecoratorOpt,
}
}

func RegisterStargateQueries(queryRouter baseapp.GRPCQueryRouter) []wasmkeeper.Option {
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Stargate: StarGateQuerier(queryRouter),
})

return []wasm.Option{
queryPluginOpt,
}
}