2021-01-06 20:56:40 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"context"
|
2021-08-05 16:24:06 +00:00
|
|
|
"fmt"
|
2021-05-14 21:44:58 +00:00
|
|
|
"time"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-05-14 21:44:58 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2021-06-11 14:29:28 +00:00
|
|
|
"github.com/palantir/stacktrace"
|
2021-05-10 16:34:00 +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-14 21:44:58 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
2021-05-10 16:34:00 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
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-05-14 21:44:58 +00:00
|
|
|
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), types.TypeMsgEthereumTx)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-01-06 20:56:40 +00:00
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
sender := msg.From
|
|
|
|
tx := msg.AsTransaction()
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
labels := []metrics.Label{telemetry.NewLabel("tx_type", fmt.Sprintf("%d", tx.Type()))}
|
2021-07-05 16:39:08 +00:00
|
|
|
if tx.To() == nil {
|
2021-05-14 21:44:58 +00:00
|
|
|
labels = []metrics.Label{
|
|
|
|
telemetry.NewLabel("execution", "create"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
labels = []metrics.Label{
|
|
|
|
telemetry.NewLabel("execution", "call"),
|
2021-07-05 16:39:08 +00:00
|
|
|
telemetry.NewLabel("to", tx.To().Hex()), // recipient address (contract or account)
|
2021-05-14 21:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
response, err := k.ApplyTransaction(tx)
|
2021-01-06 20:56:40 +00:00
|
|
|
if err != nil {
|
2021-06-11 14:29:28 +00:00
|
|
|
return nil, stacktrace.Propagate(err, "failed to apply transaction")
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:44:58 +00:00
|
|
|
defer func() {
|
2021-06-08 17:10:29 +00:00
|
|
|
if tx.Value().IsInt64() {
|
2021-05-14 21:44:58 +00:00
|
|
|
telemetry.SetGauge(
|
2021-06-08 17:10:29 +00:00
|
|
|
float32(tx.Value().Int64()),
|
2021-05-14 21:44:58 +00:00
|
|
|
"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),
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-05 16:39:08 +00:00
|
|
|
if tx.To() != nil {
|
|
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyRecipient, tx.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-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
|
|
|
),
|
|
|
|
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
|
|
|
}
|