2018-11-28 22:19:22 +00:00
|
|
|
|
package evm
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2019-09-18 13:51:18 +00:00
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
2020-09-24 17:50:47 +00:00
|
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2019-10-03 16:46:02 +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"
|
|
|
|
|
|
2020-03-09 13:17:23 +00:00
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewHandler returns a handler for Ethermint type messages.
|
2021-01-07 11:55:01 +00:00
|
|
|
|
func NewHandler(k *Keeper) sdk.Handler {
|
2021-01-08 12:28:25 +00:00
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (result *sdk.Result, err error) {
|
|
|
|
|
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())
|
2019-08-14 23:52:45 +00:00
|
|
|
|
switch msg := msg.(type) {
|
2020-04-01 18:49:21 +00:00
|
|
|
|
case types.MsgEthereumTx:
|
2021-01-08 12:28:25 +00:00
|
|
|
|
result, err = handleMsgEthereumTx(ctx, k, msg)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
case types.MsgEthermint:
|
2021-01-08 12:28:25 +00:00
|
|
|
|
result, err = handleMsgEthermint(ctx, k, msg)
|
2019-08-14 23:52:45 +00:00
|
|
|
|
default:
|
2020-04-22 19:26:01 +00:00
|
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
2019-08-14 23:52:45 +00:00
|
|
|
|
}
|
2021-01-08 12:28:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
types.CopyCommitStateDB(snapshotStateDB, k.CommitStateDB)
|
|
|
|
|
}
|
|
|
|
|
return result, err
|
2019-08-14 23:52:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
|
// handleMsgEthereumTx handles an Ethereum specific tx
|
2021-01-07 11:55:01 +00:00
|
|
|
|
func handleMsgEthereumTx(ctx sdk.Context, k *Keeper, msg types.MsgEthereumTx) (*sdk.Result, error) {
|
2021-01-06 20:56:40 +00:00
|
|
|
|
// execute state transition
|
|
|
|
|
res, err := k.EthereumTx(ctx, msg)
|
2019-09-18 13:51:18 +00:00
|
|
|
|
if err != nil {
|
2020-04-22 19:26:01 +00:00
|
|
|
|
return nil, err
|
2019-09-18 13:51:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
|
// log state transition result
|
|
|
|
|
k.Logger(ctx).Info(res.Log)
|
2019-09-18 13:51:18 +00:00
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
|
return res, nil
|
2020-04-01 18:49:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
|
// handleMsgEthermint handles an sdk.StdTx for an Ethereum state transition
|
2021-01-07 11:55:01 +00:00
|
|
|
|
func handleMsgEthermint(ctx sdk.Context, k *Keeper, msg types.MsgEthermint) (*sdk.Result, error) {
|
2019-10-03 16:46:02 +00:00
|
|
|
|
// parse the chainID from a string to a base-10 integer
|
2020-09-24 17:50:47 +00:00
|
|
|
|
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2019-10-03 16:46:02 +00:00
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
|
2020-04-10 20:47:56 +00:00
|
|
|
|
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
|
|
|
|
|
ethHash := common.BytesToHash(txHash)
|
|
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
|
st := types.StateTransition{
|
|
|
|
|
AccountNonce: msg.AccountNonce,
|
|
|
|
|
Price: msg.Price.BigInt(),
|
|
|
|
|
GasLimit: msg.GasLimit,
|
|
|
|
|
Amount: msg.Amount.BigInt(),
|
|
|
|
|
Payload: msg.Payload,
|
2020-03-09 13:17:23 +00:00
|
|
|
|
Csdb: k.CommitStateDB.WithContext(ctx),
|
2020-09-24 17:50:47 +00:00
|
|
|
|
ChainID: chainIDEpoch,
|
2020-04-30 03:36:30 +00:00
|
|
|
|
TxHash: ðHash,
|
2020-05-04 22:02:26 +00:00
|
|
|
|
Sender: common.BytesToAddress(msg.From.Bytes()),
|
2019-10-22 16:40:34 +00:00
|
|
|
|
Simulate: ctx.IsCheckTx(),
|
2019-10-03 16:46:02 +00:00
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
|
if msg.Recipient != nil {
|
|
|
|
|
to := common.BytesToAddress(msg.Recipient.Bytes())
|
|
|
|
|
st.Recipient = &to
|
2019-09-18 13:51:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 00:29:22 +00:00
|
|
|
|
if !st.Simulate {
|
2020-09-23 15:21:21 +00:00
|
|
|
|
// Prepare db for logs
|
2020-12-23 20:15:38 +00:00
|
|
|
|
blockHash := types.HashFromContext(ctx)
|
|
|
|
|
k.CommitStateDB.Prepare(ethHash, blockHash, k.TxCount)
|
2020-09-17 00:29:22 +00:00
|
|
|
|
k.TxCount++
|
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
|
config, found := k.GetChainConfig(ctx)
|
|
|
|
|
if !found {
|
|
|
|
|
return nil, types.ErrChainConfigNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
executionResult, err := st.TransitionDb(ctx, config)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
if err != nil {
|
2020-04-22 19:26:01 +00:00
|
|
|
|
return nil, err
|
2020-04-01 18:49:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 03:36:30 +00:00
|
|
|
|
// update block bloom filter
|
2020-09-17 00:29:22 +00:00
|
|
|
|
if !st.Simulate {
|
|
|
|
|
k.Bloom.Or(k.Bloom, executionResult.Bloom)
|
2020-04-30 03:36:30 +00:00
|
|
|
|
|
2020-09-17 00:29:22 +00:00
|
|
|
|
// update transaction logs in KVStore
|
|
|
|
|
err = k.SetLogs(ctx, common.BytesToHash(txHash), executionResult.Logs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
|
}
|
2020-04-30 03:36:30 +00:00
|
|
|
|
|
|
|
|
|
// log successful execution
|
|
|
|
|
k.Logger(ctx).Info(executionResult.Result.Log)
|
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
|
sdk.NewEvent(
|
|
|
|
|
types.EventTypeEthermint,
|
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()),
|
|
|
|
|
),
|
|
|
|
|
sdk.NewEvent(
|
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.String()),
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if msg.Recipient != nil {
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
|
sdk.NewEvent(
|
|
|
|
|
types.EventTypeEthermint,
|
|
|
|
|
sdk.NewAttribute(types.AttributeKeyRecipient, msg.Recipient.String()),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
|
// set the events to the result
|
2020-08-23 21:41:54 +00:00
|
|
|
|
executionResult.Result.Events = ctx.EventManager().Events()
|
2020-04-30 03:36:30 +00:00
|
|
|
|
return executionResult.Result, nil
|
2019-08-14 23:52:45 +00:00
|
|
|
|
}
|