-
Notifications
You must be signed in to change notification settings - Fork 45
/
genesis.go
93 lines (79 loc) · 2.35 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
package types
import "fmt"
func NewPostReactionsEntry(postID string, reactions []PostReaction) PostReactionsEntry {
return PostReactionsEntry{
PostId: postID,
Reactions: reactions,
}
}
func NewUserAnswersEntry(postID string, answers []UserAnswer) UserAnswersEntry {
return UserAnswersEntry{
PostId: postID,
UserAnswers: answers,
}
}
// ___________________________________________________________________________________________________________________
// NewGenesisState creates a new genesis state
func NewGenesisState(
posts []Post, userPollAnswers []UserAnswersEntry,
postReactions []PostReactionsEntry, registeredReactions []RegisteredReaction, params Params,
) *GenesisState {
return &GenesisState{
Posts: posts,
UsersPollAnswers: userPollAnswers,
PostsReactions: postReactions,
RegisteredReactions: registeredReactions,
Params: params,
}
}
// DefaultGenesisState returns a default GenesisState
func DefaultGenesisState() *GenesisState {
return NewGenesisState(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
}
}
for _, record := range data.Posts {
err := record.Validate()
if err != nil {
return err
}
}
for _, pollAnswers := range data.UsersPollAnswers {
if !containsPostWithID(data.Posts, pollAnswers.PostId) {
return fmt.Errorf("invalid poll answers; post with id %s does not exist", pollAnswers.PostId)
}
for _, pollAnswer := range pollAnswers.UserAnswers {
err := pollAnswer.Validate()
if err != nil {
return err
}
}
}
for _, postReaction := range data.PostsReactions {
if !containsPostWithID(data.Posts, postReaction.PostId) {
return fmt.Errorf("invalid reactions; post with id %s does not exist", postReaction.PostId)
}
for _, record := range postReaction.Reactions {
err := record.Validate()
if err != nil {
return err
}
}
}
return data.Params.Validate()
}
// containsPostWithID tells whether or not the given posts contain one having the provided id
func containsPostWithID(posts []Post, id string) bool {
for _, p := range posts {
if p.PostId == id {
return true
}
}
return false
}