Skip to content

Commit caa2c23

Browse files
holimankaralabe
authored andcommitted
core,state: finish implementing Eip 1283
1 parent 58374e2 commit caa2c23

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

core/state/state_object.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
183183
return value
184184
}
185185

186+
// GetOriginalStateValue returns the state value that is currently in the Trie, that is, ignoring any
187+
// changes that have been made but not yet written to trie.
188+
func (self *stateObject) GetOriginalStateValue(db Database, key common.Hash) common.Hash{
189+
if original, exist:= self.originalValue[key]; exist {
190+
// original value has been set, return it
191+
return original
192+
}
193+
return self.GetState(db, key)
194+
}
195+
186196
// SetState updates a value in account storage.
187197
func (self *stateObject) SetState(db Database, key, value common.Hash) {
188198
prev := self.GetState(db, key)

core/state/statedb.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ func (self *StateDB) GetState(addr common.Address, bhash common.Hash) common.Has
255255
return common.Hash{}
256256
}
257257

258+
func (self *StateDB) GetStateOriginal(addr common.Address, bhash common.Hash) common.Hash {
259+
stateObject := self.getStateObject(addr)
260+
if stateObject != nil {
261+
return stateObject.GetOriginalStateValue(self.db, bhash)
262+
}
263+
return common.Hash{}
264+
}
265+
258266
// Database retrieves the low level database supporting the lower level trie ops.
259267
func (self *StateDB) Database() Database {
260268
return self.db

core/vm/gas_table.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
package vm
1818

1919
import (
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
}

core/vm/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type StateDB interface {
4444
GetRefund() uint64
4545

4646
GetState(common.Address, common.Hash) common.Hash
47+
GetStateOriginal(common.Address, common.Hash) common.Hash
4748
SetState(common.Address, common.Hash, common.Hash)
4849

4950
Suicide(common.Address) bool

core/vm/jump_table.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ func newConstantinopleInstructionSet() [256]operation {
9595
writes: true,
9696
returns: true,
9797
}
98+
instructionSet[SSTORE] = operation{
99+
execute: opSstore,
100+
gasCost: gasSStoreEip1283,
101+
validateStack: makeStackFunc(2, 0),
102+
valid: true,
103+
writes: true,
104+
}
105+
98106
return instructionSet
99107
}
100108

core/vm/noop.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (NoopStateDB) AddRefund(uint64)
5959
func (NoopStateDB) SubRefund(uint64) {}
6060
func (NoopStateDB) GetRefund() uint64 { return 0 }
6161
func (NoopStateDB) GetState(common.Address, common.Hash) common.Hash { return common.Hash{} }
62+
func (NoopStateDB) GetStateOriginal(common.Address, common.Hash) common.Hash { return common.Hash{} }
6263
func (NoopStateDB) SetState(common.Address, common.Hash, common.Hash) {}
6364
func (NoopStateDB) Suicide(common.Address) bool { return false }
6465
func (NoopStateDB) HasSuicided(common.Address) bool { return false }

0 commit comments

Comments
 (0)