2021-11-05 13:48:21 +03: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"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-09-29 12:36:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2021-11-05 13:48:21 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-09-26 19:35:44 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-11-05 13:48:21 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
|
|
|
)
|
|
|
|
|
2022-09-26 19:35:44 +03:00
|
|
|
//go:generate go run github.com/fjl/gencodec -type callFrame -field-override callFrameMarshaling -out gen_callframe_json.go
|
|
|
|
|
2021-11-05 13:48:21 +03:00
|
|
|
func init() {
|
2021-11-09 14:09:35 +03:00
|
|
|
register("callTracer", newCallTracer)
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
|
2022-11-01 14:03:26 +03:00
|
|
|
type callLog struct {
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
Topics []common.Hash `json:"topics"`
|
|
|
|
Data hexutil.Bytes `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:48:21 +03:00
|
|
|
type callFrame struct {
|
2022-09-29 12:36:07 +03:00
|
|
|
Type vm.OpCode `json:"-"`
|
|
|
|
From common.Address `json:"from"`
|
|
|
|
Gas uint64 `json:"gas"`
|
|
|
|
GasUsed uint64 `json:"gasUsed"`
|
|
|
|
To common.Address `json:"to,omitempty" rlp:"optional"`
|
|
|
|
Input []byte `json:"input" rlp:"optional"`
|
|
|
|
Output []byte `json:"output,omitempty" rlp:"optional"`
|
|
|
|
Error string `json:"error,omitempty" rlp:"optional"`
|
|
|
|
Revertal string `json:"revertReason,omitempty"`
|
|
|
|
Calls []callFrame `json:"calls,omitempty" rlp:"optional"`
|
2022-11-01 14:03:26 +03:00
|
|
|
Logs []callLog `json:"logs,omitempty" rlp:"optional"`
|
2022-09-26 19:35:44 +03:00
|
|
|
// Placed at end on purpose. The RLP will be decoded to 0 instead of
|
|
|
|
// nil if there are non-empty elements after in the struct.
|
|
|
|
Value *big.Int `json:"value,omitempty" rlp:"optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f callFrame) TypeString() string {
|
|
|
|
return f.Type.String()
|
|
|
|
}
|
|
|
|
|
2022-11-01 14:03:26 +03:00
|
|
|
func (f callFrame) failed() bool {
|
|
|
|
return len(f.Error) > 0
|
|
|
|
}
|
|
|
|
|
2022-11-08 12:16:52 +03:00
|
|
|
func (f *callFrame) processOutput(output []byte, err error) {
|
2022-10-31 11:50:21 +03:00
|
|
|
output = common.CopyBytes(output)
|
|
|
|
if err == nil {
|
|
|
|
f.Output = output
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Error = err.Error()
|
|
|
|
if f.Type == vm.CREATE || f.Type == vm.CREATE2 {
|
|
|
|
f.To = common.Address{}
|
|
|
|
}
|
|
|
|
if !errors.Is(err, vm.ErrExecutionReverted) || len(output) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Output = output
|
|
|
|
if len(output) < 4 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if unpacked, err := abi.UnpackRevert(output); err == nil {
|
|
|
|
f.Revertal = unpacked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 19:35:44 +03:00
|
|
|
type callFrameMarshaling struct {
|
|
|
|
TypeString string `json:"type"`
|
|
|
|
Gas hexutil.Uint64
|
|
|
|
GasUsed hexutil.Uint64
|
|
|
|
Value *hexutil.Big
|
|
|
|
Input hexutil.Bytes
|
|
|
|
Output hexutil.Bytes
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type callTracer struct {
|
2022-11-17 11:27:48 +03:00
|
|
|
noopTracer
|
2021-11-05 13:48:21 +03:00
|
|
|
callstack []callFrame
|
2022-08-09 12:04:57 +03:00
|
|
|
config callTracerConfig
|
2022-10-28 13:28:29 +03:00
|
|
|
gasLimit uint64
|
2021-11-05 13:48:21 +03:00
|
|
|
interrupt uint32 // Atomic flag to signal execution interruption
|
|
|
|
reason error // Textual reason for the interruption
|
|
|
|
}
|
|
|
|
|
2022-08-09 12:04:57 +03:00
|
|
|
type callTracerConfig struct {
|
|
|
|
OnlyTopCall bool `json:"onlyTopCall"` // If true, call tracer won't collect any subcalls
|
2022-11-01 14:03:26 +03:00
|
|
|
WithLog bool `json:"withLog"` // If true, call tracer will collect event logs
|
2022-08-09 12:04:57 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// newCallTracer returns a native go tracer which tracks
|
2021-11-05 13:48:21 +03:00
|
|
|
// call frames of a tx, and implements vm.EVMLogger.
|
2022-08-09 12:04:57 +03:00
|
|
|
func newCallTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
|
|
|
|
var config callTracerConfig
|
|
|
|
if cfg != nil {
|
|
|
|
if err := json.Unmarshal(cfg, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 13:48:21 +03:00
|
|
|
// First callframe contains tx context info
|
|
|
|
// and is populated on start and end.
|
2022-08-09 12:04:57 +03:00
|
|
|
return &callTracer{callstack: make([]callFrame, 1), config: config}, nil
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
|
2021-11-05 13:48:21 +03:00
|
|
|
func (t *callTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
|
|
|
t.callstack[0] = callFrame{
|
2022-09-26 19:35:44 +03:00
|
|
|
Type: vm.CALL,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Input: common.CopyBytes(input),
|
|
|
|
Gas: gas,
|
|
|
|
Value: value,
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
if create {
|
2022-09-26 19:35:44 +03:00
|
|
|
t.callstack[0].Type = vm.CREATE
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// CaptureEnd is called after the call finishes to finalize the tracing.
|
2021-11-05 13:48:21 +03:00
|
|
|
func (t *callTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
|
2022-11-08 12:16:52 +03:00
|
|
|
t.callstack[0].processOutput(output, err)
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:09:35 +03: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) {
|
2022-11-01 14:03:26 +03:00
|
|
|
// Only logs need to be captured via opcode processing
|
|
|
|
if !t.config.WithLog {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Avoid processing nested calls when only caring about top call
|
|
|
|
if t.config.OnlyTopCall && depth > 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
// Skip if tracing was interrupted
|
|
|
|
if atomic.LoadUint32(&t.interrupt) > 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-11-01 14:03:26 +03:00
|
|
|
switch op {
|
|
|
|
case vm.LOG0, vm.LOG1, vm.LOG2, vm.LOG3, vm.LOG4:
|
|
|
|
size := int(op - vm.LOG0)
|
|
|
|
|
|
|
|
stack := scope.Stack
|
|
|
|
stackData := stack.Data()
|
|
|
|
|
|
|
|
// Don't modify the stack
|
|
|
|
mStart := stackData[len(stackData)-1]
|
|
|
|
mSize := stackData[len(stackData)-2]
|
|
|
|
topics := make([]common.Hash, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
topic := stackData[len(stackData)-2-(i+1)]
|
|
|
|
topics[i] = common.Hash(topic.Bytes32())
|
|
|
|
}
|
|
|
|
|
|
|
|
data := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
|
|
|
|
log := callLog{Address: scope.Contract.Address(), Topics: topics, Data: hexutil.Bytes(data)}
|
|
|
|
t.callstack[len(t.callstack)-1].Logs = append(t.callstack[len(t.callstack)-1].Logs, log)
|
|
|
|
}
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
2021-11-05 13:48:21 +03:00
|
|
|
func (t *callTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
2022-08-09 12:04:57 +03:00
|
|
|
if t.config.OnlyTopCall {
|
|
|
|
return
|
|
|
|
}
|
2021-11-05 13:48:21 +03:00
|
|
|
// Skip if tracing was interrupted
|
|
|
|
if atomic.LoadUint32(&t.interrupt) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
call := callFrame{
|
2022-09-26 19:35:44 +03:00
|
|
|
Type: typ,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Input: common.CopyBytes(input),
|
|
|
|
Gas: gas,
|
|
|
|
Value: value,
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
|
|
|
t.callstack = append(t.callstack, call)
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// CaptureExit is called when EVM exits a scope, even if the scope didn't
|
|
|
|
// execute any code.
|
2021-11-05 13:48:21 +03:00
|
|
|
func (t *callTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
2022-08-09 12:04:57 +03:00
|
|
|
if t.config.OnlyTopCall {
|
|
|
|
return
|
|
|
|
}
|
2021-11-05 13:48:21 +03:00
|
|
|
size := len(t.callstack)
|
|
|
|
if size <= 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// pop call
|
|
|
|
call := t.callstack[size-1]
|
|
|
|
t.callstack = t.callstack[:size-1]
|
|
|
|
size -= 1
|
|
|
|
|
2022-09-26 19:35:44 +03:00
|
|
|
call.GasUsed = gasUsed
|
2022-11-08 12:16:52 +03:00
|
|
|
call.processOutput(output, err)
|
2021-11-05 13:48:21 +03:00
|
|
|
t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call)
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:28:29 +03:00
|
|
|
func (t *callTracer) CaptureTxStart(gasLimit uint64) {
|
|
|
|
t.gasLimit = gasLimit
|
|
|
|
}
|
2022-03-31 12:51:44 +03:00
|
|
|
|
2022-10-28 13:28:29 +03:00
|
|
|
func (t *callTracer) CaptureTxEnd(restGas uint64) {
|
|
|
|
t.callstack[0].GasUsed = t.gasLimit - restGas
|
2022-11-01 14:03:26 +03:00
|
|
|
if t.config.WithLog {
|
|
|
|
// Logs are not emitted when the call fails
|
|
|
|
clearFailedLogs(&t.callstack[0], false)
|
|
|
|
}
|
2022-10-28 13:28:29 +03:00
|
|
|
}
|
2022-03-31 12:51:44 +03:00
|
|
|
|
2021-11-09 14:09:35 +03: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 13:48:21 +03: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 14:09:35 +03:00
|
|
|
// Stop terminates execution of the tracer at the first opportune moment.
|
2021-11-05 13:48:21 +03:00
|
|
|
func (t *callTracer) Stop(err error) {
|
|
|
|
t.reason = err
|
|
|
|
atomic.StoreUint32(&t.interrupt, 1)
|
|
|
|
}
|
2022-11-01 14:03:26 +03:00
|
|
|
|
|
|
|
// clearFailedLogs clears the logs of a callframe and all its children
|
|
|
|
// in case of execution failure.
|
|
|
|
func clearFailedLogs(cf *callFrame, parentFailed bool) {
|
|
|
|
failed := cf.failed() || parentFailed
|
|
|
|
// Clear own logs
|
|
|
|
if failed {
|
|
|
|
cf.Logs = nil
|
|
|
|
}
|
|
|
|
for i := range cf.Calls {
|
|
|
|
clearFailedLogs(&cf.Calls[i], failed)
|
|
|
|
}
|
|
|
|
}
|