Skip to content

Commit

Permalink
add firehose handling of SetBalance, add compat for SetNonce for syst…
Browse files Browse the repository at this point in the history
…emtransactions
  • Loading branch information
sduchesneau committed Feb 11, 2025
1 parent f773b89 commit 113e2d3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 3 additions & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,13 @@ func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tr
return stateObject.SetBalance(new(uint256.Int).Sub(stateObject.Balance(), amount))
}

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) (changed bool) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.SetBalance(amount)
return true
}
return false
}

func (s *StateDB) SetNonce(addr common.Address, nonce uint64, reason tracing.NonceChangeReason) {
Expand Down
12 changes: 10 additions & 2 deletions core/state/statedb_hooked.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ func (s *hookedStateDB) GetBalance(addr common.Address) *uint256.Int {
return s.inner.GetBalance(addr)
}

func (s *hookedStateDB) SetBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) {
s.inner.SetBalance(addr, amount, reason)
func (s *hookedStateDB) SetBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) (changed bool) {
var prev *uint256.Int
if s.hooks.OnBalanceChange != nil {
prev = s.inner.GetBalance(addr)
}
changed = s.inner.SetBalance(addr, amount, reason)
if changed && s.hooks.OnBalanceChange != nil {
s.hooks.OnBalanceChange(addr, prev.ToBig(), amount.ToBig(), reason)
}
return
}

func (s *hookedStateDB) GetNonce(addr common.Address) uint64 {
Expand Down
2 changes: 1 addition & 1 deletion core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type StateDB interface {
SubBalance(common.Address, *uint256.Int, tracing.BalanceChangeReason) uint256.Int
AddBalance(common.Address, *uint256.Int, tracing.BalanceChangeReason) uint256.Int
GetBalance(common.Address) *uint256.Int
SetBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason)
SetBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) bool

GetNonce(common.Address) uint64
SetNonce(common.Address, uint64, tracing.NonceChangeReason)
Expand Down
26 changes: 26 additions & 0 deletions eth/tracers/firehose.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Firehose struct {
transaction *pbeth.TransactionTrace
transactionLogIndex uint32
inSystemCall bool
inSystemTx bool
transactionIsolated bool
transactionTransient *pbeth.TransactionTrace

Expand Down Expand Up @@ -518,6 +519,20 @@ func (f *Firehose) reorderCallOrdinals(call *pbeth.Call, ordinalBase uint64) (or
return call.EndOrdinal
}

func (f *Firehose) OnSystemTxStart() {
firehoseInfo("system tx start")
f.ensureInBlockAndNotInTrx()

f.inSystemTx = true
}

func (f *Firehose) OnSystemTxEnd() {
firehoseInfo("system tx end")
f.ensureInSystemTx()

f.inSystemTx = false
}

func (f *Firehose) OnSystemCallStart() {
firehoseInfo("system call start")
f.ensureInBlockAndNotInTrx()
Expand Down Expand Up @@ -1278,6 +1293,11 @@ func (f *Firehose) newBalanceChange(tag string, address common.Address, oldValue
func (f *Firehose) OnNonceChange(a common.Address, prev, new uint64) {
f.ensureInBlockAndInTrx()

if *f.applyBackwardCompatibility && f.inSystemTx {
// Known Firehose issue: The nonce changes for system transactions are not recorded in the old Firehose instrumentation
return
}

activeCall := f.callStack.Peek()
change := &pbeth.NonceChange{
Address: a.Bytes(),
Expand Down Expand Up @@ -1559,6 +1579,12 @@ func (f *Firehose) ensureInSystemCall() {
}
}

func (f *Firehose) ensureInSystemTx() {
if !f.inSystemTx {
f.panicInvalidState("caller expected to be in system transaction state but we were not, this is a bug", 2)
}
}

func (f *Firehose) panicInvalidState(msg string, callerSkip int) string {
caller := "N/A"
if _, file, line, ok := runtime.Caller(callerSkip); ok {
Expand Down

0 comments on commit 113e2d3

Please sign in to comment.