/
genesis.go
57 lines (47 loc) · 1.54 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
package types
import (
"fmt"
"sort"
)
func createGenesis(epochs []EpochInfo) *GenesisState {
genesisState := &GenesisState{}
genesisState.EpochInfoList = append(genesisState.EpochInfoList, epochs...)
// Sort the list so the order is deterministic.
sort.SliceStable(genesisState.EpochInfoList, func(i, j int) bool {
return genesisState.EpochInfoList[i].Name < genesisState.EpochInfoList[j].Name
})
return genesisState
}
// DefaultGenesis returns the default epochs genesis state.
func DefaultGenesis() *GenesisState {
return createGenesis(GenesisEpochs)
}
// DefaultGenesisWithEpochs returns the default genesis state with input epochs added or overwritten.
func DefaultGenesisWithEpochs(epochs ...EpochInfo) *GenesisState {
newEpochs := append([]EpochInfo(nil), GenesisEpochs...)
for _, epoch := range epochs {
for i := range newEpochs {
if newEpochs[i].Name == epoch.Name {
newEpochs[i] = epoch
}
}
}
return createGenesis(newEpochs)
}
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
// Check for duplicated index in epochInfo
epochInfoIndexMap := make(map[string]struct{})
for _, epochInfo := range gs.EpochInfoList {
index := string(epochInfo.Name)
if _, ok := epochInfoIndexMap[index]; ok {
return fmt.Errorf("duplicated index for epochInfo")
}
epochInfoIndexMap[index] = struct{}{}
if err := epochInfo.Validate(); err != nil {
return fmt.Errorf("failed to validate epochInfo in genesis state: %w", err)
}
}
return nil
}