-
Notifications
You must be signed in to change notification settings - Fork 45
/
genesis.go
65 lines (57 loc) · 1.66 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
package types
import "fmt"
// NewGenesisState creates a new genesis state
func NewGenesisState(
posts []Post, userAnswers []UserAnswer,
postReactions []PostReaction, registeredReactions []RegisteredReaction, reports []Report, params Params,
) *GenesisState {
return &GenesisState{
Posts: posts,
UsersPollAnswers: userAnswers,
PostsReactions: postReactions,
RegisteredReactions: registeredReactions,
Reports: reports,
Params: params,
}
}
// DefaultGenesisState returns a default GenesisState
func DefaultGenesisState() *GenesisState {
return NewGenesisState(nil, nil, nil, nil, nil, DefaultParams())
}
// ValidateGenesis validates the given genesis state and returns an error if something is invalid
func ValidateGenesis(data *GenesisState) error {
for _, reaction := range data.RegisteredReactions {
err := reaction.Validate()
if err != nil {
return err
}
}
// The map for checking if post id exists or not
postMap := make(map[string]bool)
for _, record := range data.Posts {
err := record.Validate()
if err != nil {
return err
}
postMap[record.PostID] = true
}
for _, answer := range data.UsersPollAnswers {
if _, ok := postMap[answer.PostID]; !ok {
return fmt.Errorf("invalid user answers; post with id %s does not exist", answer.PostID)
}
err := answer.Validate()
if err != nil {
return err
}
}
for _, reaction := range data.PostsReactions {
if _, ok := postMap[reaction.PostID]; !ok {
return fmt.Errorf("invalid reactions; post with id %s does not exist", reaction.PostID)
}
err := reaction.Validate()
if err != nil {
return err
}
}
return data.Params.Validate()
}