1717package vm
1818
1919import (
20- "bytes"
2120 "github.com/ethereum/go-ethereum/common"
2221 "github.com/ethereum/go-ethereum/common/math"
2322 "github.com/ethereum/go-ethereum/params"
24- "math/big"
2523)
2624
2725// 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 *
117115 return gas , nil
118116}
119117
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 ) {
121119 var (
122120 y , x = stack .Back (1 ), stack .Back (0 )
123121 val = evm .StateDB .GetState (contract .Address (), common .BigToHash (x ))
@@ -139,10 +137,11 @@ func gasSStoreOld(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack
139137 }
140138}
141139
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 ) {
143142 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 ))
146145 )
147146 //1. If current value equals new value (this is a no-op), 200 gas is deducted.
148147 //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
161160 // 1. current == new
162161 return 200 , nil
163162 }
164- // Todo, get this value
165- original := common.Hash {}
166-
163+ original := evm .StateDB .GetStateOriginal (contract .Address (), common .BigToHash (x ))
167164 // 2
168165 if original == current { // 2.1
169- if original == (common.Hash {}){ // 2.1.1
166+ if original == (common.Hash {}) { // 2.1.1
170167 return 20000 , nil
171168 }
172169 // 2.1.2
173- if new == (common.Hash {}){
170+ if new == (common.Hash {}) {
174171 evm .StateDB .AddRefund (15000 )
175172 }
176173 return 5000 , nil
177174 }
178175 // 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
181178 evm .StateDB .SubRefund (15000 )
182- }else {
179+ } else {
183180 // 2.2.1.2
184181 evm .StateDB .AddRefund (15000 )
185182 }
186183 }
187184 if original == new { // 2.2.2
188- if original == (common.Hash {}){
185+ if original == (common.Hash {}) {
189186 evm .StateDB .AddRefund (19800 )
190- }else {
187+ } else {
191188 evm .StateDB .AddRefund (4800 )
192189 }
193190 }
0 commit comments