Skip to content

Commit

Permalink
vm: chaindatafetcher uses vm.CallTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Jun 20, 2024
1 parent 7f8692e commit 50506c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,11 @@ func GetInternalTxTrace(tracer vm.Tracer) (*vm.InternalTxTrace, error) {
if err != nil {
return nil, err
}
case *vm.CallTracer:
internalTxTrace, err = tracer.GetResultAsInternalTxTrace()
if err != nil {
return nil, err
}
default:
logger.Error("To trace internal transactions, VM tracer type should be vm.InternalTxTracer", "actualType", reflect.TypeOf(tracer).String())
return nil, ErrInvalidTracer
Expand Down
44 changes: 44 additions & 0 deletions blockchain/vm/call_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ func (f CallFrame) TypeString() string { // to satisfy gencodec
return f.Type.String()
}

func (f CallFrame) ToInternalTxTrace() *InternalTxTrace {
t := &InternalTxTrace{}

t.Type = f.Type.String()
t.From = &f.From
t.To = f.To
if f.Value != nil {
t.Value = f.Value.Text(16)
}

t.Gas = f.Gas
t.GasUsed = f.GasUsed

if len(f.Input) > 0 {
t.Input = hexutil.Encode(f.Input)
}
if len(f.Output) > 0 {
t.Output = hexutil.Encode(f.Output)
}
t.Error = errors.New(f.Error)

t.Calls = make([]*InternalTxTrace, len(f.Calls))
for i, call := range f.Calls {
t.Calls[i] = call.ToInternalTxTrace()
}

t.RevertReason = f.RevertReason
t.Reverted = f.Reverted

return t
}

// FieldType overrides for callFrame that's used for JSON encoding
// Must rerun gencodec after modifying this struct
type callFrameMarshaling struct {
Expand Down Expand Up @@ -202,6 +234,18 @@ func (t *CallTracer) GetResult() (json.RawMessage, error) {
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
}

// Stop terminates execution of the tracer at the first opportune moment.
// For CallTracer, it stops at CaptureEnter, which is the most repetitive operation.
func (t *CallTracer) Stop(err error) {
Expand Down
2 changes: 1 addition & 1 deletion blockchain/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
// If internal transaction tracing is enabled, creates a tracer for a transaction
if vmConfig.EnableInternalTxTracing {
vmConfig.Debug = true
vmConfig.Tracer = NewInternalTxTracer()
vmConfig.Tracer = NewCallTracer()
}

evm.interpreter = NewEVMInterpreter(evm)
Expand Down
3 changes: 2 additions & 1 deletion blockchain/vm/internaltx_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ type InternalTxTrace struct {
Time time.Duration `json:"time,omitempty"`
Calls []*InternalTxTrace `json:"calls,omitempty"`

Reverted *RevertedInfo `json:"reverted,omitempty"`
RevertReason string `json:"revertReason,omitempty"`
Reverted *RevertedInfo `json:"reverted,omitempty"`
}

type RevertedInfo struct {
Expand Down

0 comments on commit 50506c6

Please sign in to comment.