/
graph.go
124 lines (92 loc) · 2.18 KB
/
graph.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
package node
import (
hg "github.com/andrecronje/babble/src/hashgraph"
)
//Infos is a struct providing Hashgraph information
type Infos struct {
ParticipantEvents map[string]map[string]*hg.Event
Rounds []*hg.RoundInfo
Blocks []*hg.Block
}
//Graph is a struct containing a node
type Graph struct {
*Node
}
//GetParticipantEvents returns Participant Events
func (g *Graph) GetParticipantEvents() (map[string]map[string]*hg.Event, error) {
res := make(map[string]map[string]*hg.Event)
store := g.Node.core.hg.Store
repertoire := g.Node.core.hg.Store.RepertoireByPubKey()
for _, p := range repertoire {
root, err := store.GetRoot(p.PubKeyString())
if err != nil {
return res, err
}
start := -1
if l := len(root.Events); l > 0 {
start = root.Events[l-1].Core.Index()
}
evs, err := store.ParticipantEvents(p.PubKeyString(), start)
if err != nil {
return res, err
}
res[p.PubKeyString()] = make(map[string]*hg.Event)
for _, e := range evs {
event, err := store.GetEvent(e)
if err != nil {
return res, err
}
hash := event.Hex()
res[p.PubKeyString()][hash] = event
}
}
return res, nil
}
//GetRounds returns an array of RoundInfo
func (g *Graph) GetRounds() []*hg.RoundInfo {
res := []*hg.RoundInfo{}
round := 0
store := g.Node.core.hg.Store
for round <= store.LastRound() {
r, err := store.GetRound(round)
if err != nil {
break
}
res = append(res, r)
round++
}
return res
}
//GetBlocks returns an array of Blocks
func (g *Graph) GetBlocks() []*hg.Block {
res := []*hg.Block{}
blockIdx := 0
store := g.Node.core.hg.Store
for blockIdx <= store.LastBlockIndex() {
r, err := store.GetBlock(blockIdx)
if err != nil {
break
}
res = append(res, r)
blockIdx++
}
return res
}
//GetInfos returns an Infos struct
func (g *Graph) GetInfos() (Infos, error) {
participantEvents, err := g.GetParticipantEvents()
if err != nil {
return Infos{}, err
}
return Infos{
ParticipantEvents: participantEvents,
Rounds: g.GetRounds(),
Blocks: g.GetBlocks(),
}, nil
}
//NewGraph is a factory method returning a Graph
func NewGraph(n *Node) *Graph {
return &Graph{
Node: n,
}
}