github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/live.go (about)

     1  // Copyright 2024 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
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  
    23  	"github.com/ethereum/go-ethereum/core/tracing"
    24  )
    25  
    26  type ctorFunc func(config json.RawMessage) (*tracing.Hooks, error)
    27  
    28  // LiveDirectory is the collection of tracers which can be used
    29  // during normal block import operations.
    30  var LiveDirectory = liveDirectory{elems: make(map[string]ctorFunc)}
    31  
    32  type liveDirectory struct {
    33  	elems map[string]ctorFunc
    34  }
    35  
    36  // Register registers a tracer constructor by name.
    37  func (d *liveDirectory) Register(name string, f ctorFunc) {
    38  	d.elems[name] = f
    39  }
    40  
    41  // New instantiates a tracer by name.
    42  func (d *liveDirectory) New(name string, config json.RawMessage) (*tracing.Hooks, error) {
    43  	if len(config) == 0 {
    44  		config = json.RawMessage("{}")
    45  	}
    46  	if f, ok := d.elems[name]; ok {
    47  		return f(config)
    48  	}
    49  	return nil, errors.New("not found")
    50  }