fix!: prevent 0 gas txs (#12416)

## Description

Update `DeductFeeDecorator` to prevent 0-gas txs.

closes: #12415

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Aleksandr Bezobchuk 2022-07-04 01:22:38 -04:00 committed by GitHub
parent 686c50d113
commit 74f265c942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 46 deletions

View File

@ -62,6 +62,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes ### Bug Fixes
* [#12416](https://github.com/cosmos/cosmos-sdk/pull/12416) Prevent zero gas transactions in the `DeductFeeDecorator` AnteHandler decorator.
* (x/mint) [#12384](https://github.com/cosmos/cosmos-sdk/pull/12384) Ensure `GoalBonded` must be positive when performing `x/mint` parameter validation. * (x/mint) [#12384](https://github.com/cosmos/cosmos-sdk/pull/12384) Ensure `GoalBonded` must be positive when performing `x/mint` parameter validation.
* (x/auth) [#12261](https://github.com/cosmos/cosmos-sdk/pull/12261) Deprecate pagination in GetTxsEventRequest/Response in favor of page and limit to align with tendermint `SignClient.TxSearch` * (x/auth) [#12261](https://github.com/cosmos/cosmos-sdk/pull/12261) Deprecate pagination in GetTxsEventRequest/Response in favor of page and limit to align with tendermint `SignClient.TxSearch`
* (vesting) [#12190](https://github.com/cosmos/cosmos-sdk/pull/12190) Replace https://github.com/cosmos/cosmos-sdk/pull/12190 to use `NewBaseAccountWithAddress` in all vesting account message handlers. * (vesting) [#12190](https://github.com/cosmos/cosmos-sdk/pull/12190) Replace https://github.com/cosmos/cosmos-sdk/pull/12190 to use `NewBaseAccountWithAddress` in all vesting account message handlers.

View File

@ -161,6 +161,10 @@ var (
// ErrAppConfig defines an error occurred if min-gas-prices field in BaseConfig is empty. // ErrAppConfig defines an error occurred if min-gas-prices field in BaseConfig is empty.
ErrAppConfig = Register(RootCodespace, 40, "error in app.toml") ErrAppConfig = Register(RootCodespace, 40, "error in app.toml")
// ErrInvalidGasLimit defines an error when an invalid GasWanted value is
// supplied.
ErrInvalidGasLimit = Register(RootCodespace, 41, "invalid gas limit")
// ErrPanic should only be set when we recovering from a panic // ErrPanic should only be set when we recovering from a panic
ErrPanic = errorsmod.ErrPanic ErrPanic = errorsmod.ErrPanic
) )

View File

@ -37,6 +37,15 @@ func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKee
} }
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
if ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas")
}
fee, priority, err := dfd.txFeeChecker(ctx, tx) fee, priority, err := dfd.txFeeChecker(ctx, tx)
if err != nil { if err != nil {
return ctx, err return ctx, err

View File

@ -8,66 +8,96 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/bank/testutil"
) )
func (suite *AnteTestSuite) TestEnsureMempoolFees() { func (s *AnteTestSuite) TestDeductFeeDecorator_ZeroGas() {
suite.SetupTest(true) // setup s.SetupTest(true) // setup
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
mfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, suite.feeGrantKeeper, nil) mfd := ante.NewDeductFeeDecorator(s.accountKeeper, s.bankKeeper, s.feeGrantKeeper, nil)
antehandler := sdk.ChainAnteDecorators(mfd) antehandler := sdk.ChainAnteDecorators(mfd)
// keys and addresses // keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr() priv1, _, addr1 := testdata.KeyTestPubAddr()
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300))) coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300)))
testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, coins) testutil.FundAccount(s.bankKeeper, s.ctx, addr1, coins)
// msg and signatures
msg := testdata.NewTestMsg(addr1)
s.Require().NoError(s.txBuilder.SetMsgs(msg))
// set zero gas
s.txBuilder.SetGasLimit(0)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
s.Require().NoError(err)
// Set IsCheckTx to true
s.ctx = s.ctx.WithIsCheckTx(true)
_, err = antehandler(s.ctx, tx, false)
s.Require().Error(err)
}
func (s *AnteTestSuite) TestEnsureMempoolFees() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
mfd := ante.NewDeductFeeDecorator(s.accountKeeper, s.bankKeeper, s.feeGrantKeeper, nil)
antehandler := sdk.ChainAnteDecorators(mfd)
// keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr()
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300)))
testutil.FundAccount(s.bankKeeper, s.ctx, addr1, coins)
// msg and signatures // msg and signatures
msg := testdata.NewTestMsg(addr1) msg := testdata.NewTestMsg(addr1)
feeAmount := testdata.NewTestFeeAmount() feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit() gasLimit := testdata.NewTestGasLimit()
suite.Require().NoError(suite.txBuilder.SetMsgs(msg)) s.Require().NoError(s.txBuilder.SetMsgs(msg))
suite.txBuilder.SetFeeAmount(feeAmount) s.txBuilder.SetFeeAmount(feeAmount)
suite.txBuilder.SetGasLimit(gasLimit) s.txBuilder.SetGasLimit(gasLimit)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
suite.Require().NoError(err) s.Require().NoError(err)
// Set high gas price so standard test fee fails // Set high gas price so standard test fee fails
atomPrice := sdk.NewDecCoinFromDec("atom", sdk.NewDec(200).Quo(sdk.NewDec(100000))) atomPrice := sdk.NewDecCoinFromDec("atom", sdk.NewDec(200).Quo(sdk.NewDec(100000)))
highGasPrice := []sdk.DecCoin{atomPrice} highGasPrice := []sdk.DecCoin{atomPrice}
suite.ctx = suite.ctx.WithMinGasPrices(highGasPrice) s.ctx = s.ctx.WithMinGasPrices(highGasPrice)
// Set IsCheckTx to true // Set IsCheckTx to true
suite.ctx = suite.ctx.WithIsCheckTx(true) s.ctx = s.ctx.WithIsCheckTx(true)
// antehandler errors with insufficient fees // antehandler errors with insufficient fees
_, err = antehandler(suite.ctx, tx, false) _, err = antehandler(s.ctx, tx, false)
suite.Require().NotNil(err, "Decorator should have errored on too low fee for local gasPrice") s.Require().NotNil(err, "Decorator should have errored on too low fee for local gasPrice")
// Set IsCheckTx to false // Set IsCheckTx to false
suite.ctx = suite.ctx.WithIsCheckTx(false) s.ctx = s.ctx.WithIsCheckTx(false)
// antehandler should not error since we do not check minGasPrice in DeliverTx // antehandler should not error since we do not check minGasPrice in DeliverTx
_, err = antehandler(suite.ctx, tx, false) _, err = antehandler(s.ctx, tx, false)
suite.Require().Nil(err, "MempoolFeeDecorator returned error in DeliverTx") s.Require().Nil(err, "MempoolFeeDecorator returned error in DeliverTx")
// Set IsCheckTx back to true for testing sufficient mempool fee // Set IsCheckTx back to true for testing sufficient mempool fee
suite.ctx = suite.ctx.WithIsCheckTx(true) s.ctx = s.ctx.WithIsCheckTx(true)
atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000))) atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000)))
lowGasPrice := []sdk.DecCoin{atomPrice} lowGasPrice := []sdk.DecCoin{atomPrice}
suite.ctx = suite.ctx.WithMinGasPrices(lowGasPrice) s.ctx = s.ctx.WithMinGasPrices(lowGasPrice)
newCtx, err := antehandler(suite.ctx, tx, false) newCtx, err := antehandler(s.ctx, tx, false)
suite.Require().Nil(err, "Decorator should not have errored on fee higher than local gasPrice") s.Require().Nil(err, "Decorator should not have errored on fee higher than local gasPrice")
// Priority is the smallest amount in any denom. Since we have only 1 fee // Priority is the smallest amount in any denom. Since we have only 1 fee
// of 150atom, the priority here is 150. // of 150atom, the priority here is 150.
suite.Require().Equal(feeAmount.AmountOf("atom").Int64(), newCtx.Priority()) s.Require().Equal(feeAmount.AmountOf("atom").Int64(), newCtx.Priority())
} }
func (suite *AnteTestSuite) TestDeductFees() { func (s *AnteTestSuite) TestDeductFees() {
suite.SetupTest(false) // setup s.SetupTest(false) // setup
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
// keys and addresses // keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr() priv1, _, addr1 := testdata.KeyTestPubAddr()
@ -76,34 +106,34 @@ func (suite *AnteTestSuite) TestDeductFees() {
msg := testdata.NewTestMsg(addr1) msg := testdata.NewTestMsg(addr1)
feeAmount := testdata.NewTestFeeAmount() feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit() gasLimit := testdata.NewTestGasLimit()
suite.Require().NoError(suite.txBuilder.SetMsgs(msg)) s.Require().NoError(s.txBuilder.SetMsgs(msg))
suite.txBuilder.SetFeeAmount(feeAmount) s.txBuilder.SetFeeAmount(feeAmount)
suite.txBuilder.SetGasLimit(gasLimit) s.txBuilder.SetGasLimit(gasLimit)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
suite.Require().NoError(err) s.Require().NoError(err)
// Set account with insufficient funds // Set account with insufficient funds
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr1) acc := s.accountKeeper.NewAccountWithAddress(s.ctx, addr1)
suite.accountKeeper.SetAccount(suite.ctx, acc) s.accountKeeper.SetAccount(s.ctx, acc)
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10))) coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10)))
err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, coins) err = testutil.FundAccount(s.bankKeeper, s.ctx, addr1, coins)
suite.Require().NoError(err) s.Require().NoError(err)
dfd := ante.NewDeductFeeDecorator(suite.accountKeeper, suite.bankKeeper, nil, nil) dfd := ante.NewDeductFeeDecorator(s.accountKeeper, s.bankKeeper, nil, nil)
antehandler := sdk.ChainAnteDecorators(dfd) antehandler := sdk.ChainAnteDecorators(dfd)
_, err = antehandler(suite.ctx, tx, false) _, err = antehandler(s.ctx, tx, false)
suite.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds") s.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds")
// Set account with sufficient funds // Set account with sufficient funds
suite.accountKeeper.SetAccount(suite.ctx, acc) s.accountKeeper.SetAccount(s.ctx, acc)
err = testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200)))) err = testutil.FundAccount(s.bankKeeper, s.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
suite.Require().NoError(err) s.Require().NoError(err)
_, err = antehandler(suite.ctx, tx, false) _, err = antehandler(s.ctx, tx, false)
suite.Require().Nil(err, "Tx errored after account has been set with sufficient funds") s.Require().Nil(err, "Tx errored after account has been set with sufficient funds")
} }

View File

@ -50,6 +50,10 @@ type AnteTestSuite struct {
feeGrantKeeper feegrantkeeper.Keeper feeGrantKeeper feegrantkeeper.Keeper
} }
func TestAnteTestSuite(t *testing.T) {
suite.Run(t, new(AnteTestSuite))
}
// SetupTest setups a new test, with new app, context, and anteHandler. // SetupTest setups a new test, with new app, context, and anteHandler.
func (suite *AnteTestSuite) SetupTest(isCheckTx bool) { func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
var ( var (
@ -209,7 +213,3 @@ func (suite *AnteTestSuite) RunTestCase(privs []cryptotypes.PrivKey, msgs []sdk.
} }
}) })
} }
func TestAnteTestSuite(t *testing.T) {
suite.Run(t, new(AnteTestSuite))
}