2021-11-05 05:48:21 -05:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-11-09 05:09:35 -06:00
|
|
|
register("callTracer", newCallTracer)
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type callFrame struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to,omitempty"`
|
|
|
|
Value string `json:"value,omitempty"`
|
|
|
|
Gas string `json:"gas"`
|
|
|
|
GasUsed string `json:"gasUsed"`
|
|
|
|
Input string `json:"input"`
|
|
|
|
Output string `json:"output,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Calls []callFrame `json:"calls,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type callTracer struct {
|
2021-11-09 05:09:35 -06:00
|
|
|
env *vm.EVM
|
2021-11-05 05:48:21 -05:00
|
|
|
callstack []callFrame
|
|
|
|
interrupt uint32 // Atomic flag to signal execution interruption
|
|
|
|
reason error // Textual reason for the interruption
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// newCallTracer returns a native go tracer which tracks
|
2021-11-05 05:48:21 -05:00
|
|
|
// call frames of a tx, and implements vm.EVMLogger.
|
2021-11-09 05:09:35 -06:00
|
|
|
func newCallTracer() tracers.Tracer {
|
2021-11-05 05:48:21 -05:00
|
|
|
// First callframe contains tx context info
|
|
|
|
// and is populated on start and end.
|
2022-01-31 02:51:17 -06:00
|
|
|
return &callTracer{callstack: make([]callFrame, 1)}
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
|
2021-11-05 05:48:21 -05:00
|
|
|
func (t *callTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
2021-11-09 05:09:35 -06:00
|
|
|
t.env = env
|
2021-11-05 05:48:21 -05:00
|
|
|
t.callstack[0] = callFrame{
|
|
|
|
Type: "CALL",
|
|
|
|
From: addrToHex(from),
|
|
|
|
To: addrToHex(to),
|
|
|
|
Input: bytesToHex(input),
|
|
|
|
Gas: uintToHex(gas),
|
|
|
|
Value: bigToHex(value),
|
|
|
|
}
|
|
|
|
if create {
|
|
|
|
t.callstack[0].Type = "CREATE"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// CaptureEnd is called after the call finishes to finalize the tracing.
|
2021-11-05 05:48:21 -05:00
|
|
|
func (t *callTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
|
|
|
|
t.callstack[0].GasUsed = uintToHex(gasUsed)
|
|
|
|
if err != nil {
|
|
|
|
t.callstack[0].Error = err.Error()
|
|
|
|
if err.Error() == "execution reverted" && len(output) > 0 {
|
|
|
|
t.callstack[0].Output = bytesToHex(output)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.callstack[0].Output = bytesToHex(output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// CaptureState implements the EVMLogger interface to trace a single step of VM execution.
|
|
|
|
func (t *callTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// CaptureFault implements the EVMLogger interface to trace an execution fault.
|
|
|
|
func (t *callTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, _ *vm.ScopeContext, depth int, err error) {
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
2021-11-05 05:48:21 -05:00
|
|
|
func (t *callTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
|
|
|
// Skip if tracing was interrupted
|
|
|
|
if atomic.LoadUint32(&t.interrupt) > 0 {
|
2021-11-09 05:09:35 -06:00
|
|
|
t.env.Cancel()
|
2021-11-05 05:48:21 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
call := callFrame{
|
|
|
|
Type: typ.String(),
|
|
|
|
From: addrToHex(from),
|
|
|
|
To: addrToHex(to),
|
|
|
|
Input: bytesToHex(input),
|
|
|
|
Gas: uintToHex(gas),
|
|
|
|
Value: bigToHex(value),
|
|
|
|
}
|
|
|
|
t.callstack = append(t.callstack, call)
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// CaptureExit is called when EVM exits a scope, even if the scope didn't
|
|
|
|
// execute any code.
|
2021-11-05 05:48:21 -05:00
|
|
|
func (t *callTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
|
|
|
size := len(t.callstack)
|
|
|
|
if size <= 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// pop call
|
|
|
|
call := t.callstack[size-1]
|
|
|
|
t.callstack = t.callstack[:size-1]
|
|
|
|
size -= 1
|
|
|
|
|
|
|
|
call.GasUsed = uintToHex(gasUsed)
|
|
|
|
if err == nil {
|
|
|
|
call.Output = bytesToHex(output)
|
|
|
|
} else {
|
|
|
|
call.Error = err.Error()
|
|
|
|
if call.Type == "CREATE" || call.Type == "CREATE2" {
|
|
|
|
call.To = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call)
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// GetResult returns the json-encoded nested list of call traces, and any
|
|
|
|
// error arising from the encoding or forceful termination (via `Stop`).
|
2021-11-05 05:48:21 -05:00
|
|
|
func (t *callTracer) GetResult() (json.RawMessage, error) {
|
|
|
|
if len(t.callstack) != 1 {
|
|
|
|
return nil, errors.New("incorrect number of top-level calls")
|
|
|
|
}
|
|
|
|
res, err := json.Marshal(t.callstack[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.RawMessage(res), t.reason
|
|
|
|
}
|
|
|
|
|
2021-11-09 05:09:35 -06:00
|
|
|
// Stop terminates execution of the tracer at the first opportune moment.
|
2021-11-05 05:48:21 -05:00
|
|
|
func (t *callTracer) Stop(err error) {
|
|
|
|
t.reason = err
|
|
|
|
atomic.StoreUint32(&t.interrupt, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bytesToHex(s []byte) string {
|
|
|
|
return "0x" + common.Bytes2Hex(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bigToHex(n *big.Int) string {
|
|
|
|
if n == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "0x" + n.Text(16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uintToHex(n uint64) string {
|
|
|
|
return "0x" + strconv.FormatUint(n, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addrToHex(a common.Address) string {
|
|
|
|
return strings.ToLower(a.Hex())
|
|
|
|
}
|