Skip to content

Commit 7849b7e

Browse files
committed
Refund SSTORE properly
1 parent 5920aa7 commit 7849b7e

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎ethstate/state.go‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ type State struct {
2222
stateObjects map[string]*StateObject
2323

2424
manifest *Manifest
25+
26+
refund map[string]*big.Int
2527
}
2628

2729
// Create a new state from a given trie
2830
func New(trie *ethtrie.Trie) *State {
29-
return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest()}
31+
return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)}
3032
}
3133

3234
// Retrieve the balance from the given address or 0 if object not found
@@ -39,6 +41,16 @@ func (self *State) GetBalance(addr []byte) *big.Int {
3941
return ethutil.Big0
4042
}
4143

44+
func (self *State) Refund(addr []byte, gas, price *big.Int) {
45+
amount := new(big.Int).Mul(gas, price)
46+
47+
if self.refund[string(addr)] == nil {
48+
self.refund[string(addr)] = new(big.Int)
49+
}
50+
51+
self.refund[string(addr)] = new(big.Int).Add(self.refund[string(addr)], amount)
52+
}
53+
4254
func (self *State) AddBalance(addr []byte, amount *big.Int) {
4355
stateObject := self.GetStateObject(addr)
4456
if stateObject != nil {
@@ -186,6 +198,10 @@ func (self *State) Copy() *State {
186198
state.stateObjects[k] = stateObject.Copy()
187199
}
188200

201+
for addr, refund := range self.refund {
202+
state.refund[addr] = refund
203+
}
204+
189205
return state
190206
}
191207

@@ -199,6 +215,7 @@ func (self *State) Set(state *State) {
199215

200216
self.Trie = state.Trie
201217
self.stateObjects = state.stateObjects
218+
self.refund = state.refund
202219
}
203220

204221
func (s *State) Root() interface{} {
@@ -240,10 +257,17 @@ func (s *State) Sync() {
240257

241258
func (self *State) Empty() {
242259
self.stateObjects = make(map[string]*StateObject)
260+
self.refund = make(map[string]*big.Int)
243261
}
244262

245263
func (self *State) Update() {
246264
var deleted bool
265+
266+
// Refund any gas that's left
267+
for addr, amount := range self.refund {
268+
self.GetStateObject([]byte(addr)).AddBalance(amount)
269+
}
270+
247271
for _, stateObject := range self.stateObjects {
248272
if stateObject.remove {
249273
self.DeleteStateObject(stateObject)

‎vm/vm_debug.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) {
179179
// 0 => non 0
180180
mult = ethutil.Big3
181181
} else if val.BigInt().Cmp(ethutil.Big0) != 0 && len(y.Bytes()) == 0 {
182-
//state.AddBalance(closure.caller.Address(), new(big.Int).Mul(big.NewInt(100), closure.Price))
182+
state.Refund(closure.caller.Address(), big.NewInt(100), closure.Price)
183+
183184
mult = ethutil.Big0
184185
} else {
185186
// non 0 => non 0

0 commit comments

Comments
 (0)