github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/eth/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  	"errors"
    23  
    24  	"github.com/tirogen/go-ethereum/common"
    25  	"github.com/tirogen/go-ethereum/core/vm"
    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  	TxIndex   int         // Index of the transaction within a block (zero if dangling tx or call)
    33  	TxHash    common.Hash // Hash of the transaction being traced (zero if dangling call)
    34  }
    35  
    36  // Tracer interface extends vm.EVMLogger and additionally
    37  // allows collecting the tracing result.
    38  type Tracer interface {
    39  	vm.EVMLogger
    40  	GetResult() (json.RawMessage, error)
    41  	// Stop terminates execution of the tracer at the first opportune moment.
    42  	Stop(err error)
    43  }
    44  
    45  type lookupFunc func(string, *Context, json.RawMessage) (Tracer, error)
    46  
    47  var (
    48  	lookups []lookupFunc
    49  )
    50  
    51  // RegisterLookup registers a method as a lookup for tracers, meaning that
    52  // users can invoke a named tracer through that lookup. If 'wildcard' is true,
    53  // then the lookup will be placed last. This is typically meant for interpreted
    54  // engines (js) which can evaluate dynamic user-supplied code.
    55  func RegisterLookup(wildcard bool, lookup lookupFunc) {
    56  	if wildcard {
    57  		lookups = append(lookups, lookup)
    58  	} else {
    59  		lookups = append([]lookupFunc{lookup}, lookups...)
    60  	}
    61  }
    62  
    63  // New returns a new instance of a tracer, by iterating through the
    64  // registered lookups.
    65  func New(code string, ctx *Context, cfg json.RawMessage) (Tracer, error) {
    66  	for _, lookup := range lookups {
    67  		if tracer, err := lookup(code, ctx, cfg); err == nil {
    68  			return tracer, nil
    69  		}
    70  	}
    71  	return nil, errors.New("tracer not found")
    72  }