Skip to content

Commit

Permalink
Remove accounts that are never loaded into IBS from zero trace
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Aug 17, 2024
1 parent 88af806 commit b9df3f0
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions eth/tracers/native/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,27 @@ func init() {
}

type zeroTracer struct {
noopTracer // stub struct to mock not used interface methods
env *vm.EVM
tx types.TxnInfo
gasLimit uint64 // Amount of gas bought for the whole tx
interrupt atomic.Bool // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
ctx *tracers.Context
to *libcommon.Address
txStatus uint64
addrOpCodes map[libcommon.Address]map[vm.OpCode]struct{}
noopTracer // stub struct to mock not used interface methods
env *vm.EVM
tx types.TxnInfo
gasLimit uint64 // Amount of gas bought for the whole tx
interrupt atomic.Bool // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
ctx *tracers.Context
to *libcommon.Address
txStatus uint64
addrOpCodes map[libcommon.Address]map[vm.OpCode]struct{}
wasLoadedToIBS map[libcommon.Address]bool
}

func newZeroTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
return &zeroTracer{
tx: types.TxnInfo{
Traces: make(map[libcommon.Address]*types.TxnTrace),
},
ctx: ctx,
addrOpCodes: make(map[libcommon.Address]map[vm.OpCode]struct{}),
ctx: ctx,
addrOpCodes: make(map[libcommon.Address]map[vm.OpCode]struct{}),
wasLoadedToIBS: make(map[libcommon.Address]bool),
}, nil
}

Expand Down Expand Up @@ -174,7 +176,14 @@ func (t *zeroTracer) CaptureTxEnd(restGas uint64) {
t.tx.Meta.GasUsed = t.gasLimit - restGas
*t.ctx.CumulativeGasUsed += t.tx.Meta.GasUsed

toDelete := make([]libcommon.Address, 0)
for addr := range t.tx.Traces {
// If an account was never accessed through IntraBlockState, it means that never there was an OpCode that read into it or checks whether it exists in the state trie, and therefore we don't need the trace of it.
if _, ok := t.wasLoadedToIBS[addr]; !ok {
toDelete = append(toDelete, addr)
continue
}

trace := t.tx.Traces[addr]
hasLiveAccount := t.env.IntraBlockState().HasLiveAccount(addr)
newBalance := t.env.IntraBlockState().GetBalance(addr)
Expand Down Expand Up @@ -261,6 +270,10 @@ func (t *zeroTracer) CaptureTxEnd(restGas uint64) {
}
}

for _, addr := range toDelete {
delete(t.tx.Traces, addr)
}

receipt := &types.Receipt{Type: t.ctx.Txn.Type(), CumulativeGasUsed: *t.ctx.CumulativeGasUsed}
receipt.Status = t.txStatus
receipt.TxHash = t.ctx.Txn.Hash()
Expand Down Expand Up @@ -331,6 +344,11 @@ func (t *zeroTracer) Stop(err error) {
}

func (t *zeroTracer) addAccountToTrace(addr libcommon.Address) {
hasLiveAccount := t.env.IntraBlockState().HasLiveAccount(addr)
if hasLiveAccount {
t.wasLoadedToIBS[addr] = true
}

if _, ok := t.tx.Traces[addr]; ok {
return
}
Expand Down

0 comments on commit b9df3f0

Please sign in to comment.