libp2p 0.36.2-fab4f13 → 0.37.0-d5386df
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/src/address-manager/index.d.ts +2 -1
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +1 -1
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +7 -6
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/circuit/hop.d.ts +1 -1
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +4 -4
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/index.d.ts +1 -1
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/listener.d.ts +3 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +10 -2
- package/dist/src/circuit/listener.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +5 -4
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +28 -2
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +7 -5
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -7
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +4 -4
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/{dialer → connection-manager/dialer}/auto-dialer.d.ts +2 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.d.ts +2 -2
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.js +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +37 -8
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -23
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +57 -26
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +80 -42
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +1 -1
- package/dist/src/connection-manager/latency-monitor.js.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +2 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +4 -1
- package/dist/src/dht/dummy-dht.d.ts.map +1 -1
- package/dist/src/dht/dummy-dht.js +8 -1
- package/dist/src/dht/dummy-dht.js.map +1 -1
- package/dist/src/fetch/index.d.ts +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +1 -1
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +4 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +7 -1
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +1 -1
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +4 -4
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +7 -24
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +4 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +8 -1
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +0 -1
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +0 -1
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +4 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +57 -43
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/index.d.ts +1 -1
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/stats.d.ts +1 -1
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +1 -1
- package/dist/src/metrics/stats.js.map +1 -1
- package/dist/src/nat-manager.d.ts +6 -3
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +6 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +1 -1
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-routing.d.ts +2 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +2 -1
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +1 -0
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +1 -0
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pnet/key-generator.d.ts +1 -1
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +1 -1
- package/dist/src/pnet/key-generator.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +1 -1
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +0 -1
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +2 -17
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +3 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +1 -1
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +1 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +16 -25
- package/src/address-manager/index.ts +2 -1
- package/src/circuit/auto-relay.ts +8 -6
- package/src/circuit/circuit/hop.ts +5 -5
- package/src/circuit/index.ts +1 -1
- package/src/circuit/listener.ts +18 -5
- package/src/circuit/pb/index.ts +33 -4
- package/src/circuit/transport.ts +8 -5
- package/src/config.ts +5 -7
- package/src/connection-manager/auto-dialler.ts +5 -5
- package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
- package/src/{dialer → connection-manager/dialer}/dial-request.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/index.ts +48 -37
- package/src/connection-manager/index.ts +146 -60
- package/src/connection-manager/latency-monitor.ts +1 -1
- package/src/connection-manager/visibility-change-emitter.ts +1 -1
- package/src/content-routing/index.ts +2 -1
- package/src/dht/dummy-dht.ts +10 -1
- package/src/fetch/index.ts +2 -2
- package/src/fetch/pb/proto.ts +10 -3
- package/src/identify/index.ts +5 -5
- package/src/identify/pb/message.ts +2 -1
- package/src/index.ts +7 -28
- package/src/insecure/pb/proto.ts +11 -4
- package/src/keychain/index.ts +0 -1
- package/src/libp2p.ts +68 -49
- package/src/metrics/index.ts +1 -1
- package/src/metrics/stats.ts +1 -1
- package/src/nat-manager.ts +8 -4
- package/src/peer-record-updater.ts +1 -1
- package/src/peer-routing.ts +2 -1
- package/src/ping/index.ts +3 -2
- package/src/pnet/index.ts +2 -0
- package/src/pnet/key-generator.ts +1 -1
- package/src/pubsub/dummy-pubsub.ts +1 -1
- package/src/registrar.ts +3 -19
- package/src/transport-manager.ts +3 -1
- package/src/upgrader.ts +1 -1
- package/dist/src/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/dialer/auto-dialer.js +0 -24
- package/dist/src/dialer/auto-dialer.js.map +0 -1
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/dial-request.js.map +0 -1
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/dialer/index.js.map +0 -1
|
@@ -85,12 +85,14 @@ export class AutoRelay {
|
|
|
85
85
|
|
|
86
86
|
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
|
87
87
|
try {
|
|
88
|
-
const
|
|
88
|
+
const connections = this.components.getConnectionManager().getConnections(peerId)
|
|
89
89
|
|
|
90
|
-
if (
|
|
90
|
+
if (connections.length === 0) {
|
|
91
91
|
return
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
const connection = connections[0]
|
|
95
|
+
|
|
94
96
|
// Do not hop on a relayed connection
|
|
95
97
|
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
|
96
98
|
log(`relayed connection to ${id} will not be used to hop on`)
|
|
@@ -223,15 +225,15 @@ export class AutoRelay {
|
|
|
223
225
|
continue
|
|
224
226
|
}
|
|
225
227
|
|
|
226
|
-
const
|
|
228
|
+
const connections = this.components.getConnectionManager().getConnections(id)
|
|
227
229
|
|
|
228
230
|
// If not connected, store for possible later use.
|
|
229
|
-
if (
|
|
231
|
+
if (connections.length === 0) {
|
|
230
232
|
knownHopsToDial.push(id)
|
|
231
233
|
continue
|
|
232
234
|
}
|
|
233
235
|
|
|
234
|
-
await this._addListenRelay(
|
|
236
|
+
await this._addListenRelay(connections[0], idStr)
|
|
235
237
|
|
|
236
238
|
// Check if already listening on enough relays
|
|
237
239
|
if (this.listenRelays.size >= this.maxListeners) {
|
|
@@ -274,7 +276,7 @@ export class AutoRelay {
|
|
|
274
276
|
|
|
275
277
|
async _tryToListenOnRelay (peerId: PeerId) {
|
|
276
278
|
try {
|
|
277
|
-
const connection = await this.components.
|
|
279
|
+
const connection = await this.components.getConnectionManager().openConnection(peerId)
|
|
278
280
|
await this._addListenRelay(connection, peerId.toString())
|
|
279
281
|
} catch (err: any) {
|
|
280
282
|
log.error('Could not use %p as relay', peerId, err)
|
|
@@ -11,7 +11,7 @@ import type { Connection } from '@libp2p/interfaces/connection'
|
|
|
11
11
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
12
12
|
import type { Duplex } from 'it-stream-types'
|
|
13
13
|
import type { Circuit } from '../transport.js'
|
|
14
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
14
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
15
15
|
|
|
16
16
|
const log = logger('libp2p:circuit:hop')
|
|
17
17
|
|
|
@@ -58,8 +58,8 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
58
58
|
// Get the connection to the destination (stop) peer
|
|
59
59
|
const destinationPeer = peerIdFromBytes(request.dstPeer.id)
|
|
60
60
|
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
61
|
+
const destinationConnections = connectionManager.getConnections(destinationPeer)
|
|
62
|
+
if (destinationConnections.length === 0 && !circuit.hopActive()) {
|
|
63
63
|
log('HOP request received but we are not connected to the destination peer')
|
|
64
64
|
return streamHandler.end({
|
|
65
65
|
type: CircuitPB.Type.STATUS,
|
|
@@ -68,7 +68,7 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
// TODO: Handle being an active relay
|
|
71
|
-
if (
|
|
71
|
+
if (destinationConnections.length === 0) {
|
|
72
72
|
log('did not have connection to remote peer')
|
|
73
73
|
return streamHandler.end({
|
|
74
74
|
type: CircuitPB.Type.STATUS,
|
|
@@ -87,7 +87,7 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
87
87
|
try {
|
|
88
88
|
log('performing STOP request')
|
|
89
89
|
const result = await stop({
|
|
90
|
-
connection:
|
|
90
|
+
connection: destinationConnections[0],
|
|
91
91
|
request: stopRequest
|
|
92
92
|
})
|
|
93
93
|
|
package/src/circuit/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
RELAY_RENDEZVOUS_NS
|
|
12
12
|
} from './constants.js'
|
|
13
13
|
import type { AddressSorter } from '@libp2p/interfaces/peer-store'
|
|
14
|
-
import type { Startable } from '@libp2p/interfaces'
|
|
14
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
15
15
|
import type { Components } from '@libp2p/interfaces/components'
|
|
16
16
|
|
|
17
17
|
const log = logger('libp2p:relay')
|
package/src/circuit/listener.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
|
|
2
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
3
|
-
import type {
|
|
1
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
2
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
3
|
+
import type { PeerStore } from '@libp2p/interfaces/peer-store'
|
|
4
4
|
import type { Listener } from '@libp2p/interfaces/transport'
|
|
5
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
6
|
import { Multiaddr } from '@multiformats/multiaddr'
|
|
6
7
|
|
|
7
8
|
export interface ListenerOptions {
|
|
8
|
-
|
|
9
|
+
peerStore: PeerStore
|
|
9
10
|
connectionManager: ConnectionManager
|
|
10
11
|
}
|
|
11
12
|
|
|
@@ -17,7 +18,19 @@ export function createListener (options: ListenerOptions): Listener {
|
|
|
17
18
|
*/
|
|
18
19
|
async function listen (addr: Multiaddr): Promise<void> {
|
|
19
20
|
const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
|
|
20
|
-
const
|
|
21
|
+
const ma = new Multiaddr(addrString)
|
|
22
|
+
|
|
23
|
+
const relayPeerStr = ma.getPeerId()
|
|
24
|
+
|
|
25
|
+
if (relayPeerStr == null) {
|
|
26
|
+
throw new Error('Could not determine relay peer from multiaddr')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const relayPeerId = peerIdFromString(relayPeerStr)
|
|
30
|
+
|
|
31
|
+
await options.peerStore.addressBook.add(relayPeerId, [ma])
|
|
32
|
+
|
|
33
|
+
const relayConn = await options.connectionManager.openConnection(relayPeerId)
|
|
21
34
|
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
|
|
22
35
|
|
|
23
36
|
listeningAddrs.set(relayConn.remotePeer.toString(), relayedAddr)
|
package/src/circuit/pb/index.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
3
3
|
|
|
4
4
|
import { enumeration, encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
|
|
5
|
+
import type { Codec } from 'protons-runtime'
|
|
5
6
|
|
|
6
7
|
export interface CircuitRelay {
|
|
7
8
|
type?: CircuitRelay.Type
|
|
@@ -30,11 +31,31 @@ export namespace CircuitRelay {
|
|
|
30
31
|
MALFORMED_MESSAGE = 'MALFORMED_MESSAGE'
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
enum __StatusValues {
|
|
35
|
+
SUCCESS = 100,
|
|
36
|
+
HOP_SRC_ADDR_TOO_LONG = 220,
|
|
37
|
+
HOP_DST_ADDR_TOO_LONG = 221,
|
|
38
|
+
HOP_SRC_MULTIADDR_INVALID = 250,
|
|
39
|
+
HOP_DST_MULTIADDR_INVALID = 251,
|
|
40
|
+
HOP_NO_CONN_TO_DST = 260,
|
|
41
|
+
HOP_CANT_DIAL_DST = 261,
|
|
42
|
+
HOP_CANT_OPEN_DST_STREAM = 262,
|
|
43
|
+
HOP_CANT_SPEAK_RELAY = 270,
|
|
44
|
+
HOP_CANT_RELAY_TO_SELF = 280,
|
|
45
|
+
STOP_SRC_ADDR_TOO_LONG = 320,
|
|
46
|
+
STOP_DST_ADDR_TOO_LONG = 321,
|
|
47
|
+
STOP_SRC_MULTIADDR_INVALID = 350,
|
|
48
|
+
STOP_DST_MULTIADDR_INVALID = 351,
|
|
49
|
+
STOP_RELAY_REFUSED = 390,
|
|
50
|
+
MALFORMED_MESSAGE = 400
|
|
51
|
+
}
|
|
52
|
+
|
|
33
53
|
export namespace Status {
|
|
34
54
|
export const codec = () => {
|
|
35
|
-
return enumeration<typeof Status>(
|
|
55
|
+
return enumeration<typeof Status>(__StatusValues)
|
|
36
56
|
}
|
|
37
57
|
}
|
|
58
|
+
|
|
38
59
|
export enum Type {
|
|
39
60
|
HOP = 'HOP',
|
|
40
61
|
STOP = 'STOP',
|
|
@@ -42,18 +63,26 @@ export namespace CircuitRelay {
|
|
|
42
63
|
CAN_HOP = 'CAN_HOP'
|
|
43
64
|
}
|
|
44
65
|
|
|
66
|
+
enum __TypeValues {
|
|
67
|
+
HOP = 1,
|
|
68
|
+
STOP = 2,
|
|
69
|
+
STATUS = 3,
|
|
70
|
+
CAN_HOP = 4
|
|
71
|
+
}
|
|
72
|
+
|
|
45
73
|
export namespace Type {
|
|
46
74
|
export const codec = () => {
|
|
47
|
-
return enumeration<typeof Type>(
|
|
75
|
+
return enumeration<typeof Type>(__TypeValues)
|
|
48
76
|
}
|
|
49
77
|
}
|
|
78
|
+
|
|
50
79
|
export interface Peer {
|
|
51
80
|
id: Uint8Array
|
|
52
81
|
addrs: Uint8Array[]
|
|
53
82
|
}
|
|
54
83
|
|
|
55
84
|
export namespace Peer {
|
|
56
|
-
export const codec = () => {
|
|
85
|
+
export const codec = (): Codec<Peer> => {
|
|
57
86
|
return message<Peer>({
|
|
58
87
|
1: { name: 'id', codec: bytes },
|
|
59
88
|
2: { name: 'addrs', codec: bytes, repeats: true }
|
|
@@ -69,7 +98,7 @@ export namespace CircuitRelay {
|
|
|
69
98
|
}
|
|
70
99
|
}
|
|
71
100
|
|
|
72
|
-
export const codec = () => {
|
|
101
|
+
export const codec = (): Codec<CircuitRelay> => {
|
|
73
102
|
return message<CircuitRelay>({
|
|
74
103
|
1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true },
|
|
75
104
|
2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true },
|
package/src/circuit/transport.ts
CHANGED
|
@@ -49,7 +49,7 @@ export class Circuit implements Transport, Initializable {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
get [Symbol.toStringTag] () {
|
|
52
|
-
return
|
|
52
|
+
return 'libp2p/circuit-relay-v1'
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
async _onProtocol (data: IncomingStreamData) {
|
|
@@ -149,9 +149,12 @@ export class Circuit implements Transport, Initializable {
|
|
|
149
149
|
const destinationPeer = peerIdFromString(destinationId)
|
|
150
150
|
|
|
151
151
|
let disconnectOnFailure = false
|
|
152
|
-
|
|
152
|
+
const relayConnections = this.components.getConnectionManager().getConnections(relayPeer)
|
|
153
|
+
let relayConnection = relayConnections[0]
|
|
154
|
+
|
|
153
155
|
if (relayConnection == null) {
|
|
154
|
-
|
|
156
|
+
await this.components.getPeerStore().addressBook.add(relayPeer, [relayAddr])
|
|
157
|
+
relayConnection = await this.components.getConnectionManager().openConnection(relayPeer, options)
|
|
155
158
|
disconnectOnFailure = true
|
|
156
159
|
}
|
|
157
160
|
|
|
@@ -195,8 +198,8 @@ export class Circuit implements Transport, Initializable {
|
|
|
195
198
|
this.handler = options.handler
|
|
196
199
|
|
|
197
200
|
return createListener({
|
|
198
|
-
|
|
199
|
-
|
|
201
|
+
connectionManager: this.components.getConnectionManager(),
|
|
202
|
+
peerStore: this.components.getPeerStore()
|
|
200
203
|
})
|
|
201
204
|
}
|
|
202
205
|
|
package/src/config.ts
CHANGED
|
@@ -21,14 +21,8 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
21
21
|
connectionManager: {
|
|
22
22
|
maxConnections: 300,
|
|
23
23
|
minConnections: 50,
|
|
24
|
+
autoDial: true,
|
|
24
25
|
autoDialInterval: 10000,
|
|
25
|
-
autoDial: true
|
|
26
|
-
},
|
|
27
|
-
connectionGater: {},
|
|
28
|
-
transportManager: {
|
|
29
|
-
faultTolerance: FaultTolerance.FATAL_ALL
|
|
30
|
-
},
|
|
31
|
-
dialer: {
|
|
32
26
|
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
|
33
27
|
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
|
34
28
|
dialTimeout: Constants.DIAL_TIMEOUT,
|
|
@@ -37,6 +31,10 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
37
31
|
},
|
|
38
32
|
addressSorter: publicAddressesFirst
|
|
39
33
|
},
|
|
34
|
+
connectionGater: {},
|
|
35
|
+
transportManager: {
|
|
36
|
+
faultTolerance: FaultTolerance.FATAL_ALL
|
|
37
|
+
},
|
|
40
38
|
host: {
|
|
41
39
|
agentVersion: AGENT_VERSION
|
|
42
40
|
},
|
|
@@ -6,7 +6,7 @@ import all from 'it-all'
|
|
|
6
6
|
import { pipe } from 'it-pipe'
|
|
7
7
|
import filter from 'it-filter'
|
|
8
8
|
import sort from 'it-sort'
|
|
9
|
-
import type { Startable } from '@libp2p/interfaces'
|
|
9
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
10
10
|
import type { Components } from '@libp2p/interfaces/components'
|
|
11
11
|
|
|
12
12
|
const log = logger('libp2p:connection-manager:auto-dialler')
|
|
@@ -102,7 +102,7 @@ export class AutoDialler implements Startable {
|
|
|
102
102
|
const minConnections = this.options.minConnections
|
|
103
103
|
|
|
104
104
|
// Already has enough connections
|
|
105
|
-
if (this.components.getConnectionManager().
|
|
105
|
+
if (this.components.getConnectionManager().getConnections().length >= minConnections) {
|
|
106
106
|
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
107
107
|
|
|
108
108
|
return
|
|
@@ -126,7 +126,7 @@ export class AutoDialler implements Startable {
|
|
|
126
126
|
async (source) => await all(source)
|
|
127
127
|
)
|
|
128
128
|
|
|
129
|
-
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().
|
|
129
|
+
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnections().length < minConnections; i++) {
|
|
130
130
|
// Connection Manager was stopped during async dial
|
|
131
131
|
if (!this.running) {
|
|
132
132
|
return
|
|
@@ -134,10 +134,10 @@ export class AutoDialler implements Startable {
|
|
|
134
134
|
|
|
135
135
|
const peer = peers[i]
|
|
136
136
|
|
|
137
|
-
if (this.components.getConnectionManager().
|
|
137
|
+
if (this.components.getConnectionManager().getConnections(peer.id).length === 0) {
|
|
138
138
|
log('connecting to a peerStore stored peer %p', peer.id)
|
|
139
139
|
try {
|
|
140
|
-
await this.components.
|
|
140
|
+
await this.components.getConnectionManager().openConnection(peer.id)
|
|
141
141
|
} catch (err: any) {
|
|
142
142
|
log.error('could not connect to peerStore stored peer', err)
|
|
143
143
|
}
|
|
@@ -1,39 +1,58 @@
|
|
|
1
1
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import type { Components } from '@libp2p/interfaces/components'
|
|
4
|
+
import { TimeoutController } from 'timeout-abort-controller'
|
|
4
5
|
|
|
5
6
|
const log = logger('libp2p:dialer:auto-dialer')
|
|
6
7
|
|
|
7
8
|
export interface AutoDialerInit {
|
|
8
9
|
enabled: boolean
|
|
9
10
|
minConnections: number
|
|
11
|
+
dialTimeout: number
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export class AutoDialer {
|
|
13
15
|
private readonly components: Components
|
|
14
16
|
private readonly enabled: boolean
|
|
15
17
|
private readonly minConnections: number
|
|
18
|
+
private readonly dialTimeout: number
|
|
16
19
|
|
|
17
20
|
constructor (components: Components, init: AutoDialerInit) {
|
|
18
21
|
this.components = components
|
|
19
22
|
this.enabled = init.enabled
|
|
20
23
|
this.minConnections = init.minConnections
|
|
24
|
+
this.dialTimeout = init.dialTimeout
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
public handle (evt: CustomEvent<PeerInfo>) {
|
|
24
28
|
const { detail: peer } = evt
|
|
25
29
|
|
|
30
|
+
if (!this.enabled) {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const connections = this.components.getConnectionManager().getConnections(peer.id)
|
|
35
|
+
|
|
26
36
|
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
|
27
|
-
if (
|
|
37
|
+
if (connections.length === 0) {
|
|
28
38
|
const minConnections = this.minConnections ?? 0
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
log('auto-dialing discovered peer %p', peer.id)
|
|
40
|
+
const allConnections = this.components.getConnectionManager().getConnections()
|
|
32
41
|
|
|
33
|
-
|
|
42
|
+
if (minConnections > allConnections.length) {
|
|
43
|
+
log('auto-dialing discovered peer %p with timeout %d', peer.id, this.dialTimeout)
|
|
44
|
+
|
|
45
|
+
const controller = new TimeoutController(this.dialTimeout)
|
|
46
|
+
|
|
47
|
+
void this.components.getConnectionManager().openConnection(peer.id, {
|
|
48
|
+
signal: controller.signal
|
|
49
|
+
})
|
|
34
50
|
.catch(err => {
|
|
35
51
|
log.error('could not connect to discovered peer %p with %o', peer.id, err)
|
|
36
52
|
})
|
|
53
|
+
.finally(() => {
|
|
54
|
+
controller.clear()
|
|
55
|
+
})
|
|
37
56
|
}
|
|
38
57
|
}
|
|
39
58
|
}
|
|
@@ -3,12 +3,12 @@ import { anySignal } from 'any-signal'
|
|
|
3
3
|
import FIFO from 'p-fifo'
|
|
4
4
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
5
5
|
import { setMaxListeners } from 'events'
|
|
6
|
-
import { codes } from '
|
|
6
|
+
import { codes } from '../../errors.js'
|
|
7
7
|
import { logger } from '@libp2p/logger'
|
|
8
8
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
9
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
10
10
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
11
|
-
import type {
|
|
11
|
+
import type { Dialer } from './index.js'
|
|
12
12
|
|
|
13
13
|
const log = logger('libp2p:dialer:dial-request')
|
|
14
14
|
|
|
@@ -19,12 +19,12 @@ export interface DialAction {
|
|
|
19
19
|
export interface DialRequestOptions {
|
|
20
20
|
addrs: Multiaddr[]
|
|
21
21
|
dialAction: DialAction
|
|
22
|
-
dialer:
|
|
22
|
+
dialer: Dialer
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export class DialRequest {
|
|
26
26
|
private readonly addrs: Multiaddr[]
|
|
27
|
-
private readonly dialer:
|
|
27
|
+
private readonly dialer: Dialer
|
|
28
28
|
private readonly dialAction: DialAction
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -3,7 +3,7 @@ import all from 'it-all'
|
|
|
3
3
|
import filter from 'it-filter'
|
|
4
4
|
import { pipe } from 'it-pipe'
|
|
5
5
|
import errCode from 'err-code'
|
|
6
|
-
import { Multiaddr } from '@multiformats/multiaddr'
|
|
6
|
+
import { Multiaddr, Resolver } from '@multiformats/multiaddr'
|
|
7
7
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
8
8
|
import { AbortError } from '@libp2p/interfaces/errors'
|
|
9
9
|
import { anySignal } from 'any-signal'
|
|
@@ -12,22 +12,23 @@ import { setMaxListeners } from 'events'
|
|
|
12
12
|
import { DialAction, DialRequest } from './dial-request.js'
|
|
13
13
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
14
14
|
import { trackedMap } from '@libp2p/tracked-map'
|
|
15
|
-
import { codes } from '
|
|
15
|
+
import { codes } from '../../errors.js'
|
|
16
16
|
import {
|
|
17
17
|
DIAL_TIMEOUT,
|
|
18
18
|
MAX_PARALLEL_DIALS,
|
|
19
19
|
MAX_PER_PEER_DIALS,
|
|
20
20
|
MAX_ADDRS_TO_DIAL
|
|
21
|
-
} from '
|
|
21
|
+
} from '../../constants.js'
|
|
22
22
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
23
|
-
import type { AbortOptions
|
|
23
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
24
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
24
25
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
25
|
-
import { getPeer } from '
|
|
26
|
+
import { getPeer } from '../../get-peer.js'
|
|
26
27
|
import sort from 'it-sort'
|
|
27
|
-
import
|
|
28
|
-
import type { Dialer, DialerInit } from '@libp2p/interfaces/dialer'
|
|
28
|
+
import { Components, Initializable } from '@libp2p/interfaces/components'
|
|
29
29
|
import map from 'it-map'
|
|
30
30
|
import type { AddressSorter } from '@libp2p/interfaces/peer-store'
|
|
31
|
+
import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics'
|
|
31
32
|
|
|
32
33
|
const log = logger('libp2p:dialer')
|
|
33
34
|
|
|
@@ -52,8 +53,41 @@ export interface PendingDialTarget {
|
|
|
52
53
|
reject: (err: Error) => void
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
export
|
|
56
|
-
|
|
56
|
+
export interface DialerInit {
|
|
57
|
+
/**
|
|
58
|
+
* Sort the known addresses of a peer before trying to dial
|
|
59
|
+
*/
|
|
60
|
+
addressSorter?: AddressSorter
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Number of max concurrent dials
|
|
64
|
+
*/
|
|
65
|
+
maxParallelDials?: number
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Number of max addresses to dial for a given peer
|
|
69
|
+
*/
|
|
70
|
+
maxAddrsToDial?: number
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* How long a dial attempt is allowed to take
|
|
74
|
+
*/
|
|
75
|
+
dialTimeout?: number
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Number of max concurrent dials per peer
|
|
79
|
+
*/
|
|
80
|
+
maxDialsPerPeer?: number
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Multiaddr resolvers to use when dialing
|
|
84
|
+
*/
|
|
85
|
+
resolvers?: Record<string, Resolver>
|
|
86
|
+
metrics?: ComponentMetricsTracker
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class Dialer implements Startable, Initializable {
|
|
90
|
+
private components: Components = new Components()
|
|
57
91
|
private readonly addressSorter: AddressSorter
|
|
58
92
|
private readonly maxAddrsToDial: number
|
|
59
93
|
private readonly timeout: number
|
|
@@ -63,8 +97,7 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
63
97
|
public pendingDialTargets: Map<string, PendingDialTarget>
|
|
64
98
|
private started: boolean
|
|
65
99
|
|
|
66
|
-
constructor (
|
|
67
|
-
this.components = components
|
|
100
|
+
constructor (init: DialerInit = {}) {
|
|
68
101
|
this.started = false
|
|
69
102
|
this.addressSorter = init.addressSorter ?? publicAddressesFirst
|
|
70
103
|
this.maxAddrsToDial = init.maxAddrsToDial ?? MAX_ADDRS_TO_DIAL
|
|
@@ -87,6 +120,10 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
87
120
|
}
|
|
88
121
|
}
|
|
89
122
|
|
|
123
|
+
init (components: Components): void {
|
|
124
|
+
this.components = components
|
|
125
|
+
}
|
|
126
|
+
|
|
90
127
|
isStarted () {
|
|
91
128
|
return this.started
|
|
92
129
|
}
|
|
@@ -139,16 +176,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
139
176
|
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
|
|
140
177
|
}
|
|
141
178
|
|
|
142
|
-
log('dial to %p', id)
|
|
143
|
-
|
|
144
|
-
const existingConnection = this.components.getConnectionManager().getConnection(id)
|
|
145
|
-
|
|
146
|
-
if (existingConnection != null) {
|
|
147
|
-
log('had an existing connection to %p', id)
|
|
148
|
-
|
|
149
|
-
return existingConnection
|
|
150
|
-
}
|
|
151
|
-
|
|
152
179
|
log('creating dial target for %p', id)
|
|
153
180
|
|
|
154
181
|
const dialTarget = await this._createCancellableDialTarget(id)
|
|
@@ -176,22 +203,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
176
203
|
}
|
|
177
204
|
}
|
|
178
205
|
|
|
179
|
-
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
180
|
-
if (protocols == null) {
|
|
181
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
185
|
-
|
|
186
|
-
if (protocols.length === 0) {
|
|
187
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const connection = await this.dial(peer, options)
|
|
191
|
-
|
|
192
|
-
return await connection.newStream(protocols)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
206
|
/**
|
|
196
207
|
* Connects to a given `peer` by dialing all of its known addresses.
|
|
197
208
|
* The dial to the first address that is successfully able to upgrade a connection
|