-
Notifications
You must be signed in to change notification settings - Fork 10
/
genesis.go
415 lines (384 loc) · 14.3 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package types
import (
"errors"
"time"
"github.com/cosmos/cosmos-sdk/types/address"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/tendermint/libs/rand"
)
const DefaultBondDenom = "utgd"
// DefaultGenesisState default values
func DefaultGenesisState() GenesisState {
return GenesisState{
SeedContracts: true,
BondDenom: DefaultBondDenom,
StakeContractConfig: &StakeContractConfig{
MinBond: 1,
TokensPerPoint: 1,
UnbondingPeriod: time.Hour * 21 * 24,
ClaimAutoreturnLimit: 20,
},
ValsetContractConfig: &ValsetContractConfig{
MinPoints: 1,
MaxValidators: 100,
EpochLength: 60 * time.Second,
EpochReward: sdk.NewCoin(DefaultBondDenom, sdk.NewInt(100_000)),
Scaling: 1,
FeePercentage: sdk.NewDec(50),
AutoUnjail: false,
DoubleSignSlashRatio: sdk.NewDec(50),
ValidatorRewardRatio: sdk.MustNewDecFromStr("47.5"),
EngagementRewardRatio: sdk.MustNewDecFromStr("47.5"),
CommunityPoolRewardRatio: sdk.MustNewDecFromStr("5"),
},
EngagementContractConfig: &EngagementContractConfig{
Halflife: 180 * 24 * time.Hour,
},
OversightCommitteeContractConfig: &OversightCommitteeContractConfig{
Name: "Oversight Community",
EscrowAmount: sdk.NewCoin(DefaultBondDenom, sdk.NewInt(1_000_000)),
VotingRules: VotingRules{
VotingPeriod: 30,
Quorum: sdk.NewDec(51),
Threshold: sdk.NewDec(55),
AllowEndEarly: true,
},
},
CommunityPoolContractConfig: &CommunityPoolContractConfig{
VotingRules: VotingRules{
VotingPeriod: 21,
Quorum: sdk.NewDec(10),
Threshold: sdk.NewDec(60),
AllowEndEarly: true,
},
},
ValidatorVotingContractConfig: &ValidatorVotingContractConfig{
VotingRules: VotingRules{
VotingPeriod: 14,
Quorum: sdk.NewDec(40),
Threshold: sdk.NewDec(66),
AllowEndEarly: true,
},
},
ArbiterPoolContractConfig: &ArbiterPoolContractConfig{
Name: "Arbiter Pool",
EscrowAmount: sdk.NewCoin(DefaultBondDenom, sdk.NewInt(1_000_000)),
VotingRules: VotingRules{
VotingPeriod: 30,
Quorum: sdk.NewDec(51),
Threshold: sdk.NewDec(55),
AllowEndEarly: true,
},
DisputeCost: sdk.NewCoin(DefaultBondDenom, sdk.NewInt(1_000_000)),
},
SystemAdminAddress: sdk.AccAddress(rand.Bytes(address.Len)).String(),
Params: DefaultParams(),
}
}
func ValidateGenesis(g GenesisState, txJSONDecoder sdk.TxDecoder) error {
if g.SeedContracts {
if len(g.Contracts) != 0 {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "seed enabled but PoE contracts addresses provided")
}
if len(g.Engagement) == 0 {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty engagement group")
}
if g.EngagementContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty engagement contract config")
}
if err := g.EngagementContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "engagement contract config")
}
if err := sdk.ValidateDenom(g.BondDenom); err != nil {
return sdkerrors.Wrap(err, "bond denom")
}
if g.ValsetContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty valset contract config")
}
if err := g.ValsetContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "valset contract config")
}
if g.StakeContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty stake contract config")
}
if err := g.StakeContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "stake contract config")
}
if g.ValsetContractConfig.EpochReward.Denom != g.BondDenom {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "rewards not in bonded denom")
}
if g.OversightCommitteeContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty oversight committee contract config")
}
if err := g.OversightCommitteeContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "oversight committee config")
}
if g.ArbiterPoolContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty arbiter pool contract config")
}
if err := g.ArbiterPoolContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "arbiter pool config")
}
if g.OversightCommitteeContractConfig.EscrowAmount.Denom != g.BondDenom {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "escrow not in bonded denom")
}
if g.CommunityPoolContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty community pool contract config")
}
if err := g.CommunityPoolContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "community pool config")
}
if g.ValidatorVotingContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty validator voting contract config")
}
if err := g.ValidatorVotingContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "validator voting config")
}
} else {
return errors.New("not supported, yet")
//if len(g.Contracts) == 0 {
// return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "seed disabled but no PoE contract addresses provided")
//}
// todo (Alex): if we preserve state in the engagement contract then we need to ensure that there are no
// new members in the engagement group
// if we can reset state then the engagement group must not be empty
//if len(g.Engagement) != 0 {
// return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "engagement group set")
//}
//uniqueContractTypes := make(map[PoEContractType]struct{}, len(g.Contracts))
//for i, v := range g.Contracts {
// if err := v.ValidateBasic(); err != nil {
// return sdkerrors.Wrapf(err, "contract %d", i)
// }
// if _, exists := uniqueContractTypes[v.ContractType]; exists {
// return sdkerrors.Wrapf(wasmtypes.ErrDuplicate, "contract type %s", v.ContractType.String())
// }
//}
//if len(uniqueContractTypes) != len(PoEContractType_name)-1 {
// return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "PoE contract(s) missing")
//}
}
if _, err := sdk.AccAddressFromBech32(g.SystemAdminAddress); err != nil {
return sdkerrors.Wrap(err, "system admin address")
}
uniqueEngagementMembers := make(map[string]struct{}, len(g.Engagement))
for i, v := range g.Engagement {
if err := v.ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "contract %d", i)
}
if _, exists := uniqueEngagementMembers[v.Address]; exists {
return sdkerrors.Wrapf(wasmtypes.ErrDuplicate, "member: %s", v.Address)
}
uniqueEngagementMembers[v.Address] = struct{}{}
}
uniqueOperators := make(map[string]struct{}, len(g.GenTxs))
uniquePubKeys := make(map[string]struct{}, len(g.GenTxs))
for i, v := range g.GenTxs {
genTx, err := txJSONDecoder(v)
if err != nil {
return sdkerrors.Wrapf(err, "gentx %d", i)
}
msgs := genTx.GetMsgs()
if len(msgs) != 1 {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "tx with single message required")
}
msg := msgs[0].(*MsgCreateValidator)
if err := msg.ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "gentx %d", i)
}
if _, ok := uniqueEngagementMembers[msg.OperatorAddress]; !ok {
return sdkerrors.Wrapf(wasmtypes.ErrInvalidGenesis, "gen tx delegator not in engagement group: %q, gentx: %d", msg.OperatorAddress, i)
}
if _, exists := uniqueOperators[msg.OperatorAddress]; exists {
return sdkerrors.Wrapf(wasmtypes.ErrInvalidGenesis, "gen tx delegator used already with another gen tx: %q, gentx: %d", msg.OperatorAddress, i)
}
uniqueOperators[msg.OperatorAddress] = struct{}{}
pk := msg.Pubkey.String()
if _, exists := uniquePubKeys[pk]; exists {
return sdkerrors.Wrapf(wasmtypes.ErrInvalidGenesis, "gen tx public key used already with another gen tx: %q, gentx: %d", pk, i)
}
uniquePubKeys[pk] = struct{}{}
}
if len(g.OversightCommunityMembers) == 0 {
return sdkerrors.Wrapf(wasmtypes.ErrEmpty, "oversight community members")
}
uniqueOCMembers := make(map[string]struct{}, len(g.OversightCommunityMembers))
for _, member := range g.OversightCommunityMembers {
if _, err := sdk.AccAddressFromBech32(member); err != nil {
return sdkerrors.Wrap(err, "oc member address")
}
if _, exists := uniqueOCMembers[member]; exists {
return sdkerrors.Wrapf(wasmtypes.ErrDuplicate, "oc member: %s", member)
}
uniqueOCMembers[member] = struct{}{}
}
if len(g.ArbiterPoolMembers) == 0 {
return sdkerrors.Wrapf(wasmtypes.ErrEmpty, "arbiter pool members")
}
uniqueAPMembers := make(map[string]struct{}, len(g.ArbiterPoolMembers))
for _, member := range g.ArbiterPoolMembers {
if _, err := sdk.AccAddressFromBech32(member); err != nil {
return sdkerrors.Wrap(err, "ap member address")
}
if _, exists := uniqueAPMembers[member]; exists {
return sdkerrors.Wrapf(wasmtypes.ErrDuplicate, "ap member: %s", member)
}
uniqueAPMembers[member] = struct{}{}
}
return nil
}
// ValidateBasic ensure basic constraints
func (c *EngagementContractConfig) ValidateBasic() error {
if c.Halflife.Truncate(time.Second) != c.Halflife {
return sdkerrors.Wrap(ErrInvalid, "halflife must not contain anything less than seconds")
}
return nil
}
// ValidateBasic ensure basic constraints
func (c ValsetContractConfig) ValidateBasic() error {
if c.MaxValidators == 0 {
return sdkerrors.Wrap(ErrEmpty, "max validators")
}
if c.EpochLength == 0 {
return sdkerrors.Wrap(ErrEmpty, "epoch length")
}
if c.Scaling == 0 {
return sdkerrors.Wrap(ErrEmpty, "scaling")
}
hundred := sdk.NewDec(100)
if c.CommunityPoolRewardRatio.GT(hundred) {
return sdkerrors.Wrap(ErrInvalid, "community pool reward ratio must not be greater 100")
}
if c.EngagementRewardRatio.GT(hundred) {
return sdkerrors.Wrap(ErrInvalid, "engagement reward ratio must not be greater 100")
}
if c.ValidatorRewardRatio.GT(hundred) {
return sdkerrors.Wrap(ErrInvalid, "validator reward ratio must not be greater 100")
}
// ensure we sum up all ratios to 100%
totalRatio := c.EngagementRewardRatio.Add(c.CommunityPoolRewardRatio).Add(c.ValidatorRewardRatio)
if !totalRatio.Equal(hundred) {
return sdkerrors.Wrapf(ErrInvalid, "total reward ratio must be 100 but was %s", totalRatio)
}
minFeePercentage := sdk.NewDecFromIntWithPrec(sdk.OneInt(), 16)
if c.FeePercentage.LT(minFeePercentage) {
return sdkerrors.Wrap(ErrEmpty, "fee percentage")
}
return nil
}
// ValidateBasic ensure basic constraints
func (c StakeContractConfig) ValidateBasic() error {
if c.MinBond == 0 {
return sdkerrors.Wrap(ErrEmpty, "min bond")
}
if c.TokensPerPoint == 0 {
return sdkerrors.Wrap(ErrEmpty, "tokens per weight")
}
if c.UnbondingPeriod == 0 {
return sdkerrors.Wrap(ErrEmpty, "unbonding period")
}
if time.Duration(uint64(c.UnbondingPeriod.Seconds()))*time.Second != c.UnbondingPeriod {
return sdkerrors.Wrap(ErrInvalid, "unbonding period not convertible to seconds")
}
return nil
}
// ValidateBasic ensure basic constraints
func (c PoEContract) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(c.Address); err != nil {
return sdkerrors.Wrap(err, "address")
}
return sdkerrors.Wrap(c.ContractType.ValidateBasic(), "contract type")
}
const (
minNameLength = 1
maxNameLength = 100
minEscrowAmount = 999_999
)
// ValidateBasic ensure basic constraints
func (c OversightCommitteeContractConfig) ValidateBasic() error {
if l := len(c.Name); l < minNameLength {
return sdkerrors.Wrap(ErrEmpty, "name")
} else if l > maxNameLength {
return sdkerrors.Wrapf(ErrInvalid, "name length > %d", maxNameLength)
}
if c.EscrowAmount.Amount.LTE(sdk.NewInt(minEscrowAmount)) {
return sdkerrors.Wrapf(ErrInvalid, "escrow amount must be greater %d", minEscrowAmount)
}
if err := c.VotingRules.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "voting rules")
}
if c.DenyListContractAddress != "" {
if _, err := sdk.AccAddressFromBech32(c.DenyListContractAddress); err != nil {
return sdkerrors.Wrap(ErrInvalid, "deny list contract address")
}
}
return nil
}
// ValidateBasic ensure basic constraints
func (c CommunityPoolContractConfig) ValidateBasic() error {
if err := c.VotingRules.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "voting rules")
}
return nil
}
// ValidateBasic ensure basic constraints
func (c ValidatorVotingContractConfig) ValidateBasic() error {
if err := c.VotingRules.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "voting rules")
}
return nil
}
// ValidateBasic ensure basic constraints
func (c TG4Member) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(c.Address); err != nil {
return sdkerrors.Wrap(err, "address")
}
if c.Points == 0 {
return sdkerrors.Wrap(wasmtypes.ErrInvalid, "weight")
}
return nil
}
// ValidateBasic ensure basic constraints
func (v VotingRules) ValidateBasic() error {
if v.VotingPeriod == 0 {
return sdkerrors.Wrap(ErrEmpty, "voting period")
}
if v.Quorum.IsNil() || v.Quorum.LT(sdk.OneDec()) {
return sdkerrors.Wrap(ErrInvalid, "quorum must be > 0")
}
if v.Quorum.GT(sdk.NewDec(100)) {
return sdkerrors.Wrap(ErrInvalid, "quorum must be <=100")
}
if v.Threshold.IsNil() || v.Threshold.LT(sdk.NewDec(50)) {
return sdkerrors.Wrap(ErrInvalid, "threshold must be => 50")
}
if v.Threshold.GT(sdk.NewDec(100)) {
return sdkerrors.Wrap(ErrInvalid, "threshold must be <=100")
}
return nil
}
// ValidateBasic ensure basic constraints
func (c ArbiterPoolContractConfig) ValidateBasic() error {
if l := len(c.Name); l < minNameLength {
return sdkerrors.Wrap(ErrEmpty, "name")
} else if l > maxNameLength {
return sdkerrors.Wrapf(ErrInvalid, "name length > %d", maxNameLength)
}
if c.EscrowAmount.Amount.LTE(sdk.NewInt(minEscrowAmount)) {
return sdkerrors.Wrapf(ErrInvalid, "escrow amount must be greater %d", minEscrowAmount)
}
if err := c.VotingRules.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "voting rules")
}
if c.DenyListContractAddress != "" {
if _, err := sdk.AccAddressFromBech32(c.DenyListContractAddress); err != nil {
return sdkerrors.Wrap(ErrInvalid, "deny list contract address")
}
}
if time.Duration(uint64(c.WaitingPeriod.Seconds()))*time.Second != c.WaitingPeriod {
return sdkerrors.Wrap(ErrInvalid, "waiting period not convertible to seconds")
}
return nil
}