Skip to content

Commit

Permalink
fix(x/mint/simulation): use proper value of maxInt64
Browse files Browse the repository at this point in the history
This change uses the proper value of maxInt64, inferred
from math.MaxInt64 and typed as an int64 then passed into
math/rand.Rand.Int63n(maxInt64) instead of the non-standard
prior fidgetting that set maxInt64 as an int(^uint(0)>>1)
which sadly will overflow on 32-bit systems because the size
of int on 32-bit systems is 32 bits, on 64-bit systems is 64 bits,
and moreover the prior code used int64(Rand.Intn(maxInt64)) which
is unnecessary.
  • Loading branch information
odeke-em committed Mar 26, 2024
1 parent 02bd08d commit 39777b7
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions x/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package simulation
import (
"encoding/json"
"fmt"
"math"
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -23,7 +24,7 @@ const (
mintingRewardsDistributionStartEpochKey = "minting_rewards_distribution_start_epoch"

epochIdentifier = "day"
maxInt64 = int(^uint(0) >> 1)
maxInt64 = int64(math.MaxInt64)
)

// genDistributionProportions generates randomized DistributionProportions.
Expand All @@ -44,19 +45,19 @@ func genDistributionProportions(r *rand.Rand) types.DistributionProportions {
}

func genEpochProvisions(r *rand.Rand) sdk.Dec {
return sdk.NewDec(int64(r.Intn(maxInt64)))
return sdk.NewDec(r.Int63n(maxInt64))
}

func genReductionFactor(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(int64(r.Intn(10)), 1)
return sdk.NewDecWithPrec(r.Int63n(10), 1)
}

func genReductionPeriodInEpochs(r *rand.Rand) int64 {
return int64(r.Intn(maxInt64))
return r.Int63n(maxInt64)
}

func genMintintRewardsDistributionStartEpoch(r *rand.Rand) int64 {
return int64(r.Intn(maxInt64))
return r.Int63n(maxInt64)
}

// RandomizedGenState generates a random GenesisState for mint.
Expand Down

0 comments on commit 39777b7

Please sign in to comment.