From 3428291a615ab1546ac0c672e09e10f4d1506f32 Mon Sep 17 00:00:00 2001 From: Xuefeng Zhu Date: Mon, 14 Oct 2019 13:05:42 -0700 Subject: [PATCH] Merge PR #5160: Simply Dec and Int sign check * Simply Dec and Int sign check * address comment --- types/coin.go | 2 +- types/dec_coin.go | 6 +++--- x/distribution/keeper/delegation.go | 2 +- x/distribution/keeper/invariants.go | 4 ++-- x/distribution/keeper/validator.go | 2 +- x/mint/internal/types/minter.go | 2 +- x/mint/internal/types/params.go | 2 +- x/staking/keeper/slash.go | 4 ++-- x/staking/keeper/validator.go | 2 +- x/staking/simulation/operations/msgs.go | 6 +++--- x/staking/types/commission.go | 8 ++++---- x/staking/types/msg.go | 6 +++--- 12 files changed, 23 insertions(+), 23 deletions(-) diff --git a/types/coin.go b/types/coin.go index 2af2abc2d..c64ba0dca 100644 --- a/types/coin.go +++ b/types/coin.go @@ -56,7 +56,7 @@ func validate(denom string, amount Int) error { return err } - if amount.LT(ZeroInt()) { + if amount.IsNegative() { return fmt.Errorf("negative coin amount: %v", amount) } diff --git a/types/dec_coin.go b/types/dec_coin.go index a47fc2189..ca5046838 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -20,7 +20,7 @@ type DecCoin struct { func NewDecCoin(denom string, amount Int) DecCoin { mustValidateDenom(denom) - if amount.LT(ZeroInt()) { + if amount.IsNegative() { panic(fmt.Sprintf("negative coin amount: %v\n", amount)) } @@ -33,7 +33,7 @@ func NewDecCoin(denom string, amount Int) DecCoin { func NewDecCoinFromDec(denom string, amount Dec) DecCoin { mustValidateDenom(denom) - if amount.LT(ZeroDec()) { + if amount.IsNegative() { panic(fmt.Sprintf("negative decimal coin amount: %v\n", amount)) } @@ -44,7 +44,7 @@ func NewDecCoinFromDec(denom string, amount Dec) DecCoin { } func NewDecCoinFromCoin(coin Coin) DecCoin { - if coin.Amount.LT(ZeroInt()) { + if coin.Amount.IsNegative() { panic(fmt.Sprintf("negative decimal coin amount: %v\n", coin.Amount)) } if strings.ToLower(coin.Denom) != coin.Denom { diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 1e7a9aace..4044c4d19 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -36,7 +36,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx sdk.Context, val exported. } // sanity check - if stake.LT(sdk.ZeroDec()) { + if stake.IsNegative() { panic("stake should not be negative") } diff --git a/x/distribution/keeper/invariants.go b/x/distribution/keeper/invariants.go index 52b735ec2..4ae8dfb46 100644 --- a/x/distribution/keeper/invariants.go +++ b/x/distribution/keeper/invariants.go @@ -90,14 +90,14 @@ func CanWithdrawInvariant(k Keeper) sdk.Invariant { } remaining = k.GetValidatorOutstandingRewards(ctx, val.GetOperator()) - if len(remaining) > 0 && remaining[0].Amount.LT(sdk.ZeroDec()) { + if len(remaining) > 0 && remaining[0].Amount.IsNegative() { return true } return false }) - broken := len(remaining) > 0 && remaining[0].Amount.LT(sdk.ZeroDec()) + broken := len(remaining) > 0 && remaining[0].Amount.IsNegative() return sdk.FormatInvariant(types.ModuleName, "can withdraw", fmt.Sprintf("remaining coins: %v\n", remaining)), broken } diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index 02e29e836..4b0a4bbd6 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -88,7 +88,7 @@ func (k Keeper) decrementReferenceCount(ctx sdk.Context, valAddr sdk.ValAddress, } func (k Keeper) updateValidatorSlashFraction(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) { - if fraction.GT(sdk.OneDec()) || fraction.LT(sdk.ZeroDec()) { + if fraction.GT(sdk.OneDec()) || fraction.IsNegative() { panic(fmt.Sprintf("fraction must be >=0 and <=1, current fraction: %v", fraction)) } diff --git a/x/mint/internal/types/minter.go b/x/mint/internal/types/minter.go index e07716acd..3654ef728 100644 --- a/x/mint/internal/types/minter.go +++ b/x/mint/internal/types/minter.go @@ -39,7 +39,7 @@ func DefaultInitialMinter() Minter { // validate minter func ValidateMinter(minter Minter) error { - if minter.Inflation.LT(sdk.ZeroDec()) { + if minter.Inflation.IsNegative() { return fmt.Errorf("mint parameter Inflation should be positive, is %s", minter.Inflation.String()) } diff --git a/x/mint/internal/types/params.go b/x/mint/internal/types/params.go index 04ea58099..122f88bfe 100644 --- a/x/mint/internal/types/params.go +++ b/x/mint/internal/types/params.go @@ -59,7 +59,7 @@ func DefaultParams() Params { // validate params func ValidateParams(params Params) error { - if params.GoalBonded.LT(sdk.ZeroDec()) { + if params.GoalBonded.IsNegative() { return fmt.Errorf("mint parameter GoalBonded should be positive, is %s ", params.GoalBonded.String()) } if params.GoalBonded.GT(sdk.OneDec()) { diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 9ce1b79ce..8841d3e8e 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -24,7 +24,7 @@ import ( func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) { logger := k.Logger(ctx) - if slashFactor.LT(sdk.ZeroDec()) { + if slashFactor.IsNegative() { panic(fmt.Errorf("attempted to slash with a negative slash factor: %v", slashFactor)) } @@ -105,7 +105,7 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh tokensToBurn = sdk.MaxInt(tokensToBurn, sdk.ZeroInt()) // defensive. // we need to calculate the *effective* slash fraction for distribution - if validator.Tokens.GT(sdk.ZeroInt()) { + if validator.Tokens.IsPositive() { effectiveFraction := tokensToBurn.ToDec().QuoRoundUp(validator.Tokens.ToDec()) // possible if power has changed if effectiveFraction.GT(sdk.OneDec()) { diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index 0494acd2d..cd5e912ca 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -187,7 +187,7 @@ func (k Keeper) RemoveValidator(ctx sdk.Context, address sdk.ValAddress) { if validator.Tokens.IsPositive() { panic("attempting to remove a validator which still contains tokens") } - if validator.Tokens.GT(sdk.ZeroInt()) { + if validator.Tokens.IsPositive() { panic("validator being removed should never have positive tokens") } diff --git a/x/staking/simulation/operations/msgs.go b/x/staking/simulation/operations/msgs.go index caf6dd09c..d70ddd9ed 100644 --- a/x/staking/simulation/operations/msgs.go +++ b/x/staking/simulation/operations/msgs.go @@ -38,7 +38,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati acc := simulation.RandomAcc(r, accs) address := sdk.ValAddress(acc.Address) amount := m.GetAccount(ctx, acc.Address).GetCoins().AmountOf(denom) - if amount.GT(sdk.ZeroInt()) { + if amount.IsPositive() { amount = simulation.RandomAmount(r, amount) } @@ -116,7 +116,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper delegatorAcc := simulation.RandomAcc(r, accs) delegatorAddress := delegatorAcc.Address amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom) - if amount.GT(sdk.ZeroInt()) { + if amount.IsPositive() { amount = simulation.RandomAmount(r, amount) } if amount.Equal(sdk.ZeroInt()) { @@ -201,7 +201,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati delegatorAddress := delegatorAcc.Address // TODO amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom) - if amount.GT(sdk.ZeroInt()) { + if amount.IsPositive() { amount = simulation.RandomAmount(r, amount) } if amount.Equal(sdk.ZeroInt()) { diff --git a/x/staking/types/commission.go b/x/staking/types/commission.go index 9b7b327e1..83b346041 100644 --- a/x/staking/types/commission.go +++ b/x/staking/types/commission.go @@ -69,7 +69,7 @@ func (c Commission) String() string { // parameters. If validation fails, an SDK error is returned. func (c CommissionRates) Validate() sdk.Error { switch { - case c.MaxRate.LT(sdk.ZeroDec()): + case c.MaxRate.IsNegative(): // max rate cannot be negative return ErrCommissionNegative(DefaultCodespace) @@ -77,7 +77,7 @@ func (c CommissionRates) Validate() sdk.Error { // max rate cannot be greater than 1 return ErrCommissionHuge(DefaultCodespace) - case c.Rate.LT(sdk.ZeroDec()): + case c.Rate.IsNegative(): // rate cannot be negative return ErrCommissionNegative(DefaultCodespace) @@ -85,7 +85,7 @@ func (c CommissionRates) Validate() sdk.Error { // rate cannot be greater than the max rate return ErrCommissionGTMaxRate(DefaultCodespace) - case c.MaxChangeRate.LT(sdk.ZeroDec()): + case c.MaxChangeRate.IsNegative(): // change rate cannot be negative return ErrCommissionChangeRateNegative(DefaultCodespace) @@ -105,7 +105,7 @@ func (c Commission) ValidateNewRate(newRate sdk.Dec, blockTime time.Time) sdk.Er // new rate cannot be changed more than once within 24 hours return ErrCommissionUpdateTime(DefaultCodespace) - case newRate.LT(sdk.ZeroDec()): + case newRate.IsNegative(): // new rate cannot be negative return ErrCommissionNegative(DefaultCodespace) diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index b1d23514e..6be358786 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -170,7 +170,7 @@ func (msg MsgCreateValidator) ValidateBasic() sdk.Error { if err := msg.Commission.Validate(); err != nil { return err } - if !msg.MinSelfDelegation.GT(sdk.ZeroInt()) { + if !msg.MinSelfDelegation.IsPositive() { return ErrMinSelfDelegationInvalid(DefaultCodespace) } if msg.Value.Amount.LT(msg.MinSelfDelegation) { @@ -226,12 +226,12 @@ func (msg MsgEditValidator) ValidateBasic() sdk.Error { return sdk.NewError(DefaultCodespace, CodeInvalidInput, "transaction must include some information to modify") } - if msg.MinSelfDelegation != nil && !msg.MinSelfDelegation.GT(sdk.ZeroInt()) { + if msg.MinSelfDelegation != nil && !msg.MinSelfDelegation.IsPositive() { return ErrMinSelfDelegationInvalid(DefaultCodespace) } if msg.CommissionRate != nil { - if msg.CommissionRate.GT(sdk.OneDec()) || msg.CommissionRate.LT(sdk.ZeroDec()) { + if msg.CommissionRate.GT(sdk.OneDec()) || msg.CommissionRate.IsNegative() { return sdk.NewError(DefaultCodespace, CodeInvalidInput, "commission rate must be between 0 and 1, inclusive") } }