@@ -672,8 +672,15 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
672672
673673 /// Adds a `PeerId` and its address as reserved. The string should encode the address
674674 /// and peer ID of the remote node.
675+ ///
676+ /// Returns an `Err` if the given string is not a valid multiaddress
677+ /// or contains an invalid peer ID (which includes the local peer ID).
675678 pub fn add_reserved_peer ( & self , peer : String ) -> Result < ( ) , String > {
676679 let ( peer_id, addr) = parse_str_addr ( & peer) . map_err ( |e| format ! ( "{:?}" , e) ) ?;
680+ // Make sure the local peer ID is never added to the PSM.
681+ if peer_id == self . local_peer_id {
682+ return Err ( "Local peer ID cannot be added as a reserved peer." . to_string ( ) )
683+ }
677684 self . peerset . add_reserved_peer ( peer_id. clone ( ) ) ;
678685 let _ = self
679686 . to_worker
@@ -694,12 +701,26 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
694701 }
695702
696703 /// Modify a peerset priority group.
704+ ///
705+ /// Returns an `Err` if one of the given addresses contains an invalid
706+ /// peer ID (which includes the local peer ID).
697707 pub fn set_priority_group ( & self , group_id : String , peers : HashSet < Multiaddr > ) -> Result < ( ) , String > {
698- let peers = peers. into_iter ( ) . map ( |p| {
699- parse_addr ( p) . map_err ( |e| format ! ( "{:?}" , e) )
700- } ) . collect :: < Result < Vec < ( PeerId , Multiaddr ) > , String > > ( ) ?;
708+ let peers = peers. into_iter ( )
709+ . map ( |p| match parse_addr ( p) {
710+ Err ( e) => Err ( format ! ( "{:?}" , e) ) ,
711+ Ok ( ( peer, addr) ) =>
712+ // Make sure the local peer ID is never added to the PSM
713+ // or added as a "known address", even if given.
714+ if peer == self . local_peer_id {
715+ Err ( "Local peer ID in priority group." . to_string ( ) )
716+ } else {
717+ Ok ( ( peer, addr) )
718+ }
719+ } )
720+ . collect :: < Result < Vec < ( PeerId , Multiaddr ) > , String > > ( ) ?;
701721
702722 let peer_ids = peers. iter ( ) . map ( |( peer_id, _addr) | peer_id. clone ( ) ) . collect ( ) ;
723+
703724 self . peerset . set_priority_group ( group_id, peer_ids) ;
704725
705726 for ( peer_id, addr) in peers. into_iter ( ) {
0 commit comments