@@ -506,6 +506,7 @@ impl IoHandler<Message> for Handler {
506
506
is_inbound : true ,
507
507
} => {
508
508
let mut inbound_connections = self . inbound_connections . write ( ) ;
509
+ let outbound_connections = self . outbound_connections . write ( ) ;
509
510
let target = connection. peer_addr ( ) ;
510
511
self . peer_db . insert ( * target) ;
511
512
if let Some ( token) = self . inbound_tokens . lock ( ) . gen ( ) {
@@ -526,10 +527,17 @@ impl IoHandler<Message> for Handler {
526
527
token
527
528
) ;
528
529
}
529
-
530
- let t = inbound_connections. insert ( token, connection) ;
531
- assert ! ( t. is_none( ) ) ;
532
- io. register_stream ( token) ;
530
+ let can_insert = inbound_connections
531
+ . values ( )
532
+ . all ( |in_connection| in_connection. peer_addr ( ) != connection. peer_addr ( ) ) ;
533
+ let is_not_out = outbound_connections
534
+ . values ( )
535
+ . all ( |out_connection| out_connection. peer_addr ( ) != connection. peer_addr ( ) ) ;
536
+ if can_insert && is_not_out {
537
+ let t = inbound_connections. insert ( token, connection) ;
538
+ assert ! ( t. is_none( ) ) ;
539
+ io. register_stream ( token) ;
540
+ }
533
541
} else {
534
542
cwarn ! ( NETWORK , "Cannot establish an inbound connection" ) ;
535
543
}
@@ -539,6 +547,7 @@ impl IoHandler<Message> for Handler {
539
547
is_inbound : false ,
540
548
} => {
541
549
let mut outbound_connections = self . outbound_connections . write ( ) ;
550
+ let inbound_connections = self . inbound_connections . write ( ) ;
542
551
if let Some ( token) = self . outbound_tokens . lock ( ) . gen ( ) {
543
552
let peer_addr = * connection. peer_addr ( ) ;
544
553
let remote_node_id = peer_addr. into ( ) ;
@@ -570,9 +579,18 @@ impl IoHandler<Message> for Handler {
570
579
network_message_size,
571
580
) ;
572
581
}
573
- let t = outbound_connections. insert ( token, connection) ;
574
- assert ! ( t. is_none( ) ) ;
575
- io. register_stream ( token) ;
582
+ let can_insert = outbound_connections
583
+ . values ( )
584
+ . all ( |out_connection| out_connection. peer_addr ( ) == connection. peer_addr ( ) ) ;
585
+ let is_not_in = inbound_connections
586
+ . values ( )
587
+ . all ( |in_connection| in_connection. peer_addr ( ) == connection. peer_addr ( ) ) ;
588
+
589
+ if can_insert && is_not_in {
590
+ let t = outbound_connections. insert ( token, connection) ;
591
+ assert ! ( t. is_none( ) ) ;
592
+ io. register_stream ( token) ;
593
+ }
576
594
} else {
577
595
cwarn ! ( NETWORK , "Cannot establish an outbound connection" ) ;
578
596
}
0 commit comments