github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/state/runtime/instrumentation/tracers/tracers.go (about) 1 // Copyright 2017 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 tracers is a manager for transaction tracing engines. 18 package tracers 19 20 import ( 21 "encoding/json" 22 "math/big" 23 24 "github.com/0xPolygon/supernets2-node/state/runtime/fakevm" 25 "github.com/ethereum/go-ethereum/common" 26 ) 27 28 // Context contains some contextual infos for a transaction execution that is not 29 // available from within the EVM object. 30 type Context struct { 31 BlockHash common.Hash // Hash of the block the tx is contained within (zero if dangling tx or call) 32 BlockNumber *big.Int // Number of the block the tx is contained within (zero if dangling tx or call) 33 TxIndex int // Index of the transaction within a block (zero if dangling tx or call) 34 TxHash common.Hash // Hash of the transaction being traced (zero if dangling call) 35 } 36 37 // Tracer interface extends vm.EVMLogger and additionally 38 // allows collecting the tracing result. 39 type Tracer interface { 40 fakevm.EVMLogger 41 GetResult() (json.RawMessage, error) 42 // Stop terminates execution of the tracer at the first opportune moment. 43 Stop(err error) 44 } 45 46 type ctorFn func(*Context, json.RawMessage) (Tracer, error) 47 type jsCtorFn func(string, *Context, json.RawMessage) (Tracer, error) 48 49 type elem struct { 50 ctor ctorFn 51 isJS bool 52 } 53 54 // DefaultDirectory is the collection of tracers bundled by default. 55 var DefaultDirectory = directory{elems: make(map[string]elem)} 56 57 // directory provides functionality to lookup a tracer by name 58 // and a function to instantiate it. It falls back to a JS code evaluator 59 // if no tracer of the given name exists. 60 type directory struct { 61 elems map[string]elem 62 jsEval jsCtorFn 63 } 64 65 // Register registers a method as a lookup for tracers, meaning that 66 // users can invoke a named tracer through that lookup. 67 func (d *directory) Register(name string, f ctorFn, isJS bool) { 68 d.elems[name] = elem{ctor: f, isJS: isJS} 69 } 70 71 // RegisterJSEval registers a tracer that is able to parse 72 // dynamic user-provided JS code. 73 func (d *directory) RegisterJSEval(f jsCtorFn) { 74 d.jsEval = f 75 } 76 77 // New returns a new instance of a tracer, by iterating through the 78 // registered lookups. Name is either name of an existing tracer 79 // or an arbitrary JS code. 80 func (d *directory) New(name string, ctx *Context, cfg json.RawMessage) (Tracer, error) { 81 if elem, ok := d.elems[name]; ok { 82 return elem.ctor(ctx, cfg) 83 } 84 // Assume JS code 85 return d.jsEval(name, ctx, cfg) 86 } 87 88 // IsJS will return true if the given tracer will evaluate 89 // JS code. Because code evaluation has high overhead, this 90 // info will be used in determining fast and slow code paths. 91 func (d *directory) IsJS(name string) bool { 92 if elem, ok := d.elems[name]; ok { 93 return elem.isJS 94 } 95 // JS eval will execute JS code 96 return true 97 }