@waku/core 0.0.22 → 0.0.23
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/CHANGELOG.md +27 -0
- package/bundle/base_protocol-84d9b670.js +1198 -0
- package/bundle/index.js +1132 -1251
- package/bundle/lib/base_protocol.js +2 -116
- package/bundle/lib/message/version_0.js +1 -1
- package/bundle/lib/predefined_bootstrap_nodes.js +6 -6
- package/bundle/{version_0-86411fdf.js → version_0-74b4b9db.js} +875 -794
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +18 -5
- package/dist/lib/base_protocol.js +25 -8
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.js +10 -10
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/filter_rpc.js +4 -4
- package/dist/lib/filter/index.d.ts +4 -0
- package/dist/lib/filter/index.js +15 -11
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/filterPeers.d.ts +10 -0
- package/dist/lib/filterPeers.js +31 -0
- package/dist/lib/filterPeers.js.map +1 -0
- package/dist/lib/keep_alive_manager.d.ts +3 -2
- package/dist/lib/keep_alive_manager.js +27 -8
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.js +62 -33
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/push_rpc.js +2 -2
- package/dist/lib/message/version_0.d.ts +1 -1
- package/dist/lib/message/version_0.js +3 -3
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/predefined_bootstrap_nodes.js +6 -6
- package/dist/lib/store/history_rpc.js +3 -3
- package/dist/lib/store/index.d.ts +0 -5
- package/dist/lib/store/index.js +54 -37
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +15 -0
- package/dist/lib/stream_manager.js +53 -0
- package/dist/lib/stream_manager.js.map +1 -0
- package/dist/lib/to_proto_message.js +1 -1
- package/dist/lib/waku.d.ts +2 -2
- package/dist/lib/waku.js +1 -1
- package/package.json +15 -21
- package/src/index.ts +6 -9
- package/src/lib/base_protocol.ts +49 -18
- package/src/lib/connection_manager.ts +17 -13
- package/src/lib/filter/filter_rpc.ts +4 -4
- package/src/lib/filter/index.ts +21 -22
- package/src/lib/filterPeers.ts +43 -0
- package/src/lib/keep_alive_manager.ts +33 -9
- package/src/lib/light_push/index.ts +103 -47
- package/src/lib/light_push/push_rpc.ts +2 -2
- package/src/lib/message/version_0.ts +8 -5
- package/src/lib/predefined_bootstrap_nodes.ts +7 -7
- package/src/lib/store/history_rpc.ts +4 -4
- package/src/lib/store/index.ts +70 -51
- package/src/lib/stream_manager.ts +69 -0
- package/src/lib/to_proto_message.ts +1 -1
- package/src/lib/wait_for_remote_peer.ts +1 -1
- package/src/lib/waku.ts +3 -3
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/store/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/store/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAS9C,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAC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,GAAG,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC;QAClE;QACE,uEAAuE;QACvE,gBAAgB;YAChB,aAAa;YACb,gBAAgB,GAAG,aAAa,EAChC;YACA,MAAM;SACP;KACF;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAAwB,EACxB,cAAsB,kBAAkB;IAExC,IACE,CAAC,OAAO;QACR,CAAC,OAAO,CAAC,SAAS;QAClB,CAAC,OAAO,CAAC,OAAO;QAChB,CAAC,OAAO,CAAC,YAAY,EACrB;QACA,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;KACvD;IAED,MAAM,iBAAiB,GAAG,WAAW,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAE5D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,iBAAiB,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEpE,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAE1E,OAAO;QACL,MAAM;QACN,WAAW;QACX,UAAU,EAAE,WAAW;QACvB,YAAY,EAAE,WAAW;KAC1B,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CACvB,OAAuC,EAAE;IAEzC,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACrD,CAAC"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import type { Stream } from "@libp2p/interface/connection";
|
2
|
+
import { Peer } from "@libp2p/interface/peer-store";
|
3
|
+
import { Libp2p } from "@waku/interfaces";
|
4
|
+
export declare class StreamManager {
|
5
|
+
multicodec: string;
|
6
|
+
getConnections: Libp2p["getConnections"];
|
7
|
+
addEventListener: Libp2p["addEventListener"];
|
8
|
+
private streamPool;
|
9
|
+
private log;
|
10
|
+
constructor(multicodec: string, getConnections: Libp2p["getConnections"], addEventListener: Libp2p["addEventListener"]);
|
11
|
+
getStream(peer: Peer): Promise<Stream>;
|
12
|
+
private newStream;
|
13
|
+
private prepareNewStream;
|
14
|
+
private handlePeerUpdateStreamPool;
|
15
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
import { selectConnection } from "@waku/utils/libp2p";
|
2
|
+
import debug from "debug";
|
3
|
+
export class StreamManager {
|
4
|
+
multicodec;
|
5
|
+
getConnections;
|
6
|
+
addEventListener;
|
7
|
+
streamPool;
|
8
|
+
log;
|
9
|
+
constructor(multicodec, getConnections, addEventListener) {
|
10
|
+
this.multicodec = multicodec;
|
11
|
+
this.getConnections = getConnections;
|
12
|
+
this.addEventListener = addEventListener;
|
13
|
+
this.log = debug(`waku:stream-manager:${multicodec}`);
|
14
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool.bind(this));
|
15
|
+
this.getStream = this.getStream.bind(this);
|
16
|
+
this.streamPool = new Map();
|
17
|
+
}
|
18
|
+
async getStream(peer) {
|
19
|
+
const peerIdStr = peer.id.toString();
|
20
|
+
const streamPromise = this.streamPool.get(peerIdStr);
|
21
|
+
if (!streamPromise) {
|
22
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
23
|
+
}
|
24
|
+
// We have the stream, let's remove it from the map
|
25
|
+
this.streamPool.delete(peerIdStr);
|
26
|
+
this.prepareNewStream(peer);
|
27
|
+
const stream = await streamPromise;
|
28
|
+
if (stream.status === "closed") {
|
29
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
30
|
+
}
|
31
|
+
return stream;
|
32
|
+
}
|
33
|
+
async newStream(peer) {
|
34
|
+
const connections = this.getConnections(peer.id);
|
35
|
+
const connection = selectConnection(connections);
|
36
|
+
if (!connection) {
|
37
|
+
throw new Error("Failed to get a connection to the peer");
|
38
|
+
}
|
39
|
+
return connection.newStream(this.multicodec);
|
40
|
+
}
|
41
|
+
prepareNewStream(peer) {
|
42
|
+
const streamPromise = this.newStream(peer);
|
43
|
+
this.streamPool.set(peer.id.toString(), streamPromise);
|
44
|
+
}
|
45
|
+
handlePeerUpdateStreamPool = (evt) => {
|
46
|
+
const peer = evt.detail.peer;
|
47
|
+
if (peer.protocols.includes(this.multicodec)) {
|
48
|
+
this.log(`Preemptively opening a stream to ${peer.id.toString()}`);
|
49
|
+
this.prepareNewStream(peer);
|
50
|
+
}
|
51
|
+
};
|
52
|
+
}
|
53
|
+
//# sourceMappingURL=stream_manager.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"stream_manager.js","sourceRoot":"","sources":["../../src/lib/stream_manager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,OAAO,aAAa;IAKf;IACA;IACA;IAND,UAAU,CAA+B;IACzC,GAAG,CAAiB;IAE5B,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,KAAK,CAAC,uBAAuB,UAAU,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,gBAAgB,CACnB,aAAa,EACb,IAAI,CAAC,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC3C,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;IAC9B,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,GA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AoC,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YACnE,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;SAC7B;IACH,CAAC,CAAC;CACH"}
|
package/dist/lib/waku.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import type { Stream } from "@libp2p/interface
|
2
|
-
import { PeerId } from "@libp2p/interface
|
1
|
+
import type { Stream } from "@libp2p/interface/connection";
|
2
|
+
import { PeerId } from "@libp2p/interface/peer-id";
|
3
3
|
import { MultiaddrInput } from "@multiformats/multiaddr";
|
4
4
|
import type { IFilter, ILightPush, IRelay, IStore, Libp2p, Waku } from "@waku/interfaces";
|
5
5
|
import { Protocols } from "@waku/interfaces";
|
package/dist/lib/waku.js
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/core",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.23",
|
4
4
|
"description": "TypeScript implementation of the Waku v2 protocol",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -72,12 +72,12 @@
|
|
72
72
|
"node": ">=16"
|
73
73
|
},
|
74
74
|
"dependencies": {
|
75
|
-
"@noble/hashes": "^1.3.
|
76
|
-
"@waku/interfaces": "0.0.
|
75
|
+
"@noble/hashes": "^1.3.2",
|
76
|
+
"@waku/interfaces": "0.0.18",
|
77
77
|
"@waku/proto": "0.0.5",
|
78
|
-
"@waku/utils": "0.0.
|
78
|
+
"@waku/utils": "0.0.11",
|
79
79
|
"debug": "^4.3.4",
|
80
|
-
"it-all": "^3.0.
|
80
|
+
"it-all": "^3.0.3",
|
81
81
|
"it-length-prefixed": "^9.0.1",
|
82
82
|
"it-pipe": "^3.0.1",
|
83
83
|
"p-event": "^5.0.1",
|
@@ -85,23 +85,18 @@
|
|
85
85
|
"uuid": "^9.0.0"
|
86
86
|
},
|
87
87
|
"devDependencies": {
|
88
|
-
"@libp2p/interface-connection": "^5.1.1",
|
89
|
-
"@libp2p/interface-libp2p": "^3.2.0",
|
90
|
-
"@libp2p/interface-peer-id": "^2.0.2",
|
91
|
-
"@libp2p/interface-peer-store": "^2.0.4",
|
92
|
-
"@libp2p/interface-registrar": "^2.0.12",
|
93
88
|
"@multiformats/multiaddr": "^12.0.0",
|
94
|
-
"@rollup/plugin-commonjs": "^
|
89
|
+
"@rollup/plugin-commonjs": "^25.0.4",
|
95
90
|
"@rollup/plugin-json": "^6.0.0",
|
96
|
-
"@rollup/plugin-node-resolve": "^15.0
|
97
|
-
"@types/chai": "^4.3.
|
98
|
-
"@types/debug": "^4.1.
|
91
|
+
"@rollup/plugin-node-resolve": "^15.1.0",
|
92
|
+
"@types/chai": "^4.3.5",
|
93
|
+
"@types/debug": "^4.1.8",
|
99
94
|
"@types/mocha": "^10.0.1",
|
100
|
-
"@types/uuid": "^9.0.
|
95
|
+
"@types/uuid": "^9.0.3",
|
101
96
|
"@waku/build-utils": "*",
|
102
97
|
"chai": "^4.3.7",
|
103
|
-
"cspell": "^
|
104
|
-
"fast-check": "^3.
|
98
|
+
"cspell": "^7.3.2",
|
99
|
+
"fast-check": "^3.12.0",
|
105
100
|
"ignore-loader": "^0.1.2",
|
106
101
|
"isomorphic-fetch": "^3.0.0",
|
107
102
|
"karma": "^6.4.1",
|
@@ -110,17 +105,16 @@
|
|
110
105
|
"karma-webpack": "^5.0.0",
|
111
106
|
"mocha": "^10.2.0",
|
112
107
|
"npm-run-all": "^4.1.5",
|
113
|
-
"prettier": "^2.8.8",
|
114
108
|
"process": "^0.11.10",
|
115
|
-
"puppeteer": "^
|
116
|
-
"rollup": "^3.
|
109
|
+
"puppeteer": "^21.1.1",
|
110
|
+
"rollup": "^3.29.0",
|
117
111
|
"ts-loader": "^9.4.2",
|
118
112
|
"ts-node": "^10.9.1",
|
119
113
|
"typescript": "^5.0.4"
|
120
114
|
},
|
121
115
|
"peerDependencies": {
|
122
116
|
"@multiformats/multiaddr": "^12.0.0",
|
123
|
-
"libp2p": "^0.
|
117
|
+
"libp2p": "^0.46.3"
|
124
118
|
},
|
125
119
|
"peerDependenciesMeta": {
|
126
120
|
"@multiformats/multiaddr": {
|
package/src/index.ts
CHANGED
@@ -4,7 +4,7 @@ export { createEncoder, createDecoder } from "./lib/message/version_0.js";
|
|
4
4
|
export type {
|
5
5
|
Encoder,
|
6
6
|
Decoder,
|
7
|
-
DecodedMessage
|
7
|
+
DecodedMessage
|
8
8
|
} from "./lib/message/version_0.js";
|
9
9
|
export * as message from "./lib/message/index.js";
|
10
10
|
|
@@ -12,21 +12,18 @@ export * as waku from "./lib/waku.js";
|
|
12
12
|
export { WakuNode, WakuOptions } from "./lib/waku.js";
|
13
13
|
|
14
14
|
export * as waku_filter from "./lib/filter/index.js";
|
15
|
-
export { wakuFilter } from "./lib/filter/index.js";
|
15
|
+
export { wakuFilter, FilterCodecs } from "./lib/filter/index.js";
|
16
16
|
|
17
17
|
export * as waku_light_push from "./lib/light_push/index.js";
|
18
|
-
export { wakuLightPush
|
18
|
+
export { wakuLightPush } from "./lib/light_push/index.js";
|
19
19
|
|
20
20
|
export * as waku_store from "./lib/store/index.js";
|
21
|
-
|
22
|
-
|
23
|
-
wakuStore,
|
24
|
-
StoreCodec,
|
25
|
-
createCursor,
|
26
|
-
} from "./lib/store/index.js";
|
21
|
+
|
22
|
+
export { PageDirection, wakuStore, createCursor } from "./lib/store/index.js";
|
27
23
|
|
28
24
|
export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js";
|
29
25
|
|
30
26
|
export { ConnectionManager } from "./lib/connection_manager.js";
|
31
27
|
|
32
28
|
export { KeepAliveManager } from "./lib/keep_alive_manager.js";
|
29
|
+
export { StreamManager } from "./lib/stream_manager.js";
|
package/src/lib/base_protocol.ts
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
import type {
|
2
|
-
import type {
|
3
|
-
import type { PeerId } from "@libp2p/interface
|
4
|
-
import { Peer, PeerStore } from "@libp2p/interface
|
1
|
+
import type { Libp2p } from "@libp2p/interface";
|
2
|
+
import type { Stream } from "@libp2p/interface/connection";
|
3
|
+
import type { PeerId } from "@libp2p/interface/peer-id";
|
4
|
+
import { Peer, PeerStore } from "@libp2p/interface/peer-store";
|
5
5
|
import type { IBaseProtocol, Libp2pComponents } from "@waku/interfaces";
|
6
|
-
import {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
} from "@waku/utils/libp2p";
|
6
|
+
import { getPeersForProtocol, selectPeerForProtocol } from "@waku/utils/libp2p";
|
7
|
+
|
8
|
+
import { filterPeers } from "./filterPeers.js";
|
9
|
+
import { StreamManager } from "./stream_manager.js";
|
11
10
|
|
12
11
|
/**
|
13
12
|
* A class with predefined helpers, to be used as a base to implement Waku
|
@@ -16,14 +15,29 @@ import {
|
|
16
15
|
export class BaseProtocol implements IBaseProtocol {
|
17
16
|
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
|
18
17
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
18
|
+
protected streamManager: StreamManager;
|
19
19
|
|
20
|
-
constructor(
|
20
|
+
constructor(
|
21
|
+
public multicodec: string,
|
22
|
+
private components: Libp2pComponents
|
23
|
+
) {
|
21
24
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
22
25
|
components.events
|
23
26
|
);
|
24
27
|
this.removeLibp2pEventListener = components.events.removeEventListener.bind(
|
25
28
|
components.events
|
26
29
|
);
|
30
|
+
|
31
|
+
this.streamManager = new StreamManager(
|
32
|
+
multicodec,
|
33
|
+
components.connectionManager.getConnections.bind(
|
34
|
+
components.connectionManager
|
35
|
+
),
|
36
|
+
this.addLibp2pEventListener
|
37
|
+
);
|
38
|
+
}
|
39
|
+
protected async getStream(peer: Peer): Promise<Stream> {
|
40
|
+
return this.streamManager.getStream(peer);
|
27
41
|
}
|
28
42
|
|
29
43
|
public get peerStore(): PeerStore {
|
@@ -47,15 +61,32 @@ export class BaseProtocol implements IBaseProtocol {
|
|
47
61
|
);
|
48
62
|
return peer;
|
49
63
|
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Retrieves a list of peers based on the specified criteria.
|
67
|
+
*
|
68
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
69
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
70
|
+
* @returns A Promise that resolves to an array of peers based on the specified criteria.
|
71
|
+
*/
|
72
|
+
protected async getPeers(
|
73
|
+
{
|
74
|
+
numPeers,
|
75
|
+
maxBootstrapPeers
|
76
|
+
}: {
|
77
|
+
numPeers: number;
|
78
|
+
maxBootstrapPeers: number;
|
79
|
+
} = {
|
80
|
+
maxBootstrapPeers: 1,
|
81
|
+
numPeers: 0
|
57
82
|
}
|
83
|
+
): Promise<Peer[]> {
|
84
|
+
// Retrieve all peers that support the protocol
|
85
|
+
const allPeersForProtocol = await getPeersForProtocol(this.peerStore, [
|
86
|
+
this.multicodec
|
87
|
+
]);
|
58
88
|
|
59
|
-
|
89
|
+
// Filter the peers based on the specified criteria
|
90
|
+
return filterPeers(allPeersForProtocol, numPeers, maxBootstrapPeers);
|
60
91
|
}
|
61
92
|
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import type { PeerId } from "@libp2p/interface
|
2
|
-
import type { PeerInfo } from "@libp2p/interface
|
3
|
-
import type { Peer } from "@libp2p/interface
|
1
|
+
import type { PeerId } from "@libp2p/interface/peer-id";
|
2
|
+
import type { PeerInfo } from "@libp2p/interface/peer-info";
|
3
|
+
import type { Peer } from "@libp2p/interface/peer-store";
|
4
4
|
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
|
5
5
|
import {
|
6
6
|
ConnectionManagerOptions,
|
@@ -9,7 +9,7 @@ import {
|
|
9
9
|
IPeersByDiscoveryEvents,
|
10
10
|
IRelay,
|
11
11
|
KeepAliveOptions,
|
12
|
-
PeersByDiscoveryResult
|
12
|
+
PeersByDiscoveryResult
|
13
13
|
} from "@waku/interfaces";
|
14
14
|
import { Libp2p, Tags } from "@waku/interfaces";
|
15
15
|
import debug from "debug";
|
@@ -92,12 +92,12 @@ export class ConnectionManager
|
|
92
92
|
return {
|
93
93
|
DISCOVERED: {
|
94
94
|
[Tags.BOOTSTRAP]: peersDiscoveredByBootstrap,
|
95
|
-
[Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange
|
95
|
+
[Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange
|
96
96
|
},
|
97
97
|
CONNECTED: {
|
98
98
|
[Tags.BOOTSTRAP]: peersConnectedByBootstrap,
|
99
|
-
[Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange
|
100
|
-
}
|
99
|
+
[Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange
|
100
|
+
}
|
101
101
|
};
|
102
102
|
}
|
103
103
|
|
@@ -113,7 +113,7 @@ export class ConnectionManager
|
|
113
113
|
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
114
114
|
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
115
115
|
maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS,
|
116
|
-
...options
|
116
|
+
...options
|
117
117
|
};
|
118
118
|
|
119
119
|
this.keepAliveManager = new KeepAliveManager(keepAliveOptions, relay);
|
@@ -324,7 +324,7 @@ export class ConnectionManager
|
|
324
324
|
? EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP
|
325
325
|
: EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE,
|
326
326
|
{
|
327
|
-
detail: peerId
|
327
|
+
detail: peerId
|
328
328
|
}
|
329
329
|
)
|
330
330
|
);
|
@@ -340,7 +340,11 @@ export class ConnectionManager
|
|
340
340
|
void (async () => {
|
341
341
|
const peerId = evt.detail;
|
342
342
|
|
343
|
-
this.keepAliveManager.start(
|
343
|
+
this.keepAliveManager.start(
|
344
|
+
peerId,
|
345
|
+
this.libp2p.services.ping,
|
346
|
+
this.libp2p.peerStore
|
347
|
+
);
|
344
348
|
|
345
349
|
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
|
346
350
|
Tags.BOOTSTRAP
|
@@ -361,7 +365,7 @@ export class ConnectionManager
|
|
361
365
|
new CustomEvent<PeerId>(
|
362
366
|
EPeersByDiscoveryEvents.PEER_CONNECT_BOOTSTRAP,
|
363
367
|
{
|
364
|
-
detail: peerId
|
368
|
+
detail: peerId
|
365
369
|
}
|
366
370
|
)
|
367
371
|
);
|
@@ -371,7 +375,7 @@ export class ConnectionManager
|
|
371
375
|
new CustomEvent<PeerId>(
|
372
376
|
EPeersByDiscoveryEvents.PEER_CONNECT_PEER_EXCHANGE,
|
373
377
|
{
|
374
|
-
detail: peerId
|
378
|
+
detail: peerId
|
375
379
|
}
|
376
380
|
)
|
377
381
|
);
|
@@ -382,7 +386,7 @@ export class ConnectionManager
|
|
382
386
|
return (evt: CustomEvent<PeerId>): void => {
|
383
387
|
this.keepAliveManager.stop(evt.detail);
|
384
388
|
};
|
385
|
-
}
|
389
|
+
}
|
386
390
|
};
|
387
391
|
|
388
392
|
/**
|
@@ -42,7 +42,7 @@ export class FilterSubscribeRpc {
|
|
42
42
|
filterSubscribeType:
|
43
43
|
proto.FilterSubscribeRequest.FilterSubscribeType.SUBSCRIBE,
|
44
44
|
pubsubTopic,
|
45
|
-
contentTopics
|
45
|
+
contentTopics
|
46
46
|
});
|
47
47
|
}
|
48
48
|
|
@@ -55,7 +55,7 @@ export class FilterSubscribeRpc {
|
|
55
55
|
filterSubscribeType:
|
56
56
|
proto.FilterSubscribeRequest.FilterSubscribeType.UNSUBSCRIBE,
|
57
57
|
pubsubTopic,
|
58
|
-
contentTopics
|
58
|
+
contentTopics
|
59
59
|
});
|
60
60
|
}
|
61
61
|
|
@@ -65,7 +65,7 @@ export class FilterSubscribeRpc {
|
|
65
65
|
filterSubscribeType:
|
66
66
|
proto.FilterSubscribeRequest.FilterSubscribeType.UNSUBSCRIBE_ALL,
|
67
67
|
pubsubTopic,
|
68
|
-
contentTopics: []
|
68
|
+
contentTopics: []
|
69
69
|
});
|
70
70
|
}
|
71
71
|
|
@@ -75,7 +75,7 @@ export class FilterSubscribeRpc {
|
|
75
75
|
filterSubscribeType:
|
76
76
|
proto.FilterSubscribeRequest.FilterSubscribeType.SUBSCRIBER_PING,
|
77
77
|
pubsubTopic: "",
|
78
|
-
contentTopics: []
|
78
|
+
contentTopics: []
|
79
79
|
});
|
80
80
|
}
|
81
81
|
|
package/src/lib/filter/index.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
import { Stream } from "@libp2p/interface
|
2
|
-
import type {
|
3
|
-
import type {
|
4
|
-
import type { IncomingStreamData } from "@libp2p/interface-registrar";
|
1
|
+
import { Stream } from "@libp2p/interface/connection";
|
2
|
+
import type { Peer } from "@libp2p/interface/peer-store";
|
3
|
+
import type { IncomingStreamData } from "@libp2p/interface-internal/registrar";
|
5
4
|
import type {
|
6
5
|
Callback,
|
7
6
|
ContentTopic,
|
@@ -14,9 +13,8 @@ import type {
|
|
14
13
|
Libp2p,
|
15
14
|
PeerIdStr,
|
16
15
|
ProtocolCreateOptions,
|
17
|
-
ProtocolOptions,
|
18
16
|
PubSubTopic,
|
19
|
-
Unsubscribe
|
17
|
+
Unsubscribe
|
20
18
|
} from "@waku/interfaces";
|
21
19
|
import { WakuMessage } from "@waku/proto";
|
22
20
|
import { groupByContentTopic, toAsyncIterator } from "@waku/utils";
|
@@ -31,7 +29,7 @@ import { DefaultPubSubTopic } from "../constants.js";
|
|
31
29
|
import {
|
32
30
|
FilterPushRpc,
|
33
31
|
FilterSubscribeResponse,
|
34
|
-
FilterSubscribeRpc
|
32
|
+
FilterSubscribeRpc
|
35
33
|
} from "./filter_rpc.js";
|
36
34
|
|
37
35
|
const log = debug("waku:filter:v2");
|
@@ -41,9 +39,9 @@ type SubscriptionCallback<T extends IDecodedMessage> = {
|
|
41
39
|
callback: Callback<T>;
|
42
40
|
};
|
43
41
|
|
44
|
-
const FilterCodecs = {
|
42
|
+
export const FilterCodecs = {
|
45
43
|
SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
|
46
|
-
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
44
|
+
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
47
45
|
};
|
48
46
|
|
49
47
|
class Subscription {
|
@@ -125,7 +123,7 @@ class Subscription {
|
|
125
123
|
// Decoder that decode to different implementations of `IDecodedMessage`
|
126
124
|
const subscriptionCallback = {
|
127
125
|
decoders,
|
128
|
-
callback
|
126
|
+
callback
|
129
127
|
} as unknown as SubscriptionCallback<IDecodedMessage>;
|
130
128
|
|
131
129
|
// The callback and decoder may override previous values, this is on
|
@@ -228,6 +226,7 @@ class Subscription {
|
|
228
226
|
class Filter extends BaseProtocol implements IReceiver {
|
229
227
|
private readonly options: ProtocolCreateOptions;
|
230
228
|
private activeSubscriptions = new Map<string, Subscription>();
|
229
|
+
private readonly NUM_PEERS_PROTOCOL = 1;
|
231
230
|
|
232
231
|
private getActiveSubscription(
|
233
232
|
pubSubTopic: PubSubTopic,
|
@@ -257,31 +256,32 @@ class Filter extends BaseProtocol implements IReceiver {
|
|
257
256
|
this.options = options ?? {};
|
258
257
|
}
|
259
258
|
|
260
|
-
async createSubscription(
|
261
|
-
pubSubTopic?: string,
|
262
|
-
peerId?: PeerId
|
263
|
-
): Promise<Subscription> {
|
259
|
+
async createSubscription(pubSubTopic?: string): Promise<Subscription> {
|
264
260
|
const _pubSubTopic =
|
265
261
|
pubSubTopic ?? this.options.pubSubTopic ?? DefaultPubSubTopic;
|
266
262
|
|
267
|
-
const peer =
|
263
|
+
const peer = (
|
264
|
+
await this.getPeers({
|
265
|
+
maxBootstrapPeers: 1,
|
266
|
+
numPeers: this.NUM_PEERS_PROTOCOL
|
267
|
+
})
|
268
|
+
)[0];
|
268
269
|
|
269
270
|
const subscription =
|
270
271
|
this.getActiveSubscription(_pubSubTopic, peer.id.toString()) ??
|
271
272
|
this.setActiveSubscription(
|
272
273
|
_pubSubTopic,
|
273
274
|
peer.id.toString(),
|
274
|
-
new Subscription(_pubSubTopic, peer, this.
|
275
|
+
new Subscription(_pubSubTopic, peer, this.getStream.bind(this, peer))
|
275
276
|
);
|
276
277
|
|
277
278
|
return subscription;
|
278
279
|
}
|
279
280
|
|
280
281
|
public toSubscriptionIterator<T extends IDecodedMessage>(
|
281
|
-
decoders: IDecoder<T> | IDecoder<T>[]
|
282
|
-
opts?: ProtocolOptions | undefined
|
282
|
+
decoders: IDecoder<T> | IDecoder<T>[]
|
283
283
|
): Promise<IAsyncIterator<T>> {
|
284
|
-
return toAsyncIterator(this, decoders
|
284
|
+
return toAsyncIterator(this, decoders);
|
285
285
|
}
|
286
286
|
|
287
287
|
/**
|
@@ -301,10 +301,9 @@ class Filter extends BaseProtocol implements IReceiver {
|
|
301
301
|
*/
|
302
302
|
async subscribe<T extends IDecodedMessage>(
|
303
303
|
decoders: IDecoder<T> | IDecoder<T>[],
|
304
|
-
callback: Callback<T
|
305
|
-
opts?: ProtocolOptions
|
304
|
+
callback: Callback<T>
|
306
305
|
): Promise<Unsubscribe> {
|
307
|
-
const subscription = await this.createSubscription(
|
306
|
+
const subscription = await this.createSubscription();
|
308
307
|
|
309
308
|
await subscription.subscribe(decoders, callback);
|
310
309
|
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import { Peer } from "@libp2p/interface/peer-store";
|
2
|
+
import { Tags } from "@waku/interfaces";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Retrieves a list of peers based on the specified criteria.
|
6
|
+
*
|
7
|
+
* @param peers - The list of peers to filter from.
|
8
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
9
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
10
|
+
* @returns A Promise that resolves to an array of peers based on the specified criteria.
|
11
|
+
*/
|
12
|
+
export async function filterPeers(
|
13
|
+
peers: Peer[],
|
14
|
+
numPeers: number,
|
15
|
+
maxBootstrapPeers: number
|
16
|
+
): Promise<Peer[]> {
|
17
|
+
// Collect the bootstrap peers up to the specified maximum
|
18
|
+
const bootstrapPeers = peers
|
19
|
+
.filter((peer) => peer.tags.has(Tags.BOOTSTRAP))
|
20
|
+
.slice(0, maxBootstrapPeers);
|
21
|
+
|
22
|
+
// Collect non-bootstrap peers
|
23
|
+
const nonBootstrapPeers = peers.filter(
|
24
|
+
(peer) => !peer.tags.has(Tags.BOOTSTRAP)
|
25
|
+
);
|
26
|
+
|
27
|
+
// If numPeers is 0, return all peers
|
28
|
+
if (numPeers === 0) {
|
29
|
+
return [...bootstrapPeers, ...nonBootstrapPeers];
|
30
|
+
}
|
31
|
+
|
32
|
+
// Initialize the list of selected peers with the bootstrap peers
|
33
|
+
const selectedPeers: Peer[] = [...bootstrapPeers];
|
34
|
+
|
35
|
+
// Fill up to numPeers with remaining random peers if needed
|
36
|
+
while (selectedPeers.length < numPeers && nonBootstrapPeers.length > 0) {
|
37
|
+
const randomIndex = Math.floor(Math.random() * nonBootstrapPeers.length);
|
38
|
+
const randomPeer = nonBootstrapPeers.splice(randomIndex, 1)[0];
|
39
|
+
selectedPeers.push(randomPeer);
|
40
|
+
}
|
41
|
+
|
42
|
+
return selectedPeers;
|
43
|
+
}
|