From 45cdd908eb62895f6e9beda50d2fc4bdee88f0f9 Mon Sep 17 00:00:00 2001 From: Marko Date: Mon, 29 Nov 2021 12:38:42 +0100 Subject: [PATCH] fix: take in the smallest fee (#10607) ## Description Follow up to have the priority middleware take the smallest fee to avoid gaming the system. --- ### 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/master/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/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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) --- .../{tx_priority.go => priority.go} | 7 +++- x/auth/middleware/priority_test.go | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) rename x/auth/middleware/{tx_priority.go => priority.go} (90%) create mode 100644 x/auth/middleware/priority_test.go diff --git a/x/auth/middleware/tx_priority.go b/x/auth/middleware/priority.go similarity index 90% rename from x/auth/middleware/tx_priority.go rename to x/auth/middleware/priority.go index 81d8d3124..fa6984b74 100644 --- a/x/auth/middleware/tx_priority.go +++ b/x/auth/middleware/priority.go @@ -50,12 +50,15 @@ func (h txPriorityHandler) SimulateTx(ctx context.Context, sdkTx sdk.Tx, req tx. return h.next.SimulateTx(ctx, sdkTx, req) } -// GetTxPriority returns a naive tx priority based on the total sum of all fees +// GetTxPriority returns a naive tx priority based on the amount of the smallest denomination of the fee // provided in a transaction. func GetTxPriority(fee sdk.Coins) int64 { var priority int64 for _, c := range fee { - priority += c.Amount.Int64() + p := c.Amount.Int64() + if priority == 0 || p < priority { + priority = p + } } return priority diff --git a/x/auth/middleware/priority_test.go b/x/auth/middleware/priority_test.go new file mode 100644 index 000000000..a06233f7e --- /dev/null +++ b/x/auth/middleware/priority_test.go @@ -0,0 +1,38 @@ +package middleware_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/middleware" + abci "github.com/tendermint/tendermint/abci/types" +) + +func (s *MWTestSuite) TestPriority() { + ctx := s.SetupTest(true) // setup + txBuilder := s.clientCtx.TxConfig.NewTxBuilder() + + txHandler := middleware.ComposeMiddlewares(noopTxHandler{}, middleware.TxPriorityHandler) + + // keys and addresses + priv1, _, addr1 := testdata.KeyTestPubAddr() + + // msg and signatures + msg := testdata.NewTestMsg(addr1) + atomCoin := sdk.NewCoin("atom", sdk.NewInt(150)) + apeCoin := sdk.NewInt64Coin("ape", 1500000) + feeAmount := sdk.NewCoins(apeCoin, atomCoin) + gasLimit := testdata.NewTestGasLimit() + s.Require().NoError(txBuilder.SetMsgs(msg)) + txBuilder.SetFeeAmount(feeAmount) + txBuilder.SetGasLimit(gasLimit) + + privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} + tx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID()) + s.Require().NoError(err) + + // txHandler errors with insufficient fees + res, err := txHandler.CheckTx(sdk.WrapSDKContext(ctx), tx, abci.RequestCheckTx{}) + s.Require().NoError(err, "Middleware should not have errored on too low fee for local gasPrice") + s.Require().Equal(atomCoin.Amount.Int64(), res.Priority, "priority should be atom amount") +}