laconicd-deprecated/x/evm/keeper/msg_server.go
yihuang 7d8664043e
impr: support batch eth txs (#901)
* support batch eth tx

Closes: 896

Allow multiple MsgEthereumTx in single tx

* fix transaction receipt api

* fix tx receipt api and accumulate tx gas used

* fix lint

* fix test

* fix rpc test

* cleanup

* fix cumulativeGasUsed and gasUsed

* fix lint

* Update app/ante/eth.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update app/ante/eth.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update rpc/ethereum/backend/utils.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* pr suggestions

* typo

* fix lint

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2022-01-14 10:37:33 +01:00

89 lines
2.7 KiB
Go

package keeper
import (
"context"
"encoding/json"
"fmt"
"strconv"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmtypes "github.com/tendermint/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tharsis/ethermint/x/evm/types"
)
var _ types.MsgServer = &Keeper{}
// 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.
func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sender := msg.From
tx := msg.AsTransaction()
txIndex := k.GetTxIndexTransient(ctx)
response, err := k.ApplyTransaction(ctx, tx)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to apply transaction")
}
attrs := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyAmount, tx.Value().String()),
// add event for ethereum transaction hash format
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, response.Hash),
// add event for index of valid ethereum tx
sdk.NewAttribute(types.AttributeKeyTxIndex, strconv.FormatUint(txIndex, 10)),
// add event for eth tx gas used, we can't get it from cosmos tx result when it contains multiple eth tx msgs.
sdk.NewAttribute(types.AttributeKeyTxGasUsed, strconv.FormatUint(response.GasUsed, 10)),
}
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()))
}
if to := tx.To(); to != nil {
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyRecipient, to.Hex()))
}
if response.Failed() {
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, response.VmError))
}
txLogAttrs := make([]sdk.Attribute, len(response.Logs))
for i, log := range response.Logs {
value, err := json.Marshal(log)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to encode log")
}
txLogAttrs[i] = sdk.NewAttribute(types.AttributeKeyTxLog, string(value))
}
// emit events
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthereumTx,
attrs...,
),
sdk.NewEvent(
types.EventTypeTxLog,
txLogAttrs...,
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender),
sdk.NewAttribute(types.AttributeKeyTxType, fmt.Sprintf("%d", tx.Type())),
),
})
return response, nil
}