Skip to content

Commit 21f5a77

Browse files
committed
core/state: fix several hook issues
1 parent 8ca0cd5 commit 21f5a77

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

core/state/state_object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (s *stateObject) AddBalance(amount *uint256.Int) uint256.Int {
459459
return s.SetBalance(new(uint256.Int).Add(s.Balance(), amount))
460460
}
461461

462-
// SetBalance sets the balance for the object, and returns the prevous balance
462+
// SetBalance sets the balance for the object, and returns the previous balance.
463463
func (s *stateObject) SetBalance(amount *uint256.Int) uint256.Int {
464464
prev := *s.data.Balance
465465
s.db.journal.balanceChange(s.address, s.data.Balance)

core/state/statedb.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,24 +402,22 @@ func (s *StateDB) HasSelfDestructed(addr common.Address) bool {
402402
// AddBalance adds amount to the account associated with addr.
403403
func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
404404
stateObject := s.getOrNewStateObject(addr)
405-
if stateObject != nil {
406-
return stateObject.AddBalance(amount)
405+
if stateObject == nil {
406+
return uint256.Int{}
407407
}
408-
return uint256.Int{}
408+
return stateObject.AddBalance(amount)
409409
}
410410

411411
// SubBalance subtracts amount from the account associated with addr.
412412
func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
413413
stateObject := s.getOrNewStateObject(addr)
414-
var prev uint256.Int
415-
if amount.IsZero() {
416-
return prev
414+
if stateObject == nil {
415+
return uint256.Int{}
417416
}
418-
if stateObject != nil {
419-
prev = *(stateObject.Balance())
420-
stateObject.SetBalance(new(uint256.Int).Sub(stateObject.Balance(), amount))
417+
if amount.IsZero() {
418+
return *(stateObject.Balance())
421419
}
422-
return prev
420+
return stateObject.SetBalance(new(uint256.Int).Sub(stateObject.Balance(), amount))
423421
}
424422

425423
func (s *StateDB) SetBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) {
@@ -505,15 +503,15 @@ func (s *StateDB) SelfDestruct(addr common.Address) uint256.Int {
505503
return prevBalance
506504
}
507505

508-
func (s *StateDB) Selfdestruct6780(addr common.Address) uint256.Int {
506+
func (s *StateDB) Selfdestruct6780(addr common.Address) (uint256.Int, bool) {
509507
stateObject := s.getStateObject(addr)
510508
if stateObject == nil {
511-
return uint256.Int{}
509+
return uint256.Int{}, false
512510
}
513511
if stateObject.newContract {
514-
return s.SelfDestruct(addr)
512+
return s.SelfDestruct(addr), true
515513
}
516-
return *(stateObject.Balance())
514+
return *(stateObject.Balance()), false
517515
}
518516

519517
// SetTransientState sets transient storage for a given account. It

core/state/statedb_hooked.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *hookedStateDB) Witness() *stateless.Witness {
159159

160160
func (s *hookedStateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
161161
prev := s.inner.SubBalance(addr, amount, reason)
162-
if s.hooks.OnBalanceChange != nil {
162+
if s.hooks.OnBalanceChange != nil && !amount.IsZero() {
163163
newBalance := new(uint256.Int).Sub(&prev, amount)
164164
s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason)
165165
}
@@ -168,7 +168,7 @@ func (s *hookedStateDB) SubBalance(addr common.Address, amount *uint256.Int, rea
168168

169169
func (s *hookedStateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
170170
prev := s.inner.AddBalance(addr, amount, reason)
171-
if s.hooks.OnBalanceChange != nil {
171+
if s.hooks.OnBalanceChange != nil && !amount.IsZero() {
172172
newBalance := new(uint256.Int).Add(&prev, amount)
173173
s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason)
174174
}
@@ -207,14 +207,14 @@ func (s *hookedStateDB) SelfDestruct(address common.Address) uint256.Int {
207207
return prev
208208
}
209209

210-
func (s *hookedStateDB) Selfdestruct6780(address common.Address) uint256.Int {
211-
prev := s.inner.Selfdestruct6780(address)
212-
if !prev.IsZero() {
210+
func (s *hookedStateDB) Selfdestruct6780(address common.Address) (uint256.Int, bool) {
211+
prev, changed := s.inner.Selfdestruct6780(address)
212+
if !prev.IsZero() && changed {
213213
if s.hooks.OnBalanceChange != nil {
214214
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
215215
}
216216
}
217-
return prev
217+
return prev, changed
218218
}
219219

220220
func (s *hookedStateDB) AddLog(log *types.Log) {

core/vm/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type StateDB interface {
6060
SelfDestruct(common.Address) uint256.Int
6161
HasSelfDestructed(common.Address) bool
6262

63-
Selfdestruct6780(common.Address) uint256.Int
63+
Selfdestruct6780(common.Address) (uint256.Int, bool)
6464

6565
// Exist reports whether the given account exists in state.
6666
// Notably this should also return true for self-destructed accounts.

0 commit comments

Comments
 (0)