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