2021-01-06 20:56:40 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"context"
|
2021-05-10 16:34:00 +00:00
|
|
|
"errors"
|
2021-05-25 12:56:36 +00:00
|
|
|
"math/big"
|
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-04-18 15:54:18 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2021-05-10 16:34:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
|
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"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
var _ types.MsgServer = &Keeper{}
|
|
|
|
|
|
|
|
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-05-25 12:56:36 +00:00
|
|
|
k.CommitStateDB.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
ethMsg, err := msg.AsMessage()
|
2021-01-06 20:56:40 +00:00
|
|
|
if err != nil {
|
2021-05-10 16:34:00 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrorInvalidSigner, err.Error())
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
config, found := k.GetChainConfig(ctx)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrChainConfigNotFound
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:44:58 +00:00
|
|
|
var labels []metrics.Label
|
|
|
|
if msg.To() == nil {
|
|
|
|
labels = []metrics.Label{
|
|
|
|
telemetry.NewLabel("execution", "create"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
labels = []metrics.Label{
|
|
|
|
telemetry.NewLabel("execution", "call"),
|
|
|
|
// add label to the called recipient address (contract or account)
|
|
|
|
telemetry.NewLabel("to", msg.Data.To),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
sender := ethMsg.From()
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
|
|
|
|
ethHash := ethcmn.BytesToHash(txHash)
|
2021-05-05 13:10:21 +00:00
|
|
|
blockHash, _ := k.GetBlockHashFromHeight(ctx, ctx.BlockHeight())
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
st := &types.StateTransition{
|
2021-05-10 16:34:00 +00:00
|
|
|
Message: ethMsg,
|
|
|
|
Csdb: k.CommitStateDB.WithContext(ctx),
|
|
|
|
ChainID: msg.ChainID(),
|
|
|
|
TxHash: ðHash,
|
|
|
|
Simulate: ctx.IsCheckTx(),
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// since the txCount is used by the stateDB, and a simulated tx is run only on the node it's submitted to,
|
|
|
|
// then this will cause the txCount/stateDB of the node that ran the simulated tx to be different than the
|
|
|
|
// other nodes, causing a consensus error
|
|
|
|
if !st.Simulate {
|
|
|
|
// Prepare db for logs
|
2021-05-25 12:56:36 +00:00
|
|
|
k.CommitStateDB.Prepare(ethHash, blockHash, int(k.GetTxIndexTransient()))
|
|
|
|
k.IncreaseTxIndexTransient()
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
executionResult, err := st.TransitionDb(ctx, config)
|
|
|
|
if err != nil {
|
2021-05-10 16:34:00 +00:00
|
|
|
if errors.Is(err, vm.ErrExecutionReverted) && executionResult != nil {
|
2021-04-18 15:54:18 +00:00
|
|
|
// keep the execution result for revert reason
|
|
|
|
executionResult.Response.Reverted = true
|
|
|
|
|
|
|
|
if !st.Simulate {
|
|
|
|
k.SetTxReceiptToHash(ctx, ethHash, &types.TxReceipt{
|
2021-05-14 06:52:18 +00:00
|
|
|
Hash: ethHash.Hex(),
|
|
|
|
From: sender.Hex(),
|
2021-04-18 15:54:18 +00:00
|
|
|
Data: msg.Data,
|
|
|
|
BlockHeight: uint64(ctx.BlockHeight()),
|
2021-05-14 06:52:18 +00:00
|
|
|
BlockHash: blockHash.Hex(),
|
2021-04-18 15:54:18 +00:00
|
|
|
Result: &types.TxResult{
|
|
|
|
ContractAddress: executionResult.Response.ContractAddress,
|
|
|
|
Bloom: executionResult.Response.Bloom,
|
|
|
|
TxLogs: executionResult.Response.TxLogs,
|
|
|
|
Ret: executionResult.Response.Ret,
|
|
|
|
Reverted: executionResult.Response.Reverted,
|
|
|
|
GasUsed: executionResult.GasInfo.GasConsumed,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return executionResult.Response, nil
|
|
|
|
}
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !st.Simulate {
|
2021-05-25 12:56:36 +00:00
|
|
|
bloom, found := k.GetBlockBloomTransient()
|
|
|
|
if !found {
|
|
|
|
bloom = big.NewInt(0)
|
|
|
|
}
|
2021-01-06 20:56:40 +00:00
|
|
|
// update block bloom filter
|
2021-05-25 12:56:36 +00:00
|
|
|
bloom = bloom.Or(bloom, executionResult.Bloom)
|
|
|
|
k.SetBlockBloomTransient(bloom)
|
2021-01-06 20:56:40 +00:00
|
|
|
|
|
|
|
// update transaction logs in KVStore
|
2021-05-25 12:56:36 +00:00
|
|
|
err = k.CommitStateDB.SetLogs(ethHash, executionResult.Logs)
|
2021-01-06 20:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
|
|
|
blockHash, _ := k.GetBlockHashFromHeight(ctx, ctx.BlockHeight())
|
|
|
|
k.SetTxReceiptToHash(ctx, ethHash, &types.TxReceipt{
|
2021-05-14 06:52:18 +00:00
|
|
|
Hash: ethHash.Hex(),
|
|
|
|
From: sender.Hex(),
|
2021-04-18 15:54:18 +00:00
|
|
|
Data: msg.Data,
|
|
|
|
Index: uint64(st.Csdb.TxIndex()),
|
|
|
|
BlockHeight: uint64(ctx.BlockHeight()),
|
2021-05-14 06:52:18 +00:00
|
|
|
BlockHash: blockHash.Hex(),
|
2021-04-18 15:54:18 +00:00
|
|
|
Result: &types.TxResult{
|
|
|
|
ContractAddress: executionResult.Response.ContractAddress,
|
|
|
|
Bloom: executionResult.Response.Bloom,
|
|
|
|
TxLogs: executionResult.Response.TxLogs,
|
|
|
|
Ret: executionResult.Response.Ret,
|
|
|
|
Reverted: executionResult.Response.Reverted,
|
|
|
|
GasUsed: executionResult.GasInfo.GasConsumed,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
k.AddTxHashToBlock(ctx, ctx.BlockHeight(), ethHash)
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:44:58 +00:00
|
|
|
defer func() {
|
|
|
|
if st.Message.Value().IsInt64() {
|
|
|
|
telemetry.SetGauge(
|
|
|
|
float32(st.Message.Value().Int64()),
|
|
|
|
"tx", "msg", "ethereum_tx",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
telemetry.IncrCounterWithLabels(
|
|
|
|
[]string{types.ModuleName, "ethereum_tx"},
|
|
|
|
1,
|
|
|
|
labels,
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
// emit events
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeEthereumTx,
|
2021-05-10 16:34:00 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, st.Message.Value().String()),
|
2021-04-18 15:54:18 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyTxHash, ethcmn.BytesToHash(txHash).Hex()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
if len(msg.Data.To) > 0 {
|
2021-01-06 20:56:40 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeEthereumTx,
|
2021-05-14 06:52:18 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyRecipient, msg.Data.To),
|
2021-01-06 20:56:40 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
return executionResult.Response, nil
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|