-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefaults.js
More file actions
508 lines (448 loc) · 24.6 KB
/
defaults.js
File metadata and controls
508 lines (448 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**
* Sentinel dVPN SDK — Hardcoded Defaults & Recommended Values
*
* SINGLE SOURCE OF TRUTH for all values that may go stale.
* When the RPC query server is built, this file gets replaced by live lookups.
*
* ┌──────────────────────────────────────────────────────────────────────┐
* │ LAST VERIFIED: 2026-03-08T00:00:00Z │
* │ VERIFIED BY: 708-node scan + manual LCD/RPC checks │
* │ CHAIN: sentinelhub-2 (sentinelhub v12.0.0, Cosmos 0.47.17)│
* │ │
* │ These values are HARDCODED for easy cold-start setup. │
* │ A future RPC query server will replace static defaults with live │
* │ endpoint health checks, node scoring, and price feeds. │
* └──────────────────────────────────────────────────────────────────────┘
*/
// ─── Axios adapter fix (MUST run before any HTTP requests) ──────────────────
// Node.js 18+ uses undici internally. Without this, ~40% of HTTP requests
// fail with opaque "fetch failed" errors (no stack trace, no errno).
import axios from 'axios';
axios.defaults.adapter = 'http';
// ─── SDK Version ─────────────────────────────────────────────────────────────
// This is the npm/semver version for consumers. Internal development iterations
// (v20, v21, v22, etc.) track feature milestones and are not exposed as exports.
export const SDK_VERSION = '2.7.1';
// ─── Timestamps ──────────────────────────────────────────────────────────────
/** When these defaults were last verified against the live chain.
* RPC + LCD endpoint lists refreshed 2026-05-02 (audit-rpc-endpoints.mjs).
* Other defaults (gas, transport rates, etc.) still 2026-03-08. */
export const LAST_VERIFIED = '2026-05-02T00:00:00Z';
/** Human-readable note for builders */
export const HARDCODED_NOTE = 'Static defaults — no RPC query server yet. Verify endpoints are live before production use. See README.md "Hardcoded Defaults" section.';
// ─── Chain ───────────────────────────────────────────────────────────────────
export const CHAIN_ID = 'sentinelhub-2';
export const DENOM = 'udvpn';
export const GAS_PRICE = '0.2udvpn'; // Chain minimum as of 2026-03-08
export const CHAIN_VERSION = 'v12.0.0'; // sentinelhub version
export const COSMOS_SDK_VERSION = '0.47.17';
// ─── RPC Endpoints (TX broadcast) ────────────────────────────────────────────
// Latency-sorted list of endpoints that passed consensus health check on
// 2026-05-02. "Healthy" means: connects, /status reports catching_up=false,
// and the bank balance for a known address matches the modal answer across
// all responding candidates within 50 blocks of tip. Run
// `node tools/audit-rpc-endpoints.mjs` to refresh.
//
// rpc.sentinel.co / lcd.sentinel.co are intentionally excluded: on 2026-05-02
// they were ~22k blocks behind tip and returning 0 for funded addresses while
// reporting catching_up=false on /status. Consumers that need them can add
// them at runtime via addRpcEndpoint() / addLcdEndpoint().
export const RPC_ENDPOINTS = [
{ url: 'https://rpc-sentinel.busurnode.com', name: 'Busurnode', verified: '2026-05-02' },
{ url: 'https://rpc.trinitystake.io', name: 'Trinity Stake', verified: '2026-05-02' },
{ url: 'https://sentinel-rpc.publicnode.com', name: 'PublicNode (Allnodes)', verified: '2026-05-02' },
{ url: 'https://sentinel-rpc.polkachu.com', name: 'Polkachu', verified: '2026-05-02' },
{ url: 'https://rpc.mathnodes.com', name: 'MathNodes', verified: '2026-05-02' },
{ url: 'https://rpc.dvpn.roomit.xyz', name: 'Roomit', verified: '2026-05-02' },
{ url: 'https://rpc.sentinel.suchnode.net', name: 'SuchNode', verified: '2026-05-02' },
{ url: 'https://rpc.sentinel.chaintools.tech', name: 'ChainTools', verified: '2026-05-02' },
{ url: 'https://rpc.sentinel.validatus.com', name: 'Validatus', verified: '2026-05-02' },
{ url: 'https://rpc.sentinel.quokkastake.io', name: 'QuokkaStake', verified: '2026-05-02' },
{ url: 'https://rpc.sentineldao.com', name: 'Sentinel Growth DAO', verified: '2026-05-02' },
{ url: 'https://rpc-sentinel.chainvibes.com', name: 'ChainVibes', verified: '2026-05-02' },
];
export const DEFAULT_RPC = RPC_ENDPOINTS[0].url;
// ─── LCD Endpoints (REST queries) ────────────────────────────────────────────
// Same consensus methodology as RPC. lcd.sentinel.co excluded for the same
// stale-state reason.
export const LCD_ENDPOINTS = [
{ url: 'https://api-sentinel.busurnode.com', name: 'Busurnode', verified: '2026-05-02' },
{ url: 'https://sentinel-rest.publicnode.com', name: 'PublicNode (Allnodes)', verified: '2026-05-02' },
{ url: 'https://api.sentinel.suchnode.net', name: 'SuchNode', verified: '2026-05-02' },
{ url: 'https://sentinel-api.polkachu.com', name: 'Polkachu', verified: '2026-05-02' },
{ url: 'https://api.dvpn.roomit.xyz', name: 'Roomit', verified: '2026-05-02' },
{ url: 'https://api.sentinel.quokkastake.io', name: 'QuokkaStake', verified: '2026-05-02' },
{ url: 'https://api.sentinel.chaintools.tech', name: 'ChainTools', verified: '2026-05-02' },
{ url: 'https://api-sentinel.chainvibes.com', name: 'ChainVibes', verified: '2026-05-02' },
{ url: 'https://api.sentinel.validatus.com', name: 'Validatus', verified: '2026-05-02' },
];
export const DEFAULT_LCD = LCD_ENDPOINTS[0].url;
// ─── V2Ray ───────────────────────────────────────────────────────────────────
export const V2RAY_VERSION = '5.2.1';
/** WARNING: v5.44.1 has observatory bugs. Do NOT upgrade past 5.2.1. Verified 2026-03-08. */
export const V2RAY_VERSION_WARNING = 'v5.2.1 exactly — v5.44.1+ has observatory/balancer bugs that break multi-outbound configs';
// ─── Transport Success Rates (from 780-node scan, 2026-03-09) ────────────────
// Used by buildV2RayClientConfig() to sort outbounds by reliability.
// Dynamic rates (in-memory) override these when available — see below.
export const TRANSPORT_SUCCESS_RATES = {
'tcp': { rate: 1.00, sample: 274, note: 'Best — always first choice' },
'websocket': { rate: 1.00, sample: 23, note: 'Second choice' },
'http': { rate: 1.00, sample: 4, note: '' },
'gun': { rate: 1.00, sample: 10, note: 'gun(2) ≠ grpc(3) — different protocols' },
'mkcp': { rate: 1.00, sample: 5, note: '' },
'grpc/none': { rate: 0.87, sample: 81, note: '70/81 pass. serverName TLS fix applied.' },
'quic': { rate: 0.00, sample: 4, note: '0/4 — chacha20 mismatch fixed, low node count' },
'grpc/tls': { rate: 0.00, sample: 0, note: 'serverName TLS fix applied. No test nodes available.' },
};
// ─── Dynamic Transport Rate Tracking (persisted to disk) ─────────────────────
// Runtime success/failure tracking per transport type. Overrides hardcoded
// TRANSPORT_SUCCESS_RATES when enough samples exist. Persisted to
// ~/.sentinel-sdk/dynamic-rates.json with TTL eviction on load.
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
import path from 'path';
import os from 'os';
const DYNAMIC_RATES_DIR = path.join(os.homedir(), '.sentinel-sdk');
const DYNAMIC_RATES_FILE = path.join(DYNAMIC_RATES_DIR, 'dynamic-rates.json');
const DYNAMIC_RATES_TTL = 7 * 24 * 60 * 60_000; // 7 days
const _dynamicRates = new Map(); // transportKey -> { success, fail, updatedAt }
/** Load persisted dynamic rates from disk (evict stale entries). */
function _loadDynamicRates() {
try {
if (!existsSync(DYNAMIC_RATES_FILE)) return;
const raw = JSON.parse(readFileSync(DYNAMIC_RATES_FILE, 'utf-8'));
const now = Date.now();
for (const [key, entry] of Object.entries(raw)) {
if (entry.updatedAt && now - entry.updatedAt < DYNAMIC_RATES_TTL) {
_dynamicRates.set(key, entry);
}
}
} catch { /* corrupt file — start fresh */ }
}
/** Persist current dynamic rates to disk. */
function _saveDynamicRates() {
try {
if (!existsSync(DYNAMIC_RATES_DIR)) mkdirSync(DYNAMIC_RATES_DIR, { recursive: true, mode: 0o700 });
const obj = {};
for (const [key, entry] of _dynamicRates) obj[key] = entry;
writeFileSync(DYNAMIC_RATES_FILE, JSON.stringify(obj, null, 2), { mode: 0o600 });
} catch { /* disk write failed — rates stay in memory */ }
}
// Load on module init
_loadDynamicRates();
/** Record a transport connection result. Called automatically by setupV2Ray. */
export function recordTransportResult(transportKey, success) {
const entry = _dynamicRates.get(transportKey) || { success: 0, fail: 0, updatedAt: 0 };
if (success) entry.success++; else entry.fail++;
entry.updatedAt = Date.now();
_dynamicRates.set(transportKey, entry);
_saveDynamicRates();
}
/**
* Get the dynamic success rate for a transport. Returns null if < 2 samples.
* Used by transportSortKey() in v3protocol.js to prioritize transports.
*/
export function getDynamicRate(transportKey) {
const entry = _dynamicRates.get(transportKey);
if (!entry) return null;
const total = entry.success + entry.fail;
if (total < 2) return null;
return entry.success / total;
}
/** Get all dynamic rates as { transportKey: { rate, sample } }. */
export function getDynamicRates() {
const result = {};
for (const [key, entry] of _dynamicRates) {
const total = entry.success + entry.fail;
if (total > 0) result[key] = { rate: entry.success / total, sample: total };
}
return result;
}
/** Clear all dynamic rate data. Pass persist: true to also clear disk. */
export function resetDynamicRates(persist = false) {
_dynamicRates.clear();
if (persist) _saveDynamicRates();
}
// ─── Recommended Starter Nodes ───────────────────────────────────────────────
// High-reliability nodes from 708-node scan (2026-03-08).
// These had 100% connection success, low drift, WireGuard or proven V2Ray transports.
//
// ⚠ STALE WARNING: Nodes go offline. These are starting points, NOT guarantees.
// No hardcoded node list — nodes go offline unpredictably.
// Use queryOnlineNodes() for live, scored results:
// const nodes = await queryOnlineNodes({ lcdUrl: DEFAULT_LCD, maxNodes: 50 });
// // Returns nodes sorted by quality score (WG preferred, clock drift penalized)
// ─── Known Broken Nodes (blacklist) ──────────────────────────────────────────
// Nodes with confirmed bugs. Skip these to avoid wasting P2P.
// Verified 2026-03-08.
export const BROKEN_NODES = [
{ address: 'sentnode1qqktst6793vdxknvvkewfcmtv9edh7vvdvavrj', reason: 'nil UUID state — handshake always fails', verified: '2026-03-08' },
{ address: 'sentnode1qx2p7kyep6m44ae47yh9zf3cfxrzrv5zt9vdnj', reason: 'handshake OK but proxy always fails (0 bytes)', verified: '2026-03-08' },
];
// ─── Pricing Reference (from chain data, 2026-03-08) ─────────────────────────
// Typical values — actual prices vary per node. These are for estimation only.
export const PRICING_REFERENCE = {
verified: '2026-03-08',
note: 'Approximate values for cost estimation. Actual prices vary per node and change over time.',
session: {
typicalCostDvpn: 0.1, // ~0.04-0.15 P2P per 1GB session
minBalanceDvpn: 1, // Minimum recommended wallet balance
minBalanceUdvpn: 1_000_000, // Same in micro-denom
},
gasPerMsg: {
startSession: 200_000, // ~200k gas for MsgStartSession
startSubscription: 250_000, // ~250k gas for subscription + session
createPlan: 300_000, // ~300k gas
startLease: 250_000, // ~250k gas
batchOf5: 800_000, // ~800k gas for 5 MsgStartSession batch
},
averageNodePrices: {
gigabyteQuoteValue: '40152030', // Average udvpn per GB (0.04 P2P)
hourlyQuoteValue: '18384000', // Average udvpn per hour (0.018 P2P)
baseValue: '0.003000000000000000', // Typical base_value (sdk.Dec)
},
};
// ─── Shared Utilities ─────────────────────────────────────────────────────────
/** Promise-based delay. Used across node-connect, wireguard, speedtest. */
export function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
/** Convert bytes transferred over seconds to Mbps. */
export function bytesToMbps(bytes, seconds, decimals = null) {
if (!seconds || seconds <= 0) return 0;
const mbps = (bytes * 8) / seconds / 1_000_000;
return decimals !== null ? parseFloat(mbps.toFixed(decimals)) : mbps;
}
// ─── DNS Presets ─────────────────────────────────────────────────────────────
// DNS servers for WireGuard tunnel. Handshake is default — decentralized,
// censorship-resistant DNS that resolves both Handshake TLDs and ICANN domains.
// Matches Sentinel Shield mobile app behavior.
export const DNS_PRESETS = Object.freeze({
handshake: {
name: 'Handshake',
servers: ['103.196.38.38', '103.196.38.39'],
description: 'Decentralized DNS — resolves Handshake + ICANN domains. Censorship-resistant.',
},
google: {
name: 'Google',
servers: ['8.8.8.8', '8.8.4.4'],
description: 'Google Public DNS',
},
cloudflare: {
name: 'Cloudflare',
servers: ['1.1.1.1', '1.0.0.1'],
description: 'Cloudflare Public DNS',
},
});
export const DEFAULT_DNS_PRESET = 'handshake';
/** Fallback order: handshake → google → cloudflare. */
export const DNS_FALLBACK_ORDER = ['handshake', 'google', 'cloudflare'];
/**
* Resolve a DNS option into a comma-separated string for WireGuard config.
* Includes fallback DNS servers — if the primary DNS fails, the OS tries the next ones.
*
* @param {string|string[]|undefined} dns - Preset name ('handshake'|'google'|'cloudflare'),
* array of custom IP strings, or undefined for default (Handshake).
* @returns {string} DNS string for WireGuard config with fallbacks
* (e.g. '103.196.38.38, 103.196.38.39, 8.8.8.8, 1.1.1.1')
*/
export function resolveDnsServers(dns) {
const primary = _resolvePrimary(dns);
const primarySet = new Set(primary);
// Append one server from each fallback preset not already in the primary list
const fallbacks = [];
for (const name of DNS_FALLBACK_ORDER) {
const preset = DNS_PRESETS[name];
for (const server of preset.servers) {
if (!primarySet.has(server)) {
fallbacks.push(server);
primarySet.add(server);
break; // one per preset is enough for fallback
}
}
}
return [...primary, ...fallbacks].join(', ');
}
/** Resolve just the primary DNS servers (no fallbacks). */
function _resolvePrimary(dns) {
if (!dns) return [...DNS_PRESETS[DEFAULT_DNS_PRESET].servers];
if (typeof dns === 'string') {
const preset = DNS_PRESETS[dns.toLowerCase()];
if (preset) return [...preset.servers];
return [dns];
}
if (Array.isArray(dns) && dns.length > 0) return [...dns];
return [...DNS_PRESETS[DEFAULT_DNS_PRESET].servers];
}
// ─── Connection Timeouts (ms) ────────────────────────────────────────────────
/** Default timeout values used during node connection. Override via opts.timeouts. */
export const DEFAULT_TIMEOUTS = {
handshake: 90000, // Max time for WireGuard/V2Ray handshake with node (overloaded nodes need 60-90s)
nodeStatus: 12000, // Max time to fetch node status from remote URL
lcdQuery: 15000, // Max time for LCD chain queries
v2rayReady: 10000, // Max time waiting for V2Ray SOCKS proxy to be ready
};
// ─── Endpoint Health Check ────────────────────────────────────────────────────
/**
* Ping endpoints and return them sorted by latency (fastest first).
* Unreachable endpoints are moved to the end.
* @param {Array<{url: string, name: string}>} endpoints
* @param {number} timeoutMs - Per-endpoint timeout (default 5000ms)
* @returns {Promise<Array<{url: string, name: string, latencyMs: number|null}>>}
*/
export async function checkEndpointHealth(endpoints, timeoutMs = 5000) {
const results = await Promise.all(endpoints.map(async (ep) => {
const start = Date.now();
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
// LCD endpoints use /cosmos/base/tendermint/v1beta1/syncing for health check
// (/status doesn't exist on LCD — that's an RPC endpoint path)
await axios.get(`${ep.url}/cosmos/base/tendermint/v1beta1/syncing`, { signal: controller.signal, timeout: timeoutMs });
clearTimeout(timer);
return { ...ep, latencyMs: Date.now() - start };
} catch {
return { ...ep, latencyMs: null };
}
}));
// Sort: reachable first (by latency), unreachable last
return results.sort((a, b) => {
if (a.latencyMs != null && b.latencyMs != null) return a.latencyMs - b.latencyMs;
if (a.latencyMs != null) return -1;
if (b.latencyMs != null) return 1;
return 0;
});
}
// ─── Helper: Try endpoints with fallback ─────────────────────────────────────
/**
* Try an async operation against multiple endpoints, returning the first success.
* Use this for RPC/LCD operations that should fall back to alternatives.
*
* @param {Array<{url: string, name: string}>} endpoints - Ordered list of endpoints
* @param {function} operation - async (url) => result
* @param {string} label - For error messages (e.g. 'LCD query', 'RPC connect')
* @returns {Promise<{result: any, endpoint: string}>}
*/
export async function tryWithFallback(endpoints, operation, label = 'operation') {
const errors = [];
for (const ep of endpoints) {
try {
const result = await operation(ep.url);
return { result, endpoint: ep.url, endpointName: ep.name };
} catch (err) {
errors.push({ endpoint: ep.url, name: ep.name, error: err.message });
}
}
const tried = errors.map(e => ` ${e.name} (${e.endpoint}): ${e.error}`).join('\n');
// Lazy import to avoid circular dependency (errors.js is simple, no deps)
const { ChainError } = await import('./errors.js');
throw new ChainError('ALL_ENDPOINTS_FAILED', `${label} failed on all ${endpoints.length} endpoints (verified ${LAST_VERIFIED}):\n${tried}\n\nAll endpoints may be down, or your network may be blocking HTTPS. Try curl-ing the URLs manually.`, { endpoints: errors });
}
// ─── Runtime Endpoint Management ────────────────────────────────────────────
// Add/remove/reorder RPC and LCD endpoints at runtime without code changes.
// The arrays above are `const` but are Objects (mutable contents).
/**
* Add an RPC endpoint at runtime. Skips if URL already exists.
* @param {string} url - RPC URL (e.g. 'https://rpc.newprovider.com')
* @param {string} [name='Custom'] - Provider name
* @param {boolean} [prepend=false] - If true, adds to front (highest priority)
*/
export function addRpcEndpoint(url, name = 'Custom', prepend = false) {
if (RPC_ENDPOINTS.some(e => e.url === url)) return;
const entry = { url, name, verified: new Date().toISOString().slice(0, 10) };
prepend ? RPC_ENDPOINTS.unshift(entry) : RPC_ENDPOINTS.push(entry);
}
/**
* Add an LCD endpoint at runtime. Skips if URL already exists.
* @param {string} url - LCD URL (e.g. 'https://api.newprovider.com')
* @param {string} [name='Custom'] - Provider name
* @param {boolean} [prepend=false] - If true, adds to front (highest priority)
*/
export function addLcdEndpoint(url, name = 'Custom', prepend = false) {
if (LCD_ENDPOINTS.some(e => e.url === url)) return;
const entry = { url, name, verified: new Date().toISOString().slice(0, 10) };
prepend ? LCD_ENDPOINTS.unshift(entry) : LCD_ENDPOINTS.push(entry);
}
/**
* Remove an RPC endpoint by URL.
* @param {string} url
*/
export function removeRpcEndpoint(url) {
const idx = RPC_ENDPOINTS.findIndex(e => e.url === url);
if (idx !== -1) RPC_ENDPOINTS.splice(idx, 1);
}
/**
* Remove an LCD endpoint by URL.
* @param {string} url
*/
export function removeLcdEndpoint(url) {
const idx = LCD_ENDPOINTS.findIndex(e => e.url === url);
if (idx !== -1) LCD_ENDPOINTS.splice(idx, 1);
}
/**
* Replace ALL endpoints at once (e.g. from a config file or remote registry).
* @param {'rpc'|'lcd'} type
* @param {Array<{url: string, name?: string}>} endpoints
*/
export function setEndpoints(type, endpoints) {
const target = type === 'rpc' ? RPC_ENDPOINTS : LCD_ENDPOINTS;
target.length = 0;
for (const ep of endpoints) {
target.push({ url: ep.url, name: ep.name || 'Custom', verified: new Date().toISOString().slice(0, 10) });
}
}
/**
* Get current endpoint lists (for inspection/debugging).
* @returns {{ rpc: Array, lcd: Array }}
*/
export function getEndpoints() {
return { rpc: [...RPC_ENDPOINTS], lcd: [...LCD_ENDPOINTS] };
}
/**
* Health-check RPC endpoints and reorder by latency (fastest first).
* Uses Tendermint /status endpoint which returns node info + sync status.
* @param {number} [timeoutMs=5000] - Timeout per endpoint
* @returns {Promise<Array<{url: string, name: string, latencyMs: number|null, blockHeight?: number}>>}
*/
export async function checkRpcEndpointHealth(timeoutMs = 5000) {
const results = await Promise.all(RPC_ENDPOINTS.map(async (ep) => {
const start = Date.now();
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const resp = await axios.get(`${ep.url}/status`, { signal: controller.signal, timeout: timeoutMs });
clearTimeout(timer);
const height = parseInt(resp.data?.result?.sync_info?.latest_block_height || '0', 10);
return { ...ep, latencyMs: Date.now() - start, blockHeight: height };
} catch {
return { ...ep, latencyMs: null };
}
}));
return results.sort((a, b) => {
if (a.latencyMs != null && b.latencyMs != null) return a.latencyMs - b.latencyMs;
if (a.latencyMs != null) return -1;
if (b.latencyMs != null) return 1;
return 0;
});
}
/**
* Health-check both RPC and LCD endpoints, reorder by latency.
* Moves fastest-responding endpoints to the front of each array.
* @param {number} [timeoutMs=5000]
* @returns {Promise<{ rpc: Array, lcd: Array }>}
*/
export async function optimizeEndpoints(timeoutMs = 5000) {
const [rpcResults, lcdResults] = await Promise.all([
checkRpcEndpointHealth(timeoutMs),
checkEndpointHealth(LCD_ENDPOINTS, timeoutMs),
]);
// Reorder arrays in-place: healthy first, by latency
const reorder = (target, results) => {
const healthy = results.filter(r => r.latencyMs != null);
const dead = results.filter(r => r.latencyMs == null);
target.length = 0;
for (const r of [...healthy, ...dead]) {
target.push({ url: r.url, name: r.name, verified: r.verified || LAST_VERIFIED });
}
};
reorder(RPC_ENDPOINTS, rpcResults);
reorder(LCD_ENDPOINTS, lcdResults);
return { rpc: rpcResults, lcd: lcdResults };
}