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
|
|
|
|
|
|
|
tm "github.com/tendermint/tendermint/types"
|
2019-08-14 23:52:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewHandler returns a handler for Ethermint type messages.
|
|
|
|
func NewHandler(keeper Keeper) sdk.Handler {
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case types.EthereumTxMsg:
|
|
|
|
return handleETHTxMsg(ctx, keeper, msg)
|
2019-10-22 16:40:34 +00:00
|
|
|
case *types.EmintMsg:
|
|
|
|
return handleEmintMsg(ctx, keeper, *msg)
|
2019-08-14 23:52:45 +00:00
|
|
|
default:
|
|
|
|
errMsg := fmt.Sprintf("Unrecognized ethermint Msg type: %v", msg.Type())
|
|
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle an Ethereum specific tx
|
|
|
|
func handleETHTxMsg(ctx sdk.Context, keeper Keeper, msg types.EthereumTxMsg) sdk.Result {
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
2019-09-18 13:51:18 +00:00
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return emint.ErrInvalidSender(err.Error()).Result()
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return sdk.ErrInternal(err.Error()).Result()
|
|
|
|
}
|
|
|
|
txHash := tm.Tx(txBytes).Hash()
|
|
|
|
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,
|
2019-10-18 23:23:09 +00:00
|
|
|
Csdb: keeper.csdb.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
|
2019-10-04 19:32:56 +00:00
|
|
|
keeper.csdb.Prepare(ethHash, common.Hash{}, keeper.txCount.get())
|
2019-10-03 16:46:02 +00:00
|
|
|
keeper.txCount.increment()
|
2019-08-14 23:52:45 +00:00
|
|
|
|
2019-10-29 15:52:26 +00:00
|
|
|
bloom, res := st.TransitionCSDB(ctx)
|
2019-10-30 18:39:46 +00:00
|
|
|
if res.IsOK() {
|
|
|
|
keeper.bloom.Or(keeper.bloom, bloom)
|
|
|
|
}
|
2019-10-04 19:32:56 +00:00
|
|
|
return res
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
func handleEmintMsg(ctx sdk.Context, keeper Keeper, msg types.EmintMsg) sdk.Result {
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2019-09-18 13:51:18 +00:00
|
|
|
|
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,
|
2019-10-18 23:23:09 +00:00
|
|
|
Csdb: keeper.csdb.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
|
|
|
|
keeper.csdb.Prepare(common.Hash{}, common.Hash{}, keeper.txCount.get()) // Cannot provide tx hash
|
|
|
|
keeper.txCount.increment()
|
2019-08-14 23:52:45 +00:00
|
|
|
|
2019-10-29 15:52:26 +00:00
|
|
|
_, res := st.TransitionCSDB(ctx)
|
2019-10-04 19:32:56 +00:00
|
|
|
return res
|
2019-08-14 23:52:45 +00:00
|
|
|
}
|