2020-07-13 20:01:45 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2021-07-28 15:50:05 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-07-13 20:01:45 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-07-28 15:50:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-05-31 14:54:59 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2020-07-13 20:01:45 +00:00
|
|
|
)
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
const (
|
|
|
|
codeErrInvalidState = uint32(iota) + 2 // NOTE: code 1 is reserved for internal errors
|
|
|
|
codeErrExecutionReverted // IMPORTANT: Do not move this error as it complies with the JSON-RPC error standard
|
|
|
|
codeErrChainConfigNotFound
|
|
|
|
codeErrInvalidChainConfig
|
|
|
|
codeErrZeroAddress
|
|
|
|
codeErrEmptyHash
|
|
|
|
codeErrBloomNotFound
|
|
|
|
codeErrTxReceiptNotFound
|
|
|
|
codeErrCreateDisabled
|
|
|
|
codeErrCallDisabled
|
|
|
|
codeErrInvalidAmount
|
|
|
|
codeErrInvalidGasPrice
|
|
|
|
codeErrVMExecution
|
2021-06-11 14:29:28 +00:00
|
|
|
codeErrInvalidRefund
|
|
|
|
codeErrInconsistentGas
|
2021-05-31 14:54:59 +00:00
|
|
|
)
|
2020-07-13 20:01:45 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidState returns an error resulting from an invalid Storage State.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrInvalidState = sdkerrors.Register(ModuleName, codeErrInvalidState, "invalid storage state")
|
|
|
|
|
|
|
|
// ErrExecutionReverted returns an error resulting from an error in EVM execution.
|
|
|
|
ErrExecutionReverted = sdkerrors.Register(ModuleName, codeErrExecutionReverted, vm.ErrExecutionReverted.Error())
|
2020-09-02 19:41:05 +00:00
|
|
|
|
|
|
|
// ErrChainConfigNotFound returns an error if the chain config cannot be found on the store.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrChainConfigNotFound = sdkerrors.Register(ModuleName, codeErrChainConfigNotFound, "chain configuration not found")
|
2020-09-02 19:41:05 +00:00
|
|
|
|
|
|
|
// ErrInvalidChainConfig returns an error resulting from an invalid ChainConfig.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrInvalidChainConfig = sdkerrors.Register(ModuleName, codeErrInvalidChainConfig, "invalid chain configuration")
|
2020-11-20 19:25:11 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// ErrZeroAddress returns an error resulting from an zero (empty) ethereum Address.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrZeroAddress = sdkerrors.Register(ModuleName, codeErrZeroAddress, "invalid zero address")
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
// ErrEmptyHash returns an error resulting from an empty ethereum Hash.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrEmptyHash = sdkerrors.Register(ModuleName, codeErrEmptyHash, "empty hash")
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
// ErrBloomNotFound returns an error if the block bloom cannot be found on the store.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrBloomNotFound = sdkerrors.Register(ModuleName, codeErrBloomNotFound, "block bloom not found")
|
2021-04-18 15:54:18 +00:00
|
|
|
|
|
|
|
// ErrTxReceiptNotFound returns an error if the transaction receipt could not be found
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrTxReceiptNotFound = sdkerrors.Register(ModuleName, codeErrTxReceiptNotFound, "transaction receipt not found")
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2020-11-20 19:25:11 +00:00
|
|
|
// ErrCreateDisabled returns an error if the EnableCreate parameter is false.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrCreateDisabled = sdkerrors.Register(ModuleName, codeErrCreateDisabled, "EVM Create operation is disabled")
|
2020-11-20 19:25:11 +00:00
|
|
|
|
|
|
|
// ErrCallDisabled returns an error if the EnableCall parameter is false.
|
2021-05-31 14:54:59 +00:00
|
|
|
ErrCallDisabled = sdkerrors.Register(ModuleName, codeErrCallDisabled, "EVM Call operation is disabled")
|
|
|
|
|
|
|
|
// ErrInvalidAmount returns an error if a tx contains an invalid amount.
|
|
|
|
ErrInvalidAmount = sdkerrors.Register(ModuleName, codeErrInvalidAmount, "invalid transaction amount")
|
|
|
|
|
|
|
|
// ErrInvalidGasPrice returns an error if an invalid gas price is provided to the tx.
|
|
|
|
ErrInvalidGasPrice = sdkerrors.Register(ModuleName, codeErrInvalidGasPrice, "invalid gas price")
|
|
|
|
|
|
|
|
// ErrVMExecution returns an error resulting from an error in EVM execution.
|
|
|
|
ErrVMExecution = sdkerrors.Register(ModuleName, codeErrVMExecution, "evm transaction execution failed")
|
2021-06-11 14:29:28 +00:00
|
|
|
|
|
|
|
// ErrInvalidRefund returns an error if a the gas refund value is invalid.
|
|
|
|
ErrInvalidRefund = sdkerrors.Register(ModuleName, codeErrInvalidRefund, "invalid gas refund amount")
|
|
|
|
|
|
|
|
// ErrInconsistentGas returns an error if a the gas differs from the expected one.
|
|
|
|
ErrInconsistentGas = sdkerrors.Register(ModuleName, codeErrInconsistentGas, "inconsistent gas")
|
2020-07-13 20:01:45 +00:00
|
|
|
)
|
2021-05-31 14:54:59 +00:00
|
|
|
|
2021-07-08 08:14:11 +00:00
|
|
|
// NewExecErrorWithReason unpacks the revert return bytes and returns a wrapped error
|
2021-05-31 14:54:59 +00:00
|
|
|
// with the return reason.
|
2021-07-28 15:50:05 +00:00
|
|
|
func NewExecErrorWithReason(revertReason []byte) *RevertError {
|
|
|
|
var result = common.CopyBytes(revertReason)
|
|
|
|
reason, errUnpack := abi.UnpackRevert(result)
|
|
|
|
err := errors.New("execution reverted")
|
2021-05-31 14:54:59 +00:00
|
|
|
if errUnpack == nil {
|
2021-07-28 15:50:05 +00:00
|
|
|
err = fmt.Errorf("execution reverted: %v", reason)
|
|
|
|
}
|
|
|
|
return &RevertError{
|
|
|
|
error: err,
|
|
|
|
reason: hexutil.Encode(result),
|
2021-05-31 14:54:59 +00:00
|
|
|
}
|
2021-07-28 15:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RevertError is an API error that encompass an EVM revert with JSON error
|
|
|
|
// code and a binary data blob.
|
|
|
|
type RevertError struct {
|
|
|
|
error
|
|
|
|
reason string // revert reason hex encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorCode returns the JSON error code for a revert.
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
|
|
|
|
func (e *RevertError) ErrorCode() int {
|
|
|
|
return 3
|
|
|
|
}
|
2021-05-31 14:54:59 +00:00
|
|
|
|
2021-07-28 15:50:05 +00:00
|
|
|
// ErrorData returns the hex encoded revert reason.
|
|
|
|
func (e *RevertError) ErrorData() interface{} {
|
|
|
|
return e.reason
|
2021-05-31 14:54:59 +00:00
|
|
|
}
|