Skip to content

Commit

Permalink
migration complete
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Jan 24, 2025
1 parent 2d759ef commit 15f51bd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
38 changes: 19 additions & 19 deletions modules/light-clients/08-wasm/keeper/contract_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ var (
)

// instantiateContract calls vm.Instantiate with appropriate arguments.
func (k Keeper) instantiateContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) {
func (k Keeper) instantiateContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) {
sdkGasMeter := k.GasService.GasMeter(ctx)
multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, types.VMGasRegister)
gasLimit := VMGasRegister.RuntimeGasForContract(ctx)
gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter)

env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID)

Expand All @@ -48,53 +48,53 @@ func (k Keeper) instantiateContract(ctx sdk.Context, clientID string, clientStor
Funds: nil,
}

ctx.GasMeter().ConsumeGas(types.VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: instantiate")
sdkGasMeter.Consume(types.VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: instantiate")

Check failure on line 51 in modules/light-clients/08-wasm/keeper/contract_keeper.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sdkGasMeter.Consume` is not checked (errcheck)

resp, gasUsed, err := k.GetVM().Instantiate(checksum, env, msgInfo, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization)
types.VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed)
types.VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed)
return resp, err
}

// callContract calls vm.Sudo with internally constructed gas meter and environment.
func (k Keeper) callContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) {
sdkGasMeter := ctx.GasMeter()
func (k Keeper) callContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) {
sdkGasMeter := k.GasService.GasMeter(ctx)
multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister)
gasLimit := VMGasRegister.RuntimeGasForContract(ctx)
gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter)

env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID)

ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo")
sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: sudo")

Check failure on line 66 in modules/light-clients/08-wasm/keeper/contract_keeper.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sdkGasMeter.Consume` is not checked (errcheck)
resp, gasUsed, err := k.GetVM().Sudo(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization)
VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed)
VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed)
return resp, err
}

// queryContract calls vm.Query.
func (k Keeper) queryContract(ctx sdk.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.QueryResult, error) {
sdkGasMeter := ctx.GasMeter()
func (k Keeper) queryContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.QueryResult, error) {
sdkGasMeter := k.GasService.GasMeter(ctx)
multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister)
gasLimit := VMGasRegister.RuntimeGasForContract(ctx)
gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter)

env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID)

ctx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query")
sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: query")

Check failure on line 80 in modules/light-clients/08-wasm/keeper/contract_keeper.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sdkGasMeter.Consume` is not checked (errcheck)
resp, gasUsed, err := k.GetVM().Query(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization)
VMGasRegister.ConsumeRuntimeGas(ctx, gasUsed)
VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed)

return resp, err
}

// migrateContract calls vm.Migrate with internally constructed gas meter and environment.
func (k Keeper) migrateContract(ctx context.Context, clientID string, clientStore storetypes.KVStore, checksum types.Checksum, msg []byte) (*wasmvmtypes.ContractResult, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkGasMeter := sdkCtx.GasMeter()
sdkGasMeter := k.GasService.GasMeter(ctx)
multipliedGasMeter := types.NewMultipliedGasMeter(sdkGasMeter, VMGasRegister)
gasLimit := VMGasRegister.RuntimeGasForContract(sdkCtx)
gasLimit := VMGasRegister.RuntimeGasForContract(sdkGasMeter)

env := getEnv(k.HeaderService.HeaderInfo(ctx), clientID)

sdkCtx.GasMeter().ConsumeGas(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate")
sdkGasMeter.Consume(VMGasRegister.SetupContractCost(true, len(msg)), "Loading CosmWasm module: migrate")

Check failure on line 95 in modules/light-clients/08-wasm/keeper/contract_keeper.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sdkGasMeter.Consume` is not checked (errcheck)
resp, gasUsed, err := k.GetVM().Migrate(checksum, env, msg, internaltypes.NewStoreAdapter(clientStore), wasmvmAPI, k.newQueryHandler(ctx, clientID), multipliedGasMeter, gasLimit, types.CostJSONDeserialization)
VMGasRegister.ConsumeRuntimeGas(sdkCtx, gasUsed)
VMGasRegister.ConsumeRuntimeGas(sdkGasMeter, gasUsed)

return resp, err
}
Expand Down
10 changes: 4 additions & 6 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
errorsmod "cosmossdk.io/errors"

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

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
)
Expand Down Expand Up @@ -82,10 +80,10 @@ func (k Keeper) newQueryHandler(ctx context.Context, callerID string) *queryHand
// - Size bounds are checked. Contract length must not be 0 or exceed a specific size (maxWasmSize).
// - The contract must not have already been stored in store.
func (k Keeper) storeWasmCode(ctx context.Context, code []byte, storeFn func(code wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error)) ([]byte, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
meter := k.GasService.GasMeter(ctx)
var err error
if types.IsGzip(code) {
sdkCtx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
meter.Consume(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")

Check failure on line 86 in modules/light-clients/08-wasm/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `meter.Consume` is not checked (errcheck)
code, err = types.Uncompress(code, types.MaxWasmSize)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store contract")
Expand All @@ -108,9 +106,9 @@ func (k Keeper) storeWasmCode(ctx context.Context, code []byte, storeFn func(cod
}

// create the code in the vm
gasLeft := types.VMGasRegister.RuntimeGasForContract(sdkCtx)
gasLeft := types.VMGasRegister.RuntimeGasForContract(meter)
vmChecksum, gasUsed, err := storeFn(code, gasLeft)
types.VMGasRegister.ConsumeRuntimeGas(sdkCtx, gasUsed)
types.VMGasRegister.ConsumeRuntimeGas(meter, gasUsed)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store contract")
}
Expand Down
4 changes: 2 additions & 2 deletions modules/light-clients/08-wasm/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m Migrator) MigrateChecksums(goCtx context.Context) error {
}

// getStoredChecksums returns the checksums stored under the KeyChecksums key.
func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) {
func (m Migrator) getStoredChecksums(ctx context.Context) ([][]byte, error) {
store := m.keeper.KVStoreService.OpenKVStore(ctx)

bz, err := store.Get([]byte(types.KeyChecksums))
Expand All @@ -67,7 +67,7 @@ func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) {
}

// deleteChecksums deletes the checksums stored under the KeyChecksums key.
func (m Migrator) deleteChecksums(ctx sdk.Context) error {
func (m Migrator) deleteChecksums(ctx context.Context) error {
store := m.keeper.KVStoreService.OpenKVStore(ctx)
err := store.Delete([]byte(types.KeyChecksums))

Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (ws *WasmSnapshotter) RestoreExtension(height uint64, format uint32, payloa
return errorsmod.Wrapf(snapshot.ErrUnknownFormat, "expected %d, got %d", ws.SnapshotFormat(), format)
}

func restoreV1(ctx sdk.Context, k *Keeper, compressedCode []byte) error {
func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error {
if !types.IsGzip(compressedCode) {
return errorsmod.Wrap(types.ErrInvalidData, "expected wasm code is not gzip format")
}
Expand Down
20 changes: 13 additions & 7 deletions modules/light-clients/08-wasm/types/gas_register_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"cosmossdk.io/core/gas"
storetypes "cosmossdk.io/store/types"

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

// While gas_register.go is a direct copy of https://github.com/CosmWasm/wasmd/blob/main/x/wasm/types/gas_register.go
Expand All @@ -33,17 +31,25 @@ func (g WasmGasRegister) RuntimeGasForContract(meter gas.Meter) uint64 {
if meter.Limit() == 0 || meter.Limit() == math.MaxUint64 {
return math.MaxUint64
}
sdk.Context{}.GasMeter().GasConsumedToLimit()
var consumedToLimit
return g.ToWasmVMGas(meter.Limit() - meter.GasConsumedToLimit())
var consumedToLimit gas.Gas
if meter.Remaining() <= meter.Limit() {
consumedToLimit = meter.Limit()
} else {
consumedToLimit = meter.Consumed()
}
return g.ToWasmVMGas(meter.Limit() - consumedToLimit)
}

func (g WasmGasRegister) ConsumeRuntimeGas(meter gas.Meter, gas uint64) {
consumed := g.FromWasmVMGas(gas)
meter.Consume(consumed, "wasm contract")

Check failure on line 45 in modules/light-clients/08-wasm/types/gas_register_custom.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `meter.Consume` is not checked (errcheck)

// throw OutOfGas error if we ran out (got exactly to zero due to better limit enforcing)
if meter.Remaining() >= meter.Limit() {
// TODO(technicallyty): this used to be a meter.IsOutOfGas check, which has since been removed from the gas meter iface.
// We use the InfiniteGasMeter in tests, which would ALWAYS return false to IsOutOfGas. Now that we don't have that method,
// we have to include this weird hack that essentially checks if we're out of gas and we're not the infinite gas meter.
// Please don't replicate this if you can. It is ugly.
if meter.Remaining() >= meter.Limit() && (meter.Limit() != math.MaxUint64 && meter.Remaining() != math.MaxUint64) {
// throw OutOfGas error if we ran out (got exactly to zero due to better limit enforcing)
panic(storetypes.ErrorOutOfGas{Descriptor: "Wasmer function execution"})
}
}
Expand Down

0 comments on commit 15f51bd

Please sign in to comment.