Skip to content

Commit 0dbcef0

Browse files
authored
Merge pull request cosmos#567 from CosmWasm/553-adjust-default-msg-gas
Adjust default msg gas
2 parents 267643f + 1032853 commit 0dbcef0

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

x/wasm/keeper/gas_register.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ const (
2525
// This is used with len(key) + len(value)
2626
DefaultEventAttributeDataCost uint64 = 1
2727
// DefaultContractMessageDataCost is how much SDK gas is charged *per byte* of the message that goes to the contract
28-
// This is used with len(msg)
29-
DefaultContractMessageDataCost uint64 = 1
28+
// This is used with len(msg). Note that the message is deserialized in the receiving contract and this is charged
29+
// with wasm gas already. The derserialization of results is also charged in wasmvm. I am unsure if we need to add
30+
// additional costs here.
31+
// Note: also used for error fields on reply, and data on reply. Maybe these should be pulled out to a different (non-zero) field
32+
DefaultContractMessageDataCost uint64 = 0
3033
// DefaultPerAttributeCost is how much SDK gas we charge per attribute count.
3134
DefaultPerAttributeCost uint64 = 10
3235
// DefaultPerCustomEventCost is how much SDK gas we charge per event count.
@@ -144,7 +147,7 @@ func (g WasmGasRegister) ReplyCosts(pinned bool, reply wasmvmtypes.Reply) sdk.Ga
144147
msgLen += len(reply.Result.Ok.Data)
145148
var attrs []wasmvmtypes.EventAttribute
146149
for _, e := range reply.Result.Ok.Events {
147-
msgLen += len(e.Type)
150+
eventGas += sdk.Gas(len(e.Type)) * g.c.EventAttributeDataCost
148151
attrs = append(e.Attributes)
149152
}
150153
// apply free tier on the whole set not per event

x/wasm/keeper/gas_register_test.go

+18-20
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ func TestNewContractInstanceCosts(t *testing.T) {
5959
srcLen: 1,
6060
srcConfig: DefaultGasRegisterConfig(),
6161
pinned: true,
62-
exp: sdk.Gas(1),
62+
exp: DefaultContractMessageDataCost,
6363
},
6464
"big msg - pinned": {
6565
srcLen: math.MaxUint32,
6666
srcConfig: DefaultGasRegisterConfig(),
6767
pinned: true,
68-
exp: sdk.Gas(math.MaxUint32),
68+
exp: DefaultContractMessageDataCost * sdk.Gas(math.MaxUint32),
6969
},
7070
"empty msg - pinned": {
7171
srcLen: 0,
@@ -76,18 +76,17 @@ func TestNewContractInstanceCosts(t *testing.T) {
7676
"small msg - unpinned": {
7777
srcLen: 1,
7878
srcConfig: DefaultGasRegisterConfig(),
79-
pinned: true,
80-
exp: sdk.Gas(1),
79+
exp: DefaultContractMessageDataCost + DefaultInstanceCost,
8180
},
8281
"big msg - unpinned": {
8382
srcLen: math.MaxUint32,
8483
srcConfig: DefaultGasRegisterConfig(),
85-
exp: sdk.Gas(math.MaxUint32 + 40_000),
84+
exp: sdk.Gas(DefaultContractMessageDataCost*math.MaxUint32 + DefaultInstanceCost),
8685
},
8786
"empty msg - unpinned": {
8887
srcLen: 0,
8988
srcConfig: DefaultGasRegisterConfig(),
90-
exp: sdk.Gas(40_000),
89+
exp: sdk.Gas(DefaultInstanceCost),
9190
},
9291

9392
"negative len": {
@@ -123,13 +122,13 @@ func TestContractInstanceCosts(t *testing.T) {
123122
srcLen: 1,
124123
srcConfig: DefaultGasRegisterConfig(),
125124
pinned: true,
126-
exp: sdk.Gas(1),
125+
exp: DefaultContractMessageDataCost,
127126
},
128127
"big msg - pinned": {
129128
srcLen: math.MaxUint32,
130129
srcConfig: DefaultGasRegisterConfig(),
131130
pinned: true,
132-
exp: sdk.Gas(math.MaxUint32),
131+
exp: sdk.Gas(DefaultContractMessageDataCost * math.MaxUint32),
133132
},
134133
"empty msg - pinned": {
135134
srcLen: 0,
@@ -140,18 +139,17 @@ func TestContractInstanceCosts(t *testing.T) {
140139
"small msg - unpinned": {
141140
srcLen: 1,
142141
srcConfig: DefaultGasRegisterConfig(),
143-
pinned: true,
144-
exp: sdk.Gas(1),
142+
exp: DefaultContractMessageDataCost + DefaultInstanceCost,
145143
},
146144
"big msg - unpinned": {
147145
srcLen: math.MaxUint32,
148146
srcConfig: DefaultGasRegisterConfig(),
149-
exp: sdk.Gas(math.MaxUint32 + 40_000),
147+
exp: sdk.Gas(DefaultContractMessageDataCost*math.MaxUint32 + DefaultInstanceCost),
150148
},
151149
"empty msg - unpinned": {
152150
srcLen: 0,
153151
srcConfig: DefaultGasRegisterConfig(),
154-
exp: sdk.Gas(40_000),
152+
exp: sdk.Gas(DefaultInstanceCost),
155153
},
156154

157155
"negative len": {
@@ -195,7 +193,7 @@ func TestReplyCost(t *testing.T) {
195193
},
196194
srcConfig: DefaultGasRegisterConfig(),
197195
pinned: true,
198-
exp: sdk.Gas(3 + 10 + 1), // len("foo") + 1 * DefaultPerAttributeCost + len(data)
196+
exp: sdk.Gas(3*DefaultEventAttributeDataCost + DefaultPerAttributeCost + DefaultContractMessageDataCost), // 3 == len("foo")
199197
},
200198
"subcall response with events - pinned": {
201199
src: wasmvmtypes.Reply{
@@ -209,7 +207,7 @@ func TestReplyCost(t *testing.T) {
209207
},
210208
srcConfig: DefaultGasRegisterConfig(),
211209
pinned: true,
212-
exp: sdk.Gas(3 + 10), // len("foo") + 1 * DefaultPerAttributeCost
210+
exp: sdk.Gas(3*DefaultEventAttributeDataCost + DefaultPerAttributeCost), // 3 == len("foo")
213211
},
214212
"subcall response with events exceeds free tier- pinned": {
215213
src: wasmvmtypes.Reply{
@@ -223,7 +221,7 @@ func TestReplyCost(t *testing.T) {
223221
},
224222
srcConfig: DefaultGasRegisterConfig(),
225223
pinned: true,
226-
exp: sdk.Gas(3 + 10 + 6), // len("foo") + 1 * DefaultPerAttributeCost + len("myData")
224+
exp: sdk.Gas((3+6)*DefaultEventAttributeDataCost + DefaultPerAttributeCost), // 3 == len("foo"), 6 == len("myData")
227225
},
228226
"subcall response error - pinned": {
229227
src: wasmvmtypes.Reply{
@@ -233,7 +231,7 @@ func TestReplyCost(t *testing.T) {
233231
},
234232
srcConfig: DefaultGasRegisterConfig(),
235233
pinned: true,
236-
exp: sdk.Gas(3), // len("foo")
234+
exp: 3 * DefaultContractMessageDataCost,
237235
},
238236
"subcall response with events and data - unpinned": {
239237
src: wasmvmtypes.Reply{
@@ -247,7 +245,7 @@ func TestReplyCost(t *testing.T) {
247245
},
248246
},
249247
srcConfig: DefaultGasRegisterConfig(),
250-
exp: sdk.Gas(40_000 + 3 + 10 + 1), // DefaultInstanceCost len("foo") + 1 * DefaultPerAttributeCost + len(data)
248+
exp: sdk.Gas(DefaultInstanceCost + 3*DefaultEventAttributeDataCost + DefaultPerAttributeCost + DefaultContractMessageDataCost),
251249
},
252250
"subcall response with events - unpinned": {
253251
src: wasmvmtypes.Reply{
@@ -260,7 +258,7 @@ func TestReplyCost(t *testing.T) {
260258
},
261259
},
262260
srcConfig: DefaultGasRegisterConfig(),
263-
exp: sdk.Gas(40_000 + 3 + 10), // DefaultInstanceCost + len("foo") + 1 * DefaultPerAttributeCost
261+
exp: sdk.Gas(DefaultInstanceCost + 3*DefaultEventAttributeDataCost + DefaultPerAttributeCost),
264262
},
265263
"subcall response with events exceeds free tier- unpinned": {
266264
src: wasmvmtypes.Reply{
@@ -273,7 +271,7 @@ func TestReplyCost(t *testing.T) {
273271
},
274272
},
275273
srcConfig: DefaultGasRegisterConfig(),
276-
exp: sdk.Gas(40_000 + 3 + 10 + 6), // DefaultInstanceCost + len("foo") + 1 * DefaultPerAttributeCost + len("myData")
274+
exp: sdk.Gas(DefaultInstanceCost + (3+6)*DefaultEventAttributeDataCost + DefaultPerAttributeCost), // 3 == len("foo"), 6 == len("myData")
277275
},
278276
"subcall response error - unpinned": {
279277
src: wasmvmtypes.Reply{
@@ -282,7 +280,7 @@ func TestReplyCost(t *testing.T) {
282280
},
283281
},
284282
srcConfig: DefaultGasRegisterConfig(),
285-
exp: sdk.Gas(40_000 + 3), // DefaultInstanceCost + len("foo")
283+
exp: sdk.Gas(DefaultInstanceCost + 3*DefaultContractMessageDataCost),
286284
},
287285
}
288286
for name, spec := range specs {

x/wasm/keeper/keeper_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func TestInstantiate(t *testing.T) {
284284

285285
gasAfter := ctx.GasMeter().GasConsumed()
286286
if types.EnableGasVerification {
287-
require.Equal(t, uint64(0x12280), gasAfter-gasBefore)
287+
require.Equal(t, uint64(0x12206), gasAfter-gasBefore)
288288
}
289289

290290
// ensure it is stored properly
@@ -517,7 +517,7 @@ func TestExecute(t *testing.T) {
517517
// make sure gas is properly deducted from ctx
518518
gasAfter := ctx.GasMeter().GasConsumed()
519519
if types.EnableGasVerification {
520-
require.Equal(t, uint64(0x12afd), gasAfter-gasBefore)
520+
require.Equal(t, uint64(0x12aef), gasAfter-gasBefore)
521521
}
522522
// ensure bob now exists and got both payments released
523523
bobAcct = accKeeper.GetAccount(ctx, bob)

x/wasm/keeper/recurse_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ func initRecurseContract(t *testing.T) (contract sdk.AccAddress, creator sdk.Acc
5757

5858
func TestGasCostOnQuery(t *testing.T) {
5959
const (
60-
GasNoWork uint64 = 44_240
60+
GasNoWork uint64 = 44_149
6161
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
62-
GasWork50 uint64 = 49_901 // this is a little shy of 50k gas - to keep an eye on the limit
62+
GasWork50 uint64 = 49_809 // this is a little shy of 50k gas - to keep an eye on the limit
6363

64-
GasReturnUnhashed uint64 = 197
65-
GasReturnHashed uint64 = 173
64+
GasReturnUnhashed uint64 = 256
65+
GasReturnHashed uint64 = 232
6666
)
6767

6868
cases := map[string]struct {
@@ -221,9 +221,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
221221

222222
const (
223223
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
224-
GasWork2k uint64 = 273_170 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
224+
GasWork2k uint64 = 273_076 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
225225
// This is overhead for calling into a sub-contract
226-
GasReturnHashed uint64 = 177
226+
GasReturnHashed uint64 = 236
227227
)
228228

229229
cases := map[string]struct {

x/wasm/keeper/relay_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func TestOnRecvPacket(t *testing.T) {
411411
},
412412
"submessage reply can overwrite ack data": {
413413
contractAddr: example.Contract,
414-
expContractGas: myContractGas + 10 + DefaultInstanceCost + 3708,
414+
expContractGas: myContractGas + 10 + DefaultInstanceCost + 3707,
415415
contractResp: &wasmvmtypes.IBCReceiveResponse{
416416
Acknowledgement: []byte("myAck"),
417417
Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyAlways, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}},

0 commit comments

Comments
 (0)