-
Notifications
You must be signed in to change notification settings - Fork 18
/
genesis.go
135 lines (111 loc) · 3.98 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
package types
import (
"fmt"
"github.com/KYVENetwork/chain/util"
)
// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
DelegatorList: []Delegator{},
DelegationEntryList: []DelegationEntry{},
DelegationDataList: []DelegationData{},
DelegationSlashList: []DelegationSlash{},
UndelegationQueueEntryList: []UndelegationQueueEntry{},
QueueStateUndelegation: QueueState{},
RedelegationCooldownList: []RedelegationCooldown{},
}
}
// Validate performs basic genesis state validation returning an error upon any failure.
func (gs *GenesisState) Validate() error {
if err := gs.validateF1(); err != nil {
return err
}
if err := gs.validateUnbondingQueue(); err != nil {
return err
}
if err := gs.validateRedelegation(); err != nil {
return err
}
return gs.Params.Validate()
}
func (gs *GenesisState) validateF1() error {
// Check for duplicated index in Delegator
delegatorMap := make(map[string]struct{})
delegatorKIndexMap := make(map[string]struct{})
for _, elem := range gs.DelegatorList {
index := string(DelegatorKey(elem.Staker, elem.Delegator))
if _, ok := delegatorMap[index]; ok {
return fmt.Errorf("duplicated index for delegator %v", elem)
}
delegatorMap[index] = struct{}{}
kIndex := string(util.GetByteKey(elem.Staker, elem.KIndex))
if _, ok := delegatorKIndexMap[kIndex]; ok {
return fmt.Errorf("duplicated k-index for delegator %v", elem)
}
delegatorKIndexMap[kIndex] = struct{}{}
}
// Check for duplicated index in DelegationEntries
delegationEntriesKIndexMap := make(map[string]struct{})
for _, elem := range gs.DelegationEntryList {
index := string(DelegationEntriesKey(elem.Staker, elem.KIndex))
if _, ok := delegationEntriesKIndexMap[index]; ok {
return fmt.Errorf("duplicated k-index for delegation entry %v", elem)
}
delegationEntriesKIndexMap[index] = struct{}{}
}
// Check for duplicated index in DelegationEntries
delegationDataMap := make(map[string]struct{})
for _, elem := range gs.DelegationDataList {
index := string(DelegationDataKey(elem.Staker))
if _, ok := delegationDataMap[index]; ok {
return fmt.Errorf("duplicated index for delegation data %v", elem)
}
delegationDataMap[index] = struct{}{}
}
// Check for duplicated index in SlashEntries
slashMap := make(map[string]struct{})
for _, elem := range gs.DelegationSlashList {
index := string(DelegationSlashEntriesKey(elem.Staker, elem.KIndex))
if _, ok := slashMap[index]; ok {
return fmt.Errorf("duplicated k-index for delegation entry %v", elem)
}
//nolint:all
entryIndex := string(DelegationEntriesKey(elem.Staker, elem.KIndex))
if _, ok := delegationEntriesKIndexMap[entryIndex]; !ok {
return fmt.Errorf("slash entry pointing to non-existent delegation index: %v", elem)
}
slashMap[index] = struct{}{}
}
return nil
}
func (gs *GenesisState) validateUnbondingQueue() error {
// Check undelegation queue
unbondingMap := make(map[string]struct{})
for _, elem := range gs.UndelegationQueueEntryList {
index := string(UndelegationQueueKey(elem.Index))
if _, ok := unbondingMap[index]; ok {
return fmt.Errorf("duplicated index for unbonding entry %v", elem)
}
if elem.Index > gs.QueueStateUndelegation.HighIndex {
return fmt.Errorf("unbonding entry index too high: %v", elem)
}
if elem.Index < gs.QueueStateUndelegation.LowIndex {
return fmt.Errorf("unbonding entry index too low: %v", elem)
}
unbondingMap[index] = struct{}{}
}
return nil
}
func (gs *GenesisState) validateRedelegation() error {
// Check undelegation queue
redelegationMap := make(map[string]struct{})
for _, elem := range gs.RedelegationCooldownList {
index := string(RedelegationCooldownKey(elem.Address, elem.CreationDate))
if _, ok := redelegationMap[index]; ok {
return fmt.Errorf("duplicated index for redelegation entry %v", elem)
}
redelegationMap[index] = struct{}{}
}
return nil
}