/
gas_table.go
489 lines (426 loc) · 14.9 KB
/
gas_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gas
import (
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/mm"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/model"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/params"
)
// 本文件中定义各种操作中需要花费的Gas逻辑
type (
// CalcGasFunc 计算Gas的方法定义
CalcGasFunc func(Table, *params.EVMParam, *params.GasParam, *mm.Stack, *mm.Memory, uint64) (uint64, error) // last parameter is the requested memory size as a uint64
)
// Table 此文件中定义各种指令操作花费的Gas计算
// Gas定价表结构
type Table struct {
// ExtcodeSize 扩展代码大小计价
ExtcodeSize uint64
// ExtcodeCopy 代码复制价格
ExtcodeCopy uint64
// Balance 账户计价
Balance uint64
// SLoad 加载数据计价
SLoad uint64
// Calls 调用方法计价
Calls uint64
// Suicide 自杀计价
Suicide uint64
// ExpByte 额外数据计价
ExpByte uint64
}
var (
// TableHomestead 定义各种操作的Gas定价
TableHomestead = Table{
ExtcodeSize: 20,
ExtcodeCopy: 20,
Balance: 20,
SLoad: 50,
Calls: 40,
Suicide: 0,
ExpByte: 10,
}
)
// 计算新开辟内存空间需要使用多少Gas
func memoryGasCost(mem *mm.Memory, newMemSize uint64) (uint64, error) {
if newMemSize == 0 {
return 0, nil
}
// 如果超过最大值,则溢出
if newMemSize > MaxNewMemSize {
return 0, model.ErrGasUintOverflow
}
newMemSizeWords := common.ToWordSize(newMemSize)
// 这里之所以要再算一遍,是因为内存开辟是按字长,
// 第一次计算出来的自己长度不一定是字长的整数倍
newMemSize = newMemSizeWords * 32
if newMemSize > uint64(mem.Len()) {
square := newMemSizeWords * newMemSizeWords
linCoef := newMemSizeWords * params.MemoryGas
quadCoef := square / params.QuadCoeffDiv
newTotalFee := linCoef + quadCoef
// 本次逻辑只返回新增的内存空间需要的Gas,所以需要减去上次已经花费的Gas
fee := newTotalFee - mem.LastGasCost
mem.LastGasCost = newTotalFee
return fee, nil
}
return 0, nil
}
// ConstGasFunc Gas计算逻辑封装,返回固定值的Gas计算都可以使用此方法
func ConstGasFunc(gas uint64) CalcGasFunc {
return func(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return gas, nil
}
}
// CallDataCopy 计算数据复制需要花费的Gas
func CallDataCopy(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, GasFastestStep); overflow {
return 0, model.ErrGasUintOverflow
}
words, overflow := stack.Back(2).Uint64WithOverflow()
if overflow {
return 0, model.ErrGasUintOverflow
}
if words, overflow = common.SafeMul(common.ToWordSize(words), params.CopyGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, words); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// ReturnDataCopy 计算数据复制的价格
func ReturnDataCopy(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, GasFastestStep); overflow {
return 0, model.ErrGasUintOverflow
}
words, overflow := stack.Back(2).Uint64WithOverflow()
if overflow {
return 0, model.ErrGasUintOverflow
}
if words, overflow = common.SafeMul(common.ToWordSize(words), params.CopyGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, words); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// SStore 计算数据存储的价格
func SStore(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var (
y, x = stack.Back(1), stack.Back(0)
val = evm.StateDB.GetState(contractGas.Address.String(), common.Uint256ToHash(x))
)
// 三种场景消耗的Gas是不一样的
if val == (common.Hash{}) && y.Sign() != 0 {
// 从零值地址到非零值地址存储, 赋值的情况
// 0 => non 0
return params.SstoreSetGas, nil
} else if val != (common.Hash{}) && y.Sign() == 0 {
// 从非零值地址到零值地址存储, 删除值的情况
// non 0 => 0
evm.StateDB.AddRefund(params.SstoreRefundGas)
return params.SstoreClearGas, nil
} else {
// 从非零值地址到非零值地址存储, 变更值的情况
// non 0 => non 0
return params.SstoreResetGas, nil
}
}
// MakeGasLog 生成Gas计算方法
func MakeGasLog(n uint64) CalcGasFunc {
return func(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
requestedSize, overflow := stack.Back(1).Uint64WithOverflow()
if overflow {
return 0, model.ErrGasUintOverflow
}
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, params.LogGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, n*params.LogTopicGas); overflow {
return 0, model.ErrGasUintOverflow
}
var memorySizeGas uint64
if memorySizeGas, overflow = common.SafeMul(requestedSize, params.LogDataGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, memorySizeGas); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
}
// Sha3 sha3计费
func Sha3(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, params.Sha3Gas); overflow {
return 0, model.ErrGasUintOverflow
}
wordGas, overflow := stack.Back(1).Uint64WithOverflow()
if overflow {
return 0, model.ErrGasUintOverflow
}
if wordGas, overflow = common.SafeMul(common.ToWordSize(wordGas), params.Sha3WordGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, wordGas); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// CodeCopy 代码复制计费
func CodeCopy(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, GasFastestStep); overflow {
return 0, model.ErrGasUintOverflow
}
wordGas, overflow := stack.Back(2).Uint64WithOverflow()
if overflow {
return 0, model.ErrGasUintOverflow
}
if wordGas, overflow = common.SafeMul(common.ToWordSize(wordGas), params.CopyGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, wordGas); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// ExtCodeCopy 扩展代码复制计费
func ExtCodeCopy(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, gt.ExtcodeCopy); overflow {
return 0, model.ErrGasUintOverflow
}
wordGas, overflow := stack.Back(3).Uint64WithOverflow()
if overflow {
return 0, model.ErrGasUintOverflow
}
if wordGas, overflow = common.SafeMul(common.ToWordSize(wordGas), params.CopyGas); overflow {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, wordGas); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// MLoad 内存加载计费
func MLoad(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, GasFastestStep); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// MStore8 内存存储计费
func MStore8(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, GasFastestStep); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// MStore 内存存储计费
func MStore(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, model.ErrGasUintOverflow
}
if gas, overflow = common.SafeAdd(gas, GasFastestStep); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// Create 开辟内存计费
func Create(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var overflow bool
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, params.CreateGas); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// Balance 获取余额计费
func Balance(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return gt.Balance, nil
}
// ExtCodeSize 获取代码大小计费
func ExtCodeSize(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return gt.ExtcodeSize, nil
}
// SLoad 加载存储计费
func SLoad(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return gt.SLoad, nil
}
// Exp exp运算计费
func Exp(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
expByteLen := uint64((stack.Data()[stack.Len()-2].BitLen() + 7) / 8)
var (
gas = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas
overflow bool
)
if gas, overflow = common.SafeAdd(gas, GasSlowStep); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// Call 调用合约计费
func Call(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var (
gas = gt.Calls
transfersValue = stack.Back(2).Sign() != 0
address = common.Uint256ToAddress(stack.Back(1))
)
if !evm.StateDB.Exist(address.String()) {
gas += params.CallNewAccountGas
}
if transfersValue {
gas += params.CallValueTransferGas
}
memoryGas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, memoryGas); overflow {
return 0, model.ErrGasUintOverflow
}
evm.CallGasTemp, err = callGas(gt, contractGas.Gas, gas, stack.Back(0))
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, evm.CallGasTemp); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// CallCode 调用合约代码计费
func CallCode(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas := gt.Calls
if stack.Back(2).Sign() != 0 {
gas += params.CallValueTransferGas
}
memoryGas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, memoryGas); overflow {
return 0, model.ErrGasUintOverflow
}
evm.CallGasTemp, err = callGas(gt, contractGas.Gas, gas, stack.Back(0))
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, evm.CallGasTemp); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// Return 返回操作计费
func Return(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return memoryGasCost(mem, memorySize)
}
// Revert revert操作计费
func Revert(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return memoryGasCost(mem, memorySize)
}
// Suicide 自杀操作计费
func Suicide(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
var gas uint64
if !evm.StateDB.HasSuicided(contractGas.Address.String()) {
evm.StateDB.AddRefund(params.SelfdestructRefundGas)
}
return gas, nil
}
// DelegateCall 委托调用计费
func DelegateCall(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, gt.Calls); overflow {
return 0, model.ErrGasUintOverflow
}
evm.CallGasTemp, err = callGas(gt, contractGas.Gas, gas, stack.Back(0))
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, evm.CallGasTemp); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// StaticCall 静态调用计费
func StaticCall(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
}
var overflow bool
if gas, overflow = common.SafeAdd(gas, gt.Calls); overflow {
return 0, model.ErrGasUintOverflow
}
evm.CallGasTemp, err = callGas(gt, contractGas.Gas, gas, stack.Back(0))
if err != nil {
return 0, err
}
if gas, overflow = common.SafeAdd(gas, evm.CallGasTemp); overflow {
return 0, model.ErrGasUintOverflow
}
return gas, nil
}
// Push 压栈计费
func Push(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return GasFastestStep, nil
}
// Swap 交换计费
func Swap(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return GasFastestStep, nil
}
// Dup dup操作计费
func Dup(gt Table, evm *params.EVMParam, contractGas *params.GasParam, stack *mm.Stack, mem *mm.Memory, memorySize uint64) (uint64, error) {
return GasFastestStep, nil
}