17
17
package vm
18
18
19
19
import (
20
- "bytes"
21
20
"github.com/ethereum/go-ethereum/common"
22
21
"github.com/ethereum/go-ethereum/common/math"
23
22
"github.com/ethereum/go-ethereum/params"
24
- "math/big"
25
23
)
26
24
27
25
// memoryGasCosts calculates the quadratic gas for memory expansion. It does so
@@ -117,7 +115,7 @@ func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *
117
115
return gas , nil
118
116
}
119
117
120
- func gasSStoreOld (gt params.GasTable , evm * EVM , contract * Contract , stack * Stack , mem * Memory , memorySize uint64 ) (uint64 , error ) {
118
+ func gasSStore (gt params.GasTable , evm * EVM , contract * Contract , stack * Stack , mem * Memory , memorySize uint64 ) (uint64 , error ) {
121
119
var (
122
120
y , x = stack .Back (1 ), stack .Back (0 )
123
121
val = evm .StateDB .GetState (contract .Address (), common .BigToHash (x ))
@@ -139,10 +137,11 @@ func gasSStoreOld(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack
139
137
}
140
138
}
141
139
142
- func gasSStore (gt params.GasTable , evm * EVM , contract * Contract , stack * Stack , mem * Memory , memorySize uint64 ) (uint64 , error ) {
140
+ // gasSStoreEip1283 calculates SSTORE gas cost according to EIP-1283
141
+ func gasSStoreEip1283 (gt params.GasTable , evm * EVM , contract * Contract , stack * Stack , mem * Memory , memorySize uint64 ) (uint64 , error ) {
143
142
var (
144
- y , x = stack .Back (1 ), stack .Back (0 )
145
- current = evm .StateDB .GetState (contract .Address (), common .BigToHash (x ))
143
+ y , x = stack .Back (1 ), stack .Back (0 )
144
+ current = evm .StateDB .GetState (contract .Address (), common .BigToHash (x ))
146
145
)
147
146
//1. If current value equals new value (this is a no-op), 200 gas is deducted.
148
147
//2. If current value does not equal new value
@@ -161,33 +160,31 @@ func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
161
160
// 1. current == new
162
161
return 200 , nil
163
162
}
164
- // Todo, get this value
165
- original := common.Hash {}
166
-
163
+ original := evm .StateDB .GetStateOriginal (contract .Address (), common .BigToHash (x ))
167
164
// 2
168
165
if original == current { // 2.1
169
- if original == (common.Hash {}){ // 2.1.1
166
+ if original == (common.Hash {}) { // 2.1.1
170
167
return 20000 , nil
171
168
}
172
169
// 2.1.2
173
- if new == (common.Hash {}){
170
+ if new == (common.Hash {}) {
174
171
evm .StateDB .AddRefund (15000 )
175
172
}
176
173
return 5000 , nil
177
174
}
178
175
// 2.2
179
- if original != (common.Hash {}){ // 2.2.1
180
- if current == (common.Hash {}){ // 2.2.1.1
176
+ if original != (common.Hash {}) { // 2.2.1
177
+ if current == (common.Hash {}) { // 2.2.1.1
181
178
evm .StateDB .SubRefund (15000 )
182
- }else {
179
+ } else {
183
180
// 2.2.1.2
184
181
evm .StateDB .AddRefund (15000 )
185
182
}
186
183
}
187
184
if original == new { // 2.2.2
188
- if original == (common.Hash {}){
185
+ if original == (common.Hash {}) {
189
186
evm .StateDB .AddRefund (19800 )
190
- }else {
187
+ } else {
191
188
evm .StateDB .AddRefund (4800 )
192
189
}
193
190
}
0 commit comments