-
Notifications
You must be signed in to change notification settings - Fork 669
/
manager.go
415 lines (366 loc) · 12.4 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"context"
"errors"
"fmt"
"time"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/window"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/config"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)
const (
validatorSetsCacheSize = 64
maxRecentlyAcceptedWindowSize = 64
minRecentlyAcceptedWindowSize = 16
recentlyAcceptedWindowTTL = 2 * time.Minute
)
var (
_ validators.State = (*manager)(nil)
ErrMissingValidator = errors.New("missing validator")
ErrMissingValidatorSet = errors.New("missing validator set")
)
// Manager adds the ability to introduce newly acceted blocks IDs to the State
// interface.
type Manager interface {
validators.State
// OnAcceptedBlockID registers the ID of the latest accepted block.
// It is used to update the [recentlyAccepted] sliding window.
OnAcceptedBlockID(blkID ids.ID)
}
type State interface {
GetTx(txID ids.ID) (*txs.Tx, status.Status, error)
GetLastAccepted() ids.ID
GetStatelessBlock(blockID ids.ID) (blocks.Block, error)
// ValidatorSet adds all the validators and delegators of [subnetID] into
// [vdrs].
ValidatorSet(subnetID ids.ID, vdrs validators.Set) error
// ApplyValidatorWeightDiffs iterates from [startHeight] towards the genesis
// block until it has applied all of the diffs up to and including
// [endHeight]. Applying the diffs modifies [validators].
//
// Invariant: If attempting to generate the validator set for
// [endHeight - 1], [validators] must initially contain the validator
// weights for [startHeight].
//
// Note: Because this function iterates towards the genesis, [startHeight]
// should normally be greater than or equal to [endHeight].
ApplyValidatorWeightDiffs(
ctx context.Context,
validators map[ids.NodeID]*validators.GetValidatorOutput,
startHeight uint64,
endHeight uint64,
subnetID ids.ID,
) error
// ApplyValidatorPublicKeyDiffs iterates from [startHeight] towards the
// genesis block until it has applied all of the diffs up to and including
// [endHeight]. Applying the diffs modifies [validators].
//
// Invariant: If attempting to generate the validator set for
// [endHeight - 1], [validators] must initially contain the validator
// weights for [startHeight].
//
// Note: Because this function iterates towards the genesis, [startHeight]
// should normally be greater than or equal to [endHeight].
ApplyValidatorPublicKeyDiffs(
ctx context.Context,
validators map[ids.NodeID]*validators.GetValidatorOutput,
startHeight uint64,
endHeight uint64,
) error
}
func NewManager(
log logging.Logger,
cfg config.Config,
state State,
metrics metrics.Metrics,
clk *mockable.Clock,
) Manager {
return &manager{
log: log,
cfg: cfg,
state: state,
metrics: metrics,
clk: clk,
caches: make(map[ids.ID]cache.Cacher[uint64, map[ids.NodeID]*validators.GetValidatorOutput]),
recentlyAccepted: window.New[ids.ID](
window.Config{
Clock: clk,
MaxSize: maxRecentlyAcceptedWindowSize,
MinSize: minRecentlyAcceptedWindowSize,
TTL: recentlyAcceptedWindowTTL,
},
),
}
}
// TODO: Remove requirement for the P-chain's context lock to be held when
// calling exported functions.
type manager struct {
log logging.Logger
cfg config.Config
state State
metrics metrics.Metrics
clk *mockable.Clock
// Maps caches for each subnet that is currently tracked.
// Key: Subnet ID
// Value: cache mapping height -> validator set map
caches map[ids.ID]cache.Cacher[uint64, map[ids.NodeID]*validators.GetValidatorOutput]
// sliding window of blocks that were recently accepted
recentlyAccepted window.Window[ids.ID]
}
// GetMinimumHeight returns the height of the most recent block beyond the
// horizon of our recentlyAccepted window.
//
// Because the time between blocks is arbitrary, we're only guaranteed that
// the window's configured TTL amount of time has passed once an element
// expires from the window.
//
// To try to always return a block older than the window's TTL, we return the
// parent of the oldest element in the window (as an expired element is always
// guaranteed to be sufficiently stale). If we haven't expired an element yet
// in the case of a process restart, we default to the lastAccepted block's
// height which is likely (but not guaranteed) to also be older than the
// window's configured TTL.
//
// If [UseCurrentHeight] is true, we override the block selection policy
// described above and we will always return the last accepted block height
// as the minimum.
func (m *manager) GetMinimumHeight(ctx context.Context) (uint64, error) {
if m.cfg.UseCurrentHeight {
return m.getCurrentHeight(ctx)
}
oldest, ok := m.recentlyAccepted.Oldest()
if !ok {
return m.getCurrentHeight(ctx)
}
blk, err := m.state.GetStatelessBlock(oldest)
if err != nil {
return 0, err
}
// We subtract 1 from the height of [oldest] because we want the height of
// the last block accepted before the [recentlyAccepted] window.
//
// There is guaranteed to be a block accepted before this window because the
// first block added to [recentlyAccepted] window is >= height 1.
return blk.Height() - 1, nil
}
func (m *manager) GetCurrentHeight(ctx context.Context) (uint64, error) {
return m.getCurrentHeight(ctx)
}
// TODO: Pass the context into the state.
func (m *manager) getCurrentHeight(context.Context) (uint64, error) {
lastAcceptedID := m.state.GetLastAccepted()
lastAccepted, err := m.state.GetStatelessBlock(lastAcceptedID)
if err != nil {
return 0, err
}
return lastAccepted.Height(), nil
}
func (m *manager) GetValidatorSet(
ctx context.Context,
targetHeight uint64,
subnetID ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
validatorSetsCache := m.getValidatorSetCache(subnetID)
if validatorSet, ok := validatorSetsCache.Get(targetHeight); ok {
m.metrics.IncValidatorSetsCached()
return validatorSet, nil
}
// get the start time to track metrics
startTime := m.clk.Time()
var (
validatorSet map[ids.NodeID]*validators.GetValidatorOutput
currentHeight uint64
err error
)
if subnetID == constants.PrimaryNetworkID {
validatorSet, currentHeight, err = m.makePrimaryNetworkValidatorSet(ctx, targetHeight)
} else {
validatorSet, currentHeight, err = m.makeSubnetValidatorSet(ctx, targetHeight, subnetID)
}
if err != nil {
return nil, err
}
// cache the validator set
validatorSetsCache.Put(targetHeight, validatorSet)
duration := m.clk.Time().Sub(startTime)
m.metrics.IncValidatorSetsCreated()
m.metrics.AddValidatorSetsDuration(duration)
m.metrics.AddValidatorSetsHeightDiff(currentHeight - targetHeight)
return validatorSet, nil
}
func (m *manager) getValidatorSetCache(subnetID ids.ID) cache.Cacher[uint64, map[ids.NodeID]*validators.GetValidatorOutput] {
// Only cache tracked subnets
if subnetID != constants.PrimaryNetworkID && !m.cfg.TrackedSubnets.Contains(subnetID) {
return &cache.Empty[uint64, map[ids.NodeID]*validators.GetValidatorOutput]{}
}
validatorSetsCache, exists := m.caches[subnetID]
if exists {
return validatorSetsCache
}
validatorSetsCache = &cache.LRU[uint64, map[ids.NodeID]*validators.GetValidatorOutput]{
Size: validatorSetsCacheSize,
}
m.caches[subnetID] = validatorSetsCache
return validatorSetsCache
}
func (m *manager) makePrimaryNetworkValidatorSet(
ctx context.Context,
targetHeight uint64,
) (map[ids.NodeID]*validators.GetValidatorOutput, uint64, error) {
validatorSet, currentHeight, err := m.getCurrentPrimaryValidatorSet(ctx)
if err != nil {
return nil, 0, err
}
if currentHeight < targetHeight {
return nil, 0, database.ErrNotFound
}
// Rebuild primary network validators at [targetHeight]
//
// Note: Since we are attempting to generate the validator set at
// [targetHeight], we want to apply the diffs from
// (targetHeight, currentHeight]. Because the state interface is implemented
// to be inclusive, we apply diffs in [targetHeight + 1, currentHeight].
lastDiffHeight := targetHeight + 1
err = m.state.ApplyValidatorWeightDiffs(
ctx,
validatorSet,
currentHeight,
lastDiffHeight,
constants.PlatformChainID,
)
if err != nil {
return nil, 0, err
}
err = m.state.ApplyValidatorPublicKeyDiffs(
ctx,
validatorSet,
currentHeight,
lastDiffHeight,
)
return validatorSet, currentHeight, err
}
func (m *manager) getCurrentPrimaryValidatorSet(
ctx context.Context,
) (map[ids.NodeID]*validators.GetValidatorOutput, uint64, error) {
currentValidators, ok := m.cfg.Validators.Get(constants.PrimaryNetworkID)
if !ok {
// This should never happen
m.log.Error(ErrMissingValidatorSet.Error(),
zap.Stringer("subnetID", constants.PrimaryNetworkID),
)
return nil, 0, ErrMissingValidatorSet
}
currentHeight, err := m.getCurrentHeight(ctx)
return currentValidators.Map(), currentHeight, err
}
func (m *manager) makeSubnetValidatorSet(
ctx context.Context,
targetHeight uint64,
subnetID ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, uint64, error) {
subnetValidatorSet, primaryValidatorSet, currentHeight, err := m.getCurrentValidatorSets(ctx, subnetID)
if err != nil {
return nil, 0, err
}
if currentHeight < targetHeight {
return nil, 0, database.ErrNotFound
}
// Rebuild subnet validators at [targetHeight]
//
// Note: Since we are attempting to generate the validator set at
// [targetHeight], we want to apply the diffs from
// (targetHeight, currentHeight]. Because the state interface is implemented
// to be inclusive, we apply diffs in [targetHeight + 1, currentHeight].
lastDiffHeight := targetHeight + 1
err = m.state.ApplyValidatorWeightDiffs(
ctx,
subnetValidatorSet,
currentHeight,
lastDiffHeight,
subnetID,
)
if err != nil {
return nil, 0, err
}
// Update the subnet validator set to include the public keys at
// [currentHeight]. When we apply the public key diffs, we will convert
// these keys to represent the public keys at [targetHeight].
for nodeID, vdr := range subnetValidatorSet {
primaryVdr, ok := primaryValidatorSet[nodeID]
if !ok {
// This should never happen
m.log.Error(ErrMissingValidator.Error(),
zap.Stringer("subnetID", subnetID),
zap.Stringer("nodeID", vdr.NodeID),
)
return nil, 0, ErrMissingValidator
}
vdr.PublicKey = primaryVdr.PublicKey
}
err = m.state.ApplyValidatorPublicKeyDiffs(
ctx,
subnetValidatorSet,
currentHeight,
lastDiffHeight,
)
return subnetValidatorSet, currentHeight, err
}
func (m *manager) getCurrentValidatorSets(
ctx context.Context,
subnetID ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, map[ids.NodeID]*validators.GetValidatorOutput, uint64, error) {
currentSubnetValidators, ok := m.cfg.Validators.Get(subnetID)
if !ok {
// TODO: Require that the current validator set for all subnets is
// included in the validator manager.
currentSubnetValidators = validators.NewSet()
err := m.state.ValidatorSet(subnetID, currentSubnetValidators)
if err != nil {
return nil, nil, 0, err
}
}
currentPrimaryValidators, ok := m.cfg.Validators.Get(constants.PrimaryNetworkID)
if !ok {
// This should never happen
m.log.Error(ErrMissingValidatorSet.Error(),
zap.Stringer("subnetID", constants.PrimaryNetworkID),
)
return nil, nil, 0, ErrMissingValidatorSet
}
currentHeight, err := m.getCurrentHeight(ctx)
return currentSubnetValidators.Map(), currentPrimaryValidators.Map(), currentHeight, err
}
func (m *manager) GetSubnetID(_ context.Context, chainID ids.ID) (ids.ID, error) {
if chainID == constants.PlatformChainID {
return constants.PrimaryNetworkID, nil
}
chainTx, _, err := m.state.GetTx(chainID)
if err != nil {
return ids.Empty, fmt.Errorf(
"problem retrieving blockchain %q: %w",
chainID,
err,
)
}
chain, ok := chainTx.Unsigned.(*txs.CreateChainTx)
if !ok {
return ids.Empty, fmt.Errorf("%q is not a blockchain", chainID)
}
return chain.SubnetID, nil
}
func (m *manager) OnAcceptedBlockID(blkID ids.ID) {
m.recentlyAccepted.Add(blkID)
}