-
Notifications
You must be signed in to change notification settings - Fork 352
/
genesis.go
127 lines (107 loc) · 3.25 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package types
import (
"fmt"
types "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// DefaultNextAuctionID is the starting point for auction IDs.
const DefaultNextAuctionID uint64 = 1
// GenesisAuction extends the auction interface to add functionality
// needed for initializing auctions from genesis.
type GenesisAuction interface {
Auction
GetModuleAccountCoins() sdk.Coins
Validate() error
}
// PackGenesisAuctions converts a GenesisAuction slice to Any slice
func PackGenesisAuctions(ga []GenesisAuction) ([]*types.Any, error) {
gaAny := make([]*types.Any, len(ga))
for i, genesisAuction := range ga {
any, err := types.NewAnyWithValue(genesisAuction)
if err != nil {
return nil, err
}
gaAny[i] = any
}
return gaAny, nil
}
func mustPackGenesisAuctions(ga []GenesisAuction) []*types.Any {
anys, err := PackGenesisAuctions(ga)
if err != nil {
panic(err)
}
return anys
}
// UnpackGenesisAuctions converts Any slice to GenesisAuctions slice
func UnpackGenesisAuctions(genesisAuctionsAny []*types.Any) ([]GenesisAuction, error) {
genesisAuctions := make([]GenesisAuction, len(genesisAuctionsAny))
for i, any := range genesisAuctionsAny {
genesisAuction, ok := any.GetCachedValue().(GenesisAuction)
if !ok {
return nil, fmt.Errorf("expected genesis auction")
}
genesisAuctions[i] = genesisAuction
}
return genesisAuctions, nil
}
// Ensure this type will unpack contained interface types correctly when it is unmarshalled.
var _ types.UnpackInterfacesMessage = &GenesisState{}
// NewGenesisState returns a new genesis state object for auctions module.
func NewGenesisState(nextID uint64, ap Params, ga []GenesisAuction) (*GenesisState, error) {
packedGA, err := PackGenesisAuctions(ga)
if err != nil {
return &GenesisState{}, err
}
return &GenesisState{
NextAuctionId: nextID,
Params: ap,
Auctions: packedGA,
}, nil
}
// DefaultGenesisState returns the default genesis state for auction module.
func DefaultGenesisState() *GenesisState {
genesis, err := NewGenesisState(
DefaultNextAuctionID,
DefaultParams(),
[]GenesisAuction{},
)
if err != nil {
panic(fmt.Sprintf("could not create default genesis state: %v", err))
}
return genesis
}
// 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
}
auctions, err := UnpackGenesisAuctions(gs.Auctions)
if err != nil {
return err
}
ids := map[uint64]bool{}
for _, a := range 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
}
// UnpackInterfaces hooks into unmarshalling to unpack any interface types contained within the GenesisState.
func (gs GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, any := range gs.Auctions {
var auction GenesisAuction
err := unpacker.UnpackAny(any, &auction)
if err != nil {
return err
}
}
return nil
}