-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
83 lines (68 loc) · 2.23 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package types
import (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// DefaultNextAuctionID is the starting poiint for auction IDs.
const DefaultNextAuctionID uint64 = 1
// GenesisAuction is an interface that extends the auction interface to add functionality needed for initializing auctions from genesis.
type GenesisAuction interface {
Auction
GetModuleAccountCoins() sdk.Coins
Validate() error
}
// GenesisAuctions is a slice of genesis auctions.
type GenesisAuctions []GenesisAuction
// GenesisState is auction state that must be provided at chain genesis.
type GenesisState struct {
NextAuctionID uint64 `json:"next_auction_id" yaml:"next_auction_id"`
Params Params `json:"params" yaml:"params"`
Auctions GenesisAuctions `json:"auctions" yaml:"auctions"`
}
// NewGenesisState returns a new genesis state object for auctions module.
func NewGenesisState(nextID uint64, ap Params, ga GenesisAuctions) GenesisState {
return GenesisState{
NextAuctionID: nextID,
Params: ap,
Auctions: ga,
}
}
// DefaultGenesisState returns the default genesis state for auction module.
func DefaultGenesisState() GenesisState {
return NewGenesisState(
DefaultNextAuctionID,
DefaultParams(),
GenesisAuctions{},
)
}
// Equal checks whether two GenesisState structs are equivalent.
func (gs GenesisState) Equal(gs2 GenesisState) bool {
b1 := ModuleCdc.MustMarshalBinaryBare(gs)
b2 := ModuleCdc.MustMarshalBinaryBare(gs2)
return bytes.Equal(b1, b2)
}
// IsEmpty returns true if a GenesisState is empty.
func (gs GenesisState) IsEmpty() bool {
return gs.Equal(GenesisState{})
}
// Validate validates genesis inputs. It returns error if validation of any input fails.
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {
return err
}
ids := map[uint64]bool{}
for _, a := range gs.Auctions {
if err := a.Validate(); err != nil {
return fmt.Errorf("found invalid auction: %w", err)
}
if ids[a.GetID()] {
return fmt.Errorf("found duplicate auction ID (%d)", a.GetID())
}
ids[a.GetID()] = true
if a.GetID() >= gs.NextAuctionID {
return fmt.Errorf("found auction ID ≥ the nextAuctionID (%d ≥ %d)", a.GetID(), gs.NextAuctionID)
}
}
return nil
}