move nextAuctionID from params to genState

This commit is contained in:
rhuairahrighairigh 2019-12-28 17:16:08 +00:00
parent c786850b1c
commit c867e8ba9e
4 changed files with 15 additions and 18 deletions

View File

@ -17,7 +17,6 @@ const (
DefaultParamspace = types.DefaultParamspace DefaultParamspace = types.DefaultParamspace
DefaultMaxAuctionDuration = types.DefaultMaxAuctionDuration DefaultMaxAuctionDuration = types.DefaultMaxAuctionDuration
DefaultMaxBidDuration = types.DefaultMaxBidDuration DefaultMaxBidDuration = types.DefaultMaxBidDuration
DefaultStartingAuctionID = types.DefaultStartingAuctionID
QueryGetAuction = types.QueryGetAuction QueryGetAuction = types.QueryGetAuction
) )
@ -42,7 +41,6 @@ var (
ModuleCdc = types.ModuleCdc ModuleCdc = types.ModuleCdc
KeyAuctionBidDuration = types.KeyAuctionBidDuration KeyAuctionBidDuration = types.KeyAuctionBidDuration
KeyAuctionDuration = types.KeyAuctionDuration KeyAuctionDuration = types.KeyAuctionDuration
KeyAuctionStartingID = types.KeyAuctionStartingID
) )
type ( type (

View File

@ -6,6 +6,8 @@ import (
// InitGenesis - initializes the store state from genesis data // InitGenesis - initializes the store state from genesis data
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) { func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
keeper.SetNextAuctionID(ctx, data.NextAuctionID)
keeper.SetParams(ctx, data.AuctionParams) keeper.SetParams(ctx, data.AuctionParams)
for _, a := range data.Auctions { for _, a := range data.Auctions {
@ -15,6 +17,11 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
// ExportGenesis returns a GenesisState for a given context and keeper. // ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState { func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
nextAuctionID, err := keeper.GetNextAuctionID(ctx)
if err != nil {
panic(err)
}
params := keeper.GetParams(ctx) params := keeper.GetParams(ctx)
var genAuctions GenesisAuctions var genAuctions GenesisAuctions
@ -23,5 +30,5 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
return false return false
}) })
return NewGenesisState(params, genAuctions) return NewGenesisState(nextAuctionID, params, genAuctions)
} }

View File

@ -9,13 +9,15 @@ type GenesisAuctions []Auction
// GenesisState - auction state that must be provided at genesis // GenesisState - auction state that must be provided at genesis
type GenesisState struct { type GenesisState struct {
NextAuctionID ID
AuctionParams AuctionParams `json:"auction_params" yaml:"auction_params"` AuctionParams AuctionParams `json:"auction_params" yaml:"auction_params"`
Auctions GenesisAuctions `json:"genesis_auctions" yaml:"genesis_auctions"` Auctions GenesisAuctions `json:"genesis_auctions" yaml:"genesis_auctions"`
} }
// NewGenesisState returns a new genesis state object for auctions module // NewGenesisState returns a new genesis state object for auctions module
func NewGenesisState(ap AuctionParams, ga GenesisAuctions) GenesisState { func NewGenesisState(nextID ID, ap AuctionParams, ga GenesisAuctions) GenesisState {
return GenesisState{ return GenesisState{
NextAuctionID: nextID,
AuctionParams: ap, AuctionParams: ap,
Auctions: ga, Auctions: ga,
} }
@ -23,7 +25,7 @@ func NewGenesisState(ap AuctionParams, ga GenesisAuctions) GenesisState {
// DefaultGenesisState defines default genesis state for auction module // DefaultGenesisState defines default genesis state for auction module
func DefaultGenesisState() GenesisState { func DefaultGenesisState() GenesisState {
return NewGenesisState(DefaultAuctionParams(), GenesisAuctions{}) return NewGenesisState(ID(0), DefaultAuctionParams(), GenesisAuctions{})
} }
// Equal checks whether two GenesisState structs are equivalent // Equal checks whether two GenesisState structs are equivalent

View File

@ -14,8 +14,6 @@ const (
DefaultMaxAuctionDuration time.Duration = 2 * 24 * time.Hour DefaultMaxAuctionDuration time.Duration = 2 * 24 * time.Hour
// DefaultBidDuration how long an auction gets extended when someone bids, roughly 3 hours in blocks // DefaultBidDuration how long an auction gets extended when someone bids, roughly 3 hours in blocks
DefaultMaxBidDuration time.Duration = 3 * time.Hour DefaultMaxBidDuration time.Duration = 3 * time.Hour
// DefaultStartingAuctionID what the id of the first auction will be
DefaultStartingAuctionID ID = ID(0)
) )
// Parameter keys // Parameter keys
@ -23,7 +21,6 @@ var (
// ParamStoreKeyAuctionParams Param store key for auction params // ParamStoreKeyAuctionParams Param store key for auction params
KeyAuctionBidDuration = []byte("MaxBidDuration") KeyAuctionBidDuration = []byte("MaxBidDuration")
KeyAuctionDuration = []byte("MaxAuctionDuration") KeyAuctionDuration = []byte("MaxAuctionDuration")
KeyAuctionStartingID = []byte("StartingAuctionID")
) )
var _ subspace.ParamSet = &AuctionParams{} var _ subspace.ParamSet = &AuctionParams{}
@ -32,15 +29,13 @@ var _ subspace.ParamSet = &AuctionParams{}
type AuctionParams struct { type AuctionParams struct {
MaxAuctionDuration time.Duration `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction, in blocks MaxAuctionDuration time.Duration `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction, in blocks
MaxBidDuration time.Duration `json:"max_bid_duration" yaml:"max_bid_duration"` MaxBidDuration time.Duration `json:"max_bid_duration" yaml:"max_bid_duration"`
StartingAuctionID ID `json:"starting_auction_id" yaml:"starting_auction_id"`
} }
// NewAuctionParams creates a new AuctionParams object // NewAuctionParams creates a new AuctionParams object
func NewAuctionParams(maxAuctionDuration time.Duration, bidDuration time.Duration, startingID ID) AuctionParams { func NewAuctionParams(maxAuctionDuration time.Duration, bidDuration time.Duration) AuctionParams {
return AuctionParams{ return AuctionParams{
MaxAuctionDuration: maxAuctionDuration, MaxAuctionDuration: maxAuctionDuration,
MaxBidDuration: bidDuration, MaxBidDuration: bidDuration,
StartingAuctionID: startingID,
} }
} }
@ -49,7 +44,6 @@ func DefaultAuctionParams() AuctionParams {
return NewAuctionParams( return NewAuctionParams(
DefaultMaxAuctionDuration, DefaultMaxAuctionDuration,
DefaultMaxBidDuration, DefaultMaxBidDuration,
DefaultStartingAuctionID,
) )
} }
@ -65,7 +59,6 @@ func (ap *AuctionParams) ParamSetPairs() subspace.ParamSetPairs {
return subspace.ParamSetPairs{ return subspace.ParamSetPairs{
{KeyAuctionBidDuration, &ap.MaxBidDuration}, {KeyAuctionBidDuration, &ap.MaxBidDuration},
{KeyAuctionDuration, &ap.MaxAuctionDuration}, {KeyAuctionDuration, &ap.MaxAuctionDuration},
{KeyAuctionStartingID, &ap.StartingAuctionID},
} }
} }
@ -80,14 +73,11 @@ func (ap AuctionParams) Equal(ap2 AuctionParams) bool {
func (ap AuctionParams) String() string { func (ap AuctionParams) String() string {
return fmt.Sprintf(`Auction Params: return fmt.Sprintf(`Auction Params:
Max Auction Duration: %s Max Auction Duration: %s
Max Bid Duration: %s Max Bid Duration: %s`, ap.MaxAuctionDuration, ap.MaxBidDuration)
Starting Auction ID: %v`, ap.MaxAuctionDuration, ap.MaxBidDuration, ap.StartingAuctionID)
} }
// Validate checks that the parameters have valid values. // Validate checks that the parameters have valid values.
func (ap AuctionParams) Validate() error { func (ap AuctionParams) Validate() error {
if ap.StartingAuctionID <= ID(0) { // TODO check durations are within acceptable limits, if needed
return fmt.Errorf("starting auction ID should be positive, is %v", ap.StartingAuctionID)
}
return nil return nil
} }