@@ -7,14 +7,18 @@ import { multiaddr } from '@multiformats/multiaddr'
7
7
import { DNSMappings } from './dns-mappings.js'
8
8
import { IPMappings } from './ip-mappings.js'
9
9
import { ObservedAddresses } from './observed-addresses.js'
10
+ import { TransportAddresses } from './transport-addresses.js'
10
11
import type { ComponentLogger , Libp2pEvents , Logger , TypedEventTarget , PeerId , PeerStore } from '@libp2p/interface'
11
- import type { AddressManager as AddressManagerInterface , TransportManager , NodeAddress } from '@libp2p/interface-internal'
12
+ import type { AddressManager as AddressManagerInterface , TransportManager , NodeAddress , ConfirmAddressOptions } from '@libp2p/interface-internal'
12
13
import type { Filter } from '@libp2p/utils/filters'
13
14
import type { Multiaddr } from '@multiformats/multiaddr'
14
15
16
+ const ONE_MINUTE = 60_000
17
+
15
18
export const defaultValues = {
16
19
maxObservedAddresses : 10 ,
17
- observedAddressTTL : 60_000 * 10
20
+ addressVerificationTTL : ONE_MINUTE * 10 ,
21
+ addressVerificationRetry : ONE_MINUTE * 5
18
22
}
19
23
20
24
export interface AddressManagerInit {
@@ -50,9 +54,25 @@ export interface AddressManagerInit {
50
54
maxObservedAddresses ?: number
51
55
52
56
/**
53
- * How long before each observed address should be reverified
57
+ * How long before each public address should be reverified in ms.
58
+ *
59
+ * Requires `@libp2p/autonat` or some other verification method to be
60
+ * configured.
61
+ *
62
+ * @default 600_000
63
+ */
64
+ addressVerificationTTL ?: number
65
+
66
+ /**
67
+ * After a transport or mapped address has failed to verify, how long to wait
68
+ * before retrying it in ms
69
+ *
70
+ * Requires `@libp2p/autonat` or some other verification method to be
71
+ * configured.
72
+ *
73
+ * @default 300_000
54
74
*/
55
- observedAddressTTL ?: number
75
+ addressVerificationRetry ?: number
56
76
}
57
77
58
78
export interface AddressManagerComponents {
@@ -103,8 +123,10 @@ export class AddressManager implements AddressManagerInterface {
103
123
private readonly observed : ObservedAddresses
104
124
private readonly dnsMappings : DNSMappings
105
125
private readonly ipMappings : IPMappings
126
+ private readonly transportAddresses : TransportAddresses
106
127
private readonly observedAddressFilter : Filter
107
- private readonly observedAddressTTL : number
128
+ private readonly addressVerificationTTL : number
129
+ private readonly addressVerificationRetry : number
108
130
109
131
/**
110
132
* Responsible for managing the peer addresses.
@@ -123,9 +145,11 @@ export class AddressManager implements AddressManagerInterface {
123
145
this . observed = new ObservedAddresses ( components , init )
124
146
this . dnsMappings = new DNSMappings ( components , init )
125
147
this . ipMappings = new IPMappings ( components , init )
148
+ this . transportAddresses = new TransportAddresses ( components , init )
126
149
this . announceFilter = init . announceFilter ?? defaultAddressFilter
127
150
this . observedAddressFilter = createScalableCuckooFilter ( 1024 )
128
- this . observedAddressTTL = init . observedAddressTTL ?? defaultValues . observedAddressTTL
151
+ this . addressVerificationTTL = init . addressVerificationTTL ?? defaultValues . addressVerificationTTL
152
+ this . addressVerificationRetry = init . addressVerificationRetry ?? defaultValues . addressVerificationRetry
129
153
130
154
// this method gets called repeatedly on startup when transports start listening so
131
155
// debounce it so we don't cause multiple self:peer:update events to be emitted
@@ -221,20 +245,24 @@ export class AddressManager implements AddressManagerInterface {
221
245
this . observed . add ( addr )
222
246
}
223
247
224
- confirmObservedAddr ( addr : Multiaddr ) : void {
248
+ confirmObservedAddr ( addr : Multiaddr , options ?: ConfirmAddressOptions ) : void {
225
249
addr = stripPeerId ( addr , this . components . peerId )
226
250
let startingConfidence = true
227
251
228
252
if ( this . observed . has ( addr ) ) {
229
- startingConfidence = this . observed . confirm ( addr , this . observedAddressTTL )
253
+ startingConfidence = this . observed . confirm ( addr , options ?. ttl ?? this . addressVerificationTTL )
254
+ }
255
+
256
+ if ( this . transportAddresses . has ( addr ) ) {
257
+ startingConfidence = this . transportAddresses . confirm ( addr , options ?. ttl ?? this . addressVerificationTTL )
230
258
}
231
259
232
260
if ( this . dnsMappings . has ( addr ) ) {
233
- startingConfidence = this . dnsMappings . confirm ( addr , this . observedAddressTTL )
261
+ startingConfidence = this . dnsMappings . confirm ( addr , options ?. ttl ?? this . addressVerificationTTL )
234
262
}
235
263
236
264
if ( this . ipMappings . has ( addr ) ) {
237
- startingConfidence = this . ipMappings . confirm ( addr , this . observedAddressTTL )
265
+ startingConfidence = this . ipMappings . confirm ( addr , options ?. ttl ?? this . addressVerificationTTL )
238
266
}
239
267
240
268
// only trigger the 'self:peer:update' event if our confidence in an address has changed
@@ -243,12 +271,31 @@ export class AddressManager implements AddressManagerInterface {
243
271
}
244
272
}
245
273
246
- removeObservedAddr ( addr : Multiaddr ) : void {
274
+ removeObservedAddr ( addr : Multiaddr , options ?: ConfirmAddressOptions ) : void {
247
275
addr = stripPeerId ( addr , this . components . peerId )
248
276
249
- this . observed . remove ( addr )
250
- this . dnsMappings . remove ( addr )
251
- this . ipMappings . remove ( addr )
277
+ let startingConfidence = false
278
+
279
+ if ( this . observed . has ( addr ) ) {
280
+ startingConfidence = this . observed . remove ( addr )
281
+ }
282
+
283
+ if ( this . transportAddresses . has ( addr ) ) {
284
+ startingConfidence = this . transportAddresses . unconfirm ( addr , options ?. ttl ?? this . addressVerificationRetry )
285
+ }
286
+
287
+ if ( this . dnsMappings . has ( addr ) ) {
288
+ startingConfidence = this . dnsMappings . unconfirm ( addr , options ?. ttl ?? this . addressVerificationRetry )
289
+ }
290
+
291
+ if ( this . ipMappings . has ( addr ) ) {
292
+ startingConfidence = this . ipMappings . unconfirm ( addr , options ?. ttl ?? this . addressVerificationRetry )
293
+ }
294
+
295
+ // only trigger the 'self:peer:update' event if our confidence in an address has changed
296
+ if ( startingConfidence ) {
297
+ this . _updatePeerStoreAddresses ( )
298
+ }
252
299
}
253
300
254
301
getAddresses ( ) : Multiaddr [ ] {
@@ -299,20 +346,17 @@ export class AddressManager implements AddressManagerInterface {
299
346
multiaddr,
300
347
verified : true ,
301
348
type : 'announce' ,
302
- expires : Date . now ( ) + this . observedAddressTTL
349
+ expires : Date . now ( ) + this . addressVerificationTTL ,
350
+ lastVerified : Date . now ( )
303
351
} ) )
304
352
}
305
353
306
354
let addresses : NodeAddress [ ] = [ ]
307
355
308
356
// add transport addresses
309
357
addresses = addresses . concat (
310
- this . components . transportManager . getAddrs ( ) . map ( multiaddr => ( {
311
- multiaddr,
312
- verified : true ,
313
- type : 'transport' ,
314
- expires : Date . now ( ) + this . observedAddressTTL
315
- } ) )
358
+ this . components . transportManager . getAddrs ( )
359
+ . map ( multiaddr => this . transportAddresses . get ( multiaddr , this . addressVerificationTTL ) )
316
360
)
317
361
318
362
// add append announce addresses
@@ -321,7 +365,8 @@ export class AddressManager implements AddressManagerInterface {
321
365
multiaddr,
322
366
verified : true ,
323
367
type : 'announce' ,
324
- expires : Date . now ( ) + this . observedAddressTTL
368
+ expires : Date . now ( ) + this . addressVerificationTTL ,
369
+ lastVerified : Date . now ( )
325
370
} ) )
326
371
)
327
372
0 commit comments