Skip to content

Commit

Permalink
feat: replace wasmvmv1.5 with wasmvmv2.0.0rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
tungleanh0902 committed Mar 14, 2024
1 parent 3f7320c commit f53cabf
Show file tree
Hide file tree
Showing 29 changed files with 290 additions and 251 deletions.
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
cosmossdk.io/x/feegrant v0.1.0
cosmossdk.io/x/tx v0.13.1
cosmossdk.io/x/upgrade v0.1.1
github.com/CosmWasm/wasmvm v1.5.2
github.com/CosmWasm/wasmvm/v2 v2.0.0-rc.2
github.com/cometbft/cometbft v0.38.5
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/cosmos-sdk v0.50.4
Expand Down
4 changes: 2 additions & 2 deletions modules/light-clients/08-wasm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CosmWasm/wasmvm v1.5.2 h1:+pKB1Mz9GZVt1vadxB+EDdD1FOz3dMNjIKq/58/lrag=
github.com/CosmWasm/wasmvm v1.5.2/go.mod h1:Q0bSEtlktzh7W2hhEaifrFp1Erx11ckQZmjq8FLCyys=
github.com/CosmWasm/wasmvm/v2 v2.0.0-rc.2 h1:2rAO0MpNmKHKsrX/lsyo7lLjKoGDOWwMk+40hTNVfdE=
github.com/CosmWasm/wasmvm/v2 v2.0.0-rc.2/go.mod h1:su9lg5qLr7adV95eOfzjZWkGiky8WNaNIHDr7Fpu7Ck=
github.com/DataDog/datadog-go v3.2.0+incompatible h1:qSG2N4FghB1He/r2mFrWKCaL7dXCilEuNEeAn20fdD4=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ibcwasm

import (
wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
wasmvm "github.com/CosmWasm/wasmvm/v2"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -16,7 +16,7 @@ type WasmEngine interface {
// This must be done one time for given code, after which it can be
// instantiated many times, and each instance called many times.
// It does the same as StoreCodeUnchecked plus the static checks.
StoreCode(code wasmvm.WasmCode) (wasmvm.Checksum, error)
StoreCode(code wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)

// StoreCodeUnchecked will compile the wasm code, and store the resulting pre-compile
// as well as the original code. Both can be referenced later via checksum
Expand Down Expand Up @@ -45,7 +45,7 @@ type WasmEngine interface {
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error)
) (*wasmvmtypes.ContractResult, uint64, error)

// Query allows a client to execute a contract-specific query. If the result is not empty, it should be
// valid json-encoded data to return to the client.
Expand All @@ -60,7 +60,7 @@ type WasmEngine interface {
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) ([]byte, uint64, error)
) (*wasmvmtypes.QueryResult, uint64, error)

// Migrate migrates an existing contract to a new code binary.
// This takes storage of the data from the original contract and the checksum of the new contract that should
Expand All @@ -78,13 +78,13 @@ type WasmEngine interface {
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error)
) (*wasmvmtypes.ContractResult, uint64, error)

// Sudo allows native Go modules to make privileged (sudo) calls on the contract.
// Sudo allows native Go modules to make priviledged (sudo) calls on the contract.
// The contract can expose entry points that cannot be triggered by any transaction, but only via
// native Go modules, and delegate the access control to the system.
//
// These work much like Migrate (same scenario) but allows custom apps to extend the privileged entry points
// These work much like Migrate (same scenario) but allows custom apps to extend the priviledged entry points
// without forking cosmwasm-vm.
Sudo(
checksum wasmvm.Checksum,
Expand All @@ -96,7 +96,7 @@ type WasmEngine interface {
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error)
) (*wasmvmtypes.ContractResult, uint64, error)

// GetCode will load the original wasm code for the given checksum.
// This will only succeed if that checksum was previously returned from
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) error {
for _, contract := range gs.Contracts {
_, err := k.storeWasmCode(ctx, contract.CodeBytes, ibcwasm.GetVM().StoreCodeUnchecked)
_, err := k.storeWasmCodeUncheck(ctx, contract.CodeBytes, ibcwasm.GetVM().StoreCodeUnchecked)
if err != nil {
return err
}
Expand Down
58 changes: 55 additions & 3 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"strings"

wasmvm "github.com/CosmWasm/wasmvm"
wasmvm "github.com/CosmWasm/wasmvm/v2"

storetypes "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -94,7 +94,7 @@ func NewKeeperWithConfig(
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize)
vm, err := wasmvm.NewVM(wasmConfig.DataDir, strings.Split(wasmConfig.SupportedCapabilities, ","), types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize)
if err != nil {
panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err))
}
Expand All @@ -107,7 +107,59 @@ func (k Keeper) GetAuthority() string {
return k.authority
}

func (Keeper) storeWasmCode(ctx sdk.Context, code []byte, storeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error)) ([]byte, error) {
func (Keeper) storeWasmCode(ctx sdk.Context, code []byte, storeFn func(code wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)) ([]byte, error) {
var err error
if types.IsGzip(code) {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
code, err = types.Uncompress(code, types.MaxWasmByteSize())
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store contract")
}
}

// run the code through the wasm light client validation process
if err := types.ValidateWasmCode(code); err != nil {
return nil, errorsmod.Wrap(err, "wasm bytecode validation failed")
}

// Check to see if store already has checksum.
checksum, err := types.CreateChecksum(code)
if err != nil {
return nil, errorsmod.Wrap(err, "wasm bytecode checksum failed")
}

if types.HasChecksum(ctx, checksum) {
return nil, types.ErrWasmCodeExists
}

// create the code in the vm
ctx.GasMeter().ConsumeGas(types.VMGasRegister.CompileCosts(len(code)), "Compiling wasm bytecode")
gasLimit := ctx.GasMeter().GasConsumedToLimit()
vmChecksum, _, err := storeFn(code, gasLimit)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store contract")
}

// SANITY: We've checked our store, additional safety check to assert that the checksum returned by WasmVM equals checksum generated by us.
if !bytes.Equal(vmChecksum, checksum) {
return nil, errorsmod.Wrapf(types.ErrInvalidChecksum, "expected %s, got %s", hex.EncodeToString(checksum), hex.EncodeToString(vmChecksum))
}

// pin the code to the vm in-memory cache
if err := ibcwasm.GetVM().Pin(vmChecksum); err != nil {
return nil, errorsmod.Wrapf(err, "failed to pin contract with checksum (%s) to vm cache", hex.EncodeToString(vmChecksum))
}

// store the checksum
err = ibcwasm.Checksums.Set(ctx, checksum)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store checksum")
}

return checksum, nil
}

func (Keeper) storeWasmCodeUncheck(ctx sdk.Context, code []byte, storeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error)) ([]byte, error) {
var err error
if types.IsGzip(code) {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
Expand Down
6 changes: 4 additions & 2 deletions modules/light-clients/08-wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"testing"

wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
wasmvm "github.com/CosmWasm/wasmvm/v2"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
dbm "github.com/cosmos/cosmos-db"
testifysuite "github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -79,6 +79,8 @@ func (suite *KeeperTestSuite) SetupWasmWithMockVM() {

suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))

wasmtesting.AllowWasmClients(suite.chainA)
}

func (suite *KeeperTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[string]json.RawMessage) {
Expand Down
19 changes: 16 additions & 3 deletions modules/light-clients/08-wasm/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"

wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
wasmvm "github.com/CosmWasm/wasmvm/v2"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -65,6 +65,20 @@ func (suite *KeeperTestSuite) TestMsgStoreCode() {
},
errors.New("Wasm bytes do not not start with Wasm magic number"),
},
{
"fails with wasm code too large",
func() {
msg = types.NewMsgStoreCode(signer, append(wasmtesting.WasmMagicNumber, []byte(ibctesting.GenerateString(uint(types.MaxWasmByteSize())))...))
},
types.ErrWasmCodeTooLarge,
},
{
"fails with checksum",
func() {
msg = types.NewMsgStoreCode(signer, []byte{0, 1, 3, 4})
},
errors.New("Wasm bytes do not not start with Wasm magic number"),
},
{
"fails with wasm code too large",
func() {
Expand Down Expand Up @@ -140,7 +154,6 @@ func (suite *KeeperTestSuite) TestMsgMigrateContract() {
suite.Require().NoError(err)

newByteCode := wasmtesting.CreateMockContract([]byte("MockByteCode-TestMsgMigrateContract"))

govAcc := authtypes.NewModuleAddress(govtypes.ModuleName).String()

var (
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
27 changes: 27 additions & 0 deletions modules/light-clients/08-wasm/testing/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mock

import (
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

var _ exported.Path = KeyPath{}

// KeyPath defines a placeholder struct which implements the exported.Path interface
type KeyPath struct{}

// String implements the exported.Path interface
func (KeyPath) String() string {
return ""
}

// Empty implements the exported.Path interface
func (KeyPath) Empty() bool {
return false
}

var _ exported.Height = Height{}

// Height defines a placeholder struct which implements the exported.Height interface
type Height struct {
exported.Height
}
Loading

0 comments on commit f53cabf

Please sign in to comment.