laconicd/x/evm/handler.go

53 lines
1.2 KiB
Go
Raw Normal View History

2018-11-28 22:19:22 +00:00
package evm
import (
"fmt"
2021-04-18 15:54:18 +00:00
"runtime/debug"
tmlog "github.com/tendermint/tendermint/libs/log"
2021-04-17 10:00:07 +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
"github.com/tharsis/ethermint/x/evm/types"
)
// NewHandler returns a handler for Ethermint type messages.
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) {
defer Recover(ctx.Logger(), &err)
ctx = ctx.WithEventManager(sdk.NewEventManager())
2021-04-17 10:00:07 +00:00
switch msg := msg.(type) {
case *types.MsgEthereumTx:
// execute state transition
res, err := server.EthereumTx(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
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
}
}
}
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 {
logger.Error(
"message handler panicked",
"error", e,
"stack trace", string(debug.Stack()),
)
2021-04-18 15:54:18 +00:00
} else {
logger.Error(
"message handler panicked",
"recover", fmt.Sprintf("%v", r),
)
}
}
}