github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/native/4byte.go (about) 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 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/core/tracing" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/core/vm" 29 "github.com/ethereum/go-ethereum/eth/tracers" 30 "github.com/ethereum/go-ethereum/params" 31 ) 32 33 func init() { 34 tracers.DefaultDirectory.Register("4byteTracer", newFourByteTracer, false) 35 } 36 37 // fourByteTracer searches for 4byte-identifiers, and collects them for post-processing. 38 // It collects the methods identifiers along with the size of the supplied data, so 39 // a reversed signature can be matched against the size of the data. 40 // 41 // Example: 42 // 43 // > debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byteTracer"}) 44 // { 45 // 0x27dc297e-128: 1, 46 // 0x38cc4831-0: 2, 47 // 0x524f3889-96: 1, 48 // 0xadf59f99-288: 1, 49 // 0xc281d19e-0: 1 50 // } 51 type fourByteTracer struct { 52 ids map[string]int // ids aggregates the 4byte ids found 53 interrupt atomic.Bool // Atomic flag to signal execution interruption 54 reason error // Textual reason for the interruption 55 chainConfig *params.ChainConfig 56 activePrecompiles []common.Address // Updated on tx start based on given rules 57 } 58 59 // newFourByteTracer returns a native go tracer which collects 60 // 4 byte-identifiers of a tx, and implements vm.EVMLogger. 61 func newFourByteTracer(ctx *tracers.Context, cfg json.RawMessage, chainConfig *params.ChainConfig) (*tracers.Tracer, error) { 62 t := &fourByteTracer{ 63 ids: make(map[string]int), 64 chainConfig: chainConfig, 65 } 66 return &tracers.Tracer{ 67 Hooks: &tracing.Hooks{ 68 OnTxStart: t.OnTxStart, 69 OnEnter: t.OnEnter, 70 }, 71 GetResult: t.GetResult, 72 Stop: t.Stop, 73 }, nil 74 } 75 76 // isPrecompiled returns whether the addr is a precompile. Logic borrowed from newJsTracer in eth/tracers/js/tracer.go 77 func (t *fourByteTracer) isPrecompiled(addr common.Address) bool { 78 for _, p := range t.activePrecompiles { 79 if p == addr { 80 return true 81 } 82 } 83 return false 84 } 85 86 // store saves the given identifier and datasize. 87 func (t *fourByteTracer) store(id []byte, size int) { 88 key := bytesToHex(id) + "-" + strconv.Itoa(size) 89 t.ids[key] += 1 90 } 91 92 func (t *fourByteTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) { 93 // Update list of precompiles based on current block 94 rules := t.chainConfig.Rules(env.BlockNumber, env.Random != nil, env.Time) 95 t.activePrecompiles = vm.ActivePrecompiles(rules) 96 } 97 98 // OnEnter is called when EVM enters a new scope (via call, create or selfdestruct). 99 func (t *fourByteTracer) OnEnter(depth int, opcode byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { 100 // Skip if tracing was interrupted 101 if t.interrupt.Load() { 102 return 103 } 104 if len(input) < 4 { 105 return 106 } 107 op := vm.OpCode(opcode) 108 // primarily we want to avoid CREATE/CREATE2/SELFDESTRUCT 109 if op != vm.DELEGATECALL && op != vm.STATICCALL && 110 op != vm.CALL && op != vm.CALLCODE { 111 return 112 } 113 // Skip any pre-compile invocations, those are just fancy opcodes 114 if t.isPrecompiled(to) { 115 return 116 } 117 t.store(input[0:4], len(input)-4) 118 } 119 120 // GetResult returns the json-encoded nested list of call traces, and any 121 // error arising from the encoding or forceful termination (via `Stop`). 122 func (t *fourByteTracer) GetResult() (json.RawMessage, error) { 123 res, err := json.Marshal(t.ids) 124 if err != nil { 125 return nil, err 126 } 127 return res, t.reason 128 } 129 130 // Stop terminates execution of the tracer at the first opportune moment. 131 func (t *fourByteTracer) Stop(err error) { 132 t.reason = err 133 t.interrupt.Store(true) 134 } 135 136 func bytesToHex(s []byte) string { 137 return "0x" + common.Bytes2Hex(s) 138 }