core/state: fix several hook issues

This commit is contained in:
Gary Rong 2024-10-21 18:57:51 +08:00
parent 8ca0cd582d
commit 21f5a77ece
4 changed files with 20 additions and 22 deletions

View file

@ -459,7 +459,7 @@ func (s *stateObject) AddBalance(amount *uint256.Int) uint256.Int {
return s.SetBalance(new(uint256.Int).Add(s.Balance(), amount)) return s.SetBalance(new(uint256.Int).Add(s.Balance(), amount))
} }
// SetBalance sets the balance for the object, and returns the prevous balance // SetBalance sets the balance for the object, and returns the previous balance.
func (s *stateObject) SetBalance(amount *uint256.Int) uint256.Int { func (s *stateObject) SetBalance(amount *uint256.Int) uint256.Int {
prev := *s.data.Balance prev := *s.data.Balance
s.db.journal.balanceChange(s.address, s.data.Balance) s.db.journal.balanceChange(s.address, s.data.Balance)

View file

@ -402,24 +402,22 @@ func (s *StateDB) HasSelfDestructed(addr common.Address) bool {
// AddBalance adds amount to the account associated with addr. // AddBalance adds amount to the account associated with addr.
func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int { func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
stateObject := s.getOrNewStateObject(addr) stateObject := s.getOrNewStateObject(addr)
if stateObject != nil { if stateObject == nil {
return stateObject.AddBalance(amount) return uint256.Int{}
} }
return uint256.Int{} return stateObject.AddBalance(amount)
} }
// SubBalance subtracts amount from the account associated with addr. // SubBalance subtracts amount from the account associated with addr.
func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int { func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
stateObject := s.getOrNewStateObject(addr) stateObject := s.getOrNewStateObject(addr)
var prev uint256.Int if stateObject == nil {
return uint256.Int{}
}
if amount.IsZero() { if amount.IsZero() {
return prev return *(stateObject.Balance())
} }
if stateObject != nil { return stateObject.SetBalance(new(uint256.Int).Sub(stateObject.Balance(), amount))
prev = *(stateObject.Balance())
stateObject.SetBalance(new(uint256.Int).Sub(stateObject.Balance(), amount))
}
return prev
} }
func (s *StateDB) SetBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) { 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 {
return prevBalance return prevBalance
} }
func (s *StateDB) Selfdestruct6780(addr common.Address) uint256.Int { func (s *StateDB) Selfdestruct6780(addr common.Address) (uint256.Int, bool) {
stateObject := s.getStateObject(addr) stateObject := s.getStateObject(addr)
if stateObject == nil { if stateObject == nil {
return uint256.Int{} return uint256.Int{}, false
} }
if stateObject.newContract { if stateObject.newContract {
return s.SelfDestruct(addr) return s.SelfDestruct(addr), true
} }
return *(stateObject.Balance()) return *(stateObject.Balance()), false
} }
// SetTransientState sets transient storage for a given account. It // SetTransientState sets transient storage for a given account. It

View file

@ -159,7 +159,7 @@ func (s *hookedStateDB) Witness() *stateless.Witness {
func (s *hookedStateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int { func (s *hookedStateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
prev := s.inner.SubBalance(addr, amount, reason) prev := s.inner.SubBalance(addr, amount, reason)
if s.hooks.OnBalanceChange != nil { if s.hooks.OnBalanceChange != nil && !amount.IsZero() {
newBalance := new(uint256.Int).Sub(&prev, amount) newBalance := new(uint256.Int).Sub(&prev, amount)
s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason) s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason)
} }
@ -168,7 +168,7 @@ func (s *hookedStateDB) SubBalance(addr common.Address, amount *uint256.Int, rea
func (s *hookedStateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int { func (s *hookedStateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
prev := s.inner.AddBalance(addr, amount, reason) prev := s.inner.AddBalance(addr, amount, reason)
if s.hooks.OnBalanceChange != nil { if s.hooks.OnBalanceChange != nil && !amount.IsZero() {
newBalance := new(uint256.Int).Add(&prev, amount) newBalance := new(uint256.Int).Add(&prev, amount)
s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason) s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason)
} }
@ -207,14 +207,14 @@ func (s *hookedStateDB) SelfDestruct(address common.Address) uint256.Int {
return prev return prev
} }
func (s *hookedStateDB) Selfdestruct6780(address common.Address) uint256.Int { func (s *hookedStateDB) Selfdestruct6780(address common.Address) (uint256.Int, bool) {
prev := s.inner.Selfdestruct6780(address) prev, changed := s.inner.Selfdestruct6780(address)
if !prev.IsZero() { if !prev.IsZero() && changed {
if s.hooks.OnBalanceChange != nil { if s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct) s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
} }
} }
return prev return prev, changed
} }
func (s *hookedStateDB) AddLog(log *types.Log) { func (s *hookedStateDB) AddLog(log *types.Log) {

View file

@ -60,7 +60,7 @@ type StateDB interface {
SelfDestruct(common.Address) uint256.Int SelfDestruct(common.Address) uint256.Int
HasSelfDestructed(common.Address) bool HasSelfDestructed(common.Address) bool
Selfdestruct6780(common.Address) uint256.Int Selfdestruct6780(common.Address) (uint256.Int, bool)
// Exist reports whether the given account exists in state. // Exist reports whether the given account exists in state.
// Notably this should also return true for self-destructed accounts. // Notably this should also return true for self-destructed accounts.