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") +}