Skip to content

Commit b0dd86b

Browse files
committed
revert tracer logger
1 parent 9422f86 commit b0dd86b

File tree

8 files changed

+92
-978
lines changed

8 files changed

+92
-978
lines changed

core/types/l2trace.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type StructLogRes struct {
8585
Depth int `json:"depth"`
8686
Error string `json:"error,omitempty"`
8787
Stack []string `json:"stack,omitempty"`
88+
ReturnData string `json:"returnData,omitempty"`
8889
Memory []string `json:"memory,omitempty"`
8990
Storage map[string]string `json:"storage,omitempty"`
9091
RefundCounter uint64 `json:"refund,omitempty"`

core/vm/logger.go

+89-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ package vm
1919
import (
2020
"bytes"
2121
"encoding/hex"
22+
"encoding/json"
2223
"fmt"
2324
"io"
2425
"math/big"
2526
"strings"
27+
"sync/atomic"
2628
"time"
2729

2830
"github.com/holiman/uint256"
@@ -165,6 +167,14 @@ type StructLogger struct {
165167
logs []*StructLog
166168
output []byte
167169
err error
170+
171+
gasLimit uint64
172+
usedGas uint64
173+
174+
interrupt atomic.Bool // Atomic flag to signal execution interruption
175+
reason error // Textual reason for the interruption
176+
177+
ResultL1DataFee *big.Int
168178
}
169179

170180
// NewStructLogger returns a new logger
@@ -217,6 +227,11 @@ func (l *StructLogger) CaptureStart(env *EVM, from common.Address, to common.Add
217227
//
218228
// CaptureState also tracks SLOAD/SSTORE ops to track storage change.
219229
func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, opErr error) {
230+
// If tracing was interrupted, set the error and stop
231+
if l.interrupt.Load() {
232+
return
233+
}
234+
220235
memory := scope.Memory
221236
stack := scope.Stack
222237
contract := scope.Contract
@@ -343,9 +358,13 @@ func (l *StructLogger) CaptureExit(output []byte, gasUsed uint64, err error) {
343358

344359
}
345360

346-
func (t *StructLogger) CaptureTxStart(gasLimit uint64) {}
361+
func (l *StructLogger) CaptureTxStart(gasLimit uint64) {
362+
l.gasLimit = gasLimit
363+
}
347364

348-
func (t *StructLogger) CaptureTxEnd(restGas uint64) {}
365+
func (l *StructLogger) CaptureTxEnd(restGas uint64) {
366+
l.usedGas = l.gasLimit - restGas
367+
}
349368

350369
// UpdatedAccounts is used to collect all "touched" accounts
351370
func (l *StructLogger) UpdatedAccounts() map[common.Address]struct{} {
@@ -374,6 +393,33 @@ func (l *StructLogger) Error() error { return l.err }
374393
// Output returns the VM return value captured by the trace.
375394
func (l *StructLogger) Output() []byte { return l.output }
376395

396+
func (l *StructLogger) GetResult() (json.RawMessage, error) {
397+
// Tracing aborted
398+
if l.reason != nil {
399+
return nil, l.reason
400+
}
401+
failed := l.err != nil
402+
returnData := common.CopyBytes(l.output)
403+
// Return data when successful and revert reason when reverted, otherwise empty.
404+
returnVal := fmt.Sprintf("%x", returnData)
405+
if failed && l.err != ErrExecutionReverted {
406+
returnVal = ""
407+
}
408+
return json.Marshal(&types.ExecutionResult{
409+
Gas: l.usedGas,
410+
Failed: failed,
411+
ReturnValue: returnVal,
412+
StructLogs: formatLogs(l.StructLogs()),
413+
L1DataFee: (*hexutil.Big)(l.ResultL1DataFee),
414+
})
415+
}
416+
417+
// Stop terminates execution of the tracer at the first opportune moment.
418+
func (l *StructLogger) Stop(err error) {
419+
l.reason = err
420+
l.interrupt.Store(true)
421+
}
422+
377423
// WriteTrace writes a formatted trace to the given writer
378424
func WriteTrace(writer io.Writer, logs []*StructLog) {
379425
for _, log := range logs {
@@ -522,3 +568,44 @@ func FormatLogs(logs []*StructLog) []*types.StructLogRes {
522568
}
523569
return formatted
524570
}
571+
572+
// formatLogs formats EVM returned structured logs for json output
573+
func formatLogs(logs []*StructLog) []*types.StructLogRes {
574+
formatted := make([]*types.StructLogRes, len(logs))
575+
for index, trace := range logs {
576+
formatted[index] = &types.StructLogRes{
577+
Pc: trace.Pc,
578+
Op: trace.Op.String(),
579+
Gas: trace.Gas,
580+
GasCost: trace.GasCost,
581+
Depth: trace.Depth,
582+
Error: trace.ErrorString(),
583+
RefundCounter: trace.RefundCounter,
584+
}
585+
if trace.Stack != nil {
586+
stack := make([]string, len(trace.Stack))
587+
for i, stackValue := range trace.Stack {
588+
stack[i] = stackValue.Hex()
589+
}
590+
formatted[index].Stack = stack
591+
}
592+
if trace.ReturnData.Len() > 0 {
593+
formatted[index].ReturnData = hexutil.Bytes(trace.ReturnData.Bytes()).String()
594+
}
595+
if trace.Memory.Len() > 0 {
596+
memory := make([]string, 0, (trace.Memory.Len()+31)/32)
597+
for i := 0; i+32 <= trace.Memory.Len(); i += 32 {
598+
memory = append(memory, fmt.Sprintf("%x", trace.Memory.Bytes()[i:i+32]))
599+
}
600+
formatted[index].Memory = memory
601+
}
602+
if trace.Storage != nil {
603+
storage := make(map[string]string)
604+
for i, storageValue := range trace.Storage {
605+
storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue)
606+
}
607+
formatted[index].Storage = storage
608+
}
609+
}
610+
return formatted
611+
}

eth/tracers/api.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/morph-l2/go-ethereum/core/state"
3939
"github.com/morph-l2/go-ethereum/core/types"
4040
"github.com/morph-l2/go-ethereum/core/vm"
41-
"github.com/morph-l2/go-ethereum/eth/tracers/logger"
4241
"github.com/morph-l2/go-ethereum/ethdb"
4342
"github.com/morph-l2/go-ethereum/internal/ethapi"
4443
"github.com/morph-l2/go-ethereum/log"
@@ -927,7 +926,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex
927926
config = &TraceConfig{}
928927
}
929928
// Default tracer is the struct logger
930-
tracer = logger.NewStructLogger(config.LogConfig)
929+
tracer = vm.NewStructLogger(config.LogConfig)
931930
if config.Tracer != nil {
932931
tracer, err = New(*config.Tracer, txctx, config.TracerConfig)
933932
if err != nil {
@@ -965,7 +964,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex
965964
return nil, fmt.Errorf("tracing failed: %w", err)
966965
}
967966

968-
l, ok := tracer.(*logger.StructLogger)
967+
l, ok := tracer.(*vm.StructLogger)
969968
if ok {
970969
l.ResultL1DataFee = result.L1DataFee
971970
}

eth/tracers/logger/access_list_tracer.go

-183
This file was deleted.

0 commit comments

Comments
 (0)