logger_json.go 2.9 KB

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