libp2p 0.44.0 → 0.45.0-08d4c261

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.
Files changed (238) hide show
  1. package/dist/index.min.js +32 -32
  2. package/dist/src/address-manager/index.d.ts +9 -5
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +44 -7
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/address-manager/utils.d.ts +2 -0
  7. package/dist/src/address-manager/utils.d.ts.map +1 -0
  8. package/dist/src/address-manager/utils.js +12 -0
  9. package/dist/src/address-manager/utils.js.map +1 -0
  10. package/dist/src/autonat/constants.d.ts +16 -2
  11. package/dist/src/autonat/constants.d.ts.map +1 -1
  12. package/dist/src/autonat/constants.js +16 -2
  13. package/dist/src/autonat/constants.js.map +1 -1
  14. package/dist/src/autonat/index.d.ts +12 -33
  15. package/dist/src/autonat/index.d.ts.map +1 -1
  16. package/dist/src/autonat/index.js +48 -33
  17. package/dist/src/autonat/index.js.map +1 -1
  18. package/dist/src/circuit-relay/index.d.ts +1 -1
  19. package/dist/src/circuit-relay/index.d.ts.map +1 -1
  20. package/dist/src/circuit-relay/server/advert-service.d.ts +2 -2
  21. package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
  22. package/dist/src/circuit-relay/server/advert-service.js +8 -4
  23. package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
  24. package/dist/src/circuit-relay/server/index.d.ts +8 -8
  25. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  26. package/dist/src/circuit-relay/server/index.js +32 -12
  27. package/dist/src/circuit-relay/server/index.js.map +1 -1
  28. package/dist/src/circuit-relay/server/reservation-store.d.ts +5 -5
  29. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +1 -1
  30. package/dist/src/circuit-relay/server/reservation-store.js +10 -3
  31. package/dist/src/circuit-relay/server/reservation-store.js.map +1 -1
  32. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +1 -1
  33. package/dist/src/circuit-relay/server/reservation-voucher.js +5 -2
  34. package/dist/src/circuit-relay/server/reservation-voucher.js.map +1 -1
  35. package/dist/src/circuit-relay/transport/discovery.d.ts +4 -4
  36. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  37. package/dist/src/circuit-relay/transport/discovery.js +13 -5
  38. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  39. package/dist/src/circuit-relay/transport/index.d.ts +10 -8
  40. package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
  41. package/dist/src/circuit-relay/transport/index.js +26 -18
  42. package/dist/src/circuit-relay/transport/index.js.map +1 -1
  43. package/dist/src/circuit-relay/transport/listener.d.ts +1 -1
  44. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  45. package/dist/src/circuit-relay/transport/listener.js +43 -43
  46. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  47. package/dist/src/circuit-relay/transport/reservation-store.d.ts +8 -7
  48. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  49. package/dist/src/circuit-relay/transport/reservation-store.js +65 -52
  50. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  51. package/dist/src/circuit-relay/utils.d.ts.map +1 -1
  52. package/dist/src/circuit-relay/utils.js +3 -3
  53. package/dist/src/circuit-relay/utils.js.map +1 -1
  54. package/dist/src/components.d.ts +10 -67
  55. package/dist/src/components.d.ts.map +1 -1
  56. package/dist/src/components.js +61 -165
  57. package/dist/src/components.js.map +1 -1
  58. package/dist/src/config/connection-gater.browser.d.ts +8 -0
  59. package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
  60. package/dist/src/config/connection-gater.browser.js +27 -0
  61. package/dist/src/config/connection-gater.browser.js.map +1 -0
  62. package/dist/src/config/connection-gater.d.ts +6 -0
  63. package/dist/src/config/connection-gater.d.ts.map +1 -0
  64. package/dist/src/config/connection-gater.js +18 -0
  65. package/dist/src/config/connection-gater.js.map +1 -0
  66. package/dist/src/config.d.ts +2 -1
  67. package/dist/src/config.d.ts.map +1 -1
  68. package/dist/src/config.js +4 -66
  69. package/dist/src/config.js.map +1 -1
  70. package/dist/src/connection/index.d.ts +3 -3
  71. package/dist/src/connection/index.d.ts.map +1 -1
  72. package/dist/src/connection/index.js +38 -8
  73. package/dist/src/connection/index.js.map +1 -1
  74. package/dist/src/connection-manager/auto-dial.d.ts +4 -1
  75. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  76. package/dist/src/connection-manager/auto-dial.js +44 -13
  77. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  78. package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
  79. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  80. package/dist/src/connection-manager/connection-pruner.js +28 -8
  81. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  82. package/dist/src/connection-manager/dial-queue.d.ts +7 -6
  83. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  84. package/dist/src/connection-manager/dial-queue.js +59 -23
  85. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  86. package/dist/src/connection-manager/index.d.ts +16 -15
  87. package/dist/src/connection-manager/index.d.ts.map +1 -1
  88. package/dist/src/connection-manager/index.js +44 -41
  89. package/dist/src/connection-manager/index.js.map +1 -1
  90. package/dist/src/connection-manager/utils.d.ts +2 -3
  91. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  92. package/dist/src/connection-manager/utils.js +3 -3
  93. package/dist/src/connection-manager/utils.js.map +1 -1
  94. package/dist/src/content-routing/index.d.ts +2 -12
  95. package/dist/src/content-routing/index.d.ts.map +1 -1
  96. package/dist/src/content-routing/index.js +12 -44
  97. package/dist/src/content-routing/index.js.map +1 -1
  98. package/dist/src/content-routing/utils.d.ts +1 -1
  99. package/dist/src/content-routing/utils.d.ts.map +1 -1
  100. package/dist/src/content-routing/utils.js +3 -1
  101. package/dist/src/content-routing/utils.js.map +1 -1
  102. package/dist/src/fetch/index.d.ts +15 -37
  103. package/dist/src/fetch/index.d.ts.map +1 -1
  104. package/dist/src/fetch/index.js +19 -16
  105. package/dist/src/fetch/index.js.map +1 -1
  106. package/dist/src/get-peer.d.ts +1 -1
  107. package/dist/src/get-peer.d.ts.map +1 -1
  108. package/dist/src/get-peer.js +3 -3
  109. package/dist/src/get-peer.js.map +1 -1
  110. package/dist/src/identify/identify.d.ts +53 -0
  111. package/dist/src/identify/identify.d.ts.map +1 -0
  112. package/dist/src/identify/identify.js +417 -0
  113. package/dist/src/identify/identify.js.map +1 -0
  114. package/dist/src/identify/index.d.ts +14 -59
  115. package/dist/src/identify/index.d.ts.map +1 -1
  116. package/dist/src/identify/index.js +5 -388
  117. package/dist/src/identify/index.js.map +1 -1
  118. package/dist/src/index.d.ts +28 -79
  119. package/dist/src/index.d.ts.map +1 -1
  120. package/dist/src/index.js +2 -1
  121. package/dist/src/index.js.map +1 -1
  122. package/dist/src/insecure/index.js +6 -8
  123. package/dist/src/insecure/index.js.map +1 -1
  124. package/dist/src/libp2p.d.ts +16 -47
  125. package/dist/src/libp2p.d.ts.map +1 -1
  126. package/dist/src/libp2p.js +141 -201
  127. package/dist/src/libp2p.js.map +1 -1
  128. package/dist/src/peer-routing.d.ts +3 -40
  129. package/dist/src/peer-routing.d.ts.map +1 -1
  130. package/dist/src/peer-routing.js +8 -65
  131. package/dist/src/peer-routing.js.map +1 -1
  132. package/dist/src/ping/constants.d.ts +4 -0
  133. package/dist/src/ping/constants.d.ts.map +1 -1
  134. package/dist/src/ping/constants.js +10 -0
  135. package/dist/src/ping/constants.js.map +1 -1
  136. package/dist/src/ping/index.d.ts +11 -28
  137. package/dist/src/ping/index.d.ts.map +1 -1
  138. package/dist/src/ping/index.js +31 -28
  139. package/dist/src/ping/index.js.map +1 -1
  140. package/dist/src/pnet/crypto.js +3 -3
  141. package/dist/src/pnet/crypto.js.map +1 -1
  142. package/dist/src/pnet/index.d.ts.map +1 -1
  143. package/dist/src/pnet/index.js +9 -6
  144. package/dist/src/pnet/index.js.map +1 -1
  145. package/dist/src/pnet/key-generator.js +1 -1
  146. package/dist/src/pnet/key-generator.js.map +1 -1
  147. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  148. package/dist/src/pubsub/dummy-pubsub.js +2 -5
  149. package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
  150. package/dist/src/registrar.d.ts +6 -4
  151. package/dist/src/registrar.d.ts.map +1 -1
  152. package/dist/src/registrar.js +33 -28
  153. package/dist/src/registrar.js.map +1 -1
  154. package/dist/src/transport-manager.d.ts +14 -7
  155. package/dist/src/transport-manager.d.ts.map +1 -1
  156. package/dist/src/transport-manager.js +27 -13
  157. package/dist/src/transport-manager.js.map +1 -1
  158. package/dist/src/upgrader.d.ts +20 -15
  159. package/dist/src/upgrader.d.ts.map +1 -1
  160. package/dist/src/upgrader.js +49 -53
  161. package/dist/src/upgrader.js.map +1 -1
  162. package/dist/src/{nat-manager.d.ts → upnp-nat/index.d.ts} +12 -15
  163. package/dist/src/upnp-nat/index.d.ts.map +1 -0
  164. package/dist/src/{nat-manager.js → upnp-nat/index.js} +23 -10
  165. package/dist/src/upnp-nat/index.js.map +1 -0
  166. package/dist/src/version.d.ts +1 -1
  167. package/dist/src/version.d.ts.map +1 -1
  168. package/dist/src/version.js +1 -1
  169. package/dist/src/version.js.map +1 -1
  170. package/package.json +62 -47
  171. package/src/address-manager/index.ts +52 -10
  172. package/src/address-manager/utils.ts +14 -0
  173. package/src/autonat/constants.ts +17 -2
  174. package/src/autonat/index.ts +65 -57
  175. package/src/circuit-relay/index.ts +1 -1
  176. package/src/circuit-relay/server/advert-service.ts +5 -5
  177. package/src/circuit-relay/server/index.ts +28 -20
  178. package/src/circuit-relay/server/reservation-store.ts +5 -5
  179. package/src/circuit-relay/server/reservation-voucher.ts +1 -1
  180. package/src/circuit-relay/transport/discovery.ts +12 -10
  181. package/src/circuit-relay/transport/index.ts +26 -26
  182. package/src/circuit-relay/transport/listener.ts +43 -33
  183. package/src/circuit-relay/transport/reservation-store.ts +42 -28
  184. package/src/circuit-relay/utils.ts +6 -6
  185. package/src/components.ts +75 -238
  186. package/src/config/connection-gater.browser.ts +31 -0
  187. package/src/config/connection-gater.ts +19 -0
  188. package/src/config.ts +9 -70
  189. package/src/connection/index.ts +6 -10
  190. package/src/connection-manager/auto-dial.ts +46 -17
  191. package/src/connection-manager/connection-pruner.ts +30 -10
  192. package/src/connection-manager/dial-queue.ts +56 -35
  193. package/src/connection-manager/index.ts +45 -57
  194. package/src/connection-manager/utils.ts +4 -5
  195. package/src/content-routing/index.ts +11 -59
  196. package/src/content-routing/utils.ts +4 -2
  197. package/src/fetch/index.ts +63 -26
  198. package/src/get-peer.ts +4 -4
  199. package/src/identify/identify.ts +519 -0
  200. package/src/identify/index.ts +19 -509
  201. package/src/index.ts +25 -88
  202. package/src/insecure/index.ts +10 -10
  203. package/src/libp2p.ts +158 -279
  204. package/src/peer-routing.ts +10 -110
  205. package/src/ping/constants.ts +11 -0
  206. package/src/ping/index.ts +43 -39
  207. package/src/pnet/crypto.ts +3 -3
  208. package/src/pnet/index.ts +6 -6
  209. package/src/pnet/key-generator.ts +1 -1
  210. package/src/pubsub/dummy-pubsub.ts +2 -2
  211. package/src/registrar.ts +34 -31
  212. package/src/transport-manager.ts +33 -19
  213. package/src/upgrader.ts +75 -71
  214. package/src/{nat-manager.ts → upnp-nat/index.ts} +23 -24
  215. package/src/version.ts +1 -1
  216. package/dist/src/dht/dht-content-routing.d.ts +0 -17
  217. package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
  218. package/dist/src/dht/dht-content-routing.js +0 -32
  219. package/dist/src/dht/dht-content-routing.js.map +0 -1
  220. package/dist/src/dht/dht-peer-routing.d.ts +0 -15
  221. package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
  222. package/dist/src/dht/dht-peer-routing.js +0 -26
  223. package/dist/src/dht/dht-peer-routing.js.map +0 -1
  224. package/dist/src/dht/dummy-dht.d.ts +0 -20
  225. package/dist/src/dht/dummy-dht.d.ts.map +0 -1
  226. package/dist/src/dht/dummy-dht.js +0 -46
  227. package/dist/src/dht/dummy-dht.js.map +0 -1
  228. package/dist/src/nat-manager.d.ts.map +0 -1
  229. package/dist/src/nat-manager.js.map +0 -1
  230. package/dist/src/peer-record-updater.d.ts +0 -24
  231. package/dist/src/peer-record-updater.d.ts.map +0 -1
  232. package/dist/src/peer-record-updater.js +0 -44
  233. package/dist/src/peer-record-updater.js.map +0 -1
  234. package/dist/typedoc-urls.json +0 -41
  235. package/src/dht/dht-content-routing.ts +0 -44
  236. package/src/dht/dht-peer-routing.ts +0 -36
  237. package/src/dht/dummy-dht.ts +0 -60
  238. package/src/peer-record-updater.ts +0 -65
@@ -1,31 +1,31 @@
1
+ import { setMaxListeners } from 'events'
2
+ import { EventEmitter } from '@libp2p/interfaces/events'
1
3
  import { logger } from '@libp2p/logger'
2
- import { createLimitedRelay } from '../utils.js'
4
+ import { peerIdFromBytes } from '@libp2p/peer-id'
5
+ import { RecordEnvelope } from '@libp2p/peer-record'
6
+ import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
7
+ import { pbStream, type ProtobufStream } from 'it-pb-stream'
8
+ import pDefer from 'p-defer'
3
9
  import {
4
10
  CIRCUIT_PROTO_CODE,
5
11
  DEFAULT_HOP_TIMEOUT,
6
12
  RELAY_SOURCE_TAG
7
13
  , RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC
8
14
  } from '../constants.js'
9
- import type { PeerStore } from '@libp2p/interface-peer-store'
10
- import type { Startable } from '@libp2p/interfaces/startable'
11
- import { ReservationStore, ReservationStoreInit } from './reservation-store.js'
12
- import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
13
- import { AdvertService, AdvertServiceComponents, AdvertServiceInit } from './advert-service.js'
14
- import pDefer from 'p-defer'
15
- import { pbStream, ProtobufStream } from 'it-pb-stream'
16
- import { HopMessage, Reservation, Status, StopMessage } from '../pb/index.js'
17
- import { Multiaddr, multiaddr } from '@multiformats/multiaddr'
18
- import type { Connection, Stream } from '@libp2p/interface-connection'
19
- import type { ConnectionGater } from '@libp2p/interface-connection-gater'
20
- import { peerIdFromBytes } from '@libp2p/peer-id'
21
- import type { PeerId } from '@libp2p/interface-peer-id'
22
- import { RecordEnvelope } from '@libp2p/peer-record'
15
+ import { HopMessage, type Reservation, Status, StopMessage } from '../pb/index.js'
16
+ import { createLimitedRelay } from '../utils.js'
17
+ import { AdvertService, type AdvertServiceComponents, type AdvertServiceInit } from './advert-service.js'
18
+ import { ReservationStore, type ReservationStoreInit } from './reservation-store.js'
23
19
  import { ReservationVoucherRecord } from './reservation-voucher.js'
20
+ import type { CircuitRelayService, RelayReservation } from '../index.js'
24
21
  import type { AddressManager } from '@libp2p/interface-address-manager'
22
+ import type { Connection, Stream } from '@libp2p/interface-connection'
23
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
25
24
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
26
- import type { CircuitRelayService, RelayReservation } from '../index.js'
27
- import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
28
- import { setMaxListeners } from 'events'
25
+ import type { PeerId } from '@libp2p/interface-peer-id'
26
+ import type { PeerStore } from '@libp2p/interface-peer-store'
27
+ import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
28
+ import type { Startable } from '@libp2p/interfaces/startable'
29
29
  import type { PeerMap } from '@libp2p/peer-collections'
30
30
 
31
31
  const log = logger('libp2p:circuit-relay:server')
@@ -128,7 +128,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
128
128
  if (init.advertise != null && init.advertise !== false) {
129
129
  this.advertService = new AdvertService(components, init.advertise === true ? undefined : init.advertise)
130
130
  this.advertService.addEventListener('advert:success', () => {
131
- this.dispatchEvent(new CustomEvent('relay:advert:success'))
131
+ this.safeDispatchEvent('relay:advert:success', {})
132
132
  })
133
133
  this.advertService.addEventListener('advert:error', (evt) => {
134
134
  this.safeDispatchEvent('relay:advert:error', { detail: evt.detail })
@@ -260,7 +260,11 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
260
260
  // result.expire is non-null if `ReservationStore.reserve` returns with status == OK
261
261
  if (result.expire != null) {
262
262
  const ttl = (result.expire * 1000) - Date.now()
263
- await this.peerStore.tagPeer(connection.remotePeer, RELAY_SOURCE_TAG, { value: 1, ttl })
263
+ await this.peerStore.merge(connection.remotePeer, {
264
+ tags: {
265
+ [RELAY_SOURCE_TAG]: { value: 1, ttl }
266
+ }
267
+ })
264
268
  }
265
269
 
266
270
  hopstr.write({
@@ -283,6 +287,10 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
283
287
  const addrs = []
284
288
 
285
289
  for (const relayAddr of this.addressManager.getAddresses()) {
290
+ if (relayAddr.toString().includes('/p2p-circuit')) {
291
+ continue
292
+ }
293
+
286
294
  addrs.push(relayAddr.bytes)
287
295
  }
288
296
 
@@ -1,11 +1,11 @@
1
- import { Limit, Status } from '../pb/index.js'
2
- import type { Multiaddr } from '@multiformats/multiaddr'
3
- import type { PeerId } from '@libp2p/interface-peer-id'
4
- import type { Startable } from '@libp2p/interfaces/startable'
5
1
  import { PeerMap } from '@libp2p/peer-collections'
6
- import type { RecursivePartial } from '@libp2p/interfaces'
7
2
  import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js'
3
+ import { type Limit, Status } from '../pb/index.js'
8
4
  import type { RelayReservation } from '../index.js'
5
+ import type { PeerId } from '@libp2p/interface-peer-id'
6
+ import type { RecursivePartial } from '@libp2p/interfaces'
7
+ import type { Startable } from '@libp2p/interfaces/startable'
8
+ import type { Multiaddr } from '@multiformats/multiaddr'
9
9
 
10
10
  export type ReservationStatus = Status.OK | Status.PERMISSION_DENIED | Status.RESERVATION_REFUSED
11
11
 
@@ -1,6 +1,6 @@
1
+ import { ReservationVoucher } from '../pb/index.js'
1
2
  import type { PeerId } from '@libp2p/interface-peer-id'
2
3
  import type { Record } from '@libp2p/interface-record'
3
- import { ReservationVoucher } from '../pb/index.js'
4
4
 
5
5
  export interface ReservationVoucherOptions {
6
6
  relay: PeerId
@@ -1,18 +1,18 @@
1
+ import { EventEmitter } from '@libp2p/interfaces/events'
1
2
  import { logger } from '@libp2p/logger'
2
- import { namespaceToCid } from '../utils.js'
3
+ import { createTopology } from '@libp2p/topology'
3
4
  import {
4
5
  RELAY_RENDEZVOUS_NS,
5
6
  RELAY_V2_HOP_CODEC
6
7
  } from '../constants.js'
7
- import type { PeerId } from '@libp2p/interface-peer-id'
8
- import type { PeerStore } from '@libp2p/interface-peer-store'
9
- import { EventEmitter } from '@libp2p/interfaces/events'
10
- import type { Startable } from '@libp2p/interfaces/startable'
8
+ import { namespaceToCid } from '../utils.js'
11
9
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
12
- import type { TransportManager } from '@libp2p/interface-transport'
13
10
  import type { ContentRouting } from '@libp2p/interface-content-routing'
11
+ import type { PeerId } from '@libp2p/interface-peer-id'
12
+ import type { PeerStore } from '@libp2p/interface-peer-store'
14
13
  import type { Registrar } from '@libp2p/interface-registrar'
15
- import { createTopology } from '@libp2p/topology'
14
+ import type { TransportManager } from '@libp2p/interface-transport'
15
+ import type { Startable } from '@libp2p/interfaces/startable'
16
16
 
17
17
  const log = logger('libp2p:circuit-relay:discover-relays')
18
18
 
@@ -91,7 +91,7 @@ export class RelayDiscovery extends EventEmitter<RelayDiscoveryEvents> implement
91
91
  log('searching peer store for relays')
92
92
  const peers = (await this.peerStore.all())
93
93
  // filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
94
- .filter(({ id, protocols }) => protocols.includes(RELAY_V2_HOP_CODEC))
94
+ .filter(({ protocols }) => protocols.includes(RELAY_V2_HOP_CODEC))
95
95
  .sort(() => Math.random() - 0.5)
96
96
 
97
97
  for (const peer of peers) {
@@ -112,9 +112,11 @@ export class RelayDiscovery extends EventEmitter<RelayDiscoveryEvents> implement
112
112
  const peerId = provider.id
113
113
 
114
114
  found++
115
- log('found relay peer %p in content routing', peerId)
116
- await this.peerStore.addressBook.add(peerId, provider.multiaddrs)
115
+ await this.peerStore.merge(peerId, {
116
+ multiaddrs: provider.multiaddrs
117
+ })
117
118
 
119
+ log('found relay peer %p in content routing', peerId)
118
120
  this.safeDispatchEvent('relay:discover', { detail: peerId })
119
121
  }
120
122
  }
@@ -1,28 +1,29 @@
1
- import { StopMessage, HopMessage, Status } from '../pb/index.js'
1
+ import { symbol, type Upgrader, type Listener, type Transport, type CreateListenerOptions } from '@libp2p/interface-transport'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
2
3
  import { logger } from '@libp2p/logger'
4
+ import { peerIdFromBytes, peerIdFromString } from '@libp2p/peer-id'
5
+ import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
3
6
  import * as mafmt from '@multiformats/mafmt'
4
7
  import { multiaddr } from '@multiformats/multiaddr'
8
+ import { pbStream } from 'it-pb-stream'
5
9
  import { codes } from '../../errors.js'
6
- import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
10
+ import { CIRCUIT_PROTO_CODE, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
11
+ import { StopMessage, HopMessage, Status } from '../pb/index.js'
12
+ import { RelayDiscovery, type RelayDiscoveryComponents } from './discovery.js'
7
13
  import { createListener } from './listener.js'
8
- import { symbol, Upgrader } from '@libp2p/interface-transport'
9
- import { peerIdFromBytes, peerIdFromString } from '@libp2p/peer-id'
10
- import type { AbortOptions } from '@libp2p/interfaces'
11
- import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
12
- import type { Listener, Transport, CreateListenerOptions } from '@libp2p/interface-transport'
14
+ import { type RelayStoreInit, ReservationStore } from './reservation-store.js'
15
+ import type { AddressManager } from '@libp2p/interface-address-manager'
13
16
  import type { Connection, Stream } from '@libp2p/interface-connection'
14
17
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
15
- import type { PeerId } from '@libp2p/interface-peer-id'
16
- import type { Multiaddr } from '@multiformats/multiaddr'
17
- import type { PeerStore } from '@libp2p/interface-peer-store'
18
18
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
19
- import type { AddressManager } from '@libp2p/interface-address-manager'
20
- import { pbStream } from 'it-pb-stream'
21
19
  import type { ContentRouting } from '@libp2p/interface-content-routing'
22
- import { CIRCUIT_PROTO_CODE, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
23
- import { RelayStoreInit, ReservationStore } from './reservation-store.js'
24
- import { RelayDiscovery, RelayDiscoveryComponents } from './discovery.js'
25
- import { CodeError } from '@libp2p/interfaces/errors'
20
+ import type { Libp2pEvents } from '@libp2p/interface-libp2p'
21
+ import type { PeerId } from '@libp2p/interface-peer-id'
22
+ import type { PeerStore } from '@libp2p/interface-peer-store'
23
+ import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
24
+ import type { AbortOptions } from '@libp2p/interfaces'
25
+ import type { EventEmitter } from '@libp2p/interfaces/events'
26
+ import type { Multiaddr } from '@multiformats/multiaddr'
26
27
 
27
28
  const log = logger('libp2p:circuit-relay:transport')
28
29
 
@@ -49,6 +50,7 @@ export interface CircuitRelayTransportComponents extends RelayDiscoveryComponent
49
50
  addressManager: AddressManager
50
51
  contentRouting: ContentRouting
51
52
  connectionGater: ConnectionGater
53
+ events: EventEmitter<Libp2pEvents>
52
54
  }
53
55
 
54
56
  interface ConnectOptions {
@@ -139,13 +141,9 @@ class CircuitRelayTransport implements Transport {
139
141
  this.started = false
140
142
  }
141
143
 
142
- get [symbol] (): true {
143
- return true
144
- }
144
+ readonly [symbol] = true
145
145
 
146
- get [Symbol.toStringTag] (): 'libp2p/circuit-relay-v2' {
147
- return 'libp2p/circuit-relay-v2'
148
- }
146
+ readonly [Symbol.toStringTag] = 'libp2p/circuit-relay-v2'
149
147
 
150
148
  /**
151
149
  * Dial a peer over a relay
@@ -165,7 +163,7 @@ class CircuitRelayTransport implements Transport {
165
163
  const destinationId = destinationAddr.getPeerId()
166
164
 
167
165
  if (relayId == null || destinationId == null) {
168
- const errMsg = 'Circuit relay dial failed as addresses did not have peer id'
166
+ const errMsg = `Circuit relay dial to ${ma.toString()} failed as address did not have peer ids`
169
167
  log.error(errMsg)
170
168
  throw new CodeError(errMsg, codes.ERR_RELAYED_DIAL)
171
169
  }
@@ -178,7 +176,9 @@ class CircuitRelayTransport implements Transport {
178
176
  let relayConnection = relayConnections[0]
179
177
 
180
178
  if (relayConnection == null) {
181
- await this.peerStore.addressBook.add(relayPeer, [relayAddr])
179
+ await this.peerStore.merge(relayPeer, {
180
+ multiaddrs: [relayAddr]
181
+ })
182
182
  relayConnection = await this.connectionManager.openConnection(relayPeer, options)
183
183
  disconnectOnFailure = true
184
184
  }
@@ -196,7 +196,7 @@ class CircuitRelayTransport implements Transport {
196
196
  disconnectOnFailure
197
197
  })
198
198
  } catch (err: any) {
199
- log.error('Circuit relay dial failed', err)
199
+ log.error(`Circuit relay dial to destination ${destinationPeer.toString()} via relay ${relayPeer.toString()} failed`, err)
200
200
  disconnectOnFailure && await relayConnection.close()
201
201
  throw err
202
202
  }
@@ -237,7 +237,7 @@ class CircuitRelayTransport implements Transport {
237
237
  log('new outbound connection %s', maConn.remoteAddr)
238
238
  return await this.upgrader.upgradeOutbound(maConn)
239
239
  } catch (err) {
240
- log.error('Circuit relay dial failed', err)
240
+ log.error(`Circuit relay dial to destination ${destinationPeer.toString()} via relay ${connection.remotePeer.toString()} failed`, err)
241
241
  disconnectOnFailure && await connection.close()
242
242
  throw err
243
243
  }
@@ -1,12 +1,15 @@
1
- import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
2
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
3
- import type { Listener, ListenerEvents } from '@libp2p/interface-transport'
4
- import type { Multiaddr } from '@multiformats/multiaddr'
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { EventEmitter } from '@libp2p/interfaces/events'
3
+ import { logger } from '@libp2p/logger'
4
+ import { PeerMap } from '@libp2p/peer-collections'
5
+ import { peerIdFromString } from '@libp2p/peer-id'
5
6
  import { multiaddr } from '@multiformats/multiaddr'
6
7
  import type { ReservationStore } from './reservation-store.js'
8
+ import type { Connection } from '@libp2p/interface-connection'
9
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
7
10
  import type { PeerId } from '@libp2p/interface-peer-id'
8
- import { PeerMap } from '@libp2p/peer-collections'
9
- import { logger } from '@libp2p/logger'
11
+ import type { Listener, ListenerEvents } from '@libp2p/interface-transport'
12
+ import type { Multiaddr } from '@multiformats/multiaddr'
10
13
 
11
14
  const log = logger('libp2p:circuit-relay:transport:listener')
12
15
 
@@ -18,7 +21,7 @@ export interface CircuitRelayTransportListenerComponents {
18
21
  class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> implements Listener {
19
22
  private readonly connectionManager: ConnectionManager
20
23
  private readonly relayStore: ReservationStore
21
- private readonly listeningAddrs: PeerMap<Multiaddr>
24
+ private readonly listeningAddrs: PeerMap<Multiaddr[]>
22
25
 
23
26
  constructor (components: CircuitRelayTransportListenerComponents) {
24
27
  super()
@@ -31,19 +34,30 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
31
34
  this.relayStore.addEventListener('relay:removed', (evt) => {
32
35
  this.#removeRelayPeer(evt.detail)
33
36
  })
34
-
35
- // remove listening addrs when a peer disconnects
36
- this.connectionManager.addEventListener('peer:disconnect', (evt) => {
37
- this.#removeRelayPeer(evt.detail.remotePeer)
38
- })
39
37
  }
40
38
 
41
39
  async listen (addr: Multiaddr): Promise<void> {
42
40
  log('listen on %s', addr)
43
41
 
44
- const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
45
- const ma = multiaddr(addrString)
46
- const relayConn = await this.connectionManager.openConnection(ma)
42
+ const relayPeerStr = addr.getPeerId()
43
+ let relayConn: Connection | undefined
44
+
45
+ // check if we already have a connection to the relay
46
+ if (relayPeerStr != null) {
47
+ const relayPeer = peerIdFromString(relayPeerStr)
48
+ const connections = this.connectionManager.getConnectionsMap().get(relayPeer) ?? []
49
+
50
+ if (connections.length > 0) {
51
+ relayConn = connections[0]
52
+ }
53
+ }
54
+
55
+ // open a new connection as we don't already have one
56
+ if (relayConn == null) {
57
+ const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
58
+ const ma = multiaddr(addrString)
59
+ relayConn = await this.connectionManager.openConnection(ma)
60
+ }
47
61
 
48
62
  if (!this.relayStore.hasReservation(relayConn.remotePeer)) {
49
63
  // addRelay calls transportManager.listen which calls this listen method
@@ -51,31 +65,27 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
51
65
  return
52
66
  }
53
67
 
68
+ const reservation = this.relayStore.getReservation(relayConn.remotePeer)
69
+
70
+ if (reservation == null) {
71
+ throw new CodeError('Did not have reservation after making reservation', 'ERR_NO_RESERVATION')
72
+ }
73
+
54
74
  if (this.listeningAddrs.has(relayConn.remotePeer)) {
55
75
  log('already listening on relay %p', relayConn.remotePeer)
56
76
  return
57
77
  }
58
78
 
59
- this.listeningAddrs.set(relayConn.remotePeer, addr)
60
- this.dispatchEvent(new CustomEvent('listening'))
79
+ // add all addresses from the relay reservation
80
+ this.listeningAddrs.set(relayConn.remotePeer, reservation.addrs.map(buf => {
81
+ return multiaddr(buf).encapsulate('/p2p-circuit')
82
+ }))
83
+
84
+ this.safeDispatchEvent('listening', {})
61
85
  }
62
86
 
63
- /**
64
- * Get fixed up multiaddrs
65
- *
66
- * NOTE: This method will grab the peers multiaddrs and expand them such that:
67
- *
68
- * a) If it's an existing /p2p-circuit address for a specific relay i.e.
69
- * `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
70
- * address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
71
- * `QmPeer` is this peers id
72
- * b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
73
- * addr, such when dialing over a relay with this address, it will create the circuit using
74
- * the encapsulated transport address. This is useful when for example, a peer should only
75
- * be dialed over TCP rather than any other transport
76
- */
77
87
  getAddrs (): Multiaddr[] {
78
- return [...this.listeningAddrs.values()]
88
+ return [...this.listeningAddrs.values()].flat()
79
89
  }
80
90
 
81
91
  async close (): Promise<void> {
@@ -89,7 +99,7 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
89
99
 
90
100
  if (had) {
91
101
  // Announce listen addresses change
92
- this.dispatchEvent(new CustomEvent('close'))
102
+ this.safeDispatchEvent('close', {})
93
103
  }
94
104
  }
95
105
  }
@@ -1,19 +1,20 @@
1
- import type { PeerId } from '@libp2p/interface-peer-id'
1
+ import { EventEmitter } from '@libp2p/interfaces/events'
2
+ import { logger } from '@libp2p/logger'
2
3
  import { PeerMap } from '@libp2p/peer-collections'
4
+ import { multiaddr } from '@multiformats/multiaddr'
5
+ import { pbStream } from 'it-pb-stream'
3
6
  import PQueue from 'p-queue'
4
7
  import { DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js'
5
- import { logger } from '@libp2p/logger'
6
- import { pbStream } from 'it-pb-stream'
7
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
8
- import type { Connection } from '@libp2p/interface-connection'
9
- import type { Reservation } from '../pb/index.js'
10
8
  import { HopMessage, Status } from '../pb/index.js'
11
9
  import { getExpirationMilliseconds } from '../utils.js'
10
+ import type { Reservation } from '../pb/index.js'
11
+ import type { Connection } from '@libp2p/interface-connection'
12
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
13
+ import type { Libp2pEvents } from '@libp2p/interface-libp2p'
14
+ import type { PeerId } from '@libp2p/interface-peer-id'
15
+ import type { PeerStore } from '@libp2p/interface-peer-store'
12
16
  import type { TransportManager } from '@libp2p/interface-transport'
13
17
  import type { Startable } from '@libp2p/interfaces/startable'
14
- import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
15
- import type { PeerStore } from '@libp2p/interface-peer-store'
16
- import { multiaddr } from '@multiformats/multiaddr'
17
18
 
18
19
  const log = logger('libp2p:circuit-relay:transport:reservation-store')
19
20
 
@@ -31,6 +32,7 @@ export interface RelayStoreComponents {
31
32
  connectionManager: ConnectionManager
32
33
  transportManager: TransportManager
33
34
  peerStore: PeerStore
35
+ events: EventEmitter<Libp2pEvents>
34
36
  }
35
37
 
36
38
  export interface RelayStoreInit {
@@ -68,6 +70,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
68
70
  private readonly connectionManager: ConnectionManager
69
71
  private readonly transportManager: TransportManager
70
72
  private readonly peerStore: PeerStore
73
+ private readonly events: EventEmitter<Libp2pEvents>
71
74
  private readonly reserveQueue: PQueue
72
75
  private readonly reservations: PeerMap<RelayEntry>
73
76
  private readonly maxDiscoveredRelays: number
@@ -80,6 +83,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
80
83
  this.connectionManager = components.connectionManager
81
84
  this.transportManager = components.transportManager
82
85
  this.peerStore = components.peerStore
86
+ this.events = components.events
83
87
  this.reservations = new PeerMap()
84
88
  this.maxDiscoveredRelays = init?.discoverRelays ?? 0
85
89
  this.started = false
@@ -92,8 +96,8 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
92
96
  // When a peer disconnects, if we had a reservation on that peer
93
97
  // remove the reservation and multiaddr and maybe trigger search
94
98
  // for new relays
95
- this.connectionManager.addEventListener('peer:disconnect', (evt) => {
96
- this.removeRelay(evt.detail.remotePeer)
99
+ this.events.addEventListener('peer:disconnect', (evt) => {
100
+ this.#removeRelay(evt.detail)
97
101
  })
98
102
  }
99
103
 
@@ -121,15 +125,15 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
121
125
  * on the remote peer
122
126
  */
123
127
  async addRelay (peerId: PeerId, type: RelayType): Promise<void> {
124
- log('add relay', this.reserveQueue.size)
128
+ if (this.peerId.equals(peerId)) {
129
+ log('not trying to use self as relay')
130
+ return
131
+ }
132
+
133
+ log('add relay %p', peerId)
125
134
 
126
135
  await this.reserveQueue.add(async () => {
127
136
  try {
128
- if (this.peerId.equals(peerId)) {
129
- log('not trying to use self as relay')
130
- return
131
- }
132
-
133
137
  // allow refresh of an existing reservation if it is about to expire
134
138
  const existingReservation = this.reservations.get(peerId)
135
139
 
@@ -177,6 +181,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
177
181
  })
178
182
  }, timeoutDuration)
179
183
 
184
+ // we've managed to create a reservation successfully
180
185
  this.reservations.set(peerId, {
181
186
  timeout,
182
187
  reservation,
@@ -184,18 +189,22 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
184
189
  })
185
190
 
186
191
  // ensure we don't close the connection to the relay
187
- await this.peerStore.tagPeer(peerId, RELAY_TAG, {
188
- value: 1,
189
- ttl: expiration
192
+ await this.peerStore.merge(peerId, {
193
+ tags: {
194
+ [RELAY_TAG]: {
195
+ value: 1,
196
+ ttl: expiration
197
+ }
198
+ }
190
199
  })
191
200
 
192
- await this.transportManager.listen(
193
- reservation.addrs.map(ma => {
194
- return multiaddr(ma).encapsulate('/p2p-circuit')
195
- })
196
- )
201
+ // listen on multiaddr that only the circuit transport is listening for
202
+ await this.transportManager.listen([multiaddr(`/p2p/${peerId.toString()}/p2p-circuit`)])
197
203
  } catch (err) {
198
204
  log.error('could not reserve slot on %p', peerId, err)
205
+
206
+ // if listening failed, remove the reservation
207
+ this.reservations.delete(peerId)
199
208
  }
200
209
  })
201
210
  }
@@ -204,6 +213,10 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
204
213
  return this.reservations.has(peerId)
205
214
  }
206
215
 
216
+ getReservation (peerId: PeerId): Reservation | undefined {
217
+ return this.reservations.get(peerId)?.reservation
218
+ }
219
+
207
220
  async #createReservation (connection: Connection): Promise<Reservation> {
208
221
  log('requesting reservation from %s', connection.remotePeer)
209
222
  const stream = await connection.newStream(RELAY_V2_HOP_CODEC)
@@ -235,14 +248,14 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
235
248
  /**
236
249
  * Remove listen relay
237
250
  */
238
- removeRelay (peerId: PeerId): void {
251
+ #removeRelay (peerId: PeerId): void {
239
252
  const existingReservation = this.reservations.get(peerId)
240
253
 
241
254
  if (existingReservation == null) {
242
255
  return
243
256
  }
244
257
 
245
- log('removing relay %p', peerId)
258
+ log('connection to relay %p closed, removing reservation from local store', peerId)
246
259
 
247
260
  clearTimeout(existingReservation.timeout)
248
261
  this.reservations.delete(peerId)
@@ -250,7 +263,8 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
250
263
  this.safeDispatchEvent('relay:removed', { detail: peerId })
251
264
 
252
265
  if (this.reservations.size < this.maxDiscoveredRelays) {
253
- this.dispatchEvent(new CustomEvent('relay:not-enough-relays'))
266
+ log('not enough relays %d/%d', this.reservations.size, this.maxDiscoveredRelays)
267
+ this.safeDispatchEvent('relay:not-enough-relays', {})
254
268
  }
255
269
  }
256
270
  }
@@ -1,13 +1,13 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import { abortableSource } from 'abortable-iterator'
3
+ import { anySignal } from 'any-signal'
1
4
  import { CID } from 'multiformats/cid'
2
5
  import { sha256 } from 'multiformats/hashes/sha2'
3
- import type { Source } from 'it-stream-types'
4
- import type { Uint8ArrayList } from 'uint8arraylist'
6
+ import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT } from './constants.js'
5
7
  import type { Limit } from './pb/index.js'
6
- import { logger } from '@libp2p/logger'
7
8
  import type { Stream } from '@libp2p/interface-connection'
8
- import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT } from './constants.js'
9
- import { abortableSource } from 'abortable-iterator'
10
- import { anySignal } from 'any-signal'
9
+ import type { Source } from 'it-stream-types'
10
+ import type { Uint8ArrayList } from 'uint8arraylist'
11
11
 
12
12
  const log = logger('libp2p:circuit-relay:utils')
13
13