forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnet.go
84 lines (64 loc) · 1.74 KB
/
subnet.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chains
import (
"sync"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/engine/common"
)
var _ Subnet = &subnet{}
// Subnet keeps track of the currently bootstrapping chains in a subnet. If no
// chains in the subnet are currently bootstrapping, the subnet is considered
// bootstrapped.
type Subnet interface {
common.Subnet
afterBootstrapped() chan struct{}
addChain(chainID ids.ID)
removeChain(chainID ids.ID)
}
type SubnetConfig struct {
// ValidatorOnly indicates that this Subnet's Chains are available to only subnet validators.
ValidatorOnly bool `json:"validatorOnly"`
ConsensusParameters avalanche.Parameters `json:"consensusParameters"`
}
type subnet struct {
lock sync.RWMutex
bootstrapping ids.Set
once sync.Once
bootstrappedSema chan struct{}
}
func newSubnet() Subnet {
return &subnet{
bootstrappedSema: make(chan struct{}),
}
}
func (s *subnet) IsBootstrapped() bool {
s.lock.RLock()
defer s.lock.RUnlock()
return s.bootstrapping.Len() == 0
}
func (s *subnet) Bootstrapped(chainID ids.ID) {
s.lock.Lock()
defer s.lock.Unlock()
s.bootstrapping.Remove(chainID)
if s.bootstrapping.Len() > 0 {
return
}
s.once.Do(func() {
close(s.bootstrappedSema)
})
}
func (s *subnet) afterBootstrapped() chan struct{} {
return s.bootstrappedSema
}
func (s *subnet) addChain(chainID ids.ID) {
s.lock.Lock()
defer s.lock.Unlock()
s.bootstrapping.Add(chainID)
}
func (s *subnet) removeChain(chainID ids.ID) {
s.lock.Lock()
defer s.lock.Unlock()
s.bootstrapping.Remove(chainID)
}