-
Notifications
You must be signed in to change notification settings - Fork 43
/
genesis.go
103 lines (83 loc) · 2.42 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
package types
import (
"fmt"
)
const (
DefaultDidNamespace = "testnet"
DefaultCreateDidTxFee = 50e9 // 50 CHEQ or 50000000000 ncheq
DefaultUpdateDidTxFee = 25e9 // 25 CHEQ or 25000000000 ncheq
DefaultDeactivateDidTxFee = 10e9 // 10 CHEQ or 10000000000 ncheq
DefaultBurnFactor = "0.500000000000000000" // 0.5 or 50%
)
// DefaultGenesis returns the default `did` genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
VersionSets: []*DidDocVersionSet{},
DidNamespace: DefaultDidNamespace,
FeeParams: DefaultFeeParams(),
}
}
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
err := gs.ValidateNoDuplicates()
if err != nil {
return err
}
err = gs.ValidateVersionSets()
if err != nil {
return err
}
err = gs.FeeParams.ValidateBasic()
if err != nil {
return err
}
return gs.ValidateBasic()
}
func (gs GenesisState) ValidateNoDuplicates() error {
// Check for duplicates in version set
didCache := make(map[string]bool)
for _, versionSet := range gs.VersionSets {
did := versionSet.DidDocs[0].DidDoc.Id
if _, ok := didCache[did]; ok {
return fmt.Errorf("duplicated didDoc found with id %s", did)
}
didCache[did] = true
// Check for duplicates in didDoc versions
versionCache := make(map[string]bool)
for _, didDoc := range versionSet.DidDocs {
version := didDoc.Metadata.VersionId
if _, ok := versionCache[version]; ok {
return fmt.Errorf("duplicated didDoc version found with id %s and version %s", did, version)
}
versionCache[version] = true
}
// Check that latest version is present
if _, ok := versionCache[versionSet.LatestVersion]; !ok {
return fmt.Errorf("latest version not found in didDoc with id %s", did)
}
}
return nil
}
func (gs GenesisState) ValidateVersionSets() error {
for _, versionSet := range gs.VersionSets {
did := versionSet.DidDocs[0].DidDoc.Id
for _, didDoc := range versionSet.DidDocs {
if did != didDoc.DidDoc.Id {
return fmt.Errorf("diddoc %s does not belong to version set %s", didDoc.DidDoc.Id, did)
}
}
}
return nil
}
func (gs GenesisState) ValidateBasic() error {
for _, versionSet := range gs.VersionSets {
for _, didDoc := range versionSet.DidDocs {
err := didDoc.DidDoc.Validate(nil)
if err != nil {
return err
}
}
}
return nil
}