Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: prevent int overflow #431

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions x/feemarket/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -54,12 +56,16 @@ func NewKeeper(
panic(err)
}

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{
cdc: cdc,
storeService: storeService,
storeKey: storeKey,
authority: authority,
transientKey: transientKey,
maxGas: maxInt,
}
}

Expand Down
6 changes: 5 additions & 1 deletion x/feemarket/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@
// 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

Check warning on line 75 in x/feemarket/keeper/params.go

View check run for this annotation

Codecov / codecov/patch

x/feemarket/keeper/params.go#L75

Added line #L75 was not covered by tests
yihuang marked this conversation as resolved.
Show resolved Hide resolved
} else {
params.BaseFee = sdkmath.NewIntFromBigInt(baseFee)
}
err := k.SetParams(ctx, params)
if err != nil {
return
Expand Down
Loading