Skip to content

Commit 3325683

Browse files
committed
Fixed refund model
1 parent f7287c6 commit 3325683

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

core/state_transition.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,10 @@ func MakeContract(msg Message, state *state.StateDB) *state.StateObject {
213213
func (self *StateTransition) RefundGas() {
214214
coinbaseSub := new(big.Int).Set(self.gas)
215215
uhalf := new(big.Int).Div(self.GasUsed(), ethutil.Big2)
216-
for addr, refs := range self.state.Refunds() {
217-
for _, ref := range refs {
218-
coinbaseSub.Add(self.gas, ref)
219-
refund := ethutil.BigMin(uhalf, ref)
220-
self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice()))
221-
}
216+
for addr, ref := range self.state.Refunds() {
217+
refund := ethutil.BigMin(uhalf, ref)
218+
coinbaseSub.Add(self.gas, refund)
219+
self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice()))
222220
}
223221

224222
coinbase, sender := self.Coinbase(), self.From()

state/state.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ type StateDB struct {
2323

2424
manifest *Manifest
2525

26-
refund map[string][]*big.Int
26+
refund map[string]*big.Int
2727

2828
logs Logs
2929
}
3030

3131
// Create a new state from a given trie
3232
func New(trie *trie.Trie) *StateDB {
33-
return &StateDB{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string][]*big.Int)}
33+
return &StateDB{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)}
3434
}
3535

3636
func (self *StateDB) EmptyLogs() {
@@ -56,7 +56,10 @@ func (self *StateDB) GetBalance(addr []byte) *big.Int {
5656
}
5757

5858
func (self *StateDB) Refund(addr []byte, gas *big.Int) {
59-
self.refund[string(addr)] = append(self.refund[string(addr)], gas)
59+
if self.refund[string(addr)] == nil {
60+
self.refund[string(addr)] = new(big.Int)
61+
}
62+
self.refund[string(addr)].Add(self.refund[string(addr)], gas)
6063
}
6164

6265
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
@@ -207,7 +210,7 @@ func (self *StateDB) Copy() *StateDB {
207210
}
208211

209212
for addr, refund := range self.refund {
210-
state.refund[addr] = refund
213+
state.refund[addr] = new(big.Int).Set(refund)
211214
}
212215

213216
logs := make(Logs, len(self.logs))
@@ -269,17 +272,17 @@ func (s *StateDB) Sync() {
269272

270273
func (self *StateDB) Empty() {
271274
self.stateObjects = make(map[string]*StateObject)
272-
self.refund = make(map[string][]*big.Int)
275+
self.refund = make(map[string]*big.Int)
273276
}
274277

275-
func (self *StateDB) Refunds() map[string][]*big.Int {
278+
func (self *StateDB) Refunds() map[string]*big.Int {
276279
return self.refund
277280
}
278281

279282
func (self *StateDB) Update(gasUsed *big.Int) {
280283
var deleted bool
281284

282-
self.refund = make(map[string][]*big.Int)
285+
self.refund = make(map[string]*big.Int)
283286

284287
for _, stateObject := range self.stateObjects {
285288
if stateObject.remove {

tests/vm/gh_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ func RunVmTest(p string, t *testing.T) {
7777
tests := make(map[string]VmTest)
7878
helper.CreateFileTests(t, p, &tests)
7979

80-
helper.Logger.SetLogLevel(5)
80+
//helper.Logger.SetLogLevel(5)
8181
for name, test := range tests {
82-
if name != "ABAcalls1" {
83-
continue
84-
}
82+
// if name != "refund50_1" {
83+
// continue
84+
// }
8585
statedb := state.New(helper.NewTrie())
8686
for addr, account := range test.Pre {
8787
obj := StateObjectFromAccount(addr, account)

0 commit comments

Comments
 (0)