Skip to content

Commit

Permalink
Problem: repeated tx sender recovery is wastful (#227)
Browse files Browse the repository at this point in the history
* Problem: repeated tx sender recovery is wastful

Solution:
- only do once in ante handler, reuse the result

* Update CHANGELOG.md

Signed-off-by: yihuang <huang@crypto.com>

* Update x/evm/types/msg.go

Co-authored-by: mmsqe <tqd0800210105@gmail.com>
Signed-off-by: yihuang <huang@crypto.com>

* return value

* fix unit test

---------

Signed-off-by: yihuang <huang@crypto.com>
Co-authored-by: mmsqe <tqd0800210105@gmail.com>
  • Loading branch information
yihuang and mmsqe authored Mar 15, 2023
1 parent c5e9338 commit cb741e1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (cli) [#1615](https://github.com/evmos/ethermint/pull/1615) Support customize db opener in `StartCmd`.
* (ante) [#227](https://github.com/crypto-org-chain/ethermint/pull/227) Reuse sender recovery result.

### State Machine Breaking

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 @@ -43,7 +43,7 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t
}
}

response, err := k.ApplyTransaction(ctx, tx)
response, err := k.ApplyTransaction(ctx, msg)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to apply transaction")
}
Expand Down
7 changes: 4 additions & 3 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,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, tx *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
var (
bloom *big.Int
bloomReceipt ethtypes.Bloom
Expand All @@ -203,7 +203,8 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to load evm config")
}
txConfig := k.TxConfig(ctx, tx.Hash())
ethTx := tx.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()))
Expand Down Expand Up @@ -251,7 +252,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
34 changes: 33 additions & 1 deletion x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -308,7 +309,38 @@ 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 {
from = common.HexToAddress(msg.From)
} 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 comments on commit cb741e1

Please sign in to comment.