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
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
2019-09-18 13:51:18 +00:00
|
|
|
emint "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.
|
2020-03-09 13:17:23 +00:00
|
|
|
func NewHandler(k Keeper) sdk.Handler {
|
2020-04-22 19:26:01 +00:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
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:
|
2020-04-23 15:49:25 +00:00
|
|
|
return handleMsgEthereumTx(ctx, k, msg)
|
2020-04-01 18:49:21 +00:00
|
|
|
case types.MsgEthermint:
|
2020-04-23 15:49:25 +00:00
|
|
|
return 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
// handleMsgEthereumTx handles an Ethereum specific tx
|
|
|
|
func handleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) (*sdk.Result, error) {
|
2019-09-18 13:51:18 +00:00
|
|
|
// parse the chainID from a string to a base-10 integer
|
|
|
|
intChainID, ok := new(big.Int).SetString(ctx.ChainID(), 10)
|
|
|
|
if !ok {
|
2020-04-22 19:26:01 +00:00
|
|
|
return nil, sdkerrors.Wrap(emint.ErrInvalidChainID, ctx.ChainID())
|
2019-08-14 23:52:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 13:51:18 +00:00
|
|
|
// Verify signature and retrieve sender address
|
|
|
|
sender, err := msg.VerifySig(intChainID)
|
|
|
|
if err != nil {
|
2020-04-22 19:26:01 +00:00
|
|
|
return nil, err
|
2019-09-18 13:51:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 23:08:43 +00:00
|
|
|
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
|
|
|
|
ethHash := common.BytesToHash(txHash)
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
st := types.StateTransition{
|
|
|
|
AccountNonce: msg.Data.AccountNonce,
|
|
|
|
Price: msg.Data.Price,
|
|
|
|
GasLimit: msg.Data.GasLimit,
|
|
|
|
Recipient: msg.Data.Recipient,
|
|
|
|
Amount: msg.Data.Amount,
|
|
|
|
Payload: msg.Data.Payload,
|
2020-03-09 13:17:23 +00:00
|
|
|
Csdb: k.CommitStateDB.WithContext(ctx),
|
2019-10-03 16:46:02 +00:00
|
|
|
ChainID: intChainID,
|
2020-04-30 03:36:30 +00:00
|
|
|
TxHash: ðHash,
|
|
|
|
Sender: sender,
|
2019-10-22 16:40:34 +00:00
|
|
|
Simulate: ctx.IsCheckTx(),
|
2019-09-18 13:51:18 +00:00
|
|
|
}
|
2020-04-13 23:08:43 +00:00
|
|
|
|
2020-09-23 15:21:21 +00:00
|
|
|
// since the txCount is used by the stateDB, and a simulated tx is run only on the node it's submitted to,
|
|
|
|
// then this will cause the txCount/stateDB of the node that ran the simulated tx to be different than the
|
|
|
|
// other nodes, causing a consensus error
|
2020-09-17 00:29:22 +00:00
|
|
|
if !st.Simulate {
|
2020-09-23 15:21:21 +00:00
|
|
|
// Prepare db for logs
|
|
|
|
// TODO: block hash
|
2020-09-17 00:29:22 +00:00
|
|
|
k.CommitStateDB.Prepare(ethHash, common.Hash{}, k.TxCount)
|
|
|
|
k.TxCount++
|
|
|
|
}
|
2019-08-14 23:52:45 +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
|
2019-10-30 18:39:46 +00:00
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
2020-09-17 00:29:22 +00:00
|
|
|
if !st.Simulate {
|
|
|
|
// update block bloom filter
|
|
|
|
k.Bloom.Or(k.Bloom, executionResult.Bloom)
|
2020-04-01 18:49:21 +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-01 18:49: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.EventTypeEthereumTx,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Data.Amount.String()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
if msg.Data.Recipient != nil {
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeEthereumTx,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyRecipient, msg.Data.Recipient.String()),
|
|
|
|
),
|
|
|
|
)
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|
2019-09-18 13:51:18 +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
|
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
|
|
|
|
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
|
|
|
|
intChainID, ok := new(big.Int).SetString(ctx.ChainID(), 10)
|
|
|
|
if !ok {
|
2020-04-22 19:26:01 +00:00
|
|
|
return nil, sdkerrors.Wrap(emint.ErrInvalidChainID, ctx.ChainID())
|
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),
|
2019-10-03 16:46:02 +00:00
|
|
|
ChainID: intChainID,
|
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-09-17 00:29:22 +00:00
|
|
|
k.CommitStateDB.Prepare(ethHash, common.Hash{}, k.TxCount)
|
|
|
|
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
|
|
|
}
|