From 1ad94ecac52f69bfef1655e46719e7bcb18e01c4 Mon Sep 17 00:00:00 2001 From: Brian Luk Date: Wed, 20 Mar 2024 09:29:08 -0700 Subject: [PATCH 1/2] prevent integer overflow --- x/feemarket/keeper/keeper.go | 6 ++++++ x/feemarket/keeper/params.go | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index 83b2862c2b..13898f4293 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -20,6 +20,7 @@ import ( corestoretypes "cosmossdk.io/core/store" "cosmossdk.io/log" + sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -40,6 +41,7 @@ type Keeper struct { transientKey storetypes.StoreKey // the address capable of executing a MsgUpdateParams message. Typically, this should be the x/gov module account. authority sdk.AccAddress + maxGas sdkmath.Int } // NewKeeper generates new fee market module keeper @@ -54,12 +56,16 @@ func NewKeeper( panic(err) } + maxValue := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)) + maxInt := sdkmath.NewIntFromBigInt(maxValue) + return Keeper{ cdc: cdc, storeService: storeService, storeKey: storeKey, authority: authority, transientKey: transientKey, + maxGas: maxInt, } } diff --git a/x/feemarket/keeper/params.go b/x/feemarket/keeper/params.go index eab87a3945..6cd70faeb9 100644 --- a/x/feemarket/keeper/params.go +++ b/x/feemarket/keeper/params.go @@ -71,7 +71,11 @@ func (k Keeper) GetBaseFee(ctx sdk.Context) *big.Int { // SetBaseFee set's the base fee in the store func (k Keeper) SetBaseFee(ctx sdk.Context, baseFee *big.Int) { params := k.GetParams(ctx) - params.BaseFee = sdkmath.NewIntFromBigInt(baseFee) + if baseFee.BitLen() > sdkmath.MaxBitLen { + params.BaseFee = k.maxGas + } else { + params.BaseFee = sdkmath.NewIntFromBigInt(baseFee) + } err := k.SetParams(ctx, params) if err != nil { return From c0807a76862f724a7f4732a55efac716590e9a48 Mon Sep 17 00:00:00 2001 From: Brian Luk Date: Wed, 20 Mar 2024 18:29:16 -0700 Subject: [PATCH 2/2] use sdkmath.MaxBitLen --- x/feemarket/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/feemarket/keeper/keeper.go b/x/feemarket/keeper/keeper.go index 13898f4293..db2e211f67 100644 --- a/x/feemarket/keeper/keeper.go +++ b/x/feemarket/keeper/keeper.go @@ -56,7 +56,7 @@ func NewKeeper( panic(err) } - maxValue := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)) + maxValue := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(sdkmath.MaxBitLen), nil), big.NewInt(1)) maxInt := sdkmath.NewIntFromBigInt(maxValue) return Keeper{