json_logger.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/json"
  19. "io"
  20. "math/big"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/math"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. )
  26. type JSONLogger struct {
  27. encoder *json.Encoder
  28. cfg *vm.LogConfig
  29. }
  30. // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
  31. // into the provided stream.
  32. func NewJSONLogger(cfg *vm.LogConfig, writer io.Writer) *JSONLogger {
  33. return &JSONLogger{json.NewEncoder(writer), cfg}
  34. }
  35. func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
  36. return nil
  37. }
  38. // CaptureState outputs state information on the logger.
  39. func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
  40. log := vm.StructLog{
  41. Pc: pc,
  42. Op: op,
  43. Gas: gas,
  44. GasCost: cost,
  45. MemorySize: memory.Len(),
  46. Storage: nil,
  47. Depth: depth,
  48. Err: err,
  49. }
  50. if !l.cfg.DisableMemory {
  51. log.Memory = memory.Data()
  52. }
  53. if !l.cfg.DisableStack {
  54. log.Stack = stack.Data()
  55. }
  56. return l.encoder.Encode(log)
  57. }
  58. // CaptureFault outputs state information on the logger.
  59. func (l *JSONLogger) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
  60. return nil
  61. }
  62. // CaptureEnd is triggered at end of execution.
  63. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
  64. type endLog struct {
  65. Output string `json:"output"`
  66. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  67. Time time.Duration `json:"time"`
  68. Err string `json:"error,omitempty"`
  69. }
  70. if err != nil {
  71. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  72. }
  73. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  74. }