-
Notifications
You must be signed in to change notification settings - Fork 658
/
vm.go
665 lines (560 loc) · 18.2 KB
/
vm.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"errors"
"fmt"
"time"
"github.com/gorilla/rpc/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
safemath "github.com/ava-labs/avalanchego/utils/math"
)
const (
// PercentDenominator is the denominator used to calculate percentages
PercentDenominator = 1000000
droppedTxCacheSize = 64
validatorSetsCacheSize = 64
maxUTXOsToFetch = 1024
// TODO: Turn these constants into governable parameters
// MaxSubMinConsumptionRate is the % consumption that incentivizes staking
// longer
MaxSubMinConsumptionRate = 20000 // 2%
// MinConsumptionRate is the minimum % consumption of the remaining tokens
// to be minted
MinConsumptionRate = 100000 // 10%
// MaxValidatorWeightFactor is the maximum factor of the validator stake
// that is allowed to be placed on a validator.
MaxValidatorWeightFactor uint64 = 5
// SupplyCap is the maximum amount of AVAX that should ever exist
SupplyCap = 720 * units.MegaAvax
// Maximum future start time for staking/delegating
maxFutureStartTime = 24 * 7 * 2 * time.Hour
)
var (
errInvalidID = errors.New("invalid ID")
errDSCantValidate = errors.New("new blockchain can't be validated by primary network")
errStartTimeTooEarly = errors.New("start time is before the current chain time")
errStartAfterEndTime = errors.New("start time is after the end time")
errWrongCacheType = errors.New("unexpectedly cached type")
_ block.ChainVM = &VM{}
_ validators.Connector = &VM{}
_ secp256k1fx.VM = &VM{}
_ Fx = &secp256k1fx.Fx{}
)
// VM implements the snowman.ChainVM interface
type VM struct {
Factory
metrics
avax.AddressManager
avax.AtomicUTXOManager
*network
// Used to get time. Useful for faking time during tests.
clock mockable.Clock
// Used to create and use keys.
factory crypto.FactorySECP256K1R
blockBuilder blockBuilder
uptimeManager uptime.Manager
// The context of this vm
ctx *snow.Context
dbManager manager.Manager
// channel to send messages to the consensus engine
toEngine chan<- common.Message
internalState InternalState
// ID of the preferred block
preferred ids.ID
// ID of the last accepted block
lastAcceptedID ids.ID
fx Fx
codecRegistry codec.Registry
// Bootstrapped remembers if this chain has finished bootstrapping or not
bootstrapped utils.AtomicBool
// Contains the IDs of transactions recently dropped because they failed
// verification. These txs may be re-issued and put into accepted blocks, so
// check the database to see if it was later committed/aborted before
// reporting that it's dropped.
// Key: Tx ID
// Value: String repr. of the verification error
droppedTxCache cache.LRU
// Maps caches for each subnet that is currently whitelisted.
// Key: Subnet ID
// Value: cache mapping height -> validator set map
validatorSetCaches map[ids.ID]cache.Cacher
// Key: block ID
// Value: the block
currentBlocks map[ids.ID]Block
}
// Initialize this blockchain.
// [vm.ChainManager] and [vm.vdrMgr] must be set before this function is called.
func (vm *VM) Initialize(
ctx *snow.Context,
dbManager manager.Manager,
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
msgs chan<- common.Message,
_ []*common.Fx,
appSender common.AppSender,
) error {
ctx.Log.Verbo("initializing platform chain")
registerer := prometheus.NewRegistry()
if err := ctx.Metrics.Register(registerer); err != nil {
return err
}
// Initialize metrics as soon as possible
if err := vm.metrics.Initialize("", registerer); err != nil {
return err
}
// Initialize the utility to parse addresses
vm.AddressManager = avax.NewAddressManager(ctx)
// Initialize the utility to fetch atomic UTXOs
vm.AtomicUTXOManager = avax.NewAtomicUTXOManager(ctx.SharedMemory, Codec)
vm.fx = &secp256k1fx.Fx{}
vm.ctx = ctx
vm.dbManager = dbManager
vm.toEngine = msgs
vm.codecRegistry = linearcodec.NewDefault()
if err := vm.fx.Initialize(vm); err != nil {
return err
}
vm.droppedTxCache = cache.LRU{Size: droppedTxCacheSize}
vm.validatorSetCaches = make(map[ids.ID]cache.Cacher)
vm.currentBlocks = make(map[ids.ID]Block)
if err := vm.blockBuilder.Initialize(vm, registerer); err != nil {
return fmt.Errorf(
"failed to initialize the block builder: %w",
err,
)
}
vm.network = newNetwork(vm.ApricotPhase4Time, appSender, vm)
is, err := NewMeteredInternalState(vm, vm.dbManager.Current().Database, genesisBytes, registerer)
if err != nil {
return err
}
vm.internalState = is
// Initialize the utility to track validator uptimes
vm.uptimeManager = uptime.NewManager(is)
vm.UptimeLockedCalculator.SetCalculator(&vm.bootstrapped, &ctx.Lock, vm.uptimeManager)
if err := vm.updateValidators(); err != nil {
return fmt.Errorf(
"failed to initialize validator sets: %w",
err,
)
}
// Create all of the chains that the database says exist
if err := vm.initBlockchains(); err != nil {
return fmt.Errorf(
"failed to initialize blockchains: %w",
err,
)
}
vm.lastAcceptedID = is.GetLastAccepted()
ctx.Log.Info("initializing last accepted block as %s", vm.lastAcceptedID)
// Build off the most recently accepted block
return vm.SetPreference(vm.lastAcceptedID)
}
// Create all chains that exist that this node validates.
func (vm *VM) initBlockchains() error {
if err := vm.createSubnet(constants.PrimaryNetworkID); err != nil {
return err
}
if vm.StakingEnabled {
for subnetID := range vm.WhitelistedSubnets {
if err := vm.createSubnet(subnetID); err != nil {
return err
}
}
} else {
subnets, err := vm.internalState.GetSubnets()
if err != nil {
return err
}
for _, subnet := range subnets {
if err := vm.createSubnet(subnet.ID()); err != nil {
return err
}
}
}
return nil
}
// Create the subnet with ID [subnetID]
func (vm *VM) createSubnet(subnetID ids.ID) error {
chains, err := vm.internalState.GetChains(subnetID)
if err != nil {
return err
}
for _, chain := range chains {
if err := vm.createChain(chain); err != nil {
return err
}
}
return nil
}
// Create the blockchain described in [tx], but only if this node is a member of
// the subnet that validates the chain
func (vm *VM) createChain(tx *Tx) error {
unsignedTx, ok := tx.UnsignedTx.(*UnsignedCreateChainTx)
if !ok {
return errWrongTxType
}
if vm.StakingEnabled && // Staking is enabled, so nodes might not validate all chains
constants.PrimaryNetworkID != unsignedTx.SubnetID && // All nodes must validate the primary network
!vm.WhitelistedSubnets.Contains(unsignedTx.SubnetID) { // This node doesn't validate this blockchain
return nil
}
chainParams := chains.ChainParameters{
ID: tx.ID(),
SubnetID: unsignedTx.SubnetID,
GenesisData: unsignedTx.GenesisData,
VMAlias: unsignedTx.VMID.String(),
}
for _, fxID := range unsignedTx.FxIDs {
chainParams.FxAliases = append(chainParams.FxAliases, fxID.String())
}
vm.Chains.CreateChain(chainParams)
return nil
}
// Bootstrapping marks this VM as bootstrapping
func (vm *VM) Bootstrapping() error {
vm.bootstrapped.SetValue(false)
return vm.fx.Bootstrapping()
}
// Bootstrapped marks this VM as bootstrapped
func (vm *VM) Bootstrapped() error {
if vm.bootstrapped.GetValue() {
return nil
}
vm.bootstrapped.SetValue(true)
if err := vm.fx.Bootstrapped(); err != nil {
return err
}
primaryValidatorSet, exist := vm.Validators.GetValidators(constants.PrimaryNetworkID)
if !exist {
return errNoPrimaryValidators
}
primaryValidators := primaryValidatorSet.List()
validatorIDs := make([]ids.ShortID, len(primaryValidators))
for i, vdr := range primaryValidators {
validatorIDs[i] = vdr.ID()
}
if err := vm.uptimeManager.StartTracking(validatorIDs); err != nil {
return err
}
return vm.internalState.Commit()
}
// Shutdown this blockchain
func (vm *VM) Shutdown() error {
if vm.dbManager == nil {
return nil
}
vm.blockBuilder.Shutdown()
if vm.bootstrapped.GetValue() {
primaryValidatorSet, exist := vm.Validators.GetValidators(constants.PrimaryNetworkID)
if !exist {
return errNoPrimaryValidators
}
primaryValidators := primaryValidatorSet.List()
validatorIDs := make([]ids.ShortID, len(primaryValidators))
for i, vdr := range primaryValidators {
validatorIDs[i] = vdr.ID()
}
if err := vm.uptimeManager.Shutdown(validatorIDs); err != nil {
return err
}
if err := vm.internalState.Commit(); err != nil {
return err
}
}
errs := wrappers.Errs{}
errs.Add(
vm.internalState.Close(),
vm.dbManager.Close(),
)
return errs.Err
}
// BuildBlock builds a block to be added to consensus
func (vm *VM) BuildBlock() (snowman.Block, error) { return vm.blockBuilder.BuildBlock() }
// ParseBlock implements the snowman.ChainVM interface
func (vm *VM) ParseBlock(b []byte) (snowman.Block, error) {
var blk Block
if _, err := Codec.Unmarshal(b, &blk); err != nil {
return nil, err
}
if err := blk.initialize(vm, b, choices.Processing, blk); err != nil {
return nil, err
}
// TODO: remove this to make ParseBlock stateless
if block, err := vm.GetBlock(blk.ID()); err == nil {
// If we have seen this block before, return it with the most up-to-date
// info
return block, nil
}
return blk, nil
}
// GetBlock implements the snowman.ChainVM interface
func (vm *VM) GetBlock(blkID ids.ID) (snowman.Block, error) { return vm.getBlock(blkID) }
func (vm *VM) getBlock(blkID ids.ID) (Block, error) {
// If block is in memory, return it.
if blk, exists := vm.currentBlocks[blkID]; exists {
return blk, nil
}
return vm.internalState.GetBlock(blkID)
}
// LastAccepted returns the block most recently accepted
func (vm *VM) LastAccepted() (ids.ID, error) {
return vm.lastAcceptedID, nil
}
// SetPreference sets the preferred block to be the one with ID [blkID]
func (vm *VM) SetPreference(blkID ids.ID) error {
if blkID == vm.preferred {
// If the preference didn't change, then this is a noop
return nil
}
vm.preferred = blkID
vm.blockBuilder.ResetTimer()
return nil
}
func (vm *VM) Preferred() (Block, error) {
return vm.getBlock(vm.preferred)
}
// NotifyBlockReady tells the consensus engine that a new block is ready to be
// created
func (vm *VM) NotifyBlockReady() {
select {
case vm.toEngine <- common.PendingTxs:
default:
vm.ctx.Log.Debug("dropping message to consensus engine")
}
}
func (vm *VM) Version() (string, error) {
return version.Current.String(), nil
}
// CreateHandlers returns a map where:
// * keys are API endpoint extensions
// * values are API handlers
func (vm *VM) CreateHandlers() (map[string]*common.HTTPHandler, error) {
server := rpc.NewServer()
server.RegisterCodec(json.NewCodec(), "application/json")
server.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
server.RegisterInterceptFunc(vm.metrics.apiRequestMetrics.InterceptRequest)
server.RegisterAfterFunc(vm.metrics.apiRequestMetrics.AfterRequest)
if err := server.RegisterService(&Service{vm: vm}, "platform"); err != nil {
return nil, err
}
return map[string]*common.HTTPHandler{
"": {
Handler: server,
},
}, nil
}
// CreateStaticHandlers returns a map where:
// * keys are API endpoint extensions
// * values are API handlers
func (vm *VM) CreateStaticHandlers() (map[string]*common.HTTPHandler, error) {
server := rpc.NewServer()
server.RegisterCodec(json.NewCodec(), "application/json")
server.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
if err := server.RegisterService(&StaticService{}, "platform"); err != nil {
return nil, err
}
return map[string]*common.HTTPHandler{
"": {
LockOptions: common.NoLock,
Handler: server,
},
}, nil
}
// Connected implements validators.Connector
func (vm *VM) Connected(vdrID ids.ShortID) error {
return vm.uptimeManager.Connect(vdrID)
}
// Disconnected implements validators.Connector
func (vm *VM) Disconnected(vdrID ids.ShortID) error {
if err := vm.uptimeManager.Disconnect(vdrID); err != nil {
return err
}
return vm.internalState.Commit()
}
// GetValidatorSet returns the validator set at the specified height for the
// provided subnetID.
func (vm *VM) GetValidatorSet(height uint64, subnetID ids.ID) (map[ids.ShortID]uint64, error) {
validatorSetsCache, exists := vm.validatorSetCaches[subnetID]
if !exists {
validatorSetsCache = &cache.LRU{Size: validatorSetsCacheSize}
// Only cache whitelisted subnets
if vm.WhitelistedSubnets.Contains(subnetID) || subnetID == constants.PrimaryNetworkID {
vm.validatorSetCaches[subnetID] = validatorSetsCache
}
}
if validatorSetIntf, ok := validatorSetsCache.Get(height); ok {
validatorSet, ok := validatorSetIntf.(map[ids.ShortID]uint64)
if !ok {
return nil, errWrongCacheType
}
vm.metrics.validatorSetsCached.Inc()
return validatorSet, nil
}
lastAcceptedHeight, err := vm.GetCurrentHeight()
if err != nil {
return nil, err
}
if lastAcceptedHeight < height {
return nil, database.ErrNotFound
}
// get the start time to track metrics
startTime := vm.Clock().Time()
currentValidators, ok := vm.Validators.GetValidators(subnetID)
if !ok {
return nil, errNotEnoughValidators
}
currentValidatorList := currentValidators.List()
vdrSet := make(map[ids.ShortID]uint64, len(currentValidatorList))
for _, vdr := range currentValidatorList {
vdrSet[vdr.ID()] = vdr.Weight()
}
for i := lastAcceptedHeight; i > height; i-- {
diffs, err := vm.internalState.GetValidatorWeightDiffs(i, subnetID)
if err != nil {
return nil, err
}
for nodeID, diff := range diffs {
var op func(uint64, uint64) (uint64, error)
if diff.Decrease {
// The validator's weight was decreased at this block, so in the
// prior block it was higher.
op = safemath.Add64
} else {
// The validator's weight was increased at this block, so in the
// prior block it was lower.
op = safemath.Sub64
}
newWeight, err := op(vdrSet[nodeID], diff.Amount)
if err != nil {
return nil, err
}
if newWeight == 0 {
delete(vdrSet, nodeID)
} else {
vdrSet[nodeID] = newWeight
}
}
}
// cache the validator set
validatorSetsCache.Put(height, vdrSet)
endTime := vm.Clock().Time()
vm.metrics.validatorSetsCreated.Inc()
vm.metrics.validatorSetsDuration.Add(float64(endTime.Sub(startTime)))
vm.metrics.validatorSetsHeightDiff.Add(float64(lastAcceptedHeight - height))
return vdrSet, nil
}
// GetCurrentHeight returns the height of the last accepted block
func (vm *VM) GetCurrentHeight() (uint64, error) {
lastAccepted, err := vm.getBlock(vm.lastAcceptedID)
if err != nil {
return 0, err
}
return lastAccepted.Height(), nil
}
func (vm *VM) updateValidators() error {
currentValidators := vm.internalState.CurrentStakerChainState()
primaryValidators, err := currentValidators.ValidatorSet(constants.PrimaryNetworkID)
if err != nil {
return err
}
if err := vm.Validators.Set(constants.PrimaryNetworkID, primaryValidators); err != nil {
return err
}
weight, _ := primaryValidators.GetWeight(vm.ctx.NodeID)
vm.localStake.Set(float64(weight))
vm.totalStake.Set(float64(primaryValidators.Weight()))
for subnetID := range vm.WhitelistedSubnets {
subnetValidators, err := currentValidators.ValidatorSet(subnetID)
if err != nil {
return err
}
if err := vm.Validators.Set(subnetID, subnetValidators); err != nil {
return err
}
}
return nil
}
// Returns the time when the next staker of any subnet starts/stops staking
// after the current timestamp
func (vm *VM) nextStakerChangeTime(vs ValidatorState) (time.Time, error) {
currentStakers := vs.CurrentStakerChainState()
pendingStakers := vs.PendingStakerChainState()
earliest := mockable.MaxTime
if currentStakers := currentStakers.Stakers(); len(currentStakers) > 0 {
nextStakerToRemove := currentStakers[0]
staker, ok := nextStakerToRemove.UnsignedTx.(TimedTx)
if !ok {
return time.Time{}, errWrongTxType
}
endTime := staker.EndTime()
if endTime.Before(earliest) {
earliest = endTime
}
}
if pendingStakers := pendingStakers.Stakers(); len(pendingStakers) > 0 {
nextStakerToAdd := pendingStakers[0]
staker, ok := nextStakerToAdd.UnsignedTx.(TimedTx)
if !ok {
return time.Time{}, errWrongTxType
}
startTime := staker.StartTime()
if startTime.Before(earliest) {
earliest = startTime
}
}
return earliest, nil
}
func (vm *VM) CodecRegistry() codec.Registry { return vm.codecRegistry }
func (vm *VM) Clock() *mockable.Clock { return &vm.clock }
func (vm *VM) Logger() logging.Logger { return vm.ctx.Log }
// Returns the percentage of the total stake on the Primary Network of nodes
// connected to this node.
func (vm *VM) getPercentConnected() (float64, error) {
vdrSet, exists := vm.Validators.GetValidators(constants.PrimaryNetworkID)
if !exists {
return 0, errNoPrimaryValidators
}
vdrs := vdrSet.List()
var (
connectedStake uint64
err error
)
for _, vdr := range vdrs {
if !vm.uptimeManager.IsConnected(vdr.ID()) {
continue // not connected to us --> don't include
}
connectedStake, err = safemath.Add64(connectedStake, vdr.Weight())
if err != nil {
return 0, err
}
}
return float64(connectedStake) / float64(vdrSet.Weight()), nil
}