2018-11-28 22:19:22 +00:00
|
|
|
package evm
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-18 13:51:18 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-10-03 16:46:02 +00:00
|
|
|
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
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-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 {
|
2019-08-14 23:52:45 +00:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
|
|
switch msg := msg.(type) {
|
2020-04-01 18:49:21 +00:00
|
|
|
case types.MsgEthereumTx:
|
|
|
|
return HandleMsgEthereumTx(ctx, k, msg)
|
|
|
|
case types.MsgEthermint:
|
|
|
|
return HandleMsgEthermint(ctx, k, msg)
|
2019-08-14 23:52:45 +00:00
|
|
|
default:
|
2020-04-01 18:49:21 +00:00
|
|
|
errMsg := fmt.Sprintf("unrecognized ethermint msg type: %v", msg.Type())
|
2019-08-14 23:52:45 +00:00
|
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// HandleMsgEthereumTx handles an Ethereum specific tx
|
|
|
|
func HandleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) sdk.Result {
|
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
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 {
|
|
|
|
return emint.ErrInvalidChainID(fmt.Sprintf("invalid chainID: %s", ctx.ChainID())).Result()
|
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-01 18:49:21 +00:00
|
|
|
return sdk.ResultFromError(err)
|
2019-09-18 13:51:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
// Encode transaction by default Tx encoder
|
|
|
|
txEncoder := authutils.GetTxEncoder(types.ModuleCdc)
|
|
|
|
txBytes, err := txEncoder(msg)
|
|
|
|
if err != nil {
|
2020-04-01 18:49:21 +00:00
|
|
|
return sdk.ResultFromError(err)
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
2020-03-09 13:17:23 +00:00
|
|
|
txHash := tmtypes.Tx(txBytes).Hash()
|
2019-10-04 19:32:56 +00:00
|
|
|
ethHash := common.BytesToHash(txHash)
|
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
st := types.StateTransition{
|
|
|
|
Sender: sender,
|
|
|
|
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,
|
2019-10-04 19:32:56 +00:00
|
|
|
THash: ðHash,
|
2019-10-22 16:40:34 +00:00
|
|
|
Simulate: ctx.IsCheckTx(),
|
2019-09-18 13:51:18 +00:00
|
|
|
}
|
2019-10-03 16:46:02 +00:00
|
|
|
// Prepare db for logs
|
2020-04-01 18:49:21 +00:00
|
|
|
k.CommitStateDB.Prepare(ethHash, common.Hash{}, k.TxCount)
|
|
|
|
k.TxCount++
|
2019-08-14 23:52:45 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// TODO: move to keeper
|
|
|
|
returnData, err := st.TransitionCSDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ResultFromError(err)
|
2019-10-30 18:39:46 +00:00
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// update block bloom filter
|
|
|
|
k.Bloom.Or(k.Bloom, returnData.Bloom)
|
|
|
|
|
|
|
|
// update transaction logs in KVStore
|
|
|
|
err = k.SetTransactionLogs(ctx, returnData.Logs, txHash)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ResultFromError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
returnData.Result.Events = ctx.EventManager().Events()
|
|
|
|
return *returnData.Result
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMsgEthermint handles a MsgEthermint
|
|
|
|
func HandleMsgEthermint(ctx sdk.Context, k Keeper, msg types.MsgEthermint) sdk.Result {
|
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
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 {
|
|
|
|
return emint.ErrInvalidChainID(fmt.Sprintf("invalid chainID: %s", ctx.ChainID())).Result()
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
st := types.StateTransition{
|
|
|
|
Sender: common.BytesToAddress(msg.From.Bytes()),
|
|
|
|
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,
|
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
|
|
|
}
|
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
// Prepare db for logs
|
2020-04-01 18:49:21 +00:00
|
|
|
k.CommitStateDB.Prepare(common.Hash{}, common.Hash{}, k.TxCount) // Cannot provide tx hash
|
|
|
|
k.TxCount++
|
|
|
|
|
|
|
|
returnData, err := st.TransitionCSDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ResultFromError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
returnData.Result.Events = ctx.EventManager().Events()
|
|
|
|
return *returnData.Result
|
2019-08-14 23:52:45 +00:00
|
|
|
}
|