Skip to content

Commit 7391d64

Browse files
authored
fix: cap mempool priority overflow (#494)
* fix: cap mempool priority overflow * assgin maxPriority once
1 parent 3c361ad commit 7391d64

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

x/dynamic-fee/ante/fee.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ante
22

33
import (
4+
stdmath "math"
5+
46
"cosmossdk.io/errors"
57
"cosmossdk.io/math"
68

@@ -28,6 +30,26 @@ func NewMempoolFeeChecker(
2830
keeper,
2931
}
3032
}
33+
34+
var maxPriority math.LegacyDec
35+
36+
func init() {
37+
maxPriority = math.LegacyNewDecFromInt(math.NewInt(stdmath.MaxInt64))
38+
}
39+
40+
func priorityFromGasPrice(gasPrice math.LegacyDec) int64 {
41+
if !gasPrice.IsPositive() {
42+
return 1
43+
}
44+
45+
scaledPriority := gasPrice.MulInt64(1_000_000)
46+
if scaledPriority.GTE(maxPriority) {
47+
return stdmath.MaxInt64
48+
}
49+
50+
return math.Max(scaledPriority.TruncateInt64(), 1)
51+
}
52+
3153
func (fc MempoolFeeChecker) CheckTxFeeWithMinGasPrices(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
3254
feeTx, ok := tx.(sdk.FeeTx)
3355
if !ok {
@@ -69,8 +91,8 @@ func (fc MempoolFeeChecker) CheckTxFeeWithMinGasPrices(ctx sdk.Context, tx sdk.T
6991

7092
gasPriceFromTotalFee := math.LegacyNewDecFromInt(totalFeeBaseAmount).Quo(math.LegacyNewDec(int64(gas))) //nolint: gosec
7193

72-
// priority is max(gasPriceFromTotalFee * 1e6, 1)
73-
priority = math.Max(gasPriceFromTotalFee.MulInt64(1000000).TruncateInt64(), 1)
94+
// priority is max(gasPriceFromTotalFee * 1e6, 1), capped to int64 bounds.
95+
priority = priorityFromGasPrice(gasPriceFromTotalFee)
7496

7597
if gasPriceFromTotalFee.LT(baseGasPrice) {
7698
return nil, 0, errors.Wrapf(

x/dynamic-fee/ante/fee_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ante_test
33
import (
44
"context"
55
"fmt"
6+
stdmath "math"
67

78
"cosmossdk.io/math"
89

@@ -147,3 +148,43 @@ func (suite *AnteTestSuite) TestEnsureMempoolFees() {
147148
_, _, err = fc.CheckTxFeeWithMinGasPrices(suite.ctx, tx)
148149
suite.Require().NotNil(err, "Decorator should have errored on too low fee for local gasPrice")
149150
}
151+
152+
func (suite *AnteTestSuite) TestEnsureMempoolFees_PriorityOverflowCapped() {
153+
suite.SetupTest()
154+
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
155+
156+
fc := ante.NewMempoolFeeChecker(TestAnteKeeper{
157+
pools: map[string][]math.Int{
158+
"rare": {
159+
math.NewInt(5_000_000_000_000_000_000),
160+
math.OneInt(),
161+
},
162+
},
163+
weights: map[string][]math.LegacyDec{
164+
"rare": {
165+
math.LegacyOneDec(),
166+
math.LegacyOneDec(),
167+
},
168+
},
169+
baseDenom: baseDenom,
170+
baseGasPrice: math.LegacyZeroDec(),
171+
})
172+
173+
priv1, _, addr1 := testdata.KeyTestPubAddr()
174+
msg := testdata.NewTestMsg(addr1)
175+
176+
suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
177+
suite.txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("rare", math.OneInt())))
178+
suite.txBuilder.SetGasLimit(1)
179+
180+
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
181+
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID())
182+
suite.Require().NoError(err)
183+
184+
suite.ctx = suite.ctx.WithIsCheckTx(true)
185+
suite.ctx = suite.ctx.WithMinGasPrices(nil)
186+
187+
_, priority, err := fc.CheckTxFeeWithMinGasPrices(suite.ctx, tx)
188+
suite.Require().NoError(err)
189+
suite.Require().Equal(int64(stdmath.MaxInt64), priority)
190+
}

0 commit comments

Comments
 (0)