2021-01-06 20:56:40 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"context"
|
2021-10-05 10:26:31 +00:00
|
|
|
"encoding/json"
|
2021-08-05 16:24:06 +00:00
|
|
|
"fmt"
|
2021-12-16 22:30:22 +00:00
|
|
|
"strconv"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
tmbytes "github.com/tendermint/tendermint/libs/bytes"
|
2021-04-18 15:54:18 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2021-01-06 20:56:40 +00:00
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-11-26 14:19:28 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-05-10 16:34:00 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
2021-01-06 20:56:40 +00:00
|
|
|
)
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
var _ types.MsgServer = &Keeper{}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// EthereumTx implements the gRPC MsgServer interface. It receives a transaction which is then
|
|
|
|
// executed (i.e applied) against the go-ethereum EVM. The provided SDK Context is set to the Keeper
|
|
|
|
// so that it can implements and call the StateDB methods without receiving it as a function
|
|
|
|
// parameter.
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
2021-01-06 20:56:40 +00:00
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
sender := msg.From
|
|
|
|
tx := msg.AsTransaction()
|
2022-01-05 07:28:27 +00:00
|
|
|
txIndex := k.GetTxIndexTransient(ctx)
|
2021-07-05 16:39:08 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
response, err := k.ApplyTransaction(ctx, tx)
|
2021-01-06 20:56:40 +00:00
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return nil, sdkerrors.Wrap(err, "failed to apply transaction")
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
attrs := []sdk.Attribute{
|
2021-06-08 17:10:29 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, tx.Value().String()),
|
|
|
|
// add event for ethereum transaction hash format
|
|
|
|
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, response.Hash),
|
2021-12-16 22:30:22 +00:00
|
|
|
// add event for index of valid ethereum tx
|
|
|
|
sdk.NewAttribute(types.AttributeKeyTxIndex, strconv.FormatInt(int64(txIndex), 10)),
|
2021-06-08 17:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.TxBytes()) > 0 {
|
|
|
|
// add event for tendermint transaction hash format
|
|
|
|
hash := tmbytes.HexBytes(tmtypes.Tx(ctx.TxBytes()).Hash())
|
|
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyTxHash, hash.String()))
|
2021-05-31 14:54:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 23:51:36 +00:00
|
|
|
if to := tx.To(); to != nil {
|
|
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyRecipient, to.Hex()))
|
2021-05-31 14:54:59 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 06:01:05 +00:00
|
|
|
if response.Failed() {
|
|
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, response.VmError))
|
2021-07-08 08:14:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 00:05:12 +00:00
|
|
|
txLogAttrs := make([]sdk.Attribute, len(response.Logs))
|
|
|
|
for i, log := range response.Logs {
|
2021-10-05 10:26:31 +00:00
|
|
|
value, err := json.Marshal(log)
|
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return nil, sdkerrors.Wrap(err, "failed to encode log")
|
2021-10-05 10:26:31 +00:00
|
|
|
}
|
2021-12-14 00:05:12 +00:00
|
|
|
txLogAttrs[i] = sdk.NewAttribute(types.AttributeKeyTxLog, string(value))
|
2021-09-15 09:45:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
// emit events
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeEthereumTx,
|
2021-05-31 14:54:59 +00:00
|
|
|
attrs...,
|
2021-04-18 15:54:18 +00:00
|
|
|
),
|
2021-09-15 09:45:03 +00:00
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeTxLog,
|
|
|
|
txLogAttrs...,
|
|
|
|
),
|
2021-04-18 15:54:18 +00:00
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
2021-06-08 17:10:29 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, sender),
|
2021-08-05 16:24:06 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyTxType, fmt.Sprintf("%d", tx.Type())),
|
2021-04-18 15:54:18 +00:00
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
return response, nil
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|