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