-
Notifications
You must be signed in to change notification settings - Fork 114
/
registry.go
254 lines (209 loc) · 6.4 KB
/
registry.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package statement
import (
"context"
"sync"
"time"
"github.com/iotaledger/hive.go/identity"
"github.com/iotaledger/goshimmer/packages/clock"
"github.com/iotaledger/goshimmer/packages/ledgerstate"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/packages/vote/opinion"
)
// region Registry /////////////////////////////////////////////////////////////////////////////////////////////////////
// Registry holds the opinions of all the nodes.
type Registry struct {
nodesView map[identity.ID]*View
mu sync.RWMutex
}
// NewRegistry returns a new registry.
func NewRegistry() *Registry {
return &Registry{
nodesView: make(map[identity.ID]*View),
}
}
// NodeView returns the view of the given node, and adds a new view if not present.
func (r *Registry) NodeView(id identity.ID) *View {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.nodesView[id]; !ok {
r.nodesView[id] = &View{
NodeID: id,
Conflicts: make(map[ledgerstate.TransactionID]Entry),
Timestamps: make(map[tangle.MessageID]Entry),
}
}
return r.nodesView[id]
}
// NodesView returns a slice of the views of all nodes.
func (r *Registry) NodesView() []*View {
r.mu.RLock()
defer r.mu.RUnlock()
views := make([]*View, 0, len(r.nodesView))
for _, v := range r.nodesView {
views = append(views, v)
}
return views
}
// Clean deletes all the entries older than the given duration d.
func (r *Registry) Clean(d time.Duration) {
now := clock.SyncedTime()
for _, v := range r.NodesView() {
v.cMutex.Lock()
// loop over the conflicts
for id, c := range v.Conflicts {
if c.Timestamp.Add(d).Before(now) {
delete(v.Conflicts, id)
}
}
v.cMutex.Unlock()
v.tMutex.Lock()
// loop over the timestamps
for id, t := range v.Timestamps {
if t.Timestamp.Add(d).Before(now) {
delete(v.Timestamps, id)
}
}
v.tMutex.Unlock()
}
}
// endregion /////////////////////////////////////////////////////////////////////////////////////////////////////
// region Entry /////////////////////////////////////////////////////////////////////////////////////////////////////
// Entry defines the entry of a registry.
type Entry struct {
Opinions
Timestamp time.Time
}
// endregion /////////////////////////////////////////////////////////////////////////////////////////////////////
// region View /////////////////////////////////////////////////////////////////////////////////////////////////////
// View holds the node's opinion about conflicts and timestamps.
type View struct {
NodeID identity.ID
Conflicts map[ledgerstate.TransactionID]Entry
cMutex sync.RWMutex
Timestamps map[tangle.MessageID]Entry
tMutex sync.RWMutex
LastStatementReceivedTimestamp time.Time
lstMutex sync.RWMutex
}
// AddConflict appends the given conflict to the given view.
func (v *View) AddConflict(c Conflict) {
v.cMutex.Lock()
defer v.cMutex.Unlock()
if _, ok := v.Conflicts[c.ID]; !ok {
v.Conflicts[c.ID] = Entry{
Opinions: Opinions{c.Opinion},
Timestamp: clock.SyncedTime(),
}
return
}
entry := v.Conflicts[c.ID]
entry.Opinions = append(entry.Opinions, c.Opinion)
v.Conflicts[c.ID] = entry
}
// AddConflicts appends the given conflicts to the given view.
func (v *View) AddConflicts(conflicts Conflicts) {
v.cMutex.Lock()
defer v.cMutex.Unlock()
for _, c := range conflicts {
if _, ok := v.Conflicts[c.ID]; !ok {
v.Conflicts[c.ID] = Entry{
Opinions: Opinions{c.Opinion},
Timestamp: clock.SyncedTime(),
}
continue
}
entry := v.Conflicts[c.ID]
entry.Opinions = append(entry.Opinions, c.Opinion)
v.Conflicts[c.ID] = entry
}
}
// AddTimestamp appends the given timestamp to the given view.
func (v *View) AddTimestamp(t Timestamp) {
v.tMutex.Lock()
defer v.tMutex.Unlock()
if _, ok := v.Timestamps[t.ID]; !ok {
v.Timestamps[t.ID] = Entry{
Opinions: Opinions{t.Opinion},
Timestamp: clock.SyncedTime(),
}
return
}
entry := v.Timestamps[t.ID]
entry.Opinions = append(entry.Opinions, t.Opinion)
v.Timestamps[t.ID] = entry
}
// AddTimestamps appends the given timestamps to the given view.
func (v *View) AddTimestamps(timestamps Timestamps) {
v.tMutex.Lock()
defer v.tMutex.Unlock()
for _, t := range timestamps {
if _, ok := v.Timestamps[t.ID]; !ok {
v.Timestamps[t.ID] = Entry{
Opinions: Opinions{t.Opinion},
Timestamp: clock.SyncedTime(),
}
continue
}
entry := v.Timestamps[t.ID]
entry.Opinions = append(entry.Opinions, t.Opinion)
v.Timestamps[t.ID] = entry
}
}
// UpdateLastStatementReceivedTime updates last statement issuing time in node's View.
func (v *View) UpdateLastStatementReceivedTime(statementReceivingTime time.Time) {
v.lstMutex.RLock()
defer v.lstMutex.RUnlock()
v.LastStatementReceivedTimestamp = statementReceivingTime
}
// ConflictOpinion returns the opinion history of a given transaction ID.
func (v *View) ConflictOpinion(id ledgerstate.TransactionID) Opinions {
v.cMutex.RLock()
defer v.cMutex.RUnlock()
if _, ok := v.Conflicts[id]; !ok {
return Opinions{}
}
return v.Conflicts[id].Opinions
}
// TimestampOpinion returns the opinion history of a given message ID.
func (v *View) TimestampOpinion(id tangle.MessageID) Opinions {
v.tMutex.RLock()
defer v.tMutex.RUnlock()
if _, ok := v.Timestamps[id]; !ok {
return Opinions{}
}
return v.Timestamps[id].Opinions
}
// Query retrieves the opinions about the given conflicts and timestamps.
func (v *View) Query(ctx context.Context, conflictIDs []string, timestampIDs []string) (opinion.Opinions, error) {
answer := opinion.Opinions{}
for _, id := range conflictIDs {
ID, err := ledgerstate.TransactionIDFromBase58(id)
if err != nil {
return answer, err
}
o := v.ConflictOpinion(ID)
fetchedOpinion := opinion.Unknown
if len(o) > 0 {
fetchedOpinion = o.Last().Value
}
answer = append(answer, fetchedOpinion)
}
for _, id := range timestampIDs {
ID, err := tangle.NewMessageID(id)
if err != nil {
return answer, err
}
o := v.TimestampOpinion(ID)
fetchedOpinion := opinion.Unknown
if len(o) > 0 {
fetchedOpinion = o.Last().Value
}
answer = append(answer, fetchedOpinion)
}
return answer, nil
}
// ID returns the nodeID of the given view.
func (v *View) ID() identity.ID {
return v.NodeID
}
// endregion /////////////////////////////////////////////////////////////////////////////////////////////////////