libp2p 1.0.12 → 1.1.0-528d73781

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.
@@ -1,4 +1,4 @@
1
- import type { Libp2pEvents, ComponentLogger, TypedEventTarget, PeerStore, Startable } from '@libp2p/interface';
1
+ import type { Libp2pEvents, ComponentLogger, TypedEventTarget, PeerStore, Startable, Metrics } from '@libp2p/interface';
2
2
  import type { ConnectionManager } from '@libp2p/interface-internal';
3
3
  interface AutoDialInit {
4
4
  minConnections?: number;
@@ -14,6 +14,7 @@ interface AutoDialComponents {
14
14
  peerStore: PeerStore;
15
15
  events: TypedEventTarget<Libp2pEvents>;
16
16
  logger: ComponentLogger;
17
+ metrics?: Metrics;
17
18
  }
18
19
  export declare class AutoDial implements Startable {
19
20
  private readonly connectionManager;
@@ -1 +1 @@
1
- {"version":3,"file":"auto-dial.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/auto-dial.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAU,eAAe,EAAE,gBAAgB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AACtH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAEnE,UAAU,YAAY;IACpB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,0BAA0B,CAAC,EAAE,MAAM,CAAA;IACnC,+BAA+B,CAAC,EAAE,MAAM,CAAA;CACzC;AAED,UAAU,kBAAkB;IAC1B,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAA;IACtC,MAAM,EAAE,eAA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,5 +1,5 @@
1
1
  import { PeerMap, PeerSet } from '@libp2p/peer-collections';
2
- import { PeerJobQueue } from '@libp2p/utils/peer-job-queue';
2
+ import { PeerQueue } from '@libp2p/utils/peer-queue';
3
3
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
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
5
  const defaultOptions = {
@@ -42,11 +42,13 @@ export class AutoDial {
42
42
  this.log = components.logger.forComponent('libp2p:connection-manager:auto-dial');
43
43
  this.started = false;
44
44
  this.running = false;
45
- this.queue = new PeerJobQueue({
46
- concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
45
+ this.queue = new PeerQueue({
46
+ concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
47
+ metricName: 'libp2p_autodial_queue',
48
+ metrics: components.metrics
47
49
  });
48
- this.queue.addListener('error', (err) => {
49
- this.log.error('error during auto-dial', err);
50
+ this.queue.addEventListener('error', (evt) => {
51
+ this.log.error('error during auto-dial', evt.detail);
50
52
  });
51
53
  // check the min connection limit whenever a peer disconnects
52
54
  components.events.addEventListener('connection:close', () => {
@@ -137,7 +139,7 @@ export class AutoDial {
137
139
  return false;
138
140
  }
139
141
  // remove peers already in the autodial queue
140
- if (this.queue.hasJob(peer.id)) {
142
+ if (this.queue.has(peer.id)) {
141
143
  this.log.trace('not autodialing %p because they are already being autodialed', peer.id);
142
144
  return false;
143
145
  }
@@ -1 +1 @@
1
- {"version":3,"file":"auto-dial.js","sourceRoot":"","sources":["../../../src/connection-manager/auto-dial.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAC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';
2
+ import { Queue, type QueueAddOptions } from '@libp2p/utils/queue';
2
3
  import { type Multiaddr, type Resolver } from '@multiformats/multiaddr';
3
- import PQueue from 'p-queue';
4
- import type { AddressSorter, AbortOptions, PendingDial, ComponentLogger, Connection, ConnectionGater, Metrics, PeerId, PeerStore } from '@libp2p/interface';
4
+ import type { AddressSorter, AbortOptions, ComponentLogger, Connection, ConnectionGater, Metrics, PeerId, PeerStore } from '@libp2p/interface';
5
5
  import type { TransportManager } from '@libp2p/interface-internal';
6
6
  export interface PendingDialTarget {
7
7
  resolve(value: any): void;
@@ -11,8 +11,9 @@ export interface DialOptions extends AbortOptions {
11
11
  priority?: number;
12
12
  force?: boolean;
13
13
  }
14
- interface PendingDialInternal extends PendingDial {
15
- promise: Promise<Connection>;
14
+ interface DialQueueJobOptions extends QueueAddOptions {
15
+ peerId?: PeerId;
16
+ multiaddrs: Set<string>;
16
17
  }
17
18
  interface DialerInit {
18
19
  addressSorter?: AddressSorter;
@@ -31,8 +32,7 @@ interface DialQueueComponents {
31
32
  logger: ComponentLogger;
32
33
  }
33
34
  export declare class DialQueue {
34
- pendingDials: PendingDialInternal[];
35
- queue: PQueue;
35
+ queue: Queue<Connection, DialQueueJobOptions>;
36
36
  private readonly peerId;
37
37
  private readonly peerStore;
38
38
  private readonly connectionGater;
@@ -40,8 +40,6 @@ export declare class DialQueue {
40
40
  private readonly addressSorter;
41
41
  private readonly maxPeerAddrsToDial;
42
42
  private readonly dialTimeout;
43
- private readonly inProgressDialCount?;
44
- private readonly pendingDialCount?;
45
43
  private shutDownController;
46
44
  private readonly connections;
47
45
  private readonly log;
@@ -60,13 +58,12 @@ export declare class DialQueue {
60
58
  * Where a list of multiaddrs is passed, if any contain a peer id then all
61
59
  * multiaddrs in the list must contain the same peer id.
62
60
  *
63
- * The dial to the first address that is successfully able to upgrade a connection
64
- * will be used, all other dials will be aborted when that happens.
61
+ * The dial to the first address that is successfully able to upgrade a
62
+ * connection will be used, all other dials will be aborted when that happens.
65
63
  */
66
64
  dial(peerIdOrMultiaddr: PeerId | Multiaddr | Multiaddr[], options?: DialOptions): Promise<Connection>;
67
- private createDialAbortControllers;
65
+ private createDialAbortController;
68
66
  private calculateMultiaddrs;
69
- private performDial;
70
67
  }
71
68
  export {};
72
69
  //# sourceMappingURL=dial-queue.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dial-queue.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/dial-queue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAElD,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAa,MAAM,yBAAyB,CAAA;AAIlF,OAAO,MAAM,MAAM,SAAS,CAAA;AAW5B,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAU,UAAU,EAAE,eAAe,EAAU,OAAO,EAAE,MAAM,EAAW,SAAS,EAAE,MAAM,mBAAmB,CAAA;AACpL,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAElE,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAAA;IACzB,MAAM,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,CAAA;CACzB;AAED,MAAM,WAAW,WAAY,SAAQ,YAAY;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,UAAU,mBAAoB,SAAQ,WAAW;IAC/C,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,CAAA;CAC7B;AAED,UAAU,UAAU;IAClB,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EA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M,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,EAAE,SAAS,CAAA;IACpB,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,eAAe,EAAE,eAAe,CAAA;IAChC,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,qBAAa,SAAS;IACb,KAAK,EAAE,KAAK,CAAC,UAAU,EAAE,mBAAmB,CAAC,CAAA;IACpD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAiB;IACjD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAQ;IAC3C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;IACpC,OAAO,CAAC,kBAAkB,CAAiB;IAC3C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAuB;IACnD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,UAAU,EAAE,mBAAmB,EAAE,IAAI,GAAE,UAAe;IA+BnE,KAAK,IAAK,IAAI;IAId;;OAEG;IACH,IAAI,IAAK,IAAI;IAKb;;;;;;;;;;;OAWG;IACG,IAAI,CAAE,iBAAiB,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,EAAE,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;IAkJhH,OAAO,CAAC,yBAAyB;YAenB,mBAAmB;CAuIlC"}
@@ -1,16 +1,16 @@
1
- import { AbortError, CodeError, ERR_TIMEOUT, setMaxListeners } from '@libp2p/interface';
1
+ /* eslint-disable max-depth */
2
+ import { CodeError, AggregateCodeError, ERR_TIMEOUT, setMaxListeners } from '@libp2p/interface';
2
3
  import { PeerMap } from '@libp2p/peer-collections';
3
4
  import { defaultAddressSort } from '@libp2p/utils/address-sort';
4
- import { resolvers } from '@multiformats/multiaddr';
5
+ import { Queue } from '@libp2p/utils/queue';
6
+ import { resolvers, multiaddr } from '@multiformats/multiaddr';
5
7
  import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers';
6
8
  import { anySignal } from 'any-signal';
7
- import pDefer from 'p-defer';
8
- import PQueue from 'p-queue';
9
9
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
10
10
  import { codes } from '../errors.js';
11
11
  import { getPeerAddress } from '../get-peer.js';
12
12
  import { DIAL_TIMEOUT, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, LAST_DIAL_FAILURE_KEY } from './constants.js';
13
- import { combineSignals, resolveMultiaddrs } from './utils.js';
13
+ import { resolveMultiaddrs } from './utils.js';
14
14
  const defaultOptions = {
15
15
  addressSorter: defaultAddressSort,
16
16
  maxParallelDials: MAX_PARALLEL_DIALS,
@@ -21,7 +21,6 @@ const defaultOptions = {
21
21
  }
22
22
  };
23
23
  export class DialQueue {
24
- pendingDials;
25
24
  queue;
26
25
  peerId;
27
26
  peerStore;
@@ -30,8 +29,6 @@ export class DialQueue {
30
29
  addressSorter;
31
30
  maxPeerAddrsToDial;
32
31
  dialTimeout;
33
- inProgressDialCount;
34
- pendingDialCount;
35
32
  shutDownController;
36
33
  connections;
37
34
  log;
@@ -47,46 +44,18 @@ export class DialQueue {
47
44
  this.transportManager = components.transportManager;
48
45
  this.shutDownController = new AbortController();
49
46
  setMaxListeners(Infinity, this.shutDownController.signal);
50
- this.pendingDialCount = components.metrics?.registerMetric('libp2p_dial_queue_pending_dials');
51
- this.inProgressDialCount = components.metrics?.registerMetric('libp2p_dial_queue_in_progress_dials');
52
- this.pendingDials = [];
53
47
  for (const [key, value] of Object.entries(init.resolvers ?? {})) {
54
48
  resolvers.set(key, value);
55
49
  }
56
50
  // controls dial concurrency
57
- this.queue = new PQueue({
58
- concurrency: init.maxParallelDials ?? defaultOptions.maxParallelDials
59
- });
60
- // a job was added to the queue
61
- this.queue.on('add', () => {
62
- this.pendingDialCount?.update(this.queue.size);
63
- this.inProgressDialCount?.update(this.queue.pending);
64
- });
65
- // a queued job started
66
- this.queue.on('active', () => {
67
- this.pendingDialCount?.update(this.queue.size);
68
- this.inProgressDialCount?.update(this.queue.pending);
69
- });
70
- // a started job completed without error
71
- this.queue.on('completed', () => {
72
- this.pendingDialCount?.update(this.queue.size);
73
- this.inProgressDialCount?.update(this.queue.pending);
51
+ this.queue = new Queue({
52
+ concurrency: init.maxParallelDials ?? defaultOptions.maxParallelDials,
53
+ metricName: 'libp2p_dial_queue',
54
+ metrics: components.metrics
74
55
  });
75
56
  // a started job errored
76
- this.queue.on('error', (err) => {
77
- this.log.error('error in dial queue', err);
78
- this.pendingDialCount?.update(this.queue.size);
79
- this.inProgressDialCount?.update(this.queue.pending);
80
- });
81
- // all queued jobs have been started
82
- this.queue.on('empty', () => {
83
- this.pendingDialCount?.update(this.queue.size);
84
- this.inProgressDialCount?.update(this.queue.pending);
85
- });
86
- // add started jobs have run and the queue is empty
87
- this.queue.on('idle', () => {
88
- this.pendingDialCount?.update(this.queue.size);
89
- this.inProgressDialCount?.update(this.queue.pending);
57
+ this.queue.addEventListener('error', (event) => {
58
+ this.log.error('error in dial queue', event.detail);
90
59
  });
91
60
  }
92
61
  start() {
@@ -97,7 +66,7 @@ export class DialQueue {
97
66
  */
98
67
  stop() {
99
68
  this.shutDownController.abort();
100
- this.queue.clear();
69
+ this.queue.abort();
101
70
  }
102
71
  /**
103
72
  * Connects to a given peer, multiaddr or list of multiaddrs.
@@ -108,38 +77,22 @@ export class DialQueue {
108
77
  * Where a list of multiaddrs is passed, if any contain a peer id then all
109
78
  * multiaddrs in the list must contain the same peer id.
110
79
  *
111
- * The dial to the first address that is successfully able to upgrade a connection
112
- * will be used, all other dials will be aborted when that happens.
80
+ * The dial to the first address that is successfully able to upgrade a
81
+ * connection will be used, all other dials will be aborted when that happens.
113
82
  */
114
83
  async dial(peerIdOrMultiaddr, options = {}) {
115
84
  const { peerId, multiaddrs } = getPeerAddress(peerIdOrMultiaddr);
116
- const addrs = multiaddrs.map(multiaddr => ({
117
- multiaddr,
118
- isCertified: false
119
- }));
120
- // create abort conditions - need to do this before `calculateMultiaddrs` as we may be about to
121
- // resolve a dns addr which can time out
122
- const signal = this.createDialAbortControllers(options.signal);
123
- let addrsToDial;
124
- try {
125
- // load addresses from address book, resolve and dnsaddrs, filter undiallables, add peer IDs, etc
126
- addrsToDial = await this.calculateMultiaddrs(peerId, addrs, {
127
- ...options,
128
- signal
129
- });
130
- }
131
- catch (err) {
132
- signal.clear();
133
- throw err;
134
- }
135
85
  // make sure we don't have an existing connection to any of the addresses we
136
86
  // are about to dial
137
- let existingConnection = Array.from(this.connections.values()).flat().find(conn => {
87
+ const existingConnection = Array.from(this.connections.values()).flat().find(conn => {
138
88
  if (options.force === true) {
139
89
  return false;
140
90
  }
141
- return addrsToDial.find(addr => {
142
- return addr.multiaddr.equals(conn.remoteAddr);
91
+ if (conn.remotePeer.equals(peerId)) {
92
+ return true;
93
+ }
94
+ return multiaddrs.find(addr => {
95
+ return addr.equals(conn.remoteAddr);
143
96
  });
144
97
  });
145
98
  if (existingConnection != null) {
@@ -148,84 +101,107 @@ export class DialQueue {
148
101
  }
149
102
  // ready to dial, all async work finished - make sure we don't have any
150
103
  // pending dials in progress for this peer or set of multiaddrs
151
- const existingDial = this.pendingDials.find(dial => {
152
- // is the dial for the same peer id?
153
- if (dial.peerId != null && peerId != null && dial.peerId.equals(peerId)) {
104
+ const existingDial = this.queue.queue.find(job => {
105
+ if (peerId?.equals(job.options.peerId) === true) {
154
106
  return true;
155
107
  }
156
- // is the dial for the same set of multiaddrs?
157
- if (addrsToDial.map(({ multiaddr }) => multiaddr.toString()).join() === dial.multiaddrs.map(multiaddr => multiaddr.toString()).join()) {
158
- return true;
108
+ // does the dial contain any of the target multiaddrs?
109
+ const addresses = job.options.multiaddrs;
110
+ if (addresses == null) {
111
+ return false;
112
+ }
113
+ for (const multiaddr of multiaddrs) {
114
+ if (addresses.has(multiaddr.toString())) {
115
+ return true;
116
+ }
159
117
  }
160
118
  return false;
161
119
  });
162
120
  if (existingDial != null) {
163
121
  this.log('joining existing dial target for %p', peerId);
164
- signal.clear();
165
- return existingDial.promise;
122
+ // add all multiaddrs to the dial target
123
+ for (const multiaddr of multiaddrs) {
124
+ existingDial.options.multiaddrs.add(multiaddr.toString());
125
+ }
126
+ return existingDial.join(options);
166
127
  }
167
- this.log('creating dial target for', addrsToDial.map(({ multiaddr }) => multiaddr.toString()));
168
- // @ts-expect-error .promise property is set below
169
- const pendingDial = {
170
- id: randomId(),
171
- status: 'queued',
172
- peerId,
173
- multiaddrs: addrsToDial.map(({ multiaddr }) => multiaddr)
174
- };
175
- pendingDial.promise = this.performDial(pendingDial, {
176
- ...options,
177
- signal
178
- })
179
- .finally(() => {
180
- // remove our pending dial entry
181
- this.pendingDials = this.pendingDials.filter(p => p.id !== pendingDial.id);
182
- // clean up abort signals/controllers
183
- signal.clear();
184
- })
185
- .catch(async (err) => {
186
- this.log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err);
187
- if (peerId != null) {
188
- // record the last failed dial
189
- try {
190
- await this.peerStore.patch(peerId, {
191
- metadata: {
192
- [LAST_DIAL_FAILURE_KEY]: uint8ArrayFromString(Date.now().toString())
128
+ this.log('creating dial target for %p', peerId, multiaddrs.map(ma => ma.toString()));
129
+ return this.queue.add(async (options) => {
130
+ // create abort conditions - need to do this before `calculateMultiaddrs` as
131
+ // we may be about to resolve a dns addr which can time out
132
+ const signal = this.createDialAbortController(options?.signal);
133
+ let addrsToDial;
134
+ try {
135
+ // load addresses from address book, resolve and dnsaddrs, filter
136
+ // undiallables, add peer IDs, etc
137
+ addrsToDial = await this.calculateMultiaddrs(peerId, options?.multiaddrs, {
138
+ ...options,
139
+ signal
140
+ });
141
+ addrsToDial.map(({ multiaddr }) => multiaddr.toString()).forEach(addr => {
142
+ options?.multiaddrs.add(addr);
143
+ });
144
+ }
145
+ catch (err) {
146
+ signal.clear();
147
+ throw err;
148
+ }
149
+ try {
150
+ let dialed = 0;
151
+ const errors = [];
152
+ for (const address of addrsToDial) {
153
+ if (dialed === this.maxPeerAddrsToDial) {
154
+ this.log('dialed %d addresses for %p, not trying any others', dialed, peerId);
155
+ throw new CodeError('Peer had more than maxPeerAddrsToDial', codes.ERR_TOO_MANY_ADDRESSES);
156
+ }
157
+ dialed++;
158
+ try {
159
+ const conn = await this.transportManager.dial(address.multiaddr, {
160
+ ...options,
161
+ signal
162
+ });
163
+ this.log('dial to %a succeeded', address.multiaddr);
164
+ return conn;
165
+ }
166
+ catch (err) {
167
+ this.log.error('dial failed to %a', address.multiaddr, err);
168
+ if (peerId != null) {
169
+ // record the failed dial
170
+ try {
171
+ await this.peerStore.patch(peerId, {
172
+ metadata: {
173
+ [LAST_DIAL_FAILURE_KEY]: uint8ArrayFromString(Date.now().toString())
174
+ }
175
+ });
176
+ }
177
+ catch (err) {
178
+ this.log.error('could not update last dial failure key for %p', peerId, err);
179
+ }
193
180
  }
194
- });
181
+ // the user/dial timeout/shutdown controller signal aborted
182
+ if (signal.aborted) {
183
+ throw new CodeError(err.message, ERR_TIMEOUT);
184
+ }
185
+ errors.push(err);
186
+ }
195
187
  }
196
- catch (err) {
197
- this.log.error('could not update last dial failure key for %p', peerId, err);
188
+ if (errors.length === 1) {
189
+ throw errors[0];
198
190
  }
191
+ throw new AggregateCodeError(errors, 'All multiaddr dials failed', codes.ERR_TRANSPORT_DIAL_FAILED);
199
192
  }
200
- // Error is a timeout
201
- if (signal.aborted) {
202
- const error = new CodeError(err.message, ERR_TIMEOUT);
203
- throw error;
204
- }
205
- throw err;
206
- });
207
- // let other dials join this one
208
- this.pendingDials.push(pendingDial);
209
- const connection = await pendingDial.promise;
210
- // we may have been dialing a multiaddr without a peer id attached but by
211
- // this point we have upgraded the connection so the remote peer information
212
- // should be available - check again that we don't already have a connection
213
- // to the remote multiaddr
214
- existingConnection = Array.from(this.connections.values()).flat().find(conn => {
215
- if (options.force === true) {
216
- return false;
193
+ finally {
194
+ // clean up abort signals/controllers
195
+ signal.clear();
217
196
  }
218
- return conn.id !== connection.id && conn.remoteAddr.equals(connection.remoteAddr);
197
+ }, {
198
+ peerId,
199
+ priority: options.priority,
200
+ multiaddrs: new Set(multiaddrs.map(ma => ma.toString())),
201
+ signal: options.signal
219
202
  });
220
- if (existingConnection != null) {
221
- this.log('already connected to %a', existingConnection.remoteAddr);
222
- await connection.close();
223
- return existingConnection;
224
- }
225
- this.log('connection opened to %a', connection.remoteAddr);
226
- return connection;
227
203
  }
228
- createDialAbortControllers(userSignal) {
204
+ createDialAbortController(userSignal) {
229
205
  // let any signal abort the dial
230
206
  const signal = anySignal([
231
207
  AbortSignal.timeout(this.dialTimeout),
@@ -237,7 +213,11 @@ export class DialQueue {
237
213
  return signal;
238
214
  }
239
215
  // eslint-disable-next-line complexity
240
- async calculateMultiaddrs(peerId, addrs = [], options = {}) {
216
+ async calculateMultiaddrs(peerId, multiaddrs = new Set(), options = {}) {
217
+ const addrs = [...multiaddrs].map(ma => ({
218
+ multiaddr: multiaddr(ma),
219
+ isCertified: false
220
+ }));
241
221
  // if a peer id or multiaddr(s) with a peer id, make sure it isn't our peer id and that we are allowed to dial it
242
222
  if (peerId != null) {
243
223
  if (this.peerId.equals(peerId)) {
@@ -321,18 +301,10 @@ export class DialQueue {
321
301
  dedupedAddrs.set(maStr, addr);
322
302
  }
323
303
  const dedupedMultiaddrs = [...dedupedAddrs.values()];
324
- if (dedupedMultiaddrs.length === 0 || dedupedMultiaddrs.length > this.maxPeerAddrsToDial) {
325
- this.log('addresses for %p before filtering', peerId ?? 'unknown peer', resolvedAddresses.map(({ multiaddr }) => multiaddr.toString()));
326
- this.log('addresses for %p after filtering', peerId ?? 'unknown peer', dedupedMultiaddrs.map(({ multiaddr }) => multiaddr.toString()));
327
- }
328
304
  // make sure we actually have some addresses to dial
329
305
  if (dedupedMultiaddrs.length === 0) {
330
306
  throw new CodeError('The dial request has no valid addresses', codes.ERR_NO_VALID_ADDRESSES);
331
307
  }
332
- // make sure we don't have too many addresses to dial
333
- if (dedupedMultiaddrs.length > this.maxPeerAddrsToDial) {
334
- throw new CodeError('dial with more addresses than allowed', codes.ERR_TOO_MANY_ADDRESSES);
335
- }
336
308
  const gatedAdrs = [];
337
309
  for (const addr of dedupedMultiaddrs) {
338
310
  if (this.connectionGater.denyDialMultiaddr != null && await this.connectionGater.denyDialMultiaddr(addr.multiaddr)) {
@@ -345,110 +317,9 @@ export class DialQueue {
345
317
  if (sortedGatedAddrs.length === 0) {
346
318
  throw new CodeError('The connection gater denied all addresses in the dial request', codes.ERR_NO_VALID_ADDRESSES);
347
319
  }
320
+ this.log.trace('addresses for %p before filtering', peerId ?? 'unknown peer', resolvedAddresses.map(({ multiaddr }) => multiaddr.toString()));
321
+ this.log.trace('addresses for %p after filtering', peerId ?? 'unknown peer', sortedGatedAddrs.map(({ multiaddr }) => multiaddr.toString()));
348
322
  return sortedGatedAddrs;
349
323
  }
350
- async performDial(pendingDial, options = {}) {
351
- const dialAbortControllers = pendingDial.multiaddrs.map(() => new AbortController());
352
- try {
353
- // internal peer dial queue - only one dial per peer at a time
354
- const peerDialQueue = new PQueue({ concurrency: 1 });
355
- peerDialQueue.on('error', (err) => {
356
- this.log.error('error dialing %s %o', pendingDial.multiaddrs, err);
357
- });
358
- const conn = await Promise.any(pendingDial.multiaddrs.map(async (addr, i) => {
359
- const controller = dialAbortControllers[i];
360
- if (controller == null) {
361
- throw new CodeError('dialAction did not come with an AbortController', codes.ERR_INVALID_PARAMETERS);
362
- }
363
- // let any signal abort the dial
364
- const signal = combineSignals(controller.signal, options.signal);
365
- signal.addEventListener('abort', () => {
366
- this.log('dial to %a aborted', addr);
367
- });
368
- const deferred = pDefer();
369
- await peerDialQueue.add(async () => {
370
- if (signal.aborted) {
371
- this.log('dial to %a was aborted before reaching the head of the peer dial queue', addr);
372
- deferred.reject(new AbortError());
373
- return;
374
- }
375
- // add the individual dial to the dial queue so we don't breach maxConcurrentDials
376
- await this.queue.add(async () => {
377
- try {
378
- if (signal.aborted) {
379
- this.log('dial to %a was aborted before reaching the head of the dial queue', addr);
380
- deferred.reject(new AbortError());
381
- return;
382
- }
383
- // update dial status
384
- pendingDial.status = 'active';
385
- const conn = await this.transportManager.dial(addr, {
386
- ...options,
387
- signal
388
- });
389
- if (controller.signal.aborted) {
390
- // another dial succeeded faster than this one
391
- this.log('multiple dials succeeded, closing superfluous connection');
392
- conn.close().catch(err => {
393
- this.log.error('error closing superfluous connection', err);
394
- });
395
- deferred.reject(new AbortError());
396
- return;
397
- }
398
- // remove the successful AbortController so it is not aborted
399
- dialAbortControllers[i] = undefined;
400
- // immediately abort any other dials
401
- dialAbortControllers.forEach(c => {
402
- if (c !== undefined) {
403
- c.abort();
404
- }
405
- });
406
- this.log('dial to %a succeeded', addr);
407
- // resolve the connection promise
408
- deferred.resolve(conn);
409
- }
410
- catch (err) {
411
- // something only went wrong if our signal was not aborted
412
- this.log.error('error during dial of %a', addr, err);
413
- deferred.reject(err);
414
- }
415
- }, {
416
- ...options,
417
- signal
418
- }).catch(err => {
419
- deferred.reject(err);
420
- });
421
- }, {
422
- signal
423
- }).catch(err => {
424
- deferred.reject(err);
425
- }).finally(() => {
426
- signal.clear();
427
- });
428
- return deferred.promise;
429
- }));
430
- // dial succeeded or failed
431
- if (conn == null) {
432
- throw new CodeError('successful dial led to empty object returned from peer dial queue', codes.ERR_TRANSPORT_DIAL_FAILED);
433
- }
434
- pendingDial.status = 'success';
435
- return conn;
436
- }
437
- catch (err) {
438
- pendingDial.status = 'error';
439
- // if we only dialled one address, unwrap the AggregateError to provide more
440
- // useful feedback to the user
441
- if (pendingDial.multiaddrs.length === 1 && err.name === 'AggregateError') {
442
- throw err.errors[0];
443
- }
444
- throw err;
445
- }
446
- }
447
- }
448
- /**
449
- * Returns a random string
450
- */
451
- function randomId() {
452
- return `${(parseInt(String(Math.random() * 1e9), 10)).toString()}${Date.now()}`;
453
324
  }
454
325
  //# sourceMappingURL=dial-queue.js.map