tracer.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 is a collection of tracers written in go.
  18. In order to add a native tracer and have it compiled into the binary, a new
  19. file needs to be added to this folder, containing an implementation of the
  20. `eth.tracers.Tracer` interface.
  21. Aside from implementing the tracer, it also needs to register itself, using the
  22. `register` method -- and this needs to be done in the package initialization.
  23. Example:
  24. ```golang
  25. func init() {
  26. register("noopTracerNative", newNoopTracer)
  27. }
  28. ```
  29. */
  30. package native
  31. import (
  32. "errors"
  33. "github.com/ethereum/go-ethereum/eth/tracers"
  34. )
  35. // init registers itself this packages as a lookup for tracers.
  36. func init() {
  37. tracers.RegisterLookup(false, lookup)
  38. }
  39. /*
  40. ctors is a map of package-local tracer constructors.
  41. We cannot be certain about the order of init-functions within a package,
  42. The go spec (https://golang.org/ref/spec#Package_initialization) says
  43. > To ensure reproducible initialization behavior, build systems
  44. > are encouraged to present multiple files belonging to the same
  45. > package in lexical file name order to a compiler.
  46. Hence, we cannot make the map in init, but must make it upon first use.
  47. */
  48. var ctors map[string]func() tracers.Tracer
  49. // register is used by native tracers to register their presence.
  50. func register(name string, ctor func() tracers.Tracer) {
  51. if ctors == nil {
  52. ctors = make(map[string]func() tracers.Tracer)
  53. }
  54. ctors[name] = ctor
  55. }
  56. // lookup returns a tracer, if one can be matched to the given name.
  57. func lookup(name string, ctx *tracers.Context) (tracers.Tracer, error) {
  58. if ctors == nil {
  59. ctors = make(map[string]func() tracers.Tracer)
  60. }
  61. if ctor, ok := ctors[name]; ok {
  62. return ctor(), nil
  63. }
  64. return nil, errors.New("no tracer found")
  65. }