@waku/core 0.0.33-c50088a.0 → 0.0.33-c9fdfb3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,90 +1,121 @@
1
1
  import { Logger } from "@waku/utils";
2
- import { selectConnection } from "./utils.js";
3
- const CONNECTION_TIMEOUT = 5_000;
4
- const RETRY_BACKOFF_BASE = 1_000;
5
- const MAX_RETRIES = 3;
2
+ import { selectOpenConnection } from "./utils.js";
3
+ const STREAM_LOCK_KEY = "consumed";
6
4
  export class StreamManager {
7
5
  multicodec;
8
6
  getConnections;
9
7
  addEventListener;
10
- streamPool;
11
8
  log;
9
+ ongoingCreation = new Set();
10
+ streamPool = new Map();
12
11
  constructor(multicodec, getConnections, addEventListener) {
13
12
  this.multicodec = multicodec;
14
13
  this.getConnections = getConnections;
15
14
  this.addEventListener = addEventListener;
16
15
  this.log = new Logger(`stream-manager:${multicodec}`);
17
- this.streamPool = new Map();
18
16
  this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
19
17
  }
20
18
  async getStream(peer) {
21
- const peerIdStr = peer.id.toString();
22
- const streamPromise = this.streamPool.get(peerIdStr);
23
- if (!streamPromise) {
24
- return this.createStream(peer);
19
+ const peerId = peer.id.toString();
20
+ const scheduledStream = this.streamPool.get(peerId);
21
+ if (scheduledStream) {
22
+ this.streamPool.delete(peerId);
23
+ await scheduledStream;
25
24
  }
26
- this.streamPool.delete(peerIdStr);
27
- this.prepareStream(peer);
28
- try {
29
- const stream = await streamPromise;
30
- if (stream && stream.status !== "closed") {
31
- return stream;
32
- }
33
- }
34
- catch (error) {
35
- this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
36
- this.log.warn("Attempting to create a new stream for the peer");
25
+ let stream = this.getOpenStreamForCodec(peer.id);
26
+ if (stream) {
27
+ this.log.info(`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
28
+ this.lockStream(peer.id.toString(), stream);
29
+ return stream;
37
30
  }
38
- return this.createStream(peer);
31
+ stream = await this.createStream(peer);
32
+ this.lockStream(peer.id.toString(), stream);
33
+ return stream;
39
34
  }
40
35
  async createStream(peer, retries = 0) {
41
36
  const connections = this.getConnections(peer.id);
42
- const connection = selectConnection(connections);
37
+ const connection = selectOpenConnection(connections);
43
38
  if (!connection) {
44
- throw new Error("Failed to get a connection to the peer");
39
+ throw new Error(`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
40
+ }
41
+ let lastError;
42
+ let stream;
43
+ for (let i = 0; i < retries + 1; i++) {
44
+ try {
45
+ this.log.info(`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
46
+ stream = await connection.newStream(this.multicodec);
47
+ this.log.info(`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
48
+ break;
49
+ }
50
+ catch (error) {
51
+ lastError = error;
52
+ }
53
+ }
54
+ if (!stream) {
55
+ throw new Error(`Failed to create a new stream for ${peer.id.toString()} -- ` +
56
+ lastError);
57
+ }
58
+ return stream;
59
+ }
60
+ async createStreamWithLock(peer) {
61
+ const peerId = peer.id.toString();
62
+ if (this.ongoingCreation.has(peerId)) {
63
+ this.log.info(`Skipping creation of a stream due to lock for peerId=${peerId} multicodec=${this.multicodec}`);
64
+ return;
45
65
  }
46
66
  try {
47
- return await connection.newStream(this.multicodec);
67
+ this.ongoingCreation.add(peerId);
68
+ await this.createStream(peer);
48
69
  }
49
70
  catch (error) {
50
- if (retries < MAX_RETRIES) {
51
- const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
52
- await new Promise((resolve) => setTimeout(resolve, backoff));
53
- return this.createStream(peer, retries + 1);
54
- }
55
- throw new Error(`Failed to create a new stream for ${peer.id.toString()} -- ` + error);
71
+ this.log.error(`Failed to createStreamWithLock:`, error);
56
72
  }
57
- }
58
- prepareStream(peer) {
59
- const timeoutPromise = new Promise((resolve) => setTimeout(resolve, CONNECTION_TIMEOUT));
60
- const streamPromise = Promise.race([
61
- this.createStream(peer),
62
- timeoutPromise.then(() => {
63
- throw new Error("Connection timeout");
64
- })
65
- ]).catch((error) => {
66
- this.log.error(`Failed to prepare a new stream for ${peer.id.toString()} -- `, error);
67
- });
68
- this.streamPool.set(peer.id.toString(), streamPromise);
73
+ finally {
74
+ this.ongoingCreation.delete(peerId);
75
+ }
76
+ return;
69
77
  }
70
78
  handlePeerUpdateStreamPool = (evt) => {
71
79
  const { peer } = evt.detail;
72
- if (peer.protocols.includes(this.multicodec)) {
73
- const isConnected = this.isConnectedTo(peer.id);
74
- if (isConnected) {
75
- this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
76
- this.prepareStream(peer);
77
- }
78
- else {
79
- const peerIdStr = peer.id.toString();
80
- this.streamPool.delete(peerIdStr);
81
- this.log.info(`Removed pending stream for disconnected peer ${peerIdStr}`);
82
- }
80
+ if (!peer.protocols.includes(this.multicodec)) {
81
+ return;
83
82
  }
83
+ const stream = this.getOpenStreamForCodec(peer.id);
84
+ if (stream) {
85
+ return;
86
+ }
87
+ this.scheduleNewStream(peer);
84
88
  };
85
- isConnectedTo(peerId) {
89
+ scheduleNewStream(peer) {
90
+ this.log.info(`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
91
+ // abandon previous attempt
92
+ if (this.streamPool.has(peer.id.toString())) {
93
+ this.streamPool.delete(peer.id.toString());
94
+ }
95
+ this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
96
+ }
97
+ getOpenStreamForCodec(peerId) {
86
98
  const connections = this.getConnections(peerId);
87
- return connections.some((connection) => connection.status === "open");
99
+ const connection = selectOpenConnection(connections);
100
+ if (!connection) {
101
+ return;
102
+ }
103
+ const stream = connection.streams.find((s) => s.protocol === this.multicodec);
104
+ if (!stream) {
105
+ return;
106
+ }
107
+ const isStreamUnusable = ["done", "closed", "closing"].includes(stream.writeStatus || "");
108
+ if (isStreamUnusable || this.isStreamLocked(stream)) {
109
+ return;
110
+ }
111
+ return stream;
112
+ }
113
+ lockStream(peerId, stream) {
114
+ this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
115
+ stream.metadata[STREAM_LOCK_KEY] = true;
116
+ }
117
+ isStreamLocked(stream) {
118
+ return !!stream.metadata[STREAM_LOCK_KEY];
88
119
  }
89
120
  }
90
121
  //# sourceMappingURL=stream_manager.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream_manager.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/stream_manager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,kBAAkB,GAAG,KAAK,CAAC;AACjC,MAAM,kBAAkB,GAAG,KAAK,CAAC;AACjC,MAAM,WAAW,GAAG,CAAC,CAAC;AAEtB,MAAM,OAAO,aAAa;IAKf;IACA;IACA;IANQ,UAAU,CAAsC;IAChD,GAAG,CAAS;IAE7B,YACS,UAAkB,EAClB,cAAwC,EACxC,gBAA4C;QAF5C,eAAU,GAAV,UAAU,CAAQ;QAClB,mBAAc,GAAd,cAAc,CAA0B;QACxC,qBAAgB,GAAhB,gBAAgB,CAA4B;QAEnD,IAAI,CAAC,GAAG,GAAG,IAAI,MAAM,CAAC,kBAAkB,UAAU,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;QAC5B,IAAI,CAAC,gBAAgB,CAAC,aAAa,EAAE,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACxE,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,IAAU;QAC/B,MAAM,SAAS,GAAG,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;QACrC,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAErD,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,OAAO,IAAI,CA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CAAC,CAAC,CAAC;IAC3E,CAAC;IAEO,qBAAqB,CAAC,MAAc;QAC1C,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAChD,MAAM,UAAU,GAAG,oBAAoB,CAAC,WAAW,CAAC,CAAC;QAErD,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,IAAI,CACpC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,IAAI,CAAC,UAAU,CACtC,CAAC;QAEF,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO;QACT,CAAC;QAED,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,QAAQ,CAC7D,MAAM,CAAC,WAAW,IAAI,EAAE,CACzB,CAAC;QACF,IAAI,gBAAgB,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YACpD,OAAO;QACT,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,UAAU,CAAC,MAAc,EAAE,MAAc;QAC/C,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,6BAA6B,MAAM,cAAc,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAC5E,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,GAAG,IAAI,CAAC;IAC1C,CAAC;IAEO,cAAc,CAAC,MAAc;QACnC,OAAO,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;IAC5C,CAAC;CACF"}
@@ -1,2 +1,2 @@
1
1
  import type { Connection } from "@libp2p/interface";
2
- export declare function selectConnection(connections: Connection[]): Connection | undefined;
2
+ export declare function selectOpenConnection(connections: Connection[]): Connection | undefined;
@@ -1,19 +1,7 @@
1
- export function selectConnection(connections) {
2
- if (!connections.length)
3
- return;
4
- if (connections.length === 1)
5
- return connections[0];
6
- let latestConnection;
7
- connections.forEach((connection) => {
8
- if (connection.status === "open") {
9
- if (!latestConnection) {
10
- latestConnection = connection;
11
- }
12
- else if (connection.timeline.open > latestConnection.timeline.open) {
13
- latestConnection = connection;
14
- }
15
- }
16
- });
17
- return latestConnection;
1
+ export function selectOpenConnection(connections) {
2
+ return connections
3
+ .filter((c) => c.status === "open")
4
+ .sort((left, right) => right.timeline.open - left.timeline.open)
5
+ .at(0);
18
6
  }
19
7
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,gBAAgB,CAC9B,WAAyB;IAEzB,IAAI,CAAC,WAAW,CAAC,MAAM;QAAE,OAAO;IAChC,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC;IAEpD,IAAI,gBAAwC,CAAC;IAE7C,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,IAAI,UAAU,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YACjC,IAAI,CAAC,gBAAgB,EAAE,CAAC;gBACtB,gBAAgB,GAAG,UAAU,CAAC;YAChC,CAAC;iBAAM,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,GAAG,gBAAgB,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;gBACrE,gBAAgB,GAAG,UAAU,CAAC;YAChC,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,gBAAgB,CAAC;AAC1B,CAAC"}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,oBAAoB,CAClC,WAAyB;IAEzB,OAAO,WAAW;SACf,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC;SAClC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;SAC/D,EAAE,CAAC,CAAC,CAAC,CAAC;AACX,CAAC"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@waku/core","version":"0.0.33-c50088a.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@libp2p/ping":"^1.1.2","@waku/enr":"0.0.27-c50088a.0","@waku/interfaces":"0.0.28-c50088a.0","@waku/proto":"0.0.9-c50088a.0","@waku/utils":"0.0.21-c50088a.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","p-event":"^6.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.8.1"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
1
+ {"name":"@waku/core","version":"0.0.33-c9fdfb3.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@libp2p/ping":"^1.1.2","@waku/enr":"0.0.27-c9fdfb3.0","@waku/interfaces":"0.0.28-c9fdfb3.0","@waku/proto":"0.0.9-c9fdfb3.0","@waku/utils":"0.0.21-c9fdfb3.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","sinon":"^18.0.0","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.8.1"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
package/src/index.ts CHANGED
@@ -13,9 +13,7 @@ export * as waku_light_push from "./lib/light_push/index.js";
13
13
  export { LightPushCodec, LightPushCore } from "./lib/light_push/index.js";
14
14
 
15
15
  export * as waku_store from "./lib/store/index.js";
16
- export { StoreCore } from "./lib/store/index.js";
17
-
18
- export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js";
16
+ export { StoreCore, StoreCodec } from "./lib/store/index.js";
19
17
 
20
18
  export { ConnectionManager } from "./lib/connection_manager.js";
21
19
 
@@ -46,7 +46,6 @@ export class BaseProtocol implements IBaseProtocolCore {
46
46
  return this.streamManager.getStream(peer);
47
47
  }
48
48
 
49
- //TODO: move to SDK
50
49
  /**
51
50
  * Returns known peers from the address book (`libp2p.peerStore`) that support
52
51
  * the class protocol. Waku may or may not be currently connected to these
@@ -56,17 +55,12 @@ export class BaseProtocol implements IBaseProtocolCore {
56
55
  return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
57
56
  }
58
57
 
59
- public async connectedPeers(withOpenStreams = false): Promise<Peer[]> {
58
+ public async connectedPeers(): Promise<Peer[]> {
60
59
  const peers = await this.allPeers();
61
60
  return peers.filter((peer) => {
62
61
  const connections = this.components.connectionManager.getConnections(
63
62
  peer.id
64
63
  );
65
- if (withOpenStreams) {
66
- return connections.some((c) =>
67
- c.streams.some((s) => s.protocol === this.multicodec)
68
- );
69
- }
70
64
  return connections.length > 0;
71
65
  });
72
66
  }
@@ -37,7 +37,6 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
37
37
  wakuMessage: WakuMessage,
38
38
  peerIdStr: string
39
39
  ) => Promise<void>,
40
- private handleError: (error: Error) => Promise<void>,
41
40
  public readonly pubsubTopics: PubsubTopic[],
42
41
  libp2p: Libp2p
43
42
  ) {
@@ -313,7 +312,6 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
313
312
  "stream ",
314
313
  stream
315
314
  );
316
- await this.handleError(e);
317
315
  }
318
316
  );
319
317
  } catch (e) {
@@ -1,115 +1,173 @@
1
- import type { PeerUpdate, Stream } from "@libp2p/interface";
2
- import type { Peer, PeerId } from "@libp2p/interface";
3
- import { Libp2p } from "@waku/interfaces";
1
+ import type { Peer, PeerId, PeerUpdate, Stream } from "@libp2p/interface";
2
+ import type { Libp2p } from "@waku/interfaces";
4
3
  import { Logger } from "@waku/utils";
5
4
 
6
- import { selectConnection } from "./utils.js";
5
+ import { selectOpenConnection } from "./utils.js";
7
6
 
8
- const CONNECTION_TIMEOUT = 5_000;
9
- const RETRY_BACKOFF_BASE = 1_000;
10
- const MAX_RETRIES = 3;
7
+ const STREAM_LOCK_KEY = "consumed";
11
8
 
12
9
  export class StreamManager {
13
- private readonly streamPool: Map<string, Promise<Stream | void>>;
14
10
  private readonly log: Logger;
15
11
 
12
+ private ongoingCreation: Set<string> = new Set();
13
+ private streamPool: Map<string, Promise<void>> = new Map();
14
+
16
15
  public constructor(
17
- public multicodec: string,
18
- public getConnections: Libp2p["getConnections"],
19
- public addEventListener: Libp2p["addEventListener"]
16
+ private multicodec: string,
17
+ private getConnections: Libp2p["getConnections"],
18
+ private addEventListener: Libp2p["addEventListener"]
20
19
  ) {
21
20
  this.log = new Logger(`stream-manager:${multicodec}`);
22
- this.streamPool = new Map();
23
21
  this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
24
22
  }
25
23
 
26
24
  public async getStream(peer: Peer): Promise<Stream> {
27
- const peerIdStr = peer.id.toString();
28
- const streamPromise = this.streamPool.get(peerIdStr);
25
+ const peerId = peer.id.toString();
26
+
27
+ const scheduledStream = this.streamPool.get(peerId);
29
28
 
30
- if (!streamPromise) {
31
- return this.createStream(peer);
29
+ if (scheduledStream) {
30
+ this.streamPool.delete(peerId);
31
+ await scheduledStream;
32
32
  }
33
33
 
34
- this.streamPool.delete(peerIdStr);
35
- this.prepareStream(peer);
34
+ let stream = this.getOpenStreamForCodec(peer.id);
36
35
 
37
- try {
38
- const stream = await streamPromise;
39
- if (stream && stream.status !== "closed") {
40
- return stream;
41
- }
42
- } catch (error) {
43
- this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
44
- this.log.warn("Attempting to create a new stream for the peer");
36
+ if (stream) {
37
+ this.log.info(
38
+ `Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`
39
+ );
40
+ this.lockStream(peer.id.toString(), stream);
41
+ return stream;
45
42
  }
46
43
 
47
- return this.createStream(peer);
44
+ stream = await this.createStream(peer);
45
+ this.lockStream(peer.id.toString(), stream);
46
+
47
+ return stream;
48
48
  }
49
49
 
50
50
  private async createStream(peer: Peer, retries = 0): Promise<Stream> {
51
51
  const connections = this.getConnections(peer.id);
52
- const connection = selectConnection(connections);
52
+ const connection = selectOpenConnection(connections);
53
53
 
54
54
  if (!connection) {
55
- throw new Error("Failed to get a connection to the peer");
55
+ throw new Error(
56
+ `Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`
57
+ );
56
58
  }
57
59
 
58
- try {
59
- return await connection.newStream(this.multicodec);
60
- } catch (error) {
61
- if (retries < MAX_RETRIES) {
62
- const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
63
- await new Promise((resolve) => setTimeout(resolve, backoff));
64
- return this.createStream(peer, retries + 1);
60
+ let lastError: unknown;
61
+ let stream: Stream | undefined;
62
+
63
+ for (let i = 0; i < retries + 1; i++) {
64
+ try {
65
+ this.log.info(
66
+ `Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
67
+ );
68
+ stream = await connection.newStream(this.multicodec);
69
+ this.log.info(
70
+ `Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
71
+ );
72
+ break;
73
+ } catch (error) {
74
+ lastError = error;
65
75
  }
76
+ }
77
+
78
+ if (!stream) {
66
79
  throw new Error(
67
- `Failed to create a new stream for ${peer.id.toString()} -- ` + error
80
+ `Failed to create a new stream for ${peer.id.toString()} -- ` +
81
+ lastError
68
82
  );
69
83
  }
84
+
85
+ return stream;
70
86
  }
71
87
 
72
- private prepareStream(peer: Peer): void {
73
- const timeoutPromise = new Promise<void>((resolve) =>
74
- setTimeout(resolve, CONNECTION_TIMEOUT)
75
- );
88
+ private async createStreamWithLock(peer: Peer): Promise<void> {
89
+ const peerId = peer.id.toString();
76
90
 
77
- const streamPromise = Promise.race([
78
- this.createStream(peer),
79
- timeoutPromise.then(() => {
80
- throw new Error("Connection timeout");
81
- })
82
- ]).catch((error) => {
83
- this.log.error(
84
- `Failed to prepare a new stream for ${peer.id.toString()} -- `,
85
- error
91
+ if (this.ongoingCreation.has(peerId)) {
92
+ this.log.info(
93
+ `Skipping creation of a stream due to lock for peerId=${peerId} multicodec=${this.multicodec}`
86
94
  );
87
- });
95
+ return;
96
+ }
97
+
98
+ try {
99
+ this.ongoingCreation.add(peerId);
100
+ await this.createStream(peer);
101
+ } catch (error) {
102
+ this.log.error(`Failed to createStreamWithLock:`, error);
103
+ } finally {
104
+ this.ongoingCreation.delete(peerId);
105
+ }
88
106
 
89
- this.streamPool.set(peer.id.toString(), streamPromise);
107
+ return;
90
108
  }
91
109
 
92
110
  private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
93
111
  const { peer } = evt.detail;
94
112
 
95
- if (peer.protocols.includes(this.multicodec)) {
96
- const isConnected = this.isConnectedTo(peer.id);
113
+ if (!peer.protocols.includes(this.multicodec)) {
114
+ return;
115
+ }
116
+
117
+ const stream = this.getOpenStreamForCodec(peer.id);
97
118
 
98
- if (isConnected) {
99
- this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
100
- this.prepareStream(peer);
101
- } else {
102
- const peerIdStr = peer.id.toString();
103
- this.streamPool.delete(peerIdStr);
104
- this.log.info(
105
- `Removed pending stream for disconnected peer ${peerIdStr}`
106
- );
107
- }
119
+ if (stream) {
120
+ return;
108
121
  }
122
+
123
+ this.scheduleNewStream(peer);
109
124
  };
110
125
 
111
- private isConnectedTo(peerId: PeerId): boolean {
126
+ private scheduleNewStream(peer: Peer): void {
127
+ this.log.info(
128
+ `Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
129
+ );
130
+
131
+ // abandon previous attempt
132
+ if (this.streamPool.has(peer.id.toString())) {
133
+ this.streamPool.delete(peer.id.toString());
134
+ }
135
+
136
+ this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
137
+ }
138
+
139
+ private getOpenStreamForCodec(peerId: PeerId): Stream | undefined {
112
140
  const connections = this.getConnections(peerId);
113
- return connections.some((connection) => connection.status === "open");
141
+ const connection = selectOpenConnection(connections);
142
+
143
+ if (!connection) {
144
+ return;
145
+ }
146
+
147
+ const stream = connection.streams.find(
148
+ (s) => s.protocol === this.multicodec
149
+ );
150
+
151
+ if (!stream) {
152
+ return;
153
+ }
154
+
155
+ const isStreamUnusable = ["done", "closed", "closing"].includes(
156
+ stream.writeStatus || ""
157
+ );
158
+ if (isStreamUnusable || this.isStreamLocked(stream)) {
159
+ return;
160
+ }
161
+
162
+ return stream;
163
+ }
164
+
165
+ private lockStream(peerId: string, stream: Stream): void {
166
+ this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
167
+ stream.metadata[STREAM_LOCK_KEY] = true;
168
+ }
169
+
170
+ private isStreamLocked(stream: Stream): boolean {
171
+ return !!stream.metadata[STREAM_LOCK_KEY];
114
172
  }
115
173
  }
@@ -1,22 +1,10 @@
1
1
  import type { Connection } from "@libp2p/interface";
2
2
 
3
- export function selectConnection(
3
+ export function selectOpenConnection(
4
4
  connections: Connection[]
5
5
  ): Connection | undefined {
6
- if (!connections.length) return;
7
- if (connections.length === 1) return connections[0];
8
-
9
- let latestConnection: Connection | undefined;
10
-
11
- connections.forEach((connection) => {
12
- if (connection.status === "open") {
13
- if (!latestConnection) {
14
- latestConnection = connection;
15
- } else if (connection.timeline.open > latestConnection.timeline.open) {
16
- latestConnection = connection;
17
- }
18
- }
19
- });
20
-
21
- return latestConnection;
6
+ return connections
7
+ .filter((c) => c.status === "open")
8
+ .sort((left, right) => right.timeline.open - left.timeline.open)
9
+ .at(0);
22
10
  }
@@ -1,22 +0,0 @@
1
- import type { Waku } from "@waku/interfaces";
2
- import { Protocols } from "@waku/interfaces";
3
- /**
4
- * Wait for a remote peer to be ready given the passed protocols.
5
- * Must be used after attempting to connect to nodes, using
6
- * {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
7
- * {@link @waku/sdk!createLightNode}.
8
- *
9
- * If the passed protocols is a GossipSub protocol, then it resolves only once
10
- * a peer is in a mesh, to help ensure that other peers will send and receive
11
- * message to us.
12
- *
13
- * @param waku The Waku Node
14
- * @param protocols The protocols that need to be enabled by remote peers.
15
- * @param timeoutMs A timeout value in milliseconds..
16
- *
17
- * @returns A promise that **resolves** if all desired protocols are fulfilled by
18
- * remote nodes, **rejects** if the timeoutMs is reached.
19
- * @throws If passing a protocol that is not mounted
20
- * @default Wait for remote peers with protocols enabled locally and no time out is applied.
21
- */
22
- export declare function waitForRemotePeer(waku: Waku, protocols?: Protocols[], timeoutMs?: number): Promise<void>;