@@ -29,26 +29,15 @@ export class FullNodePeer {
29
29
private static deprioritizedIps : Set < string > = new Set ( ) ; // New set for deprioritized IPs
30
30
31
31
static {
32
- FullNodePeer . memoizedFetchNewPeerIPs = memoize (
33
- FullNodePeer . fetchNewPeerIPs
34
- ) ;
32
+ FullNodePeer . memoizedFetchNewPeerIPs = memoize ( FullNodePeer . fetchNewPeerIPs ) ;
35
33
}
36
34
37
35
private constructor ( peer : Peer ) {
38
36
this . peer = peer ;
39
37
}
40
38
41
- /**
42
- * Connect to the best peer, optionally using custom certificate and key file paths.
43
- * @param {string } [certFilePath] - Optional custom certificate file path.
44
- * @param {string } [keyFilePath] - Optional custom key file path.
45
- * @returns {Promise<Peer> } The connected peer.
46
- */
47
- public static async connect (
48
- certFilePath ?: string ,
49
- keyFilePath ?: string
50
- ) : Promise < Peer > {
51
- const peer = await FullNodePeer . getBestPeer ( certFilePath , keyFilePath ) ;
39
+ public static async connect ( ) : Promise < Peer > {
40
+ const peer = await FullNodePeer . getBestPeer ( ) ;
52
41
return new FullNodePeer ( peer ) . peer ;
53
42
}
54
43
@@ -143,9 +132,7 @@ export class FullNodePeer {
143
132
}
144
133
}
145
134
} catch ( error : any ) {
146
- console . error (
147
- `Failed to resolve IPs from ${ DNS_HOST } : ${ error . message } `
148
- ) ;
135
+ console . error ( `Failed to resolve IPs from ${ DNS_HOST } : ${ error . message } ` ) ;
149
136
}
150
137
}
151
138
throw new Error ( "No reachable IPs found in any DNS records." ) ;
@@ -191,9 +178,7 @@ export class FullNodePeer {
191
178
const timeoutPromise = new Promise < null > ( ( _ , reject ) => {
192
179
timeoutId = setTimeout ( ( ) => {
193
180
FullNodePeer . cachedPeer = null ;
194
- reject (
195
- new Error ( "Operation timed out. Reconnecting to a new peer." )
196
- ) ;
181
+ reject ( new Error ( "Operation timed out. Reconnecting to a new peer." ) ) ;
197
182
} , 60000 ) ; // 1 minute
198
183
} ) ;
199
184
@@ -212,17 +197,12 @@ export class FullNodePeer {
212
197
return result ;
213
198
} catch ( error : any ) {
214
199
// If the error is WebSocket-related or timeout, reset the peer
215
- if (
216
- error . message . includes ( "WebSocket" ) ||
217
- error . message . includes ( "Operation timed out" )
218
- ) {
200
+ if ( error . message . includes ( "WebSocket" ) || error . message . includes ( "Operation timed out" ) ) {
219
201
FullNodePeer . cachedPeer = null ;
220
202
// @ts -ignore
221
203
FullNodePeer . memoizedFetchNewPeerIPs . cache . clear ( ) ;
222
204
FullNodePeer . deprioritizedIps . clear ( ) ;
223
- console . info (
224
- `Fullnode Peer error, reconnecting to a new peer...`
225
- ) ;
205
+ console . info ( `Fullnode Peer error, reconnecting to a new peer...` ) ;
226
206
const newPeer = await FullNodePeer . getBestPeer ( ) ;
227
207
return ( newPeer as any ) [ prop ] ( ...args ) ;
228
208
}
@@ -235,10 +215,7 @@ export class FullNodePeer {
235
215
} ) ;
236
216
}
237
217
238
- private static async getBestPeer (
239
- certFilePath ?: string ,
240
- keyFilePath ?: string
241
- ) : Promise < Peer > {
218
+ private static async getBestPeer ( ) : Promise < Peer > {
242
219
const now = Date . now ( ) ;
243
220
244
221
if (
@@ -248,29 +225,12 @@ export class FullNodePeer {
248
225
return FullNodePeer . cachedPeer . peer ;
249
226
}
250
227
251
- let certFile : string ;
252
- let keyFile : string ;
253
-
254
- // If certFilePath or keyFilePath is provided, ensure both are provided
255
- if ( certFilePath && keyFilePath ) {
256
- certFile = certFilePath ;
257
- keyFile = keyFilePath ;
228
+ const sslFolder = path . resolve ( os . homedir ( ) , ".dig" , "ssl" ) ;
229
+ const certFile = path . join ( sslFolder , "public_dig.crt" ) ;
230
+ const keyFile = path . join ( sslFolder , "public_dig.key" ) ;
258
231
259
- if ( ! fs . existsSync ( certFile ) ) {
260
- throw new Error ( `Certificate file not found: ${ certFile } ` ) ;
261
- }
262
- if ( ! fs . existsSync ( keyFile ) ) {
263
- throw new Error ( `Key file not found: ${ keyFile } ` ) ;
264
- }
265
- } else {
266
- // Use default paths if no custom paths are provided
267
- const sslFolder = path . resolve ( os . homedir ( ) , ".dig" , "ssl" ) ;
268
- certFile = path . join ( sslFolder , "public_dig.crt" ) ;
269
- keyFile = path . join ( sslFolder , "public_dig.key" ) ;
270
-
271
- if ( ! fs . existsSync ( sslFolder ) ) {
272
- throw new Error ( `SSL folder not found: ${ sslFolder } ` ) ;
273
- }
232
+ if ( ! fs . existsSync ( sslFolder ) ) {
233
+ fs . mkdirSync ( sslFolder , { recursive : true } ) ;
274
234
}
275
235
276
236
new Tls ( certFile , keyFile ) ;
@@ -298,9 +258,7 @@ export class FullNodePeer {
298
258
) ;
299
259
return FullNodePeer . createPeerProxy ( peer ) ;
300
260
} catch ( error : any ) {
301
- console . error (
302
- `Failed to create peer for IP ${ ip } : ${ error . message } `
303
- ) ;
261
+ console . error ( `Failed to create peer for IP ${ ip } : ${ error . message } ` ) ;
304
262
return null ;
305
263
}
306
264
}
@@ -341,9 +299,7 @@ export class FullNodePeer {
341
299
( height , index ) =>
342
300
height === highestPeak &&
343
301
! FullNodePeer . deprioritizedIps . has ( peerIPs [ index ] ) && // Exclude deprioritized IPs
344
- ( peerIPs [ index ] === LOCALHOST ||
345
- peerIPs [ index ] === trustedNodeIp ||
346
- peerIPs [ index ] === CHIA_NODES_HOST )
302
+ ( peerIPs [ index ] === LOCALHOST || peerIPs [ index ] === trustedNodeIp || peerIPs [ index ] === CHIA_NODES_HOST )
347
303
) ;
348
304
349
305
// If LOCALHOST, TRUSTED_NODE_IP, or CHIA_NODES_HOST don't have the highest peak, select any peer with the highest peak
0 commit comments