Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/tracers: add initial revertReasonTracer tracer #25265

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
eth/tracers: rename to revertReasonTracer
  • Loading branch information
fanbsb committed Jul 12, 2022
commit a79304f3550d6b19b0e4ce028dd62a4412087215
Original file line number Diff line number Diff line change
@@ -31,52 +31,52 @@ import (
)

func init() {
register("returnMsgTracer", newReturnMsgTracer)
register("revertReasonTracer", newRevertReasonTracer)
}

var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]

// returnMsgTracer is a go implementation of the Tracer interface which
// track the error message return by the contract.
type returnMsgTracer struct {
env *vm.EVM
returnMsg string // The error message return from the tx, if tx success, empty string return
interrupt uint32 // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
// revertReasonTracer is a go implementation of the Tracer interface which
// track the error message or revert reason return by the contract.
type revertReasonTracer struct {
env *vm.EVM
revertReason string // The revert reason return from the tx, if tx success, empty string return
interrupt uint32 // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
}

// newReturnMsgTracer returns a new noop tracer.
func newReturnMsgTracer(_ *tracers.Context) tracers.Tracer {
return &returnMsgTracer{}
// newRevertReasonTracer returns a new revert reason tracer.
func newRevertReasonTracer(_ *tracers.Context) tracers.Tracer {
return &revertReasonTracer{}
}

// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
func (t *returnMsgTracer) CaptureStart(env *vm.EVM, _ common.Address, _ common.Address, _ bool, _ []byte, _ uint64, _ *big.Int) {
func (t *revertReasonTracer) CaptureStart(env *vm.EVM, _ common.Address, _ common.Address, _ bool, _ []byte, _ uint64, _ *big.Int) {
t.env = env
}

// CaptureEnd is called after the call finishes to finalize the tracing.
func (t *returnMsgTracer) CaptureEnd(output []byte, _ uint64, _ time.Duration, err error) {
func (t *revertReasonTracer) CaptureEnd(output []byte, _ uint64, _ time.Duration, err error) {
if err != nil {
if err == vm.ErrExecutionReverted && len(output) > 4 && bytes.Equal(output[:4], revertSelector) {
returnMsg, _ := abi.UnpackRevert(output)
t.returnMsg = err.Error() + ": " + returnMsg
errMsg, _ := abi.UnpackRevert(output)
t.revertReason = err.Error() + ": " + errMsg
} else {
t.returnMsg = err.Error()
t.revertReason = err.Error()
}
}
}

// CaptureState implements the EVMLogger interface to trace a single step of VM execution.
func (t *returnMsgTracer) CaptureState(_ uint64, _ vm.OpCode, _, _ uint64, _ *vm.ScopeContext, _ []byte, _ int, _ error) {
func (t *revertReasonTracer) CaptureState(_ uint64, _ vm.OpCode, _, _ uint64, _ *vm.ScopeContext, _ []byte, _ int, _ error) {
}

// CaptureFault implements the EVMLogger interface to trace an execution fault.
func (t *returnMsgTracer) CaptureFault(_ uint64, _ vm.OpCode, _, _ uint64, _ *vm.ScopeContext, _ int, _ error) {
func (t *revertReasonTracer) CaptureFault(_ uint64, _ vm.OpCode, _, _ uint64, _ *vm.ScopeContext, _ int, _ error) {
}

// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
func (t *returnMsgTracer) CaptureEnter(_ vm.OpCode, _ common.Address, _ common.Address, _ []byte, _ uint64, _ *big.Int) {
func (t *revertReasonTracer) CaptureEnter(_ vm.OpCode, _ common.Address, _ common.Address, _ []byte, _ uint64, _ *big.Int) {
// Skip if tracing was interrupted
if atomic.LoadUint32(&t.interrupt) > 0 {
t.env.Cancel()
@@ -86,23 +86,23 @@ func (t *returnMsgTracer) CaptureEnter(_ vm.OpCode, _ common.Address, _ common.A

// CaptureExit is called when EVM exits a scope, even if the scope didn't
// execute any code.
func (t *returnMsgTracer) CaptureExit(_ []byte, _ uint64, _ error) {}
func (t *revertReasonTracer) CaptureExit(_ []byte, _ uint64, _ error) {}

func (t *returnMsgTracer) CaptureTxStart(_ uint64) {}
func (t *revertReasonTracer) CaptureTxStart(_ uint64) {}

func (t *returnMsgTracer) CaptureTxEnd(_ uint64) {}
func (t *revertReasonTracer) CaptureTxEnd(_ uint64) {}

// GetResult returns an error message json object.
func (t *returnMsgTracer) GetResult() (json.RawMessage, error) {
res, err := json.Marshal(t.returnMsg)
func (t *revertReasonTracer) GetResult() (json.RawMessage, error) {
res, err := json.Marshal(t.revertReason)
if err != nil {
return nil, err
}
return res, t.reason
}

// Stop terminates execution of the tracer at the first opportune moment.
func (t *returnMsgTracer) Stop(err error) {
func (t *revertReasonTracer) Stop(err error) {
t.reason = err
atomic.StoreUint32(&t.interrupt, 1)
}