-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
90 lines (77 loc) · 2.19 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
package types
import (
"fmt"
"time"
)
// NewGenesisState returns a new GenesisState containing the provided data
func NewGenesisState(
drawEndTime time.Time, tickets []Ticket, pastDraws []HistoricalDrawData,
distributionParams DistributionParams, drawParams DrawParams, ticketParams TicketParams,
) *GenesisState {
return &GenesisState{
DrawEndTime: drawEndTime,
Tickets: tickets,
PastDraws: pastDraws,
DistributionParams: distributionParams,
DrawParams: drawParams,
TicketParams: ticketParams,
}
}
// DefaultGenesisState returns a default GenesisState
func DefaultGenesisState() *GenesisState {
return NewGenesisState(
time.Now().Add(time.Hour*24),
[]Ticket{},
[]HistoricalDrawData{},
DefaultDistributionParams(),
DefaultDrawParams(),
DefaultTicketParams(),
)
}
// ValidateGenesis validates the given genesis state and returns an error if something is invalid
func ValidateGenesis(state *GenesisState) error {
// Validate the draw
if state.DrawEndTime.IsZero() || time.Now().After(state.DrawEndTime) {
return fmt.Errorf("invalid draw end time: %s", state.DrawEndTime.Format(time.RFC3339))
}
// Validate the tickets
for _, t := range state.Tickets {
err := t.Validate()
if err != nil {
return err
}
// Check that the timestamp is not after the current draw
if t.Timestamp.After(state.DrawEndTime) {
return fmt.Errorf("ticket with id %s has creation date after the draw end time ", t.Id)
}
// Check that the timestamp is not in the future
if t.Timestamp.After(time.Now()) {
return fmt.Errorf("ticket with id %s has creation date set in the future", t.Id)
}
// Check id duplicates
if IsTicketIDDuplicated(t.Id, state.Tickets) {
return fmt.Errorf("ticket id duplicated: %s", t.Id)
}
}
// Validate the historical draws data
for _, data := range state.PastDraws {
err := data.Validate()
if err != nil {
return err
}
}
// Validate the params
err := ValidateDistributionParams(state.DistributionParams)
if err != nil {
return err
}
err = ValidateDrawParams(state.DrawParams)
if err != nil {
return err
}
err = ValidateTicketParams(state.TicketParams)
if err != nil {
return err
}
return nil
}