-
Notifications
You must be signed in to change notification settings - Fork 3
/
genesis.go
206 lines (182 loc) · 5.02 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
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewGenesisState returns new GenesisState.
func NewGenesisState(
params Params, plans []PlanRecord, stakings []StakingRecord, queuedStakings []QueuedStakingRecord, totalStakings []TotalStakingsRecord,
historicalRewards []HistoricalRewardsRecord, outstandingRewards []OutstandingRewardsRecord,
currentEpochs []CurrentEpochRecord, rewardPoolCoins sdk.Coins,
lastEpochTime *time.Time, currentEpochDays uint32,
) *GenesisState {
return &GenesisState{
Params: params,
PlanRecords: plans,
StakingRecords: stakings,
QueuedStakingRecords: queuedStakings,
TotalStakingsRecords: totalStakings,
HistoricalRewardsRecords: historicalRewards,
OutstandingRewardsRecords: outstandingRewards,
CurrentEpochRecords: currentEpochs,
RewardPoolCoins: rewardPoolCoins,
LastEpochTime: lastEpochTime,
CurrentEpochDays: currentEpochDays,
}
}
// DefaultGenesisState returns the default genesis state.
func DefaultGenesisState() *GenesisState {
return NewGenesisState(
DefaultParams(),
[]PlanRecord{},
[]StakingRecord{},
[]QueuedStakingRecord{},
[]TotalStakingsRecord{},
[]HistoricalRewardsRecord{},
[]OutstandingRewardsRecord{},
[]CurrentEpochRecord{},
sdk.Coins{},
nil,
DefaultCurrentEpochDays,
)
}
// ValidateGenesis validates GenesisState.
func ValidateGenesis(data GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
id := uint64(0)
var plans []PlanI
for _, record := range data.PlanRecords {
if err := record.Validate(); err != nil {
return err
}
plan, _ := UnpackPlan(&record.Plan)
if plan.GetId() < id {
return fmt.Errorf("pool records must be sorted")
}
plans = append(plans, plan)
id = plan.GetId() + 1
}
if err := ValidateTotalEpochRatio(plans); err != nil {
return err
}
for _, record := range data.StakingRecords {
if err := record.Validate(); err != nil {
return err
}
}
for _, record := range data.QueuedStakingRecords {
if err := record.Validate(); err != nil {
return err
}
}
for _, record := range data.TotalStakingsRecords {
if err := record.Validate(); err != nil {
return err
}
}
for _, record := range data.HistoricalRewardsRecords {
if err := record.Validate(); err != nil {
return err
}
}
for _, record := range data.OutstandingRewardsRecords {
if err := record.Validate(); err != nil {
return err
}
}
for _, record := range data.CurrentEpochRecords {
if err := record.Validate(); err != nil {
return err
}
}
if err := data.RewardPoolCoins.Validate(); err != nil {
return err
}
if data.CurrentEpochDays == 0 {
return fmt.Errorf("current epoch days must be positive")
}
return nil
}
// Validate validates PlanRecord.
func (record PlanRecord) Validate() error {
plan, err := UnpackPlan(&record.Plan)
if err != nil {
return err
}
if err := plan.Validate(); err != nil {
return err
}
if err := record.FarmingPoolCoins.Validate(); err != nil {
return err
}
return nil
}
// Validate validates StakingRecord.
func (record StakingRecord) Validate() error {
if _, err := sdk.AccAddressFromBech32(record.Farmer); err != nil {
return err
}
if err := sdk.ValidateDenom(record.StakingCoinDenom); err != nil {
return err
}
if !record.Staking.Amount.IsPositive() {
return fmt.Errorf("staking amount must be positive: %s", record.Staking.Amount)
}
return nil
}
// Validate validates StakingRecord.
func (record TotalStakingsRecord) Validate() error {
if err := sdk.ValidateDenom(record.StakingCoinDenom); err != nil {
return err
}
if !record.Amount.IsPositive() {
return fmt.Errorf("total staking amount must be positive: %s", record.Amount)
}
if err := record.StakingReserveCoins.Validate(); err != nil {
return err
}
return nil
}
// Validate validates QueuedStakingRecord.
func (record QueuedStakingRecord) Validate() error {
if _, err := sdk.AccAddressFromBech32(record.Farmer); err != nil {
return err
}
if err := sdk.ValidateDenom(record.StakingCoinDenom); err != nil {
return err
}
if !record.QueuedStaking.Amount.IsPositive() {
return fmt.Errorf("queued staking amount must be positive: %s", record.QueuedStaking.Amount)
}
return nil
}
// Validate validates HistoricalRewardsRecord.
func (record HistoricalRewardsRecord) Validate() error {
if err := sdk.ValidateDenom(record.StakingCoinDenom); err != nil {
return err
}
if err := record.HistoricalRewards.CumulativeUnitRewards.Validate(); err != nil {
return err
}
return nil
}
// Validate validates OutstandingRewardsRecord.
func (record OutstandingRewardsRecord) Validate() error {
if err := sdk.ValidateDenom(record.StakingCoinDenom); err != nil {
return err
}
if err := record.OutstandingRewards.Rewards.Validate(); err != nil {
return err
}
return nil
}
// Validate validates CurrentEpochRecord.
func (record CurrentEpochRecord) Validate() error {
if err := sdk.ValidateDenom(record.StakingCoinDenom); err != nil {
return err
}
return nil
}