internal/ethapi: refactored unpacking logic into newRevertError

This commit is contained in:
Marius van der Wijden
2020-06-04 11:26:00 +02:00
committed by Péter Szilágyi
parent c9deb8a227
commit 7cdf4eea7e
2 changed files with 29 additions and 28 deletions

View File

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
@ -344,6 +345,18 @@ func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Ad
return b.pendingState.GetCode(contract), nil
}
func newRevertError(result *core.ExecutionResult) *revertError {
reason, errUnpack := abi.UnpackRevert(result.Revert())
err := errors.New("execution reverted")
if errUnpack == nil {
err = fmt.Errorf("execution reverted: %v", reason)
}
return &revertError{
error: err,
errData: hexutil.Encode(result.Revert()),
}
}
type revertError struct {
error
errData interface{} // additional data
@ -377,13 +390,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallM
}
// If the result contains a revert reason, try to unpack and return it.
if len(res.Revert()) > 0 {
reason, err := abi.UnpackRevert(res.Revert())
if err == nil {
return nil, &revertError{
error: fmt.Errorf("execution reverted: %v", reason),
errData: res.Revert(),
}
}
return nil, newRevertError(res)
}
return res.Return(), res.Err
}
@ -400,13 +407,7 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu
}
// If the result contains a revert reason, try to unpack and return it.
if len(res.Revert()) > 0 {
reason, err := abi.UnpackRevert(res.Revert())
if err == nil {
return nil, &revertError{
error: fmt.Errorf("execution reverted: %v", reason),
errData: res.Revert(),
}
}
return nil, newRevertError(res)
}
return res.Return(), res.Err
}

View File

@ -864,6 +864,18 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo
return result, err
}
func newRevertError(result *core.ExecutionResult) *revertError {
reason, errUnpack := abi.UnpackRevert(result.Revert())
err := errors.New("execution reverted")
if errUnpack == nil {
err = fmt.Errorf("execution reverted: %v", reason)
}
return &revertError{
error: err,
errData: hexutil.Encode(result.Revert()),
}
}
type revertError struct {
error
errData interface{} // additional data
@ -896,13 +908,7 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr
}
// If the result contains a revert reason, try to unpack and return it.
if len(result.Revert()) > 0 {
reason, err := abi.UnpackRevert(result.Revert())
if err == nil {
return nil, &revertError{
error: fmt.Errorf("execution reverted: %v", reason),
errData: result.Revert(),
}
}
return nil, newRevertError(result)
}
return result.Return(), result.Err
}
@ -1000,13 +1006,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash
if failed {
if result != nil && result.Err != vm.ErrOutOfGas {
if len(result.Revert()) > 0 {
reason, err := abi.UnpackRevert(result.Revert())
if err == nil {
return 0, &revertError{
error: fmt.Errorf("execution reverted: %v", reason),
errData: result.Revert(),
}
}
return 0, newRevertError(result)
}
return 0, result.Err
}