Skip to content

Commit

Permalink
support flatCallTracer and result limit for trace transaction (#40)
Browse files Browse the repository at this point in the history
* support flatCallTracer

* trace result limit
  • Loading branch information
ylsGit authored Nov 14, 2023
1 parent 6de0441 commit 2bafd67
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
9 changes: 8 additions & 1 deletion jsonrpc/endpoints_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var defaultTraceConfig = &traceConfig{
EnableMemory: false,
EnableReturnData: false,
Tracer: nil,
Limit: 0,
}

// DebugEndpoints is the debug jsonrpc endpoint
Expand All @@ -54,6 +55,7 @@ type traceConfig struct {
EnableReturnData bool `json:"enableReturnData"`
Tracer *string `json:"tracer"`
TracerConfig json.RawMessage `json:"tracerConfig"`
Limit int `json:"limit"`
}

// StructLogRes represents the debug trace information for each opcode
Expand Down Expand Up @@ -313,6 +315,7 @@ func (d *DebugEndpoints) buildTraceTransaction(ctx context.Context, hash common.
EnableReturnData: traceCfg.EnableReturnData,
Tracer: traceCfg.Tracer,
TracerConfig: traceCfg.TracerConfig,
Limit: traceCfg.Limit,
}
result, err := d.state.DebugTransaction(ctx, hash, stateTraceConfig, dbTx)
if errors.Is(err, state.ErrNotFound) {
Expand Down Expand Up @@ -431,6 +434,10 @@ func (d *DebugEndpoints) buildStructLogs(stateStructLogs []instrumentation.Struc

structLogs = append(structLogs, structLogRes)
}

if cfg.Limit > 0 && len(structLogs) > cfg.Limit {
structLogs = structLogs[:cfg.Limit]
}
return structLogs
}

Expand All @@ -439,7 +446,7 @@ func (d *DebugEndpoints) buildStructLogs(stateStructLogs []instrumentation.Struc
func isBuiltInTracer(tracer string) bool {
// built-in tracers
switch tracer {
case "callTracer", "4byteTracer", "prestateTracer", "noopTracer":
case "callTracer", "flatCallTracer", "4byteTracer", "prestateTracer", "noopTracer":
return true
default:
return false
Expand Down
20 changes: 17 additions & 3 deletions state/runtime/instrumentation/tracers/native/call_flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
//go:generate go run github.com/fjl/gencodec -type flatCallResult -field-override flatCallResultMarshaling -out gen_flatcallresult_json.go

func init() {
tracers.DefaultDirectory.Register("flatCallTracer", newFlatCallTracer, false)
tracers.DefaultDirectory.Register("flatCallTracer", NewFlatCallTracer, false)
}

var parityErrorMapping = map[string]string{
Expand Down Expand Up @@ -113,15 +113,16 @@ type flatCallTracer struct {
ctx *tracers.Context // Holds tracer context data
reason error // Textual reason for the interruption
activePrecompiles []common.Address // Updated on CaptureStart based on given rules
limit int
}

type flatCallTracerConfig struct {
ConvertParityErrors bool `json:"convertParityErrors"` // If true, call tracer converts errors to parity format
IncludePrecompiles bool `json:"includePrecompiles"` // If true, call tracer includes calls to precompiled contracts
}

// newFlatCallTracer returns a new flatCallTracer.
func newFlatCallTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
// NewFlatCallTracer returns a new flatCallTracer.
func NewFlatCallTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
var config flatCallTracerConfig
if cfg != nil {
if err := json.Unmarshal(cfg, &config); err != nil {
Expand All @@ -141,6 +142,15 @@ func newFlatCallTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Trace
return &flatCallTracer{tracer: t, ctx: ctx, config: config}, nil
}

// SetFlatCallTracerLimit set the limit for flatCallFrame.
func SetFlatCallTracerLimit(t tracers.Tracer, l int) tracers.Tracer {
if flatTracer, ok := t.(*flatCallTracer); ok {
flatTracer.limit = l
return flatTracer
}
return t
}

// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
func (t *flatCallTracer) CaptureStart(env *fakevm.FakeEVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
t.tracer.CaptureStart(env, from, to, create, input, gas, value)
Expand Down Expand Up @@ -218,6 +228,10 @@ func (t *flatCallTracer) GetResult() (json.RawMessage, error) {
return nil, err
}

if t.limit > 0 && len(flat) > t.limit {
flat = flat[:t.limit]
}

res, err := json.Marshal(flat)
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions state/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has
log.Errorf("debug transaction: failed to create callTracer, err: %v", err)
return nil, fmt.Errorf("failed to create callTracer, err: %v", err)
}
} else if traceConfig.IsFlatCallTracer() {
customTracer, err = native.NewFlatCallTracer(tracerContext, traceConfig.TracerConfig)
if err != nil {
log.Errorf("debug transaction: failed to create flatCallTracer, err: %v", err)
return nil, fmt.Errorf("failed to create flatCallTracer, err: %v", err)
}
customTracer = native.SetFlatCallTracerLimit(customTracer, traceConfig.Limit)
} else if traceConfig.IsNoopTracer() {
customTracer, err = native.NewNoopTracer(tracerContext, traceConfig.TracerConfig)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type TraceConfig struct {
EnableReturnData bool
Tracer *string
TracerConfig json.RawMessage
Limit int
}

// IsDefaultTracer returns true when no custom tracer is set
Expand All @@ -200,6 +201,11 @@ func (t *TraceConfig) IsCallTracer() bool {
return t.Tracer != nil && *t.Tracer == "callTracer"
}

// IsFlatCallTracer returns true when should use flatCallTracer
func (t *TraceConfig) IsFlatCallTracer() bool {
return t.Tracer != nil && *t.Tracer == "flatCallTracer"
}

// IsNoopTracer returns true when should use noopTracer
func (t *TraceConfig) IsNoopTracer() bool {
return t.Tracer != nil && *t.Tracer == "noopTracer"
Expand Down

0 comments on commit 2bafd67

Please sign in to comment.