libp2p 0.46.21 → 1.0.0
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/README.md +51 -47
- package/dist/index.min.js +40 -28
- package/dist/src/address-manager/index.d.ts +4 -5
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +3 -3
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/components.d.ts +4 -15
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +2 -2
- package/dist/src/components.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +1 -1
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
- package/dist/src/config/connection-gater.browser.js.map +1 -1
- package/dist/src/config/connection-gater.d.ts +1 -1
- package/dist/src/config/connection-gater.d.ts.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +1 -2
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +4 -6
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +11 -13
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +3 -6
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +21 -21
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +3 -5
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +8 -8
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +0 -4
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +3 -10
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +30 -38
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +3 -18
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +17 -18
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.js +1 -1
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +1 -5
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +2 -2
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -2
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +1 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/errors.d.ts +0 -2
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -2
- package/dist/src/errors.js.map +1 -1
- package/dist/src/get-peer.d.ts +1 -1
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +1 -5
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +5 -16
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +4 -12
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +31 -65
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -6
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +12 -9
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -7
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +4 -4
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +5 -9
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +12 -13
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +8 -19
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +75 -38
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/dist/typedoc-urls.json +8 -69
- package/package.json +37 -106
- package/src/address-manager/index.ts +6 -9
- package/src/components.ts +5 -16
- package/src/config/connection-gater.browser.ts +1 -1
- package/src/config/connection-gater.ts +1 -1
- package/src/config.ts +1 -2
- package/src/connection/index.ts +12 -17
- package/src/connection-manager/auto-dial.ts +23 -26
- package/src/connection-manager/connection-pruner.ts +10 -12
- package/src/connection-manager/constants.defaults.ts +0 -5
- package/src/connection-manager/dial-queue.ts +31 -46
- package/src/connection-manager/index.ts +19 -36
- package/src/connection-manager/utils.ts +1 -1
- package/src/content-routing/index.ts +3 -7
- package/src/content-routing/utils.ts +2 -3
- package/src/errors.ts +0 -2
- package/src/get-peer.ts +2 -7
- package/src/index.ts +6 -17
- package/src/libp2p.ts +32 -80
- package/src/peer-routing.ts +16 -15
- package/src/registrar.ts +8 -12
- package/src/transport-manager.ts +15 -22
- package/src/upgrader.ts +110 -64
- package/src/version.ts +1 -1
- package/dist/src/autonat/constants.d.ts +0 -18
- package/dist/src/autonat/constants.d.ts.map +0 -1
- package/dist/src/autonat/constants.js +0 -18
- package/dist/src/autonat/constants.js.map +0 -1
- package/dist/src/autonat/index.d.ts +0 -62
- package/dist/src/autonat/index.d.ts.map +0 -1
- package/dist/src/autonat/index.js +0 -447
- package/dist/src/autonat/index.js.map +0 -1
- package/dist/src/autonat/pb/index.d.ts +0 -57
- package/dist/src/autonat/pb/index.d.ts.map +0 -1
- package/dist/src/autonat/pb/index.js +0 -250
- package/dist/src/autonat/pb/index.js.map +0 -1
- package/dist/src/circuit-relay/constants.d.ts +0 -50
- package/dist/src/circuit-relay/constants.d.ts.map +0 -1
- package/dist/src/circuit-relay/constants.js +0 -56
- package/dist/src/circuit-relay/constants.js.map +0 -1
- package/dist/src/circuit-relay/index.d.ts +0 -55
- package/dist/src/circuit-relay/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/index.js +0 -38
- package/dist/src/circuit-relay/index.js.map +0 -1
- package/dist/src/circuit-relay/pb/index.d.ts +0 -93
- package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/pb/index.js +0 -425
- package/dist/src/circuit-relay/pb/index.js.map +0 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/advert-service.js +0 -73
- package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
- package/dist/src/circuit-relay/server/index.d.ts +0 -65
- package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/index.js +0 -312
- package/dist/src/circuit-relay/server/index.js.map +0 -1
- package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
- package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/reservation-store.js +0 -65
- package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
- package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/discovery.js +0 -97
- package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
- package/dist/src/circuit-relay/transport/index.d.ts +0 -57
- package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/index.js +0 -276
- package/dist/src/circuit-relay/transport/index.js.map +0 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
- package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/listener.js +0 -66
- package/dist/src/circuit-relay/transport/listener.js.map +0 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
- package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
- package/dist/src/circuit-relay/utils.d.ts +0 -13
- package/dist/src/circuit-relay/utils.d.ts.map +0 -1
- package/dist/src/circuit-relay/utils.js +0 -108
- package/dist/src/circuit-relay/utils.js.map +0 -1
- package/dist/src/dcutr/dcutr.d.ts +0 -43
- package/dist/src/dcutr/dcutr.d.ts.map +0 -1
- package/dist/src/dcutr/dcutr.js +0 -313
- package/dist/src/dcutr/dcutr.js.map +0 -1
- package/dist/src/dcutr/index.d.ts +0 -92
- package/dist/src/dcutr/index.d.ts.map +0 -1
- package/dist/src/dcutr/index.js +0 -63
- package/dist/src/dcutr/index.js.map +0 -1
- package/dist/src/dcutr/pb/message.d.ts +0 -20
- package/dist/src/dcutr/pb/message.d.ts.map +0 -1
- package/dist/src/dcutr/pb/message.js +0 -77
- package/dist/src/dcutr/pb/message.js.map +0 -1
- package/dist/src/dcutr/utils.d.ts +0 -8
- package/dist/src/dcutr/utils.d.ts.map +0 -1
- package/dist/src/dcutr/utils.js +0 -27
- package/dist/src/dcutr/utils.js.map +0 -1
- package/dist/src/fetch/constants.d.ts +0 -3
- package/dist/src/fetch/constants.d.ts.map +0 -1
- package/dist/src/fetch/constants.js +0 -4
- package/dist/src/fetch/constants.js.map +0 -1
- package/dist/src/fetch/index.d.ts +0 -61
- package/dist/src/fetch/index.d.ts.map +0 -1
- package/dist/src/fetch/index.js +0 -205
- package/dist/src/fetch/index.js.map +0 -1
- package/dist/src/fetch/pb/proto.d.ts +0 -28
- package/dist/src/fetch/pb/proto.d.ts.map +0 -1
- package/dist/src/fetch/pb/proto.js +0 -120
- package/dist/src/fetch/pb/proto.js.map +0 -1
- package/dist/src/identify/consts.d.ts +0 -10
- package/dist/src/identify/consts.d.ts.map +0 -1
- package/dist/src/identify/consts.js +0 -11
- package/dist/src/identify/consts.js.map +0 -1
- package/dist/src/identify/identify.d.ts +0 -54
- package/dist/src/identify/identify.d.ts.map +0 -1
- package/dist/src/identify/identify.js +0 -447
- package/dist/src/identify/identify.js.map +0 -1
- package/dist/src/identify/index.d.ts +0 -72
- package/dist/src/identify/index.d.ts.map +0 -1
- package/dist/src/identify/index.js +0 -15
- package/dist/src/identify/index.js.map +0 -1
- package/dist/src/identify/pb/message.d.ts +0 -17
- package/dist/src/identify/pb/message.d.ts.map +0 -1
- package/dist/src/identify/pb/message.js +0 -98
- package/dist/src/identify/pb/message.js.map +0 -1
- package/dist/src/insecure/index.d.ts +0 -26
- package/dist/src/insecure/index.d.ts.map +0 -1
- package/dist/src/insecure/index.js +0 -110
- package/dist/src/insecure/index.js.map +0 -1
- package/dist/src/insecure/pb/proto.d.ts +0 -30
- package/dist/src/insecure/pb/proto.d.ts.map +0 -1
- package/dist/src/insecure/pb/proto.js +0 -127
- package/dist/src/insecure/pb/proto.js.map +0 -1
- package/dist/src/ping/constants.d.ts +0 -9
- package/dist/src/ping/constants.d.ts.map +0 -1
- package/dist/src/ping/constants.js +0 -15
- package/dist/src/ping/constants.js.map +0 -1
- package/dist/src/ping/index.d.ts +0 -24
- package/dist/src/ping/index.d.ts.map +0 -1
- package/dist/src/ping/index.js +0 -115
- package/dist/src/ping/index.js.map +0 -1
- package/dist/src/pnet/crypto.d.ts +0 -18
- package/dist/src/pnet/crypto.d.ts.map +0 -1
- package/dist/src/pnet/crypto.js +0 -60
- package/dist/src/pnet/crypto.js.map +0 -1
- package/dist/src/pnet/errors.d.ts +0 -6
- package/dist/src/pnet/errors.d.ts.map +0 -1
- package/dist/src/pnet/errors.js +0 -6
- package/dist/src/pnet/errors.js.map +0 -1
- package/dist/src/pnet/index.d.ts +0 -31
- package/dist/src/pnet/index.d.ts.map +0 -1
- package/dist/src/pnet/index.js +0 -97
- package/dist/src/pnet/index.js.map +0 -1
- package/dist/src/pnet/key-generator.d.ts +0 -11
- package/dist/src/pnet/key-generator.d.ts.map +0 -1
- package/dist/src/pnet/key-generator.js +0 -22
- package/dist/src/pnet/key-generator.js.map +0 -1
- package/dist/src/upnp-nat/index.d.ts +0 -71
- package/dist/src/upnp-nat/index.d.ts.map +0 -1
- package/dist/src/upnp-nat/index.js +0 -136
- package/dist/src/upnp-nat/index.js.map +0 -1
- package/dist/src/utils/peer-job-queue.d.ts +0 -33
- package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
- package/dist/src/utils/peer-job-queue.js +0 -82
- package/dist/src/utils/peer-job-queue.js.map +0 -1
- package/src/autonat/constants.ts +0 -19
- package/src/autonat/index.ts +0 -597
- package/src/autonat/pb/index.proto +0 -35
- package/src/autonat/pb/index.ts +0 -320
- package/src/circuit-relay/constants.ts +0 -72
- package/src/circuit-relay/index.ts +0 -59
- package/src/circuit-relay/pb/index.proto +0 -67
- package/src/circuit-relay/pb/index.ts +0 -539
- package/src/circuit-relay/server/advert-service.ts +0 -108
- package/src/circuit-relay/server/index.ts +0 -443
- package/src/circuit-relay/server/reservation-store.ts +0 -116
- package/src/circuit-relay/server/reservation-voucher.ts +0 -51
- package/src/circuit-relay/transport/discovery.ts +0 -136
- package/src/circuit-relay/transport/index.ts +0 -396
- package/src/circuit-relay/transport/listener.ts +0 -97
- package/src/circuit-relay/transport/reservation-store.ts +0 -312
- package/src/circuit-relay/utils.ts +0 -136
- package/src/dcutr/dcutr.ts +0 -378
- package/src/dcutr/index.ts +0 -102
- package/src/dcutr/pb/message.proto +0 -12
- package/src/dcutr/pb/message.ts +0 -96
- package/src/dcutr/utils.ts +0 -33
- package/src/fetch/README.md +0 -41
- package/src/fetch/constants.ts +0 -3
- package/src/fetch/index.ts +0 -315
- package/src/fetch/pb/proto.proto +0 -15
- package/src/fetch/pb/proto.ts +0 -153
- package/src/identify/README.md +0 -13
- package/src/identify/consts.ts +0 -12
- package/src/identify/identify.ts +0 -561
- package/src/identify/index.ts +0 -90
- package/src/identify/pb/message.proto +0 -30
- package/src/identify/pb/message.ts +0 -126
- package/src/insecure/index.ts +0 -138
- package/src/insecure/pb/proto.proto +0 -18
- package/src/insecure/pb/proto.ts +0 -161
- package/src/ping/constants.ts +0 -15
- package/src/ping/index.ts +0 -170
- package/src/pnet/README.md +0 -68
- package/src/pnet/crypto.ts +0 -67
- package/src/pnet/errors.ts +0 -5
- package/src/pnet/index.ts +0 -126
- package/src/pnet/key-generator.ts +0 -23
- package/src/upnp-nat/index.ts +0 -212
- package/src/utils/peer-job-queue.ts +0 -119
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { PeerMap, PeerSet } from '@libp2p/peer-collections'
|
|
2
|
+
import { PeerJobQueue } from '@libp2p/utils/peer-job-queue'
|
|
2
3
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
3
|
-
import { PeerJobQueue } from '../utils/peer-job-queue.js'
|
|
4
4
|
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js'
|
|
5
|
-
import type { Libp2pEvents, Logger, ComponentLogger } from '@libp2p/interface'
|
|
6
|
-
import type {
|
|
7
|
-
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
8
|
-
import type { Startable } from '@libp2p/interface/startable'
|
|
9
|
-
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
5
|
+
import type { Libp2pEvents, Logger, ComponentLogger, TypedEventTarget, PeerStore, Startable } from '@libp2p/interface'
|
|
6
|
+
import type { ConnectionManager } from '@libp2p/interface-internal'
|
|
10
7
|
|
|
11
8
|
interface AutoDialInit {
|
|
12
9
|
minConnections?: number
|
|
@@ -48,7 +45,7 @@ export class AutoDial implements Startable {
|
|
|
48
45
|
private autoDialInterval?: ReturnType<typeof setInterval>
|
|
49
46
|
private started: boolean
|
|
50
47
|
private running: boolean
|
|
51
|
-
readonly
|
|
48
|
+
private readonly log: Logger
|
|
52
49
|
|
|
53
50
|
/**
|
|
54
51
|
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
@@ -64,21 +61,21 @@ export class AutoDial implements Startable {
|
|
|
64
61
|
this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
|
|
65
62
|
this.autoDialPeerRetryThresholdMs = init.autoDialPeerRetryThreshold ?? defaultOptions.autoDialPeerRetryThreshold
|
|
66
63
|
this.autoDialDiscoveredPeersDebounce = init.autoDialDiscoveredPeersDebounce ?? defaultOptions.autoDialDiscoveredPeersDebounce
|
|
67
|
-
this
|
|
64
|
+
this.log = components.logger.forComponent('libp2p:connection-manager:auto-dial')
|
|
68
65
|
this.started = false
|
|
69
66
|
this.running = false
|
|
70
67
|
this.queue = new PeerJobQueue({
|
|
71
68
|
concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
|
|
72
69
|
})
|
|
73
70
|
this.queue.addListener('error', (err) => {
|
|
74
|
-
this
|
|
71
|
+
this.log.error('error during auto-dial', err)
|
|
75
72
|
})
|
|
76
73
|
|
|
77
74
|
// check the min connection limit whenever a peer disconnects
|
|
78
75
|
components.events.addEventListener('connection:close', () => {
|
|
79
76
|
this.autoDial()
|
|
80
77
|
.catch(err => {
|
|
81
|
-
this
|
|
78
|
+
this.log.error(err)
|
|
82
79
|
})
|
|
83
80
|
})
|
|
84
81
|
|
|
@@ -93,7 +90,7 @@ export class AutoDial implements Startable {
|
|
|
93
90
|
debounce = setTimeout(() => {
|
|
94
91
|
this.autoDial()
|
|
95
92
|
.catch(err => {
|
|
96
|
-
this
|
|
93
|
+
this.log.error(err)
|
|
97
94
|
})
|
|
98
95
|
}, this.autoDialDiscoveredPeersDebounce)
|
|
99
96
|
})
|
|
@@ -107,7 +104,7 @@ export class AutoDial implements Startable {
|
|
|
107
104
|
this.autoDialInterval = setTimeout(() => {
|
|
108
105
|
this.autoDial()
|
|
109
106
|
.catch(err => {
|
|
110
|
-
this
|
|
107
|
+
this.log.error('error while autodialing', err)
|
|
111
108
|
})
|
|
112
109
|
}, this.autoDialIntervalMs)
|
|
113
110
|
this.started = true
|
|
@@ -116,7 +113,7 @@ export class AutoDial implements Startable {
|
|
|
116
113
|
afterStart (): void {
|
|
117
114
|
this.autoDial()
|
|
118
115
|
.catch(err => {
|
|
119
|
-
this
|
|
116
|
+
this.log.error('error while autodialing', err)
|
|
120
117
|
})
|
|
121
118
|
}
|
|
122
119
|
|
|
@@ -139,24 +136,24 @@ export class AutoDial implements Startable {
|
|
|
139
136
|
// Already has enough connections
|
|
140
137
|
if (numConnections >= this.minConnections) {
|
|
141
138
|
if (this.minConnections > 0) {
|
|
142
|
-
this
|
|
139
|
+
this.log.trace('have enough connections %d/%d', numConnections, this.minConnections)
|
|
143
140
|
}
|
|
144
141
|
return
|
|
145
142
|
}
|
|
146
143
|
|
|
147
144
|
if (this.queue.size > this.autoDialMaxQueueLength) {
|
|
148
|
-
this
|
|
145
|
+
this.log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
|
|
149
146
|
return
|
|
150
147
|
}
|
|
151
148
|
|
|
152
149
|
if (this.running) {
|
|
153
|
-
this
|
|
150
|
+
this.log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
|
|
154
151
|
return
|
|
155
152
|
}
|
|
156
153
|
|
|
157
154
|
this.running = true
|
|
158
155
|
|
|
159
|
-
this
|
|
156
|
+
this.log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
|
|
160
157
|
|
|
161
158
|
const dialQueue = new PeerSet(
|
|
162
159
|
// @ts-expect-error boolean filter removes falsy peer IDs
|
|
@@ -172,25 +169,25 @@ export class AutoDial implements Startable {
|
|
|
172
169
|
(peer) => {
|
|
173
170
|
// Remove peers without addresses
|
|
174
171
|
if (peer.addresses.length === 0) {
|
|
175
|
-
this
|
|
172
|
+
this.log.trace('not autodialing %p because they have no addresses', peer.id)
|
|
176
173
|
return false
|
|
177
174
|
}
|
|
178
175
|
|
|
179
176
|
// remove peers we are already connected to
|
|
180
177
|
if (connections.has(peer.id)) {
|
|
181
|
-
this
|
|
178
|
+
this.log.trace('not autodialing %p because they are already connected', peer.id)
|
|
182
179
|
return false
|
|
183
180
|
}
|
|
184
181
|
|
|
185
182
|
// remove peers we are already dialling
|
|
186
183
|
if (dialQueue.has(peer.id)) {
|
|
187
|
-
this
|
|
184
|
+
this.log.trace('not autodialing %p because they are already being dialed', peer.id)
|
|
188
185
|
return false
|
|
189
186
|
}
|
|
190
187
|
|
|
191
188
|
// remove peers already in the autodial queue
|
|
192
189
|
if (this.queue.hasJob(peer.id)) {
|
|
193
|
-
this
|
|
190
|
+
this.log.trace('not autodialing %p because they are already being autodialed', peer.id)
|
|
194
191
|
return false
|
|
195
192
|
}
|
|
196
193
|
|
|
@@ -249,7 +246,7 @@ export class AutoDial implements Startable {
|
|
|
249
246
|
return Date.now() - lastDialFailureTimestamp > this.autoDialPeerRetryThresholdMs
|
|
250
247
|
})
|
|
251
248
|
|
|
252
|
-
this
|
|
249
|
+
this.log('selected %d/%d peers to dial', peersThatHaveNotFailed.length, peers.length)
|
|
253
250
|
|
|
254
251
|
for (const peer of peersThatHaveNotFailed) {
|
|
255
252
|
this.queue.add(async () => {
|
|
@@ -257,19 +254,19 @@ export class AutoDial implements Startable {
|
|
|
257
254
|
|
|
258
255
|
// Check to see if we still need to auto dial
|
|
259
256
|
if (numConnections >= this.minConnections) {
|
|
260
|
-
this
|
|
257
|
+
this.log('got enough connections now %d/%d', numConnections, this.minConnections)
|
|
261
258
|
this.queue.clear()
|
|
262
259
|
return
|
|
263
260
|
}
|
|
264
261
|
|
|
265
|
-
this
|
|
262
|
+
this.log('connecting to a peerStore stored peer %p', peer.id)
|
|
266
263
|
await this.connectionManager.openConnection(peer.id, {
|
|
267
264
|
priority: this.autoDialPriority
|
|
268
265
|
})
|
|
269
266
|
}, {
|
|
270
267
|
peerId: peer.id
|
|
271
268
|
}).catch(err => {
|
|
272
|
-
this
|
|
269
|
+
this.log.error('could not connect to peerStore stored peer', err)
|
|
273
270
|
})
|
|
274
271
|
}
|
|
275
272
|
|
|
@@ -279,7 +276,7 @@ export class AutoDial implements Startable {
|
|
|
279
276
|
this.autoDialInterval = setTimeout(() => {
|
|
280
277
|
this.autoDial()
|
|
281
278
|
.catch(err => {
|
|
282
|
-
this
|
|
279
|
+
this.log.error('error while autodialing', err)
|
|
283
280
|
})
|
|
284
281
|
}, this.autoDialIntervalMs)
|
|
285
282
|
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
2
2
|
import { MAX_CONNECTIONS } from './constants.js'
|
|
3
|
-
import type { Libp2pEvents, Logger, ComponentLogger } from '@libp2p/interface'
|
|
4
|
-
import type {
|
|
5
|
-
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
6
|
-
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
3
|
+
import type { Libp2pEvents, Logger, ComponentLogger, TypedEventTarget, PeerStore } from '@libp2p/interface'
|
|
4
|
+
import type { ConnectionManager } from '@libp2p/interface-internal'
|
|
7
5
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
8
6
|
|
|
9
7
|
interface ConnectionPrunerInit {
|
|
@@ -32,7 +30,7 @@ export class ConnectionPruner {
|
|
|
32
30
|
private readonly peerStore: PeerStore
|
|
33
31
|
private readonly allow: Multiaddr[]
|
|
34
32
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
35
|
-
readonly
|
|
33
|
+
private readonly log: Logger
|
|
36
34
|
|
|
37
35
|
constructor (components: ConnectionPrunerComponents, init: ConnectionPrunerInit = {}) {
|
|
38
36
|
this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
|
|
@@ -40,13 +38,13 @@ export class ConnectionPruner {
|
|
|
40
38
|
this.connectionManager = components.connectionManager
|
|
41
39
|
this.peerStore = components.peerStore
|
|
42
40
|
this.events = components.events
|
|
43
|
-
this
|
|
41
|
+
this.log = components.logger.forComponent('libp2p:connection-manager:connection-pruner')
|
|
44
42
|
|
|
45
43
|
// check the max connection limit whenever a peer connects
|
|
46
44
|
components.events.addEventListener('connection:open', () => {
|
|
47
45
|
this.maybePruneConnections()
|
|
48
46
|
.catch(err => {
|
|
49
|
-
this
|
|
47
|
+
this.log.error(err)
|
|
50
48
|
})
|
|
51
49
|
})
|
|
52
50
|
}
|
|
@@ -60,12 +58,12 @@ export class ConnectionPruner {
|
|
|
60
58
|
const numConnections = connections.length
|
|
61
59
|
const toPrune = Math.max(numConnections - this.maxConnections, 0)
|
|
62
60
|
|
|
63
|
-
this
|
|
61
|
+
this.log('checking max connections limit %d/%d', numConnections, this.maxConnections)
|
|
64
62
|
if (numConnections <= this.maxConnections) {
|
|
65
63
|
return
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
this
|
|
66
|
+
this.log('max connections limit exceeded %d/%d, pruning %d connection(s)', numConnections, this.maxConnections, toPrune)
|
|
69
67
|
const peerValues = new PeerMap<number>()
|
|
70
68
|
|
|
71
69
|
// work out peer values
|
|
@@ -87,7 +85,7 @@ export class ConnectionPruner {
|
|
|
87
85
|
}, 0))
|
|
88
86
|
} catch (err: any) {
|
|
89
87
|
if (err.code !== 'ERR_NOT_FOUND') {
|
|
90
|
-
this
|
|
88
|
+
this.log.error('error loading peer tags', err)
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
91
|
}
|
|
@@ -124,7 +122,7 @@ export class ConnectionPruner {
|
|
|
124
122
|
const toClose = []
|
|
125
123
|
|
|
126
124
|
for (const connection of sortedConnections) {
|
|
127
|
-
this
|
|
125
|
+
this.log('too many connections open - closing a connection to %p', connection.remotePeer)
|
|
128
126
|
// check allow list
|
|
129
127
|
const connectionInAllowList = this.allow.some((ma) => {
|
|
130
128
|
return connection.remoteAddr.toString().startsWith(ma.toString())
|
|
@@ -146,7 +144,7 @@ export class ConnectionPruner {
|
|
|
146
144
|
try {
|
|
147
145
|
await connection.close()
|
|
148
146
|
} catch (err) {
|
|
149
|
-
this
|
|
147
|
+
this.log.error(err)
|
|
150
148
|
}
|
|
151
149
|
})
|
|
152
150
|
)
|
|
@@ -13,11 +13,6 @@ export const INBOUND_UPGRADE_TIMEOUT = 30e3
|
|
|
13
13
|
*/
|
|
14
14
|
export const MAX_PEER_ADDRS_TO_DIAL = 25
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxParallelDialsPerPeer
|
|
18
|
-
*/
|
|
19
|
-
export const MAX_PARALLEL_DIALS_PER_PEER = 1
|
|
20
|
-
|
|
21
16
|
/**
|
|
22
17
|
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#autoDialInterval
|
|
23
18
|
*/
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { AbortError, CodeError } from '@libp2p/interface
|
|
2
|
-
import { setMaxListeners } from '@libp2p/interface/events'
|
|
1
|
+
import { AbortError, CodeError, ERR_TIMEOUT, setMaxListeners } from '@libp2p/interface'
|
|
3
2
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
4
3
|
import { defaultAddressSort } from '@libp2p/utils/address-sort'
|
|
5
4
|
import { type Multiaddr, type Resolver, resolvers } from '@multiformats/multiaddr'
|
|
@@ -12,19 +11,13 @@ import { codes } from '../errors.js'
|
|
|
12
11
|
import { getPeerAddress } from '../get-peer.js'
|
|
13
12
|
import {
|
|
14
13
|
DIAL_TIMEOUT,
|
|
15
|
-
MAX_PARALLEL_DIALS_PER_PEER,
|
|
16
14
|
MAX_PARALLEL_DIALS,
|
|
17
15
|
MAX_PEER_ADDRS_TO_DIAL,
|
|
18
16
|
LAST_DIAL_FAILURE_KEY
|
|
19
17
|
} from './constants.js'
|
|
20
18
|
import { combineSignals, resolveMultiaddrs } from './utils.js'
|
|
21
|
-
import type { AddressSorter, AbortOptions, PendingDial, ComponentLogger, Logger } from '@libp2p/interface'
|
|
22
|
-
import type {
|
|
23
|
-
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
|
|
24
|
-
import type { Metric, Metrics } from '@libp2p/interface/metrics'
|
|
25
|
-
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
26
|
-
import type { Address, PeerStore } from '@libp2p/interface/peer-store'
|
|
27
|
-
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
19
|
+
import type { AddressSorter, AbortOptions, PendingDial, ComponentLogger, Logger, Connection, ConnectionGater, Metric, Metrics, PeerId, Address, PeerStore } from '@libp2p/interface'
|
|
20
|
+
import type { TransportManager } from '@libp2p/interface-internal'
|
|
28
21
|
|
|
29
22
|
export interface PendingDialTarget {
|
|
30
23
|
resolve(value: any): void
|
|
@@ -44,7 +37,6 @@ interface DialerInit {
|
|
|
44
37
|
addressSorter?: AddressSorter
|
|
45
38
|
maxParallelDials?: number
|
|
46
39
|
maxPeerAddrsToDial?: number
|
|
47
|
-
maxParallelDialsPerPeer?: number
|
|
48
40
|
dialTimeout?: number
|
|
49
41
|
resolvers?: Record<string, Resolver>
|
|
50
42
|
connections?: PeerMap<Connection[]>
|
|
@@ -54,7 +46,6 @@ const defaultOptions = {
|
|
|
54
46
|
addressSorter: defaultAddressSort,
|
|
55
47
|
maxParallelDials: MAX_PARALLEL_DIALS,
|
|
56
48
|
maxPeerAddrsToDial: MAX_PEER_ADDRS_TO_DIAL,
|
|
57
|
-
maxParallelDialsPerPeer: MAX_PARALLEL_DIALS_PER_PEER,
|
|
58
49
|
dialTimeout: DIAL_TIMEOUT,
|
|
59
50
|
resolvers: {
|
|
60
51
|
dnsaddr: dnsaddrResolver
|
|
@@ -79,21 +70,19 @@ export class DialQueue {
|
|
|
79
70
|
private readonly transportManager: TransportManager
|
|
80
71
|
private readonly addressSorter: AddressSorter
|
|
81
72
|
private readonly maxPeerAddrsToDial: number
|
|
82
|
-
private readonly maxParallelDialsPerPeer: number
|
|
83
73
|
private readonly dialTimeout: number
|
|
84
74
|
private readonly inProgressDialCount?: Metric
|
|
85
75
|
private readonly pendingDialCount?: Metric
|
|
86
76
|
private readonly shutDownController: AbortController
|
|
87
77
|
private readonly connections: PeerMap<Connection[]>
|
|
88
|
-
readonly
|
|
78
|
+
private readonly log: Logger
|
|
89
79
|
|
|
90
80
|
constructor (components: DialQueueComponents, init: DialerInit = {}) {
|
|
91
81
|
this.addressSorter = init.addressSorter ?? defaultOptions.addressSorter
|
|
92
82
|
this.maxPeerAddrsToDial = init.maxPeerAddrsToDial ?? defaultOptions.maxPeerAddrsToDial
|
|
93
|
-
this.maxParallelDialsPerPeer = init.maxParallelDialsPerPeer ?? defaultOptions.maxParallelDialsPerPeer
|
|
94
83
|
this.dialTimeout = init.dialTimeout ?? defaultOptions.dialTimeout
|
|
95
84
|
this.connections = init.connections ?? new PeerMap()
|
|
96
|
-
this
|
|
85
|
+
this.log = components.logger.forComponent('libp2p:connection-manager:dial-queue')
|
|
97
86
|
|
|
98
87
|
this.peerId = components.peerId
|
|
99
88
|
this.peerStore = components.peerStore
|
|
@@ -103,8 +92,8 @@ export class DialQueue {
|
|
|
103
92
|
|
|
104
93
|
setMaxListeners(Infinity, this.shutDownController.signal)
|
|
105
94
|
|
|
106
|
-
this.pendingDialCount = components.metrics?.registerMetric('
|
|
107
|
-
this.inProgressDialCount = components.metrics?.registerMetric('
|
|
95
|
+
this.pendingDialCount = components.metrics?.registerMetric('libp2p_dial_queue_pending_dials')
|
|
96
|
+
this.inProgressDialCount = components.metrics?.registerMetric('libp2p_dial_queue_in_progress_dials')
|
|
108
97
|
this.pendingDials = []
|
|
109
98
|
|
|
110
99
|
for (const [key, value] of Object.entries(init.resolvers ?? {})) {
|
|
@@ -133,7 +122,7 @@ export class DialQueue {
|
|
|
133
122
|
})
|
|
134
123
|
// a started job errored
|
|
135
124
|
this.queue.on('error', (err) => {
|
|
136
|
-
this
|
|
125
|
+
this.log.error('error in dial queue', err)
|
|
137
126
|
this.pendingDialCount?.update(this.queue.size)
|
|
138
127
|
this.inProgressDialCount?.update(this.queue.pending)
|
|
139
128
|
})
|
|
@@ -205,7 +194,7 @@ export class DialQueue {
|
|
|
205
194
|
})
|
|
206
195
|
|
|
207
196
|
if (existingConnection != null) {
|
|
208
|
-
this
|
|
197
|
+
this.log('already connected to %a', existingConnection.remoteAddr)
|
|
209
198
|
return existingConnection
|
|
210
199
|
}
|
|
211
200
|
|
|
@@ -226,12 +215,12 @@ export class DialQueue {
|
|
|
226
215
|
})
|
|
227
216
|
|
|
228
217
|
if (existingDial != null) {
|
|
229
|
-
this
|
|
218
|
+
this.log('joining existing dial target for %p', peerId)
|
|
230
219
|
signal.clear()
|
|
231
220
|
return existingDial.promise
|
|
232
221
|
}
|
|
233
222
|
|
|
234
|
-
this
|
|
223
|
+
this.log('creating dial target for', addrsToDial.map(({ multiaddr }) => multiaddr.toString()))
|
|
235
224
|
// @ts-expect-error .promise property is set below
|
|
236
225
|
const pendingDial: PendingDialInternal = {
|
|
237
226
|
id: randomId(),
|
|
@@ -252,7 +241,7 @@ export class DialQueue {
|
|
|
252
241
|
signal.clear()
|
|
253
242
|
})
|
|
254
243
|
.catch(async err => {
|
|
255
|
-
this
|
|
244
|
+
this.log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err)
|
|
256
245
|
|
|
257
246
|
if (peerId != null) {
|
|
258
247
|
// record the last failed dial
|
|
@@ -263,13 +252,13 @@ export class DialQueue {
|
|
|
263
252
|
}
|
|
264
253
|
})
|
|
265
254
|
} catch (err: any) {
|
|
266
|
-
this
|
|
255
|
+
this.log.error('could not update last dial failure key for %p', peerId, err)
|
|
267
256
|
}
|
|
268
257
|
}
|
|
269
258
|
|
|
270
259
|
// Error is a timeout
|
|
271
260
|
if (signal.aborted) {
|
|
272
|
-
const error = new CodeError(err.message,
|
|
261
|
+
const error = new CodeError(err.message, ERR_TIMEOUT)
|
|
273
262
|
throw error
|
|
274
263
|
}
|
|
275
264
|
|
|
@@ -294,12 +283,12 @@ export class DialQueue {
|
|
|
294
283
|
})
|
|
295
284
|
|
|
296
285
|
if (existingConnection != null) {
|
|
297
|
-
this
|
|
286
|
+
this.log('already connected to %a', existingConnection.remoteAddr)
|
|
298
287
|
await connection.close()
|
|
299
288
|
return existingConnection
|
|
300
289
|
}
|
|
301
290
|
|
|
302
|
-
this
|
|
291
|
+
this.log('connection opened to %a', connection.remoteAddr)
|
|
303
292
|
return connection
|
|
304
293
|
}
|
|
305
294
|
|
|
@@ -334,11 +323,11 @@ export class DialQueue {
|
|
|
334
323
|
|
|
335
324
|
// if just a peer id was passed, load available multiaddrs for this peer from the address book
|
|
336
325
|
if (addrs.length === 0) {
|
|
337
|
-
this
|
|
326
|
+
this.log('loading multiaddrs for %p', peerId)
|
|
338
327
|
try {
|
|
339
328
|
const peer = await this.peerStore.get(peerId)
|
|
340
329
|
addrs.push(...peer.addresses)
|
|
341
|
-
this
|
|
330
|
+
this.log('loaded multiaddrs for %p', peerId, addrs.map(({ multiaddr }) => multiaddr.toString()))
|
|
342
331
|
} catch (err: any) {
|
|
343
332
|
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
344
333
|
throw err
|
|
@@ -352,7 +341,7 @@ export class DialQueue {
|
|
|
352
341
|
addrs.map(async addr => {
|
|
353
342
|
const result = await resolveMultiaddrs(addr.multiaddr, {
|
|
354
343
|
...options,
|
|
355
|
-
log: this
|
|
344
|
+
log: this.log
|
|
356
345
|
})
|
|
357
346
|
|
|
358
347
|
if (result.length === 1 && result[0].equals(addr.multiaddr)) {
|
|
@@ -425,8 +414,8 @@ export class DialQueue {
|
|
|
425
414
|
const dedupedMultiaddrs = [...dedupedAddrs.values()]
|
|
426
415
|
|
|
427
416
|
if (dedupedMultiaddrs.length === 0 || dedupedMultiaddrs.length > this.maxPeerAddrsToDial) {
|
|
428
|
-
this
|
|
429
|
-
this
|
|
417
|
+
this.log('addresses for %p before filtering', peerId ?? 'unknown peer', resolvedAddresses.map(({ multiaddr }) => multiaddr.toString()))
|
|
418
|
+
this.log('addresses for %p after filtering', peerId ?? 'unknown peer', dedupedMultiaddrs.map(({ multiaddr }) => multiaddr.toString()))
|
|
430
419
|
}
|
|
431
420
|
|
|
432
421
|
// make sure we actually have some addresses to dial
|
|
@@ -463,14 +452,10 @@ export class DialQueue {
|
|
|
463
452
|
const dialAbortControllers: Array<(AbortController | undefined)> = pendingDial.multiaddrs.map(() => new AbortController())
|
|
464
453
|
|
|
465
454
|
try {
|
|
466
|
-
// internal peer dial queue
|
|
467
|
-
|
|
468
|
-
// the dial queue
|
|
469
|
-
const peerDialQueue = new PQueue({
|
|
470
|
-
concurrency: this.maxParallelDialsPerPeer
|
|
471
|
-
})
|
|
455
|
+
// internal peer dial queue - only one dial per peer at a time
|
|
456
|
+
const peerDialQueue = new PQueue({ concurrency: 1 })
|
|
472
457
|
peerDialQueue.on('error', (err) => {
|
|
473
|
-
this
|
|
458
|
+
this.log.error('error dialing %s %o', pendingDial.multiaddrs, err)
|
|
474
459
|
})
|
|
475
460
|
|
|
476
461
|
const conn = await Promise.any(pendingDial.multiaddrs.map(async (addr, i) => {
|
|
@@ -483,13 +468,13 @@ export class DialQueue {
|
|
|
483
468
|
// let any signal abort the dial
|
|
484
469
|
const signal = combineSignals(controller.signal, options.signal)
|
|
485
470
|
signal.addEventListener('abort', () => {
|
|
486
|
-
this
|
|
471
|
+
this.log('dial to %a aborted', addr)
|
|
487
472
|
})
|
|
488
473
|
const deferred = pDefer<Connection>()
|
|
489
474
|
|
|
490
475
|
await peerDialQueue.add(async () => {
|
|
491
476
|
if (signal.aborted) {
|
|
492
|
-
this
|
|
477
|
+
this.log('dial to %a was aborted before reaching the head of the peer dial queue', addr)
|
|
493
478
|
deferred.reject(new AbortError())
|
|
494
479
|
return
|
|
495
480
|
}
|
|
@@ -498,7 +483,7 @@ export class DialQueue {
|
|
|
498
483
|
await this.queue.add(async () => {
|
|
499
484
|
try {
|
|
500
485
|
if (signal.aborted) {
|
|
501
|
-
this
|
|
486
|
+
this.log('dial to %a was aborted before reaching the head of the dial queue', addr)
|
|
502
487
|
deferred.reject(new AbortError())
|
|
503
488
|
return
|
|
504
489
|
}
|
|
@@ -513,10 +498,10 @@ export class DialQueue {
|
|
|
513
498
|
|
|
514
499
|
if (controller.signal.aborted) {
|
|
515
500
|
// another dial succeeded faster than this one
|
|
516
|
-
this
|
|
501
|
+
this.log('multiple dials succeeded, closing superfluous connection')
|
|
517
502
|
|
|
518
503
|
conn.close().catch(err => {
|
|
519
|
-
this
|
|
504
|
+
this.log.error('error closing superfluous connection', err)
|
|
520
505
|
})
|
|
521
506
|
|
|
522
507
|
deferred.reject(new AbortError())
|
|
@@ -533,13 +518,13 @@ export class DialQueue {
|
|
|
533
518
|
}
|
|
534
519
|
})
|
|
535
520
|
|
|
536
|
-
this
|
|
521
|
+
this.log('dial to %a succeeded', addr)
|
|
537
522
|
|
|
538
523
|
// resolve the connection promise
|
|
539
524
|
deferred.resolve(conn)
|
|
540
525
|
} catch (err: any) {
|
|
541
526
|
// something only went wrong if our signal was not aborted
|
|
542
|
-
this
|
|
527
|
+
this.log.error('error during dial of %a', addr, err)
|
|
543
528
|
deferred.reject(err)
|
|
544
529
|
}
|
|
545
530
|
}, {
|