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
DefaultMaxAuctionDuration = types.DefaultMaxAuctionDuration
DefaultMaxBidDuration = types.DefaultMaxBidDuration
DefaultStartingAuctionID = types.DefaultStartingAuctionID
QueryGetAuction = types.QueryGetAuction
)
@ -42,7 +41,6 @@ var (
ModuleCdc = types.ModuleCdc
KeyAuctionBidDuration = types.KeyAuctionBidDuration
KeyAuctionDuration = types.KeyAuctionDuration
KeyAuctionStartingID = types.KeyAuctionStartingID
)
type (

View File

@ -6,6 +6,8 @@ import (
// InitGenesis - initializes the store state from genesis data
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
keeper.SetNextAuctionID(ctx, data.NextAuctionID)
keeper.SetParams(ctx, data.AuctionParams)
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.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
nextAuctionID, err := keeper.GetNextAuctionID(ctx)
if err != nil {
panic(err)
}
params := keeper.GetParams(ctx)
var genAuctions GenesisAuctions
@ -23,5 +30,5 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
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
type GenesisState struct {
NextAuctionID ID
AuctionParams AuctionParams `json:"auction_params" yaml:"auction_params"`
Auctions GenesisAuctions `json:"genesis_auctions" yaml:"genesis_auctions"`
}
// 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{
NextAuctionID: nextID,
AuctionParams: ap,
Auctions: ga,
}
@ -23,7 +25,7 @@ func NewGenesisState(ap AuctionParams, ga GenesisAuctions) GenesisState {
// DefaultGenesisState defines default genesis state for auction module
func DefaultGenesisState() GenesisState {
return NewGenesisState(DefaultAuctionParams(), GenesisAuctions{})
return NewGenesisState(ID(0), DefaultAuctionParams(), GenesisAuctions{})
}
// Equal checks whether two GenesisState structs are equivalent

View File

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