@@ -99,7 +99,6 @@ func DefaultLibP2PNodeFactory(
99
99
address string ,
100
100
flowKey fcrypto.PrivateKey ,
101
101
rootBlockID flow.Identifier ,
102
- chainID flow.ChainID ,
103
102
idProvider id.IdentityProvider ,
104
103
maxPubSubMsgSize int ,
105
104
metrics module.NetworkMetrics ,
@@ -445,11 +444,13 @@ func (n *Node) RemovePeer(ctx context.Context, peerID peer.ID) error {
445
444
446
445
// CreateStream returns an existing stream connected to the peer if it exists, or creates a new stream with it.
447
446
func (n * Node ) CreateStream (ctx context.Context , peerID peer.ID ) (libp2pnet.Stream , error ) {
447
+ lg := n .logger .With ().Str ("peer_id" , peerID .Pretty ()).Logger ()
448
+
448
449
// If we do not currently have any addresses for the given peer, stream creation will almost
449
- // certainly fail. If this Node was configure with a DHT, we can try to lookup the address of
450
+ // certainly fail. If this Node was configured with a DHT, we can try to look up the address of
450
451
// the peer in the DHT as a last resort.
451
452
if len (n .host .Peerstore ().Addrs (peerID )) == 0 && n .dht != nil {
452
- n . logger . Info ().Str ( "peerID" , peerID . Pretty ()). Msg ("address not found in peerstore , searching for peer in dht" )
453
+ lg . Info ().Msg ("address not found in peer store , searching for peer in dht" )
453
454
454
455
var err error
455
456
func () {
@@ -460,36 +461,41 @@ func (n *Node) CreateStream(ctx context.Context, peerID peer.ID) (libp2pnet.Stre
460
461
}()
461
462
462
463
if err != nil {
463
- n . logger . Warn ().Err (err ).Str ( "peerID" , peerID . Pretty ()). Msg ("could not find addresses " )
464
+ lg . Warn ().Err (err ).Msg ("address not found in both peer store and dht " )
464
465
} else {
465
- n . logger . Info ().Str ( "peerID" , peerID . Pretty ()). Msg ( "addresses found" )
466
+ lg . Debug ().Msg ( "address not found in peer store, but found in dht search " )
466
467
}
467
468
}
468
- // Open libp2p Stream with the remote peer (will use an existing TCP connection underneath if it exists)
469
- stream , err := n .tryCreateNewStream (ctx , peerID , maxConnectAttempt )
469
+ stream , dialAddrs , err := n .tryCreateNewStream (ctx , peerID , maxConnectAttempt )
470
470
if err != nil {
471
- return nil , flownet .NewPeerUnreachableError (fmt .Errorf ("could not create stream (peer_id: %s): %w" , peerID , err ))
471
+ return nil , flownet .NewPeerUnreachableError (fmt .Errorf ("could not create stream (peer_id: %s, dialing address(s): %v): %w" , peerID ,
472
+ dialAddrs , err ))
472
473
}
474
+
475
+ lg .Debug ().Str ("dial_address" , fmt .Sprintf ("%v" , dialAddrs )).Msg ("stream successfully created to remote peer" )
473
476
return stream , nil
474
477
}
475
478
476
- // tryCreateNewStream makes at most maxAttempts to create a stream with the peer.
479
+ // tryCreateNewStream makes at most ` maxAttempts` to create a stream with the peer.
477
480
// This was put in as a fix for #2416. PubSub and 1-1 communication compete with each other when trying to connect to
478
- // remote nodes and once in a while NewStream returns an error 'both yamux endpoints are clients'
479
- func (n * Node ) tryCreateNewStream (ctx context.Context , peerID peer.ID , maxAttempts int ) (libp2pnet.Stream , error ) {
481
+ // remote nodes and once in a while NewStream returns an error 'both yamux endpoints are clients'.
482
+ //
483
+ // Note that in case an existing TCP connection underneath to `peerID` exists, that connection is utilized for creating a new stream.
484
+ // The multiaddr.Multiaddr return value represents the addresses of `peerID` we dial while trying to create a stream to it.
485
+ func (n * Node ) tryCreateNewStream (ctx context.Context , peerID peer.ID , maxAttempts int ) (libp2pnet.Stream , []multiaddr.Multiaddr , error ) {
480
486
// protect the underlying connection from being inadvertently pruned by the peer manager while the stream and
481
- // connection creation is being attempted
487
+ // connection creation is being attempted, and remove it from protected list once stream created.
482
488
n .connMgr .ProtectPeer (peerID )
483
- // unprotect it once done
484
489
defer n .connMgr .UnprotectPeer (peerID )
485
490
486
491
var errs error
487
492
var s libp2pnet.Stream
488
493
var retries = 0
494
+ var dialAddr []multiaddr.Multiaddr // address on which we dial peerID
489
495
for ; retries < maxAttempts ; retries ++ {
490
496
select {
491
497
case <- ctx .Done ():
492
- return nil , fmt .Errorf ("context done before stream could be created (retry attempt: %d, errors: %w)" , retries , errs )
498
+ return nil , nil , fmt .Errorf ("context done before stream could be created (retry attempt: %d, errors: %w)" , retries , errs )
493
499
default :
494
500
}
495
501
@@ -501,6 +507,7 @@ func (n *Node) tryCreateNewStream(ctx context.Context, peerID peer.ID, maxAttemp
501
507
502
508
// cancel the dial back off (if any), since we want to connect immediately
503
509
network := n .host .Network ()
510
+ dialAddr = network .Peerstore ().Addrs (peerID )
504
511
if swm , ok := network .(* swarm.Swarm ); ok {
505
512
swm .Backoff ().Clear (peerID )
506
513
}
@@ -518,12 +525,12 @@ func (n *Node) tryCreateNewStream(ctx context.Context, peerID peer.ID, maxAttemp
518
525
519
526
// if the connection was rejected due to invalid node id, skip the re-attempt
520
527
if strings .Contains (err .Error (), "failed to negotiate security protocol" ) {
521
- return s , fmt .Errorf ("invalid node id: %w" , err )
528
+ return s , dialAddr , fmt .Errorf ("invalid node id: %w" , err )
522
529
}
523
530
524
531
// if the connection was rejected due to allowlisting, skip the re-attempt
525
532
if errors .Is (err , swarm .ErrGaterDisallowedConnection ) {
526
- return s , fmt .Errorf ("target node is not on the approved list of nodes: %w" , err )
533
+ return s , dialAddr , fmt .Errorf ("target node is not on the approved list of nodes: %w" , err )
527
534
}
528
535
529
536
errs = multierror .Append (errs , err )
@@ -535,7 +542,8 @@ func (n *Node) tryCreateNewStream(ctx context.Context, peerID peer.ID, maxAttemp
535
542
if err != nil {
536
543
// if the stream creation failed due to invalid protocol id, skip the re-attempt
537
544
if strings .Contains (err .Error (), "protocol not supported" ) {
538
- return nil , fmt .Errorf ("remote node is running on a different spork: %w, protocol attempted: %s" , err , n .flowLibP2PProtocolID )
545
+ return nil , dialAddr , fmt .Errorf ("remote node is running on a different spork: %w, protocol attempted: %s" , err ,
546
+ n .flowLibP2PProtocolID )
539
547
}
540
548
errs = multierror .Append (errs , err )
541
549
continue
@@ -545,15 +553,15 @@ func (n *Node) tryCreateNewStream(ctx context.Context, peerID peer.ID, maxAttemp
545
553
}
546
554
547
555
if retries == maxAttempts {
548
- return s , errs
556
+ return s , dialAddr , errs
549
557
}
550
558
551
559
s , err := n .compressedStream (s )
552
560
if err != nil {
553
- return nil , fmt .Errorf ("could not create compressed stream: %w" , err )
561
+ return nil , dialAddr , fmt .Errorf ("could not create compressed stream: %w" , err )
554
562
}
555
563
556
- return s , nil
564
+ return s , dialAddr , nil
557
565
}
558
566
559
567
// GetIPPort returns the IP and Port the libp2p node is listening on.
0 commit comments