libp2p 0.45.9-05abd49f → 0.45.9-06f4901a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -258,7 +258,7 @@ class DefaultAutoNATService implements Startable {
258
258
  .filter(ma => {
259
259
  const isFromSameHost = ma.toOptions().host === data.connection.remoteAddr.toOptions().host
260
260
 
261
- log.trace('request to dial %s was sent from %s is same host %s', ma, data.connection.remoteAddr, isFromSameHost)
261
+ log.trace('request to dial %a was sent from %a is same host %s', ma, data.connection.remoteAddr, isFromSameHost)
262
262
  // skip any Multiaddrs where the target node's IP does not match the sending node's IP
263
263
  return isFromSameHost
264
264
  })
@@ -281,7 +281,7 @@ class DefaultAutoNATService implements Startable {
281
281
  .filter(ma => {
282
282
  const isSupportedTransport = Boolean(self.components.transportManager.transportForMultiaddr(ma))
283
283
 
284
- log.trace('transport for %s is supported %s', ma, isSupportedTransport)
284
+ log.trace('transport for %a is supported %s', ma, isSupportedTransport)
285
285
  // skip any Multiaddrs that have transports we do not support
286
286
  return isSupportedTransport
287
287
  })
@@ -324,7 +324,7 @@ class DefaultAutoNATService implements Startable {
324
324
  })
325
325
 
326
326
  if (!connection.remoteAddr.equals(multiaddr)) {
327
- log.error('tried to dial %s but dialed %s', multiaddr, connection.remoteAddr)
327
+ log.error('tried to dial %a but dialed %a', multiaddr, connection.remoteAddr)
328
328
  throw new Error('Unexpected remote address')
329
329
  }
330
330
 
@@ -193,7 +193,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
193
193
  }
194
194
 
195
195
  async onHop ({ connection, stream }: IncomingStreamData): Promise<void> {
196
- log('received circuit v2 hop protocol stream from %s', connection.remotePeer)
196
+ log('received circuit v2 hop protocol stream from %p', connection.remotePeer)
197
197
 
198
198
  const hopTimeoutPromise = pDefer<HopMessage>()
199
199
  const timeout = setTimeout(() => {
@@ -239,7 +239,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
239
239
  case HopMessage.Type.RESERVE: await this.handleReserve({ stream, request, connection }); break
240
240
  case HopMessage.Type.CONNECT: await this.handleConnect({ stream, request, connection }); break
241
241
  default: {
242
- log.error('invalid hop request type %s via peer %s', request.type, connection.remotePeer)
242
+ log.error('invalid hop request type %s via peer %p', request.type, connection.remotePeer)
243
243
  stream.pb(HopMessage).write({ type: HopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE })
244
244
  }
245
245
  }
@@ -247,7 +247,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
247
247
 
248
248
  async handleReserve ({ stream, request, connection }: HopProtocolOptions): Promise<void> {
249
249
  const hopstr = stream.pb(HopMessage)
250
- log('hop reserve request from %s', connection.remotePeer)
250
+ log('hop reserve request from %p', connection.remotePeer)
251
251
 
252
252
  if (isRelayAddr(connection.remoteAddr)) {
253
253
  log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer)
@@ -329,7 +329,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
329
329
  return
330
330
  }
331
331
 
332
- log('hop connect request from %s', connection.remotePeer)
332
+ log('hop connect request from %p', connection.remotePeer)
333
333
 
334
334
  let dstPeer: PeerId
335
335
 
@@ -381,7 +381,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
381
381
  })
382
382
 
383
383
  if (destinationStream == null) {
384
- log.error('failed to open stream to destination peer %s', destinationConnection?.remotePeer)
384
+ log.error('failed to open stream to destination peer %p', destinationConnection?.remotePeer)
385
385
  hopstr.write({ type: HopMessage.Type.STATUS, status: Status.CONNECTION_FAILED })
386
386
  return
387
387
  }
@@ -414,17 +414,17 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
414
414
  try {
415
415
  response = await stopstr.read()
416
416
  } catch (err) {
417
- log.error('error parsing stop message response from %s', connection.remotePeer)
417
+ log.error('error parsing stop message response from %p', connection.remotePeer)
418
418
  }
419
419
 
420
420
  if (response == null) {
421
- log.error('could not read response from %s', connection.remotePeer)
421
+ log.error('could not read response from %p', connection.remotePeer)
422
422
  stream.close()
423
423
  return
424
424
  }
425
425
 
426
426
  if (response.status === Status.OK) {
427
- log('stop request to %s was successful', connection.remotePeer)
427
+ log('stop request to %p was successful', connection.remotePeer)
428
428
  return pbstr.unwrap()
429
429
  }
430
430
 
@@ -266,7 +266,7 @@ class CircuitRelayTransport implements Transport {
266
266
  localAddr: relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toString()}`)
267
267
  })
268
268
 
269
- log('new outbound connection %s', maConn.remoteAddr)
269
+ log('new outbound connection %a', maConn.remoteAddr)
270
270
  return await this.upgrader.upgradeOutbound(maConn)
271
271
  } catch (err) {
272
272
  log.error(`Circuit relay dial to destination ${destinationPeer.toString()} via relay ${connection.remotePeer.toString()} failed`, err)
@@ -312,17 +312,17 @@ class CircuitRelayTransport implements Transport {
312
312
  }
313
313
 
314
314
  const stopstr = pbstr.pb(StopMessage)
315
- log('new circuit relay v2 stop stream from %s', connection.remotePeer)
315
+ log('new circuit relay v2 stop stream from %p', connection.remotePeer)
316
316
 
317
317
  // Validate the STOP request has the required input
318
318
  if (request.type !== StopMessage.Type.CONNECT) {
319
- log.error('invalid stop connect request via peer %s', connection.remotePeer)
319
+ log.error('invalid stop connect request via peer %p', connection.remotePeer)
320
320
  stopstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE })
321
321
  return
322
322
  }
323
323
 
324
324
  if (!isValidStop(request)) {
325
- log.error('invalid stop connect request via peer %s', connection.remotePeer)
325
+ log.error('invalid stop connect request via peer %p', connection.remotePeer)
326
326
  stopstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE })
327
327
  return
328
328
  }
@@ -346,7 +346,7 @@ class CircuitRelayTransport implements Transport {
346
346
 
347
347
  log('new inbound connection %s', maConn.remoteAddr)
348
348
  await this.upgrader.upgradeInbound(maConn)
349
- log('%s connection %s upgraded', 'inbound', maConn.remoteAddr)
349
+ log('%s connection %a upgraded', 'inbound', maConn.remoteAddr)
350
350
  }
351
351
  }
352
352
 
@@ -37,7 +37,7 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
37
37
  }
38
38
 
39
39
  async listen (addr: Multiaddr): Promise<void> {
40
- log('listen on %s', addr)
40
+ log('listen on %a', addr)
41
41
 
42
42
  const relayPeerStr = addr.getPeerId()
43
43
  let relayConn: Connection | undefined
@@ -237,7 +237,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
237
237
  }
238
238
 
239
239
  async #createReservation (connection: Connection): Promise<Reservation> {
240
- log('requesting reservation from %s', connection.remotePeer)
240
+ log('requesting reservation from %p', connection.remotePeer)
241
241
  const stream = await connection.newStream(RELAY_V2_HOP_CODEC)
242
242
  const pbstr = pbStream(stream)
243
243
  const hopstr = pbstr.pb(HopMessage)
@@ -418,13 +418,13 @@ export class DialQueue {
418
418
  // let any signal abort the dial
419
419
  const signal = combineSignals(controller.signal, options.signal)
420
420
  signal.addEventListener('abort', () => {
421
- log('dial to %s aborted', addr)
421
+ log('dial to %a aborted', addr)
422
422
  })
423
423
  const deferred = pDefer<Connection>()
424
424
 
425
425
  await peerDialQueue.add(async () => {
426
426
  if (signal.aborted) {
427
- log('dial to %s was aborted before reaching the head of the peer dial queue', addr)
427
+ log('dial to %a was aborted before reaching the head of the peer dial queue', addr)
428
428
  deferred.reject(new AbortError())
429
429
  return
430
430
  }
@@ -433,7 +433,7 @@ export class DialQueue {
433
433
  await this.queue.add(async () => {
434
434
  try {
435
435
  if (signal.aborted) {
436
- log('dial to %s was aborted before reaching the head of the dial queue', addr)
436
+ log('dial to %a was aborted before reaching the head of the dial queue', addr)
437
437
  deferred.reject(new AbortError())
438
438
  return
439
439
  }
@@ -468,13 +468,13 @@ export class DialQueue {
468
468
  }
469
469
  })
470
470
 
471
- log('dial to %s succeeded', addr)
471
+ log('dial to %a succeeded', addr)
472
472
 
473
473
  // resolve the connection promise
474
474
  deferred.resolve(conn)
475
475
  } catch (err: any) {
476
476
  // something only went wrong if our signal was not aborted
477
- log.error('error during dial of %s', addr, err)
477
+ log.error('error during dial of %a', addr, err)
478
478
  deferred.reject(err)
479
479
  }
480
480
  }, {
@@ -547,7 +547,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
547
547
  })
548
548
 
549
549
  if (denyConnection) {
550
- log('connection from %s refused - connection remote address was in deny list', maConn.remoteAddr)
550
+ log('connection from %a refused - connection remote address was in deny list', maConn.remoteAddr)
551
551
  return false
552
552
  }
553
553
 
@@ -564,7 +564,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
564
564
 
565
565
  // check pending connections
566
566
  if (this.incomingPendingConnections === this.maxIncomingPendingConnections) {
567
- log('connection from %s refused - incomingPendingConnections exceeded by peer %s', maConn.remoteAddr)
567
+ log('connection from %a refused - incomingPendingConnections exceeded by host', maConn.remoteAddr)
568
568
  return false
569
569
  }
570
570
 
@@ -574,7 +574,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
574
574
  try {
575
575
  await this.inboundConnectionRateLimiter.consume(host, 1)
576
576
  } catch {
577
- log('connection from %s refused - inboundConnectionThreshold exceeded by host %s', host, maConn.remoteAddr)
577
+ log('connection from %a refused - inboundConnectionThreshold exceeded by host %s', maConn.remoteAddr, host)
578
578
  return false
579
579
  }
580
580
  }
@@ -585,7 +585,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
585
585
  return true
586
586
  }
587
587
 
588
- log('connection from %s refused - maxConnections exceeded', maConn.remoteAddr)
588
+ log('connection from %a refused - maxConnections exceeded', maConn.remoteAddr)
589
589
  return false
590
590
  }
591
591
 
@@ -23,7 +23,7 @@ import {
23
23
  MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
24
24
  } from './consts.js'
25
25
  import { Identify } from './pb/message.js'
26
- import type { IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
26
+ import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
27
27
  import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } from '@libp2p/interface'
28
28
  import type { Connection, Stream } from '@libp2p/interface/connection'
29
29
  import type { EventEmitter } from '@libp2p/interface/events'
@@ -49,10 +49,11 @@ const defaultValues = {
49
49
  maxPushIncomingStreams: 1,
50
50
  maxPushOutgoingStreams: 1,
51
51
  maxObservedAddresses: 10,
52
- maxIdentifyMessageSize: 8192
52
+ maxIdentifyMessageSize: 8192,
53
+ runOnConnectionOpen: true
53
54
  }
54
55
 
55
- export class DefaultIdentifyService implements Startable {
56
+ export class DefaultIdentifyService implements Startable, IdentifyService {
56
57
  private readonly identifyProtocolStr: string
57
58
  private readonly identifyPushProtocolStr: string
58
59
  public readonly host: {
@@ -100,11 +101,13 @@ export class DefaultIdentifyService implements Startable {
100
101
  agentVersion: init.agentVersion ?? defaultValues.agentVersion
101
102
  }
102
103
 
103
- // When a new connection happens, trigger identify
104
- components.events.addEventListener('connection:open', (evt) => {
105
- const connection = evt.detail
106
- this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
107
- })
104
+ if (init.runOnConnectionOpen ?? defaultValues.runOnConnectionOpen) {
105
+ // When a new connection happens, trigger identify
106
+ components.events.addEventListener('connection:open', (evt) => {
107
+ const connection = evt.detail
108
+ this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
109
+ })
110
+ }
108
111
 
109
112
  // When self peer record changes, trigger identify-push
110
113
  components.events.addEventListener('self:peer:update', (evt) => {
@@ -296,7 +299,7 @@ export class DefaultIdentifyService implements Startable {
296
299
  }
297
300
  }
298
301
 
299
- async identify (connection: Connection, options: AbortOptions = {}): Promise<void> {
302
+ async identify (connection: Connection, options: AbortOptions = {}): Promise<IdentifyResult> {
300
303
  const message = await this._identify(connection, options)
301
304
  const {
302
305
  publicKey,
@@ -322,11 +325,11 @@ export class DefaultIdentifyService implements Startable {
322
325
  const cleanObservedAddr = getCleanMultiaddr(observedAddr)
323
326
 
324
327
  log('identify completed for peer %p and protocols %o', id, protocols)
325
- log('our observed address is %s', cleanObservedAddr)
328
+ log('our observed address is %a', cleanObservedAddr)
326
329
 
327
330
  if (cleanObservedAddr != null &&
328
331
  this.addressManager.getObservedAddrs().length < (this.maxObservedAddresses ?? Infinity)) {
329
- log('storing our observed address %s', cleanObservedAddr?.toString())
332
+ log('storing our observed address %a', cleanObservedAddr)
330
333
  this.addressManager.addObservedAddr(cleanObservedAddr)
331
334
  }
332
335
 
@@ -344,6 +347,8 @@ export class DefaultIdentifyService implements Startable {
344
347
  }
345
348
 
346
349
  this.events.safeDispatchEvent('peer:identify', { detail: result })
350
+
351
+ return result
347
352
  }
348
353
 
349
354
  /**
@@ -4,10 +4,11 @@ import {
4
4
  } from './consts.js'
5
5
  import { DefaultIdentifyService } from './identify.js'
6
6
  import { Identify } from './pb/message.js'
7
- import type { Libp2pEvents } from '@libp2p/interface'
7
+ import type { AbortOptions, IdentifyResult, Libp2pEvents } from '@libp2p/interface'
8
8
  import type { EventEmitter } from '@libp2p/interface/events'
9
9
  import type { PeerId } from '@libp2p/interface/peer-id'
10
10
  import type { PeerStore } from '@libp2p/interface/peer-store'
11
+ import type { Connection } from '@libp2p/interface/src/connection/index.js'
11
12
  import type { AddressManager } from '@libp2p/interface-internal/address-manager'
12
13
  import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
13
14
  import type { Registrar } from '@libp2p/interface-internal/registrar'
@@ -39,6 +40,11 @@ export interface IdentifyServiceInit {
39
40
  maxPushIncomingStreams?: number
40
41
  maxPushOutgoingStreams?: number
41
42
  maxObservedAddresses?: number
43
+
44
+ /**
45
+ * Whether to automatically dial identify on newly opened connections (default: true)
46
+ */
47
+ runOnConnectionOpen?: boolean
42
48
  }
43
49
 
44
50
  export interface IdentifyServiceComponents {
@@ -60,6 +66,19 @@ export const multicodecs = {
60
66
 
61
67
  export const Message = { Identify }
62
68
 
63
- export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService {
69
+ export interface IdentifyService {
70
+ /**
71
+ * due to the default limits on inbound/outbound streams for this protocol,
72
+ * invoking this method when runOnConnectionOpen is true can lead to unpredictable results
73
+ * as streams may be closed by the local or the remote node.
74
+ * Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol,
75
+ * you may be better off configuring a topology to be notified instead.
76
+ */
77
+ identify: (connection: Connection, options?: AbortOptions) => Promise<IdentifyResult>
78
+
79
+ push: () => Promise<void>
80
+ }
81
+
82
+ export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService {
64
83
  return (components) => new DefaultIdentifyService(components, init)
65
84
  }
@@ -190,7 +190,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
190
190
 
191
191
  // For each supported multiaddr, create a listener
192
192
  for (const addr of supportedAddrs) {
193
- log('creating listener for %s on %s', key, addr)
193
+ log('creating listener for %s on %a', key, addr)
194
194
  const listener = transport.createListener({
195
195
  upgrader: this.components.upgrader
196
196
  })
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.45.9-05abd49f'
1
+ export const version = '0.45.9-06f4901a'
2
2
  export const name = 'libp2p'