-
Notifications
You must be signed in to change notification settings - Fork 19
/
delegations.go
354 lines (307 loc) · 10.1 KB
/
delegations.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package services
import (
"context"
"math"
"strconv"
"sync"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/subscribers"
pb "code.vegaprotocol.io/vega/protos/vega"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
// next, current and last two.
const maxEpochsToKeep = uint64(4)
type delegationE interface {
events.Event
Proto() eventspb.DelegationBalanceEvent
}
// Delegations is a storage for keeping track of delegation state from delegation balance update events.
type Delegations struct {
*subscribers.Base
ctx context.Context
mut sync.RWMutex
epochToPartyDelegations map[string]map[string]map[string]string // epoch -> party -> node -> amount
ch chan eventspb.DelegationBalanceEvent
minEpoch uint64
}
func NewDelegations(ctx context.Context) (delegations *Delegations) {
defer func() { go delegations.consume() }()
return &Delegations{
Base: subscribers.NewBase(ctx, 1000, true),
ctx: ctx,
epochToPartyDelegations: map[string]map[string]map[string]string{},
ch: make(chan eventspb.DelegationBalanceEvent, 100),
minEpoch: math.MaxUint64,
}
}
func (d *Delegations) consume() {
defer func() { close(d.ch) }()
for {
select {
case <-d.Closed():
return
case de, ok := <-d.ch:
if !ok {
// cleanup base
d.Halt()
// channel is closed
return
}
d.mut.Lock()
d.addDelegation(pb.Delegation{
NodeId: de.NodeId,
Party: de.Party,
EpochSeq: de.EpochSeq,
Amount: de.Amount,
})
d.mut.Unlock()
}
}
}
func (d *Delegations) Push(evts ...events.Event) {
for _, e := range evts {
if ae, ok := e.(delegationE); ok {
d.ch <- ae.Proto()
}
}
}
func (d *Delegations) List(party, node, epoch string) []*pb.Delegation {
d.mut.RLock()
defer d.mut.RUnlock()
var delegations []*pb.Delegation
if epoch == "" && party == "" && node == "" { // all delegations for all parties all nodes across all epochs
delegations = d.getAllDelegations()
} else if epoch == "" && party == "" && node != "" { // all delegations for node from all parties across all epochs
delegations = d.getNodeDelegations(node)
} else if epoch == "" && party != "" && node == "" { // all delegations by a given party to all nodes across all epochs
delegations = d.getPartyDelegations(party)
} else if epoch == "" && party != "" && node != "" { // all delegations by a given party to a given node across all epochs
delegations = d.getPartyNodeDelegations(party, node)
} else if epoch != "" && party == "" && node == "" { // all delegations by all parties for all nodes in a given epoch
delegations = d.getAllDelegationsOnEpoch(epoch)
} else if epoch != "" && party == "" && node != "" { // all delegations to a given node on a given epoch
delegations = d.getNodeDelegationsOnEpoch(node, epoch)
} else if epoch != "" && party != "" && node == "" { // all delegations by a given party on a given epoch
delegations = d.getPartyDelegationsOnEpoch(party, epoch)
} else if epoch != "" && party != "" && node != "" { // all delegations by a given party to a given node on a given epoch
delegations = d.getPartyNodeDelegationsOnEpoch(party, node, epoch)
}
return delegations
}
// clearOldDelegations makes sure we only keep as many as <maxEpochsToKeep> epoch delegations.
func (d *Delegations) clearOldDelegations(epochSeq string) {
epochSeqUint, err := strconv.ParseUint(epochSeq, 10, 64)
if err != nil {
return
}
// if we see an epoch younger than we've seen before - update the min epoch
if epochSeqUint <= d.minEpoch {
d.minEpoch = epochSeqUint
}
// if we haven't seen yet <maxEpochsToKeep> or we have no more than the required number of epochs - we don't have anything to do here
if epochSeqUint < maxEpochsToKeep || d.minEpoch >= (epochSeqUint-maxEpochsToKeep+1) {
return
}
// cleanup enough epochs such that we have at most <maxEpochsToKeep> epochs
for i := d.minEpoch; i < (epochSeqUint - maxEpochsToKeep + 1); i++ {
delete(d.epochToPartyDelegations, strconv.FormatUint(i, 10))
}
d.minEpoch = epochSeqUint - maxEpochsToKeep + 1
}
// AddDelegation is updated with new delegation update from the subscriber.
func (d *Delegations) addDelegation(de pb.Delegation) {
// update party delegations
epoch, ok := d.epochToPartyDelegations[de.EpochSeq]
if !ok {
epoch = map[string]map[string]string{}
d.epochToPartyDelegations[de.EpochSeq] = epoch
d.clearOldDelegations(de.EpochSeq)
}
party, ok := epoch[de.Party]
if !ok {
party = map[string]string{}
epoch[de.Party] = party
}
party[de.NodeId] = de.Amount
}
// GetAllDelegations returns all delegations across all epochs, all parties, all nodes.
func (d *Delegations) getAllDelegations() []*pb.Delegation {
delegations := []*pb.Delegation{}
for epoch, epochDelegations := range d.epochToPartyDelegations {
for party, partyDelegations := range epochDelegations {
for node, amount := range partyDelegations {
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: amount,
EpochSeq: epoch,
})
}
}
}
return delegations
}
// GetAllDelegationsOnEpoch returns all delegation for the given epoch.
func (d *Delegations) getAllDelegationsOnEpoch(epochSeq string) []*pb.Delegation {
delegations := []*pb.Delegation{}
epochDelegations, ok := d.epochToPartyDelegations[epochSeq]
if !ok {
return delegations
}
for party, partyDelegations := range epochDelegations {
for node, amount := range partyDelegations {
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: amount,
EpochSeq: epochSeq,
})
}
}
return delegations
}
// GetNodeDelegations returns all the delegations made to a node across all epochs.
func (d *Delegations) getNodeDelegations(nodeID string) []*pb.Delegation {
delegations := []*pb.Delegation{}
for epoch, epochDelegations := range d.epochToPartyDelegations {
for party, partyDelegations := range epochDelegations {
for node, amount := range partyDelegations {
if node != nodeID {
continue
}
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: amount,
EpochSeq: epoch,
})
}
}
}
return delegations
}
// GetNodeDelegationsOnEpoch returns the delegations to a node by all parties at a given epoch.
func (d *Delegations) getNodeDelegationsOnEpoch(nodeID string, epochSeq string) []*pb.Delegation {
delegations := []*pb.Delegation{}
epochDelegations, ok := d.epochToPartyDelegations[epochSeq]
if !ok {
return delegations
}
for party, partyDelegations := range epochDelegations {
for node, amount := range partyDelegations {
if node != nodeID {
continue
}
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: amount,
EpochSeq: epochSeq,
})
}
}
return delegations
}
// GetPartyDelegations returns all the delegations by a party across all epochs.
func (d *Delegations) getPartyDelegations(party string) []*pb.Delegation {
delegations := []*pb.Delegation{}
for epoch, epochDelegations := range d.epochToPartyDelegations {
partyDelAtEpoch, ok := epochDelegations[party]
if !ok {
continue
}
for node, amount := range partyDelAtEpoch {
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: amount,
EpochSeq: epoch,
})
}
}
return delegations
}
// GetPartyDelegationsOnEpoch returns all delegation by party on a given epoch.
func (d *Delegations) getPartyDelegationsOnEpoch(party string, epochSeq string) []*pb.Delegation {
delegations := []*pb.Delegation{}
epochDelegations, ok := d.epochToPartyDelegations[epochSeq]
if !ok {
return delegations
}
partyDelegations, ok := epochDelegations[party]
if !ok {
return delegations
}
for node, amount := range partyDelegations {
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: amount,
EpochSeq: epochSeq,
})
}
return delegations
}
// GetPartyNodeDelegations returns the delegations from party to node across all epochs.
func (d *Delegations) getPartyNodeDelegations(party string, node string) []*pb.Delegation {
delegations := []*pb.Delegation{}
for epoch, epochDelegations := range d.epochToPartyDelegations {
partyDelegations, ok := epochDelegations[party]
if !ok {
continue
}
nodeDelegations, ok := partyDelegations[node]
if !ok {
continue
}
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: nodeDelegations,
EpochSeq: epoch,
})
}
return delegations
}
// GetPartyNodeDelegationsOnEpoch returns the delegations from party to node at epoch.
func (d *Delegations) getPartyNodeDelegationsOnEpoch(party, node, epochSeq string) []*pb.Delegation {
delegations := []*pb.Delegation{}
epochDelegations, ok := d.epochToPartyDelegations[epochSeq]
if !ok {
return delegations
}
partyDelegations, ok := epochDelegations[party]
if !ok {
return delegations
}
nodeDelegations, ok := partyDelegations[node]
if !ok {
return delegations
}
delegations = append(delegations, &pb.Delegation{
Party: party,
NodeId: node,
Amount: nodeDelegations,
EpochSeq: epochSeq,
})
return delegations
}
func (d *Delegations) Types() []events.Type {
return []events.Type{
events.DelegationBalanceEvent,
}
}