From 37be34b4d6a7f6ea3ec1e7883c130df5cf33c120 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Tue, 2 Feb 2021 14:42:51 -0700 Subject: [PATCH] fix: replace is zero time check (#787) --- x/auction/types/auctions.go | 2 +- x/bep3/types/params.go | 2 +- x/cdp/genesis.go | 2 +- x/cdp/legacy/v0_11/types.go | 5 +++-- x/cdp/legacy/v0_13/types.go | 2 +- x/cdp/types/cdp.go | 2 +- x/hard/alias.go | 1 - x/hard/legacy/v0_11/types.go | 4 ++-- x/hard/types/genesis.go | 6 ------ x/incentive/legacy/v0_11/types.go | 10 +++++----- x/incentive/legacy/v0_9/types.go | 10 +++++----- x/incentive/types/params.go | 8 ++++---- x/kavadist/types/params.go | 4 ++-- x/pricefeed/legacy/v0_9/types.go | 2 +- x/pricefeed/types/market.go | 2 +- x/pricefeed/types/msgs.go | 2 +- x/validator-vesting/types/genesis.go | 2 +- 17 files changed, 30 insertions(+), 36 deletions(-) diff --git a/x/auction/types/auctions.go b/x/auction/types/auctions.go index 860a4a986e..0245031789 100644 --- a/x/auction/types/auctions.go +++ b/x/auction/types/auctions.go @@ -90,7 +90,7 @@ func (a BaseAuction) Validate() error { if !a.Bid.IsValid() { return fmt.Errorf("invalid bid: %s", a.Bid) } - if a.EndTime.IsZero() || a.MaxEndTime.IsZero() { + if a.EndTime.Unix() <= 0 || a.MaxEndTime.Unix() <= 0 { return errors.New("end time cannot be zero") } if a.EndTime.After(a.MaxEndTime) { diff --git a/x/bep3/types/params.go b/x/bep3/types/params.go index b6e0f972ad..48287fe65c 100644 --- a/x/bep3/types/params.go +++ b/x/bep3/types/params.go @@ -23,7 +23,7 @@ var ( DefaultMaxAmount sdk.Int = sdk.NewInt(1000000000000) // 10,000 BNB DefaultMinBlockLock uint64 = 220 DefaultMaxBlockLock uint64 = 270 - DefaultPreviousBlockTime = tmtime.Canonical(time.Unix(0, 0)) + DefaultPreviousBlockTime = tmtime.Canonical(time.Unix(1, 0)) ) // Params governance parameters for bep3 module diff --git a/x/cdp/genesis.go b/x/cdp/genesis.go index 5da2089cf2..31d010339a 100644 --- a/x/cdp/genesis.go +++ b/x/cdp/genesis.go @@ -55,7 +55,7 @@ func InitGenesis(ctx sdk.Context, k Keeper, pk types.PricefeedKeeper, sk types.S for _, gat := range gs.PreviousAccumulationTimes { k.SetInterestFactor(ctx, gat.CollateralType, gat.InterestFactor) - if !gat.PreviousAccumulationTime.IsZero() { + if gat.PreviousAccumulationTime.Unix() > 0 { k.SetPreviousAccrualTime(ctx, gat.CollateralType, gat.PreviousAccumulationTime) } } diff --git a/x/cdp/legacy/v0_11/types.go b/x/cdp/legacy/v0_11/types.go index 787e02f405..acd9d5c3dd 100644 --- a/x/cdp/legacy/v0_11/types.go +++ b/x/cdp/legacy/v0_11/types.go @@ -109,6 +109,7 @@ func NewCDPWithFees(id uint64, owner sdk.AccAddress, collateral sdk.Coin, collat } } +// Validate performs stateless validation of cdp object state func (cdp CDP) Validate() error { if cdp.ID == 0 { return errors.New("cdp id cannot be 0") @@ -125,7 +126,7 @@ func (cdp CDP) Validate() error { if !cdp.AccumulatedFees.IsValid() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "accumulated fees %s", cdp.AccumulatedFees) } - if cdp.FeesUpdated.IsZero() { + if cdp.FeesUpdated.Unix() <= 0 { return errors.New("cdp updated fee time cannot be zero") } if strings.TrimSpace(cdp.Type) == "" { @@ -631,7 +632,7 @@ func (gs GenesisState) Validate() error { return err } - if gs.PreviousDistributionTime.IsZero() { + if gs.PreviousDistributionTime.Unix() <= 0 { return fmt.Errorf("previous distribution time not set") } diff --git a/x/cdp/legacy/v0_13/types.go b/x/cdp/legacy/v0_13/types.go index b9034bc1ce..3586676915 100644 --- a/x/cdp/legacy/v0_13/types.go +++ b/x/cdp/legacy/v0_13/types.go @@ -586,7 +586,7 @@ func (cdp CDP) Validate() error { if !cdp.AccumulatedFees.IsValid() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "accumulated fees %s", cdp.AccumulatedFees) } - if cdp.FeesUpdated.IsZero() { + if cdp.FeesUpdated.Unix() <= 0 { return errors.New("cdp updated fee time cannot be zero") } if strings.TrimSpace(cdp.Type) == "" { diff --git a/x/cdp/types/cdp.go b/x/cdp/types/cdp.go index e7f8ebfbf1..20b9e5b82b 100644 --- a/x/cdp/types/cdp.go +++ b/x/cdp/types/cdp.go @@ -90,7 +90,7 @@ func (cdp CDP) Validate() error { if !cdp.AccumulatedFees.IsValid() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "accumulated fees %s", cdp.AccumulatedFees) } - if cdp.FeesUpdated.IsZero() { + if cdp.FeesUpdated.Unix() <= 0 { return errors.New("cdp updated fee time cannot be zero") } if strings.TrimSpace(cdp.Type) == "" { diff --git a/x/hard/alias.go b/x/hard/alias.go index b5431d883a..7e951cef2f 100644 --- a/x/hard/alias.go +++ b/x/hard/alias.go @@ -94,7 +94,6 @@ var ( DefaultCheckLtvIndexCount = types.DefaultCheckLtvIndexCount DefaultDeposits = types.DefaultDeposits DefaultMoneyMarkets = types.DefaultMoneyMarkets - DefaultPreviousBlockTime = types.DefaultPreviousBlockTime DefaultTotalBorrowed = types.DefaultTotalBorrowed DefaultTotalReserves = types.DefaultTotalReserves DefaultTotalSupplied = types.DefaultTotalSupplied diff --git a/x/hard/legacy/v0_11/types.go b/x/hard/legacy/v0_11/types.go index e1d989c6dc..5cc937efab 100644 --- a/x/hard/legacy/v0_11/types.go +++ b/x/hard/legacy/v0_11/types.go @@ -142,10 +142,10 @@ func (ds DistributionSchedule) Validate() error { if ds.RewardsPerSecond.Denom != "hard" { return fmt.Errorf("reward denom should be hard, is %s", ds.RewardsPerSecond.Denom) } - if ds.Start.IsZero() { + if ds.Start.Unix() <= 0 { return errors.New("reward period start time cannot be 0") } - if ds.End.IsZero() { + if ds.End.Unix() <= 0 { return errors.New("reward period end time cannot be 0") } if ds.Start.After(ds.End) { diff --git a/x/hard/types/genesis.go b/x/hard/types/genesis.go index fbf67acf18..9057362ce5 100644 --- a/x/hard/types/genesis.go +++ b/x/hard/types/genesis.go @@ -6,12 +6,6 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" - tmtime "github.com/tendermint/tendermint/types/time" -) - -// GenesisState default values -var ( - DefaultPreviousBlockTime = tmtime.Canonical(time.Unix(0, 0)) ) // GenesisState is the state that must be provided at genesis. diff --git a/x/incentive/legacy/v0_11/types.go b/x/incentive/legacy/v0_11/types.go index 22d6451476..d296a90930 100644 --- a/x/incentive/legacy/v0_11/types.go +++ b/x/incentive/legacy/v0_11/types.go @@ -84,7 +84,7 @@ func (gs GenesisState) Validate() error { if err := gs.Params.Validate(); err != nil { return err } - if gs.PreviousBlockTime.IsZero() { + if gs.PreviousBlockTime.Unix() <= 0 { return errors.New("previous block time cannot be 0") } if err := gs.RewardPeriods.Validate(); err != nil { @@ -336,10 +336,10 @@ func NewRewardPeriod(collateralType string, start time.Time, end time.Time, rewa // Validate performs a basic check of a RewardPeriod fields. func (rp RewardPeriod) Validate() error { - if rp.Start.IsZero() { + if rp.Start.Unix() <= 0 { return errors.New("reward period start time cannot be 0") } - if rp.End.IsZero() { + if rp.End.Unix() <= 0 { return errors.New("reward period end time cannot be 0") } if rp.Start.After(rp.End) { @@ -348,7 +348,7 @@ func (rp RewardPeriod) Validate() error { if !rp.Reward.IsValid() { return fmt.Errorf("invalid reward amount: %s", rp.Reward) } - if rp.ClaimEnd.IsZero() { + if rp.ClaimEnd.Unix() <= 0 { return errors.New("reward period claim end time cannot be 0") } if err := rp.ClaimMultipliers.Validate(); err != nil { @@ -404,7 +404,7 @@ func (cp ClaimPeriod) Validate() error { if cp.ID == 0 { return errors.New("claim period id cannot be 0") } - if cp.End.IsZero() { + if cp.End.Unix() <= 0 { return errors.New("claim period end time cannot be 0") } if err := cp.ClaimMultipliers.Validate(); err != nil { diff --git a/x/incentive/legacy/v0_9/types.go b/x/incentive/legacy/v0_9/types.go index c8e72afd42..b9413ebe97 100644 --- a/x/incentive/legacy/v0_9/types.go +++ b/x/incentive/legacy/v0_9/types.go @@ -77,7 +77,7 @@ func (gs GenesisState) Validate() error { if err := gs.Params.Validate(); err != nil { return err } - if gs.PreviousBlockTime.IsZero() { + if gs.PreviousBlockTime.Unix() <= 0 { return errors.New("previous block time cannot be 0") } if err := gs.RewardPeriods.Validate(); err != nil { @@ -259,10 +259,10 @@ func NewRewardPeriod(denom string, start time.Time, end time.Time, reward sdk.Co // Validate performs a basic check of a RewardPeriod fields. func (rp RewardPeriod) Validate() error { - if rp.Start.IsZero() { + if rp.Start.Unix() <= 0 { return errors.New("reward period start time cannot be 0") } - if rp.End.IsZero() { + if rp.End.Unix() <= 0 { return errors.New("reward period end time cannot be 0") } if rp.Start.After(rp.End) { @@ -271,7 +271,7 @@ func (rp RewardPeriod) Validate() error { if !rp.Reward.IsValid() { return fmt.Errorf("invalid reward amount: %s", rp.Reward) } - if rp.ClaimEnd.IsZero() { + if rp.ClaimEnd.Unix() <= 0 { return errors.New("reward period claim end time cannot be 0") } if rp.ClaimTimeLock == 0 { @@ -324,7 +324,7 @@ func (cp ClaimPeriod) Validate() error { if cp.ID == 0 { return errors.New("claim period id cannot be 0") } - if cp.End.IsZero() { + if cp.End.Unix() <= 0 { return errors.New("claim period end time cannot be 0") } if cp.TimeLock == 0 { diff --git a/x/incentive/types/params.go b/x/incentive/types/params.go index bcee1b8a5c..e63bf307cd 100644 --- a/x/incentive/types/params.go +++ b/x/incentive/types/params.go @@ -36,7 +36,7 @@ var ( DefaultMultipliers = Multipliers{} DefaultClaims = USDXMintingClaims{} DefaultGenesisAccumulationTimes = GenesisAccumulationTimes{} - DefaultClaimEnd = tmtime.Canonical(time.Unix(0, 0)) + DefaultClaimEnd = tmtime.Canonical(time.Unix(1, 0)) GovDenom = cdptypes.DefaultGovDenom PrincipalDenom = "usdx" IncentiveMacc = kavadistTypes.ModuleName @@ -154,7 +154,7 @@ func validateClaimEndParam(i interface{}) error { if !ok { return fmt.Errorf("invalid parameter type: %T", i) } - if endTime.IsZero() { + if endTime.Unix() <= 0 { return fmt.Errorf("end time should not be zero") } return nil @@ -193,10 +193,10 @@ func NewRewardPeriod(active bool, collateralType string, start time.Time, end ti // Validate performs a basic check of a RewardPeriod fields. func (rp RewardPeriod) Validate() error { - if rp.Start.IsZero() { + if rp.Start.Unix() <= 0 { return errors.New("reward period start time cannot be 0") } - if rp.End.IsZero() { + if rp.End.Unix() <= 0 { return errors.New("reward period end time cannot be 0") } if rp.Start.After(rp.End) { diff --git a/x/kavadist/types/params.go b/x/kavadist/types/params.go index 8fa015aa96..47b31e99a2 100644 --- a/x/kavadist/types/params.go +++ b/x/kavadist/types/params.go @@ -18,7 +18,7 @@ var ( KeyPeriods = []byte("Periods") DefaultActive = false DefaultPeriods = Periods{} - DefaultPreviousBlockTime = tmtime.Canonical(time.Unix(0, 0)) + DefaultPreviousBlockTime = tmtime.Canonical(time.Unix(1, 0)) GovDenom = cdptypes.DefaultGovDenom ) @@ -132,7 +132,7 @@ func validatePeriodsParams(i interface{}) error { } prevEnd = pr.End - if pr.Start.IsZero() || pr.End.IsZero() { + if pr.Start.Unix() <= 0 || pr.End.Unix() <= 0 { return fmt.Errorf("start or end time cannot be zero: %s", pr) } diff --git a/x/pricefeed/legacy/v0_9/types.go b/x/pricefeed/legacy/v0_9/types.go index 21d3e3ea71..acd8bd1c04 100644 --- a/x/pricefeed/legacy/v0_9/types.go +++ b/x/pricefeed/legacy/v0_9/types.go @@ -146,7 +146,7 @@ func (pp PostedPrice) Validate() error { if pp.Price.IsNegative() { return fmt.Errorf("posted price cannot be negative %s", pp.Price) } - if pp.Expiry.IsZero() { + if pp.Expiry.Unix() <= 0 { return errors.New("expiry time cannot be zero") } return nil diff --git a/x/pricefeed/types/market.go b/x/pricefeed/types/market.go index 413eff8906..f1753889d4 100644 --- a/x/pricefeed/types/market.go +++ b/x/pricefeed/types/market.go @@ -135,7 +135,7 @@ func (pp PostedPrice) Validate() error { if pp.Price.IsNegative() { return fmt.Errorf("posted price cannot be negative %s", pp.Price) } - if pp.Expiry.IsZero() { + if pp.Expiry.Unix() <= 0 { return errors.New("expiry time cannot be zero") } return nil diff --git a/x/pricefeed/types/msgs.go b/x/pricefeed/types/msgs.go index 408551b8f1..960cce60f2 100644 --- a/x/pricefeed/types/msgs.go +++ b/x/pricefeed/types/msgs.go @@ -72,7 +72,7 @@ func (msg MsgPostPrice) ValidateBasic() error { if msg.Price.IsNegative() { return fmt.Errorf("price cannot be negative: %s", msg.Price.String()) } - if msg.Expiry.IsZero() { + if msg.Expiry.Unix() <= 0 { return errors.New("must set an expiration time") } return nil diff --git a/x/validator-vesting/types/genesis.go b/x/validator-vesting/types/genesis.go index 8a4daf8f76..9f21be9552 100644 --- a/x/validator-vesting/types/genesis.go +++ b/x/validator-vesting/types/genesis.go @@ -39,7 +39,7 @@ func (data GenesisState) IsEmpty() bool { // ValidateGenesis returns nil because accounts are validated by auth func ValidateGenesis(data GenesisState) error { - if data.PreviousBlockTime.IsZero() { + if data.PreviousBlockTime.Unix() <= 0 { return errors.New("previous block time cannot be zero") } return nil