tracers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // Package tracers is a manager for transaction tracing engines.
  17. package tracers
  18. import (
  19. "encoding/json"
  20. "errors"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. )
  24. // Context contains some contextual infos for a transaction execution that is not
  25. // available from within the EVM object.
  26. type Context struct {
  27. BlockHash common.Hash // Hash of the block the tx is contained within (zero if dangling tx or call)
  28. TxIndex int // Index of the transaction within a block (zero if dangling tx or call)
  29. TxHash common.Hash // Hash of the transaction being traced (zero if dangling call)
  30. }
  31. // Tracer interface extends vm.EVMLogger and additionally
  32. // allows collecting the tracing result.
  33. type Tracer interface {
  34. vm.EVMLogger
  35. GetResult() (json.RawMessage, error)
  36. // Stop terminates execution of the tracer at the first opportune moment.
  37. Stop(err error)
  38. }
  39. type lookupFunc func(string, *Context, json.RawMessage) (Tracer, error)
  40. var (
  41. lookups []lookupFunc
  42. )
  43. // RegisterLookup registers a method as a lookup for tracers, meaning that
  44. // users can invoke a named tracer through that lookup. If 'wildcard' is true,
  45. // then the lookup will be placed last. This is typically meant for interpreted
  46. // engines (js) which can evaluate dynamic user-supplied code.
  47. func RegisterLookup(wildcard bool, lookup lookupFunc) {
  48. if wildcard {
  49. lookups = append(lookups, lookup)
  50. } else {
  51. lookups = append([]lookupFunc{lookup}, lookups...)
  52. }
  53. }
  54. // New returns a new instance of a tracer, by iterating through the
  55. // registered lookups.
  56. func New(code string, ctx *Context, cfg json.RawMessage) (Tracer, error) {
  57. for _, lookup := range lookups {
  58. if tracer, err := lookup(code, ctx, cfg); err == nil {
  59. return tracer, nil
  60. }
  61. }
  62. return nil, errors.New("tracer not found")
  63. }