Fix StoreCode gas usage
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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
|
||||
@@ -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")
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user