logger_json.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  29. var _ Tracer = &JSONLogger{}
  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{json.NewEncoder(writer), 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. }
  41. func (l *JSONLogger) CaptureFault(*EVM, uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
  42. // CaptureState outputs state information on the logger.
  43. func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
  44. memory := scope.Memory
  45. stack := scope.Stack
  46. log := StructLog{
  47. Pc: pc,
  48. Op: op,
  49. Gas: gas,
  50. GasCost: cost,
  51. MemorySize: memory.Len(),
  52. Storage: nil,
  53. Depth: depth,
  54. RefundCounter: env.StateDB.GetRefund(),
  55. Err: err,
  56. }
  57. if !l.cfg.DisableMemory {
  58. log.Memory = memory.Data()
  59. }
  60. if !l.cfg.DisableStack {
  61. //TODO(@holiman) improve this
  62. logstack := make([]*big.Int, len(stack.Data()))
  63. for i, item := range stack.Data() {
  64. logstack[i] = item.ToBig()
  65. }
  66. log.Stack = logstack
  67. }
  68. if !l.cfg.DisableReturnData {
  69. log.ReturnData = rData
  70. }
  71. l.encoder.Encode(log)
  72. }
  73. // CaptureEnd is triggered at end of execution.
  74. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
  75. type endLog struct {
  76. Output string `json:"output"`
  77. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  78. Time time.Duration `json:"time"`
  79. Err string `json:"error,omitempty"`
  80. }
  81. if err != nil {
  82. l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  83. }
  84. l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  85. }