laconicd-deprecated/app/ante/fees_test.go
Loredana Cirstea 046cd00174
feemarket: global MinGasPrice parameter (#1104)
* Add min_gas_price to feemarket params

* Add MinGasPriceDecorators

* feemarket integration tests for MinGasPrice

* Restructure integration tests

* Simplify integration tests context

We use DeliverTx context to set up the app, otherwise not all settings are initialized.
We test CheckTx with `s.app.BaseApp.CheckTx(req)`, which uses the `CheckTx` mode and context.

* Update MinGasPrice spec in feemarket module

* reorder ethermint module order for initializing genesis

* feemarket migrations for adding MinGasPrice param

* update changelog

* Additional unit tests for MinGasPrice = 0, tx gas price > 0 (PR review)

https://github.com/tharsis/ethermint/pull/1104#discussion_r884991661

* Use 0 MinGasPrice for transaction simulations

* Fix duplicate registration of feemarket GenesisState and Params (PR review)

https://github.com/tharsis/ethermint/pull/1104#issuecomment-1141893712
2022-05-31 18:28:46 +02:00

340 lines
9.7 KiB
Go

package ante_test
import (
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/tharsis/ethermint/app/ante"
"github.com/tharsis/ethermint/tests"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
)
var execTypes = []struct {
name string
isCheckTx bool
simulate bool
}{
{"deliverTx", false, false},
{"deliverTxSimulate", false, true},
}
func (s AnteTestSuite) TestMinGasPriceDecorator() {
denom := evmtypes.DefaultEVMDenom
testMsg := banktypes.MsgSend{
FromAddress: "evmos1x8fhpj9nmhqk8z9kpgjt95ck2xwyue0ptzkucp",
ToAddress: "evmos1dx67l23hz9l0k9hcher8xz04uj7wf3yu26l2yn",
Amount: sdk.Coins{sdk.Coin{Amount: sdk.NewInt(10), Denom: denom}},
}
testCases := []struct {
name string
malleate func() sdk.Tx
expPass bool
errMsg string
}{
{
"invalid cosmos tx type",
func() sdk.Tx {
return &invalidTx{}
},
false,
"must be a FeeTx",
},
{
"valid cosmos tx with MinGasPrices = 0, gasPrice = 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
txBuilder := s.CreateTestCosmosTxBuilder(sdk.NewInt(0), denom, &testMsg)
return txBuilder.GetTx()
},
true,
"",
},
{
"valid cosmos tx with MinGasPrices = 0, gasPrice > 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
txBuilder := s.CreateTestCosmosTxBuilder(sdk.NewInt(10), denom, &testMsg)
return txBuilder.GetTx()
},
true,
"",
},
{
"valid cosmos tx with MinGasPrices = 10, gasPrice = 10",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
txBuilder := s.CreateTestCosmosTxBuilder(sdk.NewInt(10), denom, &testMsg)
return txBuilder.GetTx()
},
true,
"",
},
{
"invalid cosmos tx with MinGasPrices = 10, gasPrice = 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
txBuilder := s.CreateTestCosmosTxBuilder(sdk.NewInt(0), denom, &testMsg)
return txBuilder.GetTx()
},
false,
"provided fee < minimum global fee",
},
{
"invalid cosmos tx with wrong denom",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
txBuilder := s.CreateTestCosmosTxBuilder(sdk.NewInt(10), "stake", &testMsg)
return txBuilder.GetTx()
},
false,
"provided fee < minimum global fee",
},
}
for _, et := range execTypes {
for _, tc := range testCases {
s.Run(et.name+"_"+tc.name, func() {
// s.SetupTest(et.isCheckTx)
ctx := s.ctx.WithIsReCheckTx(et.isCheckTx)
dec := ante.NewMinGasPriceDecorator(s.app.FeeMarketKeeper, s.app.EvmKeeper)
_, err := dec.AnteHandle(ctx, tc.malleate(), et.simulate, nextFn)
if tc.expPass {
s.Require().NoError(err, tc.name)
} else {
s.Require().Error(err, tc.name)
s.Require().Contains(err.Error(), tc.errMsg, tc.name)
}
})
}
}
}
func (s AnteTestSuite) TestEthMinGasPriceDecorator() {
denom := evmtypes.DefaultEVMDenom
from, privKey := tests.NewAddrKey()
to := tests.GenerateAddress()
emptyAccessList := ethtypes.AccessList{}
testCases := []struct {
name string
malleate func() sdk.Tx
expPass bool
errMsg string
}{
{
"invalid tx type",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
return &invalidTx{}
},
false,
"invalid message type",
},
{
"wrong tx type",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
testMsg := banktypes.MsgSend{
FromAddress: "evmos1x8fhpj9nmhqk8z9kpgjt95ck2xwyue0ptzkucp",
ToAddress: "evmos1dx67l23hz9l0k9hcher8xz04uj7wf3yu26l2yn",
Amount: sdk.Coins{sdk.Coin{Amount: sdk.NewInt(10), Denom: denom}},
}
txBuilder := s.CreateTestCosmosTxBuilder(sdk.NewInt(0), denom, &testMsg)
return txBuilder.GetTx()
},
false,
"invalid message type",
},
{
"valid: invalid tx type with MinGasPrices = 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
return &invalidTx{}
},
true,
"",
},
{
"valid legacy tx with MinGasPrices = 0, gasPrice = 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), big.NewInt(0), nil, nil, nil)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
{
"valid legacy tx with MinGasPrices = 0, gasPrice > 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), big.NewInt(10), nil, nil, nil)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
{
"valid legacy tx with MinGasPrices = 10, gasPrice = 10",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), big.NewInt(10), nil, nil, nil)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
{
"invalid legacy tx with MinGasPrices = 10, gasPrice = 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), big.NewInt(0), nil, nil, nil)
return s.CreateTestTx(msg, privKey, 1, false)
},
false,
"provided fee < minimum global fee",
},
{
"valid dynamic tx with MinGasPrices = 0, EffectivePrice = 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), nil, big.NewInt(0), big.NewInt(0), &emptyAccessList)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
{
"valid dynamic tx with MinGasPrices = 0, EffectivePrice > 0",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.ZeroDec()
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), nil, big.NewInt(100), big.NewInt(50), &emptyAccessList)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
{
"valid dynamic tx with MinGasPrices < EffectivePrice",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), nil, big.NewInt(100), big.NewInt(100), &emptyAccessList)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
{
"invalid dynamic tx with MinGasPrices > EffectivePrice",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), nil, big.NewInt(0), big.NewInt(0), &emptyAccessList)
return s.CreateTestTx(msg, privKey, 1, false)
},
false,
"provided fee < minimum global fee",
},
{
"invalid dynamic tx with MinGasPrices > BaseFee, MinGasPrices > EffectivePrice",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(100)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
feemarketParams := s.app.FeeMarketKeeper.GetParams(s.ctx)
feemarketParams.BaseFee = sdk.NewInt(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, feemarketParams)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), nil, big.NewInt(1000), big.NewInt(0), &emptyAccessList)
return s.CreateTestTx(msg, privKey, 1, false)
},
false,
"provided fee < minimum global fee",
},
{
"valid dynamic tx with MinGasPrices > BaseFee, MinGasPrices < EffectivePrice (big GasTipCap)",
func() sdk.Tx {
params := s.app.FeeMarketKeeper.GetParams(s.ctx)
params.MinGasPrice = sdk.NewDec(100)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
feemarketParams := s.app.FeeMarketKeeper.GetParams(s.ctx)
feemarketParams.BaseFee = sdk.NewInt(10)
s.app.FeeMarketKeeper.SetParams(s.ctx, feemarketParams)
msg := s.BuildTestEthTx(from, to, nil, make([]byte, 0), nil, big.NewInt(1000), big.NewInt(101), &emptyAccessList)
return s.CreateTestTx(msg, privKey, 1, false)
},
true,
"",
},
}
for _, et := range execTypes {
for _, tc := range testCases {
s.Run(et.name+"_"+tc.name, func() {
// s.SetupTest(et.isCheckTx)
s.SetupTest()
dec := ante.NewEthMinGasPriceDecorator(s.app.FeeMarketKeeper, s.app.EvmKeeper)
_, err := dec.AnteHandle(s.ctx, tc.malleate(), et.simulate, nextFn)
if tc.expPass {
s.Require().NoError(err, tc.name)
} else {
s.Require().Error(err, tc.name)
s.Require().Contains(err.Error(), tc.errMsg, tc.name)
}
})
}
}
}