dash-platform-sdk 1.6.1 → 1.6.2
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/README.md +14 -14
- package/bundle.min.js +1 -1
- package/package.json +1 -1
- package/src/grpcConnectionPool.js +78 -29
package/package.json
CHANGED
|
@@ -49,6 +49,12 @@ const DAPI_PORTS = {
|
|
|
49
49
|
const HEALTHCHECK_TIMEOUT_MS = 5000;
|
|
50
50
|
// platform produces empty blocks every 3 minutes, so a healthy chain is never older than that
|
|
51
51
|
const HEALTHCHECK_MAX_BLOCK_AGE_MS = 10 * 60 * 1000;
|
|
52
|
+
// Per-client concurrency × clients initializing at once is the instantaneous load on nodes,
|
|
53
|
+
// so healthchecks run a small window at a time to avoid a fleet-wide request burst.
|
|
54
|
+
const HEALTHCHECK_CONCURRENCY = 2;
|
|
55
|
+
// Init resolves at this many healthy nodes; seeds only bootstrap the pool and are replaced
|
|
56
|
+
// by discovered validators, so they never stay in the long-lived pool.
|
|
57
|
+
const HEALTHCHECK_MIN_HEALTHY_NODES = 2;
|
|
52
58
|
const isAbortError = (error) => error instanceof Error && error.name === 'AbortError';
|
|
53
59
|
// errors raised before the request is sent to the node, so it is always safe to send it to another node
|
|
54
60
|
const CONNECT_ERROR_CODES = ['UND_ERR_CONNECT_TIMEOUT', 'ECONNREFUSED', 'EHOSTUNREACH', 'ENETUNREACH', 'EHOSTDOWN', 'ENETDOWN'];
|
|
@@ -86,9 +92,10 @@ const withTimeout = async (request, timeoutMs = HEALTHCHECK_TIMEOUT_MS) => {
|
|
|
86
92
|
clearTimeout(timeout);
|
|
87
93
|
}
|
|
88
94
|
};
|
|
89
|
-
const isNodeHealthy = async (url, minProtocolVersion) => {
|
|
95
|
+
const isNodeHealthy = async (url, minProtocolVersion, signal) => {
|
|
90
96
|
try {
|
|
91
97
|
const response = await withTimeout(async (abortController) => {
|
|
98
|
+
signal?.addEventListener('abort', () => abortController.abort());
|
|
92
99
|
const { response } = await createClient(url, abortController).getStatus(GetStatusRequest.create({}));
|
|
93
100
|
return response;
|
|
94
101
|
});
|
|
@@ -179,38 +186,80 @@ export default class GRPCConnectionPool {
|
|
|
179
186
|
}
|
|
180
187
|
return;
|
|
181
188
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
189
|
+
// Race all seeds for discovery; the first successful response wins and the losers are
|
|
190
|
+
// aborted. If every seed fails, init rejects with "no peers" (the original contract).
|
|
191
|
+
const quorumsInfoPromise = async () => {
|
|
192
|
+
const winner = new AbortController();
|
|
185
193
|
try {
|
|
186
|
-
|
|
187
|
-
|
|
194
|
+
return await Promise.any(seedNodes[network].map(async (seed) => await withTimeout(async (abortController) => {
|
|
195
|
+
winner.signal.addEventListener('abort', () => abortController.abort());
|
|
196
|
+
return await getCurrentQuorumsInfo({ network, getClient: () => createClient(seed, abortController) });
|
|
197
|
+
})));
|
|
188
198
|
}
|
|
189
199
|
catch (e) {
|
|
190
|
-
errors.
|
|
200
|
+
const errors = e.errors;
|
|
201
|
+
throw new AggregateError(errors, `None of the ${network} seed nodes are available: ${errors.map(String).join('; ')}`);
|
|
191
202
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
//
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
203
|
+
finally {
|
|
204
|
+
winner.abort();
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
// Bootstrap the pool by racing all seed healthchecks; resolve at the first K healthy
|
|
208
|
+
// and abort the rest, so a slow/dead seed never stalls initialization.
|
|
209
|
+
const seeds = seedNodes[network];
|
|
210
|
+
const healthySeeds = [];
|
|
211
|
+
const bootstrapPromise = new Promise((resolve, reject) => {
|
|
212
|
+
const race = new AbortController();
|
|
213
|
+
let pending = seeds.length;
|
|
214
|
+
seeds.forEach(seed => {
|
|
215
|
+
void isNodeHealthy(seed, this.#minProtocolVersion, race.signal).then(healthy => {
|
|
216
|
+
if (healthy) {
|
|
217
|
+
healthySeeds.push(seed);
|
|
218
|
+
if (healthySeeds.length >= HEALTHCHECK_MIN_HEALTHY_NODES) {
|
|
219
|
+
race.abort();
|
|
220
|
+
resolve();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
pending -= 1;
|
|
224
|
+
if (pending === 0) {
|
|
225
|
+
if (healthySeeds.length === 0) {
|
|
226
|
+
reject(new Error(`None of the ${network} seed nodes are available ` +
|
|
227
|
+
`(nodes must be synced and run protocol version ${this.#minProtocolVersion ?? 1}-${Number(LATEST_PLATFORM_VERSION)})`));
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
resolve();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
// Discovery failure fails initialization with "no peers", exactly as before.
|
|
237
|
+
const [, quorumsInfo] = await Promise.all([bootstrapPromise, quorumsInfoPromise()]);
|
|
238
|
+
this.dapiUrls = [...healthySeeds];
|
|
239
|
+
// Replace the bootstrap seeds with discovered validators in the background.
|
|
240
|
+
void (async () => {
|
|
241
|
+
const validators = shuffle([...new Set(quorumsInfo.validatorSets
|
|
242
|
+
.flatMap(validatorSet => validatorSet.members)
|
|
243
|
+
.filter(member => !member.isBanned)
|
|
244
|
+
.map(member => `https://${member.nodeIp}:${DAPI_PORTS[network]}`))]);
|
|
245
|
+
const healthyValidators = [];
|
|
246
|
+
let validatorIndex = 0;
|
|
247
|
+
const checkValidator = async () => {
|
|
248
|
+
while (validatorIndex < validators.length && healthyValidators.length < this.#poolLimit) {
|
|
249
|
+
const url = validators[validatorIndex++];
|
|
250
|
+
if (await isNodeHealthy(url, this.#minProtocolVersion)) {
|
|
251
|
+
healthyValidators.push(url);
|
|
252
|
+
// Evict the bootstrap seeds once enough validators are healthy.
|
|
253
|
+
if (healthyValidators.length >= HEALTHCHECK_MIN_HEALTHY_NODES) {
|
|
254
|
+
this.dapiUrls = healthyValidators.slice(0, this.#poolLimit);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
await Promise.all(Array.from({ length: HEALTHCHECK_CONCURRENCY }, async () => await checkValidator()));
|
|
260
|
+
})().catch(() => {
|
|
261
|
+
// keep the bootstrap seeds if validator discovery or healthchecks fail
|
|
262
|
+
});
|
|
214
263
|
}
|
|
215
264
|
/**
|
|
216
265
|
* Waits for the pool initialization and returns a client connected to the next healthy node (round-robin)
|