@waku/discovery 0.0.7-2380dac.0 → 0.0.7-2a94244.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/index.js +1243 -4271
- package/dist/.tsbuildinfo +1 -1
- package/dist/dns/constants.d.ts +0 -2
- package/dist/dns/constants.js +0 -5
- package/dist/dns/constants.js.map +1 -1
- package/dist/dns/dns.d.ts +7 -9
- package/dist/dns/dns.js +9 -11
- package/dist/dns/dns.js.map +1 -1
- package/dist/dns/dns_discovery.d.ts +3 -3
- package/dist/dns/dns_discovery.js +4 -5
- package/dist/dns/dns_discovery.js.map +1 -1
- package/dist/dns/dns_over_https.d.ts +2 -8
- package/dist/dns/dns_over_https.js +8 -22
- package/dist/dns/dns_over_https.js.map +1 -1
- package/dist/dns/fetch_nodes.d.ts +7 -9
- package/dist/dns/fetch_nodes.js +21 -99
- package/dist/dns/fetch_nodes.js.map +1 -1
- package/package.json +1 -1
- package/src/dns/constants.ts +0 -8
- package/src/dns/dns.ts +11 -33
- package/src/dns/dns_discovery.ts +6 -14
- package/src/dns/dns_over_https.ts +6 -31
- package/src/dns/fetch_nodes.ts +23 -132
package/src/dns/fetch_nodes.ts
CHANGED
@@ -1,54 +1,36 @@
|
|
1
|
-
import type { IEnr
|
1
|
+
import type { IEnr } from "@waku/interfaces";
|
2
2
|
import { Logger } from "@waku/utils";
|
3
3
|
|
4
4
|
const log = new Logger("discovery:fetch_nodes");
|
5
5
|
|
6
6
|
/**
|
7
|
-
* Fetch nodes using passed [[getNode]]
|
8
|
-
*
|
9
|
-
*
|
7
|
+
* Fetch nodes using passed [[getNode]]; results are validated before being
|
8
|
+
* returned. Stops when [[getNodes]] does not return any more nodes or the
|
9
|
+
* number call exceeds [[maxSearches]].
|
10
10
|
*/
|
11
|
-
export async function
|
12
|
-
|
13
|
-
errorTolerance: number,
|
11
|
+
export async function fetchAndValidateNodes(
|
12
|
+
maxSearches: number = 10,
|
14
13
|
getNode: () => Promise<IEnr | null>
|
15
14
|
): Promise<IEnr[]> {
|
16
|
-
const wanted = {
|
17
|
-
relay: wantedNodeCapabilityCount.relay ?? 0,
|
18
|
-
store: wantedNodeCapabilityCount.store ?? 0,
|
19
|
-
filter: wantedNodeCapabilityCount.filter ?? 0,
|
20
|
-
lightPush: wantedNodeCapabilityCount.lightPush ?? 0
|
21
|
-
};
|
22
|
-
|
23
|
-
const maxSearches =
|
24
|
-
wanted.relay + wanted.store + wanted.filter + wanted.lightPush;
|
25
|
-
|
26
|
-
const actual = {
|
27
|
-
relay: 0,
|
28
|
-
store: 0,
|
29
|
-
filter: 0,
|
30
|
-
lightPush: 0
|
31
|
-
};
|
32
|
-
|
33
15
|
let totalSearches = 0;
|
16
|
+
let emptyResults = 0;
|
34
17
|
const peers: IEnr[] = [];
|
35
18
|
|
36
19
|
while (
|
37
|
-
|
38
|
-
|
20
|
+
totalSearches < maxSearches &&
|
21
|
+
emptyResults < 2 // Allows a couple of empty results before calling it quit
|
39
22
|
) {
|
40
23
|
const peer = await getNode();
|
41
24
|
if (peer && isNewPeer(peer, peers)) {
|
42
25
|
// ENRs without a waku2 key are ignored.
|
43
26
|
if (peer.waku2) {
|
44
|
-
|
45
|
-
addCapabilities(peer.waku2, actual);
|
46
|
-
peers.push(peer);
|
47
|
-
}
|
27
|
+
peers.push(peer);
|
48
28
|
}
|
49
29
|
log.info(
|
50
30
|
`got new peer candidate from DNS address=${peer.nodeId}@${peer.ip}`
|
51
31
|
);
|
32
|
+
} else {
|
33
|
+
emptyResults++;
|
52
34
|
}
|
53
35
|
|
54
36
|
totalSearches++;
|
@@ -57,69 +39,26 @@ export async function fetchNodesUntilCapabilitiesFulfilled(
|
|
57
39
|
}
|
58
40
|
|
59
41
|
/**
|
60
|
-
* Fetch nodes using passed [[getNode]]
|
61
|
-
* fulfilled or the number of [[getNode]] call exceeds the sum of
|
62
|
-
* [[wantedNodeCapabilityCount]] plus [[errorTolerance]].
|
42
|
+
* Fetch nodes using passed [[getNode]]
|
63
43
|
*/
|
64
|
-
export async function*
|
65
|
-
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
66
|
-
errorTolerance: number,
|
44
|
+
export async function* yieldNodes(
|
67
45
|
getNode: () => Promise<IEnr | null>
|
68
46
|
): AsyncGenerator<IEnr> {
|
69
|
-
const wanted = {
|
70
|
-
relay: wantedNodeCapabilityCount.relay ?? 0,
|
71
|
-
store: wantedNodeCapabilityCount.store ?? 0,
|
72
|
-
filter: wantedNodeCapabilityCount.filter ?? 0,
|
73
|
-
lightPush: wantedNodeCapabilityCount.lightPush ?? 0
|
74
|
-
};
|
75
|
-
|
76
|
-
const maxSearches =
|
77
|
-
wanted.relay + wanted.store + wanted.filter + wanted.lightPush;
|
78
|
-
|
79
|
-
const actual = {
|
80
|
-
relay: 0,
|
81
|
-
store: 0,
|
82
|
-
filter: 0,
|
83
|
-
lightPush: 0
|
84
|
-
};
|
85
|
-
|
86
|
-
let totalSearches = 0;
|
87
47
|
const peerNodeIds = new Set();
|
88
48
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
peerNodeIds.add(peer.nodeId);
|
96
|
-
// ENRs without a waku2 key are ignored.
|
97
|
-
if (peer.waku2) {
|
98
|
-
if (helpsSatisfyCapabilities(peer.waku2, wanted, actual)) {
|
99
|
-
addCapabilities(peer.waku2, actual);
|
100
|
-
yield peer;
|
101
|
-
}
|
102
|
-
}
|
103
|
-
log.info(
|
104
|
-
`got new peer candidate from DNS address=${peer.nodeId}@${peer.ip}`
|
105
|
-
);
|
49
|
+
const peer = await getNode();
|
50
|
+
if (peer && peer.nodeId && !peerNodeIds.has(peer.nodeId)) {
|
51
|
+
peerNodeIds.add(peer.nodeId);
|
52
|
+
// ENRs without a waku2 key are ignored.
|
53
|
+
if (peer.waku2) {
|
54
|
+
yield peer;
|
106
55
|
}
|
107
|
-
|
56
|
+
log.info(
|
57
|
+
`got new peer candidate from DNS address=${peer.nodeId}@${peer.ip}`
|
58
|
+
);
|
108
59
|
}
|
109
60
|
}
|
110
61
|
|
111
|
-
function isSatisfied(
|
112
|
-
wanted: NodeCapabilityCount,
|
113
|
-
actual: NodeCapabilityCount
|
114
|
-
): boolean {
|
115
|
-
return (
|
116
|
-
actual.relay >= wanted.relay &&
|
117
|
-
actual.store >= wanted.store &&
|
118
|
-
actual.filter >= wanted.filter &&
|
119
|
-
actual.lightPush >= wanted.lightPush
|
120
|
-
);
|
121
|
-
}
|
122
|
-
|
123
62
|
function isNewPeer(peer: IEnr, peers: IEnr[]): boolean {
|
124
63
|
if (!peer.nodeId) return false;
|
125
64
|
|
@@ -131,51 +70,3 @@ function isNewPeer(peer: IEnr, peers: IEnr[]): boolean {
|
|
131
70
|
|
132
71
|
return true;
|
133
72
|
}
|
134
|
-
|
135
|
-
function addCapabilities(node: Waku2, total: NodeCapabilityCount): void {
|
136
|
-
if (node.relay) total.relay += 1;
|
137
|
-
if (node.store) total.store += 1;
|
138
|
-
if (node.filter) total.filter += 1;
|
139
|
-
if (node.lightPush) total.lightPush += 1;
|
140
|
-
}
|
141
|
-
|
142
|
-
/**
|
143
|
-
* Checks if the proposed ENR [[node]] helps satisfy the [[wanted]] capabilities,
|
144
|
-
* considering the [[actual]] capabilities of nodes retrieved so far..
|
145
|
-
*
|
146
|
-
* @throws If the function is called when the wanted capabilities are already fulfilled.
|
147
|
-
*/
|
148
|
-
function helpsSatisfyCapabilities(
|
149
|
-
node: Waku2,
|
150
|
-
wanted: NodeCapabilityCount,
|
151
|
-
actual: NodeCapabilityCount
|
152
|
-
): boolean {
|
153
|
-
if (isSatisfied(wanted, actual)) {
|
154
|
-
throw "Internal Error: Waku2 wanted capabilities are already fulfilled";
|
155
|
-
}
|
156
|
-
|
157
|
-
const missing = missingCapabilities(wanted, actual);
|
158
|
-
|
159
|
-
return (
|
160
|
-
(missing.relay && node.relay) ||
|
161
|
-
(missing.store && node.store) ||
|
162
|
-
(missing.filter && node.filter) ||
|
163
|
-
(missing.lightPush && node.lightPush)
|
164
|
-
);
|
165
|
-
}
|
166
|
-
|
167
|
-
/**
|
168
|
-
* Return a [[Waku2]] Object for which capabilities are set to true if they are
|
169
|
-
* [[wanted]] yet missing from [[actual]].
|
170
|
-
*/
|
171
|
-
function missingCapabilities(
|
172
|
-
wanted: NodeCapabilityCount,
|
173
|
-
actual: NodeCapabilityCount
|
174
|
-
): Waku2 {
|
175
|
-
return {
|
176
|
-
relay: actual.relay < wanted.relay,
|
177
|
-
store: actual.store < wanted.store,
|
178
|
-
filter: actual.filter < wanted.filter,
|
179
|
-
lightPush: actual.lightPush < wanted.lightPush
|
180
|
-
};
|
181
|
-
}
|