forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.go
124 lines (101 loc) · 2.97 KB
/
peers.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tracker
import (
"sync"
"github.com/dim4egster/qmallgo/ids"
"github.com/dim4egster/qmallgo/snow/validators"
"github.com/dim4egster/qmallgo/version"
)
var _ Peers = &peers{}
type Peers interface {
validators.SetCallbackListener
validators.Connector
// ConnectedWeight returns the currently connected stake weight
ConnectedWeight() uint64
// PreferredPeers returns the currently connected validators. If there are
// no currently connected validators then it will return the currently
// connected peers.
PreferredPeers() ids.NodeIDSet
}
type peers struct {
lock sync.RWMutex
// validators maps nodeIDs to their current stake weight
validators map[ids.NodeID]uint64
// connectedWeight contains the sum of all connected validator weights
connectedWeight uint64
// connectedValidators is the set of currently connected peers with a
// non-zero stake weight
connectedValidators ids.NodeIDSet
// connectedPeers is the set of all connected peers
connectedPeers ids.NodeIDSet
}
func NewPeers() Peers {
return &peers{
validators: make(map[ids.NodeID]uint64),
}
}
func (p *peers) OnValidatorAdded(nodeID ids.NodeID, weight uint64) {
p.lock.Lock()
defer p.lock.Unlock()
p.validators[nodeID] = weight
if p.connectedPeers.Contains(nodeID) {
p.connectedWeight += weight
p.connectedValidators.Add(nodeID)
}
}
func (p *peers) OnValidatorRemoved(nodeID ids.NodeID, weight uint64) {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.validators, nodeID)
if p.connectedPeers.Contains(nodeID) {
p.connectedWeight -= weight
p.connectedValidators.Remove(nodeID)
}
}
func (p *peers) OnValidatorWeightChanged(nodeID ids.NodeID, oldWeight, newWeight uint64) {
p.lock.Lock()
defer p.lock.Unlock()
p.validators[nodeID] = newWeight
if p.connectedPeers.Contains(nodeID) {
p.connectedWeight -= oldWeight
p.connectedWeight += newWeight
}
}
func (p *peers) Connected(nodeID ids.NodeID, _ *version.Application) error {
p.lock.Lock()
defer p.lock.Unlock()
if weight, ok := p.validators[nodeID]; ok {
p.connectedWeight += weight
p.connectedValidators.Add(nodeID)
}
p.connectedPeers.Add(nodeID)
return nil
}
func (p *peers) Disconnected(nodeID ids.NodeID) error {
p.lock.Lock()
defer p.lock.Unlock()
if weight, ok := p.validators[nodeID]; ok {
p.connectedWeight -= weight
p.connectedValidators.Remove(nodeID)
}
p.connectedPeers.Remove(nodeID)
return nil
}
func (p *peers) ConnectedWeight() uint64 {
p.lock.RLock()
defer p.lock.RUnlock()
return p.connectedWeight
}
func (p *peers) PreferredPeers() ids.NodeIDSet {
p.lock.RLock()
defer p.lock.RUnlock()
if p.connectedValidators.Len() == 0 {
connectedPeers := ids.NewNodeIDSet(p.connectedPeers.Len())
connectedPeers.Union(p.connectedPeers)
return connectedPeers
}
connectedValidators := ids.NewNodeIDSet(p.connectedValidators.Len())
connectedValidators.Union(p.connectedValidators)
return connectedValidators
}