2022-11-08 12:16:52 +03:00
|
|
|
// Copyright 2022 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"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-03-22 20:53:53 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-11-08 12:16:52 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-01-10 17:11:53 +03:00
|
|
|
tracers.DefaultDirectory.Register("muxTracer", newMuxTracer, false)
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// muxTracer is a go implementation of the Tracer interface which
|
|
|
|
// runs multiple tracers in one go.
|
|
|
|
type muxTracer struct {
|
|
|
|
names []string
|
2024-03-22 20:53:53 +03:00
|
|
|
tracers []*tracers.Tracer
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newMuxTracer returns a new mux tracer.
|
2024-03-22 20:53:53 +03:00
|
|
|
func newMuxTracer(ctx *tracers.Context, cfg json.RawMessage) (*tracers.Tracer, error) {
|
2022-11-08 12:16:52 +03:00
|
|
|
var config map[string]json.RawMessage
|
|
|
|
if cfg != nil {
|
|
|
|
if err := json.Unmarshal(cfg, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-03-22 20:53:53 +03:00
|
|
|
objects := make([]*tracers.Tracer, 0, len(config))
|
2022-11-08 12:16:52 +03:00
|
|
|
names := make([]string, 0, len(config))
|
|
|
|
for k, v := range config {
|
2023-01-10 17:11:53 +03:00
|
|
|
t, err := tracers.DefaultDirectory.New(k, ctx, v)
|
2022-11-08 12:16:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
objects = append(objects, t)
|
|
|
|
names = append(names, k)
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
t := &muxTracer{names: names, tracers: objects}
|
|
|
|
return &tracers.Tracer{
|
|
|
|
Hooks: &tracing.Hooks{
|
|
|
|
OnTxStart: t.OnTxStart,
|
|
|
|
OnTxEnd: t.OnTxEnd,
|
|
|
|
OnEnter: t.OnEnter,
|
|
|
|
OnExit: t.OnExit,
|
|
|
|
OnOpcode: t.OnOpcode,
|
|
|
|
OnFault: t.OnFault,
|
|
|
|
OnGasChange: t.OnGasChange,
|
|
|
|
OnBalanceChange: t.OnBalanceChange,
|
|
|
|
OnNonceChange: t.OnNonceChange,
|
|
|
|
OnCodeChange: t.OnCodeChange,
|
|
|
|
OnStorageChange: t.OnStorageChange,
|
|
|
|
OnLog: t.OnLog,
|
|
|
|
},
|
|
|
|
GetResult: t.GetResult,
|
|
|
|
Stop: t.Stop,
|
|
|
|
}, nil
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnOpcode != nil {
|
|
|
|
t.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *muxTracer) OnFault(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
|
|
|
|
for _, t := range t.tracers {
|
|
|
|
if t.OnFault != nil {
|
|
|
|
t.OnFault(pc, op, gas, cost, scope, depth, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *muxTracer) OnGasChange(old, new uint64, reason tracing.GasChangeReason) {
|
|
|
|
for _, t := range t.tracers {
|
|
|
|
if t.OnGasChange != nil {
|
|
|
|
t.OnGasChange(old, new, reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *muxTracer) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
|
|
|
for _, t := range t.tracers {
|
|
|
|
if t.OnEnter != nil {
|
|
|
|
t.OnEnter(depth, typ, from, to, input, gas, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *muxTracer) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
|
|
|
|
for _, t := range t.tracers {
|
|
|
|
if t.OnExit != nil {
|
|
|
|
t.OnExit(depth, output, gasUsed, err, reverted)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnTxStart != nil {
|
|
|
|
t.OnTxStart(env, tx, from)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnTxEnd(receipt *types.Receipt, err error) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnTxEnd != nil {
|
|
|
|
t.OnTxEnd(receipt, err)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnBalanceChange(a common.Address, prev, new *big.Int, reason tracing.BalanceChangeReason) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnBalanceChange != nil {
|
|
|
|
t.OnBalanceChange(a, prev, new, reason)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnNonceChange(a common.Address, prev, new uint64) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnNonceChange != nil {
|
|
|
|
t.OnNonceChange(a, prev, new)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnCodeChange(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnCodeChange != nil {
|
|
|
|
t.OnCodeChange(a, prevCodeHash, prev, codeHash, code)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnStorageChange(a common.Address, k, prev, new common.Hash) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnStorageChange != nil {
|
|
|
|
t.OnStorageChange(a, k, prev, new)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
func (t *muxTracer) OnLog(log *types.Log) {
|
2022-11-08 12:16:52 +03:00
|
|
|
for _, t := range t.tracers {
|
2024-03-22 20:53:53 +03:00
|
|
|
if t.OnLog != nil {
|
|
|
|
t.OnLog(log)
|
|
|
|
}
|
2022-11-08 12:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResult returns an empty json object.
|
|
|
|
func (t *muxTracer) GetResult() (json.RawMessage, error) {
|
|
|
|
resObject := make(map[string]json.RawMessage)
|
|
|
|
for i, tt := range t.tracers {
|
|
|
|
r, err := tt.GetResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resObject[t.names[i]] = r
|
|
|
|
}
|
|
|
|
res, err := json.Marshal(resObject)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates execution of the tracer at the first opportune moment.
|
|
|
|
func (t *muxTracer) Stop(err error) {
|
|
|
|
for _, t := range t.tracers {
|
|
|
|
t.Stop(err)
|
|
|
|
}
|
|
|
|
}
|