-
Notifications
You must be signed in to change notification settings - Fork 44
/
genesis.go
67 lines (57 loc) · 1.6 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
package types
import (
"fmt"
)
// NewGenesisState creates a new genesis state
func NewGenesisState(relationships []Relationship, blocks []UserBlock) *GenesisState {
return &GenesisState{
Relationships: relationships,
Blocks: blocks,
}
}
// DefaultGenesisState returns a default GenesisState
func DefaultGenesisState() *GenesisState {
return NewGenesisState(nil, nil)
}
// ValidateGenesis validates the given genesis state and returns an error if something is invalid
func ValidateGenesis(data *GenesisState) error {
for i, rel := range data.Relationships {
if ContainDuplicatesRelationships(data.Relationships, rel) {
return fmt.Errorf("duplicated relationship: %s", &data.Relationships[i])
}
err := rel.Validate()
if err != nil {
return err
}
}
for i, ub := range data.Blocks {
if ContainDuplicatesBlocks(data.Blocks, ub) {
return fmt.Errorf("duplicated user block: %s", &data.Blocks[i])
}
err := ub.Validate()
if err != nil {
return err
}
}
return nil
}
// ContainDuplicatesRelationships tells whether the given relationships slice contain duplicates of the provided relationship
func ContainDuplicatesRelationships(relationships []Relationship, relationship Relationship) bool {
var count = 0
for _, r := range relationships {
if r.Equal(relationship) {
count++
}
}
return count > 1
}
// ContainDuplicatesBlocks tells whether the given blocks slice contain duplicates of the provided block
func ContainDuplicatesBlocks(blocks []UserBlock, block UserBlock) bool {
var count = 0
for _, r := range blocks {
if r.Equal(block) {
count++
}
}
return count > 1
}