Skip to content

Commit 58374e2

Browse files
holimankaralabe
authored andcommitted
core, state: initial implementation of Eip-1283
1 parent b8aa598 commit 58374e2

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

core/state/state_object.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type stateObject struct {
7979

8080
cachedStorage Storage // Storage entry cache to avoid duplicate reads
8181
dirtyStorage Storage // Storage entries that need to be flushed to disk
82-
82+
originalValue Storage // Map of original storage values, at the beginning of current call context
8383
// Cache flags.
8484
// When an object is marked suicided it will be delete from the trie
8585
// during the "update" phase of the state transition.
@@ -117,6 +117,7 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject {
117117
data: data,
118118
cachedStorage: make(Storage),
119119
dirtyStorage: make(Storage),
120+
originalValue: make(Storage),
120121
}
121122
}
122123

@@ -184,11 +185,16 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
184185

185186
// SetState updates a value in account storage.
186187
func (self *stateObject) SetState(db Database, key, value common.Hash) {
188+
prev := self.GetState(db, key)
187189
self.db.journal.append(storageChange{
188190
account: &self.address,
189191
key: key,
190-
prevalue: self.GetState(db, key),
192+
prevalue: prev,
191193
})
194+
if _, isSet := self.originalValue[key]; !isSet {
195+
// original value has not been set, so set it now
196+
self.originalValue[key] = prev
197+
}
192198
self.setState(key, value)
193199
}
194200

@@ -210,6 +216,10 @@ func (self *stateObject) updateTrie(db Database) Trie {
210216
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
211217
self.setError(tr.TryUpdate(key[:], v))
212218
}
219+
// Clean the map containing 'original' value of storage entries
220+
for k, _ := range self.originalValue {
221+
delete(self.originalValue, k)
222+
}
213223
return tr
214224
}
215225

@@ -280,6 +290,7 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject {
280290
stateObject.code = self.code
281291
stateObject.dirtyStorage = self.dirtyStorage.Copy()
282292
stateObject.cachedStorage = self.dirtyStorage.Copy()
293+
stateObject.originalValue = self.originalValue.Copy()
283294
stateObject.suicided = self.suicided
284295
stateObject.dirtyCode = self.dirtyCode
285296
stateObject.deleted = self.deleted

core/state/statedb.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,22 @@ func (self *StateDB) Preimages() map[common.Hash][]byte {
169169
return self.preimages
170170
}
171171

172+
// AddRefund adds gas to the refund counter
172173
func (self *StateDB) AddRefund(gas uint64) {
173174
self.journal.append(refundChange{prev: self.refund})
174175
self.refund += gas
175176
}
176177

178+
// SubRefund removes gas from the refund counter.
179+
// This method will panic if the refund counter goes below zero
180+
func (self *StateDB) SubRefund(gas uint64) {
181+
self.journal.append(refundChange{prev: self.refund})
182+
if gas > self.refund {
183+
panic("Refund counter below zero")
184+
}
185+
self.refund -= gas
186+
}
187+
177188
// Exist reports whether the given account address exists in the state.
178189
// Notably this also returns true for suicided accounts.
179190
func (self *StateDB) Exist(addr common.Address) bool {

core/vm/gas_table.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package vm
1818

1919
import (
20+
"bytes"
2021
"github.com/ethereum/go-ethereum/common"
2122
"github.com/ethereum/go-ethereum/common/math"
2223
"github.com/ethereum/go-ethereum/params"
24+
"math/big"
2325
)
2426

2527
// memoryGasCosts calculates the quadratic gas for memory expansion. It does so
@@ -115,7 +117,7 @@ func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *
115117
return gas, nil
116118
}
117119

118-
func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
120+
func gasSStoreOld(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
119121
var (
120122
y, x = stack.Back(1), stack.Back(0)
121123
val = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
@@ -137,6 +139,61 @@ func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
137139
}
138140
}
139141

142+
func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
143+
var (
144+
y, x = stack.Back(1), stack.Back(0)
145+
current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
146+
)
147+
//1. If current value equals new value (this is a no-op), 200 gas is deducted.
148+
//2. If current value does not equal new value
149+
// 2.1 If original value equals current value (this storage slot has not been changed by the current execution context)
150+
// 2.1.1 If original value is 0, 20000 gas is deducted.
151+
// 2.1.2 Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
152+
// 2.2 If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
153+
// 2.2.1 If original value is not 0
154+
// 2.2.1.1 If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
155+
// 2.2.1.2 If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
156+
// 2.2.2 If original value equals new value (this storage slot is reset)
157+
// 2.2.2.1 If original value is 0, add 19800 gas to refund counter.
158+
// 2.2.2.2 Otherwise, add 4800 gas to refund counter.
159+
new := common.BigToHash(y)
160+
if current == new {
161+
// 1. current == new
162+
return 200, nil
163+
}
164+
// Todo, get this value
165+
original := common.Hash{}
166+
167+
// 2
168+
if original == current { // 2.1
169+
if original == (common.Hash{}){ // 2.1.1
170+
return 20000, nil
171+
}
172+
// 2.1.2
173+
if new == (common.Hash{}){
174+
evm.StateDB.AddRefund(15000)
175+
}
176+
return 5000, nil
177+
}
178+
// 2.2
179+
if original != (common.Hash{}){ // 2.2.1
180+
if current == (common.Hash{}){ // 2.2.1.1
181+
evm.StateDB.SubRefund(15000)
182+
}else{
183+
// 2.2.1.2
184+
evm.StateDB.AddRefund(15000)
185+
}
186+
}
187+
if original == new { // 2.2.2
188+
if original == (common.Hash{}){
189+
evm.StateDB.AddRefund(19800)
190+
}else{
191+
evm.StateDB.AddRefund(4800)
192+
}
193+
}
194+
return 200, nil
195+
}
196+
140197
func makeGasLog(n uint64) gasFunc {
141198
return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
142199
requestedSize, overflow := bigUint64(stack.Back(1))

core/vm/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type StateDB interface {
4040
GetCodeSize(common.Address) int
4141

4242
AddRefund(uint64)
43+
SubRefund(uint64)
4344
GetRefund() uint64
4445

4546
GetState(common.Address, common.Hash) common.Hash

core/vm/noop.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (NoopStateDB) GetCode(common.Address) []byte
5656
func (NoopStateDB) SetCode(common.Address, []byte) {}
5757
func (NoopStateDB) GetCodeSize(common.Address) int { return 0 }
5858
func (NoopStateDB) AddRefund(uint64) {}
59+
func (NoopStateDB) SubRefund(uint64) {}
5960
func (NoopStateDB) GetRefund() uint64 { return 0 }
6061
func (NoopStateDB) GetState(common.Address, common.Hash) common.Hash { return common.Hash{} }
6162
func (NoopStateDB) SetState(common.Address, common.Hash, common.Hash) {}

0 commit comments

Comments
 (0)