Skip to content

Commit

Permalink
vm: Unify CallTracer.GetResult methods
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Jul 3, 2024
1 parent ee11904 commit ff0eed4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
3 changes: 2 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2795,10 +2795,11 @@ func GetInternalTxTrace(tracer vm.Tracer) (*vm.InternalTxTrace, error) {
return nil, err
}
case *vm.CallTracer:
internalTxTrace, err = tracer.GetResultAsInternalTxTrace()
callTrace, err := tracer.GetResult()
if err != nil {
return nil, err
}
internalTxTrace = callTrace.ToInternalTxTrace()
default:
logger.Error("To trace internal transactions, VM tracer type should be vm.InternalTxTracer", "actualType", reflect.TypeOf(tracer).String())
return nil, ErrInvalidTracer
Expand Down
33 changes: 3 additions & 30 deletions blockchain/vm/call_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package vm

import (
"encoding/json"
"errors"
"math/big"
"sync/atomic"
Expand Down Expand Up @@ -220,39 +219,13 @@ func (t *CallTracer) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost, ccL
func (t *CallTracer) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost, ccLeft, ccOpcode uint64, scope *ScopeContext, depth int, err error) {
}

func (t *CallTracer) GetResult() ([]CallFrame, error) {
func (t *CallTracer) GetResult() (CallFrame, error) {
if len(t.callstack) != 1 {
return nil, errors.New("incorrect number of top-level calls")
return CallFrame{}, errors.New("incorrect number of top-level calls")
}

// Return with interrupt reason if any
return t.callstack, t.interruptReason
}

func (t *CallTracer) GetResultAsJson() (json.RawMessage, error) {
if len(t.callstack) != 1 {
return nil, errors.New("incorrect number of top-level calls")
}

result, err := json.Marshal(t.callstack[0])
if err != nil {
return nil, err
}

// Return with interrupt reason if any
return result, t.interruptReason
}

// Convert to the legacy InternalTxTrace type
func (t *CallTracer) GetResultAsInternalTxTrace() (*InternalTxTrace, error) {
if len(t.callstack) != 1 {
return nil, errors.New("incorrect number of top-level calls")
}

result := t.callstack[0].ToInternalTxTrace()

// Return with interrupt reason if any
return result, t.interruptReason
return t.callstack[0], t.interruptReason
}

// Stop terminates execution of the tracer at the first opportune moment.
Expand Down
7 changes: 5 additions & 2 deletions node/cn/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ func runTracer(t *testing.T, tc *tracerTestdata, tracer vm.Tracer) (*types.Trans
switch tracer := tracer.(type) {
case *Tracer:
tracerResult, err = tracer.GetResult()
require.NoError(t, err)
case *vm.CallTracer:
tracerResult, err = tracer.GetResultAsJson()
callFrame, err := tracer.GetResult()
require.NoError(t, err)
tracerResult, err = json.Marshal(callFrame)
require.NoError(t, err)
}
require.NoError(t, err)
assert.JSONEq(t, string(tc.Result), string(tracerResult))

return msg, execResult, tracerResult
Expand Down

0 comments on commit ff0eed4

Please sign in to comment.