@@ -277,15 +277,32 @@ func (s *hookedStateDB) AddLog(log *types.Log) {
277277
278278func (s * hookedStateDB ) Finalise (deleteEmptyObjects bool ) {
279279 defer s .inner .Finalise (deleteEmptyObjects )
280- if s .hooks .OnBalanceChange == nil {
281- return
282- }
283- for addr := range s .inner .journal .dirties {
284- obj := s .inner .stateObjects [addr ]
285- if obj != nil && obj .selfDestructed {
286- // If ether was sent to account post-selfdestruct it is burnt.
287- if bal := obj .Balance (); bal .Sign () != 0 {
288- s .hooks .OnBalanceChange (addr , bal .ToBig (), new (big.Int ), tracing .BalanceDecreaseSelfdestructBurn )
280+ if s .hooks .OnBalanceChange != nil || s .hooks .OnNonceChangeV2 != nil || s .hooks .OnCodeChangeV2 != nil || s .hooks .OnCodeChange != nil {
281+ for addr := range s .inner .journal .dirties {
282+ obj := s .inner .stateObjects [addr ]
283+ if obj != nil && obj .selfDestructed {
284+ // If ether was sent to account post-selfdestruct it is burnt.
285+ if s .hooks .OnBalanceChange != nil {
286+ if bal := obj .Balance (); bal .Sign () != 0 {
287+ s .hooks .OnBalanceChange (addr , bal .ToBig (), new (big.Int ), tracing .BalanceDecreaseSelfdestructBurn )
288+ }
289+ }
290+ if s .hooks .OnNonceChangeV2 != nil {
291+ prevNonce := obj .Nonce ()
292+ s .hooks .OnNonceChangeV2 (addr , prevNonce , 0 , tracing .NonceChangeSelfdestruct )
293+ }
294+ prevCodeHash := s .inner .GetCodeHash (addr )
295+ prevCode := s .inner .GetCode (addr )
296+
297+ // if an initcode invokes selfdestruct, do not emit a code change.
298+ if prevCodeHash == types .EmptyCodeHash {
299+ continue
300+ }
301+ if s .hooks .OnCodeChangeV2 != nil {
302+ s .hooks .OnCodeChangeV2 (addr , prevCodeHash , prevCode , types .EmptyCodeHash , nil , tracing .CodeChangeSelfDestruct )
303+ } else if s .hooks .OnCodeChange != nil {
304+ s .hooks .OnCodeChange (addr , prevCodeHash , prevCode , types .EmptyCodeHash , nil )
305+ }
289306 }
290307 }
291308 }
0 commit comments