Skip to content

Commit 5adb929

Browse files
committed
feat: refactor MsgEthereumTx with eth transaction
Closes: #220 cleanup fix tests fix evm test cases
1 parent 66dd661 commit 5adb929

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2110
-11161
lines changed

ante/evm/04_validate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
anteinterfaces "github.com/cosmos/evm/ante/interfaces"
88
evmtypes "github.com/cosmos/evm/x/vm/types"
9+
ethtypes "github.com/ethereum/go-ethereum/core/types"
910

1011
errorsmod "cosmossdk.io/errors"
1112
sdkmath "cosmossdk.io/math"
@@ -21,7 +22,7 @@ import (
2122
// - If the transaction is a contract creation or call, the corresponding operation must be enabled in the EVM parameters
2223
func ValidateMsg(
2324
evmParams evmtypes.Params,
24-
txData evmtypes.TxData,
25+
txData *ethtypes.Transaction,
2526
from sdktypes.AccAddress,
2627
) error {
2728
if from != nil {
@@ -37,10 +38,10 @@ func ValidateMsg(
3738
// checkDisabledCreateCall checks if the transaction is a contract creation or call,
3839
// and if those actions are disabled through governance.
3940
func checkDisabledCreateCall(
40-
txData evmtypes.TxData,
41+
txData *ethtypes.Transaction,
4142
permissions *evmtypes.AccessControl,
4243
) error {
43-
to := txData.GetTo()
44+
to := txData.To()
4445
blockCreate := permissions.Create.AccessType == evmtypes.AccessTypeRestricted
4546
blockCall := permissions.Call.AccessType == evmtypes.AccessTypeRestricted
4647

ante/evm/05_signature_verification.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package evm
22

33
import (
4+
"bytes"
45
"math/big"
56

67
ethtypes "github.com/ethereum/go-ethereum/core/types"
@@ -92,7 +93,12 @@ func SignatureVerification(
9293
)
9394
}
9495

95-
// set up the sender to the transaction field if not already
96-
msg.From = sender.Hex()
96+
if !bytes.Equal(msg.From, sender.Bytes()) {
97+
return errorsmod.Wrapf(
98+
errortypes.ErrorInvalidSigner,
99+
"rejected ethereum transaction with invalid sender address; expected %s, got %s",
100+
evmtypes.HexAddress(msg.From), sender.String(),
101+
)
102+
}
97103
return nil
98104
}

ante/evm/06_account_verification.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package evm
22

33
import (
44
"github.com/ethereum/go-ethereum/common"
5+
ethtypes "github.com/ethereum/go-ethereum/core/types"
56

67
anteinterfaces "github.com/cosmos/evm/ante/interfaces"
78
"github.com/cosmos/evm/x/vm/keeper"
89
"github.com/cosmos/evm/x/vm/statedb"
9-
evmtypes "github.com/cosmos/evm/x/vm/types"
1010

1111
errorsmod "cosmossdk.io/errors"
1212
sdkmath "cosmossdk.io/math"
@@ -25,7 +25,7 @@ func VerifyAccountBalance(
2525
accountKeeper anteinterfaces.AccountKeeper,
2626
account *statedb.Account,
2727
from common.Address,
28-
txData evmtypes.TxData,
28+
txData *ethtypes.Transaction,
2929
) error {
3030
// Only EOA are allowed to send transactions.
3131
if account != nil && account.IsContract() {

ante/evm/08_gas_consume.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"math/big"
55

66
"github.com/ethereum/go-ethereum/common"
7+
ethtypes "github.com/ethereum/go-ethereum/core/types"
78

89
anteinterfaces "github.com/cosmos/evm/ante/interfaces"
910
"github.com/cosmos/evm/types"
@@ -84,7 +85,7 @@ func deductFees(
8485

8586
// GetMsgPriority returns the priority of an Eth Tx capped by the minimum priority
8687
func GetMsgPriority(
87-
txData evmtypes.TxData,
88+
txData *ethtypes.Transaction,
8889
minPriority int64,
8990
baseFee *big.Int,
9091
) int64 {

ante/evm/11_emit_event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func EmitTxHashEvent(ctx sdk.Context, msg *evmtypes.MsgEthereumTx, blockTxIndex,
5757
ctx.EventManager().EmitEvent(
5858
sdk.NewEvent(
5959
evmtypes.EventTypeEthereumTx,
60-
sdk.NewAttribute(evmtypes.AttributeKeyEthereumTxHash, msg.Hash),
60+
sdk.NewAttribute(evmtypes.AttributeKeyEthereumTxHash, msg.Hash().String()),
6161
sdk.NewAttribute(evmtypes.AttributeKeyTxIndex, strconv.FormatUint(blockTxIndex+msgIndex, 10)), // #nosec G115
6262
),
6363
)

ante/evm/mono_decorator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func (md MonoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
8383
return ctx, err
8484
}
8585

86-
feeAmt := txData.Fee()
87-
gas := txData.GetGas()
86+
feeAmt := ethMsg.GetFee()
87+
gas := txData.Gas()
8888
fee := sdkmath.LegacyNewDecFromBigInt(feeAmt)
8989
gasLimit := sdkmath.LegacyNewDecFromBigInt(new(big.Int).SetUint64(gas))
9090

@@ -99,13 +99,13 @@ func (md MonoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
9999
}
100100
}
101101

102-
if txData.TxType() == ethtypes.DynamicFeeTxType && decUtils.BaseFee != nil {
102+
if txData.Type() == ethtypes.DynamicFeeTxType && decUtils.BaseFee != nil {
103103
// If the base fee is not empty, we compute the effective gas price
104104
// according to current base fee price. The gas limit is specified
105105
// by the user, while the price is given by the minimum between the
106106
// max price paid for the entire tx, and the sum between the price
107107
// for the tip and the base fee.
108-
feeAmt = txData.EffectiveFee(decUtils.BaseFee)
108+
feeAmt = ethMsg.GetEffectiveFee(decUtils.BaseFee)
109109
fee = sdkmath.LegacyNewDecFromBigInt(feeAmt)
110110
}
111111

@@ -211,7 +211,7 @@ func (md MonoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
211211

212212
// Update the fee to be paid for the tx adding the fee specified for the
213213
// current message.
214-
decUtils.TxFee.Add(decUtils.TxFee, txData.Fee())
214+
decUtils.TxFee.Add(decUtils.TxFee, ethMsg.GetFee())
215215

216216
// Update the transaction gas limit adding the gas specified in the
217217
// current message.
@@ -228,7 +228,7 @@ func (md MonoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
228228
)
229229
}
230230

231-
if err := IncrementNonce(ctx, md.accountKeeper, acc, txData.GetNonce()); err != nil {
231+
if err := IncrementNonce(ctx, md.accountKeeper, acc, txData.Nonce()); err != nil {
232232
return ctx, err
233233
}
234234

api/cosmos/evm/vm/v1/access_list_tx.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

api/cosmos/evm/vm/v1/dynamic_fee_tx.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

api/cosmos/evm/vm/v1/legacy_tx.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

api/cosmos/evm/vm/v1/msg.go

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,9 @@ package vmv1
33
import (
44
"fmt"
55

6-
"github.com/ethereum/go-ethereum/common"
7-
ethtypes "github.com/ethereum/go-ethereum/core/types"
86
protov2 "google.golang.org/protobuf/proto"
97
)
108

11-
// supportedTxs holds the Ethereum transaction types
12-
// supported by Cosmos EVM
13-
//
14-
// Use a function to return a new pointer and avoid
15-
// possible reuse or racing conditions when using the same pointer
16-
var supportedTxs = map[string]func() TxDataV2{
17-
"/cosmos.evm.vm.v1.DynamicFeeTx": func() TxDataV2 { return &DynamicFeeTx{} },
18-
"/cosmos.evm.vm.v1.AccessListTx": func() TxDataV2 { return &AccessListTx{} },
19-
"/cosmos.evm.vm.v1.LegacyTx": func() TxDataV2 { return &LegacyTx{} },
20-
}
21-
22-
// getSender extracts the sender address from the signature values using the latest signer for the given chainID.
23-
func getSender(txData TxDataV2) (common.Address, error) {
24-
signer := ethtypes.LatestSignerForChainID(txData.GetChainID())
25-
from, err := signer.Sender(ethtypes.NewTx(txData.AsEthereumData()))
26-
if err != nil {
27-
return common.Address{}, err
28-
}
29-
return from, nil
30-
}
31-
329
// GetSigners is the custom function to get signers on Ethereum transactions
3310
// Gets the signer's address from the Ethereum tx signature
3411
func GetSigners(msg protov2.Message) ([][]byte, error) {
@@ -37,21 +14,5 @@ func GetSigners(msg protov2.Message) ([][]byte, error) {
3714
return nil, fmt.Errorf("invalid type, expected MsgEthereumTx and got %T", msg)
3815
}
3916

40-
txDataFn, found := supportedTxs[msgEthTx.Data.TypeUrl]
41-
if !found {
42-
return nil, fmt.Errorf("invalid TypeUrl %s", msgEthTx.Data.TypeUrl)
43-
}
44-
txData := txDataFn()
45-
46-
// msgEthTx.Data is a message (DynamicFeeTx, LegacyTx or AccessListTx)
47-
if err := msgEthTx.Data.UnmarshalTo(txData); err != nil {
48-
return nil, err
49-
}
50-
51-
sender, err := getSender(txData)
52-
if err != nil {
53-
return nil, err
54-
}
55-
56-
return [][]byte{sender.Bytes()}, nil
17+
return [][]byte{msgEthTx.From}, nil
5718
}

0 commit comments

Comments
 (0)