@@ -471,100 +471,75 @@ func (k *Keeper) traceTx(
471471) (* interface {}, uint , error ) {
472472 // Assemble the structured logger or the JavaScript tracer
473473 var (
474- tracer vm. EVMLogger
474+ tracer tracers. Tracer
475475 overrides * ethparams.ChainConfig
476476 err error
477+ timeout = defaultTraceTimeout
477478 )
478-
479479 msg , err := tx .AsMessage (signer , cfg .BaseFee )
480480 if err != nil {
481481 return nil , 0 , status .Error (codes .Internal , err .Error ())
482482 }
483483
484- if traceConfig != nil && traceConfig .Overrides != nil {
484+ if traceConfig == nil {
485+ traceConfig = & types.TraceConfig {}
486+ }
487+
488+ if traceConfig .Overrides != nil {
485489 overrides = traceConfig .Overrides .EthereumConfig (cfg .ChainConfig .ChainID )
486490 }
487491
488- switch {
489- case traceConfig != nil && traceConfig .Tracer != "" :
490- timeout := defaultTraceTimeout
491- // TODO: change timeout to time.duration
492- // Used string to comply with go ethereum
493- if traceConfig .Timeout != "" {
494- timeout , err = time .ParseDuration (traceConfig .Timeout )
495- if err != nil {
496- return nil , 0 , status .Errorf (codes .InvalidArgument , "timeout value: %s" , err .Error ())
497- }
498- }
492+ logConfig := logger.Config {
493+ EnableMemory : traceConfig .EnableMemory ,
494+ DisableStorage : traceConfig .DisableStorage ,
495+ DisableStack : traceConfig .DisableStack ,
496+ EnableReturnData : traceConfig .EnableReturnData ,
497+ Debug : traceConfig .Debug ,
498+ Limit : int (traceConfig .Limit ),
499+ Overrides : overrides ,
500+ }
499501
500- tCtx := & tracers.Context {
501- BlockHash : txConfig .BlockHash ,
502- TxIndex : int (txConfig .TxIndex ),
503- TxHash : txConfig .TxHash ,
504- }
502+ tracer = logger .NewStructLogger (& logConfig )
505503
506- // Construct the JavaScript tracer to execute with
504+ tCtx := & tracers.Context {
505+ BlockHash : txConfig .BlockHash ,
506+ TxIndex : int (txConfig .TxIndex ),
507+ TxHash : txConfig .TxHash ,
508+ }
509+
510+ if traceConfig .Tracer != "" {
507511 if tracer , err = tracers .New (traceConfig .Tracer , tCtx ); err != nil {
508512 return nil , 0 , status .Error (codes .Internal , err .Error ())
509513 }
514+ }
510515
511- // Handle timeouts and RPC cancellations
512- deadlineCtx , cancel := context .WithTimeout (ctx .Context (), timeout )
513- defer cancel ()
514-
515- go func () {
516- <- deadlineCtx .Done ()
517- if errors .Is (deadlineCtx .Err (), context .DeadlineExceeded ) {
518- tracer .(tracers.Tracer ).Stop (errors .New ("execution timeout" ))
519- }
520- }()
521-
522- case traceConfig != nil :
523- logConfig := logger.Config {
524- EnableMemory : traceConfig .EnableMemory ,
525- DisableStorage : traceConfig .DisableStorage ,
526- DisableStack : traceConfig .DisableStack ,
527- EnableReturnData : traceConfig .EnableReturnData ,
528- Debug : traceConfig .Debug ,
529- Limit : int (traceConfig .Limit ),
530- Overrides : overrides ,
516+ // Define a meaningful timeout of a single transaction trace
517+ if traceConfig .Timeout != "" {
518+ if timeout , err = time .ParseDuration (traceConfig .Timeout ); err != nil {
519+ return nil , 0 , status .Errorf (codes .InvalidArgument , "timeout value: %s" , err .Error ())
531520 }
532- tracer = logger .NewStructLogger (& logConfig )
533- default :
534- tracer = types .NewTracer (types .TracerStruct , msg , cfg .ChainConfig , ctx .BlockHeight ())
535521 }
536522
523+ // Handle timeouts and RPC cancellations
524+ deadlineCtx , cancel := context .WithTimeout (ctx .Context (), timeout )
525+ defer cancel ()
526+
527+ go func () {
528+ <- deadlineCtx .Done ()
529+ if errors .Is (deadlineCtx .Err (), context .DeadlineExceeded ) {
530+ tracer .Stop (errors .New ("execution timeout" ))
531+ }
532+ }()
533+
537534 res , err := k .ApplyMessageWithConfig (ctx , msg , tracer , commitMessage , cfg , txConfig )
538535 if err != nil {
539536 return nil , 0 , status .Error (codes .Internal , err .Error ())
540537 }
541538
542539 var result interface {}
543-
544- // Depending on the tracer type, format and return the trace result data.
545- switch tracer := tracer .(type ) {
546- case * logger.StructLogger :
547- returnVal := ""
548- revert := res .Revert ()
549- if len (revert ) > 0 {
550- returnVal = fmt .Sprintf ("%x" , revert )
551- } else {
552- returnVal = fmt .Sprintf ("%x" , res .Return ())
553- }
554- result = types.ExecutionResult {
555- Gas : res .GasUsed ,
556- Failed : res .Failed (),
557- ReturnValue : returnVal ,
558- StructLogs : types .FormatLogs (tracer .StructLogs ()),
559- }
560- case tracers.Tracer :
561- result , err = tracer .GetResult ()
562- if err != nil {
563- return nil , 0 , status .Error (codes .Internal , err .Error ())
564- }
565-
566- default :
567- return nil , 0 , status .Errorf (codes .InvalidArgument , "invalid tracer type %T" , tracer )
540+ result , err = tracer .GetResult ()
541+ if err != nil {
542+ return nil , 0 , status .Error (codes .Internal , err .Error ())
568543 }
569544
570545 return & result , txConfig .LogIndex + uint (len (res .Logs )), nil
0 commit comments