replace some errors with panics

This commit is contained in:
rhuairahrighairigh 2020-05-11 14:55:32 +01:00
parent 976f8f632d
commit 8899a7ff04
4 changed files with 85 additions and 113 deletions

View File

@ -1,109 +1,105 @@
package auction
// DO NOT EDIT - generated by aliasgen tool (github.com/rhuairahrighairidh/aliasgen)
import (
"github.com/kava-labs/kava/x/auction/keeper"
"github.com/kava-labs/kava/x/auction/types"
)
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/kava-labs/kava/x/auction/keeper
// ALIASGEN: github.com/kava-labs/kava/x/auction/types
const (
EventTypeAuctionStart = types.EventTypeAuctionStart
EventTypeAuctionBid = types.EventTypeAuctionBid
EventTypeAuctionClose = types.EventTypeAuctionClose
AttributeValueCategory = types.AttributeValueCategory
AttributeKeyAuctionID = types.AttributeKeyAuctionID
AttributeKeyAuctionType = types.AttributeKeyAuctionType
AttributeKeyBid = types.AttributeKeyBid
AttributeKeyBidder = types.AttributeKeyBidder
AttributeKeyBidDenom = types.AttributeKeyBidDenom
AttributeKeyLotDenom = types.AttributeKeyLotDenom
AttributeKeyBidAmount = types.AttributeKeyBidAmount
AttributeKeyLotAmount = types.AttributeKeyLotAmount
AttributeKeyCloseBlock = types.AttributeKeyCloseBlock
AttributeKeyEndTime = types.AttributeKeyEndTime
DefaultNextAuctionID = types.DefaultNextAuctionID
ModuleName = types.ModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
DefaultParamspace = types.DefaultParamspace
QuerierRoute = types.QuerierRoute
DefaultMaxAuctionDuration = types.DefaultMaxAuctionDuration
AttributeKeyLot = types.AttributeKeyLot
AttributeKeyMaxBid = types.AttributeKeyMaxBid
AttributeValueCategory = types.AttributeValueCategory
DefaultBidDuration = types.DefaultBidDuration
DefaultMaxAuctionDuration = types.DefaultMaxAuctionDuration
DefaultNextAuctionID = types.DefaultNextAuctionID
DefaultParamspace = types.DefaultParamspace
EventTypeAuctionBid = types.EventTypeAuctionBid
EventTypeAuctionClose = types.EventTypeAuctionClose
EventTypeAuctionStart = types.EventTypeAuctionStart
ModuleName = types.ModuleName
QuerierRoute = types.QuerierRoute
QueryGetAuction = types.QueryGetAuction
QueryGetAuctions = types.QueryGetAuctions
QueryGetParams = types.QueryGetParams
RouterKey = types.RouterKey
StoreKey = types.StoreKey
)
var (
// functions aliases
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
RegisterInvariants = keeper.RegisterInvariants
NewSurplusAuction = types.NewSurplusAuction
NewDebtAuction = types.NewDebtAuction
NewCollateralAuction = types.NewCollateralAuction
NewWeightedAddresses = types.NewWeightedAddresses
RegisterCodec = types.RegisterCodec
ErrInvalidInitialAuctionID = types.ErrInvalidInitialAuctionID
ErrInvalidModulePermissions = types.ErrInvalidModulePermissions
ErrUnrecognizedAuctionType = types.ErrUnrecognizedAuctionType
ErrAuctionNotFound = types.ErrAuctionNotFound
ErrAuctionHasNotExpired = types.ErrAuctionHasNotExpired
ErrAuctionHasExpired = types.ErrAuctionHasExpired
ErrInvalidBidDenom = types.ErrInvalidBidDenom
ErrInvalidLotDenom = types.ErrInvalidLotDenom
ErrBidTooSmall = types.ErrBidTooSmall
ErrBidTooLarge = types.ErrBidTooLarge
ErrLotTooSmall = types.ErrLotTooSmall
ErrLotTooLarge = types.ErrLotTooLarge
ErrCollateralAuctionIsInReversePhase = types.ErrCollateralAuctionIsInReversePhase
ErrCollateralAuctionIsInForwardPhase = types.ErrCollateralAuctionIsInForwardPhase
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
GetAuctionKey = types.GetAuctionKey
GetAuctionByTimeKey = types.GetAuctionByTimeKey
Uint64ToBytes = types.Uint64ToBytes
Uint64FromBytes = types.Uint64FromBytes
NewMsgPlaceBid = types.NewMsgPlaceBid
NewParams = types.NewParams
DefaultParams = types.DefaultParams
ParamKeyTable = types.ParamKeyTable
NewQueryAllAuctionParams = types.NewQueryAllAuctionParams
NewAuctionWithPhase = types.NewAuctionWithPhase
// function aliases
ModuleAccountInvariants = keeper.ModuleAccountInvariants
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
RegisterInvariants = keeper.RegisterInvariants
ValidAuctionInvariant = keeper.ValidAuctionInvariant
ValidIndexInvariant = keeper.ValidIndexInvariant
DefaultGenesisState = types.DefaultGenesisState
DefaultParams = types.DefaultParams
GetAuctionByTimeKey = types.GetAuctionByTimeKey
GetAuctionKey = types.GetAuctionKey
NewAuctionWithPhase = types.NewAuctionWithPhase
NewCollateralAuction = types.NewCollateralAuction
NewDebtAuction = types.NewDebtAuction
NewGenesisState = types.NewGenesisState
NewMsgPlaceBid = types.NewMsgPlaceBid
NewParams = types.NewParams
NewQueryAllAuctionParams = types.NewQueryAllAuctionParams
NewSurplusAuction = types.NewSurplusAuction
NewWeightedAddresses = types.NewWeightedAddresses
ParamKeyTable = types.ParamKeyTable
RegisterCodec = types.RegisterCodec
Uint64FromBytes = types.Uint64FromBytes
Uint64ToBytes = types.Uint64ToBytes
// variable aliases
DistantFuture = types.DistantFuture
ModuleCdc = types.ModuleCdc
AuctionKeyPrefix = types.AuctionKeyPrefix
AuctionByTimeKeyPrefix = types.AuctionByTimeKeyPrefix
NextAuctionIDKey = types.NextAuctionIDKey
DefaultIncrement = types.DefaultIncrement
KeyBidDuration = types.KeyBidDuration
KeyMaxAuctionDuration = types.KeyMaxAuctionDuration
KeyIncrementSurplus = types.KeyIncrementSurplus
KeyIncrementDebt = types.KeyIncrementDebt
KeyIncrementCollateral = types.KeyIncrementCollateral
AuctionByTimeKeyPrefix = types.AuctionByTimeKeyPrefix
AuctionKeyPrefix = types.AuctionKeyPrefix
DefaultIncrement = types.DefaultIncrement
DistantFuture = types.DistantFuture
ErrAuctionHasExpired = types.ErrAuctionHasExpired
ErrAuctionHasNotExpired = types.ErrAuctionHasNotExpired
ErrAuctionNotFound = types.ErrAuctionNotFound
ErrBidTooLarge = types.ErrBidTooLarge
ErrBidTooSmall = types.ErrBidTooSmall
ErrInvalidBidDenom = types.ErrInvalidBidDenom
ErrInvalidInitialAuctionID = types.ErrInvalidInitialAuctionID
ErrInvalidLotDenom = types.ErrInvalidLotDenom
ErrLotTooLarge = types.ErrLotTooLarge
ErrLotTooSmall = types.ErrLotTooSmall
ErrUnrecognizedAuctionType = types.ErrUnrecognizedAuctionType
KeyBidDuration = types.KeyBidDuration
KeyIncrementCollateral = types.KeyIncrementCollateral
KeyIncrementDebt = types.KeyIncrementDebt
KeyIncrementSurplus = types.KeyIncrementSurplus
KeyMaxAuctionDuration = types.KeyMaxAuctionDuration
ModuleCdc = types.ModuleCdc
NextAuctionIDKey = types.NextAuctionIDKey
)
type (
Keeper = keeper.Keeper
Auction = types.Auction
AuctionWithPhase = types.AuctionWithPhase
Auctions = types.Auctions
BaseAuction = types.BaseAuction
SurplusAuction = types.SurplusAuction
DebtAuction = types.DebtAuction
CollateralAuction = types.CollateralAuction
WeightedAddresses = types.WeightedAddresses
SupplyKeeper = types.SupplyKeeper
DebtAuction = types.DebtAuction
GenesisAuction = types.GenesisAuction
GenesisAuctions = types.GenesisAuctions
GenesisState = types.GenesisState
MsgPlaceBid = types.MsgPlaceBid
Params = types.Params
QueryAuctionParams = types.QueryAuctionParams
QueryAllAuctionParams = types.QueryAllAuctionParams
AuctionWithPhase = types.AuctionWithPhase
QueryAuctionParams = types.QueryAuctionParams
SupplyKeeper = types.SupplyKeeper
SurplusAuction = types.SurplusAuction
WeightedAddresses = types.WeightedAddresses
)

View File

@ -57,8 +57,7 @@ func (k Keeper) StartDebtAuction(ctx sdk.Context, buyer string, bid sdk.Coin, in
// This auction type mints coins at close. Need to check module account has minting privileges to avoid potential err in endblocker.
macc := k.supplyKeeper.GetModuleAccount(ctx, buyer)
if !macc.HasPermission(supply.Minter) {
// TODO: this should panic?
return 0, sdkerrors.Wrap(types.ErrInvalidModulePermissions, supply.Minter)
panic(fmt.Errorf("module '%s' does not have '%s' permission", buyer, supply.Minter))
}
// NOTE: for the duration of the auction the auction module account holds the debt
@ -239,8 +238,7 @@ func (k Keeper) PlaceForwardBidCollateral(ctx sdk.Context, a types.CollateralAuc
return a, sdkerrors.Wrapf(types.ErrInvalidBidDenom, "%s ≠ %s", bid.Denom, a.Bid.Denom)
}
if a.IsReversePhase() {
// TODO: panic maybe?
return a, sdkerrors.Wrapf(types.ErrCollateralAuctionIsInReversePhase, "%d", a.ID)
panic("cannot place forward bid on auction in reverse phase")
}
minNewBidAmt := a.Bid.Amount.Add( // new bids must be some % greater than old bid, and at least 1 larger to avoid replacing an old bid at no cost
sdk.MaxInt(
@ -316,8 +314,7 @@ func (k Keeper) PlaceReverseBidCollateral(ctx sdk.Context, a types.CollateralAuc
return a, sdkerrors.Wrapf(types.ErrInvalidLotDenom, lot.Denom, a.Lot.Denom)
}
if !a.IsReversePhase() {
// TODO: Panic here?
return a, sdkerrors.Wrapf(types.ErrCollateralAuctionIsInForwardPhase, "%d", a.ID)
panic("cannot place reverse bid on auction in forward phase")
}
maxNewLotAmt := a.Lot.Amount.Sub( // new lot must be some % less than old lot, and at least 1 smaller to avoid replacing an old bid at no cost
sdk.MaxInt(
@ -454,13 +451,10 @@ func (k Keeper) CloseAuction(ctx sdk.Context, auctionID uint64) error {
auction, found := k.GetAuction(ctx, auctionID)
if !found {
// TODO: panic if we are closing an auction in BB that isn't in the store?
// how else would we get here?
return sdkerrors.Wrapf(types.ErrAuctionNotFound, "%d", auctionID)
}
if ctx.BlockTime().Before(auction.GetEndTime()) {
// TODO: Do we check this upstream? should this be a panic
return sdkerrors.Wrapf(types.ErrAuctionHasNotExpired, "block time %s, auction end time %s", ctx.BlockTime().UTC(), auction.GetEndTime().UTC())
}
@ -488,7 +482,7 @@ func (k Keeper) CloseAuction(ctx sdk.Context, auctionID uint64) error {
sdk.NewEvent(
types.EventTypeAuctionClose,
sdk.NewAttribute(types.AttributeKeyAuctionID, fmt.Sprintf("%d", auction.GetID())),
sdk.NewAttribute(types.AttributeKeyCloseBlock, fmt.Sprintf("%d", ctx.BlockHeight()))
sdk.NewAttribute(types.AttributeKeyCloseBlock, fmt.Sprintf("%d", ctx.BlockHeight())),
),
)
return nil
@ -499,20 +493,17 @@ func (k Keeper) PayoutDebtAuction(ctx sdk.Context, a types.DebtAuction) error {
// create the coins that are needed to pay off the debt
err := k.supplyKeeper.MintCoins(ctx, a.Initiator, sdk.NewCoins(a.Lot))
if err != nil {
// TODO: how would we get here? should this be a panic?
return err
panic(fmt.Errorf("could not mint coins: %w", err))
}
// send the new coins from the initiator module to the bidder
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, a.Initiator, a.Bidder, sdk.NewCoins(a.Lot))
if err != nil {
// TODO: how would we get here? should this be a panic?
return err
}
// if there is remaining debt, return it to the calling module to manage
if a.CorrespondingDebt.IsPositive() {
err = k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, a.Initiator, sdk.NewCoins(a.CorrespondingDebt))
if err != nil {
// TODO: how would we get here? should this be a panic?
return err
}
}
@ -524,7 +515,6 @@ func (k Keeper) PayoutSurplusAuction(ctx sdk.Context, a types.SurplusAuction) er
// Send the tokens from the auction module account where they are being managed to the bidder who won the auction
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, a.Bidder, sdk.NewCoins(a.Lot))
if err != nil {
// TODO: how would we get here? should this be a panic?
return err
}
return nil
@ -535,7 +525,6 @@ func (k Keeper) PayoutCollateralAuction(ctx sdk.Context, a types.CollateralAucti
// Send the tokens from the auction module account where they are being managed to the bidder who won the auction
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, a.Bidder, sdk.NewCoins(a.Lot))
if err != nil {
// TODO: how would we get here? should this be a panic?
return err
}
@ -543,7 +532,6 @@ func (k Keeper) PayoutCollateralAuction(ctx sdk.Context, a types.CollateralAucti
if a.CorrespondingDebt.IsPositive() {
err = k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, a.Initiator, sdk.NewCoins(a.CorrespondingDebt))
if err != nil {
// TODO: how would we get here? should this be a panic?
return err
}
}
@ -576,12 +564,6 @@ func earliestTime(t1, t2 time.Time) time.Time {
// splitCoinIntoWeightedBuckets divides up some amount of coins according to some weights.
func splitCoinIntoWeightedBuckets(coin sdk.Coin, buckets []sdk.Int) ([]sdk.Coin, error) {
for _, bucket := range buckets {
if bucket.IsNegative() {
// TODO: Panic here? How would the code get here (should catch at validation)
return nil, fmt.Errorf("cannot split %s into bucket with negative weight (%s)", coin.String(), bucket.String())
}
}
amounts := splitIntIntoWeightedBuckets(coin.Amount, buckets)
result := make([]sdk.Coin, len(amounts))
for i, a := range amounts {

View File

@ -7,30 +7,24 @@ import sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
var (
// ErrInvalidInitialAuctionID error for when the initial auction ID hasn't been set
ErrInvalidInitialAuctionID = sdkerrors.Register(ModuleName, 2, "initial auction ID hasn't been set")
// ErrInvalidModulePermissions error for when module doesn't have valid permissions
ErrInvalidModulePermissions = sdkerrors.Register(ModuleName, 3, "module does not have required permission")
// ErrUnrecognizedAuctionType error for unrecognized auction type
ErrUnrecognizedAuctionType = sdkerrors.Register(ModuleName, 4, "unrecognized auction type")
ErrUnrecognizedAuctionType = sdkerrors.Register(ModuleName, 3, "unrecognized auction type")
// ErrAuctionNotFound error for when an auction is not found
ErrAuctionNotFound = sdkerrors.Register(ModuleName, 5, "auction not found")
ErrAuctionNotFound = sdkerrors.Register(ModuleName, 4, "auction not found")
// ErrAuctionHasNotExpired error for attempting to close an auction that has not passed its end time
ErrAuctionHasNotExpired = sdkerrors.Register(ModuleName, 6, "auction can't be closed as curent block time has not passed auction end time")
ErrAuctionHasNotExpired = sdkerrors.Register(ModuleName, 5, "auction can't be closed as curent block time has not passed auction end time")
// ErrAuctionHasExpired error for when an auction is closed and unavailable for bidding
ErrAuctionHasExpired = sdkerrors.Register(ModuleName, 7, "auction has closed")
ErrAuctionHasExpired = sdkerrors.Register(ModuleName, 6, "auction has closed")
// ErrInvalidBidDenom error for when bid denom doesn't match auction bid denom
ErrInvalidBidDenom = sdkerrors.Register(ModuleName, 8, "bid denom doesn't match auction bid denom")
ErrInvalidBidDenom = sdkerrors.Register(ModuleName, 7, "bid denom doesn't match auction bid denom")
// ErrInvalidLotDenom error for when lot denom doesn't match auction lot denom
ErrInvalidLotDenom = sdkerrors.Register(ModuleName, 9, "lot denom doesn't match auction lot denom")
ErrInvalidLotDenom = sdkerrors.Register(ModuleName, 8, "lot denom doesn't match auction lot denom")
// ErrBidTooSmall error for when bid is not greater than auction's min bid amount
ErrBidTooSmall = sdkerrors.Register(ModuleName, 10, "bid is not greater than auction's min new bid amount")
ErrBidTooSmall = sdkerrors.Register(ModuleName, 9, "bid is not greater than auction's min new bid amount")
// ErrBidTooLarge error for when bid is larger than auction's maximum allowed bid
ErrBidTooLarge = sdkerrors.Register(ModuleName, 11, "bid is greater than auction's max bid")
ErrBidTooLarge = sdkerrors.Register(ModuleName, 10, "bid is greater than auction's max bid")
// ErrLotTooSmall error for when lot is less than zero
ErrLotTooSmall = sdkerrors.Register(ModuleName, 12, "lot is not greater than auction's min new lot amount")
ErrLotTooSmall = sdkerrors.Register(ModuleName, 11, "lot is not greater than auction's min new lot amount")
// ErrLotTooLarge error for when lot is not smaller than auction's max new lot amount
ErrLotTooLarge = sdkerrors.Register(ModuleName, 13, "lot is greater than auction's max new lot amount")
// ErrCollateralAuctionIsInReversePhase error for when attempting to place a forward bid on a collateral auction in reverse phase
ErrCollateralAuctionIsInReversePhase = sdkerrors.Register(ModuleName, 14, "invalid bid: auction is in reverse phase")
// ErrCollateralAuctionIsInForwardPhase error for when attempting to place a reverse bid on a collateral auction in forward phase
ErrCollateralAuctionIsInForwardPhase = sdkerrors.Register(ModuleName, 15, "invalid bid: auction is in forward phase")
ErrLotTooLarge = sdkerrors.Register(ModuleName, 12, "lot is greater than auction's max new lot amount")
)

View File

@ -76,7 +76,7 @@ func (gs GenesisState) Validate() error {
ids[a.GetID()] = true
if a.GetID() >= gs.NextAuctionID {
return fmt.Errorf("found auction ID >= the nextAuctionID (%d >= %d)", a.GetID(), gs.NextAuctionID)
return fmt.Errorf("found auction ID ≥ the nextAuctionID (%d ≥ %d)", a.GetID(), gs.NextAuctionID)
}
}
return nil