aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/gas_table.go
blob: 4d2c4e94aef81b23adf2489249738300481a2401 (plain) (blame)
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
package vm

import (
    "math/big"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/params"
)

func memoryGasCost(mem *Memory, newMemSize *big.Int) *big.Int {
    gas := new(big.Int)
    if newMemSize.Cmp(common.Big0) > 0 {
        newMemSizeWords := toWordSize(newMemSize)

        if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
            // be careful reusing variables here when changing.
            // The order has been optimised to reduce allocation
            oldSize := toWordSize(big.NewInt(int64(mem.Len())))
            pow := new(big.Int).Exp(oldSize, common.Big2, Zero)
            linCoef := oldSize.Mul(oldSize, params.MemoryGas)
            quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv)
            oldTotalFee := new(big.Int).Add(linCoef, quadCoef)

            pow.Exp(newMemSizeWords, common.Big2, Zero)
            linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas)
            quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv)
            newTotalFee := linCoef.Add(linCoef, quadCoef)

            fee := newTotalFee.Sub(newTotalFee, oldTotalFee)
            gas.Add(gas, fee)
        }
    }
    return gas
}

func constGasFunc(gas *big.Int) gasFunc {
    return func(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
        return gas
    }
}

func gasCalldataCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := memoryGasCost(mem, memorySize)
    gas.Add(gas, GasFastestStep)
    words := toWordSize(stack.Back(2))

    return gas.Add(gas, words.Mul(words, params.CopyGas))
}

func gasSStore(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    var (
        y, x = stack.Back(1), stack.Back(0)
        val  = env.StateDB.GetState(contract.Address(), common.BigToHash(x))
    )
    // This checks for 3 scenario's and calculates gas accordingly
    // 1. From a zero-value address to a non-zero value         (NEW VALUE)
    // 2. From a non-zero value address to a zero-value address (DELETE)
    // 3. From a non-zero to a non-zero                         (CHANGE)
    if common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) {
        // 0 => non 0
        return new(big.Int).Set(params.SstoreSetGas)
    } else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) {
        env.StateDB.AddRefund(params.SstoreRefundGas)

        return new(big.Int).Set(params.SstoreClearGas)
    } else {
        // non 0 => non 0 (or 0 => 0)
        return new(big.Int).Set(params.SstoreResetGas)
    }
}

func makeGasLog(n uint) gasFunc {
    return func(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
        mSize := stack.Back(1)

        gas := new(big.Int).Add(memoryGasCost(mem, memorySize), params.LogGas)
        gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(n)), params.LogTopicGas))
        gas.Add(gas, new(big.Int).Mul(mSize, params.LogDataGas))
        return gas
    }
}

func gasSha3(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := memoryGasCost(mem, memorySize)
    gas.Add(gas, params.Sha3Gas)
    words := toWordSize(stack.Back(1))
    return gas.Add(gas, words.Mul(words, params.Sha3WordGas))
}

func gasCodeCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := memoryGasCost(mem, memorySize)
    gas.Add(gas, GasFastestStep)
    words := toWordSize(stack.Back(2))

    return gas.Add(gas, words.Mul(words, params.CopyGas))
}

func gasExtCodeCopy(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := memoryGasCost(mem, memorySize)
    gas.Add(gas, gt.ExtcodeCopy)
    words := toWordSize(stack.Back(3))

    return gas.Add(gas, words.Mul(words, params.CopyGas))
}

func gasMLoad(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return new(big.Int).Add(GasFastestStep, memoryGasCost(mem, memorySize))
}

func gasMStore8(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return new(big.Int).Add(GasFastestStep, memoryGasCost(mem, memorySize))
}

func gasMStore(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return new(big.Int).Add(GasFastestStep, memoryGasCost(mem, memorySize))
}

func gasCreate(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return new(big.Int).Add(params.CreateGas, memoryGasCost(mem, memorySize))
}

func gasBalance(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return gt.Balance
}

func gasExtCodeSize(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return gt.ExtcodeSize
}

func gasSLoad(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return gt.SLoad
}

func gasExp(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    expByteLen := int64((stack.data[stack.len()-2].BitLen() + 7) / 8)
    gas := big.NewInt(expByteLen)
    gas.Mul(gas, gt.ExpByte)
    return gas.Add(gas, GasSlowStep)
}

func gasCall(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := new(big.Int).Set(gt.Calls)

    transfersValue := stack.Back(2).BitLen() > 0
    var (
        address = common.BigToAddress(stack.Back(1))
        eip158  = env.ChainConfig().IsEIP158(env.BlockNumber)
    )
    if eip158 {
        if env.StateDB.Empty(address) && transfersValue {
            gas.Add(gas, params.CallNewAccountGas)
        }
    } else if !env.StateDB.Exist(address) {
        gas.Add(gas, params.CallNewAccountGas)
    }
    if transfersValue {
        gas.Add(gas, params.CallValueTransferGas)
    }
    gas.Add(gas, memoryGasCost(mem, memorySize))

    cg := callGas(gt, contract.Gas, gas, stack.data[stack.len()-1])
    // Replace the stack item with the new gas calculation. This means that
    // either the original item is left on the stack or the item is replaced by:
    // (availableGas - gas) * 63 / 64
    // We replace the stack item so that it's available when the opCall instruction is
    // called. This information is otherwise lost due to the dependency on *current*
    // available gas.
    stack.data[stack.len()-1] = cg

    return gas.Add(gas, cg)
}

func gasCallCode(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := new(big.Int).Set(gt.Calls)
    if stack.Back(2).BitLen() > 0 {
        gas.Add(gas, params.CallValueTransferGas)
    }
    gas.Add(gas, memoryGasCost(mem, memorySize))

    cg := callGas(gt, contract.Gas, gas, stack.data[stack.len()-1])
    // Replace the stack item with the new gas calculation. This means that
    // either the original item is left on the stack or the item is replaced by:
    // (availableGas - gas) * 63 / 64
    // We replace the stack item so that it's available when the opCall instruction is
    // called. This information is otherwise lost due to the dependency on *current*
    // available gas.
    stack.data[stack.len()-1] = cg

    return gas.Add(gas, cg)
}

func gasReturn(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return memoryGasCost(mem, memorySize)
}

func gasSuicide(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := new(big.Int)
    // EIP150 homestead gas reprice fork:
    if env.ChainConfig().IsEIP150(env.BlockNumber) {
        gas.Set(gt.Suicide)
        var (
            address = common.BigToAddress(stack.Back(0))
            eip158  = env.ChainConfig().IsEIP158(env.BlockNumber)
        )

        if eip158 {
            // if empty and transfers value
            if env.StateDB.Empty(address) && env.StateDB.GetBalance(contract.Address()).BitLen() > 0 {
                gas.Add(gas, gt.CreateBySuicide)
            }
        } else if !env.StateDB.Exist(address) {
            gas.Add(gas, gt.CreateBySuicide)
        }
    }

    if !env.StateDB.HasSuicided(contract.Address()) {
        env.StateDB.AddRefund(params.SuicideRefundGas)
    }
    return gas
}

func gasDelegateCall(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    gas := new(big.Int).Add(gt.Calls, memoryGasCost(mem, memorySize))

    cg := callGas(gt, contract.Gas, gas, stack.data[stack.len()-1])
    // Replace the stack item with the new gas calculation. This means that
    // either the original item is left on the stack or the item is replaced by:
    // (availableGas - gas) * 63 / 64
    // We replace the stack item so that it's available when the opCall instruction is
    // called.
    stack.data[stack.len()-1] = cg

    return gas.Add(gas, cg)
}

func gasPush(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return GasFastestStep
}

func gasSwap(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return GasFastestStep
}

func gasDup(gt params.GasTable, env *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize *big.Int) *big.Int {
    return GasFastestStep
}