Merge PR #5160: Simply Dec and Int sign check

* Simply Dec and Int sign check

* address comment
This commit is contained in:
Xuefeng Zhu 2019-10-14 13:05:42 -07:00 committed by Alexander Bezobchuk
parent 21f2896f8d
commit 3428291a61
12 changed files with 23 additions and 23 deletions

View File

@ -56,7 +56,7 @@ func validate(denom string, amount Int) error {
return err return err
} }
if amount.LT(ZeroInt()) { if amount.IsNegative() {
return fmt.Errorf("negative coin amount: %v", amount) return fmt.Errorf("negative coin amount: %v", amount)
} }

View File

@ -20,7 +20,7 @@ type DecCoin struct {
func NewDecCoin(denom string, amount Int) DecCoin { func NewDecCoin(denom string, amount Int) DecCoin {
mustValidateDenom(denom) mustValidateDenom(denom)
if amount.LT(ZeroInt()) { if amount.IsNegative() {
panic(fmt.Sprintf("negative coin amount: %v\n", amount)) 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 { func NewDecCoinFromDec(denom string, amount Dec) DecCoin {
mustValidateDenom(denom) mustValidateDenom(denom)
if amount.LT(ZeroDec()) { if amount.IsNegative() {
panic(fmt.Sprintf("negative decimal coin amount: %v\n", amount)) 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 { 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)) panic(fmt.Sprintf("negative decimal coin amount: %v\n", coin.Amount))
} }
if strings.ToLower(coin.Denom) != coin.Denom { if strings.ToLower(coin.Denom) != coin.Denom {

View File

@ -36,7 +36,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx sdk.Context, val exported.
} }
// sanity check // sanity check
if stake.LT(sdk.ZeroDec()) { if stake.IsNegative() {
panic("stake should not be negative") panic("stake should not be negative")
} }

View File

@ -90,14 +90,14 @@ func CanWithdrawInvariant(k Keeper) sdk.Invariant {
} }
remaining = k.GetValidatorOutstandingRewards(ctx, val.GetOperator()) 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 true
} }
return false 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", return sdk.FormatInvariant(types.ModuleName, "can withdraw",
fmt.Sprintf("remaining coins: %v\n", remaining)), broken fmt.Sprintf("remaining coins: %v\n", remaining)), broken
} }

View File

@ -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) { 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)) panic(fmt.Sprintf("fraction must be >=0 and <=1, current fraction: %v", fraction))
} }

View File

@ -39,7 +39,7 @@ func DefaultInitialMinter() Minter {
// validate minter // validate minter
func ValidateMinter(minter Minter) error { 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", return fmt.Errorf("mint parameter Inflation should be positive, is %s",
minter.Inflation.String()) minter.Inflation.String())
} }

View File

@ -59,7 +59,7 @@ func DefaultParams() Params {
// validate params // validate params
func ValidateParams(params Params) error { 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()) return fmt.Errorf("mint parameter GoalBonded should be positive, is %s ", params.GoalBonded.String())
} }
if params.GoalBonded.GT(sdk.OneDec()) { if params.GoalBonded.GT(sdk.OneDec()) {

View File

@ -24,7 +24,7 @@ import (
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) { func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) {
logger := k.Logger(ctx) 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)) 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. tokensToBurn = sdk.MaxInt(tokensToBurn, sdk.ZeroInt()) // defensive.
// we need to calculate the *effective* slash fraction for distribution // 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()) effectiveFraction := tokensToBurn.ToDec().QuoRoundUp(validator.Tokens.ToDec())
// possible if power has changed // possible if power has changed
if effectiveFraction.GT(sdk.OneDec()) { if effectiveFraction.GT(sdk.OneDec()) {

View File

@ -187,7 +187,7 @@ func (k Keeper) RemoveValidator(ctx sdk.Context, address sdk.ValAddress) {
if validator.Tokens.IsPositive() { if validator.Tokens.IsPositive() {
panic("attempting to remove a validator which still contains tokens") 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") panic("validator being removed should never have positive tokens")
} }

View File

@ -38,7 +38,7 @@ func SimulateMsgCreateValidator(m auth.AccountKeeper, k staking.Keeper) simulati
acc := simulation.RandomAcc(r, accs) acc := simulation.RandomAcc(r, accs)
address := sdk.ValAddress(acc.Address) address := sdk.ValAddress(acc.Address)
amount := m.GetAccount(ctx, acc.Address).GetCoins().AmountOf(denom) amount := m.GetAccount(ctx, acc.Address).GetCoins().AmountOf(denom)
if amount.GT(sdk.ZeroInt()) { if amount.IsPositive() {
amount = simulation.RandomAmount(r, amount) amount = simulation.RandomAmount(r, amount)
} }
@ -116,7 +116,7 @@ func SimulateMsgDelegate(m auth.AccountKeeper, k staking.Keeper) simulation.Oper
delegatorAcc := simulation.RandomAcc(r, accs) delegatorAcc := simulation.RandomAcc(r, accs)
delegatorAddress := delegatorAcc.Address delegatorAddress := delegatorAcc.Address
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom) amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
if amount.GT(sdk.ZeroInt()) { if amount.IsPositive() {
amount = simulation.RandomAmount(r, amount) amount = simulation.RandomAmount(r, amount)
} }
if amount.Equal(sdk.ZeroInt()) { if amount.Equal(sdk.ZeroInt()) {
@ -201,7 +201,7 @@ func SimulateMsgBeginRedelegate(m auth.AccountKeeper, k staking.Keeper) simulati
delegatorAddress := delegatorAcc.Address delegatorAddress := delegatorAcc.Address
// TODO // TODO
amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom) amount := m.GetAccount(ctx, delegatorAddress).GetCoins().AmountOf(denom)
if amount.GT(sdk.ZeroInt()) { if amount.IsPositive() {
amount = simulation.RandomAmount(r, amount) amount = simulation.RandomAmount(r, amount)
} }
if amount.Equal(sdk.ZeroInt()) { if amount.Equal(sdk.ZeroInt()) {

View File

@ -69,7 +69,7 @@ func (c Commission) String() string {
// parameters. If validation fails, an SDK error is returned. // parameters. If validation fails, an SDK error is returned.
func (c CommissionRates) Validate() sdk.Error { func (c CommissionRates) Validate() sdk.Error {
switch { switch {
case c.MaxRate.LT(sdk.ZeroDec()): case c.MaxRate.IsNegative():
// max rate cannot be negative // max rate cannot be negative
return ErrCommissionNegative(DefaultCodespace) return ErrCommissionNegative(DefaultCodespace)
@ -77,7 +77,7 @@ func (c CommissionRates) Validate() sdk.Error {
// max rate cannot be greater than 1 // max rate cannot be greater than 1
return ErrCommissionHuge(DefaultCodespace) return ErrCommissionHuge(DefaultCodespace)
case c.Rate.LT(sdk.ZeroDec()): case c.Rate.IsNegative():
// rate cannot be negative // rate cannot be negative
return ErrCommissionNegative(DefaultCodespace) return ErrCommissionNegative(DefaultCodespace)
@ -85,7 +85,7 @@ func (c CommissionRates) Validate() sdk.Error {
// rate cannot be greater than the max rate // rate cannot be greater than the max rate
return ErrCommissionGTMaxRate(DefaultCodespace) return ErrCommissionGTMaxRate(DefaultCodespace)
case c.MaxChangeRate.LT(sdk.ZeroDec()): case c.MaxChangeRate.IsNegative():
// change rate cannot be negative // change rate cannot be negative
return ErrCommissionChangeRateNegative(DefaultCodespace) 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 // new rate cannot be changed more than once within 24 hours
return ErrCommissionUpdateTime(DefaultCodespace) return ErrCommissionUpdateTime(DefaultCodespace)
case newRate.LT(sdk.ZeroDec()): case newRate.IsNegative():
// new rate cannot be negative // new rate cannot be negative
return ErrCommissionNegative(DefaultCodespace) return ErrCommissionNegative(DefaultCodespace)

View File

@ -170,7 +170,7 @@ func (msg MsgCreateValidator) ValidateBasic() sdk.Error {
if err := msg.Commission.Validate(); err != nil { if err := msg.Commission.Validate(); err != nil {
return err return err
} }
if !msg.MinSelfDelegation.GT(sdk.ZeroInt()) { if !msg.MinSelfDelegation.IsPositive() {
return ErrMinSelfDelegationInvalid(DefaultCodespace) return ErrMinSelfDelegationInvalid(DefaultCodespace)
} }
if msg.Value.Amount.LT(msg.MinSelfDelegation) { 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") 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) return ErrMinSelfDelegationInvalid(DefaultCodespace)
} }
if msg.CommissionRate != nil { 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") return sdk.NewError(DefaultCodespace, CodeInvalidInput, "commission rate must be between 0 and 1, inclusive")
} }
} }