@@ -55,6 +55,13 @@ export interface DPTOptions {
55
55
*/
56
56
shouldFindNeighbours ?: boolean
57
57
58
+ /**
59
+ * Toggles whether or not peers should be discovered by querying EIP-1459 DNS lists
60
+ *
61
+ * Default: false
62
+ */
63
+ shouldGetDnsPeers ?: boolean
64
+
58
65
/**
59
66
* Max number of candidate peers to retrieve from DNS records when
60
67
* attempting to discover new nodes
@@ -87,6 +94,7 @@ export class DPT extends EventEmitter {
87
94
private _refreshIntervalId : NodeJS . Timeout
88
95
private _refreshIntervalSelectionCounter : number = 0
89
96
private _shouldFindNeighbours : boolean
97
+ private _shouldGetDnsPeers : boolean
90
98
private _dnsRefreshQuantity : number
91
99
private _dnsNetworks : string [ ]
92
100
private _dnsAddr : string
@@ -97,6 +105,7 @@ export class DPT extends EventEmitter {
97
105
this . privateKey = Buffer . from ( privateKey )
98
106
this . _id = pk2id ( Buffer . from ( publicKeyCreate ( this . privateKey , false ) ) )
99
107
this . _shouldFindNeighbours = options . shouldFindNeighbours === false ? false : true
108
+ this . _shouldGetDnsPeers = options . shouldGetDnsPeers ?? false
100
109
// By default, tries to connect to 12 new peers every 3s
101
110
this . _dnsRefreshQuantity = Math . floor ( ( options . dnsRefreshQuantity ?? 25 ) / 2 )
102
111
this . _dnsNetworks = options . dnsNetworks ?? [ ]
@@ -117,9 +126,15 @@ export class DPT extends EventEmitter {
117
126
} )
118
127
this . _server . once ( 'listening' , ( ) => this . emit ( 'listening' ) )
119
128
this . _server . once ( 'close' , ( ) => this . emit ( 'close' ) )
120
- this . _server . on ( 'peers' , ( peers ) => this . _onServerPeers ( peers ) )
121
129
this . _server . on ( 'error' , ( err ) => this . emit ( 'error' , err ) )
122
130
131
+ // When not using peer neighbour discovery we don't add peers here
132
+ // because it results in duplicate calls for the same targets
133
+ this . _server . on ( 'peers' , ( peers ) => {
134
+ if ( ! this . _shouldFindNeighbours ) return
135
+ this . _addPeerBatch ( peers )
136
+ } )
137
+
123
138
// By default calls refresh every 3s
124
139
const refreshIntervalSubdivided = Math . floor ( ( options . refreshInterval ?? ms ( '60s' ) ) / 10 )
125
140
this . _refreshIntervalId = setInterval ( ( ) => this . refresh ( ) , refreshIntervalSubdivided )
@@ -155,12 +170,7 @@ export class DPT extends EventEmitter {
155
170
}
156
171
}
157
172
158
- _onServerPeers ( peers : PeerInfo [ ] ) : void {
159
- // We don't want this to run when using
160
- // dns - it's fired in the server.on:peer hook
161
- // and results in duplicate addition attempts
162
- if ( ! this . _shouldFindNeighbours ) return
163
-
173
+ _addPeerBatch ( peers : PeerInfo [ ] ) : void {
164
174
const DIFF_TIME_MS = 200
165
175
let ms = 0
166
176
for ( const peer of peers ) {
@@ -251,16 +261,16 @@ export class DPT extends EventEmitter {
251
261
}
252
262
}
253
263
254
- const dnsPeers = await this . getDnsPeers ( )
264
+ if ( this . _shouldGetDnsPeers ) {
265
+ const dnsPeers = await this . getDnsPeers ( )
255
266
256
- debug (
257
- `.refresh() ( Adding ${ dnsPeers . length } ) from DNS tree, (${
258
- this . getPeers ( ) . length
259
- } current peers in table)`
260
- )
267
+ debug (
268
+ `.refresh() Adding ${ dnsPeers . length } from DNS tree, (${
269
+ this . getPeers ( ) . length
270
+ } current peers in table)`
271
+ )
261
272
262
- for ( const peer of dnsPeers ) {
263
- await this . bootstrap ( peer )
273
+ this . _addPeerBatch ( dnsPeers )
264
274
}
265
275
}
266
276
}
0 commit comments