Skip to content

Commit

Permalink
fix evm module test
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Nov 12, 2024
1 parent cfa90f9 commit 11ece47
Show file tree
Hide file tree
Showing 37 changed files with 571 additions and 205 deletions.
62 changes: 62 additions & 0 deletions api/ethermint/evm/v1/access_list_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package evmv1

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/types"
)

// GetChainID returns the chain id field from the AccessListTx
func (tx *AccessListTx) GetChainID() *big.Int {
return stringToBigInt(tx.GetChainId())
}

// GetAccessList returns the AccessList field.
func (tx *AccessListTx) GetAccessList() ethtypes.AccessList {
if tx.Accesses == nil {
return nil
}
var ethAccessList ethtypes.AccessList

for _, tuple := range tx.Accesses {
storageKeys := make([]common.Hash, len(tuple.StorageKeys))

for i := range tuple.StorageKeys {
storageKeys[i] = common.HexToHash(tuple.StorageKeys[i])
}

ethAccessList = append(ethAccessList, ethtypes.AccessTuple{
Address: common.HexToAddress(tuple.Address),
StorageKeys: storageKeys,
})
}

return ethAccessList
}

// AsEthereumData returns an AccessListTx transaction tx from the proto-formatted
// TxData defined on the Cosmos EVM.
func (tx *AccessListTx) AsEthereumData() ethtypes.TxData {
v, r, s := tx.GetRawSignatureValues()
return &ethtypes.AccessListTx{
ChainID: tx.GetChainID(),
Nonce: tx.GetNonce(),
GasPrice: stringToBigInt(tx.GetGasPrice()),
Gas: tx.GetGas(),
To: stringToAddress(tx.GetTo()),
Value: stringToBigInt(tx.GetValue()),
Data: tx.GetData(),
AccessList: tx.GetAccessList(),
V: v,
R: r,
S: s,
}
}

// GetRawSignatureValues returns the V, R, S signature values of the transaction.
// The return values should not be modified by the caller.
func (tx *AccessListTx) GetRawSignatureValues() (v, r, s *big.Int) {
return types.RawSignatureValues(tx.V, tx.R, tx.S)
}
66 changes: 66 additions & 0 deletions api/ethermint/evm/v1/dynamic_fee_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package evmv1

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/types"
)

// GetChainID returns the chain id field from the DynamicFeeTx
func (tx *DynamicFeeTx) GetChainID() *big.Int {
return stringToBigInt(tx.GetChainId())
}

// AsEthereumData returns an DynamicFeeTx transaction tx from the proto-formatted
// TxData defined on the Cosmos EVM.
func (tx *DynamicFeeTx) AsEthereumData() ethtypes.TxData {
v, r, s := tx.GetRawSignatureValues()
return &ethtypes.DynamicFeeTx{
ChainID: tx.GetChainID(),
Nonce: tx.GetNonce(),
GasTipCap: stringToBigInt(tx.GetGasTipCap()),
GasFeeCap: stringToBigInt(tx.GetGasFeeCap()),
Gas: tx.GetGas(),
To: stringToAddress(tx.GetTo()),
Value: stringToBigInt(tx.GetValue()),
Data: tx.GetData(),
AccessList: tx.GetAccessList(),
V: v,
R: r,
S: s,
}
}

// GetAccessList returns the AccessList field.
func (tx *DynamicFeeTx) GetAccessList() ethtypes.AccessList {
if tx.Accesses == nil {
return nil
}
var ethAccessList ethtypes.AccessList

for _, tuple := range tx.Accesses {
storageKeys := make([]common.Hash, len(tuple.StorageKeys))

for i := range tuple.StorageKeys {
storageKeys[i] = common.HexToHash(tuple.StorageKeys[i])
}

ethAccessList = append(ethAccessList, ethtypes.AccessTuple{
Address: common.HexToAddress(tuple.Address),
StorageKeys: storageKeys,
})
}

return ethAccessList
}

// GetRawSignatureValues returns the V, R, S signature values of the transaction.
// The return values should not be modified by the caller.
func (tx *DynamicFeeTx) GetRawSignatureValues() (v, r, s *big.Int) {
return types.RawSignatureValues(tx.V, tx.R, tx.S)
}
42 changes: 42 additions & 0 deletions api/ethermint/evm/v1/legacy_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package evmv1

import (
"math/big"

ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/types"
)

// GetChainID returns the chain id field from the derived signature values
func (tx *LegacyTx) GetChainID() *big.Int {
v, _, _ := tx.GetRawSignatureValues()
return types.DeriveChainID(v)
}

// AsEthereumData returns an LegacyTx transaction tx from the proto-formatted
// TxData defined on the Cosmos EVM.
func (tx *LegacyTx) AsEthereumData() ethtypes.TxData {
v, r, s := tx.GetRawSignatureValues()
return &ethtypes.LegacyTx{
Nonce: tx.GetNonce(),
GasPrice: stringToBigInt(tx.GetGasPrice()),
Gas: tx.GetGas(),
To: stringToAddress(tx.GetTo()),
Value: stringToBigInt(tx.GetValue()),
Data: tx.GetData(),
V: v,
R: r,
S: s,
}
}

// GetRawSignatureValues returns the V, R, S signature values of the transaction.
// The return values should not be modified by the caller.
func (tx *LegacyTx) GetRawSignatureValues() (v, r, s *big.Int) {
return types.RawSignatureValues(tx.V, tx.R, tx.S)
}

// GetAccessList returns nil
func (tx *LegacyTx) GetAccessList() ethtypes.AccessList {
return nil
}
56 changes: 56 additions & 0 deletions api/ethermint/evm/v1/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package evmv1

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
protov2 "google.golang.org/protobuf/proto"
)

// supportedTxs holds the Ethereum transaction types
// supported by Evmos.
// Use a function to return a new pointer and avoid
// possible reuse or racing conditions when using the same pointer
var supportedTxs = map[string]func() TxDataV2{
"/ethermint.evm.v1.DynamicFeeTx": func() TxDataV2 { return &DynamicFeeTx{} },
"/ethermint.evm.v1.AccessListTx": func() TxDataV2 { return &AccessListTx{} },
"/ethermint.evm.v1.LegacyTx": func() TxDataV2 { return &LegacyTx{} },
}

// getSender extracts the sender address from the signature values using the latest signer for the given chainID.
func getSender(txData TxDataV2) (common.Address, error) {
signer := ethtypes.LatestSignerForChainID(txData.GetChainID())
from, err := signer.Sender(ethtypes.NewTx(txData.AsEthereumData()))
if err != nil {
return common.Address{}, err
}
return from, nil
}

// GetSigners is the custom function to get signers on Ethereum transactions
// Gets the signer's address from the Ethereum tx signature
func GetSigners(msg protov2.Message) ([][]byte, error) {
msgEthTx, ok := msg.(*MsgEthereumTx)
if !ok {
return nil, fmt.Errorf("invalid type, expected MsgEthereumTx and got %T", msg)
}

txDataFn, found := supportedTxs[msgEthTx.Data.TypeUrl]
if !found {
return nil, fmt.Errorf("invalid TypeUrl %s", msgEthTx.Data.TypeUrl)
}
txData := txDataFn()

// msgEthTx.Data is a message (DynamicFeeTx, LegacyTx or AccessListTx)
if err := msgEthTx.Data.UnmarshalTo(txData); err != nil {
return nil, err
}

sender, err := getSender(txData)
if err != nil {
return nil, err
}

return [][]byte{sender.Bytes()}, nil
}
45 changes: 45 additions & 0 deletions api/ethermint/evm/v1/tx_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package evmv1

import (
"math/big"

sdkmath "cosmossdk.io/math"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"google.golang.org/protobuf/reflect/protoreflect"
)

var (
_ TxDataV2 = &LegacyTx{}
_ TxDataV2 = &AccessListTx{}
_ TxDataV2 = &DynamicFeeTx{}
)

// TxDataV2 implements the Ethereum transaction tx structure. It is used
// solely to define the custom logic for getting signers on Ethereum transactions.
type TxDataV2 interface {
GetChainID() *big.Int
AsEthereumData() ethtypes.TxData

ProtoReflect() protoreflect.Message
}

// helper function to parse string to bigInt
func stringToBigInt(str string) *big.Int {
if str == "" {
return nil
}
res, ok := sdkmath.NewIntFromString(str)
if !ok {
return nil
}
return res.BigInt()
}

func stringToAddress(toStr string) *common.Address {
if toStr == "" {
return nil
}
addr := common.HexToAddress(toStr)
return &addr
}
3 changes: 1 addition & 2 deletions app/ante/fee_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"

"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -55,7 +54,7 @@ func TestSDKTxFeeChecker(t *testing.T) {
// with extension option
// without extension option
// london hardfork enableness
encodingConfig := encoding.MakeConfig(module.NewBasicManager())
encodingConfig := encoding.MakeConfig()
minGasPrices := sdk.NewDecCoins(sdk.NewDecCoin("aphoton", math.NewInt(10)))

genesisCtx := sdk.NewContext(nil, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
9 changes: 3 additions & 6 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/app/ante"
"github.com/evmos/ethermint/encoding"
"github.com/evmos/ethermint/tests"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
Expand Down Expand Up @@ -128,12 +127,10 @@ func (suite *AnteTestSuite) SetupTest() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

encodingConfig := encoding.MakeConfig(app.ModuleBasics)
// We're using TestMsg amino encoding in some tests, so register it here.
encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
eip712.SetEncodingConfig(encodingConfig)
suite.app.LegacyAmino().RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)

suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
suite.clientCtx = client.Context{}.WithTxConfig(suite.app.TxConfig())

anteHandler, err := ante.NewAnteHandler(ante.HandlerOptions{
AccountKeeper: suite.app.AccountKeeper,
Expand All @@ -142,7 +139,7 @@ func (suite *AnteTestSuite) SetupTest() {
FeegrantKeeper: suite.app.FeeGrantKeeper,
IBCKeeper: suite.app.IBCKeeper,
FeeMarketKeeper: suite.app.FeeMarketKeeper,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SignModeHandler: suite.app.TxConfig().SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
DisabledAuthzMsgs: []string{
sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}),
Expand Down
Loading

0 comments on commit 11ece47

Please sign in to comment.