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 initial whitelist for Stargate Query #2619

Merged
merged 30 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0835673
Add stargate queries
mattverse Jul 22, 2022
3d5b97c
Fix test
mattverse Jul 22, 2022
9dd65ff
Remove initial whitelist queries
mattverse Aug 1, 2022
344b1c1
Add initial bindings, change names
mattverse Aug 1, 2022
746c4af
Remove cosmos protos
mattverse Aug 1, 2022
b13bff2
Fix lint
mattverse Aug 2, 2022
eb216a1
Update wasm binary
mattverse Aug 3, 2022
55a9668
Revert "Update wasm binary"
mattverse Aug 3, 2022
ab9adc6
Bez's review
mattverse Aug 4, 2022
d8d2013
Add test cases
mattverse Aug 4, 2022
9871ddf
Remove logs
mattverse Aug 10, 2022
ba40941
WIP: return json marshalled response
mattverse Aug 10, 2022
2498529
Add Test Case
mattverse Aug 10, 2022
8ff9dbb
Merge branch 'main' into mattverse/test-json-marshalling
mattverse Aug 10, 2022
3681fac
Add changelog
mattverse Aug 10, 2022
c28e94e
Modify Changelog
mattverse Aug 11, 2022
36ab5d7
Normalize -> ProtoToJson
mattverse Aug 16, 2022
7e148a4
Merge branch 'main' into mattverse/test-json-marshalling
mattverse Aug 22, 2022
3957854
Fix merge conflict
mattverse Aug 22, 2022
204f20a
Add stargate querier test
mattverse Aug 30, 2022
49f709d
Add alot more test cases
mattverse Aug 31, 2022
1e6a842
Add test case for breaking grpc querier
mattverse Sep 1, 2022
18499bf
Update wasmbinding/query_plugin_test.go
mattverse Sep 1, 2022
0ad85d4
code review from upstream
mattverse Sep 6, 2022
4ffac7b
Add initial Stargate whitelist
mattverse Sep 6, 2022
4206c57
Fix lint
mattverse Sep 6, 2022
e6c13c6
Fix test
mattverse Sep 6, 2022
0e3543c
Update wasmbinding/stargate_whitelist.go
ValarDragon Sep 6, 2022
ac14ce3
Update wasmbinding/stargate_whitelist.go
ValarDragon Sep 6, 2022
c2f5554
Merge branch 'main' into mattverse/initial-stargate-queries
mattverse Sep 7, 2022
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1667](https://github.com/osmosis-labs/osmosis/pull/1673) Move wasm-bindings code out of app package into its own root level package.
* [#2013](https://github.com/osmosis-labs/osmosis/pull/2013) Make `SetParams`, `SetPool`, `SetTotalLiquidity`, and `SetDenomLiquidity` GAMM APIs private
* [#1857](https://github.com/osmosis-labs/osmosis/pull/1857) x/mint rename GetLastHalvenEpochNum to GetLastReductionEpochNum
* [#2353](https://github.com/osmosis-labs/osmosis/pull/2353) Re-enable stargate query via whitelsit
* [#2394](https://github.com/osmosis-labs/osmosis/pull/2394) Remove unused interface methods from expected keepers of each module
* [#2390](https://github.com/osmosis-labs/osmosis/pull/2390) x/mint remove unused mintCoins parameter from AfterDistributeMintedCoin
* [#2418](https://github.com/osmosis-labs/osmosis/pull/2418) x/mint remove SetInitialSupplyOffsetDuringMigration from keeper
Expand Down
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
supportedFeatures := "iterator,staking,stargate,osmosis"

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

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

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

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

// StargateQuerier dispatches whitelisted stargate queries
func StargateQuerier(queryRouter baseapp.GRPCQueryRouter, codec codec.Codec) func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
return func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
protoResponse, whitelisted := StargateWhitelist.Load(request.Path)
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,
})
if err != nil {
return nil, err
}

bz, err := ConvertProtoToJSONMarshal(protoResponse, res.Value, codec)
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 @@ -138,6 +171,50 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag
}
}

// 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 marshalling, which is supported in cosmwasm.
// func ConvertProtoToJSONMarshal(protoResponse interface{}, bz []byte, cdc codec.Codec) ([]byte, error) {
// // all values are proto message
// message, ok := protoResponse.(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{}
// }

// bz, err = codec.MarshalJSON(message)
// if err != nil {
// return nil, wasmvmtypes.Unknown{}
// }

// return bz, nil
// }
func ConvertProtoToJSONMarshal(protoResponse interface{}, bz []byte, cdc codec.Codec) ([]byte, error) {
// all values are proto message
message, ok := protoResponse.(codec.ProtoMarshaler)
if !ok {
return nil, wasmvmtypes.Unknown{}
}

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

bz, err = cdc.MarshalJSON(message)
if err != nil {
return nil, wasmvmtypes.Unknown{}
}

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
Loading