/
genesis.go
131 lines (115 loc) · 3.86 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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cast"
)
// DefaultGenesis returns the default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
OrderBookList: []OrderBook{},
OrderBookParticipationList: []OrderBookParticipation{},
OrderBookExposureList: []OrderBookOddsExposure{},
ParticipationExposureList: []ParticipationExposure{},
ParticipationExposureByIndexList: []ParticipationExposure{},
HistoricalParticipationExposureList: []ParticipationExposure{},
ParticipationBetPairExposureList: []ParticipationBetPair{},
PayoutLock: [][]byte{},
Stats: OrderBookStats{ResolvedUnsettled: []string{}},
}
}
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
for _, p := range gs.OrderBookParticipationList {
_, err := sdk.AccAddressFromBech32(p.ParticipantAddress)
if err != nil {
return fmt.Errorf("invalid participant address %s", p.ParticipantAddress)
}
bookFound := false
for _, b := range gs.OrderBookList {
if b.UID == p.OrderBookUID {
bookFound = true
}
}
if !bookFound {
return fmt.Errorf("book with id %s not found for participation %d", p.OrderBookUID, p.Index)
}
}
for _, b := range gs.OrderBookList {
oddsCount := 0
for _, be := range gs.OrderBookExposureList {
exposureFound := false
for _, pe := range gs.ParticipationExposureList {
if pe.OrderBookUID == b.UID && pe.OddsUID == be.OddsUID {
exposureFound = true
}
}
if !exposureFound {
return fmt.Errorf("book with id %s not found for odds with uid %s", be.OrderBookUID, be.OddsUID)
}
if be.OrderBookUID == b.UID {
oddsCount++
}
}
exposureCount := cast.ToUint64(oddsCount)
if exposureCount != b.OddsCount {
return fmt.Errorf("book with id %s count does not match the odds exposure count %d", b.UID, exposureCount)
}
}
for _, pei := range gs.ParticipationExposureByIndexList {
exposureIndexFound := false
for _, pe := range gs.ParticipationExposureList {
if pei.OrderBookUID == pe.OrderBookUID &&
pei.OddsUID == pe.OddsUID &&
pei.ParticipationIndex == pe.ParticipationIndex {
exposureIndexFound = true
}
}
if !exposureIndexFound {
return fmt.Errorf("participation index for the book %s, odds %s and index %d not found",
pei.OrderBookUID, pei.OddsUID, pei.ParticipationIndex)
}
}
for _, pe := range gs.ParticipationExposureList {
exposureIndexFound := false
for _, pei := range gs.ParticipationExposureByIndexList {
if pe.OrderBookUID == pei.OrderBookUID &&
pe.OddsUID == pei.OddsUID &&
pe.ParticipationIndex == pei.ParticipationIndex {
exposureIndexFound = true
}
}
if !exposureIndexFound {
return fmt.Errorf("participation for the book %s, odds %s and index %d not found",
pe.OrderBookUID, pe.OddsUID, pe.ParticipationIndex)
}
}
for _, hpei := range gs.HistoricalParticipationExposureList {
exposureIndexFound := false
for _, pe := range gs.ParticipationExposureList {
if hpei.OrderBookUID == pe.OrderBookUID &&
hpei.OddsUID == pe.OddsUID &&
hpei.ParticipationIndex == pe.ParticipationIndex {
exposureIndexFound = true
}
}
if !exposureIndexFound {
return fmt.Errorf("participation history for the book %s, odds %s and index %d not found",
hpei.OrderBookUID, hpei.OddsUID, hpei.ParticipationIndex)
}
}
for _, bp := range gs.ParticipationBetPairExposureList {
bookFound := false
for _, b := range gs.OrderBookList {
if bp.OrderBookUID == b.UID {
bookFound = true
}
}
if !bookFound {
return fmt.Errorf("participation bet pair not found for the book %s", bp.OrderBookUID)
}
}
return gs.Params.Validate()
}