-
Notifications
You must be signed in to change notification settings - Fork 45
/
genesis.go
308 lines (261 loc) · 8.6 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package types
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec/types"
)
// NewGenesisState returns a new GenesisState instance
func NewGenesisState(
subspacesData []SubspaceDataEntry,
posts []Post,
postsData []PostDataEntry,
attachments []Attachment,
activePolls []ActivePollData,
userAnswers []UserAnswer,
params Params,
transferRequests []PostOwnerTransferRequest,
) *GenesisState {
return &GenesisState{
SubspacesData: subspacesData,
Posts: posts,
PostsData: postsData,
Attachments: attachments,
ActivePolls: activePolls,
UserAnswers: userAnswers,
Params: params,
PostOwnerTransferRequests: transferRequests,
}
}
// DefaultGenesisState returns a default GenesisState
func DefaultGenesisState() *GenesisState {
return NewGenesisState(nil, nil, nil, nil, nil, nil, DefaultParams(), nil)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (e GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, report := range e.Attachments {
err := report.UnpackInterfaces(unpacker)
if err != nil {
return err
}
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// ValidateGenesis validates the given genesis state and returns an error if something is invalid
func ValidateGenesis(data *GenesisState) error {
for _, entry := range data.SubspacesData {
if containsDuplicatedSubspaceDataEntry(data.SubspacesData, entry) {
return fmt.Errorf("duplicated subspace data entry: %d", entry.SubspaceID)
}
err := entry.Validate()
if err != nil {
return err
}
}
for _, post := range data.Posts {
if containsDuplicatedPost(data.Posts, post) {
return fmt.Errorf("duplicated post: subspace id %d, post id %d", post.SubspaceID, post.ID)
}
err := post.Validate()
if err != nil {
return err
}
}
for _, entry := range data.PostsData {
if containsDuplicatedPostDataEntry(data.PostsData, entry) {
return fmt.Errorf("duplicated post data entry: subspace id %d, post id %d", entry.SubspaceID, entry.PostID)
}
err := entry.Validate()
if err != nil {
return err
}
}
for _, attachment := range data.Attachments {
if containsDuplicatedAttachment(data.Attachments, attachment) {
return fmt.Errorf("duplicated attachment: subspace id %d, post id %d, attachment id %d",
attachment.SubspaceID, attachment.PostID, attachment.ID)
}
err := attachment.Validate()
if err != nil {
return err
}
}
for _, pollData := range data.ActivePolls {
if containsDuplicatedPollData(data.ActivePolls, pollData) {
return fmt.Errorf("duplicated poll data: subspace id %d, post id %d, poll id %d",
pollData.SubspaceID, pollData.PostID, pollData.PollID)
}
err := pollData.Validate()
if err != nil {
return err
}
}
for _, answer := range data.UserAnswers {
if containsDuplicatedAnswer(data.UserAnswers, answer) {
return fmt.Errorf("duplicated user answer: subspace id %d, post id %d, poll id %d, user: %s",
answer.SubspaceID, answer.PostID, answer.PollID, answer.User)
}
err := answer.Validate()
if err != nil {
return err
}
}
err := data.Params.Validate()
if err != nil {
return err
}
for _, request := range data.PostOwnerTransferRequests {
if containDuplicatedRequest(data.PostOwnerTransferRequests, request) {
return fmt.Errorf("duplicated post owner transfer request: subspace id %d, post id %d",
request.SubspaceID, request.PostID)
}
err := request.Validate()
if err != nil {
return err
}
}
return nil
}
// containsDuplicatedSubspaceDataEntry tells whether the given entries slice contains
// two or more entries for the same subspace
func containsDuplicatedSubspaceDataEntry(entries []SubspaceDataEntry, entry SubspaceDataEntry) bool {
var count = 0
for _, s := range entries {
if s.SubspaceID == entry.SubspaceID {
count++
}
}
return count > 1
}
// containsDuplicatedPostDataEntry tells whether the given entries slice contains
// two or more entries for the same post
func containsDuplicatedPostDataEntry(entries []PostDataEntry, entry PostDataEntry) bool {
var count = 0
for _, s := range entries {
if s.SubspaceID == entry.SubspaceID && s.PostID == entry.PostID {
count++
}
}
return count > 1
}
// containsDuplicatedPost tells whether the given posts slice contains two or more posts
// having the same id of the given one
func containsDuplicatedPost(posts []Post, post Post) bool {
var count = 0
for _, s := range posts {
if s.SubspaceID == post.SubspaceID && s.ID == post.ID {
count++
}
}
return count > 1
}
// containsDuplicatedAttachment tells whether the given attachments slice contains two or more attachments
// having the same id of the given one
func containsDuplicatedAttachment(attachments []Attachment, attachment Attachment) bool {
var count = 0
for _, s := range attachments {
if s.SubspaceID == attachment.SubspaceID && s.PostID == attachment.PostID && s.ID == attachment.ID {
count++
}
}
return count > 1
}
// containsDuplicatedPollData tells whether the given polls data slice contains two or more data
// having the same id of the given one
func containsDuplicatedPollData(pollsData []ActivePollData, data ActivePollData) bool {
var count = 0
for _, d := range pollsData {
if d.SubspaceID == data.SubspaceID && d.PostID == data.PostID && d.PollID == data.PollID {
count++
}
}
return count > 1
}
// containsDuplicatedAnswer tells whether the given user answers slice contains two or more answers
// by the same user as the given one
func containsDuplicatedAnswer(answers []UserAnswer, answer UserAnswer) bool {
var count = 0
for _, s := range answers {
if s.SubspaceID == answer.SubspaceID && s.PostID == answer.PostID && s.PollID == answer.PollID && s.User == answer.User {
count++
}
}
return count > 1
}
// containDuplicatedRequest tells whether the given post owner transfer request slice contains two or more given
// post owner transfer request by the same id as the given one
func containDuplicatedRequest(requests []PostOwnerTransferRequest, request PostOwnerTransferRequest) bool {
var count = 0
for _, r := range requests {
if r.SubspaceID == request.SubspaceID && r.PostID == request.PostID {
count++
}
}
return count > 1
}
// --------------------------------------------------------------------------------------------------------------------
// NewSubspaceDataEntry returns a new SubspaceDataEntry instance
func NewSubspaceDataEntry(subspaceID uint64, initialPostID uint64) SubspaceDataEntry {
return SubspaceDataEntry{
SubspaceID: subspaceID,
InitialPostID: initialPostID,
}
}
// Validate returns an error if something is wrong within the entry data
func (e SubspaceDataEntry) Validate() error {
if e.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", e.SubspaceID)
}
if e.InitialPostID == 0 {
return fmt.Errorf("invalid initial post id: %d", e.InitialPostID)
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewPostDataEntry returns a new PostDataEntry instance
func NewPostDataEntry(subspaceID uint64, postID uint64, attachmentID uint32) PostDataEntry {
return PostDataEntry{
SubspaceID: subspaceID,
PostID: postID,
InitialAttachmentID: attachmentID,
}
}
// Validate returns an error if something is wrong within the entry
func (p PostDataEntry) Validate() error {
if p.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", p.SubspaceID)
}
if p.PostID == 0 {
return fmt.Errorf("invalid post id: %d", p.PostID)
}
if p.InitialAttachmentID == 0 {
return fmt.Errorf("invalid initial attachment id: %d", p.InitialAttachmentID)
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewActivePollData returns a new ActivePollData instance
func NewActivePollData(subspaceID uint64, postID uint64, pollID uint32, endTime time.Time) ActivePollData {
return ActivePollData{
SubspaceID: subspaceID,
PostID: postID,
PollID: pollID,
EndDate: endTime,
}
}
// Validate returns an error if something is wrong with the poll data
func (d ActivePollData) Validate() error {
if d.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id %d", d.SubspaceID)
}
if d.PostID == 0 {
return fmt.Errorf("invalid post id %d", d.PostID)
}
if d.PollID == 0 {
return fmt.Errorf("invalid poll id %d", d.PollID)
}
if d.EndDate.IsZero() {
return fmt.Errorf("invalid end time: %s", d.EndDate)
}
return nil
}