revertreason.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2022 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. "bytes"
  19. "encoding/json"
  20. "math/big"
  21. "sync/atomic"
  22. "time"
  23. "github.com/ethereum/go-ethereum/accounts/abi"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/eth/tracers"
  28. )
  29. func init() {
  30. register("revertReasonTracer", newRevertReasonTracer)
  31. }
  32. var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]
  33. // revertReasonTracer is a go implementation of the Tracer interface which
  34. // track the error message or revert reason return by the contract.
  35. type revertReasonTracer struct {
  36. env *vm.EVM
  37. revertReason string // The revert reason return from the tx, if tx success, empty string return
  38. interrupt uint32 // Atomic flag to signal execution interruption
  39. reason error // Textual reason for the interruption
  40. }
  41. // newRevertReasonTracer returns a new revert reason tracer.
  42. func newRevertReasonTracer(_ *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
  43. return &revertReasonTracer{}, nil
  44. }
  45. // CaptureStart implements the EVMLogger interface to initialize the tracing operation.
  46. func (t *revertReasonTracer) CaptureStart(env *vm.EVM, _ common.Address, _ common.Address, _ bool, _ []byte, _ uint64, _ *big.Int) {
  47. t.env = env
  48. }
  49. // CaptureEnd is called after the call finishes to finalize the tracing.
  50. func (t *revertReasonTracer) CaptureEnd(output []byte, _ uint64, _ time.Duration, err error) {
  51. if err != nil {
  52. if err == vm.ErrExecutionReverted && len(output) > 4 && bytes.Equal(output[:4], revertSelector) {
  53. errMsg, _ := abi.UnpackRevert(output)
  54. t.revertReason = err.Error() + ": " + errMsg
  55. } else {
  56. t.revertReason = err.Error()
  57. }
  58. }
  59. }
  60. // CaptureState implements the EVMLogger interface to trace a single step of VM execution.
  61. func (t *revertReasonTracer) CaptureState(_ uint64, _ vm.OpCode, _, _ uint64, _ *vm.ScopeContext, _ []byte, _ int, _ error) {
  62. }
  63. // CaptureFault implements the EVMLogger interface to trace an execution fault.
  64. func (t *revertReasonTracer) CaptureFault(_ uint64, _ vm.OpCode, _, _ uint64, _ *vm.ScopeContext, _ int, _ error) {
  65. }
  66. // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
  67. func (t *revertReasonTracer) CaptureEnter(_ vm.OpCode, _ common.Address, _ common.Address, _ []byte, _ uint64, _ *big.Int) {
  68. // Skip if tracing was interrupted
  69. if atomic.LoadUint32(&t.interrupt) > 0 {
  70. t.env.Cancel()
  71. return
  72. }
  73. }
  74. // CaptureExit is called when EVM exits a scope, even if the scope didn't
  75. // execute any code.
  76. func (t *revertReasonTracer) CaptureExit(_ []byte, _ uint64, _ error) {}
  77. func (t *revertReasonTracer) CaptureTxStart(_ uint64) {}
  78. func (t *revertReasonTracer) CaptureTxEnd(_ uint64) {}
  79. // GetResult returns an error message json object.
  80. func (t *revertReasonTracer) GetResult() (json.RawMessage, error) {
  81. res, err := json.Marshal(t.revertReason)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return res, t.reason
  86. }
  87. // Stop terminates execution of the tracer at the first opportune moment.
  88. func (t *revertReasonTracer) Stop(err error) {
  89. t.reason = err
  90. atomic.StoreUint32(&t.interrupt, 1)
  91. }