error migration (#254)

* migrate errors

* ante handler errors

* return err instead of panic
This commit is contained in:
Federico Kunze
2020-04-16 11:47:39 -04:00
committed by GitHub
parent c9b09c1d55
commit c30205cae9
11 changed files with 156 additions and 170 deletions
+9 -8
View File
@@ -4,18 +4,19 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
)
// ModuleCdc defines the codec to be used by evm module
// ModuleCdc defines the evm module's codec
var ModuleCdc = codec.New()
func init() {
cdc := codec.New()
codec.RegisterCrypto(cdc)
ModuleCdc = cdc.Seal()
}
// RegisterCodec registers concrete types and interfaces on the given codec.
// RegisterCodec registers all the necessary types and interfaces for the
// evm module
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgEthereumTx{}, "ethermint/MsgEthereumTx", nil)
cdc.RegisterConcrete(MsgEthermint{}, "ethermint/MsgEthermint", nil)
cdc.RegisterConcrete(EncodableTxData{}, "ethermint/EncodableTxData", nil)
}
func init() {
RegisterCodec(ModuleCdc)
codec.RegisterCrypto(ModuleCdc)
ModuleCdc.Seal()
}
+7 -4
View File
@@ -1,9 +1,8 @@
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common"
@@ -61,12 +60,16 @@ func (msg MsgEthermint) GetSignBytes() []byte {
// ValidateBasic runs stateless checks on the message
func (msg MsgEthermint) ValidateBasic() sdk.Error {
if msg.Price.Sign() != 1 {
return types.ErrInvalidValue(fmt.Sprintf("Price must be positive: %x", msg.Price))
return sdk.ConvertError(
sdkerrors.Wrapf(types.ErrInvalidValue, "price must be positive %s", msg.Price),
)
}
// Amount can be 0
if msg.Amount.Sign() == -1 {
return types.ErrInvalidValue(fmt.Sprintf("amount cannot be negative: %x", msg.Amount))
return sdk.ConvertError(
sdkerrors.Wrapf(types.ErrInvalidValue, "amount cannot be negative %s", msg.Amount),
)
}
return nil
+15 -4
View File
@@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common"
@@ -127,12 +128,16 @@ func (msg MsgEthereumTx) Type() string { return TypeMsgEthereumTx }
// checks of a Transaction. If returns an error if validation fails.
func (msg MsgEthereumTx) ValidateBasic() sdk.Error {
if msg.Data.Price.Sign() != 1 {
return types.ErrInvalidValue(fmt.Sprintf("price must be positive: %x", msg.Data.Price))
return sdk.ConvertError(
sdkerrors.Wrapf(types.ErrInvalidValue, "price must be positive %s", msg.Data.Price),
)
}
// Amount can be 0
if msg.Data.Amount.Sign() == -1 {
return types.ErrInvalidValue(fmt.Sprintf("amount must be positive: %x", msg.Data.Amount))
return sdk.ConvertError(
sdkerrors.Wrapf(types.ErrInvalidValue, "amount cannot be negative %s", msg.Data.Amount),
)
}
return nil
@@ -306,12 +311,18 @@ func TxDecoder(cdc *codec.Codec) sdk.TxDecoder {
var tx sdk.Tx
if len(txBytes) == 0 {
return nil, sdk.ErrTxDecode("txBytes are empty")
return nil, sdk.ConvertError(
sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx bytes are empty"),
)
}
// sdk.Tx is an interface. The concrete message types
// are registered by MakeTxCodec
err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return nil, sdk.ErrTxDecode("failed to decode tx").TraceSDK(err.Error())
return nil, sdk.ConvertError(
sdkerrors.Wrap(sdkerrors.ErrTxDecode, err.Error()),
)
}
return tx, nil
+16 -8
View File
@@ -1,7 +1,7 @@
package types
import (
"fmt"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
@@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
emint "github.com/cosmos/ethermint/types"
)
@@ -39,12 +40,11 @@ type ReturnData struct {
// TransitionCSDB performs an evm state transition from a transaction
// TODO: update godoc, it doesn't explain what it does in depth.
func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
returnData := new(ReturnData)
contractCreation := st.Recipient == nil
cost, err := core.IntrinsicGas(st.Payload, contractCreation, true)
if err != nil {
return nil, fmt.Errorf("invalid intrinsic gas for transaction: %s", err.Error())
return nil, sdkerrors.Wrap(err, "invalid intrinsic gas for transaction")
}
// This gas limit the the transaction gas limit with intrinsic gas subtracted
@@ -73,6 +73,11 @@ func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
// Clear cache of accounts to handle changes outside of the EVM
csdb.UpdateAccounts()
gasPrice := ctx.MinGasPrices().AmountOf(emint.DenomDefault)
if gasPrice.IsNil() {
return nil, errors.New("gas price cannot be nil")
}
// Create context for evm
context := vm.Context{
CanTransfer: core.CanTransfer,
@@ -83,7 +88,7 @@ func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
Time: big.NewInt(ctx.BlockHeader().Time.Unix()),
Difficulty: big.NewInt(0), // unused. Only required in PoW context
GasLimit: gasLimit,
GasPrice: ctx.MinGasPrices().AmountOf(emint.DenomDefault).Int,
GasPrice: gasPrice.Int,
}
evm := vm.NewEVM(context, csdb, GenerateChainConfig(st.ChainID), vm.Config{})
@@ -150,7 +155,7 @@ func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
// handle errors
if err != nil {
if err == vm.ErrOutOfGas || err == vm.ErrCodeStoreOutOfGas {
return nil, fmt.Errorf("evm execution went out of gas: %s", err.Error())
return nil, sdkerrors.Wrap(err, "evm execution went out of gas")
}
// Consume gas before returning
@@ -177,8 +182,11 @@ func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
return nil, err
}
returnData.Logs = logs
returnData.Bloom = bloomInt
returnData.Result = &sdk.Result{Data: resultData, GasUsed: gasConsumed}
returnData := &ReturnData{
Logs: logs,
Bloom: bloomInt,
Result: &sdk.Result{Data: resultData},
}
return returnData, nil
}
+7 -7
View File
@@ -165,6 +165,10 @@ func (csdb *CommitStateDB) SetLogs(hash ethcmn.Hash, logs []*ethtypes.Log) error
return err
}
if len(enc) == 0 {
return nil
}
store.Set(LogsKey(hash[:]), enc)
return nil
}
@@ -310,18 +314,14 @@ func (csdb *CommitStateDB) GetLogs(hash ethcmn.Hash) ([]*ethtypes.Log, error) {
encLogs := store.Get(LogsKey(hash[:]))
if len(encLogs) == 0 {
// return nil if logs are not found
return []*ethtypes.Log{}, nil
}
logs, err := DecodeLogs(encLogs)
if err != nil {
return nil, err
}
return logs, nil
return DecodeLogs(encLogs)
}
// Logs returns all the current logs in the state.
// AllLogs returns all the current logs in the state.
func (csdb *CommitStateDB) AllLogs() []*ethtypes.Log {
// nolint: prealloc
var logs []*ethtypes.Log