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

Refactor 08-wasm with improvements after 02-client refactor #6088

Merged
merged 10 commits into from
Apr 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type WasmEngine interface {
Expand Down Expand Up @@ -123,8 +122,3 @@ type QueryRouter interface {
// if not found
Route(path string) baseapp.GRPCQueryHandler
}

type QueryPluginsI interface {
// HandleQuery will route the query to the correct plugin and return the result
HandleQuery(ctx sdk.Context, caller string, request wasmvmtypes.QueryRequest) ([]byte, error)
}
77 changes: 0 additions & 77 deletions modules/light-clients/08-wasm/internal/ibcwasm/wasm.go

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types_test
package keeper_test

import (
"encoding/json"
Expand All @@ -15,7 +15,7 @@ import (
localhost "github.com/cosmos/ibc-go/v8/modules/light-clients/09-localhost"
)

func (suite *TypesTestSuite) TestWasmInstantiate() {
func (suite *KeeperTestSuite) TestWasmInstantiate() {
testCases := []struct {
name string
malleate func()
Expand Down Expand Up @@ -165,17 +165,19 @@ func (suite *TypesTestSuite) TestWasmInstantiate() {
tc := tc
suite.Run(tc.name, func() {
suite.SetupWasmWithMockVM()
checksum := storeWasmCode(suite, wasmtesting.Code)

tc.malleate()

initMsg := types.InstantiateMessage{
ClientState: clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), wasmtesting.MockTendermitClientState),
ConsensusState: clienttypes.MustMarshalConsensusState(suite.chainA.App.AppCodec(), wasmtesting.MockTendermintClientConsensusState),
Checksum: suite.checksum,
Checksum: checksum,
}

clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), defaultWasmClientID)
err := types.WasmInstantiate(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, &types.ClientState{Checksum: suite.checksum}, initMsg)
wasmClientKeeper := GetSimApp(suite.chainA).WasmClientKeeper
err := wasmClientKeeper.WasmInstantiate(suite.chainA.GetContext(), defaultWasmClientID, clientStore, &types.ClientState{Checksum: checksum}, initMsg)

expPass := tc.expError == nil
if expPass {
Expand All @@ -187,7 +189,7 @@ func (suite *TypesTestSuite) TestWasmInstantiate() {
}
}

func (suite *TypesTestSuite) TestWasmMigrate() {
func (suite *KeeperTestSuite) TestWasmMigrate() {
testCases := []struct {
name string
malleate func()
Expand Down Expand Up @@ -305,6 +307,7 @@ func (suite *TypesTestSuite) TestWasmMigrate() {
tc := tc
suite.Run(tc.name, func() {
suite.SetupWasmWithMockVM()
_ = storeWasmCode(suite, wasmtesting.Code)

endpoint := wasmtesting.NewWasmEndpoint(suite.chainA)
err := endpoint.CreateClient()
Expand All @@ -313,7 +316,8 @@ func (suite *TypesTestSuite) TestWasmMigrate() {
tc.malleate()

clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), defaultWasmClientID)
err = types.WasmMigrate(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, &types.ClientState{}, defaultWasmClientID, []byte("{}"))
wasmClientKeeper := GetSimApp(suite.chainA).WasmClientKeeper
err = wasmClientKeeper.WasmMigrate(suite.chainA.GetContext(), clientStore, &types.ClientState{}, defaultWasmClientID, []byte("{}"))

expPass := tc.expError == nil
if expPass {
Expand All @@ -325,7 +329,7 @@ func (suite *TypesTestSuite) TestWasmMigrate() {
}
}

func (suite *TypesTestSuite) TestWasmQuery() {
func (suite *KeeperTestSuite) TestWasmQuery() {
var payload types.QueryMsg

testCases := []struct {
Expand Down Expand Up @@ -368,21 +372,13 @@ func (suite *TypesTestSuite) TestWasmQuery() {
},
types.ErrWasmContractCallFailed,
},
{
"failure: response fails to unmarshal",
func() {
suite.mockVM.RegisterQueryCallback(types.StatusMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, _ wasmvm.KVStore, _ wasmvm.GoAPI, _ wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) (*wasmvmtypes.QueryResult, uint64, error) {
return &wasmvmtypes.QueryResult{Ok: []byte("invalid json")}, wasmtesting.DefaultGasUsed, nil
})
},
types.ErrWasmInvalidResponseData,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupWasmWithMockVM()
_ = storeWasmCode(suite, wasmtesting.Code)

endpoint := wasmtesting.NewWasmEndpoint(suite.chainA)
err := endpoint.CreateClient()
Expand All @@ -398,7 +394,8 @@ func (suite *TypesTestSuite) TestWasmQuery() {

tc.malleate()

res, err := types.WasmQuery[types.StatusResult](suite.chainA.GetContext(), clientStore, wasmClientState, payload)
wasmClientKeeper := GetSimApp(suite.chainA).WasmClientKeeper
res, err := wasmClientKeeper.WasmQuery(suite.chainA.GetContext(), endpoint.ClientID, clientStore, wasmClientState, payload)

expPass := tc.expError == nil
if expPass {
Expand All @@ -411,7 +408,7 @@ func (suite *TypesTestSuite) TestWasmQuery() {
}
}

func (suite *TypesTestSuite) TestWasmSudo() {
func (suite *KeeperTestSuite) TestWasmSudo() {
var payload types.SudoMsg

testCases := []struct {
Expand Down Expand Up @@ -487,15 +484,6 @@ func (suite *TypesTestSuite) TestWasmSudo() {
},
types.ErrWasmAttributesNotAllowed,
},
{
"failure: response fails to unmarshal",
func() {
suite.mockVM.RegisterSudoCallback(types.UpdateStateMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, _ wasmvm.KVStore, _ wasmvm.GoAPI, _ wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) (*wasmvmtypes.ContractResult, uint64, error) {
return &wasmvmtypes.ContractResult{Ok: &wasmvmtypes.Response{Data: []byte("invalid json")}}, wasmtesting.DefaultGasUsed, nil
})
},
types.ErrWasmInvalidResponseData,
},
{
"failure: invalid clientstate type",
func() {
Expand Down Expand Up @@ -561,6 +549,7 @@ func (suite *TypesTestSuite) TestWasmSudo() {
tc := tc
suite.Run(tc.name, func() {
suite.SetupWasmWithMockVM()
_ = storeWasmCode(suite, wasmtesting.Code)

endpoint := wasmtesting.NewWasmEndpoint(suite.chainA)
err := endpoint.CreateClient()
Expand All @@ -576,7 +565,8 @@ func (suite *TypesTestSuite) TestWasmSudo() {

tc.malleate()

res, err := types.WasmSudo[types.UpdateStateResult](suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, wasmClientState, payload)
wasmClientKeeper := GetSimApp(suite.chainA).WasmClientKeeper
res, err := wasmClientKeeper.WasmSudo(suite.chainA.GetContext(), endpoint.ClientID, clientStore, wasmClientState, payload)

expPass := tc.expError == nil
if expPass {
Expand Down
8 changes: 8 additions & 0 deletions modules/light-clients/08-wasm/keeper/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package keeper

import sdk "github.com/cosmos/cosmos-sdk/types"

// MigrateContractCode is a wrapper around k.migrateContractCode to allow the method to be directly called in tests.
func (k Keeper) MigrateContractCode(ctx sdk.Context, clientID string, newChecksum, migrateMsg []byte) error {
return k.migrateContractCode(ctx, clientID, newChecksum, migrateMsg)
}
9 changes: 4 additions & 5 deletions modules/light-clients/08-wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
)

// InitGenesis initializes the 08-wasm module's state from a provided genesis
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) error {
storeFn := func(code wasmvm.WasmCode, _ uint64) (wasmvm.Checksum, uint64, error) {
checksum, err := ibcwasm.GetVM().StoreCodeUnchecked(code)
checksum, err := k.GetVM().StoreCodeUnchecked(code)
return checksum, 0, err
}

Expand All @@ -28,16 +27,16 @@ func (k Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) error {

// ExportGenesis returns the 08-wasm module's exported genesis. This includes the code
// for all contracts previously stored.
func (Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
checksums, err := types.GetAllChecksums(ctx)
func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
checksums, err := k.GetAllChecksums(ctx)
if err != nil {
panic(err)
}

// Grab code from wasmVM and add to genesis state.
var genesisState types.GenesisState
for _, checksum := range checksums {
code, err := ibcwasm.GetVM().GetCode(checksum)
code, err := k.GetVM().GetCode(checksum)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
suite.Require().NoError(err)

var storedHashes []string
checksums, err := types.GetAllChecksums(suite.chainA.GetContext())
checksums, err := GetSimApp(suite.chainA).WasmClientKeeper.GetAllChecksums(suite.chainA.GetContext())
suite.Require().NoError(err)

for _, hash := range checksums {
Expand Down
11 changes: 5 additions & 6 deletions modules/light-clients/08-wasm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkquery "github.com/cosmos/cosmos-sdk/types/query"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
)

var _ types.QueryServer = (*Keeper)(nil)

// Code implements the Query/Code gRPC method
func (Keeper) Code(goCtx context.Context, req *types.QueryCodeRequest) (*types.QueryCodeResponse, error) {
func (k Keeper) Code(goCtx context.Context, req *types.QueryCodeRequest) (*types.QueryCodeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
Expand All @@ -31,11 +30,11 @@ func (Keeper) Code(goCtx context.Context, req *types.QueryCodeRequest) (*types.Q
}

// Only return checksums we previously stored, not arbitrary checksums that might be stored via e.g Wasmd.
if !types.HasChecksum(sdk.UnwrapSDKContext(goCtx), checksum) {
if !k.HasChecksum(sdk.UnwrapSDKContext(goCtx), checksum) {
return nil, status.Error(codes.NotFound, errorsmod.Wrap(types.ErrWasmChecksumNotFound, req.Checksum).Error())
}

code, err := ibcwasm.GetVM().GetCode(checksum)
code, err := k.GetVM().GetCode(checksum)
if err != nil {
return nil, status.Error(codes.NotFound, errorsmod.Wrap(types.ErrWasmChecksumNotFound, req.Checksum).Error())
}
Expand All @@ -46,10 +45,10 @@ func (Keeper) Code(goCtx context.Context, req *types.QueryCodeRequest) (*types.Q
}

// Checksums implements the Query/Checksums gRPC method. It returns a list of hex encoded checksums stored.
func (Keeper) Checksums(goCtx context.Context, req *types.QueryChecksumsRequest) (*types.QueryChecksumsResponse, error) {
func (k Keeper) Checksums(goCtx context.Context, req *types.QueryChecksumsRequest) (*types.QueryChecksumsResponse, error) {
checksums, pageRes, err := sdkquery.CollectionPaginate(
goCtx,
ibcwasm.Checksums,
k.GetChecksums(),
req.Pagination,
func(key []byte, value collections.NoValue) (string, error) {
return hex.EncodeToString(key), nil
Expand Down
Loading
Loading