logger_json.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 vm
  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. )
  25. type JSONLogger struct {
  26. encoder *json.Encoder
  27. cfg *LogConfig
  28. env *EVM
  29. }
  30. // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
  31. // into the provided stream.
  32. func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
  33. l := &JSONLogger{encoder: json.NewEncoder(writer), cfg: cfg}
  34. if l.cfg == nil {
  35. l.cfg = &LogConfig{}
  36. }
  37. return l
  38. }
  39. func (l *JSONLogger) CaptureStart(env *EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
  40. l.env = env
  41. }
  42. func (l *JSONLogger) CaptureFault(uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
  43. // CaptureState outputs state information on the logger.
  44. func (l *JSONLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
  45. memory := scope.Memory
  46. stack := scope.Stack
  47. log := StructLog{
  48. Pc: pc,
  49. Op: op,
  50. Gas: gas,
  51. GasCost: cost,
  52. MemorySize: memory.Len(),
  53. Depth: depth,
  54. RefundCounter: l.env.StateDB.GetRefund(),
  55. Err: err,
  56. }
  57. if l.cfg.EnableMemory {
  58. log.Memory = memory.Data()
  59. }
  60. if !l.cfg.DisableStack {
  61. log.Stack = stack.data
  62. }
  63. if l.cfg.EnableReturnData {
  64. log.ReturnData = rData
  65. }
  66. l.encoder.Encode(log)
  67. }
  68. // CaptureEnd is triggered at end of execution.
  69. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
  70. type endLog struct {
  71. Output string `json:"output"`
  72. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  73. Time time.Duration `json:"time"`
  74. Err string `json:"error,omitempty"`
  75. }
  76. if err != nil {
  77. l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  78. }
  79. l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  80. }
  81. func (l *JSONLogger) CaptureEnter(typ OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
  82. }
  83. func (l *JSONLogger) CaptureExit(output []byte, gasUsed uint64, err error) {}