4byte.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2021 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 native
  17. import (
  18. "encoding/json"
  19. "math/big"
  20. "strconv"
  21. "sync/atomic"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/eth/tracers"
  26. )
  27. func init() {
  28. register("4byteTracer", newFourByteTracer)
  29. }
  30. // fourByteTracer searches for 4byte-identifiers, and collects them for post-processing.
  31. // It collects the methods identifiers along with the size of the supplied data, so
  32. // a reversed signature can be matched against the size of the data.
  33. //
  34. // Example:
  35. // > debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byteTracer"})
  36. // {
  37. // 0x27dc297e-128: 1,
  38. // 0x38cc4831-0: 2,
  39. // 0x524f3889-96: 1,
  40. // 0xadf59f99-288: 1,
  41. // 0xc281d19e-0: 1
  42. // }
  43. type fourByteTracer struct {
  44. env *vm.EVM
  45. ids map[string]int // ids aggregates the 4byte ids found
  46. interrupt uint32 // Atomic flag to signal execution interruption
  47. reason error // Textual reason for the interruption
  48. activePrecompiles []common.Address // Updated on CaptureStart based on given rules
  49. }
  50. // newFourByteTracer returns a native go tracer which collects
  51. // 4 byte-identifiers of a tx, and implements vm.EVMLogger.
  52. func newFourByteTracer(ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
  53. t := &fourByteTracer{
  54. ids: make(map[string]int),
  55. }
  56. return t, nil
  57. }
  58. // isPrecompiled returns whether the addr is a precompile. Logic borrowed from newJsTracer in eth/tracers/js/tracer.go
  59. func (t *fourByteTracer) isPrecompiled(addr common.Address) bool {
  60. for _, p := range t.activePrecompiles {
  61. if p == addr {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. // store saves the given identifier and datasize.
  68. func (t *fourByteTracer) store(id []byte, size int) {
  69. key := bytesToHex(id) + "-" + strconv.Itoa(size)
  70. t.ids[key] += 1
  71. }
  72. // CaptureStart implements the EVMLogger interface to initialize the tracing operation.
  73. func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
  74. t.env = env
  75. // Update list of precompiles based on current block
  76. rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Random != nil)
  77. t.activePrecompiles = vm.ActivePrecompiles(rules)
  78. // Save the outer calldata also
  79. if len(input) >= 4 {
  80. t.store(input[0:4], len(input)-4)
  81. }
  82. }
  83. // CaptureState implements the EVMLogger interface to trace a single step of VM execution.
  84. func (t *fourByteTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
  85. }
  86. // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
  87. func (t *fourByteTracer) CaptureEnter(op vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
  88. // Skip if tracing was interrupted
  89. if atomic.LoadUint32(&t.interrupt) > 0 {
  90. t.env.Cancel()
  91. return
  92. }
  93. if len(input) < 4 {
  94. return
  95. }
  96. // primarily we want to avoid CREATE/CREATE2/SELFDESTRUCT
  97. if op != vm.DELEGATECALL && op != vm.STATICCALL &&
  98. op != vm.CALL && op != vm.CALLCODE {
  99. return
  100. }
  101. // Skip any pre-compile invocations, those are just fancy opcodes
  102. if t.isPrecompiled(to) {
  103. return
  104. }
  105. t.store(input[0:4], len(input)-4)
  106. }
  107. // CaptureExit is called when EVM exits a scope, even if the scope didn't
  108. // execute any code.
  109. func (t *fourByteTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
  110. }
  111. // CaptureFault implements the EVMLogger interface to trace an execution fault.
  112. func (t *fourByteTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
  113. }
  114. // CaptureEnd is called after the call finishes to finalize the tracing.
  115. func (t *fourByteTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
  116. }
  117. func (*fourByteTracer) CaptureTxStart(gasLimit uint64) {}
  118. func (*fourByteTracer) CaptureTxEnd(restGas uint64) {}
  119. // GetResult returns the json-encoded nested list of call traces, and any
  120. // error arising from the encoding or forceful termination (via `Stop`).
  121. func (t *fourByteTracer) GetResult() (json.RawMessage, error) {
  122. res, err := json.Marshal(t.ids)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return res, t.reason
  127. }
  128. // Stop terminates execution of the tracer at the first opportune moment.
  129. func (t *fourByteTracer) Stop(err error) {
  130. t.reason = err
  131. atomic.StoreUint32(&t.interrupt, 1)
  132. }