Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.

Commit e9294a7

Browse files
wardbradtholiman
andauthored
eth/tracers: add golang 4byte tracer (ethereum#23882)
* native 4byte tracer * Update eth/tracers/native/4byte.go Co-authored-by: Martin Holst Swende <[email protected]> * Update eth/tracers/native/4byte.go Co-authored-by: Martin Holst Swende <[email protected]> * goimports * eth/tracers: make 4byte tracer not care about create Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 5358e49 commit e9294a7

File tree

3 files changed

+157
-4
lines changed

3 files changed

+157
-4
lines changed

eth/tracers/js/internal/tracers/4byte_tracer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@
3737
this.ids[key] = this.ids[key] + 1 || 1;
3838
},
3939

40-
enter: function(frame) {
40+
enter: function(frame, log) {
4141
// Skip any pre-compile invocations, those are just fancy opcodes
4242
if (isPrecompiled(frame.getTo())) {
4343
return;
4444
}
45+
var type = frame.getType()
46+
if (type !== "CALL" && type !== "STATICCALL" &&
47+
type !== "DELEGATECALL" && type !== "CALLCODE"){
48+
return;
49+
}
4550
var input = frame.getInput()
4651
if (input.length >= 4) {
4752
this.store(slice(input, 0, 4), input.length - 4);

eth/tracers/js/internal/tracers/assets.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/tracers/native/4byte.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package native
18+
19+
import (
20+
"encoding/json"
21+
"math/big"
22+
"strconv"
23+
"sync/atomic"
24+
"time"
25+
26+
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/core/vm"
28+
"github.com/ethereum/go-ethereum/eth/tracers"
29+
)
30+
31+
func init() {
32+
register("4byte", newFourByteTracer)
33+
}
34+
35+
// fourByteTracer searches for 4byte-identifiers, and collects them for post-processing.
36+
// It collects the methods identifiers along with the size of the supplied data, so
37+
// a reversed signature can be matched against the size of the data.
38+
//
39+
// Example:
40+
// > debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byte"})
41+
// {
42+
// 0x27dc297e-128: 1,
43+
// 0x38cc4831-0: 2,
44+
// 0x524f3889-96: 1,
45+
// 0xadf59f99-288: 1,
46+
// 0xc281d19e-0: 1
47+
// }
48+
type fourByteTracer struct {
49+
env *vm.EVM
50+
ids map[string]int // ids aggregates the 4byte ids found
51+
interrupt uint32 // Atomic flag to signal execution interruption
52+
reason error // Textual reason for the interruption
53+
activePrecompiles []common.Address // Updated on CaptureStart based on given rules
54+
}
55+
56+
// newFourByteTracer returns a native go tracer which collects
57+
// 4 byte-identifiers of a tx, and implements vm.EVMLogger.
58+
func newFourByteTracer() tracers.Tracer {
59+
t := &fourByteTracer{
60+
ids: make(map[string]int),
61+
}
62+
return t
63+
}
64+
65+
// isPrecompiled returns whether the addr is a precompile. Logic borrowed from newJsTracer in eth/tracers/js/tracer.go
66+
func (t *fourByteTracer) isPrecompiled(addr common.Address) bool {
67+
for _, p := range t.activePrecompiles {
68+
if p == addr {
69+
return true
70+
}
71+
}
72+
return false
73+
}
74+
75+
// store saves the given identifier and datasize.
76+
func (t *fourByteTracer) store(id []byte, size int) {
77+
key := bytesToHex(id) + "-" + strconv.Itoa(size)
78+
t.ids[key] += 1
79+
}
80+
81+
// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
82+
func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
83+
t.env = env
84+
85+
// Update list of precompiles based on current block
86+
rules := env.ChainConfig().Rules(env.Context.BlockNumber)
87+
t.activePrecompiles = vm.ActivePrecompiles(rules)
88+
89+
// Save the outer calldata also
90+
if len(input) >= 4 {
91+
t.store(input[0:4], len(input)-4)
92+
}
93+
}
94+
95+
// CaptureState implements the EVMLogger interface to trace a single step of VM execution.
96+
func (t *fourByteTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
97+
}
98+
99+
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
100+
func (t *fourByteTracer) CaptureEnter(op vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
101+
// Skip if tracing was interrupted
102+
if atomic.LoadUint32(&t.interrupt) > 0 {
103+
t.env.Cancel()
104+
return
105+
}
106+
if len(input) < 4 {
107+
return
108+
}
109+
// primarily we want to avoid CREATE/CREATE2/SELFDESTRUCT
110+
if op != vm.DELEGATECALL && op != vm.STATICCALL &&
111+
op != vm.CALL && op != vm.CALLCODE {
112+
return
113+
}
114+
// Skip any pre-compile invocations, those are just fancy opcodes
115+
if t.isPrecompiled(to) {
116+
return
117+
}
118+
t.store(input[0:4], len(input)-4)
119+
}
120+
121+
// CaptureExit is called when EVM exits a scope, even if the scope didn't
122+
// execute any code.
123+
func (t *fourByteTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
124+
}
125+
126+
// CaptureFault implements the EVMLogger interface to trace an execution fault.
127+
func (t *fourByteTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
128+
}
129+
130+
// CaptureEnd is called after the call finishes to finalize the tracing.
131+
func (t *fourByteTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
132+
}
133+
134+
// GetResult returns the json-encoded nested list of call traces, and any
135+
// error arising from the encoding or forceful termination (via `Stop`).
136+
func (t *fourByteTracer) GetResult() (json.RawMessage, error) {
137+
res, err := json.Marshal(t.ids)
138+
if err != nil {
139+
return nil, err
140+
}
141+
return res, t.reason
142+
}
143+
144+
// Stop terminates execution of the tracer at the first opportune moment.
145+
func (t *fourByteTracer) Stop(err error) {
146+
t.reason = err
147+
atomic.StoreUint32(&t.interrupt, 1)
148+
}

0 commit comments

Comments
 (0)