-
Notifications
You must be signed in to change notification settings - Fork 113
/
manualpeering.go
381 lines (342 loc) · 12.2 KB
/
manualpeering.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package manualpeering
import (
"bytes"
"context"
"net"
"sync"
"sync/atomic"
"time"
"github.com/iotaledger/hive.go/core/autopeering/peer/service"
"github.com/iotaledger/hive.go/core/generics/event"
"github.com/iotaledger/hive.go/core/typeutils"
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/core/autopeering/peer"
"github.com/iotaledger/hive.go/core/crypto/ed25519"
"github.com/iotaledger/hive.go/core/identity"
"github.com/iotaledger/hive.go/core/logger"
"github.com/iotaledger/goshimmer/packages/node/p2p"
)
const defaultReconnectInterval = 5 * time.Second
// ConnectionDirection is an enum for the type of connection between local peer and the other peer in the gossip layer.
type ConnectionDirection string
const (
// ConnDirectionOutbound means that the local peer dials for the connection in the gossip layer.
ConnDirectionOutbound ConnectionDirection = "outbound"
// ConnDirectionInbound means that the local peer accepts for the connection in the gossip layer.
ConnDirectionInbound ConnectionDirection = "inbound"
)
// ConnectionStatus is an enum for the peer connection status in the gossip layer.
type ConnectionStatus string
const (
// ConnStatusDisconnected means that there is no real connection established in the gossip layer for that peer.
ConnStatusDisconnected ConnectionStatus = "disconnected"
// ConnStatusConnected means that there is a real connection established in the gossip layer for that peer.
ConnStatusConnected ConnectionStatus = "connected"
)
// KnownPeerToAdd defines a type that is used in .AddPeer() method.
type KnownPeerToAdd struct {
PublicKey ed25519.PublicKey `json:"publicKey"`
Address string `json:"address"`
}
// KnownPeer defines a peer record in the manual peering layer.
type KnownPeer struct {
PublicKey ed25519.PublicKey `json:"publicKey"`
Address string `json:"address"`
ConnDirection ConnectionDirection `json:"connectionDirection"`
ConnStatus ConnectionStatus `json:"connectionStatus"`
}
// Manager is the core entity in the manual peering package.
// It holds a list of known peers and constantly provisions it to the gossip layer.
// Its job is to keep in sync the list of known peers
// and the list of current manual neighbors connected in the gossip layer.
// If a new peer is added to known peers, manager will forward it to gossip and make sure it establishes a connection.
// And vice versa, if a peer is being removed from the list of known peers,
// manager will make sure gossip drops that connection.
// Manager also subscribes to the gossip events and in case the connection with a manual peer fails it will reconnect.
type Manager struct {
p2pm *p2p.Manager
log *logger.Logger
local *peer.Local
startOnce sync.Once
isStarted typeutils.AtomicBool
stopOnce sync.Once
stopMutex sync.RWMutex
isStopped bool
reconnectInterval time.Duration
knownPeersMutex sync.RWMutex
knownPeers map[identity.ID]*knownPeer
onGossipNeighborRemovedClosure *event.Closure[*p2p.NeighborRemovedEvent]
onGossipNeighborAddedClosure *event.Closure[*p2p.NeighborAddedEvent]
}
// NewManager initializes a new Manager instance.
func NewManager(p2pm *p2p.Manager, local *peer.Local, log *logger.Logger) *Manager {
m := &Manager{
p2pm: p2pm,
local: local,
log: log,
reconnectInterval: defaultReconnectInterval,
knownPeers: map[identity.ID]*knownPeer{},
}
m.onGossipNeighborRemovedClosure = event.NewClosure(func(event *p2p.NeighborRemovedEvent) { m.onGossipNeighborRemoved(event.Neighbor) })
m.onGossipNeighborAddedClosure = event.NewClosure(func(event *p2p.NeighborAddedEvent) { m.onGossipNeighborAdded(event.Neighbor) })
return m
}
// AddPeer adds multiple peers to the list of known peers.
func (m *Manager) AddPeer(peers ...*KnownPeerToAdd) error {
var resultErr error
for _, p := range peers {
if err := m.addPeer(p); err != nil {
resultErr = errors.CombineErrors(resultErr, err)
}
}
return resultErr
}
// RemovePeer removes multiple peers from the list of known peers.
func (m *Manager) RemovePeer(keys ...ed25519.PublicKey) error {
var resultErr error
for _, key := range keys {
if err := m.removePeer(key); err != nil {
resultErr = errors.CombineErrors(resultErr, err)
}
}
return resultErr
}
// GetPeersConfig holds optional parameters for the GetPeers method.
type GetPeersConfig struct {
// If true, GetPeers returns peers that have actual connection established in the gossip layer.
OnlyConnected bool `json:"onlyConnected"`
}
// GetPeersOption defines a single option for GetPeers method.
type GetPeersOption func(conf *GetPeersConfig)
// BuildGetPeersConfig builds GetPeersConfig struct from a list of options.
func BuildGetPeersConfig(opts []GetPeersOption) *GetPeersConfig {
conf := &GetPeersConfig{}
for _, o := range opts {
o(conf)
}
return conf
}
// ToOptions translates config struct to a list of corresponding options.
func (c *GetPeersConfig) ToOptions() (opts []GetPeersOption) {
if c.OnlyConnected {
opts = append(opts, WithOnlyConnectedPeers())
}
return opts
}
// WithOnlyConnectedPeers returns a GetPeersOption that sets OnlyConnected field to true.
func WithOnlyConnectedPeers() GetPeersOption {
return func(conf *GetPeersConfig) {
conf.OnlyConnected = true
}
}
// GetPeers returns the list of known peers.
func (m *Manager) GetPeers(opts ...GetPeersOption) []*KnownPeer {
conf := BuildGetPeersConfig(opts)
m.knownPeersMutex.RLock()
defer m.knownPeersMutex.RUnlock()
peers := make([]*KnownPeer, 0, len(m.knownPeers))
for _, kp := range m.knownPeers {
connStatus := kp.getConnStatus()
if !conf.OnlyConnected || connStatus == ConnStatusConnected {
peers = append(peers, &KnownPeer{
PublicKey: kp.peer.PublicKey(),
Address: kp.peerAddress,
ConnDirection: kp.connDirection,
ConnStatus: connStatus,
})
}
}
return peers
}
// Start subscribes to the gossip layer events and starts internal background workers.
// Calling multiple times has no effect.
func (m *Manager) Start() {
m.startOnce.Do(func() {
m.p2pm.NeighborGroupEvents(p2p.NeighborsGroupManual).NeighborRemoved.Attach(m.onGossipNeighborRemovedClosure)
m.p2pm.NeighborGroupEvents(p2p.NeighborsGroupManual).NeighborAdded.Attach(m.onGossipNeighborAddedClosure)
m.isStarted.Set()
})
}
// Stop terminates internal background workers. Calling multiple times has no effect.
func (m *Manager) Stop() (err error) {
if !m.isStarted.IsSet() {
return errors.New("can't stop the manager: it hasn't been started yet")
}
m.stopOnce.Do(func() {
m.stopMutex.Lock()
defer m.stopMutex.Unlock()
m.isStopped = true
err = errors.WithStack(m.removeAllKnownPeers())
m.p2pm.NeighborGroupEvents(p2p.NeighborsGroupManual).NeighborRemoved.Detach(m.onGossipNeighborRemovedClosure)
m.p2pm.NeighborGroupEvents(p2p.NeighborsGroupManual).NeighborAdded.Detach(m.onGossipNeighborAddedClosure)
})
return err
}
type knownPeer struct {
peer *peer.Peer
peerAddress string
connDirection ConnectionDirection
connStatus *atomic.Value
removeCh chan struct{}
doneCh chan struct{}
}
func newKnownPeer(p *KnownPeerToAdd, connDirection ConnectionDirection) (*knownPeer, error) {
tcpAddress, err := net.ResolveTCPAddr("tcp", p.Address)
if err != nil {
return nil, errors.Wrap(err, "failed to parse peer address")
}
services := service.New()
// Peering key is required in order to initialize a peer,
// but it's not used in both manual peering and gossip layers so we just specify the default one.
services.Update(service.PeeringKey, "tcp", 14626)
services.Update(service.P2PKey, tcpAddress.Network(), tcpAddress.Port)
kp := &knownPeer{
peer: peer.NewPeer(identity.New(p.PublicKey), tcpAddress.IP, services),
peerAddress: p.Address,
connDirection: connDirection,
connStatus: &atomic.Value{},
removeCh: make(chan struct{}),
doneCh: make(chan struct{}),
}
kp.setConnStatus(ConnStatusDisconnected)
return kp, nil
}
func (kp *knownPeer) getConnStatus() ConnectionStatus {
return kp.connStatus.Load().(ConnectionStatus)
}
func (kp *knownPeer) setConnStatus(cs ConnectionStatus) {
kp.connStatus.Store(cs)
}
func (m *Manager) addPeer(p *KnownPeerToAdd) error {
if !m.isStarted.IsSet() {
return errors.New("manual peering manager hasn't been started yet")
}
m.stopMutex.RLock()
defer m.stopMutex.RUnlock()
if m.isStopped {
return errors.New("manual peering manager was stopped")
}
m.knownPeersMutex.Lock()
defer m.knownPeersMutex.Unlock()
connDirection, err := m.connectionDirection(p.PublicKey)
if err != nil {
return errors.WithStack(err)
}
kp, err := newKnownPeer(p, connDirection)
if err != nil {
return errors.WithStack(err)
}
if _, exists := m.knownPeers[kp.peer.ID()]; exists {
return nil
}
m.log.Infow("Adding new peer to the list of known peers in manual peering", "peer", p)
m.knownPeers[kp.peer.ID()] = kp
go func() {
defer close(kp.doneCh)
m.keepPeerConnected(kp)
}()
return nil
}
func (m *Manager) removePeer(key ed25519.PublicKey) error {
m.knownPeersMutex.Lock()
defer m.knownPeersMutex.Unlock()
m.log.Infow("Removing peer from from the list of known peers in manual peering",
"publicKey", key)
peerID := identity.NewID(key)
err := m.removePeerByID(peerID)
return errors.WithStack(err)
}
func (m *Manager) removeAllKnownPeers() error {
m.knownPeersMutex.Lock()
defer m.knownPeersMutex.Unlock()
var resultErr error
for peerID := range m.knownPeers {
if err := m.removePeerByID(peerID); err != nil {
resultErr = errors.CombineErrors(resultErr, err)
}
}
return resultErr
}
func (m *Manager) removePeerByID(peerID identity.ID) error {
kp, exists := m.knownPeers[peerID]
if !exists {
return nil
}
delete(m.knownPeers, peerID)
close(kp.removeCh)
<-kp.doneCh
if err := m.p2pm.DropNeighbor(peerID, p2p.NeighborsGroupManual); err != nil && !errors.Is(err, p2p.ErrUnknownNeighbor) {
return errors.Wrapf(err, "failed to drop known peer %s in the gossip layer", peerID)
}
return nil
}
func (m *Manager) keepPeerConnected(kp *knownPeer) {
ctx, ctxCancel := context.WithCancel(context.Background())
cancelContextOnRemove := func() {
<-kp.removeCh
ctxCancel()
}
go cancelContextOnRemove()
ticker := time.NewTicker(m.reconnectInterval)
defer ticker.Stop()
peerID := kp.peer.ID()
for {
if kp.getConnStatus() == ConnStatusDisconnected {
m.log.Infow(
"Peer is disconnected, calling gossip layer to establish the connection",
"peer", kp.peer, "connectionDirection", kp.connDirection,
)
var err error
if kp.connDirection == ConnDirectionOutbound {
err = m.p2pm.AddOutbound(ctx, kp.peer, p2p.NeighborsGroupManual)
} else if kp.connDirection == ConnDirectionInbound {
err = m.p2pm.AddInbound(ctx, kp.peer, p2p.NeighborsGroupManual, p2p.WithNoDefaultTimeout())
}
if err != nil && !errors.Is(err, p2p.ErrDuplicateNeighbor) && !errors.Is(err, context.Canceled) {
m.log.Errorw(
"Failed to connect a neighbor in the gossip layer",
"peerID", peerID, "connectionDirection", kp.connDirection, "err", err,
)
}
}
select {
case <-ticker.C:
case <-kp.removeCh:
<-ctx.Done()
return
}
}
}
func (m *Manager) onGossipNeighborRemoved(neighbor *p2p.Neighbor) {
m.changeNeighborStatus(neighbor, ConnStatusDisconnected)
}
func (m *Manager) onGossipNeighborAdded(neighbor *p2p.Neighbor) {
m.changeNeighborStatus(neighbor, ConnStatusConnected)
m.log.Infow(
"Gossip layer successfully connected with the peer",
"peer", neighbor.Peer,
)
}
func (m *Manager) changeNeighborStatus(neighbor *p2p.Neighbor, connStatus ConnectionStatus) {
m.knownPeersMutex.RLock()
defer m.knownPeersMutex.RUnlock()
kp, exists := m.knownPeers[neighbor.ID()]
if !exists {
return
}
kp.setConnStatus(connStatus)
}
func (m *Manager) connectionDirection(peerPK ed25519.PublicKey) (ConnectionDirection, error) {
result := bytes.Compare(m.local.PublicKey().Bytes(), peerPK.Bytes())
if result < 0 {
return ConnDirectionOutbound, nil
} else if result > 0 {
return ConnDirectionInbound, nil
} else {
return "", errors.Newf(
"manual peering: provided neighbor public key %s is the same as the local %s: can't compare lexicographically",
peerPK,
m.local.PublicKey(),
)
}
}