@@ -5,6 +5,7 @@ use crate::peer_conn_handler::{
5
5
use crate :: peer_manager:: { PeerEvents , PeerManagerOut , Peers } ;
6
6
use crate :: protocol:: ProtocolConfig ;
7
7
use crate :: protocol_api:: ProtocolEvents ;
8
+ use crate :: protocol_upgrade:: supported_protocol_vers:: SupportedProtocolIdMap ;
8
9
use crate :: types:: { ProtocolId , ProtocolVer } ;
9
10
10
11
use libp2p:: core:: connection:: ConnectionId ;
@@ -215,7 +216,7 @@ impl<TPeers, TPeerManager, THandler> NetworkEvents for NetworkController<TPeers,
215
216
pub struct NetworkController < TPeers , TPeerManager , THandler > {
216
217
conn_handler_conf : PeerConnHandlerConf ,
217
218
/// All supported protocols and their handlers
218
- supported_protocols : HashMap < ProtocolId , ( ProtocolConfig , THandler ) > ,
219
+ supported_protocols : SupportedProtocolIdMap < ( ProtocolConfig , THandler ) > ,
219
220
/// PeerManager API
220
221
peers : TPeers ,
221
222
/// PeerManager stream itself
@@ -240,7 +241,7 @@ where
240
241
) -> Self {
241
242
Self {
242
243
conn_handler_conf,
243
- supported_protocols,
244
+ supported_protocols : supported_protocols . into ( ) ,
244
245
peers,
245
246
peer_manager,
246
247
enabled_peers : HashMap :: new ( ) ,
@@ -254,8 +255,7 @@ where
254
255
self . conn_handler_conf . clone ( ) ,
255
256
self . supported_protocols
256
257
. iter ( )
257
- . clone ( )
258
- . map ( |( prot_id, ( conf, _) ) | ( * prot_id, conf. clone ( ) ) )
258
+ . map ( |( prot_id, ( conf, _) ) | ( prot_id, conf. clone ( ) ) )
259
259
. collect :: < Vec < _ > > ( ) ,
260
260
)
261
261
}
@@ -361,7 +361,7 @@ where
361
361
{
362
362
let protocol_id = protocol_tag. protocol_id ( ) ;
363
363
let protocol_ver = protocol_tag. protocol_ver ( ) ;
364
- match enabled_protocols. entry ( protocol_id) {
364
+ match enabled_protocols. entry ( protocol_id. get_inner ( ) ) {
365
365
Entry :: Occupied ( mut entry) => {
366
366
trace ! (
367
367
"Current state of protocol {:?} is {:?}" ,
@@ -371,12 +371,12 @@ where
371
371
if let ( EnabledProtocol :: PendingEnable , handler) = entry. get ( ) {
372
372
handler. protocol_enabled (
373
373
peer_id,
374
- protocol_ver,
374
+ protocol_ver. get_inner ( ) ,
375
375
out_channel. clone ( ) ,
376
376
handshake,
377
377
) ;
378
378
let enabled_protocol = EnabledProtocol :: Enabled {
379
- ver : protocol_ver,
379
+ ver : protocol_ver. get_inner ( ) ,
380
380
sink : out_channel,
381
381
} ;
382
382
entry. insert ( ( enabled_protocol, handler. clone ( ) ) ) ;
@@ -398,42 +398,51 @@ where
398
398
} ) = self . enabled_peers . get_mut ( & peer_id)
399
399
{
400
400
let protocol_id = protocol_tag. protocol_id ( ) ;
401
- if let Some ( ( _, prot_handler) ) = self . supported_protocols . get ( & protocol_id) {
402
- match enabled_protocols. entry ( protocol_id) {
403
- Entry :: Vacant ( entry) => {
404
- entry. insert ( ( EnabledProtocol :: PendingApprove , prot_handler. clone ( ) ) ) ;
405
- prot_handler. protocol_requested (
401
+ let ( _, prot_handler) = self . supported_protocols . get_supported ( protocol_id) ;
402
+ match enabled_protocols. entry ( protocol_id. get_inner ( ) ) {
403
+ Entry :: Vacant ( entry) => {
404
+ entry. insert ( ( EnabledProtocol :: PendingApprove , prot_handler. clone ( ) ) ) ;
405
+ prot_handler. protocol_requested (
406
+ peer_id,
407
+ protocol_tag. protocol_ver ( ) . get_inner ( ) ,
408
+ handshake,
409
+ ) ;
410
+ }
411
+ Entry :: Occupied ( _) => {
412
+ warn ! (
413
+ "Peer {:?} opened already enabled protocol {:?}" ,
414
+ peer_id, protocol_id
415
+ ) ;
416
+ self . pending_actions
417
+ . push_back ( NetworkBehaviourAction :: NotifyHandler {
406
418
peer_id,
407
- protocol_tag. protocol_ver ( ) ,
408
- handshake,
409
- ) ;
410
- }
411
- Entry :: Occupied ( _) => {
412
- warn ! (
413
- "Peer {:?} opened already enabled protocol {:?}" ,
414
- peer_id, protocol_id
415
- ) ;
416
- self . pending_actions
417
- . push_back ( NetworkBehaviourAction :: NotifyHandler {
418
- peer_id,
419
- handler : NotifyHandler :: One ( connection) ,
420
- event : ConnHandlerIn :: Close ( protocol_id) ,
421
- } )
422
- }
419
+ handler : NotifyHandler :: One ( connection) ,
420
+ event : ConnHandlerIn :: Close ( protocol_id) ,
421
+ } )
423
422
}
424
- } else {
425
- self . pending_actions
426
- . push_back ( NetworkBehaviourAction :: NotifyHandler {
423
+ }
424
+ }
425
+ }
426
+ ConnHandlerOut :: ClosedByPeer ( protocol_id) | ConnHandlerOut :: RefusedToOpen ( protocol_id) => {
427
+ if let Some ( ConnectedPeer :: Connected {
428
+ enabled_protocols, ..
429
+ } ) = self . enabled_peers . get_mut ( & peer_id)
430
+ {
431
+ match enabled_protocols. entry ( protocol_id. get_inner ( ) ) {
432
+ Entry :: Occupied ( entry) => {
433
+ trace ! (
434
+ "Peer {:?} closed the substream for protocol {:?}" ,
427
435
peer_id,
428
- handler : NotifyHandler :: One ( connection) ,
429
- event : ConnHandlerIn :: Close ( protocol_id) ,
430
- } )
436
+ protocol_id
437
+ ) ;
438
+ entry. remove ( ) ;
439
+ }
440
+ Entry :: Vacant ( _) => { }
431
441
}
432
442
}
433
443
}
434
- ConnHandlerOut :: ClosedByPeer ( protocol_id)
435
- | ConnHandlerOut :: RefusedToOpen ( protocol_id)
436
- | ConnHandlerOut :: Closed ( protocol_id) => {
444
+
445
+ ConnHandlerOut :: Closed ( protocol_id) => {
437
446
if let Some ( ConnectedPeer :: Connected {
438
447
enabled_protocols, ..
439
448
} ) = self . enabled_peers . get_mut ( & peer_id)
@@ -564,7 +573,7 @@ where
564
573
ConnectedPeer :: Connected {
565
574
enabled_protocols, ..
566
575
} => {
567
- if let Some ( ( _, prot_handler) ) = self . supported_protocols . get ( & protocol) {
576
+ if let Some ( ( _, prot_handler) ) = self . supported_protocols . get ( protocol) {
568
577
match enabled_protocols. entry ( protocol) {
569
578
Entry :: Occupied ( _) => warn ! (
570
579
"PM requested already enabled protocol {:?} with peer {:?}" ,
@@ -617,7 +626,7 @@ where
617
626
enabled_protocols,
618
627
} ) = self . enabled_peers . get_mut ( & peer_id)
619
628
{
620
- let ( _, prot_handler) = self . supported_protocols . get ( & protocol_id) . unwrap ( ) ;
629
+ let ( _, prot_handler) = self . supported_protocols . get ( protocol_id) . unwrap ( ) ;
621
630
match enabled_protocols. entry ( protocol_id) {
622
631
Entry :: Occupied ( protocol_entry) => match protocol_entry. remove_entry ( ) . 1 {
623
632
// Protocol handler approves either outbound or inbound protocol request.
0 commit comments