@waku/core 0.0.33-c50088a.0 → 0.0.33-d3301ff.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.
- package/bundle/{base_protocol-BUWwtk7E.js → base_protocol-C47QkJ2o.js} +91 -16
- package/bundle/{index-CpCu13gb.js → index-tdQNdKHx.js} +444 -1
- package/bundle/index.js +9 -66
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-CnjebqQa.js → version_0-BrbNEwD-.js} +2 -445
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/base_protocol.d.ts +7 -5
- package/dist/lib/base_protocol.js +15 -15
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -2
- package/dist/lib/filter/index.js +3 -6
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/metadata/index.js +1 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/base_protocol.ts +29 -21
- package/src/lib/filter/index.ts +2 -13
- package/src/lib/metadata/index.ts +1 -1
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"name":"@waku/core","version":"0.0.33-
|
1
|
+
{"name":"@waku/core","version":"0.0.33-d3301ff.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-d3301ff.0","@waku/interfaces":"0.0.28-d3301ff.0","@waku/proto":"0.0.9-d3301ff.0","@waku/utils":"0.0.21-d3301ff.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"]}
|
package/src/lib/base_protocol.ts
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
import type { Libp2p } from "@libp2p/interface";
|
2
|
-
import type { Peer, Stream } from "@libp2p/interface";
|
2
|
+
import type { Peer, PeerStore, Stream } from "@libp2p/interface";
|
3
3
|
import type {
|
4
4
|
IBaseProtocolCore,
|
5
5
|
Libp2pComponents,
|
6
6
|
PubsubTopic
|
7
7
|
} from "@waku/interfaces";
|
8
|
-
import { Logger } from "@waku/utils";
|
9
|
-
import {
|
8
|
+
import { Logger, pubsubTopicsToShardInfo } from "@waku/utils";
|
9
|
+
import {
|
10
|
+
getConnectedPeersForProtocolAndShard,
|
11
|
+
getPeersForProtocol,
|
12
|
+
sortPeersByLatency
|
13
|
+
} from "@waku/utils/libp2p";
|
10
14
|
|
11
15
|
import { filterPeersByDiscovery } from "./filterPeers.js";
|
12
16
|
import { StreamManager } from "./stream_manager/index.js";
|
@@ -22,7 +26,7 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
22
26
|
|
23
27
|
protected constructor(
|
24
28
|
public multicodec: string,
|
25
|
-
|
29
|
+
private components: Libp2pComponents,
|
26
30
|
private log: Logger,
|
27
31
|
public readonly pubsubTopics: PubsubTopic[]
|
28
32
|
) {
|
@@ -46,28 +50,25 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
46
50
|
return this.streamManager.getStream(peer);
|
47
51
|
}
|
48
52
|
|
49
|
-
|
53
|
+
public get peerStore(): PeerStore {
|
54
|
+
return this.components.peerStore;
|
55
|
+
}
|
56
|
+
|
50
57
|
/**
|
51
58
|
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
52
59
|
* the class protocol. Waku may or may not be currently connected to these
|
53
60
|
* peers.
|
54
61
|
*/
|
55
62
|
public async allPeers(): Promise<Peer[]> {
|
56
|
-
return getPeersForProtocol(this.
|
63
|
+
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
57
64
|
}
|
58
65
|
|
59
|
-
public async connectedPeers(
|
66
|
+
public async connectedPeers(): Promise<Peer[]> {
|
60
67
|
const peers = await this.allPeers();
|
61
68
|
return peers.filter((peer) => {
|
62
|
-
|
63
|
-
peer.id
|
69
|
+
return (
|
70
|
+
this.components.connectionManager.getConnections(peer.id).length > 0
|
64
71
|
);
|
65
|
-
if (withOpenStreams) {
|
66
|
-
return connections.some((c) =>
|
67
|
-
c.streams.some((s) => s.protocol === this.multicodec)
|
68
|
-
);
|
69
|
-
}
|
70
|
-
return connections.length > 0;
|
71
72
|
});
|
72
73
|
}
|
73
74
|
|
@@ -76,8 +77,9 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
76
77
|
*
|
77
78
|
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
78
79
|
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
79
|
-
|
80
|
-
|
80
|
+
|
81
|
+
* @returns A list of peers that support the protocol sorted by latency.
|
82
|
+
*/
|
81
83
|
public async getPeers(
|
82
84
|
{
|
83
85
|
numPeers,
|
@@ -86,23 +88,29 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
86
88
|
numPeers: number;
|
87
89
|
maxBootstrapPeers: number;
|
88
90
|
} = {
|
89
|
-
maxBootstrapPeers:
|
91
|
+
maxBootstrapPeers: 1,
|
90
92
|
numPeers: 0
|
91
93
|
}
|
92
94
|
): Promise<Peer[]> {
|
93
95
|
// Retrieve all connected peers that support the protocol & shard (if configured)
|
94
|
-
const
|
96
|
+
const connectedPeersForProtocolAndShard =
|
97
|
+
await getConnectedPeersForProtocolAndShard(
|
98
|
+
this.components.connectionManager.getConnections(),
|
99
|
+
this.peerStore,
|
100
|
+
[this.multicodec],
|
101
|
+
pubsubTopicsToShardInfo(this.pubsubTopics)
|
102
|
+
);
|
95
103
|
|
96
104
|
// Filter the peers based on discovery & number of peers requested
|
97
105
|
const filteredPeers = filterPeersByDiscovery(
|
98
|
-
|
106
|
+
connectedPeersForProtocolAndShard,
|
99
107
|
numPeers,
|
100
108
|
maxBootstrapPeers
|
101
109
|
);
|
102
110
|
|
103
111
|
// Sort the peers by latency
|
104
112
|
const sortedFilteredPeers = await sortPeersByLatency(
|
105
|
-
this.
|
113
|
+
this.peerStore,
|
106
114
|
filteredPeers
|
107
115
|
);
|
108
116
|
|
package/src/lib/filter/index.ts
CHANGED
@@ -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
|
) {
|
@@ -302,18 +301,8 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
302
301
|
() => {
|
303
302
|
log.info("Receiving pipe closed.");
|
304
303
|
},
|
305
|
-
|
306
|
-
log.error(
|
307
|
-
"Error with receiving pipe",
|
308
|
-
e,
|
309
|
-
" -- ",
|
310
|
-
"on peer ",
|
311
|
-
connection.remotePeer.toString(),
|
312
|
-
" -- ",
|
313
|
-
"stream ",
|
314
|
-
stream
|
315
|
-
);
|
316
|
-
await this.handleError(e);
|
304
|
+
(e) => {
|
305
|
+
log.error("Error with receiving pipe", e);
|
317
306
|
}
|
318
307
|
);
|
319
308
|
} catch (e) {
|
@@ -45,7 +45,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
45
45
|
pubsubTopicsToShardInfo(this.pubsubTopics)
|
46
46
|
);
|
47
47
|
|
48
|
-
const peer = await this.
|
48
|
+
const peer = await this.peerStore.get(peerId);
|
49
49
|
if (!peer) {
|
50
50
|
return {
|
51
51
|
shardInfo: null,
|