-
Notifications
You must be signed in to change notification settings - Fork 661
/
manager.go
313 lines (252 loc) · 7.96 KB
/
manager.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"errors"
"fmt"
"strings"
"sync"
"golang.org/x/exp/maps"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/set"
)
var (
_ Manager = (*manager)(nil)
ErrZeroWeight = errors.New("weight must be non-zero")
ErrMissingValidators = errors.New("missing validators")
)
type SetCallbackListener interface {
OnValidatorAdded(nodeID ids.NodeID, pk *bls.PublicKey, txID ids.ID, weight uint64)
OnValidatorRemoved(nodeID ids.NodeID, weight uint64)
OnValidatorWeightChanged(nodeID ids.NodeID, oldWeight, newWeight uint64)
}
// Manager holds the validator set of each subnet
type Manager interface {
fmt.Stringer
// Add a new staker to the subnet.
// Returns an error if:
// - [weight] is 0
// - [nodeID] is already in the validator set
// If an error is returned, the set will be unmodified.
AddStaker(subnetID ids.ID, nodeID ids.NodeID, pk *bls.PublicKey, txID ids.ID, weight uint64) error
// AddWeight to an existing staker to the subnet.
// Returns an error if:
// - [weight] is 0
// - [nodeID] is not already in the validator set
// If an error is returned, the set will be unmodified.
// AddWeight can result in a total weight that overflows uint64.
// In this case no error will be returned for this call.
// However, the next TotalWeight call will return an error.
AddWeight(subnetID ids.ID, nodeID ids.NodeID, weight uint64) error
// GetWeight retrieves the validator weight from the subnet.
GetWeight(subnetID ids.ID, nodeID ids.NodeID) uint64
// GetValidator returns the validator tied to the specified ID in subnet.
// If the validator doesn't exist, returns false.
GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*Validator, bool)
// GetValidatoIDs returns the validator IDs in the subnet.
GetValidatorIDs(subnetID ids.ID) []ids.NodeID
// SubsetWeight returns the sum of the weights of the validators in the subnet.
// Returns err if subset weight overflows uint64.
SubsetWeight(subnetID ids.ID, validatorIDs set.Set[ids.NodeID]) (uint64, error)
// RemoveWeight from a staker in the subnet. If the staker's weight becomes 0, the staker
// will be removed from the subnet set.
// Returns an error if:
// - [weight] is 0
// - [nodeID] is not already in the subnet set
// - the weight of the validator would become negative
// If an error is returned, the set will be unmodified.
RemoveWeight(subnetID ids.ID, nodeID ids.NodeID, weight uint64) error
// Count returns the number of validators currently in the subnet.
Count(subnetID ids.ID) int
// TotalWeight returns the cumulative weight of all validators in the subnet.
// Returns err if total weight overflows uint64.
TotalWeight(subnetID ids.ID) (uint64, error)
// Sample returns a collection of validatorIDs in the subnet, potentially with duplicates.
// If sampling the requested size isn't possible, an error will be returned.
Sample(subnetID ids.ID, size int) ([]ids.NodeID, error)
// Map of the validators in this subnet
GetMap(subnetID ids.ID) map[ids.NodeID]*GetValidatorOutput
// When a validator's weight changes, or a validator is added/removed,
// this listener is called.
RegisterCallbackListener(subnetID ids.ID, listener SetCallbackListener)
}
// NewManager returns a new, empty manager
func NewManager() Manager {
return &manager{
subnetToVdrs: make(map[ids.ID]*vdrSet),
}
}
type manager struct {
lock sync.RWMutex
// Key: Subnet ID
// Value: The validators that validate the subnet
subnetToVdrs map[ids.ID]*vdrSet
}
func (m *manager) AddStaker(subnetID ids.ID, nodeID ids.NodeID, pk *bls.PublicKey, txID ids.ID, weight uint64) error {
if weight == 0 {
return ErrZeroWeight
}
m.lock.Lock()
defer m.lock.Unlock()
set, exists := m.subnetToVdrs[subnetID]
if !exists {
set = newSet()
m.subnetToVdrs[subnetID] = set
}
return set.Add(nodeID, pk, txID, weight)
}
func (m *manager) AddWeight(subnetID ids.ID, nodeID ids.NodeID, weight uint64) error {
if weight == 0 {
return ErrZeroWeight
}
// We do not need to grab a write lock here because we never modify the
// subnetToVdrs map. However, we must hold the read lock during the entirity
// of this function to ensure that errors are returned consistently.
//
// Consider the case that:
// AddStaker(subnetID, nodeID, 1)
// go func() {
// AddWeight(subnetID, nodeID, 1)
// }
// go func() {
// RemoveWeight(subnetID, nodeID, 1)
// }
//
// In this case, after both goroutines have finished, either AddWeight
// should have errored, or the weight of the node should equal 1. It would
// be unexpected to not have received an error from AddWeight but for the
// node to no longer be tracked as a validator.
m.lock.RLock()
defer m.lock.RUnlock()
set, exists := m.subnetToVdrs[subnetID]
if !exists {
return errMissingValidator
}
return set.AddWeight(nodeID, weight)
}
func (m *manager) GetWeight(subnetID ids.ID, nodeID ids.NodeID) uint64 {
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return 0
}
return set.GetWeight(nodeID)
}
func (m *manager) GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*Validator, bool) {
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return nil, false
}
return set.Get(nodeID)
}
func (m *manager) SubsetWeight(subnetID ids.ID, validatorIDs set.Set[ids.NodeID]) (uint64, error) {
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return 0, nil
}
return set.SubsetWeight(validatorIDs)
}
func (m *manager) RemoveWeight(subnetID ids.ID, nodeID ids.NodeID, weight uint64) error {
if weight == 0 {
return ErrZeroWeight
}
m.lock.Lock()
defer m.lock.Unlock()
set, exists := m.subnetToVdrs[subnetID]
if !exists {
return errMissingValidator
}
if err := set.RemoveWeight(nodeID, weight); err != nil {
return err
}
// If this was the last validator in the subnet and no callback listeners
// are registered, remove the subnet
if set.Len() == 0 && !set.HasCallbackRegistered() {
delete(m.subnetToVdrs, subnetID)
}
return nil
}
func (m *manager) Count(subnetID ids.ID) int {
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return 0
}
return set.Len()
}
func (m *manager) TotalWeight(subnetID ids.ID) (uint64, error) {
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return 0, nil
}
return set.TotalWeight()
}
func (m *manager) Sample(subnetID ids.ID, size int) ([]ids.NodeID, error) {
if size == 0 {
return nil, nil
}
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return nil, ErrMissingValidators
}
return set.Sample(size)
}
func (m *manager) GetMap(subnetID ids.ID) map[ids.NodeID]*GetValidatorOutput {
m.lock.RLock()
set, exists := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exists {
return make(map[ids.NodeID]*GetValidatorOutput)
}
return set.Map()
}
func (m *manager) RegisterCallbackListener(subnetID ids.ID, listener SetCallbackListener) {
m.lock.Lock()
defer m.lock.Unlock()
set, exists := m.subnetToVdrs[subnetID]
if !exists {
set = newSet()
m.subnetToVdrs[subnetID] = set
}
set.RegisterCallbackListener(listener)
}
func (m *manager) String() string {
m.lock.RLock()
defer m.lock.RUnlock()
subnets := maps.Keys(m.subnetToVdrs)
utils.Sort(subnets)
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("Validator Manager: (Size = %d)",
len(subnets),
))
for _, subnetID := range subnets {
vdrs := m.subnetToVdrs[subnetID]
sb.WriteString(fmt.Sprintf(
"\n Subnet[%s]: %s",
subnetID,
vdrs.PrefixedString(" "),
))
}
return sb.String()
}
func (m *manager) GetValidatorIDs(subnetID ids.ID) []ids.NodeID {
m.lock.RLock()
vdrs, exist := m.subnetToVdrs[subnetID]
m.lock.RUnlock()
if !exist {
return nil
}
return vdrs.GetValidatorIDs()
}