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
|
|
|
|
2022-05-30 08:24:29 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
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-05-30 08:24:29 +00:00
|
|
|
labels := []metrics.Label{telemetry.NewLabel("tx_type", fmt.Sprintf("%d", tx.Type()))}
|
|
|
|
if tx.To() == nil {
|
|
|
|
labels = []metrics.Label{
|
|
|
|
telemetry.NewLabel("execution", "create"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
labels = []metrics.Label{
|
|
|
|
telemetry.NewLabel("execution", "call"),
|
|
|
|
telemetry.NewLabel("to", tx.To().Hex()), // recipient address (contract or account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-30 08:24:29 +00:00
|
|
|
defer func() {
|
|
|
|
if tx.Value().IsInt64() {
|
|
|
|
telemetry.SetGauge(
|
|
|
|
float32(tx.Value().Int64()),
|
|
|
|
"tx", "msg", "ethereum_tx",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
telemetry.IncrCounterWithLabels(
|
|
|
|
[]string{types.ModuleName, "ethereum_tx"},
|
|
|
|
1,
|
|
|
|
labels,
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
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
|
2022-01-14 09:37:33 +00:00
|
|
|
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)),
|
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
|
|
|
}
|