github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/tracers/tracers.go (about) 1 // Copyright 2017 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package tracers is a collection of JavaScript transaction tracers. 18 package tracers 19 20 import ( 21 "encoding/json" 22 "errors" 23 "github.com/SmartMeshFoundation/Spectrum/common" 24 "github.com/SmartMeshFoundation/Spectrum/core/vm" 25 ) 26 27 var all = make(map[string]string) 28 29 // tracer retrieves a specific JavaScript tracer by name. 30 func tracer(name string) (string, bool) { 31 if tracer, ok := all[name]; ok { 32 return tracer, true 33 } 34 return "", false 35 } 36 37 // Context contains some contextual infos for a transaction execution that is not 38 // available from within the EVM object. 39 type Context struct { 40 BlockHash common.Hash // Hash of the block the tx is contained within (zero if dangling tx or call) 41 TxIndex int // Index of the transaction within a block (zero if dangling tx or call) 42 TxHash common.Hash // Hash of the transaction being traced (zero if dangling call) 43 } 44 45 type Tracer interface { 46 vm.EVMLogger 47 GetResult() (json.RawMessage, error) 48 // Stop terminates execution of the tracer at the first opportune moment. 49 Stop(err error) 50 } 51 52 type lookupFunc func(string, *Context) (Tracer, error) 53 54 var ( 55 lookups []lookupFunc 56 ) 57 58 // RegisterLookup registers a method as a lookup for tracers, meaning that 59 // users can invoke a named tracer through that lookup. If 'wildcard' is true, 60 // then the lookup will be placed last. This is typically meant for interpreted 61 // engines (js) which can evaluate dynamic user-supplied code. 62 func RegisterLookup(wildcard bool, lookup lookupFunc) { 63 if wildcard { 64 lookups = append(lookups, lookup) 65 } else { 66 lookups = append([]lookupFunc{lookup}, lookups...) 67 } 68 } 69 70 // New returns a new instance of a tracer, by iterating through the 71 // registered lookups. 72 func New(code string, ctx *Context) (Tracer, error) { 73 for _, lookup := range lookups { 74 if tracer, err := lookup(code, ctx); err == nil { 75 return tracer, nil 76 } 77 } 78 return nil, errors.New("tracer not found") 79 }