@libp2p/circuit-relay-v2 3.2.17-439d2c9ce → 3.2.17
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 +1 -1
- package/dist/index.min.js.map +3 -3
- package/dist/src/transport/discovery.d.ts +6 -11
- package/dist/src/transport/discovery.d.ts.map +1 -1
- package/dist/src/transport/discovery.js +26 -69
- package/dist/src/transport/discovery.js.map +1 -1
- package/dist/typedoc-urls.json +23 -0
- package/package.json +10 -10
- package/src/transport/discovery.ts +27 -86
@@ -1,5 +1,5 @@
|
|
1
1
|
import { TypedEventEmitter } from 'main-event';
|
2
|
-
import type { ComponentLogger,
|
2
|
+
import type { ComponentLogger, PeerId, PeerStore, Startable, TopologyFilter } from '@libp2p/interface';
|
3
3
|
import type { ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal';
|
4
4
|
export interface RelayDiscoveryEvents {
|
5
5
|
'relay:discover': CustomEvent<PeerId>;
|
@@ -11,7 +11,6 @@ export interface RelayDiscoveryComponents {
|
|
11
11
|
registrar: Registrar;
|
12
12
|
logger: ComponentLogger;
|
13
13
|
randomWalk: RandomWalk;
|
14
|
-
events: TypedEventTarget<Libp2pEvents>;
|
15
14
|
}
|
16
15
|
export interface RelayDiscoveryInit {
|
17
16
|
filter?: TopologyFilter;
|
@@ -21,7 +20,10 @@ export interface RelayDiscoveryInit {
|
|
21
20
|
* peers that support the circuit v2 HOP protocol.
|
22
21
|
*/
|
23
22
|
export declare class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> implements Startable {
|
24
|
-
private readonly
|
23
|
+
private readonly peerStore;
|
24
|
+
private readonly registrar;
|
25
|
+
private readonly connectionManager;
|
26
|
+
private readonly randomWalk;
|
25
27
|
private started;
|
26
28
|
private running;
|
27
29
|
private topologyId?;
|
@@ -39,16 +41,9 @@ export declare class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEven
|
|
39
41
|
*
|
40
42
|
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected to
|
41
43
|
* 2. Dial and try to listen on the peers we know that support hop but are not connected
|
42
|
-
* 3. Search the network
|
43
|
-
* 4. Dial any peers discovered - this covers when no peer routing implementation has been configured but some peer discovery mechanism is also present
|
44
|
+
* 3. Search the network
|
44
45
|
*/
|
45
46
|
startDiscovery(): void;
|
46
47
|
stopDiscovery(): void;
|
47
|
-
onPeer(evt: CustomEvent<PeerInfo>): void;
|
48
|
-
maybeDialPeer(evt: CustomEvent<PeerInfo>): Promise<void>;
|
49
|
-
dialPeer({ peerId, signal }: {
|
50
|
-
peerId: PeerId;
|
51
|
-
signal?: AbortSignal;
|
52
|
-
}): Promise<void>;
|
53
48
|
}
|
54
49
|
//# sourceMappingURL=discovery.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../../../src/transport/discovery.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAmB,MAAM,YAAY,CAAA;AAK/D,OAAO,KAAK,EAAE,eAAe,
|
1
|
+
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../../../src/transport/discovery.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAmB,MAAM,YAAY,CAAA;AAK/D,OAAO,KAAK,EAAE,eAAe,EAAgB,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AACpH,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAE5G,MAAM,WAAW,oBAAoB;IACnC,gBAAgB,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CACtC;AAED,MAAM,WAAW,wBAAwB;IACvC,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,eAAe,CAAA;IACvB,UAAU,EAAE,UAAU,CAAA;CACvB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,cAAc,CAAA;CACxB;AAED;;;GAGG;AACH,qBAAa,cAAe,SAAQ,iBAAiB,CAAC,oBAAoB,CAAE,YAAW,SAAS;IAC9F,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IACvC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,UAAU,CAAC,CAAQ;IAC3B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,mBAAmB,CAAiB;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAgB;IACxC,OAAO,CAAC,KAAK,CAAC,CAAW;gBAEZ,UAAU,EAAE,wBAAwB,EAAE,IAAI,GAAE,kBAAuB;IAehF,SAAS,IAAK,OAAO;IAIf,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAc7B,IAAI,IAAK,IAAI;IASb;;;;;;;OAOG;IACH,cAAc,IAAK,IAAI;IA2HvB,aAAa,IAAK,IAAI;CAKvB"}
|
@@ -8,7 +8,10 @@ import { RELAY_V2_HOP_CODEC } from '../constants.js';
|
|
8
8
|
* peers that support the circuit v2 HOP protocol.
|
9
9
|
*/
|
10
10
|
export class RelayDiscovery extends TypedEventEmitter {
|
11
|
-
|
11
|
+
peerStore;
|
12
|
+
registrar;
|
13
|
+
connectionManager;
|
14
|
+
randomWalk;
|
12
15
|
started;
|
13
16
|
running;
|
14
17
|
topologyId;
|
@@ -19,14 +22,15 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
19
22
|
constructor(components, init = {}) {
|
20
23
|
super();
|
21
24
|
this.log = components.logger.forComponent('libp2p:circuit-relay:discover-relays');
|
22
|
-
this.components = components;
|
23
25
|
this.started = false;
|
24
26
|
this.running = false;
|
27
|
+
this.peerStore = components.peerStore;
|
28
|
+
this.registrar = components.registrar;
|
29
|
+
this.connectionManager = components.connectionManager;
|
30
|
+
this.randomWalk = components.randomWalk;
|
25
31
|
this.filter = init.filter;
|
26
32
|
this.discoveryController = new AbortController();
|
27
33
|
setMaxListeners(Infinity, this.discoveryController.signal);
|
28
|
-
this.dialPeer = this.dialPeer.bind(this);
|
29
|
-
this.onPeer = this.onPeer.bind(this);
|
30
34
|
}
|
31
35
|
isStarted() {
|
32
36
|
return this.started;
|
@@ -34,7 +38,7 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
34
38
|
async start() {
|
35
39
|
// register a topology listener for when new peers are encountered
|
36
40
|
// that support the hop protocol
|
37
|
-
this.topologyId = await this.
|
41
|
+
this.topologyId = await this.registrar.register(RELAY_V2_HOP_CODEC, {
|
38
42
|
filter: this.filter,
|
39
43
|
onConnect: (peerId) => {
|
40
44
|
this.log.trace('discovered relay %p queue (length: %d, active %d)', peerId, this.queue?.size, this.queue?.running);
|
@@ -45,11 +49,9 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
45
49
|
}
|
46
50
|
stop() {
|
47
51
|
if (this.topologyId != null) {
|
48
|
-
this.
|
49
|
-
}
|
50
|
-
if (this.running) {
|
51
|
-
this.stopDiscovery();
|
52
|
+
this.registrar.unregister(this.topologyId);
|
52
53
|
}
|
54
|
+
this.discoveryController?.abort();
|
53
55
|
this.started = false;
|
54
56
|
}
|
55
57
|
/**
|
@@ -58,8 +60,7 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
58
60
|
*
|
59
61
|
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected to
|
60
62
|
* 2. Dial and try to listen on the peers we know that support hop but are not connected
|
61
|
-
* 3. Search the network
|
62
|
-
* 4. Dial any peers discovered - this covers when no peer routing implementation has been configured but some peer discovery mechanism is also present
|
63
|
+
* 3. Search the network
|
63
64
|
*/
|
64
65
|
startDiscovery() {
|
65
66
|
if (this.running) {
|
@@ -69,12 +70,10 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
69
70
|
this.running = true;
|
70
71
|
this.discoveryController = new AbortController();
|
71
72
|
setMaxListeners(Infinity, this.discoveryController.signal);
|
72
|
-
// dial any peer we discover
|
73
|
-
this.components.events.addEventListener('peer:discovery', this.onPeer);
|
74
73
|
Promise.resolve()
|
75
74
|
.then(async () => {
|
76
75
|
this.log('searching peer store for relays');
|
77
|
-
const peers = (await this.
|
76
|
+
const peers = (await this.peerStore.all({
|
78
77
|
filters: [
|
79
78
|
// filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
|
80
79
|
(peer) => {
|
@@ -109,19 +108,19 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
109
108
|
concurrency: 5
|
110
109
|
});
|
111
110
|
this.log('start random walk');
|
112
|
-
for await (const peer of this.
|
111
|
+
for await (const peer of this.randomWalk.walk({ signal: this.discoveryController.signal })) {
|
113
112
|
this.log.trace('found random peer %p', peer.id);
|
114
113
|
if (queue.has(peer.id)) {
|
115
114
|
this.log.trace('random peer %p was already in queue', peer.id);
|
116
115
|
// skip peers already in the queue
|
117
116
|
continue;
|
118
117
|
}
|
119
|
-
if (this.
|
118
|
+
if (this.connectionManager.getConnections(peer.id)?.length > 0) {
|
120
119
|
this.log.trace('random peer %p was already connected', peer.id);
|
121
120
|
// skip peers we are already connected to
|
122
121
|
continue;
|
123
122
|
}
|
124
|
-
if (!(await this.
|
123
|
+
if (!(await this.connectionManager.isDialable(peer.multiaddrs))) {
|
125
124
|
this.log.trace('random peer %p was not dialable', peer.id, peer.multiaddrs.map(ma => ma.toString()));
|
126
125
|
// skip peers we can't dial
|
127
126
|
continue;
|
@@ -136,7 +135,16 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
136
135
|
this.log('adding random peer %p to dial queue (length: %d, active %d)', peer.id, queue.size, queue.running);
|
137
136
|
// dial the peer - this will cause identify to run and our topology to
|
138
137
|
// be notified and we'll attempt to create reservations
|
139
|
-
queue.add(
|
138
|
+
queue.add(async () => {
|
139
|
+
const signal = anySignal([this.discoveryController.signal, AbortSignal.timeout(5000)]);
|
140
|
+
setMaxListeners(Infinity, signal);
|
141
|
+
try {
|
142
|
+
await this.connectionManager.openConnection(peer.id, { signal });
|
143
|
+
}
|
144
|
+
finally {
|
145
|
+
signal.clear();
|
146
|
+
}
|
147
|
+
}, {
|
140
148
|
peerId: peer.id,
|
141
149
|
signal: this.discoveryController.signal
|
142
150
|
})
|
@@ -157,57 +165,6 @@ export class RelayDiscovery extends TypedEventEmitter {
|
|
157
165
|
this.log('stop discovery');
|
158
166
|
this.running = false;
|
159
167
|
this.discoveryController?.abort();
|
160
|
-
this.queue?.clear();
|
161
|
-
// stop dialing any peer we discover
|
162
|
-
this.components.events.removeEventListener('peer:discovery', this.onPeer);
|
163
|
-
}
|
164
|
-
onPeer(evt) {
|
165
|
-
this.log.trace('maybe dialing discovered peer %p - %e', evt.detail.id);
|
166
|
-
this.maybeDialPeer(evt)
|
167
|
-
.catch(err => {
|
168
|
-
this.log.trace('error dialing discovered peer %p - %e', evt.detail.id, err);
|
169
|
-
});
|
170
|
-
}
|
171
|
-
async maybeDialPeer(evt) {
|
172
|
-
if (this.queue == null) {
|
173
|
-
return;
|
174
|
-
}
|
175
|
-
const peerId = evt.detail.id;
|
176
|
-
const multiaddrs = evt.detail.multiaddrs;
|
177
|
-
if (this.queue.has(peerId)) {
|
178
|
-
this.log.trace('random peer %p was already in queue', peerId);
|
179
|
-
// skip peers already in the queue
|
180
|
-
return;
|
181
|
-
}
|
182
|
-
if (this.components.connectionManager.getConnections(peerId)?.length > 0) {
|
183
|
-
this.log.trace('random peer %p was already connected', peerId);
|
184
|
-
// skip peers we are already connected to
|
185
|
-
return;
|
186
|
-
}
|
187
|
-
if (!(await this.components.connectionManager.isDialable(multiaddrs))) {
|
188
|
-
this.log.trace('random peer %p was not dialable', peerId);
|
189
|
-
// skip peers we can't dial
|
190
|
-
return;
|
191
|
-
}
|
192
|
-
this.queue?.add(this.dialPeer, {
|
193
|
-
peerId: evt.detail.id,
|
194
|
-
signal: this.discoveryController.signal
|
195
|
-
})
|
196
|
-
.catch(err => {
|
197
|
-
this.log.error('error opening connection to discovered peer %p', evt.detail.id, err);
|
198
|
-
});
|
199
|
-
}
|
200
|
-
async dialPeer({ peerId, signal }) {
|
201
|
-
const combinedSignal = anySignal([AbortSignal.timeout(5_000), signal]);
|
202
|
-
setMaxListeners(Infinity, combinedSignal);
|
203
|
-
try {
|
204
|
-
await this.components.connectionManager.openConnection(peerId, {
|
205
|
-
signal: combinedSignal
|
206
|
-
});
|
207
|
-
}
|
208
|
-
finally {
|
209
|
-
combinedSignal.clear();
|
210
|
-
}
|
211
168
|
}
|
212
169
|
}
|
213
170
|
/**
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../../src/transport/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC/D,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EACL,kBAAkB,EACnB,MAAM,iBAAiB,CAAA;
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../../src/transport/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC/D,OAAO,EAAE,QAAQ,IAA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I,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;QAC1B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,mBAAmB,EAAE,KAAK,EAAE,CAAA;IACnC,CAAC;CACF;AAED;;;GAGG;AACH,SAAS,WAAW,CAAE,IAAU;IAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;IAEvD,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;QACrB,OAAO,CAAC,CAAA;IACV,CAAC;IAED,OAAO,IAAI,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,EAAE,CAAA;AACzD,CAAC"}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
"codec": "https://libp2p.github.io/js-libp2p/functions/_libp2p_circuit-relay-v2.Limit.codec.html",
|
3
|
+
"decode": "https://libp2p.github.io/js-libp2p/functions/_libp2p_circuit-relay-v2.Limit.decode.html",
|
4
|
+
"encode": "https://libp2p.github.io/js-libp2p/functions/_libp2p_circuit-relay-v2.Limit.encode.html",
|
5
|
+
"CircuitRelayServerComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayServerComponents.html",
|
6
|
+
"CircuitRelayServerInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayServerInit.html",
|
7
|
+
"CircuitRelayService": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayService.html",
|
8
|
+
".:CircuitRelayService": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayService.html",
|
9
|
+
"CircuitRelayServiceEvents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayServiceEvents.html",
|
10
|
+
".:CircuitRelayServiceEvents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayServiceEvents.html",
|
11
|
+
"CircuitRelayTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayTransportComponents.html",
|
12
|
+
"CircuitRelayTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.CircuitRelayTransportInit.html",
|
13
|
+
"Limit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.Limit.html",
|
14
|
+
"RelayDiscoveryComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.RelayDiscoveryComponents.html",
|
15
|
+
"RelayReservation": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.RelayReservation.html",
|
16
|
+
".:RelayReservation": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.RelayReservation.html",
|
17
|
+
"ServerReservationStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.ServerReservationStoreInit.html",
|
18
|
+
"TransportReservationStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_circuit-relay-v2.TransportReservationStoreInit.html",
|
19
|
+
"RELAY_V2_HOP_CODEC": "https://libp2p.github.io/js-libp2p/variables/_libp2p_circuit-relay-v2.RELAY_V2_HOP_CODEC.html",
|
20
|
+
"RELAY_V2_STOP_CODEC": "https://libp2p.github.io/js-libp2p/variables/_libp2p_circuit-relay-v2.RELAY_V2_STOP_CODEC.html",
|
21
|
+
"circuitRelayServer": "https://libp2p.github.io/js-libp2p/functions/_libp2p_circuit-relay-v2.circuitRelayServer.html",
|
22
|
+
"circuitRelayTransport": "https://libp2p.github.io/js-libp2p/functions/_libp2p_circuit-relay-v2.circuitRelayTransport.html"
|
23
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@libp2p/circuit-relay-v2",
|
3
|
-
"version": "3.2.17
|
3
|
+
"version": "3.2.17",
|
4
4
|
"description": "Implementation of Circuit Relay v2",
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-circuit-relay-v2#readme",
|
@@ -45,13 +45,13 @@
|
|
45
45
|
"doc-check": "aegir doc-check"
|
46
46
|
},
|
47
47
|
"dependencies": {
|
48
|
-
"@libp2p/crypto": "5.1.6
|
49
|
-
"@libp2p/interface": "2.10.4
|
50
|
-
"@libp2p/interface-internal": "2.3.17
|
51
|
-
"@libp2p/peer-collections": "6.0.33
|
52
|
-
"@libp2p/peer-id": "5.1.7
|
53
|
-
"@libp2p/peer-record": "8.0.33
|
54
|
-
"@libp2p/utils": "6.7.0
|
48
|
+
"@libp2p/crypto": "^5.1.6",
|
49
|
+
"@libp2p/interface": "^2.10.4",
|
50
|
+
"@libp2p/interface-internal": "^2.3.17",
|
51
|
+
"@libp2p/peer-collections": "^6.0.33",
|
52
|
+
"@libp2p/peer-id": "^5.1.7",
|
53
|
+
"@libp2p/peer-record": "^8.0.33",
|
54
|
+
"@libp2p/utils": "^6.7.0",
|
55
55
|
"@multiformats/multiaddr": "^12.4.4",
|
56
56
|
"@multiformats/multiaddr-matcher": "^1.7.2",
|
57
57
|
"any-signal": "^4.1.1",
|
@@ -67,8 +67,8 @@
|
|
67
67
|
"uint8arrays": "^5.1.0"
|
68
68
|
},
|
69
69
|
"devDependencies": {
|
70
|
-
"@libp2p/interface-compliance-tests": "6.4.11
|
71
|
-
"@libp2p/logger": "5.1.20
|
70
|
+
"@libp2p/interface-compliance-tests": "^6.4.11",
|
71
|
+
"@libp2p/logger": "^5.1.20",
|
72
72
|
"aegir": "^47.0.14",
|
73
73
|
"delay": "^6.0.0",
|
74
74
|
"it-drain": "^3.0.9",
|
@@ -5,7 +5,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
5
5
|
import {
|
6
6
|
RELAY_V2_HOP_CODEC
|
7
7
|
} from '../constants.js'
|
8
|
-
import type { ComponentLogger,
|
8
|
+
import type { ComponentLogger, Logger, Peer, PeerId, PeerStore, Startable, TopologyFilter } from '@libp2p/interface'
|
9
9
|
import type { ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal'
|
10
10
|
|
11
11
|
export interface RelayDiscoveryEvents {
|
@@ -19,7 +19,6 @@ export interface RelayDiscoveryComponents {
|
|
19
19
|
registrar: Registrar
|
20
20
|
logger: ComponentLogger
|
21
21
|
randomWalk: RandomWalk
|
22
|
-
events: TypedEventTarget<Libp2pEvents>
|
23
22
|
}
|
24
23
|
|
25
24
|
export interface RelayDiscoveryInit {
|
@@ -31,7 +30,10 @@ export interface RelayDiscoveryInit {
|
|
31
30
|
* peers that support the circuit v2 HOP protocol.
|
32
31
|
*/
|
33
32
|
export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> implements Startable {
|
34
|
-
private readonly
|
33
|
+
private readonly peerStore: PeerStore
|
34
|
+
private readonly registrar: Registrar
|
35
|
+
private readonly connectionManager: ConnectionManager
|
36
|
+
private readonly randomWalk: RandomWalk
|
35
37
|
private started: boolean
|
36
38
|
private running: boolean
|
37
39
|
private topologyId?: string
|
@@ -44,14 +46,15 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
44
46
|
super()
|
45
47
|
|
46
48
|
this.log = components.logger.forComponent('libp2p:circuit-relay:discover-relays')
|
47
|
-
this.components = components
|
48
49
|
this.started = false
|
49
50
|
this.running = false
|
51
|
+
this.peerStore = components.peerStore
|
52
|
+
this.registrar = components.registrar
|
53
|
+
this.connectionManager = components.connectionManager
|
54
|
+
this.randomWalk = components.randomWalk
|
50
55
|
this.filter = init.filter
|
51
56
|
this.discoveryController = new AbortController()
|
52
57
|
setMaxListeners(Infinity, this.discoveryController.signal)
|
53
|
-
this.dialPeer = this.dialPeer.bind(this)
|
54
|
-
this.onPeer = this.onPeer.bind(this)
|
55
58
|
}
|
56
59
|
|
57
60
|
isStarted (): boolean {
|
@@ -61,7 +64,7 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
61
64
|
async start (): Promise<void> {
|
62
65
|
// register a topology listener for when new peers are encountered
|
63
66
|
// that support the hop protocol
|
64
|
-
this.topologyId = await this.
|
67
|
+
this.topologyId = await this.registrar.register(RELAY_V2_HOP_CODEC, {
|
65
68
|
filter: this.filter,
|
66
69
|
onConnect: (peerId) => {
|
67
70
|
this.log.trace('discovered relay %p queue (length: %d, active %d)', peerId, this.queue?.size, this.queue?.running)
|
@@ -74,13 +77,10 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
74
77
|
|
75
78
|
stop (): void {
|
76
79
|
if (this.topologyId != null) {
|
77
|
-
this.
|
78
|
-
}
|
79
|
-
|
80
|
-
if (this.running) {
|
81
|
-
this.stopDiscovery()
|
80
|
+
this.registrar.unregister(this.topologyId)
|
82
81
|
}
|
83
82
|
|
83
|
+
this.discoveryController?.abort()
|
84
84
|
this.started = false
|
85
85
|
}
|
86
86
|
|
@@ -90,8 +90,7 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
90
90
|
*
|
91
91
|
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected to
|
92
92
|
* 2. Dial and try to listen on the peers we know that support hop but are not connected
|
93
|
-
* 3. Search the network
|
94
|
-
* 4. Dial any peers discovered - this covers when no peer routing implementation has been configured but some peer discovery mechanism is also present
|
93
|
+
* 3. Search the network
|
95
94
|
*/
|
96
95
|
startDiscovery (): void {
|
97
96
|
if (this.running) {
|
@@ -103,14 +102,11 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
103
102
|
this.discoveryController = new AbortController()
|
104
103
|
setMaxListeners(Infinity, this.discoveryController.signal)
|
105
104
|
|
106
|
-
// dial any peer we discover
|
107
|
-
this.components.events.addEventListener('peer:discovery', this.onPeer)
|
108
|
-
|
109
105
|
Promise.resolve()
|
110
106
|
.then(async () => {
|
111
107
|
this.log('searching peer store for relays')
|
112
108
|
|
113
|
-
const peers = (await this.
|
109
|
+
const peers = (await this.peerStore.all({
|
114
110
|
filters: [
|
115
111
|
// filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
|
116
112
|
(peer) => {
|
@@ -153,7 +149,7 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
153
149
|
|
154
150
|
this.log('start random walk')
|
155
151
|
|
156
|
-
for await (const peer of this.
|
152
|
+
for await (const peer of this.randomWalk.walk({ signal: this.discoveryController.signal })) {
|
157
153
|
this.log.trace('found random peer %p', peer.id)
|
158
154
|
|
159
155
|
if (queue.has(peer.id)) {
|
@@ -163,14 +159,14 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
163
159
|
continue
|
164
160
|
}
|
165
161
|
|
166
|
-
if (this.
|
162
|
+
if (this.connectionManager.getConnections(peer.id)?.length > 0) {
|
167
163
|
this.log.trace('random peer %p was already connected', peer.id)
|
168
164
|
|
169
165
|
// skip peers we are already connected to
|
170
166
|
continue
|
171
167
|
}
|
172
168
|
|
173
|
-
if (!(await this.
|
169
|
+
if (!(await this.connectionManager.isDialable(peer.multiaddrs))) {
|
174
170
|
this.log.trace('random peer %p was not dialable', peer.id, peer.multiaddrs.map(ma => ma.toString()))
|
175
171
|
|
176
172
|
// skip peers we can't dial
|
@@ -190,7 +186,16 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
190
186
|
|
191
187
|
// dial the peer - this will cause identify to run and our topology to
|
192
188
|
// be notified and we'll attempt to create reservations
|
193
|
-
queue.add(
|
189
|
+
queue.add(async () => {
|
190
|
+
const signal = anySignal([this.discoveryController.signal, AbortSignal.timeout(5000)])
|
191
|
+
setMaxListeners(Infinity, signal)
|
192
|
+
|
193
|
+
try {
|
194
|
+
await this.connectionManager.openConnection(peer.id, { signal })
|
195
|
+
} finally {
|
196
|
+
signal.clear()
|
197
|
+
}
|
198
|
+
}, {
|
194
199
|
peerId: peer.id,
|
195
200
|
signal: this.discoveryController.signal
|
196
201
|
})
|
@@ -214,70 +219,6 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
214
219
|
this.log('stop discovery')
|
215
220
|
this.running = false
|
216
221
|
this.discoveryController?.abort()
|
217
|
-
this.queue?.clear()
|
218
|
-
|
219
|
-
// stop dialing any peer we discover
|
220
|
-
this.components.events.removeEventListener('peer:discovery', this.onPeer)
|
221
|
-
}
|
222
|
-
|
223
|
-
onPeer (evt: CustomEvent<PeerInfo>): void {
|
224
|
-
this.log.trace('maybe dialing discovered peer %p - %e', evt.detail.id)
|
225
|
-
|
226
|
-
this.maybeDialPeer(evt)
|
227
|
-
.catch(err => {
|
228
|
-
this.log.trace('error dialing discovered peer %p - %e', evt.detail.id, err)
|
229
|
-
})
|
230
|
-
}
|
231
|
-
|
232
|
-
async maybeDialPeer (evt: CustomEvent<PeerInfo>): Promise<void> {
|
233
|
-
if (this.queue == null) {
|
234
|
-
return
|
235
|
-
}
|
236
|
-
|
237
|
-
const peerId = evt.detail.id
|
238
|
-
const multiaddrs = evt.detail.multiaddrs
|
239
|
-
|
240
|
-
if (this.queue.has(peerId)) {
|
241
|
-
this.log.trace('random peer %p was already in queue', peerId)
|
242
|
-
|
243
|
-
// skip peers already in the queue
|
244
|
-
return
|
245
|
-
}
|
246
|
-
|
247
|
-
if (this.components.connectionManager.getConnections(peerId)?.length > 0) {
|
248
|
-
this.log.trace('random peer %p was already connected', peerId)
|
249
|
-
|
250
|
-
// skip peers we are already connected to
|
251
|
-
return
|
252
|
-
}
|
253
|
-
|
254
|
-
if (!(await this.components.connectionManager.isDialable(multiaddrs))) {
|
255
|
-
this.log.trace('random peer %p was not dialable', peerId)
|
256
|
-
|
257
|
-
// skip peers we can't dial
|
258
|
-
return
|
259
|
-
}
|
260
|
-
|
261
|
-
this.queue?.add(this.dialPeer, {
|
262
|
-
peerId: evt.detail.id,
|
263
|
-
signal: this.discoveryController.signal
|
264
|
-
})
|
265
|
-
.catch(err => {
|
266
|
-
this.log.error('error opening connection to discovered peer %p', evt.detail.id, err)
|
267
|
-
})
|
268
|
-
}
|
269
|
-
|
270
|
-
async dialPeer ({ peerId, signal }: { peerId: PeerId, signal?: AbortSignal }): Promise<void> {
|
271
|
-
const combinedSignal = anySignal([AbortSignal.timeout(5_000), signal])
|
272
|
-
setMaxListeners(Infinity, combinedSignal)
|
273
|
-
|
274
|
-
try {
|
275
|
-
await this.components.connectionManager.openConnection(peerId, {
|
276
|
-
signal: combinedSignal
|
277
|
-
})
|
278
|
-
} finally {
|
279
|
-
combinedSignal.clear()
|
280
|
-
}
|
281
222
|
}
|
282
223
|
}
|
283
224
|
|