libp2p 0.43.4 → 0.44.0-17eb162a
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.
- package/dist/index.min.js +29 -29
- package/dist/src/address-manager/index.d.ts +4 -4
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +9 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/autonat/index.d.ts +1 -2
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +4 -6
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.js +2 -2
- package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +7 -3
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +3 -1
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +3 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +6 -2
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +3 -0
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +5 -4
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +4 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +11 -6
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/circuit-relay/utils.d.ts.map +1 -1
- package/dist/src/circuit-relay/utils.js +3 -1
- package/dist/src/circuit-relay/utils.js.map +1 -1
- package/dist/src/components.d.ts +8 -6
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +10 -10
- package/dist/src/components.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -9
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +39 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
- package/dist/src/connection-manager/auto-dial.js +131 -0
- package/dist/src/connection-manager/auto-dial.js.map +1 -0
- package/dist/src/connection-manager/connection-pruner.d.ts +32 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
- package/dist/src/connection-manager/connection-pruner.js +111 -0
- package/dist/src/connection-manager/connection-pruner.js.map +1 -0
- package/dist/src/connection-manager/constants.d.ts +49 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -0
- package/dist/src/connection-manager/constants.js +49 -0
- package/dist/src/connection-manager/constants.js.map +1 -0
- package/dist/src/connection-manager/dial-queue.d.ts +78 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
- package/dist/src/connection-manager/dial-queue.js +374 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +70 -73
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +116 -248
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +9 -0
- package/dist/src/connection-manager/utils.d.ts.map +1 -0
- package/dist/src/connection-manager/utils.js +66 -0
- package/dist/src/connection-manager/utils.js.map +1 -0
- package/dist/src/content-routing/index.js +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +3 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/fetch/index.js +3 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +4 -0
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +104 -128
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +2 -19
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +3 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +5 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +47 -50
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -2
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +25 -23
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +23 -9
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +14 -11
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +14 -10
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +46 -45
- package/src/address-manager/index.ts +12 -8
- package/src/autonat/index.ts +6 -9
- package/src/circuit-relay/server/advert-service.ts +2 -2
- package/src/circuit-relay/server/index.ts +7 -3
- package/src/circuit-relay/transport/discovery.ts +3 -1
- package/src/circuit-relay/transport/index.ts +10 -2
- package/src/circuit-relay/transport/listener.ts +8 -4
- package/src/circuit-relay/transport/reservation-store.ts +15 -7
- package/src/circuit-relay/utils.ts +3 -1
- package/src/components.ts +19 -17
- package/src/config.ts +0 -9
- package/src/connection-manager/auto-dial.ts +183 -0
- package/src/connection-manager/connection-pruner.ts +157 -0
- package/src/connection-manager/constants.ts +60 -0
- package/src/connection-manager/dial-queue.ts +512 -0
- package/src/connection-manager/index.ts +191 -340
- package/src/connection-manager/utils.ts +76 -0
- package/src/content-routing/index.ts +1 -1
- package/src/content-routing/utils.ts +3 -1
- package/src/fetch/index.ts +4 -4
- package/src/identify/index.ts +100 -140
- package/src/index.ts +2 -20
- package/src/insecure/index.ts +9 -5
- package/src/libp2p.ts +54 -60
- package/src/registrar.ts +29 -25
- package/src/transport-manager.ts +31 -11
- package/src/upgrader.ts +31 -22
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
- package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
- package/dist/src/connection-manager/auto-dialler.js +0 -105
- package/dist/src/connection-manager/auto-dialler.js.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.js +0 -114
- package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
- package/dist/src/connection-manager/dialer/index.d.ts +0 -121
- package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/index.js +0 -324
- package/dist/src/connection-manager/dialer/index.js.map +0 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
- package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
- package/dist/src/connection-manager/latency-monitor.js +0 -220
- package/dist/src/connection-manager/latency-monitor.js.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
- package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
- package/dist/src/constants.d.ts +0 -27
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -31
- package/dist/src/constants.js.map +0 -1
- package/dist/src/peer-record-updater.d.ts +0 -24
- package/dist/src/peer-record-updater.d.ts.map +0 -1
- package/dist/src/peer-record-updater.js +0 -44
- package/dist/src/peer-record-updater.js.map +0 -1
- package/dist/typedoc-urls.json +0 -41
- package/src/connection-manager/auto-dialler.ts +0 -158
- package/src/connection-manager/dialer/dial-request.ts +0 -144
- package/src/connection-manager/dialer/index.ts +0 -461
- package/src/connection-manager/latency-monitor.ts +0 -319
- package/src/connection-manager/visibility-change-emitter.ts +0 -135
- package/src/constants.ts +0 -36
- package/src/peer-record-updater.ts +0 -65
package/src/autonat/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
2
2
|
import type { Connection } from '@libp2p/interface-connection'
|
|
3
|
-
import type { ConnectionManager
|
|
3
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
4
4
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
5
5
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
6
6
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
@@ -74,7 +74,6 @@ export interface DefaultAutonatComponents {
|
|
|
74
74
|
registrar: Registrar
|
|
75
75
|
addressManager: AddressManager
|
|
76
76
|
transportManager: TransportManager
|
|
77
|
-
dialer: Dialer
|
|
78
77
|
peerId: PeerId
|
|
79
78
|
connectionManager: ConnectionManager
|
|
80
79
|
peerRouting: PeerRouting
|
|
@@ -151,7 +150,7 @@ export class AutonatService implements Startable {
|
|
|
151
150
|
|
|
152
151
|
await pipe(
|
|
153
152
|
source,
|
|
154
|
-
lp.decode(),
|
|
153
|
+
(source) => lp.decode(source),
|
|
155
154
|
async function * (stream) {
|
|
156
155
|
const buf = await first(stream)
|
|
157
156
|
|
|
@@ -319,9 +318,7 @@ export class AutonatService implements Startable {
|
|
|
319
318
|
lastMultiaddr = multiaddr
|
|
320
319
|
|
|
321
320
|
try {
|
|
322
|
-
|
|
323
|
-
// multiaddr for the peer
|
|
324
|
-
connection = await self.components.dialer.dial(multiaddr, {
|
|
321
|
+
connection = await self.components.connectionManager.openConnection(multiaddr, {
|
|
325
322
|
signal: controller.signal
|
|
326
323
|
})
|
|
327
324
|
|
|
@@ -360,7 +357,7 @@ export class AutonatService implements Startable {
|
|
|
360
357
|
}
|
|
361
358
|
})
|
|
362
359
|
},
|
|
363
|
-
lp.encode(),
|
|
360
|
+
(source) => lp.encode(source),
|
|
364
361
|
// pipe to the stream, not the abortable source other wise we
|
|
365
362
|
// can't tell the remote when a dial timed out..
|
|
366
363
|
data.stream
|
|
@@ -449,9 +446,9 @@ export class AutonatService implements Startable {
|
|
|
449
446
|
|
|
450
447
|
const buf = await pipe(
|
|
451
448
|
[request],
|
|
452
|
-
lp.encode(),
|
|
449
|
+
(source) => lp.encode(source),
|
|
453
450
|
source,
|
|
454
|
-
lp.decode(),
|
|
451
|
+
(source) => lp.decode(source),
|
|
455
452
|
async (stream) => await first(stream)
|
|
456
453
|
)
|
|
457
454
|
if (buf == null) {
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from '../constants.js'
|
|
8
8
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
9
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
10
|
-
import {
|
|
10
|
+
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
11
11
|
import pRetry from 'p-retry'
|
|
12
12
|
|
|
13
13
|
const log = logger('libp2p:circuit-relay:advert-service')
|
|
@@ -90,7 +90,7 @@ export class AdvertService extends EventEmitter<AdvertServiceEvents> implements
|
|
|
90
90
|
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
|
91
91
|
await this.contentRouting.provide(cid)
|
|
92
92
|
|
|
93
|
-
this.
|
|
93
|
+
this.safeDispatchEvent('advert:success', { detail: undefined })
|
|
94
94
|
} catch (err: any) {
|
|
95
95
|
this.safeDispatchEvent('advert:error', { detail: err })
|
|
96
96
|
|
|
@@ -24,7 +24,7 @@ import { ReservationVoucherRecord } from './reservation-voucher.js'
|
|
|
24
24
|
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
25
25
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
26
26
|
import type { CircuitRelayService, RelayReservation } from '../index.js'
|
|
27
|
-
import {
|
|
27
|
+
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
28
28
|
import { setMaxListeners } from 'events'
|
|
29
29
|
import type { PeerMap } from '@libp2p/peer-collections'
|
|
30
30
|
|
|
@@ -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.
|
|
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.
|
|
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({
|
|
@@ -113,7 +113,9 @@ export class RelayDiscovery extends EventEmitter<RelayDiscoveryEvents> implement
|
|
|
113
113
|
|
|
114
114
|
found++
|
|
115
115
|
log('found relay peer %p in content routing', peerId)
|
|
116
|
-
await this.peerStore.
|
|
116
|
+
await this.peerStore.merge(peerId, {
|
|
117
|
+
multiaddrs: provider.multiaddrs
|
|
118
|
+
})
|
|
117
119
|
|
|
118
120
|
this.safeDispatchEvent('relay:discover', { detail: peerId })
|
|
119
121
|
}
|
|
@@ -23,6 +23,8 @@ import { CIRCUIT_PROTO_CODE, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../
|
|
|
23
23
|
import { RelayStoreInit, ReservationStore } from './reservation-store.js'
|
|
24
24
|
import { RelayDiscovery, RelayDiscoveryComponents } from './discovery.js'
|
|
25
25
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
26
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
27
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
26
28
|
|
|
27
29
|
const log = logger('libp2p:circuit-relay:transport')
|
|
28
30
|
|
|
@@ -49,6 +51,7 @@ export interface CircuitRelayTransportComponents extends RelayDiscoveryComponent
|
|
|
49
51
|
addressManager: AddressManager
|
|
50
52
|
contentRouting: ContentRouting
|
|
51
53
|
connectionGater: ConnectionGater
|
|
54
|
+
events: EventEmitter<Libp2pEvents>
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
interface ConnectOptions {
|
|
@@ -81,6 +84,7 @@ class CircuitRelayTransport implements Transport {
|
|
|
81
84
|
private readonly upgrader: Upgrader
|
|
82
85
|
private readonly addressManager: AddressManager
|
|
83
86
|
private readonly connectionGater: ConnectionGater
|
|
87
|
+
private readonly events: EventEmitter<Libp2pEvents>
|
|
84
88
|
private readonly reservationStore: ReservationStore
|
|
85
89
|
private started: boolean
|
|
86
90
|
|
|
@@ -92,6 +96,7 @@ class CircuitRelayTransport implements Transport {
|
|
|
92
96
|
this.upgrader = components.upgrader
|
|
93
97
|
this.addressManager = components.addressManager
|
|
94
98
|
this.connectionGater = components.connectionGater
|
|
99
|
+
this.events = components.events
|
|
95
100
|
|
|
96
101
|
if (init.discoverRelays != null && init.discoverRelays > 0) {
|
|
97
102
|
this.discovery = new RelayDiscovery(components)
|
|
@@ -178,7 +183,9 @@ class CircuitRelayTransport implements Transport {
|
|
|
178
183
|
let relayConnection = relayConnections[0]
|
|
179
184
|
|
|
180
185
|
if (relayConnection == null) {
|
|
181
|
-
await this.peerStore.
|
|
186
|
+
await this.peerStore.merge(relayPeer, {
|
|
187
|
+
multiaddrs: [relayAddr]
|
|
188
|
+
})
|
|
182
189
|
relayConnection = await this.connectionManager.openConnection(relayPeer, options)
|
|
183
190
|
disconnectOnFailure = true
|
|
184
191
|
}
|
|
@@ -249,7 +256,8 @@ class CircuitRelayTransport implements Transport {
|
|
|
249
256
|
createListener (options: CreateListenerOptions): Listener {
|
|
250
257
|
return createListener({
|
|
251
258
|
connectionManager: this.connectionManager,
|
|
252
|
-
relayStore: this.reservationStore
|
|
259
|
+
relayStore: this.reservationStore,
|
|
260
|
+
events: this.events
|
|
253
261
|
})
|
|
254
262
|
}
|
|
255
263
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
2
2
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
3
3
|
import type { Listener, ListenerEvents } from '@libp2p/interface-transport'
|
|
4
4
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
@@ -7,24 +7,28 @@ import type { ReservationStore } from './reservation-store.js'
|
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
8
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
9
9
|
import { logger } from '@libp2p/logger'
|
|
10
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
10
11
|
|
|
11
12
|
const log = logger('libp2p:circuit-relay:transport:listener')
|
|
12
13
|
|
|
13
14
|
export interface CircuitRelayTransportListenerComponents {
|
|
14
15
|
connectionManager: ConnectionManager
|
|
15
16
|
relayStore: ReservationStore
|
|
17
|
+
events: EventEmitter<Libp2pEvents>
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> implements Listener {
|
|
19
21
|
private readonly connectionManager: ConnectionManager
|
|
20
22
|
private readonly relayStore: ReservationStore
|
|
21
23
|
private readonly listeningAddrs: PeerMap<Multiaddr>
|
|
24
|
+
private readonly events: EventEmitter<Libp2pEvents>
|
|
22
25
|
|
|
23
26
|
constructor (components: CircuitRelayTransportListenerComponents) {
|
|
24
27
|
super()
|
|
25
28
|
|
|
26
29
|
this.connectionManager = components.connectionManager
|
|
27
30
|
this.relayStore = components.relayStore
|
|
31
|
+
this.events = components.events
|
|
28
32
|
this.listeningAddrs = new PeerMap()
|
|
29
33
|
|
|
30
34
|
// remove listening addrs when a relay is removed
|
|
@@ -33,7 +37,7 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
|
|
|
33
37
|
})
|
|
34
38
|
|
|
35
39
|
// remove listening addrs when a peer disconnects
|
|
36
|
-
this.
|
|
40
|
+
this.events.addEventListener('connection:close', (evt) => {
|
|
37
41
|
this.#removeRelayPeer(evt.detail.remotePeer)
|
|
38
42
|
})
|
|
39
43
|
}
|
|
@@ -57,7 +61,7 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
|
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
this.listeningAddrs.set(relayConn.remotePeer, addr)
|
|
60
|
-
this.
|
|
64
|
+
this.safeDispatchEvent('listening', {})
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
/**
|
|
@@ -89,7 +93,7 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
|
|
|
89
93
|
|
|
90
94
|
if (had) {
|
|
91
95
|
// Announce listen addresses change
|
|
92
|
-
this.
|
|
96
|
+
this.safeDispatchEvent('close', {})
|
|
93
97
|
}
|
|
94
98
|
}
|
|
95
99
|
}
|
|
@@ -10,10 +10,11 @@ import type { Reservation } from '../pb/index.js'
|
|
|
10
10
|
import { HopMessage, Status } from '../pb/index.js'
|
|
11
11
|
import { getExpirationMilliseconds } from '../utils.js'
|
|
12
12
|
import type { TransportManager } from '@libp2p/interface-transport'
|
|
13
|
-
import type { Startable } from '@libp2p/interfaces/
|
|
14
|
-
import {
|
|
13
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
14
|
+
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
15
15
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
16
16
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
17
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
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,7 +96,7 @@ 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.
|
|
99
|
+
this.events.addEventListener('connection:close', (evt) => {
|
|
96
100
|
this.removeRelay(evt.detail.remotePeer)
|
|
97
101
|
})
|
|
98
102
|
}
|
|
@@ -184,9 +188,13 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
184
188
|
})
|
|
185
189
|
|
|
186
190
|
// ensure we don't close the connection to the relay
|
|
187
|
-
await this.peerStore.
|
|
188
|
-
|
|
189
|
-
|
|
191
|
+
await this.peerStore.merge(peerId, {
|
|
192
|
+
tags: {
|
|
193
|
+
[RELAY_TAG]: {
|
|
194
|
+
value: 1,
|
|
195
|
+
ttl: expiration
|
|
196
|
+
}
|
|
197
|
+
}
|
|
190
198
|
})
|
|
191
199
|
|
|
192
200
|
await this.transportManager.listen(
|
|
@@ -250,7 +258,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
250
258
|
this.safeDispatchEvent('relay:removed', { detail: peerId })
|
|
251
259
|
|
|
252
260
|
if (this.reservations.size < this.maxDiscoveredRelays) {
|
|
253
|
-
this.
|
|
261
|
+
this.safeDispatchEvent('relay:not-enough-relays', {})
|
|
254
262
|
}
|
|
255
263
|
}
|
|
256
264
|
}
|
|
@@ -7,7 +7,7 @@ import { logger } from '@libp2p/logger'
|
|
|
7
7
|
import type { Stream } from '@libp2p/interface-connection'
|
|
8
8
|
import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT } from './constants.js'
|
|
9
9
|
import { abortableSource } from 'abortable-iterator'
|
|
10
|
-
import anySignal from 'any-signal'
|
|
10
|
+
import { anySignal } from 'any-signal'
|
|
11
11
|
|
|
12
12
|
const log = logger('libp2p:circuit-relay:utils')
|
|
13
13
|
|
|
@@ -69,6 +69,7 @@ const doRelay = (src: Stream, dst: Stream, abortSignal: AbortSignal, limit: Requ
|
|
|
69
69
|
srcDstFinished = true
|
|
70
70
|
|
|
71
71
|
if (dstSrcFinished) {
|
|
72
|
+
signal.clear()
|
|
72
73
|
clearTimeout(timeout)
|
|
73
74
|
}
|
|
74
75
|
})
|
|
@@ -86,6 +87,7 @@ const doRelay = (src: Stream, dst: Stream, abortSignal: AbortSignal, limit: Requ
|
|
|
86
87
|
dstSrcFinished = true
|
|
87
88
|
|
|
88
89
|
if (srcDstFinished) {
|
|
90
|
+
signal.clear()
|
|
89
91
|
clearTimeout(timeout)
|
|
90
92
|
}
|
|
91
93
|
})
|
package/src/components.ts
CHANGED
|
@@ -12,11 +12,14 @@ import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
|
|
|
12
12
|
import type { Datastore } from 'interface-datastore'
|
|
13
13
|
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
14
14
|
import type { DualDHT } from '@libp2p/interface-dht'
|
|
15
|
-
import type { ConnectionManager
|
|
15
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
16
16
|
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
|
|
17
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
18
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
17
19
|
|
|
18
20
|
export interface Components {
|
|
19
21
|
peerId: PeerId
|
|
22
|
+
events: EventEmitter<Libp2pEvents>
|
|
20
23
|
addressManager: AddressManager
|
|
21
24
|
peerStore: PeerStore
|
|
22
25
|
upgrader: Upgrader
|
|
@@ -28,7 +31,6 @@ export interface Components {
|
|
|
28
31
|
peerRouting: PeerRouting
|
|
29
32
|
datastore: Datastore
|
|
30
33
|
connectionProtector?: ConnectionProtector
|
|
31
|
-
dialer: Dialer
|
|
32
34
|
metrics?: Metrics
|
|
33
35
|
dht?: DualDHT
|
|
34
36
|
pubsub?: PubSub
|
|
@@ -36,6 +38,7 @@ export interface Components {
|
|
|
36
38
|
|
|
37
39
|
export interface ComponentsInit {
|
|
38
40
|
peerId?: PeerId
|
|
41
|
+
events?: EventEmitter<Libp2pEvents>
|
|
39
42
|
addressManager?: AddressManager
|
|
40
43
|
peerStore?: PeerStore
|
|
41
44
|
upgrader?: Upgrader
|
|
@@ -50,11 +53,11 @@ export interface ComponentsInit {
|
|
|
50
53
|
connectionProtector?: ConnectionProtector
|
|
51
54
|
dht?: DualDHT
|
|
52
55
|
pubsub?: PubSub
|
|
53
|
-
dialer?: Dialer
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
export class DefaultComponents implements Components, Startable {
|
|
57
59
|
private _peerId?: PeerId
|
|
60
|
+
private _events?: EventEmitter<Libp2pEvents>
|
|
58
61
|
private _addressManager?: AddressManager
|
|
59
62
|
private _peerStore?: PeerStore
|
|
60
63
|
private _upgrader?: Upgrader
|
|
@@ -69,11 +72,11 @@ export class DefaultComponents implements Components, Startable {
|
|
|
69
72
|
private _connectionProtector?: ConnectionProtector
|
|
70
73
|
private _dht?: DualDHT
|
|
71
74
|
private _pubsub?: PubSub
|
|
72
|
-
private _dialer?: Dialer
|
|
73
75
|
private _started = false
|
|
74
76
|
|
|
75
77
|
constructor (init: ComponentsInit = {}) {
|
|
76
78
|
this._peerId = init.peerId
|
|
79
|
+
this._events = init.events
|
|
77
80
|
this._addressManager = init.addressManager
|
|
78
81
|
this._peerStore = init.peerStore
|
|
79
82
|
this._upgrader = init.upgrader
|
|
@@ -88,7 +91,6 @@ export class DefaultComponents implements Components, Startable {
|
|
|
88
91
|
this._connectionProtector = init.connectionProtector
|
|
89
92
|
this._dht = init.dht
|
|
90
93
|
this._pubsub = init.pubsub
|
|
91
|
-
this._dialer = init.dialer
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
isStarted (): boolean {
|
|
@@ -167,6 +169,18 @@ export class DefaultComponents implements Components, Startable {
|
|
|
167
169
|
this._peerId = peerId
|
|
168
170
|
}
|
|
169
171
|
|
|
172
|
+
get events (): EventEmitter<Libp2pEvents> {
|
|
173
|
+
if (this._events == null) {
|
|
174
|
+
throw new CodeError('events not set', 'ERR_SERVICE_MISSING')
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return this._events
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
set events (events: EventEmitter<Libp2pEvents>) {
|
|
181
|
+
this._events = events
|
|
182
|
+
}
|
|
183
|
+
|
|
170
184
|
get addressManager (): AddressManager {
|
|
171
185
|
if (this._addressManager == null) {
|
|
172
186
|
throw new CodeError('addressManager not set', 'ERR_SERVICE_MISSING')
|
|
@@ -295,18 +309,6 @@ export class DefaultComponents implements Components, Startable {
|
|
|
295
309
|
this._connectionProtector = connectionProtector
|
|
296
310
|
}
|
|
297
311
|
|
|
298
|
-
get dialer (): Dialer {
|
|
299
|
-
if (this._dialer == null) {
|
|
300
|
-
throw new CodeError('dialer not set', 'ERR_SERVICE_MISSING')
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return this._dialer
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
set dialer (dialer: Dialer) {
|
|
307
|
-
this._dialer = dialer
|
|
308
|
-
}
|
|
309
|
-
|
|
310
312
|
get metrics (): Metrics | undefined {
|
|
311
313
|
return this._metrics
|
|
312
314
|
}
|
package/src/config.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import mergeOptions from 'merge-options'
|
|
2
2
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
3
|
-
import * as Constants from './constants.js'
|
|
4
3
|
import { AGENT_VERSION } from './identify/consts.js'
|
|
5
4
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
6
5
|
import { FaultTolerance } from '@libp2p/interface-transport'
|
|
@@ -19,19 +18,11 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
19
18
|
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
|
|
20
19
|
},
|
|
21
20
|
connectionManager: {
|
|
22
|
-
maxConnections: 300,
|
|
23
|
-
minConnections: 50,
|
|
24
|
-
autoDialInterval: 10000,
|
|
25
|
-
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
|
26
|
-
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
|
27
|
-
dialTimeout: Constants.DIAL_TIMEOUT,
|
|
28
|
-
inboundUpgradeTimeout: Constants.INBOUND_UPGRADE_TIMEOUT,
|
|
29
21
|
resolvers: {
|
|
30
22
|
dnsaddr: dnsaddrResolver
|
|
31
23
|
},
|
|
32
24
|
addressSorter: publicAddressesFirst
|
|
33
25
|
},
|
|
34
|
-
connectionGater: {},
|
|
35
26
|
transportManager: {
|
|
36
27
|
faultTolerance: FaultTolerance.FATAL_ALL
|
|
37
28
|
},
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
3
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
4
|
+
import { PeerMap } from '@libp2p/peer-collections'
|
|
5
|
+
import PQueue from 'p-queue'
|
|
6
|
+
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, MIN_CONNECTIONS } from './constants.js'
|
|
7
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
8
|
+
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
9
|
+
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
10
|
+
|
|
11
|
+
const log = logger('libp2p:connection-manager:auto-dial')
|
|
12
|
+
|
|
13
|
+
interface AutoDialInit {
|
|
14
|
+
minConnections?: number
|
|
15
|
+
autoDialConcurrency?: number
|
|
16
|
+
autoDialPriority?: number
|
|
17
|
+
autoDialInterval?: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface AutoDialComponents {
|
|
21
|
+
connectionManager: ConnectionManager
|
|
22
|
+
peerStore: PeerStore
|
|
23
|
+
events: EventEmitter<Libp2pEvents>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const defaultOptions = {
|
|
27
|
+
minConnections: MIN_CONNECTIONS,
|
|
28
|
+
autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
|
|
29
|
+
autoDialPriority: AUTO_DIAL_PRIORITY,
|
|
30
|
+
autoDialInterval: AUTO_DIAL_INTERVAL
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class AutoDial implements Startable {
|
|
34
|
+
private readonly connectionManager: ConnectionManager
|
|
35
|
+
private readonly peerStore: PeerStore
|
|
36
|
+
private readonly queue: PQueue
|
|
37
|
+
private readonly minConnections: number
|
|
38
|
+
private readonly autoDialPriority: number
|
|
39
|
+
private readonly autoDialIntervalMs: number
|
|
40
|
+
private autoDialInterval?: ReturnType<typeof setInterval>
|
|
41
|
+
private started: boolean
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
45
|
+
* It will keep the number of connections below the upper limit and sort
|
|
46
|
+
* the peers to connect based on whether we know their keys and protocols.
|
|
47
|
+
*/
|
|
48
|
+
constructor (components: AutoDialComponents, init: AutoDialInit) {
|
|
49
|
+
this.connectionManager = components.connectionManager
|
|
50
|
+
this.peerStore = components.peerStore
|
|
51
|
+
this.minConnections = init.minConnections ?? defaultOptions.minConnections
|
|
52
|
+
this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority
|
|
53
|
+
this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
|
|
54
|
+
this.started = false
|
|
55
|
+
this.queue = new PQueue({
|
|
56
|
+
concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
|
|
57
|
+
})
|
|
58
|
+
this.queue.addListener('error', (err) => {
|
|
59
|
+
log.error('error during auto-dial', err)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// check the min connection limit whenever a peer disconnects
|
|
63
|
+
components.events.addEventListener('connection:close', () => {
|
|
64
|
+
this.autoDial()
|
|
65
|
+
.catch(err => {
|
|
66
|
+
log.error(err)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
isStarted (): boolean {
|
|
72
|
+
return this.started
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
start (): void {
|
|
76
|
+
this.autoDialInterval = setInterval(() => {
|
|
77
|
+
this.autoDial()
|
|
78
|
+
.catch(err => {
|
|
79
|
+
log.error('error while autodialing', err)
|
|
80
|
+
})
|
|
81
|
+
}, this.autoDialIntervalMs)
|
|
82
|
+
this.started = true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
afterStart (): void {
|
|
86
|
+
this.autoDial()
|
|
87
|
+
.catch(err => {
|
|
88
|
+
log.error('error while autodialing', err)
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
stop (): void {
|
|
93
|
+
// clear the queue
|
|
94
|
+
this.queue.clear()
|
|
95
|
+
clearInterval(this.autoDialInterval)
|
|
96
|
+
this.started = false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async autoDial (): Promise<void> {
|
|
100
|
+
if (!this.started) {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const numConnections = this.connectionManager.getConnections().length
|
|
105
|
+
|
|
106
|
+
// Already has enough connections
|
|
107
|
+
if (numConnections >= this.minConnections) {
|
|
108
|
+
log('have enough connections %d/%d', numConnections, this.minConnections)
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
|
|
113
|
+
|
|
114
|
+
// Sort peers on whether we know protocols or public keys for them
|
|
115
|
+
const peers = await this.peerStore.all()
|
|
116
|
+
|
|
117
|
+
// Remove some peers
|
|
118
|
+
const filteredPeers = peers.filter((peer) => {
|
|
119
|
+
// Remove peers without addresses
|
|
120
|
+
if (peer.addresses.length === 0) {
|
|
121
|
+
return false
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return true
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
// shuffle the peers - this is so peers with the same tag values will be
|
|
128
|
+
// dialled in a different order each time
|
|
129
|
+
const shuffledPeers = filteredPeers.sort(() => Math.random() > 0.5 ? 1 : -1)
|
|
130
|
+
|
|
131
|
+
// Sort shuffled peers by tag value
|
|
132
|
+
const peerValues = new PeerMap<number>()
|
|
133
|
+
for (const peer of shuffledPeers) {
|
|
134
|
+
if (peerValues.has(peer.id)) {
|
|
135
|
+
continue
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// sum all tag values
|
|
139
|
+
peerValues.set(peer.id, [...peer.tags.values()].reduce((acc, curr) => {
|
|
140
|
+
return acc + curr.value
|
|
141
|
+
}, 0))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// sort by value, highest to lowest
|
|
145
|
+
const sortedPeers = shuffledPeers.sort((a, b) => {
|
|
146
|
+
const peerAValue = peerValues.get(a.id) ?? 0
|
|
147
|
+
const peerBValue = peerValues.get(b.id) ?? 0
|
|
148
|
+
|
|
149
|
+
if (peerAValue > peerBValue) {
|
|
150
|
+
return -1
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (peerAValue < peerBValue) {
|
|
154
|
+
return 1
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return 0
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
log('selected %d/%d peers to dial', sortedPeers.length, peers.length)
|
|
161
|
+
|
|
162
|
+
for (const peer of sortedPeers) {
|
|
163
|
+
this.queue.add(async () => {
|
|
164
|
+
const numConnections = this.connectionManager.getConnections().length
|
|
165
|
+
|
|
166
|
+
// Check to see if we still need to auto dial
|
|
167
|
+
if (numConnections >= this.minConnections) {
|
|
168
|
+
log('got enough connections now %d/%d', numConnections, this.minConnections)
|
|
169
|
+
this.queue.clear()
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
log('connecting to a peerStore stored peer %p', peer.id)
|
|
174
|
+
await this.connectionManager.openConnection(peer.id, {
|
|
175
|
+
// @ts-expect-error needs adding to the ConnectionManager interface
|
|
176
|
+
priority: this.autoDialPriority
|
|
177
|
+
})
|
|
178
|
+
}).catch(err => {
|
|
179
|
+
log.error('could not connect to peerStore stored peer', err)
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|