1- use libp2p:: { multiaddr:: Protocol , swarm:: SwarmEvent } ;
1+ use libp2p:: { core :: Endpoint , multiaddr:: Protocol , swarm:: SwarmEvent } ;
22use tracing:: { debug, error, info, warn} ;
33
44use crate :: { error:: P2PError , event:: ComposedEvent , Event , Runtime } ;
@@ -62,13 +62,13 @@ impl EventHandler<SwarmEvent<ComposedEvent>> for Runtime {
6262 error,
6363 } if self
6464 . health_state
65- . successfully_connected_to_bootpeer
65+ . successfully_connected_to_bootnode
6666 . is_none ( )
67- && self . health_state . dialed_bootpeer . contains ( & connection_id) =>
67+ && self . health_state . dialed_bootnode . contains ( & connection_id) =>
6868 {
69- warn ! ( "Unable to connect to bootpeer {peer_id}: {error:?}" ) ;
70- self . health_state . dialed_bootpeer . remove ( & connection_id) ;
71- if self . health_state . dialed_bootpeer . is_empty ( ) {
69+ warn ! ( "Unable to connect to bootnode {peer_id}: {error:?}" ) ;
70+ self . health_state . dialed_bootnode . remove ( & connection_id) ;
71+ if self . health_state . dialed_bootnode . is_empty ( ) {
7272 // We tried to connect to all bootnode without success
7373 error ! ( "Unable to connect to any bootnode" ) ;
7474 }
@@ -100,25 +100,49 @@ impl EventHandler<SwarmEvent<ComposedEvent>> for Runtime {
100100 num_established,
101101 concurrent_dial_errors,
102102 established_in,
103- } if self . health_state . dialed_bootpeer . contains ( & connection_id) => {
104- info ! ( "Successfully connected to bootpeer {peer_id}" ) ;
103+ } if self . health_state . dialed_bootnode . contains ( & connection_id) => {
104+ info ! ( "Successfully connected to bootnode {peer_id}" ) ;
105105 if self
106106 . health_state
107- . successfully_connected_to_bootpeer
107+ . successfully_connected_to_bootnode
108108 . is_none ( )
109109 {
110- self . health_state . successfully_connected_to_bootpeer = Some ( connection_id) ;
111- _ = self . health_state . dialed_bootpeer . remove ( & connection_id) ;
110+ self . health_state . successfully_connected_to_bootnode = Some ( connection_id) ;
111+ _ = self . health_state . dialed_bootnode . remove ( & connection_id) ;
112112 }
113113 }
114114
115115 SwarmEvent :: ConnectionEstablished {
116- peer_id, endpoint, ..
116+ peer_id,
117+ endpoint,
118+ connection_id,
119+ ..
117120 } => {
118- info ! (
119- "Connection established with peer {peer_id} as {:?}" ,
120- endpoint. to_endpoint( )
121- ) ;
121+ if self
122+ . health_state
123+ . successfully_connected_to_bootnode
124+ . is_none ( )
125+ && self . boot_peers . contains ( & peer_id)
126+ {
127+ info ! (
128+ "Connection established with bootnode {peer_id} as {:?}" ,
129+ endpoint. to_endpoint( )
130+ ) ;
131+
132+ if endpoint. to_endpoint ( ) == Endpoint :: Listener {
133+ if let Err ( error) = self . swarm . dial ( peer_id) {
134+ error ! (
135+ "Unable to dial bootnode {peer_id} after incoming connection: \
136+ {error}"
137+ ) ;
138+ }
139+ }
140+ } else {
141+ info ! (
142+ "Connection established with peer {peer_id} as {:?}" ,
143+ endpoint. to_endpoint( )
144+ ) ;
145+ }
122146
123147 if self . swarm . connected_peers ( ) . count ( ) >= self . config . minimum_cluster_size {
124148 if let Err ( error) = self . swarm . behaviour_mut ( ) . gossipsub . subscribe ( ) {
@@ -164,8 +188,8 @@ impl EventHandler<SwarmEvent<ComposedEvent>> for Runtime {
164188 peer_id : Some ( ref peer_id) ,
165189 connection_id,
166190 } if self . boot_peers . contains ( peer_id) => {
167- info ! ( "Dialing bootpeer {peer_id} on connection: {connection_id}" ) ;
168- self . health_state . dialed_bootpeer . insert ( connection_id) ;
191+ info ! ( "Dialing bootnode {peer_id} on connection: {connection_id}" ) ;
192+ self . health_state . dialed_bootnode . insert ( connection_id) ;
169193 }
170194
171195 SwarmEvent :: Dialing {
@@ -185,6 +209,7 @@ impl EventHandler<SwarmEvent<ComposedEvent>> for Runtime {
185209 SwarmEvent :: ListenerError { listener_id, error } => {
186210 error ! ( "Unhandled ListenerError {listener_id:?} | {error}" )
187211 }
212+
188213 event => {
189214 warn ! ( "Unhandled SwarmEvent: {:?}" , event) ;
190215 }
0 commit comments