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

Commit bc2871a

Browse files
committed
refactor test files
1 parent 66f6f19 commit bc2871a

File tree

6 files changed

+190
-167
lines changed

6 files changed

+190
-167
lines changed

app/ante/eth_test.go

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

77
sdk "github.com/cosmos/cosmos-sdk/types"
88

9-
storetypes "github.com/cosmos/cosmos-sdk/store/types"
109
"github.com/evmos/ethermint/app/ante"
1110
"github.com/evmos/ethermint/server/config"
1211
"github.com/evmos/ethermint/tests"
@@ -16,59 +15,6 @@ import (
1615
ethtypes "github.com/ethereum/go-ethereum/core/types"
1716
)
1817

19-
func (suite AnteTestSuite) TestEthSigVerificationDecorator() {
20-
addr, privKey := tests.NewAddrKey()
21-
22-
signedTx := evmtypes.NewTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil)
23-
signedTx.From = addr.Hex()
24-
err := signedTx.Sign(suite.ethSigner, tests.NewSigner(privKey))
25-
suite.Require().NoError(err)
26-
27-
unprotectedTx := evmtypes.NewTxContract(nil, 1, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil)
28-
unprotectedTx.From = addr.Hex()
29-
err = unprotectedTx.Sign(ethtypes.HomesteadSigner{}, tests.NewSigner(privKey))
30-
suite.Require().NoError(err)
31-
32-
testCases := []struct {
33-
name string
34-
tx sdk.Tx
35-
allowUnprotectedTxs bool
36-
reCheckTx bool
37-
expPass bool
38-
}{
39-
{"ReCheckTx", &invalidTx{}, false, true, false},
40-
{"invalid transaction type", &invalidTx{}, false, false, false},
41-
{
42-
"invalid sender",
43-
evmtypes.NewTx(suite.app.EvmKeeper.ChainID(), 1, &addr, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil),
44-
true,
45-
false,
46-
false,
47-
},
48-
{"successful signature verification", signedTx, false, false, true},
49-
{"invalid, reject unprotected txs", unprotectedTx, false, false, false},
50-
{"successful, allow unprotected txs", unprotectedTx, true, false, true},
51-
}
52-
53-
for _, tc := range testCases {
54-
suite.Run(tc.name, func() {
55-
suite.evmParamsOption = func(params *evmtypes.Params) {
56-
params.AllowUnprotectedTxs = tc.allowUnprotectedTxs
57-
}
58-
suite.SetupTest()
59-
dec := ante.NewEthSigVerificationDecorator(suite.app.EvmKeeper)
60-
_, err := dec.AnteHandle(suite.ctx.WithIsReCheckTx(tc.reCheckTx), tc.tx, false, NextFn)
61-
62-
if tc.expPass {
63-
suite.Require().NoError(err)
64-
} else {
65-
suite.Require().Error(err)
66-
}
67-
})
68-
}
69-
suite.evmParamsOption = nil
70-
}
71-
7218
func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
7319
dec := ante.NewEthAccountVerificationDecorator(
7420
suite.app.AccountKeeper, suite.app.EvmKeeper,
@@ -522,35 +468,3 @@ func (suite AnteTestSuite) TestEthIncrementSenderSequenceDecorator() {
522468
})
523469
}
524470
}
525-
526-
func (suite AnteTestSuite) TestEthSetupContextDecorator() {
527-
dec := ante.NewEthSetUpContextDecorator(suite.app.EvmKeeper)
528-
tx := evmtypes.NewTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil)
529-
530-
testCases := []struct {
531-
name string
532-
tx sdk.Tx
533-
expPass bool
534-
}{
535-
{"invalid transaction type - does not implement GasTx", &invalidTx{}, false},
536-
{
537-
"success - transaction implement GasTx",
538-
tx,
539-
true,
540-
},
541-
}
542-
543-
for _, tc := range testCases {
544-
suite.Run(tc.name, func() {
545-
ctx, err := dec.AnteHandle(suite.ctx, tc.tx, false, NextFn)
546-
547-
if tc.expPass {
548-
suite.Require().NoError(err)
549-
suite.Equal(storetypes.GasConfig{}, ctx.KVGasConfig())
550-
suite.Equal(storetypes.GasConfig{}, ctx.TransientKVGasConfig())
551-
} else {
552-
suite.Require().Error(err)
553-
}
554-
})
555-
}
556-
}

app/ante/fees.go

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,46 @@ type MinGasPriceDecorator struct {
2121
evmKeeper EVMKeeper
2222
}
2323

24+
// EthMinGasPriceDecorator will check if the transaction's fee is at least as large
25+
// as the MinGasPrices param. If fee is too low, decorator returns error and tx
26+
// is rejected. This applies to both CheckTx and DeliverTx and regardless
27+
// if London hard fork or fee market params (EIP-1559) are enabled.
28+
// If fee is high enough, then call next AnteHandler
29+
type EthMinGasPriceDecorator struct {
30+
feesKeeper FeeMarketKeeper
31+
evmKeeper EVMKeeper
32+
}
33+
34+
// EthMempoolFeeDecorator will check if the transaction's effective fee is at least as large
35+
// as the local validator's minimum gasFee (defined in validator config).
36+
// If fee is too low, decorator returns error and tx is rejected from mempool.
37+
// Note this only applies when ctx.CheckTx = true
38+
// If fee is high enough or not CheckTx, then call next AnteHandler
39+
// CONTRACT: Tx must implement FeeTx to use MempoolFeeDecorator
40+
type EthMempoolFeeDecorator struct {
41+
evmKeeper EVMKeeper
42+
}
43+
44+
// NewMinGasPriceDecorator creates a new MinGasPriceDecorator instance used only for
45+
// Cosmos transactions.
2446
func NewMinGasPriceDecorator(fk FeeMarketKeeper, ek EVMKeeper) MinGasPriceDecorator {
2547
return MinGasPriceDecorator{feesKeeper: fk, evmKeeper: ek}
2648
}
2749

50+
// NewEthMinGasPriceDecorator creates a new MinGasPriceDecorator instance used only for
51+
// Ethereum transactions.
52+
func NewEthMinGasPriceDecorator(fk FeeMarketKeeper, ek EVMKeeper) EthMinGasPriceDecorator {
53+
return EthMinGasPriceDecorator{feesKeeper: fk, evmKeeper: ek}
54+
}
55+
56+
// NewEthMempoolFeeDecorator creates a new NewEthMempoolFeeDecorator instance used only for
57+
// Ethereum transactions.
58+
func NewEthMempoolFeeDecorator(ek EVMKeeper) EthMempoolFeeDecorator {
59+
return EthMempoolFeeDecorator{
60+
evmKeeper: ek,
61+
}
62+
}
63+
2864
func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
2965
feeTx, ok := tx.(sdk.FeeTx)
3066
if !ok {
@@ -72,20 +108,8 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
72108
return next(ctx, tx, simulate)
73109
}
74110

75-
// EthMinGasPriceDecorator will check if the transaction's fee is at least as large
76-
// as the MinGasPrices param. If fee is too low, decorator returns error and tx
77-
// is rejected. This applies to both CheckTx and DeliverTx and regardless
78-
// if London hard fork or fee market params (EIP-1559) are enabled.
79-
// If fee is high enough, then call next AnteHandler
80-
type EthMinGasPriceDecorator struct {
81-
feesKeeper FeeMarketKeeper
82-
evmKeeper EVMKeeper
83-
}
84-
85-
func NewEthMinGasPriceDecorator(fk FeeMarketKeeper, ek EVMKeeper) EthMinGasPriceDecorator {
86-
return EthMinGasPriceDecorator{feesKeeper: fk, evmKeeper: ek}
87-
}
88-
111+
// AnteHandle ensures that the that the effective fee from the transaction is greater than the
112+
// minimum global fee, which is defined by the MinGasPrice (parameter) * GasLimit (tx argument).
89113
func (empd EthMinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
90114
minGasPrice := empd.feesKeeper.GetParams(ctx).MinGasPrice
91115

@@ -144,3 +168,44 @@ func (empd EthMinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
144168

145169
return next(ctx, tx, simulate)
146170
}
171+
172+
// AnteHandle ensures that the provided fees meet a minimum threshold for the validator.
173+
// This check only for local mempool purposes, and thus it is only run on (Re)CheckTx.
174+
// The logic is also skipped if the London hard fork and EIP-1559 are enabled.
175+
func (mfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
176+
if !ctx.IsCheckTx() || simulate {
177+
return next(ctx, tx, simulate)
178+
}
179+
chainCfg := mfd.evmKeeper.GetChainConfig(ctx)
180+
ethCfg := chainCfg.EthereumConfig(mfd.evmKeeper.ChainID())
181+
182+
baseFee := mfd.evmKeeper.GetBaseFee(ctx, ethCfg)
183+
// skip check as the London hard fork and EIP-1559 are enabled
184+
if baseFee != nil {
185+
return next(ctx, tx, simulate)
186+
}
187+
188+
evmDenom := mfd.evmKeeper.GetEVMDenom(ctx)
189+
minGasPrice := ctx.MinGasPrices().AmountOf(evmDenom)
190+
191+
for _, msg := range tx.GetMsgs() {
192+
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
193+
if !ok {
194+
return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
195+
}
196+
197+
fee := sdk.NewDecFromBigInt(ethMsg.GetFee())
198+
gasLimit := sdk.NewDecFromBigInt(new(big.Int).SetUint64(ethMsg.GetGas()))
199+
requiredFee := minGasPrice.Mul(gasLimit)
200+
201+
if fee.LT(requiredFee) {
202+
return ctx, errorsmod.Wrapf(
203+
errortypes.ErrInsufficientFee,
204+
"insufficient fees; got: %s required: %s",
205+
fee, requiredFee,
206+
)
207+
}
208+
}
209+
210+
return next(ctx, tx, simulate)
211+
}

app/ante/fees_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,7 @@ func (s AnteTestSuite) TestEthMinGasPriceDecorator() {
345345
}
346346
}
347347
}
348+
349+
func (suite AnteTestSuite) TestEthMempoolFeeDecorator() {
350+
// TODO: add test
351+
}

app/ante/mempool.go

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

app/ante/setup_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ante_test
2+
3+
import (
4+
"math/big"
5+
6+
storetypes "github.com/cosmos/cosmos-sdk/store/types"
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/evmos/ethermint/app/ante"
9+
evmtypes "github.com/evmos/ethermint/x/evm/types"
10+
)
11+
12+
func (suite AnteTestSuite) TestEthSetupContextDecorator() {
13+
dec := ante.NewEthSetUpContextDecorator(suite.app.EvmKeeper)
14+
tx := evmtypes.NewTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil)
15+
16+
testCases := []struct {
17+
name string
18+
tx sdk.Tx
19+
expPass bool
20+
}{
21+
{"invalid transaction type - does not implement GasTx", &invalidTx{}, false},
22+
{
23+
"success - transaction implement GasTx",
24+
tx,
25+
true,
26+
},
27+
}
28+
29+
for _, tc := range testCases {
30+
suite.Run(tc.name, func() {
31+
ctx, err := dec.AnteHandle(suite.ctx, tc.tx, false, NextFn)
32+
33+
if tc.expPass {
34+
suite.Require().NoError(err)
35+
suite.Equal(storetypes.GasConfig{}, ctx.KVGasConfig())
36+
suite.Equal(storetypes.GasConfig{}, ctx.TransientKVGasConfig())
37+
} else {
38+
suite.Require().Error(err)
39+
}
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)