chore: use sdkmath in misc packages (#14606)

Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
Aleksandr Bezobchuk
2023-01-16 12:48:32 +00:00
committed by GitHub
co-authored by atheeshp Marko
parent ae1c29b8f8
commit f2b6013cee
12 changed files with 67 additions and 64 deletions
+11 -10
View File
@@ -7,7 +7,8 @@ import (
"time"
"unsafe"
"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -41,19 +42,19 @@ func RandStringOfLength(r *rand.Rand, n int) string {
}
// RandPositiveInt get a rand positive math.Int
func RandPositiveInt(r *rand.Rand, max math.Int) (math.Int, error) {
if !max.GTE(math.OneInt()) {
return math.Int{}, errors.New("max too small")
func RandPositiveInt(r *rand.Rand, max sdkmath.Int) (sdkmath.Int, error) {
if !max.GTE(sdkmath.OneInt()) {
return sdkmath.Int{}, errors.New("max too small")
}
max = max.Sub(math.OneInt())
max = max.Sub(sdkmath.OneInt())
return sdk.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(math.OneInt()), nil
return sdkmath.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(sdkmath.OneInt()), nil
}
// RandomAmount generates a random amount
// Note: The range of RandomAmount includes max, and is, in fact, biased to return max as well as 0.
func RandomAmount(r *rand.Rand, max math.Int) math.Int {
func RandomAmount(r *rand.Rand, max sdkmath.Int) sdkmath.Int {
randInt := big.NewInt(0)
switch r.Intn(10) {
@@ -65,12 +66,12 @@ func RandomAmount(r *rand.Rand, max math.Int) math.Int {
randInt = big.NewInt(0).Rand(r, max.BigInt()) // up to max - 1
}
return sdk.NewIntFromBigInt(randInt)
return sdkmath.NewIntFromBigInt(randInt)
}
// RandomDecAmount generates a random decimal amount
// Note: The range of RandomDecAmount includes max, and is, in fact, biased to return max as well as 0.
func RandomDecAmount(r *rand.Rand, max sdk.Dec) math.LegacyDec {
func RandomDecAmount(r *rand.Rand, max sdkmath.LegacyDec) sdkmath.LegacyDec {
randInt := big.NewInt(0)
switch r.Intn(10) {
@@ -82,7 +83,7 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) math.LegacyDec {
randInt = big.NewInt(0).Rand(r, max.BigInt())
}
return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
return sdkmath.LegacyNewDecFromBigIntWithPrec(randInt, sdkmath.LegacyPrecision)
}
// RandTimestamp generates a random timestamp
+8 -4
View File
@@ -1,5 +1,9 @@
package types
import (
sdkmath "cosmossdk.io/math"
)
// Delay, in blocks, between when validator updates are returned to the
// consensus-engine and when they are applied. For example, if
// ValidatorUpdateDelay is set to X, and if a validator set update is
@@ -17,15 +21,15 @@ var (
DefaultBondDenom = "stake"
// DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power
DefaultPowerReduction = NewIntFromUint64(1000000)
DefaultPowerReduction = sdkmath.NewIntFromUint64(1000000)
)
// TokensToConsensusPower - convert input tokens to potential consensus-engine power
func TokensToConsensusPower(tokens Int, powerReduction Int) int64 {
func TokensToConsensusPower(tokens sdkmath.Int, powerReduction sdkmath.Int) int64 {
return (tokens.Quo(powerReduction)).Int64()
}
// TokensFromConsensusPower - convert input power to tokens
func TokensFromConsensusPower(power int64, powerReduction Int) Int {
return NewInt(power).Mul(powerReduction)
func TokensFromConsensusPower(power int64, powerReduction sdkmath.Int) sdkmath.Int {
return sdkmath.NewInt(power).Mul(powerReduction)
}