2018-11-28 22:19:22 +00:00
|
|
|
|
package evm
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
"github.com/cosmos/ethermint/x/evm/keeper"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2019-10-03 16:46:02 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewHandler returns a handler for Ethermint type messages.
|
2021-04-17 10:00:07 +00:00
|
|
|
|
func NewHandler(k keeper.Keeper) sdk.Handler {
|
|
|
|
|
defer telemetry.MeasureSince(time.Now(), "evm", "state_transition")
|
|
|
|
|
|
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
2021-01-08 12:28:25 +00:00
|
|
|
|
snapshotStateDB := k.CommitStateDB.Copy()
|
|
|
|
|
|
|
|
|
|
// The "recover" code here is used to solve the problem of dirty data
|
|
|
|
|
// in CommitStateDB due to insufficient gas.
|
|
|
|
|
|
|
|
|
|
// The following is a detailed description:
|
|
|
|
|
// If the gas is insufficient during the execution of the "handler",
|
|
|
|
|
// panic will be thrown from the function "ConsumeGas" and finally
|
|
|
|
|
// caught by the function "runTx" from Cosmos. The function "runTx"
|
|
|
|
|
// will think that the execution of Msg has failed and the modified
|
|
|
|
|
// data in the Store will not take effect.
|
|
|
|
|
|
|
|
|
|
// Stacktrace:runTx->runMsgs->handler->...->gaskv.Store.Set->ConsumeGas
|
|
|
|
|
|
|
|
|
|
// The problem is that when the modified data in the Store does not take
|
|
|
|
|
// effect, the data in the modified CommitStateDB is not rolled back,
|
|
|
|
|
// they take effect, and dirty data is generated.
|
|
|
|
|
// Therefore, the code here specifically deals with this situation.
|
|
|
|
|
// See https://github.com/cosmos/ethermint/issues/668 for more information.
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
// We first used "k.CommitStateDB = snapshotStateDB" to roll back
|
|
|
|
|
// CommitStateDB, but this can only change the CommitStateDB in the
|
|
|
|
|
// current Keeper object, but the Keeper object will be destroyed
|
|
|
|
|
// soon, it is not a global variable, so the content pointed to by
|
|
|
|
|
// the CommitStateDB pointer can be modified to take effect.
|
|
|
|
|
types.CopyCommitStateDB(snapshotStateDB, k.CommitStateDB)
|
|
|
|
|
panic(r)
|
|
|
|
|
}
|
|
|
|
|
}()
|
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
|
|
|
|
|
res, err := k.EthereumTx(sdk.WrapSDKContext(ctx), msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
result, err := sdk.WrapServiceResult(ctx, res, err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
// log state transition result
|
|
|
|
|
var recipientLog string
|
|
|
|
|
if res.ContractAddress != "" {
|
|
|
|
|
recipientLog = fmt.Sprintf("contract address %s", res.ContractAddress)
|
|
|
|
|
} else {
|
|
|
|
|
recipientLog = fmt.Sprintf("recipient address %s", msg.Data.Recipient)
|
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
sender := ethcmn.BytesToAddress(msg.GetFrom().Bytes())
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
log := fmt.Sprintf(
|
|
|
|
|
"executed EVM state transition; sender address %s; %s", sender, recipientLog,
|
|
|
|
|
)
|
2020-09-02 19:41:05 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
k.Logger(ctx).Info(log)
|
|
|
|
|
result.Log = log
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
return result, nil
|
2020-04-30 03:36:30 +00:00
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
|
default:
|
|
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
|
2020-09-17 00:29:22 +00:00
|
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
|
}
|
2019-08-14 23:52:45 +00:00
|
|
|
|
}
|