@@ -506,6 +506,7 @@ impl IoHandler<Message> for Handler {
506506 is_inbound : true ,
507507 } => {
508508 let mut inbound_connections = self . inbound_connections . write ( ) ;
509+ let outbound_connections = self . outbound_connections . write ( ) ;
509510 let target = connection. peer_addr ( ) ;
510511 self . peer_db . insert ( * target) ;
511512 if let Some ( token) = self . inbound_tokens . lock ( ) . gen ( ) {
@@ -526,10 +527,17 @@ impl IoHandler<Message> for Handler {
526527 token
527528 ) ;
528529 }
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+ }
533541 } else {
534542 cwarn ! ( NETWORK , "Cannot establish an inbound connection" ) ;
535543 }
@@ -539,6 +547,7 @@ impl IoHandler<Message> for Handler {
539547 is_inbound : false ,
540548 } => {
541549 let mut outbound_connections = self . outbound_connections . write ( ) ;
550+ let inbound_connections = self . inbound_connections . write ( ) ;
542551 if let Some ( token) = self . outbound_tokens . lock ( ) . gen ( ) {
543552 let peer_addr = * connection. peer_addr ( ) ;
544553 let remote_node_id = peer_addr. into ( ) ;
@@ -570,9 +579,18 @@ impl IoHandler<Message> for Handler {
570579 network_message_size,
571580 ) ;
572581 }
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+ }
576594 } else {
577595 cwarn ! ( NETWORK , "Cannot establish an outbound connection" ) ;
578596 }
0 commit comments