Skip to content

Commit

Permalink
core/state: simplify state logger
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Oct 15, 2024
1 parent 9432163 commit 0a4002e
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions core/state/statedb_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,58 @@ import (

type stateDBLogger struct {
*StateDB
logger *tracing.Hooks
hooks *tracing.Hooks
}

func newStateDBLogger(db *StateDB, logger *tracing.Hooks) *stateDBLogger {
s := &stateDBLogger{db, logger}

func newStateDBLogger(db *StateDB, hooks *tracing.Hooks) *stateDBLogger {
s := &stateDBLogger{db, hooks}
if s.hooks == nil {
s.hooks = new(tracing.Hooks)
}
db.SetBurnCallback(func(address common.Address, amount *uint256.Int) {
if s.logger != nil && s.logger.OnBalanceChange != nil {
s.logger.OnBalanceChange(address, amount.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestructBurn)
if s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(address, amount.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestructBurn)
}
})
return s
}

func (s *stateDBLogger) AddBalance(address common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int {
prev := s.StateDB.AddBalance(address, amount, reason)
if s.logger != nil && s.logger.OnBalanceChange != nil {
if s.hooks.OnBalanceChange != nil {
newBalance := new(uint256.Int).Add(&prev, amount)
s.logger.OnBalanceChange(address, prev.ToBig(), newBalance.ToBig(), reason)
s.hooks.OnBalanceChange(address, prev.ToBig(), newBalance.ToBig(), reason)
}
return prev
}

func (s *stateDBLogger) SetNonce(address common.Address, nonce uint64) {
s.StateDB.SetNonce(address, nonce)
if s.logger != nil && s.logger.OnNonceChange != nil {
s.logger.OnNonceChange(address, nonce-1, nonce)
if s.hooks.OnNonceChange != nil {
s.hooks.OnNonceChange(address, nonce-1, nonce)
}
}

func (s *stateDBLogger) SetCode(address common.Address, code []byte) {
s.StateDB.SetCode(address, code)
if s.logger != nil && s.logger.OnCodeChange != nil {
s.logger.OnCodeChange(address, types.EmptyCodeHash, nil, crypto.Keccak256Hash(code), code)
if s.hooks.OnCodeChange != nil {
s.hooks.OnCodeChange(address, types.EmptyCodeHash, nil, crypto.Keccak256Hash(code), code)
}
}

func (s *stateDBLogger) SetState(address common.Address, key common.Hash, value common.Hash) common.Hash {
prev := s.StateDB.SetState(address, key, value)
if s.logger != nil && s.logger.OnStorageChange != nil && prev != value {
s.logger.OnStorageChange(address, key, prev, value)
if s.hooks.OnStorageChange != nil && prev != value {
s.hooks.OnStorageChange(address, key, prev, value)
}
return prev
}

func (s *stateDBLogger) SelfDestruct(address common.Address) uint256.Int {
prev := s.StateDB.SelfDestruct(address)
if !prev.IsZero() {
if s.logger != nil && s.logger.OnBalanceChange != nil {
s.logger.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
if s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
}
}
return prev
Expand All @@ -86,8 +88,8 @@ func (s *stateDBLogger) SelfDestruct(address common.Address) uint256.Int {
func (s *stateDBLogger) Selfdestruct6780(address common.Address) uint256.Int {
prev := s.StateDB.Selfdestruct6780(address)
if !prev.IsZero() {
if s.logger != nil && s.logger.OnBalanceChange != nil {
s.logger.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
if s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
}
}
return prev
Expand All @@ -96,7 +98,7 @@ func (s *stateDBLogger) Selfdestruct6780(address common.Address) uint256.Int {
func (s *stateDBLogger) AddLog(log *types.Log) {
// The inner will modify the log (add fields), so invoke that first
s.StateDB.AddLog(log)
if s.logger != nil && s.logger.OnLog != nil {
s.logger.OnLog(log)
if s.hooks.OnLog != nil {
s.hooks.OnLog(log)
}
}

0 comments on commit 0a4002e

Please sign in to comment.