Skip to content

Commit

Permalink
core/state: invoke OnCodeChange-hook on selfdestruct (ethereum#30686)
Browse files Browse the repository at this point in the history
This change invokes the OnCodeChange hook when selfdestruct operation is performed, and a contract is removed. This is an event which can be consumed by tracers.
  • Loading branch information
kchojn authored Nov 8, 2024
1 parent 7cbce8e commit 3c7336b
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions core/state/statedb_hooked.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,46 @@ func (s *hookedStateDB) SetState(address common.Address, key common.Hash, value
}

func (s *hookedStateDB) SelfDestruct(address common.Address) uint256.Int {
var prevCode []byte
var prevCodeHash common.Hash

if s.hooks.OnCodeChange != nil {
prevCode = s.inner.GetCode(address)
prevCodeHash = s.inner.GetCodeHash(address)
}

prev := s.inner.SelfDestruct(address)
if !prev.IsZero() {
if s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
}

if s.hooks.OnBalanceChange != nil && !prev.IsZero() {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
}

if s.hooks.OnCodeChange != nil && len(prevCode) > 0 {
s.hooks.OnCodeChange(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil)
}

return prev
}

func (s *hookedStateDB) SelfDestruct6780(address common.Address) (uint256.Int, bool) {
var prevCode []byte
var prevCodeHash common.Hash

if s.hooks.OnCodeChange != nil {
prevCodeHash = s.inner.GetCodeHash(address)
prevCode = s.inner.GetCode(address)
}

prev, changed := s.inner.SelfDestruct6780(address)
if !prev.IsZero() && changed {
if s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
}

if s.hooks.OnBalanceChange != nil && changed && !prev.IsZero() {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
}

if s.hooks.OnCodeChange != nil && changed && len(prevCode) > 0 {
s.hooks.OnCodeChange(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil)
}

return prev, changed
}

Expand Down

0 comments on commit 3c7336b

Please sign in to comment.