json_logger.go 2.8 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. RefundCounter: env.StateDB.GetRefund(),
  49. Err: err,
  50. }
  51. if !l.cfg.DisableMemory {
  52. log.Memory = memory.Data()
  53. }
  54. if !l.cfg.DisableStack {
  55. log.Stack = stack.Data()
  56. }
  57. return l.encoder.Encode(log)
  58. }
  59. // CaptureFault outputs state information on the logger.
  60. 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 {
  61. return nil
  62. }
  63. // CaptureEnd is triggered at end of execution.
  64. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
  65. type endLog struct {
  66. Output string `json:"output"`
  67. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  68. Time time.Duration `json:"time"`
  69. Err string `json:"error,omitempty"`
  70. }
  71. if err != nil {
  72. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  73. }
  74. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  75. }