Skip to content

Commit

Permalink
Merge pull request #1797 from CosmWasm/rename-to-setup-cost
Browse files Browse the repository at this point in the history
Rename InstantiateContractCosts -> SetupContractCost; Remove NewContractInstanceCosts
  • Loading branch information
webmaster128 authored Jan 25, 2024
2 parents 58a5ef0 + ce0dafa commit 796907e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 107 deletions.
20 changes: 10 additions & 10 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ func (k Keeper) instantiate(
return nil, nil, types.ErrEmpty.Wrap("creator")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
instanceCosts := k.gasRegister.NewContractInstanceCosts(k.IsPinnedCode(sdkCtx, codeID), len(initMsg))
sdkCtx.GasMeter().ConsumeGas(instanceCosts, "Loading CosmWasm module: instantiate")
setupCost := k.gasRegister.SetupContractCost(k.IsPinnedCode(sdkCtx, codeID), len(initMsg))
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: instantiate")

codeInfo := k.GetCodeInfo(ctx, codeID)
if codeInfo == nil {
Expand Down Expand Up @@ -382,8 +382,8 @@ func (k Keeper) execute(ctx context.Context, contractAddress, caller sdk.AccAddr
return nil, err
}

executeCosts := k.gasRegister.InstantiateContractCosts(k.IsPinnedCode(ctx, contractInfo.CodeID), len(msg))
sdkCtx.GasMeter().ConsumeGas(executeCosts, "Loading CosmWasm module: execute")
setupCost := k.gasRegister.SetupContractCost(k.IsPinnedCode(ctx, contractInfo.CodeID), len(msg))
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: execute")

// add more funds
if !coins.IsZero() {
Expand Down Expand Up @@ -428,8 +428,8 @@ func (k Keeper) migrate(
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "migrate")

sdkCtx := sdk.UnwrapSDKContext(ctx)
migrateSetupCosts := k.gasRegister.InstantiateContractCosts(k.IsPinnedCode(ctx, newCodeID), len(msg))
sdkCtx.GasMeter().ConsumeGas(migrateSetupCosts, "Loading CosmWasm module: migrate")
setupCost := k.gasRegister.SetupContractCost(k.IsPinnedCode(ctx, newCodeID), len(msg))
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: migrate")

contractInfo := k.GetContractInfo(ctx, contractAddress)
if contractInfo == nil {
Expand Down Expand Up @@ -525,9 +525,9 @@ func (k Keeper) Sudo(ctx context.Context, contractAddress sdk.AccAddress, msg []
return nil, err
}

sudoSetupCosts := k.gasRegister.InstantiateContractCosts(k.IsPinnedCode(ctx, contractInfo.CodeID), len(msg))
setupCost := k.gasRegister.SetupContractCost(k.IsPinnedCode(ctx, contractInfo.CodeID), len(msg))
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.GasMeter().ConsumeGas(sudoSetupCosts, "Loading CosmWasm module: sudo")
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: sudo")

env := types.NewEnv(sdkCtx, contractAddress)

Expand Down Expand Up @@ -747,8 +747,8 @@ func (k Keeper) QuerySmart(ctx context.Context, contractAddr sdk.AccAddress, req
return nil, err
}

smartQuerySetupCosts := k.gasRegister.InstantiateContractCosts(k.IsPinnedCode(sdkCtx, contractInfo.CodeID), len(req))
sdkCtx.GasMeter().ConsumeGas(smartQuerySetupCosts, "Loading CosmWasm module: query")
setupCost := k.gasRegister.SetupContractCost(k.IsPinnedCode(sdkCtx, contractInfo.CodeID), len(req))
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: query")

// prepare querier
querier := k.newQueryHandler(sdkCtx, contractAddr)
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/recurse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {

const (
// Note: about 100 SDK gas (10k CosmWasm gas) for each round of sha256
GasWork2k uint64 = 77_161 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
GasWork2k uint64 = 77_161 // = SetupContractCost + x // we have 6x gas used in cpu than in the instance
// This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 27
)
Expand Down
28 changes: 10 additions & 18 deletions x/wasm/keeper/wasmtesting/gas_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ import (

// MockGasRegister mock that implements keeper.GasRegister
type MockGasRegister struct {
CompileCostFn func(byteLength int) storetypes.Gas
NewContractInstanceCostFn func(pinned bool, msgLen int) storetypes.Gas
InstantiateContractCostFn func(pinned bool, msgLen int) storetypes.Gas
ReplyCostFn func(pinned bool, reply wasmvmtypes.Reply) storetypes.Gas
EventCostsFn func(evts []wasmvmtypes.EventAttribute) storetypes.Gas
ToWasmVMGasFn func(source storetypes.Gas) uint64
FromWasmVMGasFn func(source uint64) storetypes.Gas
UncompressCostsFn func(byteLength int) storetypes.Gas
}

func (m MockGasRegister) NewContractInstanceCosts(pinned bool, msgLen int) storetypes.Gas {
if m.NewContractInstanceCostFn == nil {
panic("not expected to be called")
}
return m.NewContractInstanceCostFn(pinned, msgLen)
CompileCostFn func(byteLength int) storetypes.Gas
SetupContractCostFn func(pinned bool, msgLen int) storetypes.Gas
ReplyCostFn func(pinned bool, reply wasmvmtypes.Reply) storetypes.Gas
EventCostsFn func(evts []wasmvmtypes.EventAttribute) storetypes.Gas
ToWasmVMGasFn func(source storetypes.Gas) uint64
FromWasmVMGasFn func(source uint64) storetypes.Gas
UncompressCostsFn func(byteLength int) storetypes.Gas
}

func (m MockGasRegister) CompileCosts(byteLength int) storetypes.Gas {
Expand All @@ -39,11 +31,11 @@ func (m MockGasRegister) UncompressCosts(byteLength int) storetypes.Gas {
return m.UncompressCostsFn(byteLength)
}

func (m MockGasRegister) InstantiateContractCosts(pinned bool, msgLen int) storetypes.Gas {
if m.InstantiateContractCostFn == nil {
func (m MockGasRegister) SetupContractCost(pinned bool, msgLen int) storetypes.Gas {
if m.SetupContractCostFn == nil {
panic("not expected to be called")
}
return m.InstantiateContractCostFn(pinned, msgLen)
return m.SetupContractCostFn(pinned, msgLen)
}

func (m MockGasRegister) ReplyCosts(pinned bool, reply wasmvmtypes.Reply) storetypes.Gas {
Expand Down
18 changes: 6 additions & 12 deletions x/wasm/types/gas_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ func DefaultPerByteUncompressCost() wasmvmtypes.UFraction {

// GasRegister abstract source for gas costs
type GasRegister interface {
// NewContractInstanceCosts costs to create a new contract instance from code
NewContractInstanceCosts(pinned bool, msgLen int) storetypes.Gas
// 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
// InstantiateContractCosts costs when interacting with a wasm contract
InstantiateContractCosts(pinned bool, msgLen int) storetypes.Gas
// SetupContractCost are charged when interacting with a Wasm contract, i.e. every time
// the contract is prepared for execution through any entry point (execute/instantiate/sudo/query/ibc_*/...).
SetupContractCost(pinned bool, msgLen int) storetypes.Gas
// ReplyCosts costs to to handle a message reply
ReplyCosts(pinned bool, reply wasmvmtypes.Reply) storetypes.Gas
// EventCosts costs to persist an event
Expand Down Expand Up @@ -152,11 +151,6 @@ func NewWasmGasRegister(c WasmGasRegisterConfig) WasmGasRegister {
}
}

// NewContractInstanceCosts costs to create a new contract instance from code
func (g WasmGasRegister) NewContractInstanceCosts(pinned bool, msgLen int) storetypes.Gas {
return g.InstantiateContractCosts(pinned, msgLen)
}

// CompileCosts costs to persist and "compile" a new wasm contract
func (g WasmGasRegister) CompileCosts(byteLength int) storetypes.Gas {
if byteLength < 0 {
Expand All @@ -173,8 +167,8 @@ func (g WasmGasRegister) UncompressCosts(byteLength int) storetypes.Gas {
return g.c.UncompressCost.Mul(uint64(byteLength)).Floor()
}

// InstantiateContractCosts costs when interacting with a wasm contract
func (g WasmGasRegister) InstantiateContractCosts(pinned bool, msgLen int) storetypes.Gas {
// SetupContractCost costs when interacting with a wasm contract
func (g WasmGasRegister) SetupContractCost(pinned bool, msgLen int) storetypes.Gas {
if msgLen < 0 {
panic(errorsmod.Wrap(ErrInvalid, "negative length"))
}
Expand All @@ -199,7 +193,7 @@ func (g WasmGasRegister) ReplyCosts(pinned bool, reply wasmvmtypes.Reply) storet
// apply free tier on the whole set not per event
eventGas += g.EventCosts(attrs, nil)
}
return eventGas + g.InstantiateContractCosts(pinned, msgLen)
return eventGas + g.SetupContractCost(pinned, msgLen)
}

// EventCosts costs to persist an event
Expand Down
69 changes: 3 additions & 66 deletions x/wasm/types/gas_register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,70 +48,7 @@ func TestCompileCosts(t *testing.T) {
}
}

func TestNewContractInstanceCosts(t *testing.T) {
specs := map[string]struct {
srcLen int
srcConfig WasmGasRegisterConfig
pinned bool
exp storetypes.Gas
expPanic bool
}{
"small msg - pinned": {
srcLen: 1,
srcConfig: DefaultGasRegisterConfig(),
pinned: true,
exp: DefaultContractMessageDataCost,
},
"big msg - pinned": {
srcLen: math.MaxUint32,
srcConfig: DefaultGasRegisterConfig(),
pinned: true,
exp: DefaultContractMessageDataCost * storetypes.Gas(math.MaxUint32),
},
"empty msg - pinned": {
srcLen: 0,
pinned: true,
srcConfig: DefaultGasRegisterConfig(),
exp: storetypes.Gas(0),
},
"small msg - unpinned": {
srcLen: 1,
srcConfig: DefaultGasRegisterConfig(),
exp: DefaultContractMessageDataCost + DefaultInstanceCost,
},
"big msg - unpinned": {
srcLen: math.MaxUint32,
srcConfig: DefaultGasRegisterConfig(),
exp: DefaultContractMessageDataCost*math.MaxUint32 + DefaultInstanceCost,
},
"empty msg - unpinned": {
srcLen: 0,
srcConfig: DefaultGasRegisterConfig(),
exp: DefaultInstanceCost,
},

"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).NewContractInstanceCosts(spec.pinned, spec.srcLen)
})
return
}
gotGas := NewWasmGasRegister(spec.srcConfig).NewContractInstanceCosts(spec.pinned, spec.srcLen)
assert.Equal(t, spec.exp, gotGas)
})
}
}

func TestContractInstanceCosts(t *testing.T) {
// same as TestNewContractInstanceCosts currently
func TestSetupContractCost(t *testing.T) {
specs := map[string]struct {
srcLen int
srcConfig WasmGasRegisterConfig
Expand Down Expand Up @@ -163,11 +100,11 @@ func TestContractInstanceCosts(t *testing.T) {
t.Run(name, func(t *testing.T) {
if spec.expPanic {
assert.Panics(t, func() {
NewWasmGasRegister(spec.srcConfig).InstantiateContractCosts(spec.pinned, spec.srcLen)
NewWasmGasRegister(spec.srcConfig).SetupContractCost(spec.pinned, spec.srcLen)
})
return
}
gotGas := NewWasmGasRegister(spec.srcConfig).InstantiateContractCosts(spec.pinned, spec.srcLen)
gotGas := NewWasmGasRegister(spec.srcConfig).SetupContractCost(spec.pinned, spec.srcLen)
assert.Equal(t, spec.exp, gotGas)
})
}
Expand Down

0 comments on commit 796907e

Please sign in to comment.