2018-11-28 22:19:22 +00:00
|
|
|
package evm
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-19 16:55:13 +00:00
|
|
|
"fmt"
|
2021-04-18 15:54:18 +00:00
|
|
|
"runtime/debug"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
tmlog "github.com/tendermint/tendermint/libs/log"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2020-07-02 15:19:48 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
2019-08-14 23:52:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewHandler returns a handler for Ethermint type messages.
|
2021-06-08 17:10:29 +00:00
|
|
|
func NewHandler(server types.MsgServer) sdk.Handler {
|
2021-04-18 15:54:18 +00:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (result *sdk.Result, err error) {
|
2021-08-19 16:55:13 +00:00
|
|
|
defer Recover(ctx.Logger(), &err)
|
2021-01-08 12:28:25 +00:00
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.MsgEthereumTx:
|
|
|
|
// execute state transition
|
2021-06-08 17:10:29 +00:00
|
|
|
res, err := server.EthereumTx(sdk.WrapSDKContext(ctx), msg)
|
2021-06-08 11:11:37 +00:00
|
|
|
return sdk.WrapServiceResult(ctx, res, err)
|
2020-04-30 03:36:30 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
default:
|
2021-04-18 15:54:18 +00:00
|
|
|
err := sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
func Recover(logger tmlog.Logger, err *error) {
|
2021-04-18 15:54:18 +00:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
*err = sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r)
|
|
|
|
|
|
|
|
if e, ok := r.(error); ok {
|
2021-08-19 16:55:13 +00:00
|
|
|
logger.Error(
|
|
|
|
"message handler panicked",
|
|
|
|
"error", e,
|
|
|
|
"stack trace", string(debug.Stack()),
|
|
|
|
)
|
2021-04-18 15:54:18 +00:00
|
|
|
} else {
|
2021-08-19 16:55:13 +00:00
|
|
|
logger.Error(
|
|
|
|
"message handler panicked",
|
|
|
|
"recover", fmt.Sprintf("%v", r),
|
|
|
|
)
|
2020-09-17 00:29:22 +00:00
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
2019-08-14 23:52:45 +00:00
|
|
|
}
|