8000 Problem: repeated tx sender recovery is wastful by yihuang · Pull Request #1717 · evmos/ethermint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Problem: repeated tx sender recovery is wastful #1717

Merged
merged 7 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (evm) [#1582](https://github.com/evmos/ethermint/pull/1582) Cleanup `evm` files
* (evm) [#1544](https://github.com/evmos/ethermint/pull/1544) Migrate deprecated event emitting to new `TypedEvent`
* (deps) [#1532](https://github.com/evmos/ethermint/pull/1532) Upgrade Go-Ethereum version to [`v1.10.26`](https://github.com/ethereum/go-ethereum/releases/tag/v1.10.26).
* (ante) [#1717](https://github.com/evmos/ethermint/pull/1717) Reuse sender recovery result.

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t
labels = append(labels, telemetry.NewLabel("execution", "call"))
}

response, err := k.ApplyTransaction(ctx, tx)
response, err := k.ApplyTransaction(ctx, msg)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to apply transaction")
}
Expand Down
9 changes: 5 additions & 4 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc {
// returning.
//
// For relevant discussion see: https://github.com/cosmos/cosmos-sdk/discussions/9072
func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*types.MsgEthereumTxResponse, error) {
func (k *Keeper) ApplyTransaction(ctx sdk.Context, msgEth *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
var (
bloom *big.Int
bloomReceipt ethtypes.Bloom
Expand All @@ -156,11 +156,12 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
if err != nil {
return nil, errorsmod.Wrap(err, "failed to load evm config")
}
txConfig := k.TxConfig(ctx, tx.Hash())
ethTx := msgEth.AsTransaction()
txConfig := k.TxConfig(ctx, ethTx.Hash())

// get the signer according to the chain rules from the config and block height
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
msg, err := tx.AsMessage(signer, cfg.BaseFee)
msg, err := msgEth.AsMessage(signer, cfg.BaseFee)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to return ethereum transaction as core message")
}
Expand Down Expand Up @@ -206,7 +207,7 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
}

receipt := &ethtypes.Receipt{
Type: tx.Type(),
Type: ethTx.Type(),
PostState: nil, // TODO: intermediate state root
CumulativeGasUsed: cumulativeGasUsed,
Bloom: bloomReceipt,
Expand Down
8 changes: 6 additions & 2 deletions x/evm/keeper/state_transition_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func newSignedEthTx(
addr sdk.Address,
krSigner keyring.Signer,
ethSigner ethtypes.Signer,
) (*ethtypes.Transaction, error) {
) (*evmtypes.MsgEthereumTx, error) {
var ethTx *ethtypes.Transaction
switch txData := txData.(type) {
case *ethtypes.AccessListTx:
Expand All @@ -72,7 +72,11 @@ func newSignedEthTx(
return nil, err
}

return ethTx, nil
var msg evmtypes.MsgEthereumTx
if err := msg.FromEthereumTx(ethTx); err != nil {
return nil, err
}
return &msg, nil
}

func newEthMsgTx(
Expand Down
38 changes: 37 additions & 1 deletion x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/evmos/ethermint/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
Expand Down Expand Up @@ -325,7 +326,42 @@ func (msg MsgEthereumTx) AsTransaction() *ethtypes.Transaction {

// AsMessage creates an Ethereum core.Message from the msg fields
func (msg MsgEthereumTx) AsMessage(signer ethtypes.Signer, baseFee *big.Int) (core.Message, error) {
return msg.AsTransaction().AsMessage(signer, baseFee)
txData, err := UnpackTxData(msg.Data)
if err != nil {
return nil, err
}

gasPrice, gasFeeCap, gasTipCap := txData.GetGasPrice(), txData.GetGasFeeCap(), txData.GetGasTipCap()
if baseFee != nil {
gasPrice = math.BigMin(gasPrice.Add(gasTipCap, baseFee), gasFeeCap)
}
var from common.Address
if len(msg.From) > 0 {
// user can't set arbitrary value in `From` field in transaction,
// the SigVerify ante handler will verify the signature and recover
// the sender address and populate the `From` field, so the other code can
// use it directly when available.
from = common.HexToAddress(msg.From)
8224
} else {
// heavy path
from, err = signer.Sender(msg.AsTransaction())
if err != nil {
return nil, err
}
}
ethMsg := ethtypes.NewMessage(
from,
txData.GetTo(),
txData.GetNonce(),
txData.GetValue(),
txData.GetGas(),
gasPrice, gasFeeCap, gasTipCap,
txData.GetData(),
txData.GetAccessList(),
false,
)

return ethMsg, nil
}

// GetSender extracts the sender address from the signature values using the latest signer for the given chainID.
Expand Down
0