@@ -174,51 +174,67 @@ export class ConnectionManager
174
174
private async dialPeer ( peerId : PeerId ) : Promise < void > {
175
175
this . currentActiveDialCount += 1 ;
176
176
let dialAttempt = 0 ;
177
- while ( dialAttempt <= this . options . maxDialAttemptsForPeer ) {
177
+ while ( dialAttempt < this . options . maxDialAttemptsForPeer ) {
178
178
try {
179
- log ( `Dialing peer ${ peerId . toString ( ) } ` ) ;
179
+ log ( `Dialing peer ${ peerId . toString ( ) } on attempt ${ dialAttempt + 1 } ` ) ;
180
180
await this . libp2p . dial ( peerId ) ;
181
181
182
182
const tags = await this . getTagNamesForPeer ( peerId ) ;
183
183
// add tag to connection describing discovery mechanism
184
184
// don't add duplicate tags
185
- this . libp2p
186
- . getConnections ( peerId )
187
- . forEach (
188
- ( conn ) => ( conn . tags = Array . from ( new Set ( [ ...conn . tags , ...tags ] ) ) )
189
- ) ;
185
+ this . libp2p . getConnections ( peerId ) . forEach ( ( conn ) => {
186
+ conn . tags = Array . from ( new Set ( [ ...conn . tags , ...tags ] ) ) ;
187
+ } ) ;
190
188
191
189
this . dialAttemptsForPeer . delete ( peerId . toString ( ) ) ;
192
- return ;
193
- } catch ( e ) {
194
- const error = e as AggregateError ;
195
-
190
+ // Dialing succeeded, break the loop
191
+ break ;
192
+ } catch ( error ) {
193
+ if ( error instanceof AggregateError ) {
194
+ // Handle AggregateError
195
+ log ( `Error dialing peer ${ peerId . toString ( ) } - ${ error . errors } ` ) ;
196
+ } else {
197
+ // Handle generic error
198
+ log (
199
+ `Error dialing peer ${ peerId . toString ( ) } - ${
200
+ ( error as any ) . message
201
+ } `
202
+ ) ;
203
+ }
196
204
this . dialErrorsForPeer . set ( peerId . toString ( ) , error ) ;
197
- log ( `Error dialing peer ${ peerId . toString ( ) } - ${ error . errors } ` ) ;
198
205
199
- dialAttempt = this . dialAttemptsForPeer . get ( peerId . toString ( ) ) ?? 1 ;
200
- this . dialAttemptsForPeer . set ( peerId . toString ( ) , dialAttempt + 1 ) ;
201
-
202
- if ( dialAttempt <= this . options . maxDialAttemptsForPeer ) {
203
- log ( `Reattempting dial (${ dialAttempt } )` ) ;
204
- }
206
+ dialAttempt ++ ;
207
+ this . dialAttemptsForPeer . set ( peerId . toString ( ) , dialAttempt ) ;
205
208
}
206
209
}
207
210
208
- try {
209
- log (
210
- `Deleting undialable peer ${ peerId . toString ( ) } from peer store. Error: ${ JSON . stringify (
211
- this . dialErrorsForPeer . get ( peerId . toString ( ) ) . errors [ 0 ]
212
- ) }
213
- }`
214
- ) ;
215
- this . dialErrorsForPeer . delete ( peerId . toString ( ) ) ;
216
- return await this . libp2p . peerStore . delete ( peerId ) ;
217
- } catch ( error ) {
218
- throw `Error deleting undialable peer ${ peerId . toString ( ) } from peer store - ${ error } ` ;
219
- } finally {
220
- this . currentActiveDialCount -= 1 ;
221
- this . processDialQueue ( ) ;
211
+ // Always decrease the active dial count and process the dial queue
212
+ this . currentActiveDialCount -- ;
213
+ this . processDialQueue ( ) ;
214
+
215
+ // If max dial attempts reached and dialing failed, delete the peer
216
+ if ( dialAttempt === this . options . maxDialAttemptsForPeer ) {
217
+ try {
218
+ const error = this . dialErrorsForPeer . get ( peerId . toString ( ) ) ;
219
+
220
+ let errorMessage ;
221
+ if ( error instanceof AggregateError ) {
222
+ errorMessage = JSON . stringify ( error . errors [ 0 ] ) ;
223
+ } else {
224
+ errorMessage = error . message ;
225
+ }
226
+
227
+ log (
228
+ `Deleting undialable peer ${ peerId . toString ( ) } from peer store. Error: ${ errorMessage } `
229
+ ) ;
230
+
231
+ this . dialErrorsForPeer . delete ( peerId . toString ( ) ) ;
232
+ await this . libp2p . peerStore . delete ( peerId ) ;
233
+ } catch ( error ) {
234
+ throw new Error (
235
+ `Error deleting undialable peer ${ peerId . toString ( ) } from peer store - ${ error } `
236
+ ) ;
237
+ }
222
238
}
223
239
}
224
240
@@ -302,25 +318,16 @@ export class ConnectionManager
302
318
Tags . BOOTSTRAP
303
319
) ;
304
320
305
- if ( isBootstrap ) {
306
- this . dispatchEvent (
307
- new CustomEvent < PeerId > (
308
- EPeersByDiscoveryEvents . PEER_DISCOVERY_BOOTSTRAP ,
309
- {
310
- detail : peerId ,
311
- }
312
- )
313
- ) ;
314
- } else {
315
- this . dispatchEvent (
316
- new CustomEvent < PeerId > (
317
- EPeersByDiscoveryEvents . PEER_DISCOVERY_PEER_EXCHANGE ,
318
- {
319
- detail : peerId ,
320
- }
321
- )
322
- ) ;
323
- }
321
+ this . dispatchEvent (
322
+ new CustomEvent < PeerId > (
323
+ isBootstrap
324
+ ? EPeersByDiscoveryEvents . PEER_DISCOVERY_BOOTSTRAP
325
+ : EPeersByDiscoveryEvents . PEER_DISCOVERY_PEER_EXCHANGE ,
326
+ {
327
+ detail : peerId ,
328
+ }
329
+ )
330
+ ) ;
324
331
325
332
try {
326
333
await this . attemptDial ( peerId ) ;
0 commit comments