forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
startup.go
72 lines (54 loc) · 1.64 KB
/
startup.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tracker
import (
"context"
"sync"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/crypto/bls"
"github.com/MetalBlockchain/metalgo/version"
)
var _ Startup = (*startup)(nil)
type Startup interface {
Peers
ShouldStart() bool
}
type startup struct {
Peers
lock sync.RWMutex
startupWeight uint64
shouldStart bool
}
func NewStartup(peers Peers, startupWeight uint64) Startup {
return &startup{
Peers: peers,
startupWeight: startupWeight,
shouldStart: peers.ConnectedWeight() >= startupWeight,
}
}
func (s *startup) OnValidatorAdded(nodeID ids.NodeID, pk *bls.PublicKey, txID ids.ID, weight uint64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Peers.OnValidatorAdded(nodeID, pk, txID, weight)
s.shouldStart = s.shouldStart || s.Peers.ConnectedWeight() >= s.startupWeight
}
func (s *startup) OnValidatorWeightChanged(nodeID ids.NodeID, oldWeight, newWeight uint64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Peers.OnValidatorWeightChanged(nodeID, oldWeight, newWeight)
s.shouldStart = s.shouldStart || s.Peers.ConnectedWeight() >= s.startupWeight
}
func (s *startup) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
s.lock.Lock()
defer s.lock.Unlock()
if err := s.Peers.Connected(ctx, nodeID, nodeVersion); err != nil {
return err
}
s.shouldStart = s.shouldStart || s.Peers.ConnectedWeight() >= s.startupWeight
return nil
}
func (s *startup) ShouldStart() bool {
s.lock.RLock()
defer s.lock.RUnlock()
return s.shouldStart
}