Skip to content

Commit

Permalink
Fix StoreCode gas usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Feb 7, 2024
1 parent b078794 commit b31e7c4
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 60 deletions.
5 changes: 2 additions & 3 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ func (k Keeper) create(ctx context.Context, creator sdk.AccAddress, wasmCode []b
}
}

sdkCtx.GasMeter().ConsumeGas(k.gasRegister.CompileCosts(len(wasmCode)), "Compiling wasm bytecode")
gas := k.runtimeGasForContract(sdkCtx)
checksum, gasUsed, err := k.wasmVM.StoreCode(wasmCode, gas)
gasLeft := k.runtimeGasForContract(sdkCtx)
checksum, gasUsed, err := k.wasmVM.StoreCode(wasmCode, gasLeft)
k.consumeRuntimeGas(sdkCtx, gasUsed)
if err != nil {
return 0, checksum, errorsmod.Wrap(types.ErrCreateFailed, err.Error())
Expand Down
8 changes: 0 additions & 8 deletions x/wasm/keeper/wasmtesting/gas_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

// MockGasRegister mock that implements keeper.GasRegister
type MockGasRegister struct {
CompileCostFn func(byteLength int) storetypes.Gas
SetupContractCostFn func(discount bool, msgLen int) storetypes.Gas
ReplyCostFn func(discount bool, reply wasmvmtypes.Reply) storetypes.Gas
EventCostsFn func(evts []wasmvmtypes.EventAttribute) storetypes.Gas
Expand All @@ -17,13 +16,6 @@ type MockGasRegister struct {
UncompressCostsFn func(byteLength int) storetypes.Gas
}

func (m MockGasRegister) CompileCosts(byteLength int) storetypes.Gas {
if m.CompileCostFn == nil {
panic("not expected to be called")
}
return m.CompileCostFn(byteLength)
}

func (m MockGasRegister) UncompressCosts(byteLength int) storetypes.Gas {
if m.UncompressCostsFn == nil {
panic("not expected to be called")
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/wasmtesting/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ func NoOpInstantiateFn(wasmvm.Checksum, wasmvmtypes.Env, wasmvmtypes.MessageInfo
return &wasmvmtypes.ContractResult{Ok: &wasmvmtypes.Response{}}, 0, nil
}

func NoOpStoreCodeFn(_ wasmvm.WasmCode) (wasmvm.Checksum, error) {
return rand.Bytes(32), nil
func NoOpStoreCodeFn(wasm wasmvm.WasmCode, gasLimit uint64) (wasmvm.Checksum, uint64, error) {
return rand.Bytes(32), uint64(MockStoreCodeCostPerByte * len(wasm)), nil
}

func HasIBCAnalyzeFn(wasmvm.Checksum) (*wasmvmtypes.AnalysisReport, error) {
Expand Down
10 changes: 0 additions & 10 deletions x/wasm/types/gas_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func DefaultPerByteUncompressCost() wasmvmtypes.UFraction {

// GasRegister abstract source for gas costs
type GasRegister interface {
// CompileCosts costs to persist and "compile" a new wasm contract
CompileCosts(byteLength int) storetypes.Gas
// UncompressCosts costs to unpack a new wasm contract
UncompressCosts(byteLength int) storetypes.Gas
// SetupContractCost are charged when interacting with a Wasm contract, i.e. every time
Expand Down Expand Up @@ -165,14 +163,6 @@ func NewWasmGasRegister(c WasmGasRegisterConfig) WasmGasRegister {
}
}

// CompileCosts costs to persist and "compile" a new wasm contract
func (g WasmGasRegister) CompileCosts(byteLength int) storetypes.Gas {
if byteLength < 0 {
panic(errorsmod.Wrap(ErrInvalid, "negative length"))
}
return g.c.CompileCost * uint64(byteLength)
}

// UncompressCosts costs to unpack a new wasm contract
func (g WasmGasRegister) UncompressCosts(byteLength int) storetypes.Gas {
if byteLength < 0 {
Expand Down
37 changes: 0 additions & 37 deletions x/wasm/types/gas_register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,6 @@ import (
storetypes "cosmossdk.io/store/types"
)

func TestCompileCosts(t *testing.T) {
specs := map[string]struct {
srcLen int
srcConfig WasmGasRegisterConfig
exp storetypes.Gas
expPanic bool
}{
"one byte": {
srcLen: 1,
srcConfig: DefaultGasRegisterConfig(),
exp: storetypes.Gas(3), // DefaultCompileCost
},
"zero byte": {
srcLen: 0,
srcConfig: DefaultGasRegisterConfig(),
exp: storetypes.Gas(0),
},
"negative len": {
srcLen: -1,
srcConfig: DefaultGasRegisterConfig(),
expPanic: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
if spec.expPanic {
assert.Panics(t, func() {
NewWasmGasRegister(spec.srcConfig).CompileCosts(spec.srcLen)
})
return
}
gotGas := NewWasmGasRegister(spec.srcConfig).CompileCosts(spec.srcLen)
assert.Equal(t, spec.exp, gotGas)
})
}
}

func TestSetupContractCost(t *testing.T) {
specs := map[string]struct {
srcLen int
Expand Down

0 comments on commit b31e7c4

Please sign in to comment.