@waku/core 0.0.26 → 0.0.28-070b625.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/CHANGELOG.md +26 -0
- package/bundle/{base_protocol-pDODy0G6.js → base_protocol-D0Zdzb-v.js} +134 -89
- package/bundle/{browser-mTOOnVZp.js → browser-DoQRY-an.js} +518 -712
- package/bundle/{index-cmONXM-V.js → index-BJwgMx4y.js} +116 -88
- package/bundle/index.js +2967 -21667
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +17 -17
- package/bundle/version_0-C6o0DvNW.js +4055 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +3 -6
- package/dist/index.js +3 -6
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +15 -13
- package/dist/lib/base_protocol.js +35 -22
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +2 -2
- package/dist/lib/connection_manager.js +16 -6
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -1
- package/dist/lib/filter/index.js +144 -82
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/filterPeers.d.ts +8 -5
- package/dist/lib/filterPeers.js +12 -5
- package/dist/lib/filterPeers.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +2 -3
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +12 -2
- package/dist/lib/light_push/index.js +80 -80
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/message/version_0.js +1 -1
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +2 -2
- package/dist/lib/metadata/index.js +58 -16
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/predefined_bootstrap_nodes.d.ts +11 -11
- package/dist/lib/predefined_bootstrap_nodes.js +16 -16
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/store/history_rpc.js +1 -1
- package/dist/lib/store/history_rpc.js.map +1 -1
- package/dist/lib/store/index.d.ts +14 -6
- package/dist/lib/store/index.js +51 -235
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +2 -2
- package/dist/lib/stream_manager.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.d.ts +1 -1
- package/dist/lib/wait_for_remote_peer.js +42 -10
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/package.json +1 -127
- package/src/index.ts +3 -7
- package/src/lib/base_protocol.ts +57 -37
- package/src/lib/connection_manager.ts +17 -10
- package/src/lib/filter/index.ts +234 -136
- package/src/lib/filterPeers.ts +15 -7
- package/src/lib/keep_alive_manager.ts +2 -3
- package/src/lib/light_push/index.ts +104 -124
- package/src/lib/metadata/index.ts +92 -30
- package/src/lib/predefined_bootstrap_nodes.ts +22 -22
- package/src/lib/store/index.ts +79 -344
- package/src/lib/stream_manager.ts +2 -3
- package/src/lib/wait_for_remote_peer.ts +68 -12
- package/bundle/version_0-LQTFNC7k.js +0 -5008
- package/dist/lib/waku.d.ts +0 -57
- package/dist/lib/waku.js +0 -130
- package/dist/lib/waku.js.map +0 -1
- package/src/lib/waku.ts +0 -214
package/src/index.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
export { DefaultUserAgent } from "./lib/waku.js";
|
2
1
|
export { createEncoder, createDecoder } from "./lib/message/version_0.js";
|
3
2
|
export type {
|
4
3
|
Encoder,
|
@@ -7,19 +6,16 @@ export type {
|
|
7
6
|
} from "./lib/message/version_0.js";
|
8
7
|
export * as message from "./lib/message/index.js";
|
9
8
|
|
10
|
-
export * as waku from "./lib/waku.js";
|
11
|
-
export { WakuNode, WakuOptions } from "./lib/waku.js";
|
12
|
-
|
13
9
|
export * as waku_filter from "./lib/filter/index.js";
|
14
10
|
export { wakuFilter, FilterCodecs } from "./lib/filter/index.js";
|
15
11
|
|
16
12
|
export * as waku_light_push from "./lib/light_push/index.js";
|
17
|
-
export { LightPushCodec } from "./lib/light_push/index.js";
|
18
|
-
export { wakuLightPush } from "./lib/light_push/index.js";
|
13
|
+
export { LightPushCodec, LightPushCore } from "./lib/light_push/index.js";
|
19
14
|
|
20
15
|
export * as waku_store from "./lib/store/index.js";
|
16
|
+
export { StoreCore } from "./lib/store/index.js";
|
21
17
|
|
22
|
-
export { PageDirection
|
18
|
+
export { PageDirection } from "./lib/store/index.js";
|
23
19
|
|
24
20
|
export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js";
|
25
21
|
|
package/src/lib/base_protocol.ts
CHANGED
@@ -1,36 +1,36 @@
|
|
1
1
|
import type { Libp2p } from "@libp2p/interface";
|
2
|
-
import type { Stream } from "@libp2p/interface
|
3
|
-
import type { PeerId } from "@libp2p/interface/peer-id";
|
4
|
-
import { Peer, PeerStore } from "@libp2p/interface/peer-store";
|
2
|
+
import type { Peer, PeerStore, Stream } from "@libp2p/interface";
|
5
3
|
import type {
|
6
|
-
|
4
|
+
IBaseProtocolCore,
|
7
5
|
Libp2pComponents,
|
8
6
|
ProtocolCreateOptions,
|
9
7
|
PubsubTopic
|
10
8
|
} from "@waku/interfaces";
|
11
|
-
import {
|
12
|
-
import { shardInfoToPubsubTopics } from "@waku/utils";
|
9
|
+
import { ensureShardingConfigured, Logger } from "@waku/utils";
|
13
10
|
import {
|
14
|
-
|
11
|
+
getConnectedPeersForProtocolAndShard,
|
15
12
|
getPeersForProtocol,
|
16
|
-
|
13
|
+
sortPeersByLatency
|
17
14
|
} from "@waku/utils/libp2p";
|
18
15
|
|
19
|
-
import {
|
16
|
+
import { filterPeersByDiscovery } from "./filterPeers.js";
|
20
17
|
import { StreamManager } from "./stream_manager.js";
|
21
18
|
|
22
19
|
/**
|
23
20
|
* A class with predefined helpers, to be used as a base to implement Waku
|
24
21
|
* Protocols.
|
25
22
|
*/
|
26
|
-
export class BaseProtocol implements
|
23
|
+
export class BaseProtocol implements IBaseProtocolCore {
|
27
24
|
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
|
28
25
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
29
26
|
protected streamManager: StreamManager;
|
30
27
|
|
31
28
|
constructor(
|
32
29
|
public multicodec: string,
|
33
|
-
private components: Libp2pComponents
|
30
|
+
private components: Libp2pComponents,
|
31
|
+
private log: Logger,
|
32
|
+
public readonly pubsubTopics: PubsubTopic[],
|
33
|
+
private options?: ProtocolCreateOptions
|
34
34
|
) {
|
35
35
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
36
36
|
components.events
|
@@ -60,27 +60,28 @@ export class BaseProtocol implements IBaseProtocol {
|
|
60
60
|
* the class protocol. Waku may or may not be currently connected to these
|
61
61
|
* peers.
|
62
62
|
*/
|
63
|
-
public async
|
63
|
+
public async allPeers(): Promise<Peer[]> {
|
64
64
|
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
65
65
|
}
|
66
66
|
|
67
|
-
|
68
|
-
const
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
67
|
+
public async connectedPeers(): Promise<Peer[]> {
|
68
|
+
const peers = await this.allPeers();
|
69
|
+
return peers.filter((peer) => {
|
70
|
+
return (
|
71
|
+
this.components.connectionManager.getConnections(peer.id).length > 0
|
72
|
+
);
|
73
|
+
});
|
74
74
|
}
|
75
75
|
|
76
76
|
/**
|
77
|
-
* Retrieves a list of connected peers
|
77
|
+
* Retrieves a list of connected peers that support the protocol. The list is sorted by latency.
|
78
78
|
*
|
79
79
|
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
80
80
|
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
|
82
|
+
* @returns A list of peers that support the protocol sorted by latency.
|
83
|
+
*/
|
84
|
+
async getPeers(
|
84
85
|
{
|
85
86
|
numPeers,
|
86
87
|
maxBootstrapPeers
|
@@ -92,23 +93,42 @@ export class BaseProtocol implements IBaseProtocol {
|
|
92
93
|
numPeers: 0
|
93
94
|
}
|
94
95
|
): Promise<Peer[]> {
|
95
|
-
// Retrieve all connected peers that support the protocol
|
96
|
-
const
|
97
|
-
|
96
|
+
// Retrieve all connected peers that support the protocol & shard (if configured)
|
97
|
+
const connectedPeersForProtocolAndShard =
|
98
|
+
await getConnectedPeersForProtocolAndShard(
|
99
|
+
this.components.connectionManager.getConnections(),
|
100
|
+
this.peerStore,
|
101
|
+
[this.multicodec],
|
102
|
+
this.options?.shardInfo
|
103
|
+
? ensureShardingConfigured(this.options.shardInfo).shardInfo
|
104
|
+
: undefined
|
105
|
+
);
|
106
|
+
|
107
|
+
// Filter the peers based on discovery & number of peers requested
|
108
|
+
const filteredPeers = filterPeersByDiscovery(
|
109
|
+
connectedPeersForProtocolAndShard,
|
110
|
+
numPeers,
|
111
|
+
maxBootstrapPeers
|
112
|
+
);
|
113
|
+
|
114
|
+
// Sort the peers by latency
|
115
|
+
const sortedFilteredPeers = await sortPeersByLatency(
|
98
116
|
this.peerStore,
|
99
|
-
|
117
|
+
filteredPeers
|
100
118
|
);
|
101
119
|
|
102
|
-
|
103
|
-
|
104
|
-
|
120
|
+
if (sortedFilteredPeers.length === 0) {
|
121
|
+
this.log.warn(
|
122
|
+
"No peers found. Ensure you have a connection to the network."
|
123
|
+
);
|
124
|
+
}
|
105
125
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
126
|
+
if (sortedFilteredPeers.length < numPeers) {
|
127
|
+
this.log.warn(
|
128
|
+
`Only ${sortedFilteredPeers.length} peers found. Requested ${numPeers}.`
|
129
|
+
);
|
130
|
+
}
|
131
|
+
|
132
|
+
return sortedFilteredPeers;
|
113
133
|
}
|
114
134
|
}
|
@@ -1,9 +1,5 @@
|
|
1
|
-
import type { PeerId } from "@libp2p/interface
|
2
|
-
import
|
3
|
-
import type { Peer } from "@libp2p/interface/peer-store";
|
4
|
-
import type { PeerStore } from "@libp2p/interface/peer-store";
|
5
|
-
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
|
6
|
-
import { decodeRelayShard } from "@waku/enr";
|
1
|
+
import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
|
2
|
+
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
|
7
3
|
import {
|
8
4
|
ConnectionManagerOptions,
|
9
5
|
EConnectionStateEvents,
|
@@ -18,7 +14,7 @@ import {
|
|
18
14
|
ShardInfo
|
19
15
|
} from "@waku/interfaces";
|
20
16
|
import { Libp2p, Tags } from "@waku/interfaces";
|
21
|
-
import { shardInfoToPubsubTopics } from "@waku/utils";
|
17
|
+
import { decodeRelayShard, shardInfoToPubsubTopics } from "@waku/utils";
|
22
18
|
import { Logger } from "@waku/utils";
|
23
19
|
|
24
20
|
import { KeepAliveManager } from "./keep_alive_manager.js";
|
@@ -30,7 +26,7 @@ export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
|
30
26
|
export const DEFAULT_MAX_PARALLEL_DIALS = 3;
|
31
27
|
|
32
28
|
export class ConnectionManager
|
33
|
-
extends
|
29
|
+
extends TypedEventEmitter<IPeersByDiscoveryEvents & IConnectionStateEvents>
|
34
30
|
implements IConnectionManager
|
35
31
|
{
|
36
32
|
private static instances = new Map<string, ConnectionManager>();
|
@@ -101,8 +97,11 @@ export class ConnectionManager
|
|
101
97
|
|
102
98
|
const peersDiscoveredByBootstrap: Peer[] = [];
|
103
99
|
const peersDiscoveredByPeerExchange: Peer[] = [];
|
100
|
+
const peersDiscoveredByLocal: Peer[] = [];
|
101
|
+
|
104
102
|
const peersConnectedByBootstrap: Peer[] = [];
|
105
103
|
const peersConnectedByPeerExchange: Peer[] = [];
|
104
|
+
const peersConnectedByLocal: Peer[] = [];
|
106
105
|
|
107
106
|
for (const peer of peersDiscovered) {
|
108
107
|
const tags = await this.getTagNamesForPeer(peer.id);
|
@@ -111,6 +110,8 @@ export class ConnectionManager
|
|
111
110
|
peersDiscoveredByBootstrap.push(peer);
|
112
111
|
} else if (tags.includes(Tags.PEER_EXCHANGE)) {
|
113
112
|
peersDiscoveredByPeerExchange.push(peer);
|
113
|
+
} else if (tags.includes(Tags.LOCAL)) {
|
114
|
+
peersDiscoveredByLocal.push(peer);
|
114
115
|
}
|
115
116
|
}
|
116
117
|
|
@@ -122,17 +123,21 @@ export class ConnectionManager
|
|
122
123
|
peersConnectedByBootstrap.push(peer);
|
123
124
|
} else if (tags.includes(Tags.PEER_EXCHANGE)) {
|
124
125
|
peersConnectedByPeerExchange.push(peer);
|
126
|
+
} else if (tags.includes(Tags.LOCAL)) {
|
127
|
+
peersConnectedByLocal.push(peer);
|
125
128
|
}
|
126
129
|
}
|
127
130
|
|
128
131
|
return {
|
129
132
|
DISCOVERED: {
|
130
133
|
[Tags.BOOTSTRAP]: peersDiscoveredByBootstrap,
|
131
|
-
[Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange
|
134
|
+
[Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange,
|
135
|
+
[Tags.LOCAL]: peersDiscoveredByLocal
|
132
136
|
},
|
133
137
|
CONNECTED: {
|
134
138
|
[Tags.BOOTSTRAP]: peersConnectedByBootstrap,
|
135
|
-
[Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange
|
139
|
+
[Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange,
|
140
|
+
[Tags.LOCAL]: peersConnectedByLocal
|
136
141
|
}
|
137
142
|
};
|
138
143
|
}
|
@@ -380,6 +385,8 @@ export class ConnectionManager
|
|
380
385
|
},
|
381
386
|
"peer:connect": (evt: CustomEvent<PeerId>): void => {
|
382
387
|
void (async () => {
|
388
|
+
log.info(`Connected to peer ${evt.detail.toString()}`);
|
389
|
+
|
383
390
|
const peerId = evt.detail;
|
384
391
|
|
385
392
|
this.keepAliveManager.start(
|