From 32213ed56c346dc599653eda778aa100a70fde26 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 11 May 2020 18:56:28 -0400 Subject: [PATCH] x/auction: types validation --- x/auction/types/auctions.go | 45 ++++++++++++++++++++++++++++++++---- x/auction/types/msg.go | 7 ++++++ x/incentive/types/genesis.go | 4 ++-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/x/auction/types/auctions.go b/x/auction/types/auctions.go index 2e06b86e..37e797f5 100644 --- a/x/auction/types/auctions.go +++ b/x/auction/types/auctions.go @@ -1,7 +1,9 @@ package types import ( + "errors" "fmt" + "strings" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -66,15 +68,39 @@ func (a BaseAuction) GetType() string { return "base" } // Validate verifies that the auction end time is before max end time func (a BaseAuction) Validate() error { - if a.EndTime.After(a.MaxEndTime) { - return fmt.Errorf("MaxEndTime < EndTime (%s < %s)", a.MaxEndTime, a.EndTime) + if a.ID == 0 { + return errors.New("auction id cannot be zero") + } + if strings.TrimSpace(a.Initiator) == "" { + return errors.New("auction initiator cannot be blank") } if !a.Lot.IsValid() { return fmt.Errorf("invalid lot: %s", a.Lot) } + if a.Bidder.Empty() { + return errors.New("auction bidder cannot be empty") + } + if len(a.Bidder) != sdk.AddrLen { + return fmt.Errorf("the expected bidder address length is %d, actual length is %d", sdk.AddrLen, len(a.Bidder)) + } + if len(a.Bidder.Bytes()) != sdk.AddrLen { + return errors.New("auction bidder cannot be empty") + } if !a.Bid.IsValid() { return fmt.Errorf("invalid bid: %s", a.Bid) } + if a.EndTime.IsZero() || a.MaxEndTime.IsZero() { + return errors.New("end time cannot be zero") + } + if a.EndTime.After(a.MaxEndTime) { + return fmt.Errorf("MaxEndTime < EndTime (%s < %s)", a.MaxEndTime, a.EndTime) + } + if a.HasReceivedBids && !a.Bid.IsPositive() { + return fmt.Errorf("cannot have a zero value bid when auction has a true HasReceivedBids flag value") + } + if !a.HasReceivedBids && a.Bid.IsPositive() { + return fmt.Errorf("cannot have a positive value bid when auction has a false HasReceivedBids flag value") + } return nil } @@ -154,6 +180,7 @@ func (a DebtAuction) GetModuleAccountCoins() sdk.Coins { // GetPhase returns the direction of a debt auction, which never changes. func (a DebtAuction) GetPhase() string { return "reverse" } +// Validate validates the DebtAuction fields values. func (a DebtAuction) Validate() error { if !a.CorrespondingDebt.IsValid() { return fmt.Errorf("invalid corresponding debt: %s", a.CorrespondingDebt) @@ -221,6 +248,7 @@ func (a CollateralAuction) GetPhase() string { return "forward" } +// Validate validates the CollateralAuction fields values. func (a CollateralAuction) Validate() error { if !a.CorrespondingDebt.IsValid() { return fmt.Errorf("invalid corresponding debt: %s", a.CorrespondingDebt) @@ -292,9 +320,16 @@ func (wa WeightedAddresses) Validate() error { if len(wa.Addresses) != len(wa.Weights) { return fmt.Errorf("number of addresses doesn't match number of weights, %d ≠ %d", len(wa.Addresses), len(wa.Weights)) } - for _, w := range wa.Weights { - if w.IsNegative() { - return fmt.Errorf("weights contain a negative amount: %s", w) + + for i := range wa.Addresses { + if wa.Addresses[i].Empty() { + return fmt.Errorf("address %d cannot be empty", i) + } + if len(wa.Addresses[i]) != sdk.AddrLen { + return fmt.Errorf("address %d has an invalid length: expectd %d, got %d", i, sdk.AddrLen, len(wa.Addresses[i])) + } + if wa.Weights[i].IsNegative() { + return fmt.Errorf("weight %d contains a negative amount: %s", i, wa.Weights[i]) } } return nil diff --git a/x/auction/types/msg.go b/x/auction/types/msg.go index 4614f512..ca4ff49e 100644 --- a/x/auction/types/msg.go +++ b/x/auction/types/msg.go @@ -1,6 +1,7 @@ package types import ( + "errors" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -34,9 +35,15 @@ func (msg MsgPlaceBid) Type() string { return "place_bid" } // ValidateBasic does a simple validation check that doesn't require access to state. func (msg MsgPlaceBid) ValidateBasic() error { + if msg.AuctionID == 0 { + return errors.New("auction id cannot be zero") + } if msg.Bidder.Empty() { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "bidder address cannot be empty") } + if len(msg.Bidder) != sdk.AddrLen { + return fmt.Errorf("the expected bidder address length is %d, actual length is %d", sdk.AddrLen, len(msg.Bidder)) + } if !msg.Amount.IsValid() { return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "bid amount %s", msg.Amount) } diff --git a/x/incentive/types/genesis.go b/x/incentive/types/genesis.go index d73d1b5e..f9514727 100644 --- a/x/incentive/types/genesis.go +++ b/x/incentive/types/genesis.go @@ -56,8 +56,8 @@ func (gs GenesisState) Validate() error { if err := gs.Params.Validate(); err != nil { return err } - if gs.PreviousBlockTime.Equal(time.Time{}) { - return fmt.Errorf("previous block time not set") + if gs.PreviousBlockTime.IsZero() { + return fmt.Errorf("previous block time not set or zero") } return nil }