cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
// Copyright 2017 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/>.
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// Package tracers is a manager for transaction tracing engines.
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
package tracers
|
|
|
|
|
|
|
|
import (
|
2021-11-05 13:48:21 +03:00
|
|
|
"encoding/json"
|
2023-03-28 12:46:53 +03:00
|
|
|
"fmt"
|
2023-02-28 13:24:37 +03:00
|
|
|
"math/big"
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-11-05 13:48:21 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
)
|
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// Context contains some contextual infos for a transaction execution that is not
|
|
|
|
// available from within the EVM object.
|
|
|
|
type Context struct {
|
2023-02-28 13:24:37 +03:00
|
|
|
BlockHash common.Hash // Hash of the block the tx is contained within (zero if dangling tx or call)
|
|
|
|
BlockNumber *big.Int // Number of the block the tx is contained within (zero if dangling tx or call)
|
|
|
|
TxIndex int // Index of the transaction within a block (zero if dangling tx or call)
|
|
|
|
TxHash common.Hash // Hash of the transaction being traced (zero if dangling call)
|
2021-11-09 14:09:35 +03:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:48:21 +03:00
|
|
|
// Tracer interface extends vm.EVMLogger and additionally
|
|
|
|
// allows collecting the tracing result.
|
|
|
|
type Tracer interface {
|
|
|
|
vm.EVMLogger
|
|
|
|
GetResult() (json.RawMessage, error)
|
|
|
|
// Stop terminates execution of the tracer at the first opportune moment.
|
|
|
|
Stop(err error)
|
|
|
|
}
|
|
|
|
|
2023-01-10 17:11:53 +03:00
|
|
|
type ctorFn func(*Context, json.RawMessage) (Tracer, error)
|
|
|
|
type jsCtorFn func(string, *Context, json.RawMessage) (Tracer, error)
|
2021-11-09 14:09:35 +03:00
|
|
|
|
2023-01-10 17:11:53 +03:00
|
|
|
type elem struct {
|
|
|
|
ctor ctorFn
|
|
|
|
isJS bool
|
|
|
|
}
|
2021-11-05 13:48:21 +03:00
|
|
|
|
2023-01-10 17:11:53 +03:00
|
|
|
// DefaultDirectory is the collection of tracers bundled by default.
|
|
|
|
var DefaultDirectory = directory{elems: make(map[string]elem)}
|
|
|
|
|
|
|
|
// directory provides functionality to lookup a tracer by name
|
|
|
|
// and a function to instantiate it. It falls back to a JS code evaluator
|
|
|
|
// if no tracer of the given name exists.
|
|
|
|
type directory struct {
|
|
|
|
elems map[string]elem
|
|
|
|
jsEval jsCtorFn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers a method as a lookup for tracers, meaning that
|
|
|
|
// users can invoke a named tracer through that lookup.
|
|
|
|
func (d *directory) Register(name string, f ctorFn, isJS bool) {
|
|
|
|
d.elems[name] = elem{ctor: f, isJS: isJS}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterJSEval registers a tracer that is able to parse
|
|
|
|
// dynamic user-provided JS code.
|
|
|
|
func (d *directory) RegisterJSEval(f jsCtorFn) {
|
|
|
|
d.jsEval = f
|
2021-11-05 13:48:21 +03:00
|
|
|
}
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
|
2021-11-09 14:09:35 +03:00
|
|
|
// New returns a new instance of a tracer, by iterating through the
|
2023-01-10 17:11:53 +03:00
|
|
|
// registered lookups. Name is either name of an existing tracer
|
|
|
|
// or an arbitrary JS code.
|
|
|
|
func (d *directory) New(name string, ctx *Context, cfg json.RawMessage) (Tracer, error) {
|
|
|
|
if elem, ok := d.elems[name]; ok {
|
|
|
|
return elem.ctor(ctx, cfg)
|
|
|
|
}
|
|
|
|
// Assume JS code
|
|
|
|
return d.jsEval(name, ctx, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsJS will return true if the given tracer will evaluate
|
|
|
|
// JS code. Because code evaluation has high overhead, this
|
|
|
|
// info will be used in determining fast and slow code paths.
|
|
|
|
func (d *directory) IsJS(name string) bool {
|
|
|
|
if elem, ok := d.elems[name]; ok {
|
|
|
|
return elem.isJS
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
}
|
2023-01-10 17:11:53 +03:00
|
|
|
// JS eval will execute JS code
|
|
|
|
return true
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 14:56:11 +03:00
|
|
|
}
|
2023-03-28 12:46:53 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
memoryPadLimit = 1024 * 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetMemoryCopyPadded returns offset + size as a new slice.
|
|
|
|
// It zero-pads the slice if it extends beyond memory bounds.
|
|
|
|
func GetMemoryCopyPadded(m *vm.Memory, offset, size int64) ([]byte, error) {
|
|
|
|
if offset < 0 || size < 0 {
|
|
|
|
return nil, fmt.Errorf("offset or size must not be negative")
|
|
|
|
}
|
|
|
|
if int(offset+size) < m.Len() { // slice fully inside memory
|
|
|
|
return m.GetCopy(offset, size), nil
|
|
|
|
}
|
|
|
|
paddingNeeded := int(offset+size) - m.Len()
|
|
|
|
if paddingNeeded > memoryPadLimit {
|
|
|
|
return nil, fmt.Errorf("reached limit for padding memory slice: %d", paddingNeeded)
|
|
|
|
}
|
|
|
|
cpy := make([]byte, size)
|
|
|
|
if overlap := int64(m.Len()) - offset; overlap > 0 {
|
|
|
|
copy(cpy, m.GetPtr(offset, overlap))
|
|
|
|
}
|
|
|
|
return cpy, nil
|
|
|
|
}
|