-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathclient.go
More file actions
525 lines (449 loc) · 13 KB
/
client.go
File metadata and controls
525 lines (449 loc) · 13 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package p2p
import (
"context"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/ipfs/go-datastore"
libp2p "github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/crypto"
cdiscovery "github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
discovery "github.com/libp2p/go-libp2p/p2p/discovery/routing"
discutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"github.com/multiformats/go-multiaddr"
"github.com/rs/zerolog"
"github.com/evstack/ev-node/pkg/config"
rollhash "github.com/evstack/ev-node/pkg/hash"
)
// TODO(tzdybal): refactor to configuration parameters
const (
// reAdvertisePeriod defines a period after which P2P client re-attempt advertising namespace in DHT.
reAdvertisePeriod = 1 * time.Hour
// peerLimit defines limit of number of peers returned during active peer discovery.
peerLimit = 60
// seedReconnectBackoff is the initial backoff when reconnecting to a disconnected seed peer.
seedReconnectBackoff = 1 * time.Second
// seedReconnectMaxBackoff is the maximum backoff for seed peer reconnection attempts.
seedReconnectMaxBackoff = 30 * time.Second
)
// Client is a P2P client, implemented with libp2p.
//
// Initially, client connects to predefined seed nodes (aka bootnodes, bootstrap nodes).
// Those seed nodes serve Kademlia DHT protocol, and are agnostic to ORU chain. Using DHT
// peer routing and discovery clients find other peers within ORU network.
type Client struct {
logger zerolog.Logger
conf config.P2PConfig
chainID string
privKey crypto.PrivKey
host host.Host
dht *dht.IpfsDHT
disc *discovery.RoutingDiscovery
gater *conngater.BasicConnectionGater
ps *pubsub.PubSub
started bool
ctx context.Context
cancel context.CancelFunc
seedPeers []peer.AddrInfo
metrics *Metrics
}
// NewClient creates new Client object.
//
// Basic checks on parameters are done, and default parameters are provided for unset-configuration
func NewClient(
conf config.P2PConfig,
privKey crypto.PrivKey,
ds datastore.Datastore,
chainID string,
logger zerolog.Logger,
metrics *Metrics,
) (*Client, error) {
gater, err := conngater.NewBasicConnectionGater(ds)
if err != nil {
return nil, fmt.Errorf("failed to create connection gater: %w", err)
}
return &Client{
conf: conf,
gater: gater,
privKey: privKey,
chainID: chainID,
logger: logger,
metrics: metrics,
}, nil
}
func NewClientWithHost(
conf config.P2PConfig,
privKey crypto.PrivKey,
ds datastore.Datastore,
chainID string,
logger zerolog.Logger,
metrics *Metrics,
h host.Host, // injected host (mocknet or custom)
) (*Client, error) {
c, err := NewClient(conf, privKey, ds, chainID, logger, metrics)
if err != nil {
return nil, err
}
// Reject hosts whose identity does not match the supplied node key
expectedID, _ := peer.IDFromPrivateKey(privKey)
if h.ID() != expectedID {
return nil, fmt.Errorf(
"injected host ID %s does not match node key ID %s",
h.ID(),
expectedID,
)
}
c.host = h
return c, nil
}
// Start establish Client's P2P connectivity.
//
// Following steps are taken:
// 1. Setup libp2p host, start listening for incoming connections.
// 2. Setup gossibsub.
// 3. Setup DHT, establish connection to seed nodes and initialize peer discovery.
// 4. Use active peer discovery to look for peers from same ORU network.
func (c *Client) Start(ctx context.Context) error {
if c.started {
return nil
}
c.logger.Debug().Msg("starting P2P client")
if c.host != nil {
return c.startWithHost(ctx, c.host)
}
h, err := c.listen()
if err != nil {
return err
}
return c.startWithHost(ctx, h)
}
func (c *Client) startWithHost(ctx context.Context, h host.Host) error {
c.host = h
c.ctx, c.cancel = context.WithCancel(ctx)
for _, a := range c.host.Addrs() {
c.logger.Info().Str("address", fmt.Sprintf("%s/p2p/%s", a, c.host.ID())).Msg("listening on address")
}
c.logger.Debug().Str("blacklist", c.conf.BlockedPeers).Msg("blocking blacklisted peers")
if err := c.setupBlockedPeers(c.parseAddrInfoList(c.conf.BlockedPeers)); err != nil {
return err
}
c.logger.Debug().Str("whitelist", c.conf.AllowedPeers).Msg("allowing whitelisted peers")
if err := c.setupAllowedPeers(c.parseAddrInfoList(c.conf.AllowedPeers)); err != nil {
return err
}
c.logger.Debug().Msg("setting up gossiping")
if err := c.setupGossiping(ctx); err != nil {
return err
}
c.logger.Debug().Msg("setting up DHT")
if err := c.setupDHT(ctx); err != nil {
return err
}
c.logger.Debug().Msg("setting up active peer discovery")
if err := c.peerDiscovery(ctx); err != nil {
return err
}
c.started = true
c.host.Network().Notify(c.newDisconnectNotifee())
return nil
}
// Close gently stops Client.
func (c *Client) Close() error {
if c.cancel != nil {
c.cancel()
}
var err error
if c.dht != nil {
err = errors.Join(err, c.dht.Close())
}
if c.host != nil {
err = errors.Join(err, c.host.Close())
}
c.started = false
return err
}
// Addrs returns listen addresses of Client.
func (c *Client) Addrs() []multiaddr.Multiaddr {
return c.host.Addrs()
}
// Host returns the libp2p node in a peer-to-peer network
func (c *Client) Host() host.Host {
return c.host
}
// PubSub returns the libp2p node pubsub for adding future subscriptions
func (c *Client) PubSub() *pubsub.PubSub {
return c.ps
}
// ConnectionGater returns the client's connection gater
func (c *Client) ConnectionGater() *conngater.BasicConnectionGater {
return c.gater
}
// Info returns client ID, ListenAddr, and Network info
func (c *Client) Info() (string, string, string, error) {
rawKey, err := c.privKey.GetPublic().Raw()
if err != nil {
return "", "", "", err
}
return hex.EncodeToString(rollhash.SumTruncated(rawKey)), c.conf.ListenAddress, c.chainID, nil
}
// PeerIDs returns list of peer IDs of connected peers excluding self and inactive
func (c *Client) PeerIDs() []peer.ID {
peerIDs := make([]peer.ID, 0)
for _, conn := range c.host.Network().Conns() {
if conn.RemotePeer() != c.host.ID() {
peerIDs = append(peerIDs, conn.RemotePeer())
}
}
return peerIDs
}
// Peers returns list of peers connected to Client.
func (c *Client) Peers() []PeerConnection {
conns := c.host.Network().Conns()
res := make([]PeerConnection, 0, len(conns))
for _, conn := range conns {
pc := PeerConnection{
NodeInfo: NodeInfo{
ListenAddr: c.conf.ListenAddress,
Network: c.chainID,
NodeID: conn.RemotePeer().String(),
},
IsOutbound: conn.Stat().Direction == network.DirOutbound,
RemoteIP: conn.RemoteMultiaddr().String(),
}
res = append(res, pc)
}
return res
}
type disconnectNotifee struct {
c *Client
}
func (n disconnectNotifee) Connected(_ network.Network, conn network.Conn) {
p := conn.RemotePeer()
for _, sp := range n.c.seedPeers {
if sp.ID == p {
n.c.logger.Info().Str("peer", p.String()).Msg("connected to seed peer")
return
}
}
}
func (n disconnectNotifee) OpenedStream(_ network.Network, _ network.Stream) {}
func (n disconnectNotifee) ClosedStream(_ network.Network, _ network.Stream) {}
func (n disconnectNotifee) Listen(_ network.Network, _ multiaddr.Multiaddr) {}
func (n disconnectNotifee) ListenClose(_ network.Network, _ multiaddr.Multiaddr) {}
func (n disconnectNotifee) Disconnected(_ network.Network, conn network.Conn) {
p := conn.RemotePeer()
for _, sp := range n.c.seedPeers {
if sp.ID == p {
n.c.logger.Warn().Str("peer", p.String()).Msg("disconnected from seed peer, scheduling reconnect")
go n.c.reconnectSeedPeer(sp)
return
}
}
}
func (c *Client) reconnectSeedPeer(sp peer.AddrInfo) {
backoff := seedReconnectBackoff
for {
if c.ctx.Err() != nil {
return
}
if c.isConnected(sp.ID) {
return
}
err := c.host.Connect(c.ctx, sp)
if err == nil {
c.logger.Info().Str("peer", sp.ID.String()).Msg("reconnected to seed peer")
return
}
if c.ctx.Err() != nil {
return
}
c.logger.Debug().Str("peer", sp.ID.String()).Dur("backoff", backoff).Err(err).Msg("failed to reconnect to seed peer, retrying")
select {
case <-c.ctx.Done():
return
case <-time.After(backoff):
}
backoff *= 2
if backoff > seedReconnectMaxBackoff {
backoff = seedReconnectMaxBackoff
}
}
}
func (c *Client) newDisconnectNotifee() disconnectNotifee {
return disconnectNotifee{c: c}
}
// isConnected returns true if there is an active connection to the given peer.
func (c *Client) isConnected(id peer.ID) bool {
return c.host.Network().Connectedness(id) == network.Connected
}
func (c *Client) listen() (host.Host, error) {
maddr, err := multiaddr.NewMultiaddr(c.conf.ListenAddress)
if err != nil {
return nil, err
}
return libp2p.New(libp2p.ListenAddrs(maddr), libp2p.Identity(c.privKey), libp2p.ConnectionGater(c.gater))
}
func (c *Client) setupDHT(ctx context.Context) error {
peers := c.parseAddrInfoList(c.conf.Peers)
c.seedPeers = peers
if len(peers) == 0 {
c.logger.Info().Msg("no peers - only listening for connections")
}
for _, sa := range peers {
c.logger.Debug().Str("addr", sa.String()).Msg("peer")
}
var err error
c.dht, err = dht.New(ctx, c.host, dht.Mode(dht.ModeServer), dht.BootstrapPeers(peers...))
if err != nil {
return fmt.Errorf("failed to create DHT: %w", err)
}
err = c.dht.Bootstrap(ctx)
if err != nil {
return fmt.Errorf("failed to bootstrap DHT: %w", err)
}
c.host = routedhost.Wrap(c.host, c.dht)
return nil
}
func (c *Client) peerDiscovery(ctx context.Context) error {
err := c.setupPeerDiscovery(ctx)
if err != nil {
return err
}
err = c.advertise(ctx)
if err != nil {
return err
}
err = c.findPeers(ctx)
if err != nil {
return err
}
return nil
}
func (c *Client) setupPeerDiscovery(ctx context.Context) error {
// wait for DHT
select {
case <-ctx.Done():
return ctx.Err()
case <-c.dht.RefreshRoutingTable():
}
c.disc = discovery.NewRoutingDiscovery(c.dht)
return nil
}
func (c *Client) setupBlockedPeers(peers []peer.AddrInfo) error {
for _, p := range peers {
if err := c.gater.BlockPeer(p.ID); err != nil {
return err
}
}
return nil
}
func (c *Client) setupAllowedPeers(peers []peer.AddrInfo) error {
for _, p := range peers {
if err := c.gater.UnblockPeer(p.ID); err != nil {
return err
}
}
return nil
}
func (c *Client) advertise(ctx context.Context) error {
discutil.Advertise(ctx, c.disc, c.getNamespace(), cdiscovery.TTL(reAdvertisePeriod))
return nil
}
func (c *Client) findPeers(ctx context.Context) error {
peerCh, err := c.disc.FindPeers(ctx, c.getNamespace(), cdiscovery.Limit(peerLimit))
if err != nil {
return err
}
for peer := range peerCh {
go c.tryConnect(ctx, peer)
}
return nil
}
// tryConnect attempts to connect to a peer and logs error if necessary
func (c *Client) tryConnect(ctx context.Context, peer peer.AddrInfo) {
if peer.ID == c.host.ID() {
return
}
err := c.host.Connect(ctx, peer)
if err != nil && ctx.Err() == nil {
c.logger.Error().Str("peer", peer.String()).Err(err).Msg("failed to connect to peer")
}
}
func (c *Client) setupGossiping(ctx context.Context) error {
var err error
c.ps, err = pubsub.NewGossipSub(ctx, c.host)
if err != nil {
return err
}
return nil
}
// parseAddrInfoList parses a comma-separated string of multiaddrs into a list of peer.AddrInfo structs
func (c *Client) parseAddrInfoList(addrInfoStr string) []peer.AddrInfo {
if len(addrInfoStr) == 0 {
return []peer.AddrInfo{}
}
peers := strings.Split(addrInfoStr, ",")
addrs := make([]peer.AddrInfo, 0, len(peers))
for _, p := range peers {
maddr, err := multiaddr.NewMultiaddr(p)
if err != nil {
c.logger.Error().Str("address", p).Err(err).Msg("failed to parse peer")
continue
}
addrInfo, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
c.logger.Error().Str("address", maddr.String()).Err(err).Msg("failed to create addr info for peer")
continue
}
addrs = append(addrs, *addrInfo)
}
return addrs
}
// getNamespace returns unique string identifying ORU network.
//
// It is used to advertise/find peers in libp2p DHT.
// For now, chainID is used.
func (c *Client) getNamespace() string {
return c.chainID
}
func (c *Client) GetPeers() ([]peer.AddrInfo, error) {
peerCh, err := c.disc.FindPeers(context.Background(), c.getNamespace(), cdiscovery.Limit(peerLimit))
if err != nil {
return nil, err
}
var peers []peer.AddrInfo
for p := range peerCh {
if p.ID == c.host.ID() {
continue
}
peers = append(peers, p)
}
return peers, nil
}
func (c *Client) GetNetworkInfo() (NetworkInfo, error) {
hostAddrs := c.host.Addrs()
addrs := make([]string, 0, len(hostAddrs))
peerIDSuffix := "/p2p/" + c.host.ID().String()
for _, a := range hostAddrs {
addr := a.String()
// Only append peer ID if not already present
if !strings.HasSuffix(addr, peerIDSuffix) {
addr = addr + peerIDSuffix
}
addrs = append(addrs, addr)
}
return NetworkInfo{
ID: c.host.ID().String(),
ListenAddress: addrs,
ConnectedPeers: c.PeerIDs(),
}, nil
}