dash-platform-sdk 1.6.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dash-platform-sdk",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "main": "index.js",
5
5
  "description": "Lightweight SDK for accessing Dash Platform blockchain",
6
6
  "repository": {
@@ -16,7 +16,7 @@
16
16
  "defaults"
17
17
  ],
18
18
  "engines": {
19
- "node": ">=20.19.0 <=24"
19
+ "node": ">=20.19.0 <=26"
20
20
  },
21
21
  "type": "module",
22
22
  "devDependencies": {
@@ -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
- let quorumsInfo;
183
- const errors = [];
184
- for (const seed of seedNodes[network]) {
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
- quorumsInfo = await withTimeout(async (abortController) => await getCurrentQuorumsInfo({ network, getClient: () => createClient(seed, abortController) }));
187
- break;
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.push(e);
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
- if (quorumsInfo == null) {
194
- throw new AggregateError(errors, `None of the ${network} seed nodes are available: ${errors.map(String).join('; ')}`);
195
- }
196
- // map it to array of dapiUrls
197
- const networkDAPIUrls = shuffle([...new Set(quorumsInfo.validatorSets
198
- .flatMap(validatorSet => validatorSet.members)
199
- .filter(member => !member.isBanned)
200
- .map(member => `https://${member.nodeIp}:${DAPI_PORTS[network]}`))]);
201
- // healthcheck nodes in parallel batches until the pool is full
202
- const dapiUrls = [];
203
- for (let i = 0; i < networkDAPIUrls.length && dapiUrls.length < this.#poolLimit; i += this.#poolLimit) {
204
- const batch = networkDAPIUrls.slice(i, i + this.#poolLimit);
205
- const healthy = await Promise.all(batch.map(async (url) => await isNodeHealthy(url, this.#minProtocolVersion)));
206
- dapiUrls.push(...batch.filter((_, j) => healthy[j]));
207
- }
208
- // fail the initialization, so it is retried on the next request
209
- if (dapiUrls.length === 0) {
210
- throw new Error(`None of the ${network} evonodes are available (${networkDAPIUrls.length} checked, ` +
211
- `nodes must be synced and run protocol version ${this.#minProtocolVersion ?? 1}-${Number(LATEST_PLATFORM_VERSION)})`);
212
- }
213
- this.dapiUrls = dapiUrls.slice(0, this.#poolLimit);
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)