Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Problem: repeated tx sender recovery is wastful
Browse files Browse the repository at this point in the history
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 committed Mar 16, 2023
1 parent f6d1a0a commit 5f7c4c3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
16 changes: 1 addition & 15 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +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`.
* (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).

### Bug Fixes

* (upgrade) [#1617](https://github.com/evmos/ethermint/pull/1617) Refactor `evm` module's parameters to store them under a single store key
* (rpc) [#1600](https://github.com/evmos/ethermint/pull/1600) Revert changes from `TypedEvents`
* (rpc) [#1613](https://github.com/evmos/ethermint/pull/1613) Change the default json-rpc listen address to localhost.
* (rpc) [#1611](https://github.com/evmos/ethermint/pull/1611) Add missing next fee in fee history, fix wrong oldestBlock and align earliest input as ethereum.
* (proto) [#1586](https://github.com/evmos/ethermint/pull/1586) Avoid duplicate register proto type in `evm` & `feemarket`
* (rpc) [#1638](https://github.com/evmos/ethermint/pull/1638) Align results when querying `eth_getTransactionCount` for future blocks for accounts with zero and non-zero transaction counts.
* (rpc) [#1639](https://github.com/evmos/ethermint/pull/1639) Align block number input behaviour for `eth_getProof` as Ethereum.

## [v0.20.0] - 2022-12-28
* (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 @@ -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
7 changes: 4 additions & 3 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, tx *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
var (
bloom *big.Int
bloomReceipt ethtypes.Bloom
Expand All @@ -156,7 +156,8 @@ 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 := 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 @@ -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
34 changes: 33 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,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 5f7c4c3

Please sign in to comment.