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"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2022-09-29 04:36:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2021-11-05 05:48:21 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-09-26 11:35:44 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2024-03-22 12:53:53 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2021-11-05 05:48:21 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
|
|
|
)
|
|
|
|
|
2022-09-26 11:35:44 -05:00
|
|
|
//go:generate go run github.com/fjl/gencodec -type callFrame -field-override callFrameMarshaling -out gen_callframe_json.go
|
|
|
|
|
2021-11-05 05:48:21 -05:00
|
|
|
func init() {
|
2023-01-10 08:11:53 -06:00
|
|
|
tracers.DefaultDirectory.Register("callTracer", newCallTracer, false)
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
2022-11-01 06:03:26 -05:00
|
|
|
type callLog struct {
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
Topics []common.Hash `json:"topics"`
|
|
|
|
Data hexutil.Bytes `json:"data"`
|
2023-11-03 04:28:27 -05:00
|
|
|
// Position of the log relative to subcalls within the same trace
|
|
|
|
// See https://github.com/ethereum/go-ethereum/pull/28389 for details
|
|
|
|
Position hexutil.Uint `json:"position"`
|
2022-11-01 06:03:26 -05:00
|
|
|
}
|
|
|
|
|
2021-11-05 05:48:21 -05:00
|
|
|
type callFrame struct {
|
2023-03-07 07:39:11 -06: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"`
|
|
|
|
RevertReason string `json:"revertReason,omitempty"`
|
|
|
|
Calls []callFrame `json:"calls,omitempty" rlp:"optional"`
|
|
|
|
Logs []callLog `json:"logs,omitempty" rlp:"optional"`
|
2022-09-26 11:35:44 -05: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.
|
2024-03-22 12:53:53 -05:00
|
|
|
Value *big.Int `json:"value,omitempty" rlp:"optional"`
|
|
|
|
revertedSnapshot bool
|
2022-09-26 11:35:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f callFrame) TypeString() string {
|
|
|
|
return f.Type.String()
|
|
|
|
}
|
|
|
|
|
2022-11-01 06:03:26 -05:00
|
|
|
func (f callFrame) failed() bool {
|
2024-03-22 12:53:53 -05:00
|
|
|
return len(f.Error) > 0 && f.revertedSnapshot
|
2022-11-01 06:03:26 -05:00
|
|
|
}
|
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
func (f *callFrame) processOutput(output []byte, err error, reverted bool) {
|
2022-10-31 03:50:21 -05:00
|
|
|
output = common.CopyBytes(output)
|
2024-05-28 06:12:46 -05:00
|
|
|
// Clear error if tx wasn't reverted. This happened
|
|
|
|
// for pre-homestead contract storage OOG.
|
|
|
|
if err != nil && !reverted {
|
|
|
|
err = nil
|
|
|
|
}
|
2022-10-31 03:50:21 -05:00
|
|
|
if err == nil {
|
|
|
|
f.Output = output
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Error = err.Error()
|
2024-03-22 12:53:53 -05:00
|
|
|
f.revertedSnapshot = reverted
|
2022-10-31 03:50:21 -05:00
|
|
|
if f.Type == vm.CREATE || f.Type == vm.CREATE2 {
|
2023-03-07 07:39:11 -06:00
|
|
|
f.To = nil
|
2022-10-31 03:50:21 -05:00
|
|
|
}
|
|
|
|
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 {
|
2023-01-31 02:29:17 -06:00
|
|
|
f.RevertReason = unpacked
|
2022-10-31 03:50:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 11:35:44 -05: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 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type callTracer struct {
|
|
|
|
callstack []callFrame
|
2022-08-09 04:04:57 -05:00
|
|
|
config callTracerConfig
|
2022-10-28 05:28:29 -05:00
|
|
|
gasLimit uint64
|
2024-03-22 12:53:53 -05:00
|
|
|
depth int
|
2023-04-04 11:34:52 -05:00
|
|
|
interrupt atomic.Bool // Atomic flag to signal execution interruption
|
|
|
|
reason error // Textual reason for the interruption
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
2022-08-09 04:04:57 -05:00
|
|
|
type callTracerConfig struct {
|
|
|
|
OnlyTopCall bool `json:"onlyTopCall"` // If true, call tracer won't collect any subcalls
|
2022-11-01 06:03:26 -05:00
|
|
|
WithLog bool `json:"withLog"` // If true, call tracer will collect event logs
|
2022-08-09 04:04:57 -05:00
|
|
|
}
|
|
|
|
|
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.
|
2024-03-22 12:53:53 -05:00
|
|
|
func newCallTracer(ctx *tracers.Context, cfg json.RawMessage) (*tracers.Tracer, error) {
|
|
|
|
t, err := newCallTracerObject(ctx, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tracers.Tracer{
|
|
|
|
Hooks: &tracing.Hooks{
|
|
|
|
OnTxStart: t.OnTxStart,
|
|
|
|
OnTxEnd: t.OnTxEnd,
|
|
|
|
OnEnter: t.OnEnter,
|
|
|
|
OnExit: t.OnExit,
|
|
|
|
OnLog: t.OnLog,
|
|
|
|
},
|
|
|
|
GetResult: t.GetResult,
|
|
|
|
Stop: t.Stop,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCallTracerObject(ctx *tracers.Context, cfg json.RawMessage) (*callTracer, error) {
|
2022-08-09 04:04:57 -05:00
|
|
|
var config callTracerConfig
|
|
|
|
if cfg != nil {
|
|
|
|
if err := json.Unmarshal(cfg, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 05:48:21 -05:00
|
|
|
// First callframe contains tx context info
|
|
|
|
// and is populated on start and end.
|
2024-03-22 12:53:53 -05:00
|
|
|
return &callTracer{callstack: make([]callFrame, 0, 1), config: config}, nil
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
// OnEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
|
|
|
func (t *callTracer) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
|
|
|
t.depth = depth
|
|
|
|
if t.config.OnlyTopCall && depth > 0 {
|
2022-08-09 04:04:57 -05:00
|
|
|
return
|
|
|
|
}
|
2021-11-05 05:48:21 -05:00
|
|
|
// Skip if tracing was interrupted
|
2023-04-04 11:34:52 -05:00
|
|
|
if t.interrupt.Load() {
|
2021-11-05 05:48:21 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-07 07:39:11 -06:00
|
|
|
toCopy := to
|
2021-11-05 05:48:21 -05:00
|
|
|
call := callFrame{
|
2024-03-22 12:53:53 -05:00
|
|
|
Type: vm.OpCode(typ),
|
2022-09-26 11:35:44 -05:00
|
|
|
From: from,
|
2023-03-07 07:39:11 -06:00
|
|
|
To: &toCopy,
|
2022-09-26 11:35:44 -05:00
|
|
|
Input: common.CopyBytes(input),
|
|
|
|
Gas: gas,
|
|
|
|
Value: value,
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
2024-03-22 12:53:53 -05:00
|
|
|
if depth == 0 {
|
|
|
|
call.Gas = t.gasLimit
|
|
|
|
}
|
2021-11-05 05:48:21 -05:00
|
|
|
t.callstack = append(t.callstack, call)
|
|
|
|
}
|
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
// OnExit is called when EVM exits a scope, even if the scope didn't
|
2021-11-09 05:09:35 -06:00
|
|
|
// execute any code.
|
2024-03-22 12:53:53 -05:00
|
|
|
func (t *callTracer) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
|
|
|
|
if depth == 0 {
|
|
|
|
t.captureEnd(output, gasUsed, err, reverted)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.depth = depth - 1
|
2022-08-09 04:04:57 -05:00
|
|
|
if t.config.OnlyTopCall {
|
|
|
|
return
|
|
|
|
}
|
2024-03-22 12:53:53 -05:00
|
|
|
|
2021-11-05 05:48:21 -05:00
|
|
|
size := len(t.callstack)
|
|
|
|
if size <= 1 {
|
|
|
|
return
|
|
|
|
}
|
2024-03-22 12:53:53 -05:00
|
|
|
// Pop call.
|
2021-11-05 05:48:21 -05:00
|
|
|
call := t.callstack[size-1]
|
|
|
|
t.callstack = t.callstack[:size-1]
|
|
|
|
size -= 1
|
|
|
|
|
2022-09-26 11:35:44 -05:00
|
|
|
call.GasUsed = gasUsed
|
2024-03-22 12:53:53 -05:00
|
|
|
call.processOutput(output, err, reverted)
|
|
|
|
// Nest call into parent.
|
2021-11-05 05:48:21 -05:00
|
|
|
t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call)
|
|
|
|
}
|
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
func (t *callTracer) captureEnd(output []byte, gasUsed uint64, err error, reverted bool) {
|
|
|
|
if len(t.callstack) != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.callstack[0].processOutput(output, err, reverted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *callTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
|
|
|
|
t.gasLimit = tx.Gas()
|
2022-10-28 05:28:29 -05:00
|
|
|
}
|
2022-03-31 04:51:44 -05:00
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
func (t *callTracer) OnTxEnd(receipt *types.Receipt, err error) {
|
|
|
|
// Error happened during tx validation.
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.callstack[0].GasUsed = receipt.GasUsed
|
2022-11-01 06:03:26 -05:00
|
|
|
if t.config.WithLog {
|
|
|
|
// Logs are not emitted when the call fails
|
|
|
|
clearFailedLogs(&t.callstack[0], false)
|
|
|
|
}
|
2022-10-28 05:28:29 -05:00
|
|
|
}
|
2022-03-31 04:51:44 -05:00
|
|
|
|
2024-03-22 12:53:53 -05:00
|
|
|
func (t *callTracer) OnLog(log *types.Log) {
|
|
|
|
// 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 && t.depth > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Skip if tracing was interrupted
|
|
|
|
if t.interrupt.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l := callLog{
|
|
|
|
Address: log.Address,
|
|
|
|
Topics: log.Topics,
|
|
|
|
Data: log.Data,
|
|
|
|
Position: hexutil.Uint(len(t.callstack[len(t.callstack)-1].Calls)),
|
|
|
|
}
|
|
|
|
t.callstack[len(t.callstack)-1].Logs = append(t.callstack[len(t.callstack)-1].Logs, l)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
2023-02-28 04:24:37 -06:00
|
|
|
|
2021-11-05 05:48:21 -05:00
|
|
|
res, err := json.Marshal(t.callstack[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-22 12:53:53 -05:00
|
|
|
return res, t.reason
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
|
|
|
|
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
|
2023-04-04 11:34:52 -05:00
|
|
|
t.interrupt.Store(true)
|
2021-11-05 05:48:21 -05:00
|
|
|
}
|
2022-11-01 06:03:26 -05: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)
|
|
|
|
}
|
|
|
|
}
|