-
Notifications
You must be signed in to change notification settings - Fork 41
/
genesis.go
44 lines (35 loc) · 1.21 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
package types
import (
"fmt"
)
// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
Resources: []*ResourceWithMetadata{},
FeeParams: &FeeParams{},
}
}
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
return gs.ValidateNoDuplicates()
}
func (gs GenesisState) ValidateNoDuplicates() error {
// Group resources by collection
resourcesByCollection := make(map[string][]*ResourceWithMetadata)
for _, resource := range gs.Resources {
existing := resourcesByCollection[resource.Metadata.CollectionId]
resourcesByCollection[resource.Metadata.CollectionId] = append(existing, resource)
}
// Check that there are no collisions within each collection
for _, resources := range resourcesByCollection {
resourceIdMap := make(map[string]bool)
for _, resource := range resources {
if _, ok := resourceIdMap[resource.Metadata.Id]; ok {
return fmt.Errorf("duplicated id for resource within the same collection. collection: %s, id: %s", resource.Metadata.CollectionId, resource.Metadata.Id)
}
resourceIdMap[resource.Metadata.Id] = true
}
}
return nil
}