Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(fees): only allow user to pass in aevmos native token as transaction fees #1998

Merged
merged 11 commits into from
Nov 23, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (werc20) [#1990](https://github.com/evmos/evmos/pull/1990) Add WERC-20 Precompile events.
- (distribution) [#1992](https://github.com/evmos/evmos/pull/1992) Remove outdated check utility for distribution approval.
- (erc20) [#1994](https://github.com/evmos/evmos/pull/1994) Add tests for ERC20 precompile type utilities.
- (fees) [#1998](https://github.com/evmos/evmos/pull/1998) Only allow user to pass in aevmos native token as transaction fees.
- (erc20) [#2005](https://github.com/evmos/evmos/pull/2005) Add tests for ERC20 precompile approvals.
- (bin) [#2007](https://github.com/evmos/evmos/pull/2007) Add commands in Makefile for building binary with rocksDB and pebbleDB
- (erc20) [#2009](https://github.com/evmos/evmos/pull/2009) Add ERC20 precompile transaction unit tests.
Expand Down
18 changes: 14 additions & 4 deletions app/ante/cosmos/min_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package cosmos

import (
"fmt"
"math/big"

errorsmod "cosmossdk.io/errors"
evmante "github.com/evmos/evmos/v15/app/ante/evm"

sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
evmante "github.com/evmos/evmos/v15/app/ante/evm"
)

// MinGasPriceDecorator will check if the transaction's fee is at least as large
Expand All @@ -35,20 +37,28 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate

minGasPrice := mpd.feesKeeper.GetParams(ctx).MinGasPrice

feeCoins := feeTx.GetFee()
evmParams := mpd.evmKeeper.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()

// only allow user to pass in aevmos native token as transaction fees
validFees := len(feeCoins) == 0 || (len(feeCoins) == 1 && feeCoins.GetDenomByIndex(0) == evmDenom)
if !validFees && !simulate {
return ctx, fmt.Errorf("expected only use native token %s for fee, but got %s", evmDenom, feeCoins.String())
}

// Short-circuit if min gas price is 0 or if simulating
if minGasPrice.IsZero() || simulate {
return next(ctx, tx, simulate)
}
evmParams := mpd.evmKeeper.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()

minGasPrices := sdk.DecCoins{
{
Denom: evmDenom,
Amount: minGasPrice,
},
}

feeCoins := feeTx.GetFee()
gas := feeTx.GetGas()

requiredFees := make(sdk.Coins, 0)
Expand Down
66 changes: 65 additions & 1 deletion app/ante/cosmos/min_price_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cosmos_test

import (
"fmt"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
cosmosante "github.com/evmos/evmos/v15/app/ante/cosmos"
Expand Down Expand Up @@ -115,7 +118,68 @@ func (suite *AnteTestSuite) TestMinGasPriceDecorator() {
return txBuilder.GetTx()
},
false,
"provided fee < minimum global fee",
fmt.Sprintf("expected only use native token %s for fee", denom),
true,
},
{
"valid cosmos tx with MinGasPrices = 0, gasPrice = 0, valid fee",
func() sdk.Tx {
params := suite.app.FeeMarketKeeper.GetParams(suite.ctx)
params.MinGasPrice = sdk.ZeroDec()
err := suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
suite.Require().NoError(err)

txBuilder := suite.CreateTestCosmosTxBuilderWithFees(sdk.Coins{sdk.Coin{Amount: sdkmath.NewInt(0), Denom: denom}}, &testMsg)
return txBuilder.GetTx()
},
true,
"",
true,
},
{
"valid cosmos tx with MinGasPrices = 0, gasPrice = 0, nil fees, means len(fees) == 0",
func() sdk.Tx {
params := suite.app.FeeMarketKeeper.GetParams(suite.ctx)
params.MinGasPrice = sdk.ZeroDec()
err := suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
suite.Require().NoError(err)

txBuilder := suite.CreateTestCosmosTxBuilderWithFees(nil, &testMsg)
return txBuilder.GetTx()
},
true,
"",
true,
},
{
"valid cosmos tx with MinGasPrices = 0, gasPrice = 0, empty fees, means len(fees) == 0",
func() sdk.Tx {
params := suite.app.FeeMarketKeeper.GetParams(suite.ctx)
params.MinGasPrice = sdk.ZeroDec()
err := suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
suite.Require().NoError(err)

txBuilder := suite.CreateTestCosmosTxBuilderWithFees(sdk.Coins{}, &testMsg)
return txBuilder.GetTx()
},
true,
"",
true,
},
{
"valid cosmos tx with MinGasPrices = 0, gasPrice = 0, invalid fees",
func() sdk.Tx {
params := suite.app.FeeMarketKeeper.GetParams(suite.ctx)
params.MinGasPrice = sdk.ZeroDec()
err := suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
suite.Require().NoError(err)

fees := sdk.Coins{sdk.Coin{Amount: sdkmath.NewInt(0), Denom: denom}, sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "stake"}}
txBuilder := suite.CreateTestCosmosTxBuilderWithFees(fees, &testMsg)
return txBuilder.GetTx()
},
false,
fmt.Sprintf("expected only use native token %s for fee", denom),
true,
},
}
Expand Down
9 changes: 9 additions & 0 deletions app/ante/cosmos/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func (suite *AnteTestSuite) CreateTestCosmosTxBuilder(gasPrice sdkmath.Int, deno
return txBuilder
}

func (suite *AnteTestSuite) CreateTestCosmosTxBuilderWithFees(fees sdk.Coins, msgs ...sdk.Msg) client.TxBuilder {
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetGasLimit(TestGasLimit)
txBuilder.SetFeeAmount(fees)
err := txBuilder.SetMsgs(msgs...)
suite.Require().NoError(err)
return txBuilder
}

func newMsgExec(grantee sdk.AccAddress, msgs []sdk.Msg) *authz.MsgExec {
msg := authz.NewMsgExec(grantee, msgs)
return &msg
Expand Down