## Description We decided to remove middlewares, and revert to antehandlers (exactly like in v045) for this release. A better middleware solution will be implemented after v046. ref: https://github.com/cosmos/cosmos-sdk/issues/11955 This PR is part 2 of 2: - part 1: Revert baseapp and middlewares to v0.45.4 - part 2: Add posthandler, tips, priority Depends on: - [x] #11979 --- Suggestion for reviewers: - Apart from correctness, I would also like someone to review **exhaustiveness**. I.e. all changes we made in v046 into the [middleware folder](https://github.com/cosmos/cosmos-sdk/tree/v0.46.0-beta2/x/auth/middleware) are reflected in this PR, and that I didn't forget anything. I found the following ones: - add a TxFeeChecker in DeductFee - add a ExtensionChecker in ExtCheckerDecorator - add a TipDecorator --- ### 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)
110 lines
4.2 KiB
Go
110 lines
4.2 KiB
Go
package ante_test
|
|
|
|
import (
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/ante"
|
|
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
|
)
|
|
|
|
func (suite *AnteTestSuite) TestEnsureMempoolFees() {
|
|
suite.SetupTest(true) // setup
|
|
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
mfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, nil)
|
|
antehandler := sdk.ChainAnteDecorators(mfd)
|
|
|
|
// keys and addresses
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300)))
|
|
testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, coins)
|
|
|
|
// msg and signatures
|
|
msg := testdata.NewTestMsg(addr1)
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
|
|
suite.txBuilder.SetFeeAmount(feeAmount)
|
|
suite.txBuilder.SetGasLimit(gasLimit)
|
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID())
|
|
suite.Require().NoError(err)
|
|
|
|
// Set high gas price so standard test fee fails
|
|
atomPrice := sdk.NewDecCoinFromDec("atom", sdk.NewDec(200).Quo(sdk.NewDec(100000)))
|
|
highGasPrice := []sdk.DecCoin{atomPrice}
|
|
suite.ctx = suite.ctx.WithMinGasPrices(highGasPrice)
|
|
|
|
// Set IsCheckTx to true
|
|
suite.ctx = suite.ctx.WithIsCheckTx(true)
|
|
|
|
// antehandler errors with insufficient fees
|
|
_, err = antehandler(suite.ctx, tx, false)
|
|
suite.Require().NotNil(err, "Decorator should have errored on too low fee for local gasPrice")
|
|
|
|
// Set IsCheckTx to false
|
|
suite.ctx = suite.ctx.WithIsCheckTx(false)
|
|
|
|
// antehandler should not error since we do not check minGasPrice in DeliverTx
|
|
_, err = antehandler(suite.ctx, tx, false)
|
|
suite.Require().Nil(err, "MempoolFeeDecorator returned error in DeliverTx")
|
|
|
|
// Set IsCheckTx back to true for testing sufficient mempool fee
|
|
suite.ctx = suite.ctx.WithIsCheckTx(true)
|
|
|
|
atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000)))
|
|
lowGasPrice := []sdk.DecCoin{atomPrice}
|
|
suite.ctx = suite.ctx.WithMinGasPrices(lowGasPrice)
|
|
|
|
newCtx, err := antehandler(suite.ctx, tx, false)
|
|
suite.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
|
|
// of 150atom, the priority here is 150.
|
|
suite.Require().Equal(feeAmount.AmountOf("atom").Int64(), newCtx.Priority())
|
|
}
|
|
|
|
func (suite *AnteTestSuite) TestDeductFees() {
|
|
suite.SetupTest(false) // setup
|
|
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
// keys and addresses
|
|
priv1, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
// msg and signatures
|
|
msg := testdata.NewTestMsg(addr1)
|
|
feeAmount := testdata.NewTestFeeAmount()
|
|
gasLimit := testdata.NewTestGasLimit()
|
|
suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
|
|
suite.txBuilder.SetFeeAmount(feeAmount)
|
|
suite.txBuilder.SetGasLimit(gasLimit)
|
|
|
|
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
|
|
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID())
|
|
suite.Require().NoError(err)
|
|
|
|
// Set account with insufficient funds
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr1)
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10)))
|
|
err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, coins)
|
|
suite.Require().NoError(err)
|
|
|
|
dfd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, nil, nil)
|
|
antehandler := sdk.ChainAnteDecorators(dfd)
|
|
|
|
_, err = antehandler(suite.ctx, tx, false)
|
|
|
|
suite.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds")
|
|
|
|
// Set account with sufficient funds
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
err = testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
|
|
suite.Require().NoError(err)
|
|
|
|
_, err = antehandler(suite.ctx, tx, false)
|
|
|
|
suite.Require().Nil(err, "Tx errored after account has been set with sufficient funds")
|
|
}
|