forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
868 lines (756 loc) · 26.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chains
import (
"crypto"
"crypto/tls"
"errors"
"fmt"
"sync"
"time"
"github.com/ava-labs/avalanchego/api/health"
"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/api/server"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/message"
"github.com/ava-labs/avalanchego/network"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/consensus/snowball"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/state"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/common/queue"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/networking/router"
"github.com/ava-labs/avalanchego/snow/networking/sender"
"github.com/ava-labs/avalanchego/snow/networking/timeout"
"github.com/ava-labs/avalanchego/snow/triggers"
"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/vms"
"github.com/ava-labs/avalanchego/vms/metervm"
"github.com/ava-labs/avalanchego/vms/proposervm"
dbManager "github.com/ava-labs/avalanchego/database/manager"
avcon "github.com/ava-labs/avalanchego/snow/consensus/avalanche"
aveng "github.com/ava-labs/avalanchego/snow/engine/avalanche"
avbootstrap "github.com/ava-labs/avalanchego/snow/engine/avalanche/bootstrap"
smcon "github.com/ava-labs/avalanchego/snow/consensus/snowman"
smeng "github.com/ava-labs/avalanchego/snow/engine/snowman"
smbootstrap "github.com/ava-labs/avalanchego/snow/engine/snowman/bootstrap"
)
const (
defaultChannelSize = 1
)
var (
errUnknownChainID = errors.New("unknown chain ID")
errUnknownVMType = errors.New("the vm should have type avalanche.DAGVM or snowman.ChainVM")
_ Manager = &manager{}
)
// Manager manages the chains running on this node.
// It can:
// * Create a chain
// * Add a registrant. When a chain is created, each registrant calls
// RegisterChain with the new chain as the argument.
// * Manage the aliases of chains
type Manager interface {
ids.Aliaser
// Return the router this Manager is using to route consensus messages to chains
Router() router.Router
// Create a chain in the future
CreateChain(ChainParameters)
// Create a chain now
ForceCreateChain(ChainParameters)
// Add a registrant [r]. Every time a chain is
// created, [r].RegisterChain([new chain]) is called.
AddRegistrant(Registrant)
// Given an alias, return the ID of the chain associated with that alias
Lookup(string) (ids.ID, error)
// Given an alias, return the ID of the VM associated with that alias
LookupVM(string) (ids.ID, error)
// Returns the ID of the subnet that is validating the provided chain
SubnetID(chainID ids.ID) (ids.ID, error)
// Returns true iff the chain with the given ID exists and is finished bootstrapping
IsBootstrapped(ids.ID) bool
Shutdown()
}
// ChainParameters defines the chain being created
type ChainParameters struct {
ID ids.ID // The ID of the chain being created
SubnetID ids.ID // ID of the subnet that validates this chain
GenesisData []byte // The genesis data of this chain's ledger
VMAlias string // The ID of the vm this chain is running
FxAliases []string // The IDs of the feature extensions this chain is running
CustomBeacons validators.Set // Should only be set if the default beacons can't be used.
}
type chain struct {
Name string
Engine common.Engine
Handler *router.Handler
Ctx *snow.Context
Beacons validators.Set
}
// ChainConfig is configuration settings for the current execution.
// [Config] is the user-provided config blob for the chain.
// [Upgrade] is a chain-specific blob for coordinating upgrades.
type ChainConfig struct {
Config []byte
Upgrade []byte
}
type ManagerConfig struct {
StakingEnabled bool // True iff the network has staking enabled
StakingCert tls.Certificate // needed to sign snowman++ blocks
Log logging.Logger
LogFactory logging.Factory
VMManager vms.Manager // Manage mappings from vm ID --> vm
DecisionEvents *triggers.EventDispatcher
ConsensusEvents *triggers.EventDispatcher
DBManager dbManager.Manager
MsgCreator message.Creator // message creator, shared with network
Router router.Router // Routes incoming messages to the appropriate chain
Net network.Network // Sends consensus messages to other validators
ConsensusParams avcon.Parameters // The consensus parameters (alpha, beta, etc.) for new chains
EpochFirstTransition time.Time
EpochDuration time.Duration
Validators validators.Manager // Validators validating on this chain
NodeID ids.ShortID // The ID of this node
NetworkID uint32 // ID of the network this node is connected to
Server *server.Server // Handles HTTP API calls
Keystore keystore.Keystore
AtomicMemory *atomic.Memory
AVAXAssetID ids.ID
XChainID ids.ID
CriticalChains ids.Set // Chains that can't exit gracefully
WhitelistedSubnets ids.Set // Subnets to validate
TimeoutManager *timeout.Manager // Manages request timeouts when sending messages to other validators
HealthService health.Service
RetryBootstrap bool // Should Bootstrap be retried
RetryBootstrapWarnFrequency int // Max number of times to retry bootstrap before warning the node operator
SubnetConfigs map[ids.ID]SubnetConfig // ID -> SubnetConfig
ChainConfigs map[string]ChainConfig // alias -> ChainConfig
// ShutdownNodeFunc allows the chain manager to issue a request to shutdown the node
ShutdownNodeFunc func(exitCode int)
MeterVMEnabled bool // Should each VM be wrapped with a MeterVM
AppGossipValidatorSize int
AppGossipNonValidatorSize int
GossipAcceptedFrontierSize int
// Max Time to spend fetching a container and its
// ancestors when responding to a GetAncestors
BootstrapMaxTimeGetAncestors time.Duration
// Max number of containers in a multiput message sent by this node.
BootstrapMultiputMaxContainersSent int
// This node will only consider the first [MultiputMaxContainersReceived]
// containers in a multiput it receives.
BootstrapMultiputMaxContainersReceived int
ApricotPhase4Time time.Time
ApricotPhase4MinPChainHeight uint64
}
type manager struct {
// Note: The string representation of a chain's ID is also considered to be an alias of the chain
// That is, [chainID].String() is an alias for the chain, too
ids.Aliaser
ManagerConfig
// Those notified when a chain is created
registrants []Registrant
unblocked bool
blockedChains []ChainParameters
// Key: Subnet's ID
// Value: Subnet description
subnets map[ids.ID]Subnet
chainsLock sync.Mutex
// Key: Chain's ID
// Value: The chain
chains map[ids.ID]*router.Handler
// snowman++ related interface to allow validators retrival
validatorState validators.State
}
// New returns a new Manager
func New(config *ManagerConfig) Manager {
return &manager{
Aliaser: ids.NewAliaser(),
ManagerConfig: *config,
subnets: make(map[ids.ID]Subnet),
chains: make(map[ids.ID]*router.Handler),
}
}
// Router that this chain manager is using to route consensus messages to chains
func (m *manager) Router() router.Router { return m.ManagerConfig.Router }
// Create a chain
func (m *manager) CreateChain(chain ChainParameters) {
if !m.unblocked {
m.blockedChains = append(m.blockedChains, chain)
} else {
m.ForceCreateChain(chain)
}
}
// Create a chain, this is only called from the P-chain thread, except for
// creating the P-chain.
func (m *manager) ForceCreateChain(chainParams ChainParameters) {
if m.StakingEnabled && chainParams.SubnetID != constants.PrimaryNetworkID && !m.WhitelistedSubnets.Contains(chainParams.SubnetID) {
m.Log.Debug("Skipped creating non-whitelisted chain:\n"+
" ID: %s\n"+
" VMID:%s",
chainParams.ID,
chainParams.VMAlias,
)
return
}
// Assert that there isn't already a chain with an alias in [chain].Aliases
// (Recall that the string representation of a chain's ID is also an alias
// for a chain)
if alias, isRepeat := m.isChainWithAlias(chainParams.ID.String()); isRepeat {
m.Log.Debug("there is already a chain with alias '%s'. Chain not created.",
alias)
return
}
m.Log.Info("creating chain:\n"+
" ID: %s\n"+
" VMID:%s",
chainParams.ID,
chainParams.VMAlias,
)
sb, exists := m.subnets[chainParams.SubnetID]
if !exists {
sb = newSubnet()
m.subnets[chainParams.SubnetID] = sb
}
sb.addChain(chainParams.ID)
chain, err := m.buildChain(chainParams, sb)
if err != nil {
sb.removeChain(chainParams.ID)
if m.CriticalChains.Contains(chainParams.ID) {
// Shut down if we fail to create a required chain (i.e. X, P or C)
m.Log.Fatal("error creating required chain %s: %s", chainParams.ID, err)
go m.ShutdownNodeFunc(1)
return
}
m.Log.Error("error creating chain %s: %s", chainParams.ID, err)
return
}
m.chainsLock.Lock()
m.chains[chainParams.ID] = chain.Handler
m.chainsLock.Unlock()
// Associate the newly created chain with its default alias
m.Log.AssertNoError(m.Alias(chainParams.ID, chainParams.ID.String()))
// Notify those that registered to be notified when a new chain is created
m.notifyRegistrants(chain.Name, chain.Ctx, chain.Engine)
// Tell the chain to start processing messages.
// If the X or P Chain panics, do not attempt to recover
if m.CriticalChains.Contains(chainParams.ID) {
go chain.Ctx.Log.RecoverAndPanic(chain.Handler.Dispatch)
} else {
go chain.Ctx.Log.RecoverAndExit(chain.Handler.Dispatch, func() {
chain.Ctx.Log.Error("Chain with ID: %s was shutdown due to a panic", chainParams.ID)
})
}
// Allows messages to be routed to the new chain
m.ManagerConfig.Router.AddChain(chain.Handler)
}
// Create a chain
func (m *manager) buildChain(chainParams ChainParameters, sb Subnet) (*chain, error) {
vmID, err := m.VMManager.Lookup(chainParams.VMAlias)
if err != nil {
return nil, fmt.Errorf("error while looking up VM: %w", err)
}
primaryAlias, err := m.PrimaryAlias(chainParams.ID)
if err != nil {
primaryAlias = chainParams.ID.String()
}
// Create the log and context of the chain
chainLog, err := m.LogFactory.MakeChain(primaryAlias)
if err != nil {
return nil, fmt.Errorf("error while creating chain's log %w", err)
}
ctx := &snow.Context{
NetworkID: m.NetworkID,
SubnetID: chainParams.SubnetID,
ChainID: chainParams.ID,
NodeID: m.NodeID,
XChainID: m.XChainID,
AVAXAssetID: m.AVAXAssetID,
Log: chainLog,
DecisionDispatcher: m.DecisionEvents,
ConsensusDispatcher: m.ConsensusEvents,
Keystore: m.Keystore.NewBlockchainKeyStore(chainParams.ID),
SharedMemory: m.AtomicMemory.NewSharedMemory(chainParams.ID),
BCLookup: m,
SNLookup: m,
Namespace: fmt.Sprintf("%s_%s_vm", constants.PlatformName, primaryAlias),
Metrics: m.ConsensusParams.Metrics,
EpochFirstTransition: m.EpochFirstTransition,
EpochDuration: m.EpochDuration,
ValidatorState: m.validatorState,
StakingCertLeaf: m.StakingCert.Leaf,
StakingLeafSigner: m.StakingCert.PrivateKey.(crypto.Signer),
}
if sbConfigs, ok := m.SubnetConfigs[chainParams.SubnetID]; ok {
if sbConfigs.ValidatorOnly {
ctx.SetValidatorOnly()
}
}
// Get a factory for the vm we want to use on our chain
vmFactory, err := m.VMManager.GetFactory(vmID)
if err != nil {
return nil, fmt.Errorf("error while getting vmFactory: %w", err)
}
// Create the chain
vm, err := vmFactory.New(ctx)
if err != nil {
return nil, fmt.Errorf("error while creating vm: %w", err)
}
// TODO: Shutdown VM if an error occurs
fxs := make([]*common.Fx, len(chainParams.FxAliases))
for i, fxAlias := range chainParams.FxAliases {
fxID, err := m.VMManager.Lookup(fxAlias)
if err != nil {
return nil, fmt.Errorf("error while looking up Fx: %w", err)
}
// Get a factory for the fx we want to use on our chain
fxFactory, err := m.VMManager.GetFactory(fxID)
if err != nil {
return nil, fmt.Errorf("error while getting fxFactory: %w", err)
}
fx, err := fxFactory.New(ctx)
if err != nil {
return nil, fmt.Errorf("error while creating fx: %w", err)
}
// Create the fx
fxs[i] = &common.Fx{
ID: fxID,
Fx: fx,
}
}
consensusParams := m.ConsensusParams
if sbConfigs, ok := m.SubnetConfigs[chainParams.SubnetID]; ok && chainParams.SubnetID != constants.PrimaryNetworkID {
consensusParams = sbConfigs.ConsensusParameters
// TODO: move metrics to another place so this can be tidier
consensusParams.Metrics = m.ConsensusParams.Metrics
}
consensusParams.Namespace = fmt.Sprintf("%s_%s", constants.PlatformName, primaryAlias)
// The validators of this blockchain
var vdrs validators.Set // Validators validating this blockchain
var ok bool
if m.StakingEnabled {
vdrs, ok = m.Validators.GetValidators(chainParams.SubnetID)
} else { // Staking is disabled. Every peer validates every subnet.
vdrs, ok = m.Validators.GetValidators(constants.PrimaryNetworkID)
}
if !ok {
return nil, fmt.Errorf("couldn't get validator set of subnet with ID %s. The subnet may not exist", chainParams.SubnetID)
}
beacons := vdrs
if chainParams.CustomBeacons != nil {
beacons = chainParams.CustomBeacons
}
bootstrapWeight := beacons.Weight()
var chain *chain
switch vm := vm.(type) {
case vertex.DAGVM:
chain, err = m.createAvalancheChain(
ctx,
chainParams.GenesisData,
vdrs,
beacons,
vm,
fxs,
consensusParams,
bootstrapWeight,
sb,
)
if err != nil {
return nil, fmt.Errorf("error while creating new avalanche vm %w", err)
}
case block.ChainVM:
chain, err = m.createSnowmanChain(
ctx,
chainParams.GenesisData,
vdrs,
beacons,
vm,
fxs,
consensusParams.Parameters,
bootstrapWeight,
sb,
)
if err != nil {
return nil, fmt.Errorf("error while creating new snowman vm %w", err)
}
default:
return nil, errUnknownVMType
}
// Register the chain with the timeout manager
if err := m.TimeoutManager.RegisterChain(ctx, consensusParams.Namespace); err != nil {
return nil, err
}
return chain, nil
}
// Implements Manager.AddRegistrant
func (m *manager) AddRegistrant(r Registrant) { m.registrants = append(m.registrants, r) }
func (m *manager) unblockChains() {
m.unblocked = true
blocked := m.blockedChains
m.blockedChains = nil
for _, chainParams := range blocked {
m.ForceCreateChain(chainParams)
}
}
// Create a DAG-based blockchain that uses Avalanche
func (m *manager) createAvalancheChain(
ctx *snow.Context,
genesisData []byte,
vdrs,
beacons validators.Set,
vm vertex.DAGVM,
fxs []*common.Fx,
consensusParams avcon.Parameters,
bootstrapWeight uint64,
sb Subnet,
) (*chain, error) {
ctx.Lock.Lock()
defer ctx.Lock.Unlock()
meterDBManager, err := m.DBManager.NewMeterDBManager(consensusParams.Namespace+"_db", ctx.Metrics)
if err != nil {
return nil, err
}
prefixDBManager := meterDBManager.NewPrefixDBManager(ctx.ChainID[:])
vmDBManager := prefixDBManager.NewPrefixDBManager([]byte("vm"))
db := prefixDBManager.Current()
vertexDB := prefixdb.New([]byte("vertex"), db.Database)
vertexBootstrappingDB := prefixdb.New([]byte("vertex_bs"), db.Database)
txBootstrappingDB := prefixdb.New([]byte("tx_bs"), db.Database)
vtxBlocker, err := queue.NewWithMissing(vertexBootstrappingDB, consensusParams.Namespace+"_vtx", ctx.Metrics)
if err != nil {
return nil, err
}
txBlocker, err := queue.New(txBootstrappingDB, consensusParams.Namespace+"_tx", ctx.Metrics)
if err != nil {
return nil, err
}
// The channel through which a VM may send messages to the consensus engine
// VM uses this channel to notify engine that a block is ready to be made
msgChan := make(chan common.Message, defaultChannelSize)
// Passes messages from the consensus engine to the network
sender := sender.Sender{}
if err := sender.Initialize(
ctx,
m.MsgCreator,
m.Net,
m.ManagerConfig.Router,
m.TimeoutManager,
consensusParams.Namespace,
consensusParams.Metrics,
m.AppGossipValidatorSize,
m.AppGossipNonValidatorSize,
m.GossipAcceptedFrontierSize,
); err != nil {
return nil, fmt.Errorf("couldn't initialize sender: %w", err)
}
chainConfig, err := m.getChainConfig(ctx.ChainID)
if err != nil {
return nil, fmt.Errorf("error while fetching chain config: %w", err)
}
if m.MeterVMEnabled {
vm = metervm.NewVertexVM(vm)
}
if err := vm.Initialize(
ctx,
vmDBManager,
genesisData,
chainConfig.Upgrade,
chainConfig.Config,
msgChan,
fxs,
&sender,
); err != nil {
return nil, fmt.Errorf("error during vm's Initialize: %w", err)
}
// Handles serialization/deserialization of vertices and also the
// persistence of vertices
vtxManager := &state.Serializer{}
vtxManager.Initialize(ctx, vm, vertexDB)
sampleK := consensusParams.K
if uint64(sampleK) > bootstrapWeight {
sampleK = int(bootstrapWeight)
}
// Asynchronously passes messages from the network to the consensus engine
handler := &router.Handler{}
timer := &router.Timer{
Handler: handler,
Preempt: sb.afterBootstrapped(),
}
// The engine handles consensus
engine := &aveng.Transitive{}
if err := engine.Initialize(aveng.Config{
Config: avbootstrap.Config{
Config: common.Config{
Ctx: ctx,
Validators: vdrs,
Beacons: beacons,
SampleK: sampleK,
StartupAlpha: (3*bootstrapWeight + 3) / 4,
Alpha: bootstrapWeight/2 + 1, // must be > 50%
Sender: &sender,
Subnet: sb,
Timer: timer,
RetryBootstrap: m.RetryBootstrap,
RetryBootstrapWarnFrequency: m.RetryBootstrapWarnFrequency,
MaxTimeGetAncestors: m.BootstrapMaxTimeGetAncestors,
MultiputMaxContainersSent: m.BootstrapMultiputMaxContainersSent,
MultiputMaxContainersReceived: m.BootstrapMultiputMaxContainersReceived,
},
VtxBlocked: vtxBlocker,
TxBlocked: txBlocker,
Manager: vtxManager,
VM: vm,
},
Params: consensusParams,
Consensus: &avcon.Topological{},
}); err != nil {
return nil, fmt.Errorf("error initializing avalanche engine: %w", err)
}
// Register health check for this chain
chainAlias, err := m.PrimaryAlias(ctx.ChainID)
if err != nil {
chainAlias = ctx.ChainID.String()
}
// Grab the context lock before calling the chain's health check
checkFn := func() (interface{}, error) {
ctx.Lock.Lock()
defer ctx.Lock.Unlock()
return engine.HealthCheck()
}
if err := m.HealthService.RegisterCheck(chainAlias, checkFn); err != nil {
return nil, fmt.Errorf("couldn't add health check for chain %s: %w", chainAlias, err)
}
err = handler.Initialize(
m.MsgCreator,
engine,
vdrs,
msgChan,
fmt.Sprintf("%s_handler", consensusParams.Namespace),
consensusParams.Metrics,
)
return &chain{
Name: chainAlias,
Engine: engine,
Handler: handler,
Ctx: ctx,
}, err
}
// Create a linear chain using the Snowman consensus engine
func (m *manager) createSnowmanChain(
ctx *snow.Context,
genesisData []byte,
vdrs,
beacons validators.Set,
vm block.ChainVM,
fxs []*common.Fx,
consensusParams snowball.Parameters,
bootstrapWeight uint64,
sb Subnet,
) (*chain, error) {
ctx.Lock.Lock()
defer ctx.Lock.Unlock()
meterDBManager, err := m.DBManager.NewMeterDBManager(consensusParams.Namespace+"_db", ctx.Metrics)
if err != nil {
return nil, err
}
prefixDBManager := meterDBManager.NewPrefixDBManager(ctx.ChainID[:])
vmDBManager := prefixDBManager.NewPrefixDBManager([]byte("vm"))
db := prefixDBManager.Current()
bootstrappingDB := prefixdb.New([]byte("bs"), db.Database)
blocked, err := queue.NewWithMissing(bootstrappingDB, consensusParams.Namespace+"_block", ctx.Metrics)
if err != nil {
return nil, err
}
// The channel through which a VM may send messages to the consensus engine
// VM uses this channel to notify engine that a block is ready to be made
msgChan := make(chan common.Message, defaultChannelSize)
// Passes messages from the consensus engine to the network
sender := sender.Sender{}
if err := sender.Initialize(
ctx,
m.MsgCreator,
m.Net,
m.ManagerConfig.Router,
m.TimeoutManager,
consensusParams.Namespace,
consensusParams.Metrics,
m.AppGossipValidatorSize,
m.AppGossipNonValidatorSize,
m.GossipAcceptedFrontierSize,
); err != nil {
return nil, fmt.Errorf("couldn't initialize sender: %w", err)
}
// first vm to be init is P-Chain once, which provides validator interface to all ProposerVMs
if m.validatorState == nil {
if m.ManagerConfig.StakingEnabled {
valState, ok := vm.(validators.State)
if !ok {
return nil, fmt.Errorf("expected validators.State but got %T", vm)
}
// Initialize the validator state for future chains.
m.validatorState = validators.NewLockedState(&ctx.Lock, valState)
// Notice that this context is left unlocked. This is because the
// lock will already be held when accessing these values on the
// P-chain.
ctx.ValidatorState = valState
} else {
m.validatorState = validators.NewNoState()
ctx.ValidatorState = m.validatorState
}
}
// Initialize the ProposerVM and the vm wrapped inside it
chainConfig, err := m.getChainConfig(ctx.ChainID)
if err != nil {
return nil, fmt.Errorf("error while fetching chain config: %w", err)
}
// enable ProposerVM on this VM
vm = proposervm.New(vm, m.ApricotPhase4Time, m.ApricotPhase4MinPChainHeight)
if m.MeterVMEnabled {
vm = metervm.NewBlockVM(vm)
}
if err := vm.Initialize(
ctx,
vmDBManager,
genesisData,
chainConfig.Upgrade,
chainConfig.Config,
msgChan,
fxs,
&sender,
); err != nil {
return nil, err
}
sampleK := consensusParams.K
if uint64(sampleK) > bootstrapWeight {
sampleK = int(bootstrapWeight)
}
// Asynchronously passes messages from the network to the consensus engine
handler := &router.Handler{}
timer := &router.Timer{
Handler: handler,
Preempt: sb.afterBootstrapped(),
}
// The engine handles consensus
engine := &smeng.Transitive{}
if err := engine.Initialize(smeng.Config{
Config: smbootstrap.Config{
Config: common.Config{
Ctx: ctx,
Validators: vdrs,
Beacons: beacons,
SampleK: sampleK,
StartupAlpha: (3*bootstrapWeight + 3) / 4,
Alpha: bootstrapWeight/2 + 1, // must be > 50%
Sender: &sender,
Subnet: sb,
Timer: timer,
RetryBootstrap: m.RetryBootstrap,
RetryBootstrapWarnFrequency: m.RetryBootstrapWarnFrequency,
MaxTimeGetAncestors: m.BootstrapMaxTimeGetAncestors,
MultiputMaxContainersSent: m.BootstrapMultiputMaxContainersSent,
MultiputMaxContainersReceived: m.BootstrapMultiputMaxContainersReceived,
},
Blocked: blocked,
VM: vm,
Bootstrapped: m.unblockChains,
},
Params: consensusParams,
Consensus: &smcon.Topological{},
}); err != nil {
return nil, fmt.Errorf("error initializing snowman engine: %w", err)
}
err = handler.Initialize(
m.MsgCreator,
engine,
vdrs,
msgChan,
fmt.Sprintf("%s_handler", consensusParams.Namespace),
consensusParams.Metrics,
)
if err != nil {
return nil, fmt.Errorf("couldn't initialize message handler: %s", err)
}
// Register health checks
chainAlias, err := m.PrimaryAlias(ctx.ChainID)
if err != nil {
chainAlias = ctx.ChainID.String()
}
checkFn := func() (interface{}, error) {
ctx.Lock.Lock()
defer ctx.Lock.Unlock()
return engine.HealthCheck()
}
if err := m.HealthService.RegisterCheck(chainAlias, checkFn); err != nil {
return nil, fmt.Errorf("couldn't add health check for chain %s: %w", chainAlias, err)
}
return &chain{
Name: chainAlias,
Engine: engine,
Handler: handler,
Ctx: ctx,
}, nil
}
func (m *manager) SubnetID(chainID ids.ID) (ids.ID, error) {
m.chainsLock.Lock()
defer m.chainsLock.Unlock()
chain, exists := m.chains[chainID]
if !exists {
return ids.ID{}, errUnknownChainID
}
return chain.Context().SubnetID, nil
}
func (m *manager) IsBootstrapped(id ids.ID) bool {
m.chainsLock.Lock()
chain, exists := m.chains[id]
m.chainsLock.Unlock()
if !exists {
return false
}
return chain.Engine().IsBootstrapped()
}
// Shutdown stops all the chains
func (m *manager) Shutdown() {
m.Log.Info("shutting down chain manager")
m.ManagerConfig.Router.Shutdown()
}
// LookupVM returns the ID of the VM associated with an alias
func (m *manager) LookupVM(alias string) (ids.ID, error) { return m.VMManager.Lookup(alias) }
// Notify registrants [those who want to know about the creation of chains]
// that the specified chain has been created
func (m *manager) notifyRegistrants(name string, ctx *snow.Context, engine common.Engine) {
for _, registrant := range m.registrants {
registrant.RegisterChain(name, ctx, engine)
}
}
// Returns:
// 1) the alias that already exists, or the empty string if there is none
// 2) true iff there exists a chain such that the chain has an alias in [aliases]
func (m *manager) isChainWithAlias(aliases ...string) (string, bool) {
for _, alias := range aliases {
if _, err := m.Lookup(alias); err == nil {
return alias, true
}
}
return "", false
}
// getChainConfig returns value of a entry by looking at ID key and alias key
// it first searches ID key, then falls back to it's corresponding primary alias
func (m *manager) getChainConfig(id ids.ID) (ChainConfig, error) {
if val, ok := m.ManagerConfig.ChainConfigs[id.String()]; ok {
return val, nil
}
aliases, err := m.Aliases(id)
if err != nil {
return ChainConfig{}, err
}
for _, alias := range aliases {
if val, ok := m.ManagerConfig.ChainConfigs[alias]; ok {
return val, nil
}
}
return ChainConfig{}, nil
}