forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
subnets.go
82 lines (65 loc) · 1.97 KB
/
subnets.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chains
import (
"errors"
"sync"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/subnets"
"github.com/MetalBlockchain/metalgo/utils/constants"
)
var ErrNoPrimaryNetworkConfig = errors.New("no subnet config for primary network found")
// Subnets holds the currently running subnets on this node
type Subnets struct {
nodeID ids.NodeID
configs map[ids.ID]subnets.Config
lock sync.RWMutex
subnets map[ids.ID]subnets.Subnet
}
// GetOrCreate returns a subnet running on this node, or creates one if it was
// not running before. Returns the subnet and if the subnet was created.
func (s *Subnets) GetOrCreate(subnetID ids.ID) (subnets.Subnet, bool) {
s.lock.Lock()
defer s.lock.Unlock()
if subnet, ok := s.subnets[subnetID]; ok {
return subnet, false
}
// Default to the primary network config if a subnet config was not
// specified
config, ok := s.configs[subnetID]
if !ok {
config = s.configs[constants.PrimaryNetworkID]
}
subnet := subnets.New(s.nodeID, config)
s.subnets[subnetID] = subnet
return subnet, true
}
// Bootstrapping returns the subnetIDs of any chains that are still
// bootstrapping.
func (s *Subnets) Bootstrapping() []ids.ID {
s.lock.RLock()
defer s.lock.RUnlock()
subnetsBootstrapping := make([]ids.ID, 0, len(s.subnets))
for subnetID, subnet := range s.subnets {
if !subnet.IsBootstrapped() {
subnetsBootstrapping = append(subnetsBootstrapping, subnetID)
}
}
return subnetsBootstrapping
}
// NewSubnets returns an instance of Subnets
func NewSubnets(
nodeID ids.NodeID,
configs map[ids.ID]subnets.Config,
) (*Subnets, error) {
if _, ok := configs[constants.PrimaryNetworkID]; !ok {
return nil, ErrNoPrimaryNetworkConfig
}
s := &Subnets{
nodeID: nodeID,
configs: configs,
subnets: make(map[ids.ID]subnets.Subnet),
}
_, _ = s.GetOrCreate(constants.PrimaryNetworkID)
return s, nil
}