Skip to content

Commit b31e7c4

Browse files
committed
Fix StoreCode gas usage
1 parent b078794 commit b31e7c4

File tree

5 files changed

+4
-60
lines changed

5 files changed

+4
-60
lines changed

x/wasm/keeper/keeper.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ func (k Keeper) create(ctx context.Context, creator sdk.AccAddress, wasmCode []b
168168
}
169169
}
170170

171-
sdkCtx.GasMeter().ConsumeGas(k.gasRegister.CompileCosts(len(wasmCode)), "Compiling wasm bytecode")
172-
gas := k.runtimeGasForContract(sdkCtx)
173-
checksum, gasUsed, err := k.wasmVM.StoreCode(wasmCode, gas)
171+
gasLeft := k.runtimeGasForContract(sdkCtx)
172+
checksum, gasUsed, err := k.wasmVM.StoreCode(wasmCode, gasLeft)
174173
k.consumeRuntimeGas(sdkCtx, gasUsed)
175174
if err != nil {
176175
return 0, checksum, errorsmod.Wrap(types.ErrCreateFailed, err.Error())

x/wasm/keeper/wasmtesting/gas_register.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
// MockGasRegister mock that implements keeper.GasRegister
1010
type MockGasRegister struct {
11-
CompileCostFn func(byteLength int) storetypes.Gas
1211
SetupContractCostFn func(discount bool, msgLen int) storetypes.Gas
1312
ReplyCostFn func(discount bool, reply wasmvmtypes.Reply) storetypes.Gas
1413
EventCostsFn func(evts []wasmvmtypes.EventAttribute) storetypes.Gas
@@ -17,13 +16,6 @@ type MockGasRegister struct {
1716
UncompressCostsFn func(byteLength int) storetypes.Gas
1817
}
1918

20-
func (m MockGasRegister) CompileCosts(byteLength int) storetypes.Gas {
21-
if m.CompileCostFn == nil {
22-
panic("not expected to be called")
23-
}
24-
return m.CompileCostFn(byteLength)
25-
}
26-
2719
func (m MockGasRegister) UncompressCosts(byteLength int) storetypes.Gas {
2820
if m.UncompressCostsFn == nil {
2921
panic("not expected to be called")

x/wasm/keeper/wasmtesting/mock_engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ func NoOpInstantiateFn(wasmvm.Checksum, wasmvmtypes.Env, wasmvmtypes.MessageInfo
348348
return &wasmvmtypes.ContractResult{Ok: &wasmvmtypes.Response{}}, 0, nil
349349
}
350350

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

355355
func HasIBCAnalyzeFn(wasmvm.Checksum) (*wasmvmtypes.AnalysisReport, error) {

x/wasm/types/gas_register.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ func DefaultPerByteUncompressCost() wasmvmtypes.UFraction {
7474

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

168-
// CompileCosts costs to persist and "compile" a new wasm contract
169-
func (g WasmGasRegister) CompileCosts(byteLength int) storetypes.Gas {
170-
if byteLength < 0 {
171-
panic(errorsmod.Wrap(ErrInvalid, "negative length"))
172-
}
173-
return g.c.CompileCost * uint64(byteLength)
174-
}
175-
176166
// UncompressCosts costs to unpack a new wasm contract
177167
func (g WasmGasRegister) UncompressCosts(byteLength int) storetypes.Gas {
178168
if byteLength < 0 {

x/wasm/types/gas_register_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,6 @@ import (
1111
storetypes "cosmossdk.io/store/types"
1212
)
1313

14-
func TestCompileCosts(t *testing.T) {
15-
specs := map[string]struct {
16-
srcLen int
17-
srcConfig WasmGasRegisterConfig
18-
exp storetypes.Gas
19-
expPanic bool
20-
}{
21-
"one byte": {
22-
srcLen: 1,
23-
srcConfig: DefaultGasRegisterConfig(),
24-
exp: storetypes.Gas(3), // DefaultCompileCost
25-
},
26-
"zero byte": {
27-
srcLen: 0,
28-
srcConfig: DefaultGasRegisterConfig(),
29-
exp: storetypes.Gas(0),
30-
},
31-
"negative len": {
32-
srcLen: -1,
33-
srcConfig: DefaultGasRegisterConfig(),
34-
expPanic: true,
35-
},
36-
}
37-
for name, spec := range specs {
38-
t.Run(name, func(t *testing.T) {
39-
if spec.expPanic {
40-
assert.Panics(t, func() {
41-
NewWasmGasRegister(spec.srcConfig).CompileCosts(spec.srcLen)
42-
})
43-
return
44-
}
45-
gotGas := NewWasmGasRegister(spec.srcConfig).CompileCosts(spec.srcLen)
46-
assert.Equal(t, spec.exp, gotGas)
47-
})
48-
}
49-
}
50-
5114
func TestSetupContractCost(t *testing.T) {
5215
specs := map[string]struct {
5316
srcLen int

0 commit comments

Comments
 (0)