libp2p 0.46.19-fb8a6f188 → 0.46.19
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 +22 -22
- package/dist/src/components.d.ts +1 -3
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +0 -4
- package/dist/src/components.js.map +1 -1
- package/dist/src/connection/index.d.ts +1 -3
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +9 -9
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +1 -3
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +20 -20
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +1 -3
- 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/dial-queue.d.ts +1 -3
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +23 -26
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +1 -3
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +18 -21
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +1 -2
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +4 -2
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/identify/consts.d.ts +1 -1
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/identify.d.ts.map +1 -1
- package/dist/src/identify/identify.js +20 -20
- package/dist/src/identify/identify.js.map +1 -1
- package/dist/src/identify/index.d.ts +1 -2
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -21
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +16 -20
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/upgrader.d.ts +1 -3
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +29 -30
- 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 +71 -0
- package/package.json +21 -21
- package/src/components.ts +1 -8
- package/src/connection/index.ts +11 -12
- package/src/connection-manager/auto-dial.ts +22 -22
- package/src/connection-manager/connection-pruner.ts +10 -10
- package/src/connection-manager/dial-queue.ts +25 -28
- package/src/connection-manager/index.ts +20 -23
- package/src/connection-manager/utils.ts +7 -5
- package/src/identify/identify.ts +22 -21
- package/src/identify/index.ts +1 -2
- package/src/index.ts +1 -22
- package/src/libp2p.ts +18 -21
- package/src/upgrader.ts +31 -32
- package/src/version.ts +1 -1
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
1
2
|
import { PeerMap, PeerSet } from '@libp2p/peer-collections'
|
|
2
3
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
3
4
|
import { PeerJobQueue } from '../utils/peer-job-queue.js'
|
|
4
5
|
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
|
|
6
|
+
import type { Libp2pEvents } from '@libp2p/interface'
|
|
6
7
|
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
7
8
|
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
8
9
|
import type { Startable } from '@libp2p/interface/startable'
|
|
9
10
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
10
11
|
|
|
12
|
+
const log = logger('libp2p:connection-manager:auto-dial')
|
|
13
|
+
|
|
11
14
|
interface AutoDialInit {
|
|
12
15
|
minConnections?: number
|
|
13
16
|
maxQueueLength?: number
|
|
@@ -22,7 +25,6 @@ interface AutoDialComponents {
|
|
|
22
25
|
connectionManager: ConnectionManager
|
|
23
26
|
peerStore: PeerStore
|
|
24
27
|
events: TypedEventTarget<Libp2pEvents>
|
|
25
|
-
logger: ComponentLogger
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
const defaultOptions = {
|
|
@@ -48,7 +50,6 @@ export class AutoDial implements Startable {
|
|
|
48
50
|
private autoDialInterval?: ReturnType<typeof setInterval>
|
|
49
51
|
private started: boolean
|
|
50
52
|
private running: boolean
|
|
51
|
-
readonly #log: Logger
|
|
52
53
|
|
|
53
54
|
/**
|
|
54
55
|
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
@@ -64,21 +65,20 @@ export class AutoDial implements Startable {
|
|
|
64
65
|
this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
|
|
65
66
|
this.autoDialPeerRetryThresholdMs = init.autoDialPeerRetryThreshold ?? defaultOptions.autoDialPeerRetryThreshold
|
|
66
67
|
this.autoDialDiscoveredPeersDebounce = init.autoDialDiscoveredPeersDebounce ?? defaultOptions.autoDialDiscoveredPeersDebounce
|
|
67
|
-
this.#log = components.logger.forComponent('libp2p:connection-manager:auto-dial')
|
|
68
68
|
this.started = false
|
|
69
69
|
this.running = false
|
|
70
70
|
this.queue = new PeerJobQueue({
|
|
71
71
|
concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
|
|
72
72
|
})
|
|
73
73
|
this.queue.addListener('error', (err) => {
|
|
74
|
-
|
|
74
|
+
log.error('error during auto-dial', err)
|
|
75
75
|
})
|
|
76
76
|
|
|
77
77
|
// check the min connection limit whenever a peer disconnects
|
|
78
78
|
components.events.addEventListener('connection:close', () => {
|
|
79
79
|
this.autoDial()
|
|
80
80
|
.catch(err => {
|
|
81
|
-
|
|
81
|
+
log.error(err)
|
|
82
82
|
})
|
|
83
83
|
})
|
|
84
84
|
|
|
@@ -93,7 +93,7 @@ export class AutoDial implements Startable {
|
|
|
93
93
|
debounce = setTimeout(() => {
|
|
94
94
|
this.autoDial()
|
|
95
95
|
.catch(err => {
|
|
96
|
-
|
|
96
|
+
log.error(err)
|
|
97
97
|
})
|
|
98
98
|
}, this.autoDialDiscoveredPeersDebounce)
|
|
99
99
|
})
|
|
@@ -107,7 +107,7 @@ export class AutoDial implements Startable {
|
|
|
107
107
|
this.autoDialInterval = setTimeout(() => {
|
|
108
108
|
this.autoDial()
|
|
109
109
|
.catch(err => {
|
|
110
|
-
|
|
110
|
+
log.error('error while autodialing', err)
|
|
111
111
|
})
|
|
112
112
|
}, this.autoDialIntervalMs)
|
|
113
113
|
this.started = true
|
|
@@ -116,7 +116,7 @@ export class AutoDial implements Startable {
|
|
|
116
116
|
afterStart (): void {
|
|
117
117
|
this.autoDial()
|
|
118
118
|
.catch(err => {
|
|
119
|
-
|
|
119
|
+
log.error('error while autodialing', err)
|
|
120
120
|
})
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -139,24 +139,24 @@ export class AutoDial implements Startable {
|
|
|
139
139
|
// Already has enough connections
|
|
140
140
|
if (numConnections >= this.minConnections) {
|
|
141
141
|
if (this.minConnections > 0) {
|
|
142
|
-
|
|
142
|
+
log.trace('have enough connections %d/%d', numConnections, this.minConnections)
|
|
143
143
|
}
|
|
144
144
|
return
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
if (this.queue.size > this.autoDialMaxQueueLength) {
|
|
148
|
-
|
|
148
|
+
log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
|
|
149
149
|
return
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
if (this.running) {
|
|
153
|
-
|
|
153
|
+
log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
|
|
154
154
|
return
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
this.running = true
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
|
|
160
160
|
|
|
161
161
|
const dialQueue = new PeerSet(
|
|
162
162
|
// @ts-expect-error boolean filter removes falsy peer IDs
|
|
@@ -172,25 +172,25 @@ export class AutoDial implements Startable {
|
|
|
172
172
|
(peer) => {
|
|
173
173
|
// Remove peers without addresses
|
|
174
174
|
if (peer.addresses.length === 0) {
|
|
175
|
-
|
|
175
|
+
log.trace('not autodialing %p because they have no addresses', peer.id)
|
|
176
176
|
return false
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
// remove peers we are already connected to
|
|
180
180
|
if (connections.has(peer.id)) {
|
|
181
|
-
|
|
181
|
+
log.trace('not autodialing %p because they are already connected', peer.id)
|
|
182
182
|
return false
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
// remove peers we are already dialling
|
|
186
186
|
if (dialQueue.has(peer.id)) {
|
|
187
|
-
|
|
187
|
+
log.trace('not autodialing %p because they are already being dialed', peer.id)
|
|
188
188
|
return false
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
// remove peers already in the autodial queue
|
|
192
192
|
if (this.queue.hasJob(peer.id)) {
|
|
193
|
-
|
|
193
|
+
log.trace('not autodialing %p because they are already being autodialed', peer.id)
|
|
194
194
|
return false
|
|
195
195
|
}
|
|
196
196
|
|
|
@@ -249,7 +249,7 @@ export class AutoDial implements Startable {
|
|
|
249
249
|
return Date.now() - lastDialFailureTimestamp > this.autoDialPeerRetryThresholdMs
|
|
250
250
|
})
|
|
251
251
|
|
|
252
|
-
|
|
252
|
+
log('selected %d/%d peers to dial', peersThatHaveNotFailed.length, peers.length)
|
|
253
253
|
|
|
254
254
|
for (const peer of peersThatHaveNotFailed) {
|
|
255
255
|
this.queue.add(async () => {
|
|
@@ -257,19 +257,19 @@ export class AutoDial implements Startable {
|
|
|
257
257
|
|
|
258
258
|
// Check to see if we still need to auto dial
|
|
259
259
|
if (numConnections >= this.minConnections) {
|
|
260
|
-
|
|
260
|
+
log('got enough connections now %d/%d', numConnections, this.minConnections)
|
|
261
261
|
this.queue.clear()
|
|
262
262
|
return
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
|
|
265
|
+
log('connecting to a peerStore stored peer %p', peer.id)
|
|
266
266
|
await this.connectionManager.openConnection(peer.id, {
|
|
267
267
|
priority: this.autoDialPriority
|
|
268
268
|
})
|
|
269
269
|
}, {
|
|
270
270
|
peerId: peer.id
|
|
271
271
|
}).catch(err => {
|
|
272
|
-
|
|
272
|
+
log.error('could not connect to peerStore stored peer', err)
|
|
273
273
|
})
|
|
274
274
|
}
|
|
275
275
|
|
|
@@ -279,7 +279,7 @@ export class AutoDial implements Startable {
|
|
|
279
279
|
this.autoDialInterval = setTimeout(() => {
|
|
280
280
|
this.autoDial()
|
|
281
281
|
.catch(err => {
|
|
282
|
-
|
|
282
|
+
log.error('error while autodialing', err)
|
|
283
283
|
})
|
|
284
284
|
}, this.autoDialIntervalMs)
|
|
285
285
|
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
1
2
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
2
3
|
import { MAX_CONNECTIONS } from './constants.js'
|
|
3
|
-
import type { Libp2pEvents
|
|
4
|
+
import type { Libp2pEvents } from '@libp2p/interface'
|
|
4
5
|
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
5
6
|
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
6
7
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
7
8
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
8
9
|
|
|
10
|
+
const log = logger('libp2p:connection-manager:connection-pruner')
|
|
11
|
+
|
|
9
12
|
interface ConnectionPrunerInit {
|
|
10
13
|
maxConnections?: number
|
|
11
14
|
allow?: Multiaddr[]
|
|
@@ -15,7 +18,6 @@ interface ConnectionPrunerComponents {
|
|
|
15
18
|
connectionManager: ConnectionManager
|
|
16
19
|
peerStore: PeerStore
|
|
17
20
|
events: TypedEventTarget<Libp2pEvents>
|
|
18
|
-
logger: ComponentLogger
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
const defaultOptions = {
|
|
@@ -32,7 +34,6 @@ export class ConnectionPruner {
|
|
|
32
34
|
private readonly peerStore: PeerStore
|
|
33
35
|
private readonly allow: Multiaddr[]
|
|
34
36
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
35
|
-
readonly #log: Logger
|
|
36
37
|
|
|
37
38
|
constructor (components: ConnectionPrunerComponents, init: ConnectionPrunerInit = {}) {
|
|
38
39
|
this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
|
|
@@ -40,13 +41,12 @@ export class ConnectionPruner {
|
|
|
40
41
|
this.connectionManager = components.connectionManager
|
|
41
42
|
this.peerStore = components.peerStore
|
|
42
43
|
this.events = components.events
|
|
43
|
-
this.#log = components.logger.forComponent('libp2p:connection-manager:connection-pruner')
|
|
44
44
|
|
|
45
45
|
// check the max connection limit whenever a peer connects
|
|
46
46
|
components.events.addEventListener('connection:open', () => {
|
|
47
47
|
this.maybePruneConnections()
|
|
48
48
|
.catch(err => {
|
|
49
|
-
|
|
49
|
+
log.error(err)
|
|
50
50
|
})
|
|
51
51
|
})
|
|
52
52
|
}
|
|
@@ -60,12 +60,12 @@ export class ConnectionPruner {
|
|
|
60
60
|
const numConnections = connections.length
|
|
61
61
|
const toPrune = Math.max(numConnections - this.maxConnections, 0)
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
log('checking max connections limit %d/%d', numConnections, this.maxConnections)
|
|
64
64
|
if (numConnections <= this.maxConnections) {
|
|
65
65
|
return
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
log('max connections limit exceeded %d/%d, pruning %d connection(s)', numConnections, this.maxConnections, toPrune)
|
|
69
69
|
const peerValues = new PeerMap<number>()
|
|
70
70
|
|
|
71
71
|
// work out peer values
|
|
@@ -87,7 +87,7 @@ export class ConnectionPruner {
|
|
|
87
87
|
}, 0))
|
|
88
88
|
} catch (err: any) {
|
|
89
89
|
if (err.code !== 'ERR_NOT_FOUND') {
|
|
90
|
-
|
|
90
|
+
log.error('error loading peer tags', err)
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
@@ -124,7 +124,7 @@ export class ConnectionPruner {
|
|
|
124
124
|
const toClose = []
|
|
125
125
|
|
|
126
126
|
for (const connection of sortedConnections) {
|
|
127
|
-
|
|
127
|
+
log('too many connections open - closing a connection to %p', connection.remotePeer)
|
|
128
128
|
// check allow list
|
|
129
129
|
const connectionInAllowList = this.allow.some((ma) => {
|
|
130
130
|
return connection.remoteAddr.toString().startsWith(ma.toString())
|
|
@@ -146,7 +146,7 @@ export class ConnectionPruner {
|
|
|
146
146
|
try {
|
|
147
147
|
await connection.close()
|
|
148
148
|
} catch (err) {
|
|
149
|
-
|
|
149
|
+
log.error(err)
|
|
150
150
|
}
|
|
151
151
|
})
|
|
152
152
|
)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbortError, CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { setMaxListeners } from '@libp2p/interface/events'
|
|
3
|
+
import { logger } from '@libp2p/logger'
|
|
3
4
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
4
5
|
import { defaultAddressSort } from '@libp2p/utils/address-sort'
|
|
5
6
|
import { type Multiaddr, type Resolver, resolvers } from '@multiformats/multiaddr'
|
|
@@ -18,7 +19,7 @@ import {
|
|
|
18
19
|
LAST_DIAL_FAILURE_KEY
|
|
19
20
|
} from './constants.js'
|
|
20
21
|
import { combineSignals, resolveMultiaddrs } from './utils.js'
|
|
21
|
-
import type { AddressSorter, AbortOptions, PendingDial
|
|
22
|
+
import type { AddressSorter, AbortOptions, PendingDial } from '@libp2p/interface'
|
|
22
23
|
import type { Connection } from '@libp2p/interface/connection'
|
|
23
24
|
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
|
|
24
25
|
import type { Metric, Metrics } from '@libp2p/interface/metrics'
|
|
@@ -26,6 +27,8 @@ import type { PeerId } from '@libp2p/interface/peer-id'
|
|
|
26
27
|
import type { Address, PeerStore } from '@libp2p/interface/peer-store'
|
|
27
28
|
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
28
29
|
|
|
30
|
+
const log = logger('libp2p:connection-manager:dial-queue')
|
|
31
|
+
|
|
29
32
|
export interface PendingDialTarget {
|
|
30
33
|
resolve(value: any): void
|
|
31
34
|
reject(err: Error): void
|
|
@@ -67,7 +70,6 @@ interface DialQueueComponents {
|
|
|
67
70
|
peerStore: PeerStore
|
|
68
71
|
transportManager: TransportManager
|
|
69
72
|
connectionGater: ConnectionGater
|
|
70
|
-
logger: ComponentLogger
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
export class DialQueue {
|
|
@@ -85,7 +87,6 @@ export class DialQueue {
|
|
|
85
87
|
private readonly pendingDialCount?: Metric
|
|
86
88
|
private readonly shutDownController: AbortController
|
|
87
89
|
private readonly connections: PeerMap<Connection[]>
|
|
88
|
-
readonly #log: Logger
|
|
89
90
|
|
|
90
91
|
constructor (components: DialQueueComponents, init: DialerInit = {}) {
|
|
91
92
|
this.addressSorter = init.addressSorter ?? defaultOptions.addressSorter
|
|
@@ -93,7 +94,6 @@ export class DialQueue {
|
|
|
93
94
|
this.maxParallelDialsPerPeer = init.maxParallelDialsPerPeer ?? defaultOptions.maxParallelDialsPerPeer
|
|
94
95
|
this.dialTimeout = init.dialTimeout ?? defaultOptions.dialTimeout
|
|
95
96
|
this.connections = init.connections ?? new PeerMap()
|
|
96
|
-
this.#log = components.logger.forComponent('libp2p:connection-manager:dial-queue')
|
|
97
97
|
|
|
98
98
|
this.peerId = components.peerId
|
|
99
99
|
this.peerStore = components.peerStore
|
|
@@ -133,7 +133,7 @@ export class DialQueue {
|
|
|
133
133
|
})
|
|
134
134
|
// a started job errored
|
|
135
135
|
this.queue.on('error', (err) => {
|
|
136
|
-
|
|
136
|
+
log.error('error in dial queue', err)
|
|
137
137
|
this.pendingDialCount?.update(this.queue.size)
|
|
138
138
|
this.inProgressDialCount?.update(this.queue.pending)
|
|
139
139
|
})
|
|
@@ -205,7 +205,7 @@ export class DialQueue {
|
|
|
205
205
|
})
|
|
206
206
|
|
|
207
207
|
if (existingConnection != null) {
|
|
208
|
-
|
|
208
|
+
log('already connected to %a', existingConnection.remoteAddr)
|
|
209
209
|
return existingConnection
|
|
210
210
|
}
|
|
211
211
|
|
|
@@ -226,12 +226,12 @@ export class DialQueue {
|
|
|
226
226
|
})
|
|
227
227
|
|
|
228
228
|
if (existingDial != null) {
|
|
229
|
-
|
|
229
|
+
log('joining existing dial target for %p', peerId)
|
|
230
230
|
signal.clear()
|
|
231
231
|
return existingDial.promise
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
-
|
|
234
|
+
log('creating dial target for', addrsToDial.map(({ multiaddr }) => multiaddr.toString()))
|
|
235
235
|
// @ts-expect-error .promise property is set below
|
|
236
236
|
const pendingDial: PendingDialInternal = {
|
|
237
237
|
id: randomId(),
|
|
@@ -252,7 +252,7 @@ export class DialQueue {
|
|
|
252
252
|
signal.clear()
|
|
253
253
|
})
|
|
254
254
|
.catch(async err => {
|
|
255
|
-
|
|
255
|
+
log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err)
|
|
256
256
|
|
|
257
257
|
if (peerId != null) {
|
|
258
258
|
// record the last failed dial
|
|
@@ -263,7 +263,7 @@ export class DialQueue {
|
|
|
263
263
|
}
|
|
264
264
|
})
|
|
265
265
|
} catch (err: any) {
|
|
266
|
-
|
|
266
|
+
log.error('could not update last dial failure key for %p', peerId, err)
|
|
267
267
|
}
|
|
268
268
|
}
|
|
269
269
|
|
|
@@ -294,12 +294,12 @@ export class DialQueue {
|
|
|
294
294
|
})
|
|
295
295
|
|
|
296
296
|
if (existingConnection != null) {
|
|
297
|
-
|
|
297
|
+
log('already connected to %a', existingConnection.remoteAddr)
|
|
298
298
|
await connection.close()
|
|
299
299
|
return existingConnection
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
-
|
|
302
|
+
log('connection opened to %a', connection.remoteAddr)
|
|
303
303
|
return connection
|
|
304
304
|
}
|
|
305
305
|
|
|
@@ -334,11 +334,11 @@ export class DialQueue {
|
|
|
334
334
|
|
|
335
335
|
// if just a peer id was passed, load available multiaddrs for this peer from the address book
|
|
336
336
|
if (addrs.length === 0) {
|
|
337
|
-
|
|
337
|
+
log('loading multiaddrs for %p', peerId)
|
|
338
338
|
try {
|
|
339
339
|
const peer = await this.peerStore.get(peerId)
|
|
340
340
|
addrs.push(...peer.addresses)
|
|
341
|
-
|
|
341
|
+
log('loaded multiaddrs for %p', peerId, addrs.map(({ multiaddr }) => multiaddr.toString()))
|
|
342
342
|
} catch (err: any) {
|
|
343
343
|
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
344
344
|
throw err
|
|
@@ -350,10 +350,7 @@ export class DialQueue {
|
|
|
350
350
|
// resolve addresses - this can result in a one-to-many translation when dnsaddrs are resolved
|
|
351
351
|
let resolvedAddresses = (await Promise.all(
|
|
352
352
|
addrs.map(async addr => {
|
|
353
|
-
const result = await resolveMultiaddrs(addr.multiaddr,
|
|
354
|
-
...options,
|
|
355
|
-
log: this.#log
|
|
356
|
-
})
|
|
353
|
+
const result = await resolveMultiaddrs(addr.multiaddr, options)
|
|
357
354
|
|
|
358
355
|
if (result.length === 1 && result[0].equals(addr.multiaddr)) {
|
|
359
356
|
return addr
|
|
@@ -425,8 +422,8 @@ export class DialQueue {
|
|
|
425
422
|
const dedupedMultiaddrs = [...dedupedAddrs.values()]
|
|
426
423
|
|
|
427
424
|
if (dedupedMultiaddrs.length === 0 || dedupedMultiaddrs.length > this.maxPeerAddrsToDial) {
|
|
428
|
-
|
|
429
|
-
|
|
425
|
+
log('addresses for %p before filtering', peerId ?? 'unknown peer', resolvedAddresses.map(({ multiaddr }) => multiaddr.toString()))
|
|
426
|
+
log('addresses for %p after filtering', peerId ?? 'unknown peer', dedupedMultiaddrs.map(({ multiaddr }) => multiaddr.toString()))
|
|
430
427
|
}
|
|
431
428
|
|
|
432
429
|
// make sure we actually have some addresses to dial
|
|
@@ -470,7 +467,7 @@ export class DialQueue {
|
|
|
470
467
|
concurrency: this.maxParallelDialsPerPeer
|
|
471
468
|
})
|
|
472
469
|
peerDialQueue.on('error', (err) => {
|
|
473
|
-
|
|
470
|
+
log.error('error dialling', err)
|
|
474
471
|
})
|
|
475
472
|
|
|
476
473
|
const conn = await Promise.any(pendingDial.multiaddrs.map(async (addr, i) => {
|
|
@@ -483,13 +480,13 @@ export class DialQueue {
|
|
|
483
480
|
// let any signal abort the dial
|
|
484
481
|
const signal = combineSignals(controller.signal, options.signal)
|
|
485
482
|
signal.addEventListener('abort', () => {
|
|
486
|
-
|
|
483
|
+
log('dial to %a aborted', addr)
|
|
487
484
|
})
|
|
488
485
|
const deferred = pDefer<Connection>()
|
|
489
486
|
|
|
490
487
|
await peerDialQueue.add(async () => {
|
|
491
488
|
if (signal.aborted) {
|
|
492
|
-
|
|
489
|
+
log('dial to %a was aborted before reaching the head of the peer dial queue', addr)
|
|
493
490
|
deferred.reject(new AbortError())
|
|
494
491
|
return
|
|
495
492
|
}
|
|
@@ -498,7 +495,7 @@ export class DialQueue {
|
|
|
498
495
|
await this.queue.add(async () => {
|
|
499
496
|
try {
|
|
500
497
|
if (signal.aborted) {
|
|
501
|
-
|
|
498
|
+
log('dial to %a was aborted before reaching the head of the dial queue', addr)
|
|
502
499
|
deferred.reject(new AbortError())
|
|
503
500
|
return
|
|
504
501
|
}
|
|
@@ -513,10 +510,10 @@ export class DialQueue {
|
|
|
513
510
|
|
|
514
511
|
if (controller.signal.aborted) {
|
|
515
512
|
// another dial succeeded faster than this one
|
|
516
|
-
|
|
513
|
+
log('multiple dials succeeded, closing superfluous connection')
|
|
517
514
|
|
|
518
515
|
conn.close().catch(err => {
|
|
519
|
-
|
|
516
|
+
log.error('error closing superfluous connection', err)
|
|
520
517
|
})
|
|
521
518
|
|
|
522
519
|
deferred.reject(new AbortError())
|
|
@@ -533,13 +530,13 @@ export class DialQueue {
|
|
|
533
530
|
}
|
|
534
531
|
})
|
|
535
532
|
|
|
536
|
-
|
|
533
|
+
log('dial to %a succeeded', addr)
|
|
537
534
|
|
|
538
535
|
// resolve the connection promise
|
|
539
536
|
deferred.resolve(conn)
|
|
540
537
|
} catch (err: any) {
|
|
541
538
|
// something only went wrong if our signal was not aborted
|
|
542
|
-
|
|
539
|
+
log.error('error during dial of %a', addr, err)
|
|
543
540
|
deferred.reject(err)
|
|
544
541
|
}
|
|
545
542
|
}, {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { KEEP_ALIVE } from '@libp2p/interface/peer-store/tags'
|
|
3
|
+
import { logger } from '@libp2p/logger'
|
|
3
4
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
4
5
|
import { defaultAddressSort } from '@libp2p/utils/address-sort'
|
|
5
6
|
import { type Multiaddr, type Resolver, multiaddr } from '@multiformats/multiaddr'
|
|
@@ -11,7 +12,7 @@ import { AutoDial } from './auto-dial.js'
|
|
|
11
12
|
import { ConnectionPruner } from './connection-pruner.js'
|
|
12
13
|
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PARALLEL_DIALS_PER_PEER, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
|
|
13
14
|
import { DialQueue } from './dial-queue.js'
|
|
14
|
-
import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions
|
|
15
|
+
import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions } from '@libp2p/interface'
|
|
15
16
|
import type { Connection, MultiaddrConnection } from '@libp2p/interface/connection'
|
|
16
17
|
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
|
|
17
18
|
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
@@ -22,6 +23,8 @@ import type { Startable } from '@libp2p/interface/startable'
|
|
|
22
23
|
import type { ConnectionManager, OpenConnectionOptions } from '@libp2p/interface-internal/connection-manager'
|
|
23
24
|
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
24
25
|
|
|
26
|
+
const log = logger('libp2p:connection-manager')
|
|
27
|
+
|
|
25
28
|
const DEFAULT_DIAL_PRIORITY = 50
|
|
26
29
|
|
|
27
30
|
export interface ConnectionManagerInit {
|
|
@@ -163,7 +166,6 @@ export interface DefaultConnectionManagerComponents {
|
|
|
163
166
|
transportManager: TransportManager
|
|
164
167
|
connectionGater: ConnectionGater
|
|
165
168
|
events: TypedEventTarget<Libp2pEvents>
|
|
166
|
-
logger: ComponentLogger
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
/**
|
|
@@ -186,7 +188,6 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
186
188
|
private readonly peerStore: PeerStore
|
|
187
189
|
private readonly metrics?: Metrics
|
|
188
190
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
189
|
-
readonly #log: Logger
|
|
190
191
|
|
|
191
192
|
constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit = {}) {
|
|
192
193
|
this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
|
|
@@ -205,7 +206,6 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
205
206
|
this.peerStore = components.peerStore
|
|
206
207
|
this.metrics = components.metrics
|
|
207
208
|
this.events = components.events
|
|
208
|
-
this.#log = components.logger.forComponent('libp2p:connection-manager')
|
|
209
209
|
|
|
210
210
|
this.onConnect = this.onConnect.bind(this)
|
|
211
211
|
this.onDisconnect = this.onDisconnect.bind(this)
|
|
@@ -229,8 +229,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
229
229
|
this.autoDial = new AutoDial({
|
|
230
230
|
connectionManager: this,
|
|
231
231
|
peerStore: components.peerStore,
|
|
232
|
-
events: components.events
|
|
233
|
-
logger: components.logger
|
|
232
|
+
events: components.events
|
|
234
233
|
}, {
|
|
235
234
|
minConnections,
|
|
236
235
|
autoDialConcurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
|
|
@@ -242,8 +241,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
242
241
|
this.connectionPruner = new ConnectionPruner({
|
|
243
242
|
connectionManager: this,
|
|
244
243
|
peerStore: components.peerStore,
|
|
245
|
-
events: components.events
|
|
246
|
-
logger: components.logger
|
|
244
|
+
events: components.events
|
|
247
245
|
}, {
|
|
248
246
|
maxConnections: this.maxConnections,
|
|
249
247
|
allow: this.allow
|
|
@@ -254,8 +252,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
254
252
|
metrics: components.metrics,
|
|
255
253
|
peerStore: components.peerStore,
|
|
256
254
|
transportManager: components.transportManager,
|
|
257
|
-
connectionGater: components.connectionGater
|
|
258
|
-
logger: components.logger
|
|
255
|
+
connectionGater: components.connectionGater
|
|
259
256
|
}, {
|
|
260
257
|
addressSorter: init.addressSorter ?? defaultAddressSort,
|
|
261
258
|
maxParallelDials: init.maxParallelDials ?? MAX_PARALLEL_DIALS,
|
|
@@ -359,7 +356,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
359
356
|
this.autoDial.start()
|
|
360
357
|
|
|
361
358
|
this.started = true
|
|
362
|
-
|
|
359
|
+
log('started')
|
|
363
360
|
}
|
|
364
361
|
|
|
365
362
|
async afterStart (): Promise<void> {
|
|
@@ -376,13 +373,13 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
376
373
|
keepAlivePeers.map(async peer => {
|
|
377
374
|
await this.openConnection(peer.id)
|
|
378
375
|
.catch(err => {
|
|
379
|
-
|
|
376
|
+
log.error(err)
|
|
380
377
|
})
|
|
381
378
|
})
|
|
382
379
|
)
|
|
383
380
|
})
|
|
384
381
|
.catch(err => {
|
|
385
|
-
|
|
382
|
+
log.error(err)
|
|
386
383
|
})
|
|
387
384
|
|
|
388
385
|
this.autoDial.afterStart()
|
|
@@ -403,22 +400,22 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
403
400
|
try {
|
|
404
401
|
await connection.close()
|
|
405
402
|
} catch (err) {
|
|
406
|
-
|
|
403
|
+
log.error(err)
|
|
407
404
|
}
|
|
408
405
|
})())
|
|
409
406
|
}
|
|
410
407
|
}
|
|
411
408
|
|
|
412
|
-
|
|
409
|
+
log('closing %d connections', tasks.length)
|
|
413
410
|
await Promise.all(tasks)
|
|
414
411
|
this.connections.clear()
|
|
415
412
|
|
|
416
|
-
|
|
413
|
+
log('stopped')
|
|
417
414
|
}
|
|
418
415
|
|
|
419
416
|
onConnect (evt: CustomEvent<Connection>): void {
|
|
420
417
|
void this._onConnect(evt).catch(err => {
|
|
421
|
-
|
|
418
|
+
log.error(err)
|
|
422
419
|
})
|
|
423
420
|
}
|
|
424
421
|
|
|
@@ -508,12 +505,12 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
508
505
|
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
|
|
509
506
|
|
|
510
507
|
if (peerId != null && options.force !== true) {
|
|
511
|
-
|
|
508
|
+
log('dial %p', peerId)
|
|
512
509
|
const existingConnection = this.getConnections(peerId)
|
|
513
510
|
.find(conn => !conn.transient)
|
|
514
511
|
|
|
515
512
|
if (existingConnection != null) {
|
|
516
|
-
|
|
513
|
+
log('had an existing non-transient connection to %p', peerId)
|
|
517
514
|
|
|
518
515
|
return existingConnection
|
|
519
516
|
}
|
|
@@ -569,7 +566,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
569
566
|
})
|
|
570
567
|
|
|
571
568
|
if (denyConnection) {
|
|
572
|
-
|
|
569
|
+
log('connection from %a refused - connection remote address was in deny list', maConn.remoteAddr)
|
|
573
570
|
return false
|
|
574
571
|
}
|
|
575
572
|
|
|
@@ -586,7 +583,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
586
583
|
|
|
587
584
|
// check pending connections
|
|
588
585
|
if (this.incomingPendingConnections === this.maxIncomingPendingConnections) {
|
|
589
|
-
|
|
586
|
+
log('connection from %a refused - incomingPendingConnections exceeded by host', maConn.remoteAddr)
|
|
590
587
|
return false
|
|
591
588
|
}
|
|
592
589
|
|
|
@@ -596,7 +593,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
596
593
|
try {
|
|
597
594
|
await this.inboundConnectionRateLimiter.consume(host, 1)
|
|
598
595
|
} catch {
|
|
599
|
-
|
|
596
|
+
log('connection from %a refused - inboundConnectionThreshold exceeded by host %s', maConn.remoteAddr, host)
|
|
600
597
|
return false
|
|
601
598
|
}
|
|
602
599
|
}
|
|
@@ -607,7 +604,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
607
604
|
return true
|
|
608
605
|
}
|
|
609
606
|
|
|
610
|
-
|
|
607
|
+
log('connection from %a refused - maxConnections exceeded', maConn.remoteAddr)
|
|
611
608
|
return false
|
|
612
609
|
}
|
|
613
610
|
|