-
Notifications
You must be signed in to change notification settings - Fork 20.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eth/tracers: add golang 4byte tracer #23882
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d3487b
native 4byte tracer
wardbradt d79b015
Update eth/tracers/native/4byte.go
wardbradt 0badf15
Update eth/tracers/native/4byte.go
wardbradt a276328
goimports
wardbradt bf6da57
eth/tracers: make 4byte tracer not care about create
holiman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// 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" | ||
"math/big" | ||
"strconv" | ||
"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() { | ||
register("4byte", newFourByteTracer) | ||
} | ||
|
||
// fourByteTracer searches for 4byte-identifiers, and collects them for post-processing. | ||
// It collects the methods identifiers along with the size of the supplied data, so | ||
// a reversed signature can be matched against the size of the data. | ||
// | ||
// Example: | ||
// > debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byte"}) | ||
// { | ||
// 0x27dc297e-128: 1, | ||
// 0x38cc4831-0: 2, | ||
// 0x524f3889-96: 1, | ||
// 0xadf59f99-288: 1, | ||
// 0xc281d19e-0: 1 | ||
// } | ||
type fourByteTracer struct { | ||
env *vm.EVM | ||
ids map[string]int // ids aggregates the 4byte ids found | ||
interrupt uint32 // Atomic flag to signal execution interruption | ||
reason error // Textual reason for the interruption | ||
activePrecompiles []common.Address // Updated on CaptureStart based on given rules | ||
} | ||
|
||
// newFourByteTracer returns a native go tracer which collects | ||
// 4 byte-identifiers of a tx, and implements vm.EVMLogger. | ||
func newFourByteTracer() tracers.Tracer { | ||
t := &fourByteTracer{ | ||
ids: make(map[string]int), | ||
} | ||
return t | ||
} | ||
|
||
// isPrecompiled returns whether the addr is a precompile. Logic borrowed from newJsTracer in eth/tracers/js/tracer.go | ||
func (t *fourByteTracer) isPrecompiled(addr common.Address) bool { | ||
for _, p := range t.activePrecompiles { | ||
if p == addr { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// store saves the given identifier and datasize. | ||
func (t *fourByteTracer) store(id []byte, size int) { | ||
key := bytesToHex(id) + "-" + strconv.Itoa(size) | ||
t.ids[key] += 1 | ||
} | ||
|
||
// CaptureStart implements the EVMLogger interface to initialize the tracing operation. | ||
func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { | ||
t.env = env | ||
|
||
// Update list of precompiles based on current block | ||
rules := env.ChainConfig().Rules(env.Context.BlockNumber) | ||
t.activePrecompiles = vm.ActivePrecompiles(rules) | ||
|
||
// Save the outer calldata also | ||
if len(input) >= 4 { | ||
t.store(input[0:4], len(input)-4) | ||
} | ||
} | ||
|
||
// CaptureState implements the EVMLogger interface to trace a single step of VM execution. | ||
func (t *fourByteTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { | ||
} | ||
|
||
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct). | ||
func (t *fourByteTracer) 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 { | ||
t.env.Cancel() | ||
return | ||
} | ||
if len(input) < 4 { | ||
return | ||
} | ||
// Skip any pre-compile invocations, those are just fancy opcodes | ||
if t.isPrecompiled(to) { | ||
return | ||
} | ||
t.store(input[0:4], len(input)-4) | ||
} | ||
|
||
// CaptureExit is called when EVM exits a scope, even if the scope didn't | ||
// execute any code. | ||
func (t *fourByteTracer) CaptureExit(output []byte, gasUsed uint64, err error) { | ||
} | ||
|
||
// CaptureFault implements the EVMLogger interface to trace an execution fault. | ||
func (t *fourByteTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) { | ||
} | ||
|
||
// CaptureEnd is called after the call finishes to finalize the tracing. | ||
func (t *fourByteTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) { | ||
} | ||
|
||
// GetResult returns the json-encoded nested list of call traces, and any | ||
// error arising from the encoding or forceful termination (via `Stop`). | ||
func (t *fourByteTracer) GetResult() (json.RawMessage, error) { | ||
res, err := json.Marshal(t.ids) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return res, t.reason | ||
} | ||
|
||
// Stop terminates execution of the tracer at the first opportune moment. | ||
func (t *fourByteTracer) Stop(err error) { | ||
t.reason = err | ||
atomic.StoreUint32(&t.interrupt, 1) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this would introduce a new tracer. If we want to make it the default it has to be called
4byteTracer