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
108 changes: 108 additions & 0 deletions eth/tracers/native/returnmsg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package native

import (
"bytes"
"encoding/json"
"math/big"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/tracers"
)

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

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
}

// newNoopTracer returns a new noop tracer.
fanbsb marked this conversation as resolved.
Show resolved Hide resolved
func newReturnMsgTracer(_ *tracers.Context) tracers.Tracer {
return &returnMsgTracer{}
}

// 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) {
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) {
if err != nil {
if err == vm.ErrExecutionReverted && len(output) > 4 && bytes.Equal(output[:4], revertSelector) {
returnMsg, _ := abi.UnpackRevert(output)
t.returnMsg = err.Error() + ": " + returnMsg
} else {
t.returnMsg = 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) {
}

// CaptureFault implements the EVMLogger interface to trace an execution fault.
func (t *returnMsgTracer) 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) {
// Skip if tracing was interrupted
if atomic.LoadUint32(&t.interrupt) > 0 {
t.env.Cancel()
return
}
}

// 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 *returnMsgTracer) CaptureTxStart(_ uint64) {}

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

// GetResult returns an error message json object.
func (t *returnMsgTracer) GetResult() (json.RawMessage, error) {
res, err := json.Marshal(t.returnMsg)
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) {
t.reason = err
atomic.StoreUint32(&t.interrupt, 1)
}