-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
223 lines (185 loc) · 5.76 KB
/
peer.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
package libp2p
import (
"bytes"
"context"
"sort"
"sync"
"github.com/libp2p/go-libp2p-core/network"
libp2ppeer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"github.com/redesblock/mop/core/cluster"
"github.com/redesblock/mop/core/p2p"
)
type peerRegistry struct {
underlays map[string]libp2ppeer.ID // map overlay address to underlay peer id
overlays map[libp2ppeer.ID]cluster.Address // map underlay peer id to overlay address
full map[libp2ppeer.ID]bool // map to track whether a node is full or light node (true=full)
connections map[libp2ppeer.ID]map[network.Conn]struct{} // list of connections for safe removal on Disconnect notification
streams map[libp2ppeer.ID]map[network.Stream]context.CancelFunc
mu sync.RWMutex
//nolint:misspell
disconnecter disconnecter // peerRegistry notifies libp2p on peer disconnection
network.Notifiee // peerRegistry can be the receiver for network.Notify
}
type disconnecter interface {
disconnected(cluster.Address)
}
func newPeerRegistry() *peerRegistry {
return &peerRegistry{
underlays: make(map[string]libp2ppeer.ID),
overlays: make(map[libp2ppeer.ID]cluster.Address),
full: make(map[libp2ppeer.ID]bool),
connections: make(map[libp2ppeer.ID]map[network.Conn]struct{}),
streams: make(map[libp2ppeer.ID]map[network.Stream]context.CancelFunc),
Notifiee: new(network.NoopNotifiee),
}
}
func (r *peerRegistry) Exists(overlay cluster.Address) (found bool) {
_, found = r.peerID(overlay)
return found
}
// Disconnect removes the peer from registry in disconnect.
// peerRegistry has to be set by network.Network.Notify().
func (r *peerRegistry) Disconnected(_ network.Network, c network.Conn) {
peerID := c.RemotePeer()
r.mu.Lock()
// remove only the related connection,
// not eventusally newly created one for the same peer
if _, ok := r.connections[peerID][c]; !ok {
r.mu.Unlock()
return
}
// if there are multiple libp2p connections, consider the node disconnected only when the last connection is disconnected
delete(r.connections[peerID], c)
if len(r.connections[peerID]) > 0 {
r.mu.Unlock()
return
}
delete(r.connections, peerID)
overlay := r.overlays[peerID]
delete(r.overlays, peerID)
delete(r.underlays, overlay.ByteString())
for _, cancel := range r.streams[peerID] {
cancel()
}
delete(r.streams, peerID)
delete(r.full, peerID)
r.mu.Unlock()
r.disconnecter.disconnected(overlay)
}
func (r *peerRegistry) addStream(peerID libp2ppeer.ID, stream network.Stream, cancel context.CancelFunc) {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.streams[peerID]; !ok {
// it is possible that an addStream will be called after a disconnect
return
}
r.streams[peerID][stream] = cancel
}
func (r *peerRegistry) removeStream(peerID libp2ppeer.ID, stream network.Stream) {
r.mu.Lock()
defer r.mu.Unlock()
peer, ok := r.streams[peerID]
if !ok {
return
}
cancel, ok := peer[stream]
if !ok {
return
}
cancel()
delete(r.streams[peerID], stream)
}
func (r *peerRegistry) peers() []p2p.Peer {
r.mu.RLock()
peers := make([]p2p.Peer, 0, len(r.overlays))
for p, a := range r.overlays {
peers = append(peers, p2p.Peer{
Address: a,
FullNode: r.full[p],
})
}
r.mu.RUnlock()
sort.Slice(peers, func(i, j int) bool {
return bytes.Compare(peers[i].Address.Bytes(), peers[j].Address.Bytes()) == -1
})
return peers
}
func (r *peerRegistry) addIfNotExists(c network.Conn, overlay cluster.Address, full bool) (exists bool) {
peerID := c.RemotePeer()
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.connections[peerID]; !ok {
r.connections[peerID] = make(map[network.Conn]struct{})
}
// the connection is added even if the peer already exists in peer registry
// this is solving a case of multiple underlying libp2p connections for the same peer
r.connections[peerID][c] = struct{}{}
if _, exists := r.underlays[overlay.ByteString()]; exists {
return true
}
r.streams[peerID] = make(map[network.Stream]context.CancelFunc)
r.underlays[overlay.ByteString()] = peerID
r.overlays[peerID] = overlay
r.full[peerID] = full
return false
}
func (r *peerRegistry) peerID(overlay cluster.Address) (peerID libp2ppeer.ID, found bool) {
r.mu.RLock()
peerID, found = r.underlays[overlay.ByteString()]
r.mu.RUnlock()
return peerID, found
}
func (r *peerRegistry) overlay(peerID libp2ppeer.ID) (cluster.Address, bool) {
r.mu.RLock()
overlay, found := r.overlays[peerID]
r.mu.RUnlock()
return overlay, found
}
func (r *peerRegistry) fullnode(peerID libp2ppeer.ID) (bool, bool) {
r.mu.RLock()
full, found := r.full[peerID]
r.mu.RUnlock()
return full, found
}
func (r *peerRegistry) isConnected(peerID libp2ppeer.ID, remoteAddr ma.Multiaddr) (cluster.Address, bool) {
if remoteAddr == nil {
return cluster.ZeroAddress, false
}
r.mu.RLock()
defer r.mu.RUnlock()
overlay, found := r.overlays[peerID]
if !found {
return cluster.ZeroAddress, false
}
// check connection remote address
conns, ok := r.connections[peerID]
if !ok {
return cluster.ZeroAddress, false
}
for c := range conns {
if c.RemoteMultiaddr().Equal(remoteAddr) {
// we ARE connected to the peer on expected address
return overlay, true
}
}
return cluster.ZeroAddress, false
}
func (r *peerRegistry) remove(overlay cluster.Address) (found, full bool, peerID libp2ppeer.ID) {
r.mu.Lock()
peerID, found = r.underlays[overlay.ByteString()]
delete(r.overlays, peerID)
delete(r.underlays, overlay.ByteString())
delete(r.connections, peerID)
for _, cancel := range r.streams[peerID] {
cancel()
}
delete(r.streams, peerID)
full = r.full[peerID]
delete(r.full, peerID)
r.mu.Unlock()
return found, full, peerID
}
func (r *peerRegistry) setDisconnecter(d disconnecter) {
r.disconnecter = d
}