From 818f55d41c66e4a7705d53cb7e61b4e8eb1e405d Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 12:00:07 -0600 Subject: [PATCH 1/4] feat(testnet): discover evonodes with published dapi-client 5.0.2 Testnet IS lock streams ran on a list from the quorum service, falling back to a hard-coded snapshot of 20 evonodes, because dapi-client 3.1.0-dev.7's nested dashcore-lib 0.22 could not parse V24 masternode list diffs (CbTx v4). - Upgrade @dashevo/dapi-client to 5.0.2 and @dashevo/dashcore-lib to 0.26.1 (the version dash-spv 6.0.2 uses), leaving one dashcore-lib in the tree. - EvonodeDiscovery reads the valid evonodes from the client's synced simplified masternode list on every subscription. It skips the address provider, which on testnet filters by a snapshot bundled with the library. It replaces the client when dapi-client drops the list stream (which it never resyncs) or a sync misses its 10 s deadline, and falls back to the quorum service list. If both fail, the subscription rejects before broadcast. - The multi-node path (3 distinct streams, bounded height race, replacements, readiness cap, cleanup) now runs on the discovered nodes. Devnets keep their configured addresses; mainnet keeps JSON-RPC. - Discovery is warmed in the funding gate alongside the SDK connect, before the key backup download; idle visitors start none. - Drop TESTNET.dapiAddresses. Co-Authored-By: Claude Opus 5.5 --- e2e/deterministic.spec.ts | 32 ++-- package-lock.json | 199 +++++++++-------------- package.json | 4 +- src/api/dapi-subscription-multi.test.ts | 154 +++++++++++++++++- src/api/dapi-subscription.ts | 81 ++++++---- src/api/dapi.ts | 5 +- src/api/evonode-discovery.test.ts | 206 ++++++++++++++++++++++++ src/api/evonode-discovery.ts | 177 ++++++++++++++++++++ src/api/evonodes.test.ts | 97 ++++------- src/api/evonodes.ts | 78 ++++----- src/api/islock.test.ts | 15 +- src/api/islock.ts | 14 +- src/config.ts | 36 +---- src/main.ts | 24 +-- 14 files changed, 791 insertions(+), 331 deletions(-) create mode 100644 src/api/evonode-discovery.test.ts create mode 100644 src/api/evonode-discovery.ts diff --git a/e2e/deterministic.spec.ts b/e2e/deterministic.spec.ts index 0c3a5e1..026f179 100644 --- a/e2e/deterministic.spec.ts +++ b/e2e/deterministic.spec.ts @@ -28,42 +28,34 @@ async function advanceMockDeposit(page: Page) { } test.describe('Deterministic UI E2E (mock mode)', () => { - test('testnet discovers evonodes from the quorum service, never calls digitalcash.dev and does not load the SDK for the health poll', async ({ page }) => { + test('an idle testnet page never calls digitalcash.dev, starts no evonode discovery and does not load the SDK for the health poll', async ({ page }) => { const digitalcashRequests: string[] = []; const sdkRequests: string[] = []; + const discoveryRequests: string[] = []; page.on('request', (request) => { const url = new URL(request.url()); if (/(^|\.)digitalcash\.dev$/.test(url.hostname)) digitalcashRequests.push(request.url()); // The EvoSDK bundle/wasm must not be fetched just to draw the health indicator. if (/evo-sdk|wasm[-_]sdk/i.test(url.pathname)) sdkRequests.push(request.url()); + // Evonode discovery (masternode list from the seeds, quorum-service + // fallback) starts with a funding flow, not on page load. + if (/^seed-\d+\.testnet\.networks\.dash\.org$/.test(url.hostname) || url.pathname === '/masternodes') { + discoveryRequests.push(request.url()); + } }); - await page.route('https://quorums.testnet.networks.dash.org/masternodes', (route) => - route.fulfill({ - status: 200, - contentType: 'application/json', - headers: { 'access-control-allow-origin': '*' }, - body: JSON.stringify({ - success: true, - data: [1, 2, 3].map((n) => ({ - address: `203.0.113.${n}:19999`, - status: 'ENABLED', - versionCheck: 'success', - })), - }), - }) - ); - - // Client init (warmup) fetches the evonode list and starts network-status polling. - const evonodeRequest = page.waitForRequest('https://quorums.testnet.networks.dash.org/masternodes', { + + // Client init (warmup) loads the IS lock chunk and starts network-status polling. + const islockChunk = page.waitForRequest((request) => /\/islock(-[^/]*\.js|\.ts)$/.test(new URL(request.url()).pathname), { timeout: 15000, }); await page.goto(MOCK_QUERY); - await evonodeRequest; + await islockChunk; // Let the first network-status poll (Insight + Platform status) go out. await page.waitForTimeout(2000); expect(digitalcashRequests).toEqual([]); expect(sdkRequests).toEqual([]); + expect(discoveryRequests).toEqual([]); }); test('create identity flow transitions to completion and DPNS registration', async ({ page }) => { diff --git a/package-lock.json b/package-lock.json index b928c0a..5566045 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,8 +8,8 @@ "name": "mainnet-bridge", "version": "1.0.0", "dependencies": { - "@dashevo/dapi-client": "3.1.0-dev.7", - "@dashevo/dashcore-lib": "^0.22.0", + "@dashevo/dapi-client": "5.0.2", + "@dashevo/dashcore-lib": "0.26.1", "@dashevo/evo-sdk": "4.1.1", "@noble/hashes": "^1.4.0", "@noble/secp256k1": "^2.1.0", @@ -32,12 +32,12 @@ } }, "node_modules/@babel/code-frame": { - "version": "7.29.0", - "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.0.tgz", - "integrity": "sha512-9NhCeYjq9+3uxgdtp20LSiJXJvN0FeCtNGpJxuMFZ1Kv3cWUNb6DOhJwUvcVCzKGR66cw4njwM6hrJLqgOwbcw==", + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.7.tgz", + "integrity": "sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw==", "license": "MIT", "dependencies": { - "@babel/helper-validator-identifier": "^7.28.5", + "@babel/helper-validator-identifier": "^7.29.7", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" }, @@ -52,9 +52,9 @@ "license": "MIT" }, "node_modules/@babel/helper-validator-identifier": { - "version": "7.28.5", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.28.5.tgz", - "integrity": "sha512-qSs4ifwzKJSV39ucNjsvc6WVHs6b7S03sOh2OcHF9UHfVPqWWALUsNUVzhSBiItjRZoLHx7nIarVjqKVusUZ1Q==", + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz", + "integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==", "license": "MIT", "engines": { "node": ">=6.9.0" @@ -69,19 +69,19 @@ } }, "node_modules/@dashevo/dapi-client": { - "version": "3.1.0-dev.7", - "resolved": "https://registry.npmjs.org/@dashevo/dapi-client/-/dapi-client-3.1.0-dev.7.tgz", - "integrity": "sha512-WPJ47IFtKznHmcchbQ1LR4DunnWggC2qnyty8f8tJL7+Ay+2y2Pn7L+dmw/wQ+MgYOCqn++EUH9Skwi3xpZTZg==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@dashevo/dapi-client/-/dapi-client-5.0.2.tgz", + "integrity": "sha512-9CFsfxLxsu8ItuEtechtFrrF/xipLvCHbgZbYkD9Pf49Wh0OIbpvFp8piXU5wShMTYGmKhJ6o3R2/H8sBHnwOw==", "license": "MIT", "dependencies": { - "@dashevo/dapi-grpc": "3.1.0-dev.7", - "@dashevo/dash-spv": "4.1.0-dev.7", - "@dashevo/dashcore-lib": "~0.22.0", - "@dashevo/grpc-common": "3.1.0-dev.7", - "@dashevo/wasm-dpp": "3.1.0-dev.7", + "@dashevo/dapi-grpc": "5.0.2", + "@dashevo/dash-spv": "6.0.2", + "@dashevo/dashcore-lib": "~0.26.1", + "@dashevo/grpc-common": "5.0.2", + "@dashevo/wasm-dpp": "5.0.2", "cbor": "^8.0.0", "google-protobuf": "^3.12.2", - "undici": "^6.0.0", + "undici": "^6.28.1", "wasm-x11-hash": "~0.0.2" }, "engines": { @@ -89,14 +89,14 @@ } }, "node_modules/@dashevo/dapi-grpc": { - "version": "3.1.0-dev.7", - "resolved": "https://registry.npmjs.org/@dashevo/dapi-grpc/-/dapi-grpc-3.1.0-dev.7.tgz", - "integrity": "sha512-A98LnQqm92BfE7RuMaNwn1o5Gv59tMK3EtKnZqaTCWrkpdLq5+IV6hwrqp7WHnbzBjTc5+Hx+/K+IrP0brF3oA==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@dashevo/dapi-grpc/-/dapi-grpc-5.0.2.tgz", + "integrity": "sha512-cyDd3pjmc4XuFen/Ld9pxuEcFXvavMjrtc9g2AEtklQMxbR7sgeZzYesUmPcIgQt9V9mYyf8WURhbOYNypkTeg==", "license": "MIT", "dependencies": { - "@dashevo/grpc-common": "3.1.0-dev.7", + "@dashevo/grpc-common": "5.0.2", "@dashevo/protobufjs": "6.10.5", - "@grpc/grpc-js": "^1.14.3", + "@grpc/grpc-js": "^1.14.5", "@improbable-eng/grpc-web": "^0.15.0", "google-protobuf": "^3.12.2", "long": "^5.2.0" @@ -109,14 +109,14 @@ "license": "MIT" }, "node_modules/@dashevo/dash-spv": { - "version": "4.1.0-dev.7", - "resolved": "https://registry.npmjs.org/@dashevo/dash-spv/-/dash-spv-4.1.0-dev.7.tgz", - "integrity": "sha512-vCnvfLJE3ghN69Cj38jXKL4aNdysD/Jz570l7Q2gSqAASECh0DuATB8EEecn+sVW7aHkp4lRpkMEILWjcblhAg==", + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/@dashevo/dash-spv/-/dash-spv-6.0.2.tgz", + "integrity": "sha512-beSfx5kyYZfGGEUCeVCq+fZuQvflN+s8XoUpsuqp/G1sebT/C26R/7RBZGKhwaMP/iJPmFvQ31MahpWtcwNg8g==", "license": "MIT", "dependencies": { "@dashevo/dark-gravity-wave": "^1.1.1", "@dashevo/dash-util": "^2.0.3", - "@dashevo/dashcore-lib": "~0.22.0", + "@dashevo/dashcore-lib": "~0.26.1", "levelup": "^4.4.0", "memdown": "^5.1.0", "wasm-x11-hash": "~0.0.2" @@ -133,13 +133,14 @@ } }, "node_modules/@dashevo/dashcore-lib": { - "version": "0.22.0", - "resolved": "https://registry.npmjs.org/@dashevo/dashcore-lib/-/dashcore-lib-0.22.0.tgz", - "integrity": "sha512-9zvTcr8eFE+aeA/SVN5jsDgB/Kg6ozmsOY4u3gJdFJ6kQSU+yVyFNsflaWhHwRakZ7CVlU88P7AuDKneukp3zQ==", + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/@dashevo/dashcore-lib/-/dashcore-lib-0.26.1.tgz", + "integrity": "sha512-lJxRhSUaCSuFbgw7EQd02KlgFjDepfQ3M9ydEtX1lV674556lyMZw5xccXHkRLvEdXDuW/QwOQpmLDHOGRUIHw==", "license": "MIT", "dependencies": { "@dashevo/bls": "~1.2.9", "@dashevo/x11-hash-js": "^1.0.2", + "@noble/hashes": "^1.8.0", "@types/node": "^12.12.47", "bloom-filter": "^0.2.0", "bn.js": "^4.12.0", @@ -188,14 +189,14 @@ } }, "node_modules/@dashevo/grpc-common": { - "version": "3.1.0-dev.7", - "resolved": "https://registry.npmjs.org/@dashevo/grpc-common/-/grpc-common-3.1.0-dev.7.tgz", - "integrity": "sha512-2jdtis8ZextI/ROKG2Zf4eGy0Wv7Da/vyqez0h5WPXKWLL/Le7QsDzkR4U6EPqIZhpLxc/ckHDWXsij4QRYaLA==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@dashevo/grpc-common/-/grpc-common-5.0.2.tgz", + "integrity": "sha512-4K3fMQPkzBLhQT9XUzlIa5GcWEf7xVjiC2B96Usk2dFxalcUVFR+PINud3bh7nnqlyTFeVLSMUtrKaw77vlSKg==", "license": "MIT", "dependencies": { "@dashevo/protobufjs": "6.10.5", - "@grpc/grpc-js": "^1.14.3", - "@grpc/proto-loader": "^0.5.2", + "@grpc/grpc-js": "^1.14.5", + "@grpc/proto-loader": "^0.8.0", "cbor": "^8.0.0", "lodash": "^4.17.23", "long": "^5.2.0", @@ -241,9 +242,9 @@ "license": "Apache-2.0" }, "node_modules/@dashevo/wasm-dpp": { - "version": "3.1.0-dev.7", - "resolved": "https://registry.npmjs.org/@dashevo/wasm-dpp/-/wasm-dpp-3.1.0-dev.7.tgz", - "integrity": "sha512-8yvRgpP9rzpJpBPgeBDLU7fhM2jzVWuHy9KABnC2ap2/+fVG4kiWINVq7cgN01p7MKzbLthAOVmyW8LRod1xsw==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@dashevo/wasm-dpp/-/wasm-dpp-5.0.2.tgz", + "integrity": "sha512-GqfJS5EWbVi91h3XFvNlf06fHWirYDBv7tyJHL7OqkgPXOLMUJK/BGDNKXD1/ftpkDfRfRFRTB8bDQ86qFgR0g==", "license": "MIT", "dependencies": { "@dashevo/bls": "~1.2.9", @@ -676,9 +677,9 @@ } }, "node_modules/@grpc/grpc-js": { - "version": "1.14.4", - "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.14.4.tgz", - "integrity": "sha512-k9Dj3DV/itK9D06Y8f190Qgop7/Ui+D0njFV3LHMPwPT75DpXLQohE9Wmz0QElrJnzsjB7KPWiKJbOl7IPDArQ==", + "version": "1.14.7", + "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.14.7.tgz", + "integrity": "sha512-W4W/fcBMGRVtG6dW2c7cw2mnpS3hvbBd+oQXjh2iYFTZDk1JJmcNqkM5KSaPubQpamlUUurrlb7G1s9NJeERmQ==", "license": "Apache-2.0", "dependencies": { "@grpc/proto-loader": "^0.8.0", @@ -688,7 +689,7 @@ "node": ">=12.10.0" } }, - "node_modules/@grpc/grpc-js/node_modules/@grpc/proto-loader": { + "node_modules/@grpc/proto-loader": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.8.1.tgz", "integrity": "sha512-wtF6h+DY6M3YaDBPAmvuuA6jV8Sif9MjtOI5euKFWRgCDl5PeDpPsHR9u2l6St5ceY8AZgoNDww5+HvEsXFsGg==", @@ -706,7 +707,7 @@ "node": ">=6" } }, - "node_modules/@grpc/grpc-js/node_modules/ansi-styles": { + "node_modules/@grpc/proto-loader/node_modules/ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", @@ -721,7 +722,7 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, - "node_modules/@grpc/grpc-js/node_modules/cliui": { + "node_modules/@grpc/proto-loader/node_modules/cliui": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", "integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==", @@ -735,31 +736,7 @@ "node": ">=12" } }, - "node_modules/@grpc/grpc-js/node_modules/protobufjs": { - "version": "7.6.1", - "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.6.1.tgz", - "integrity": "sha512-4K0myLaWL5EteuSAro91EGFgcfVgxb64Jx+7oDAY6GOkXD4M69yuSEljNcInGVCA5sOPxmZ/EqDLj2x0Q0+Ygg==", - "hasInstallScript": true, - "license": "BSD-3-Clause", - "dependencies": { - "@protobufjs/aspromise": "^1.1.2", - "@protobufjs/base64": "^1.1.2", - "@protobufjs/codegen": "^2.0.5", - "@protobufjs/eventemitter": "^1.1.1", - "@protobufjs/fetch": "^1.1.1", - "@protobufjs/float": "^1.0.2", - "@protobufjs/inquire": "^1.1.2", - "@protobufjs/path": "^1.1.2", - "@protobufjs/pool": "^1.1.0", - "@protobufjs/utf8": "^1.1.1", - "@types/node": ">=13.7.0", - "long": "^5.3.2" - }, - "engines": { - "node": ">=12.0.0" - } - }, - "node_modules/@grpc/grpc-js/node_modules/wrap-ansi": { + "node_modules/@grpc/proto-loader/node_modules/wrap-ansi": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", @@ -776,7 +753,7 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, - "node_modules/@grpc/grpc-js/node_modules/y18n": { + "node_modules/@grpc/proto-loader/node_modules/y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", @@ -785,10 +762,10 @@ "node": ">=10" } }, - "node_modules/@grpc/grpc-js/node_modules/yargs": { - "version": "17.7.2", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz", - "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", + "node_modules/@grpc/proto-loader/node_modules/yargs": { + "version": "17.7.3", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.3.tgz", + "integrity": "sha512-GZtjxm/J/4TSxuL3FNYjCmLktBTnIw/rVmKSIyKeYAZpmJB2ig9VauCC5xsa82GNKVKDAqpOn3KVzNt0zmrU0g==", "license": "MIT", "dependencies": { "cliui": "^8.0.1", @@ -803,7 +780,7 @@ "node": ">=12" } }, - "node_modules/@grpc/grpc-js/node_modules/yargs-parser": { + "node_modules/@grpc/proto-loader/node_modules/yargs-parser": { "version": "21.1.1", "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz", "integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==", @@ -812,19 +789,6 @@ "node": ">=12" } }, - "node_modules/@grpc/proto-loader": { - "version": "0.5.6", - "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.6.tgz", - "integrity": "sha512-DT14xgw3PSzPxwS13auTEwxhMMOoz33DPUKNtmYK/QYbBSpLXJy78FGGs5yVoxVobEqPm4iW9MOIoz0A3bLTRQ==", - "license": "Apache-2.0", - "dependencies": { - "lodash.camelcase": "^4.3.0", - "protobufjs": "^6.8.6" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/@improbable-eng/grpc-web": { "version": "0.15.0", "resolved": "https://registry.npmjs.org/@improbable-eng/grpc-web/-/grpc-web-0.15.0.tgz", @@ -1023,9 +987,9 @@ "license": "BSD-3-Clause" }, "node_modules/@protobufjs/utf8": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.1.tgz", - "integrity": "sha512-oOAWABowe8EAbMyWKM0tYDKi8Yaox52D+HWZhAIJqQXbqe0xI/GV7FhLWqlEKreMkfDjshR5FKgi3mnle0h6Eg==", + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.2.tgz", + "integrity": "sha512-b1UQwcEZ4yCnMCD8DAL1VlbvBJE9/IX4FTIp7BG1xYpf29SLazLSrqUkj4w7Y5y7cCVP6E5tcqqcI0xemPkHug==", "license": "BSD-3-Clause" }, "node_modules/@rollup/plugin-inject": { @@ -2870,9 +2834,9 @@ } }, "node_modules/fastq": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.1.tgz", - "integrity": "sha512-GGToxJ/w1x32s/D2EKND7kTil4n8OVk/9mycTc4VDza13lOvpUZTGX3mFSCtV9ksdGBVzvsyAVLM6mHFThxXxw==", + "version": "1.20.3", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.3.tgz", + "integrity": "sha512-XKv5nnLs6nLF71NgiKJLIZFLkPyIEuOselLG7ujZnGrRfQK8HpvY+WqKhAJUAdLomwVHErVS4LfxFlPq0/FTAw==", "license": "ISC", "dependencies": { "reusify": "^1.0.4" @@ -4640,37 +4604,28 @@ "license": "MIT" }, "node_modules/protobufjs": { - "version": "6.11.6", - "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.6.tgz", - "integrity": "sha512-k8BHqgPBOtrlougZZqF2uUk5Z7bN8f0wj+3e8M3hvtSv0NBAz4VBy5f6R5Nxq/l+i7mRFTgNZb2trxqTpHNY/A==", + "version": "7.6.6", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.6.6.tgz", + "integrity": "sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg==", "hasInstallScript": true, "license": "BSD-3-Clause", "dependencies": { "@protobufjs/aspromise": "^1.1.2", "@protobufjs/base64": "^1.1.2", - "@protobufjs/codegen": "^2.0.4", - "@protobufjs/eventemitter": "^1.1.0", - "@protobufjs/fetch": "^1.1.0", + "@protobufjs/codegen": "^2.0.5", + "@protobufjs/eventemitter": "^1.1.1", + "@protobufjs/fetch": "^1.1.1", "@protobufjs/float": "^1.0.2", - "@protobufjs/inquire": "^1.1.0", "@protobufjs/path": "^1.1.2", "@protobufjs/pool": "^1.1.0", - "@protobufjs/utf8": "^1.1.0", - "@types/long": "^4.0.1", + "@protobufjs/utf8": "^1.1.1", "@types/node": ">=13.7.0", - "long": "^4.0.0" + "long": "^5.3.2" }, - "bin": { - "pbjs": "bin/pbjs", - "pbts": "bin/pbts" + "engines": { + "node": ">=12.0.0" } }, - "node_modules/protobufjs/node_modules/long": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", - "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==", - "license": "Apache-2.0" - }, "node_modules/prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", @@ -5076,9 +5031,9 @@ } }, "node_modules/semver": { - "version": "7.8.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.0.tgz", - "integrity": "sha512-AcM7dV/5ul4EekoQ29Agm5vri8JNqRyj39o0qpX6vDF2GZrtutZl5RwgD1XnZjiTAfncsJhMI48QQH3sN87YNA==", + "version": "7.8.6", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.6.tgz", + "integrity": "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw==", "license": "ISC", "bin": { "semver": "bin/semver.js" @@ -5310,9 +5265,9 @@ } }, "node_modules/spdx-license-ids": { - "version": "3.0.23", - "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.23.tgz", - "integrity": "sha512-CWLcCCH7VLu13TgOH+r8p1O/Znwhqv/dbb6lqWy67G+pT1kHmeD/+V36AVb/vq8QMIQwVShJ6Ssl5FPh0fuSdw==", + "version": "3.0.24", + "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.24.tgz", + "integrity": "sha512-cLS9TtWkIQFyLkJ3/5aFQAOHOSKTlOs/7WDut/XPSdjom1fJhUdkepeJAKXa9Y05+FabHd0sti+Et559vDtkpQ==", "license": "CC0-1.0" }, "node_modules/stackback": { @@ -5638,9 +5593,9 @@ "license": "MIT" }, "node_modules/undici": { - "version": "6.26.0", - "resolved": "https://registry.npmjs.org/undici/-/undici-6.26.0.tgz", - "integrity": "sha512-4yqz8a3n5HmGTlsbADNtr/dJlhkh/55Rq798G6ibiULcXbDtaLpTl1pvdqcbFfeoj3iSi52lePFM7h9H21cw/A==", + "version": "6.29.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.1.tgz", + "integrity": "sha512-EvRlUt/QqAEvXdQeRMYUfwOQsfzpDztykrG2owGEE/rBOBoYv9uF39ZzsNDh4lTFHhTr2XOZjKkYgueD12ZmGA==", "license": "MIT", "engines": { "node": ">=18.17" diff --git a/package.json b/package.json index 4204785..665875b 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ "test:e2e:install": "playwright install --with-deps chromium" }, "dependencies": { - "@dashevo/dapi-client": "3.1.0-dev.7", - "@dashevo/dashcore-lib": "^0.22.0", + "@dashevo/dapi-client": "5.0.2", + "@dashevo/dashcore-lib": "0.26.1", "@dashevo/evo-sdk": "4.1.1", "@noble/hashes": "^1.4.0", "@noble/secp256k1": "^2.1.0", diff --git a/src/api/dapi-subscription-multi.test.ts b/src/api/dapi-subscription-multi.test.ts index bc89145..b956b42 100644 --- a/src/api/dapi-subscription-multi.test.ts +++ b/src/api/dapi-subscription-multi.test.ts @@ -15,6 +15,10 @@ const state = vi.hoisted(() => ({ state.slowWaiters.splice(0).forEach((release) => release()); }, height: 100, + // Valid evonode IPs in the simplified masternode list that discovery clients sync. + smlHosts: [] as string[], + smlError: undefined as Error | undefined, + smlHangs: false, })); vi.mock('@dashevo/dapi-client', () => { @@ -25,9 +29,27 @@ vi.mock('@dashevo/dapi-client', () => { core: any; platform = {}; + dapiAddressProvider: any; + constructor(options: any) { this.options = options; state.clients.push(this); + if (!options.dapiAddresses) { + const smlProvider: any = { + stream: undefined, + getSimplifiedMNList: vi.fn(async () => { + if (state.smlHangs) return new Promise(() => {}); + if (state.smlError) { + smlProvider.stream = null; + throw state.smlError; + } + smlProvider.stream = {}; + const entries = state.smlHosts.map((ip) => ({ nType: 1, platformHTTPPort: 1443, getIp: () => ip })); + return { getValidMasternodesList: () => entries }; + }), + }; + this.dapiAddressProvider = { smlProvider }; + } this.core = { getBestBlockHeight: vi.fn(async () => { const only = options.dapiAddresses?.length === 1 ? options.dapiAddresses[0].host : undefined; @@ -77,6 +99,12 @@ vi.mock('@dashevo/dashcore-lib', () => ({ vi.mock('../utils/islock-debug.js', () => ({ describeIslock: () => ({}) })); +const fallbackMock = vi.hoisted(() => vi.fn<[], Promise>()); +vi.mock('./evonodes.js', async (importOriginal) => ({ + ...(await importOriginal()), + fetchTestnetEvonodeAddresses: fallbackMock, +})); + import { DAPISubscriptionClient, ISLOCK_CONCURRENT_NODES, @@ -127,6 +155,11 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { state.slowHosts.clear(); state.slowWaiters.length = 0; state.height = 100; + state.smlHosts = []; + state.smlError = undefined; + state.smlHangs = false; + fallbackMock.mockReset(); + fallbackMock.mockResolvedValue([]); vi.spyOn(console, 'log').mockImplementation(() => {}); vi.spyOn(console, 'warn').mockImplementation(() => {}); vi.spyOn(console, 'error').mockImplementation(() => {}); @@ -513,10 +546,10 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { await sub.wait(); }); - it('uses the single shared client with fewer than two addresses', async () => { + it('uses the single shared client with fewer than two configured addresses', async () => { for (const dapiAddresses of [undefined, [] as string[], addresses(1)]) { state.clients.length = 0; - const client = new DAPISubscriptionClient({ network: 'testnet', dapiAddresses }); + const client = new DAPISubscriptionClient({ network: 'devnet-moutai', dapiAddresses }); expect(state.clients).toHaveLength(1); const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT); @@ -580,4 +613,121 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { liveStreams()[0].emit('data', lockMessage(TXID)); await sub.wait(); }); + + describe('testnet evonode discovery', () => { + const hosts = (n: number): string[] => Array.from({ length: n }, (_, i) => `10.0.0.${i + 1}`); + const discoveryClients = (): any[] => state.clients.filter((c) => !c.options.dapiAddresses); + + it('opens the streams on distinct evonodes from the masternode list, not on the shared client', async () => { + state.smlHosts = hosts(6); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + expect(discoveryClients()).toHaveLength(1); + expect(discoveryClients()[0].options.network).toBe('testnet'); + + const sub = await startReady(client); + + const streamHosts = nodeClients().map((c) => c.options.dapiAddresses[0].host); + expect(streamHosts).toHaveLength(ISLOCK_CONCURRENT_NODES); + expect(new Set(streamHosts).size).toBe(ISLOCK_CONCURRENT_NODES); + for (const host of streamHosts) expect(state.smlHosts).toContain(host); + expect(discoveryClients()[0].core.subscribeToTransactionsWithProofs).not.toHaveBeenCalled(); + expect(fallbackMock).not.toHaveBeenCalled(); + + liveStreams()[1].emit('data', lockMessage(TXID)); + await expect(sub.wait()).resolves.toBeInstanceOf(Uint8Array); + }); + + it('subscribes with a single discovered evonode', async () => { + state.smlHosts = hosts(1); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + const sub = await startReady(client); + + expect(nodeClients()).toHaveLength(1); + liveStreams()[0].emit('data', lockMessage(TXID)); + await expect(sub.wait()).resolves.toBeInstanceOf(Uint8Array); + }); + + it('uses the list as it is at each subscription', async () => { + state.smlHosts = ['10.0.0.1']; + const client = new DAPISubscriptionClient({ network: 'testnet' }); + const first = await startReady(client); + liveStreams()[0].emit('data', lockMessage(TXID)); + await first.wait(); + + state.clients.length = 0; + state.smlHosts = ['10.0.0.9']; + await startReady(client); + + expect(nodeClients().map((c) => c.options.dapiAddresses[0].host)).toEqual(['10.0.0.9']); + }); + + it('falls back to the quorum service list when the masternode list fails', async () => { + state.smlError = new Error('Failed to parse payload'); + fallbackMock.mockResolvedValue(['https://10.1.0.1:1443', 'https://10.1.0.2:1443', 'https://10.1.0.3:1443']); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + await startReady(client); + + expect(fallbackMock).toHaveBeenCalledOnce(); + expect(nodeClients().map((c) => c.options.dapiAddresses[0].host).sort()).toEqual(['10.1.0.1', '10.1.0.2', '10.1.0.3']); + }); + + it('rejects before opening any stream when no evonode can be found', async () => { + state.smlError = new Error('No available addresses'); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + await expect(client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT)).rejects.toThrow( + /No testnet evonodes found/ + ); + expect(nodeClients()).toHaveLength(0); + }); + + it('disconnect() during discovery rejects with the abort error and opens nothing', async () => { + state.smlHangs = true; + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT); + const assertion = expect(pending).rejects.toThrow(`InstantSend lock subscription aborted for ${TXID}`); + await flush(); + await client.disconnect(); + await assertion; + + expect(nodeClients()).toHaveLength(0); + expect(discoveryClients()[0].disconnect).toHaveBeenCalled(); + }); + + it('a caller abort during discovery rejects with the abort error', async () => { + state.smlHangs = true; + const client = new DAPISubscriptionClient({ network: 'testnet' }); + const controller = new AbortController(); + + const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT, undefined, controller.signal); + const assertion = expect(pending).rejects.toThrow(`InstantSend lock subscription aborted for ${TXID}`); + await flush(); + controller.abort(); + await assertion; + expect(nodeClients()).toHaveLength(0); + }); + + it('warmEvonodeDiscovery() syncs the list without opening streams', async () => { + state.smlHosts = hosts(3); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + await client.warmEvonodeDiscovery(); + + expect(discoveryClients()[0].dapiAddressProvider.smlProvider.getSimplifiedMNList).toHaveBeenCalledOnce(); + expect(nodeClients()).toHaveLength(0); + }); + + it('configured addresses (devnets) never start discovery', async () => { + const client = new DAPISubscriptionClient({ network: 'devnet-moutai', dapiAddresses: addresses(3) }); + + await client.warmEvonodeDiscovery(); + await startReady(client); + + expect(discoveryClients()).toHaveLength(0); + expect(fallbackMock).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/api/dapi-subscription.ts b/src/api/dapi-subscription.ts index 5173a14..0afd6a1 100644 --- a/src/api/dapi-subscription.ts +++ b/src/api/dapi-subscription.ts @@ -5,6 +5,7 @@ import dashcoreLib from '@dashevo/dashcore-lib'; import { getNetwork } from '../config.js'; import { hash160 } from '../crypto/hash.js'; import { describeIslock } from '../utils/islock-debug.js'; +import { EvonodeDiscovery, parseDapiAddress, type DapiAddress } from './evonode-discovery.js'; import { fetchPlatformStatus, fetchPlatformStatusIfSdkReady, @@ -46,12 +47,6 @@ export const ISLOCK_READINESS_CAP_MS = 3000; const CLIENT_OPTIONS = { timeout: 30000, retries: 3 }; -interface DapiAddress { - protocol: string; - host: string; - port: number; -} - interface NodeSlot { client?: any; stream?: any; @@ -69,7 +64,11 @@ function shuffled(items: readonly T[]): T[] { export class DAPISubscriptionClient { readonly network: string; - private readonly dapiClient: any; + // Exactly one of these is set: testnet discovers its evonodes, every other + // network uses one client over its configured addresses (or, on mainnet, + // dapi-client's default seeds). + private readonly discovery?: EvonodeDiscovery; + private readonly fixedClient?: any; private readonly nodeAddresses: DapiAddress[]; // Aborts of in-flight multi-node subscriptions. DAPIClient.disconnect() does // not cancel streams, so disconnect() must stop these to close the per-node clients. @@ -77,27 +76,39 @@ export class DAPISubscriptionClient { constructor(config: DAPISubscriptionConfig) { this.network = config.network; - const options: any = { ...CLIENT_OPTIONS }; this.nodeAddresses = []; if (config.dapiAddresses?.length) { // DAPIClient expects address objects { host, port, protocol }, not URL strings. // Our configs store URLs (https://host:port) for EvoSDK compatibility, so // parse them here. - const parsed: DapiAddress[] = config.dapiAddresses.map((addr) => { - const url = new URL(addr); - return { - protocol: url.protocol.replace(':', ''), - host: url.hostname, - port: url.port ? parseInt(url.port, 10) : 443, - }; - }); - options.dapiAddresses = parsed; + const parsed = config.dapiAddresses.map(parseDapiAddress); // A repeated address would put two "distinct" streams on one node. this.nodeAddresses = [...new Map(parsed.map((a) => [`${a.host}:${a.port}`, a])).values()]; + this.fixedClient = new DAPIClientClass({ ...CLIENT_OPTIONS, dapiAddresses: parsed }); + } else if (getNetwork(config.network).type === 'testnet') { + this.discovery = new EvonodeDiscovery( + () => new DAPIClientClass({ ...CLIENT_OPTIONS, network: 'testnet' }) + ); } else { - options.network = config.network === 'mainnet' ? 'mainnet' : 'testnet'; + this.fixedClient = new DAPIClientClass({ + ...CLIENT_OPTIONS, + network: config.network === 'mainnet' ? 'mainnet' : 'testnet', + }); } - this.dapiClient = new DAPIClientClass(options); + } + + /** Client for calls that may go to any node. */ + private get dapiClient(): any { + return this.discovery ? this.discovery.sharedClient : this.fixedClient; + } + + /** + * Start syncing the testnet masternode list so the first IS lock + * subscription doesn't wait for it. Never rejects; resolves at once on + * networks with configured addresses. + */ + warmEvonodeDiscovery(): Promise { + return this.discovery ? this.discovery.warm() : Promise.resolve(); } /** A client pinned to one node, so streams and height probes can't land on the same node twice. */ @@ -259,27 +270,38 @@ export class DAPISubscriptionClient { onProgress?.('Creating bloom filter...'); const bloomFilter = this.createBloomFilter(hash160(publicKey), utxo); - onProgress?.('Getting current block height...'); - if (this.nodeAddresses.length >= 2) { + if (this.discovery || this.nodeAddresses.length >= 2) { // The shared client picks one random node and waits out its full connect // timeout (~75 s in browsers) when that node is unreachable, with nothing // broadcast meanwhile. Ask the nodes the streams will use, all at once. - const nodes = shuffled(this.nodeAddresses); - const clients = nodes - .slice(0, ISLOCK_CONCURRENT_NODES) - .map((address) => this.tryCreateNodeClient(address)); - // disconnect() (e.g. a network switch) must also stop this wait, or the + // disconnect() (e.g. a network switch) must also stop these waits, or the // streams below would open on the network being left. const raceAbort = new AbortController(); const abortRace = (): void => raceAbort.abort(); if (signal?.aborted) abortRace(); signal?.addEventListener('abort', abortRace, { once: true }); this.activeSubscriptions.add(abortRace); + let nodes: DapiAddress[] = []; + let clients: Array = []; let currentHeight: number; try { + let candidates = this.nodeAddresses; + if (this.discovery) { + onProgress?.('Finding evonodes...'); + candidates = await this.discovery.getNodes(raceAbort.signal); + console.log(`[islock-sub] discovered ${candidates.length} evonodes`); + } + onProgress?.('Getting current block height...'); + nodes = shuffled(candidates); + clients = nodes + .slice(0, ISLOCK_CONCURRENT_NODES) + .map((address) => this.tryCreateNodeClient(address)); currentHeight = await this.raceBestBlockHeight(txid, clients, raceAbort.signal); } catch (error) { for (const client of clients) this.disconnectQuietly(client); + if (raceAbort.signal.aborted) { + throw new Error(`InstantSend lock subscription aborted for ${txid}`); + } throw error; } finally { this.activeSubscriptions.delete(abortRace); @@ -291,6 +313,7 @@ export class DAPISubscriptionClient { ); } + onProgress?.('Getting current block height...'); const currentHeight: number = await this.dapiClient.core.getBestBlockHeight(); console.log('[islock-sub] Current block height:', currentHeight); const fromBlockHeight = Math.max(1, currentHeight - 10); @@ -560,7 +583,11 @@ export class DAPISubscriptionClient { async disconnect(): Promise { for (const abort of [...this.activeSubscriptions]) abort(); - try { await this.dapiClient.disconnect(); } catch { /* ignore */ } + if (this.discovery) { + await this.discovery.disconnect(); + return; + } + try { await this.fixedClient.disconnect(); } catch { /* ignore */ } } /** diff --git a/src/api/dapi.ts b/src/api/dapi.ts index c16ccac..33d9e25 100644 --- a/src/api/dapi.ts +++ b/src/api/dapi.ts @@ -7,8 +7,9 @@ import { describeIslock } from '../utils/islock-debug.js'; import { abortableSleep } from '../utils/sleep.js'; import { fetchJson } from '../utils/fetch-json.js'; -// Testnet has no default: it reads IS locks from the DAPI bloom stream (see -// TESTNET.dapiAddresses) because trpc.digitalcash.dev never upgraded to V24. +// Testnet has no default: it reads IS locks from the DAPI bloom stream on +// evonodes it discovers (see api/evonode-discovery.ts) because +// trpc.digitalcash.dev never upgraded to V24. const API_URLS: Record = { mainnet: 'https://rpc.digitalcash.dev', }; diff --git a/src/api/evonode-discovery.test.ts b/src/api/evonode-discovery.test.ts new file mode 100644 index 0000000..29e2a74 --- /dev/null +++ b/src/api/evonode-discovery.test.ts @@ -0,0 +1,206 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { + DISCOVERY_TIMEOUT_MS, + EvonodeDiscovery, + evonodeEndpointsFromSml, + parseDapiAddress, +} from './evonode-discovery.js'; + +const entry = (ip: string, overrides: Record = {}) => ({ + nType: 1, + platformHTTPPort: 1443, + getIp: () => ip, + ...overrides, +}); +const sml = (...entries: unknown[]) => ({ getValidMasternodesList: () => entries }); + +/** A stand-in for a dapi-client instance: just its masternode list provider and disconnect(). */ +function fakeClient(getList: () => Promise) { + const smlProvider: any = { + stream: undefined, + getSimplifiedMNList: vi.fn(async () => { + const list = await getList(); + smlProvider.stream = {}; + return list; + }), + }; + return { dapiAddressProvider: { smlProvider }, disconnect: vi.fn(async () => {}) }; +} + +describe('evonodeEndpointsFromSml', () => { + it('keeps valid evonodes, using their Platform HTTP port, without duplicates', () => { + const result = evonodeEndpointsFromSml( + sml( + entry('68.67.122.1'), + entry('68.67.122.2', { nType: 0 }), // regular masternode: no DAPI + entry('68.67.122.3', { platformHTTPPort: '2443' }), + entry('68.67.122.1'), + entry('0.0.0.0.0'), + entry('68.67.122.4', { platformHTTPPort: undefined }), + entry('68.67.122.5', { getIp: undefined }), + null + ) + ); + + expect(result).toEqual(['https://68.67.122.1:1443', 'https://68.67.122.3:2443']); + }); + + it.each([undefined, null, {}, { getValidMasternodesList: () => undefined }])( + 'returns nothing for list %j', + (bad) => { + expect(evonodeEndpointsFromSml(bad)).toEqual([]); + } + ); +}); + +describe('parseDapiAddress', () => { + it('splits a URL into the address object dapi-client takes', () => { + expect(parseDapiAddress('https://68.67.122.1:1443')).toEqual({ protocol: 'https', host: '68.67.122.1', port: 1443 }); + expect(parseDapiAddress('https://evo.example.org')).toEqual({ protocol: 'https', host: 'evo.example.org', port: 443 }); + }); +}); + +describe('EvonodeDiscovery', () => { + const fallback = vi.fn<[], Promise>(); + let clients: ReturnType[]; + let lists: Array<() => Promise>; + + const createClient = () => { + const client = fakeClient(lists[Math.min(clients.length, lists.length - 1)]); + clients.push(client); + return client; + }; + + beforeEach(() => { + vi.useFakeTimers(); + vi.spyOn(console, 'warn').mockImplementation(() => {}); + fallback.mockReset(); + fallback.mockResolvedValue([]); + clients = []; + lists = [async () => sml(entry('10.0.0.1'), entry('10.0.0.2'))]; + }); + + afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); + }); + + it('returns the evonodes from the masternode list and does not ask the quorum service', async () => { + const discovery = new EvonodeDiscovery(createClient, fallback); + + await expect(discovery.getNodes()).resolves.toEqual([ + { protocol: 'https', host: '10.0.0.1', port: 1443 }, + { protocol: 'https', host: '10.0.0.2', port: 1443 }, + ]); + expect(fallback).not.toHaveBeenCalled(); + expect(discovery.sharedClient).toBe(clients[0]); + }); + + it('reads the current list on every call, so updates from the list stream are seen', async () => { + let current = sml(entry('10.0.0.1')); + lists = [async () => current]; + const discovery = new EvonodeDiscovery(createClient, fallback); + + expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.1']); + current = sml(entry('10.0.0.7'), entry('10.0.0.8')); + expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.7', '10.0.0.8']); + expect(clients).toHaveLength(1); + }); + + it('shares one in-flight sync between concurrent callers', async () => { + let release!: () => void; + lists = [() => new Promise((resolve) => { release = () => resolve(sml(entry('10.0.0.1'))); })]; + const discovery = new EvonodeDiscovery(createClient, fallback); + + const a = discovery.getNodes(); + const b = discovery.getNodes(); + const warm = discovery.warm(); + release(); + + await expect(a).resolves.toHaveLength(1); + await expect(b).resolves.toHaveLength(1); + await expect(warm).resolves.toBeUndefined(); + expect(clients[0].dapiAddressProvider.smlProvider.getSimplifiedMNList).toHaveBeenCalledTimes(1); + }); + + it('falls back to the quorum service when the masternode list sync fails', async () => { + lists = [async () => { throw new Error('Failed to parse payload'); }]; + fallback.mockResolvedValue(['https://10.0.0.9:1443']); + const discovery = new EvonodeDiscovery(createClient, fallback); + + await expect(discovery.getNodes()).resolves.toEqual([{ protocol: 'https', host: '10.0.0.9', port: 1443 }]); + expect(fallback).toHaveBeenCalledOnce(); + }); + + it('falls back to the quorum service when the list has no valid evonodes', async () => { + lists = [async () => sml(entry('10.0.0.1', { nType: 0 }))]; + fallback.mockResolvedValue(['https://10.0.0.9:1443']); + const discovery = new EvonodeDiscovery(createClient, fallback); + + await expect(discovery.getNodes()).resolves.toHaveLength(1); + }); + + it(`falls back after ${DISCOVERY_TIMEOUT_MS} ms and starts over on a new client next time`, async () => { + lists = [() => new Promise(() => {}), async () => sml(entry('10.0.0.5'))]; + fallback.mockResolvedValue(['https://10.0.0.9:1443']); + const discovery = new EvonodeDiscovery(createClient, fallback); + + const first = discovery.getNodes(); + await vi.advanceTimersByTimeAsync(DISCOVERY_TIMEOUT_MS); + expect((await first).map((a) => a.host)).toEqual(['10.0.0.9']); + + expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.5']); + expect(clients).toHaveLength(2); + expect(clients[0].disconnect).toHaveBeenCalled(); + expect(discovery.sharedClient).toBe(clients[1]); + }); + + it('replaces the client once dapi-client dropped its list stream, instead of serving the stale list', async () => { + lists = [async () => sml(entry('10.0.0.1')), async () => sml(entry('10.0.0.2'))]; + const discovery = new EvonodeDiscovery(createClient, fallback); + await discovery.getNodes(); + + // What dapi-client does when the masternode list stream errors or ends. + clients[0].dapiAddressProvider.smlProvider.stream = null; + + expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.2']); + expect(clients).toHaveLength(2); + await vi.advanceTimersByTimeAsync(0); + expect(clients[0].disconnect).toHaveBeenCalled(); + }); + + it('rejects when neither source yields an evonode, so nothing is broadcast', async () => { + lists = [async () => { throw new Error('No available addresses'); }]; + const discovery = new EvonodeDiscovery(createClient, fallback); + + await expect(discovery.getNodes()).rejects.toThrow(/No testnet evonodes found/); + }); + + it('rejects at once when the signal aborts during the sync', async () => { + lists = [() => new Promise(() => {})]; + const discovery = new EvonodeDiscovery(createClient, fallback); + const controller = new AbortController(); + + const pending = discovery.getNodes(controller.signal); + controller.abort(); + + await expect(pending).rejects.toThrow(/aborted/); + expect(fallback).not.toHaveBeenCalled(); + }); + + it('warm() settles without rejecting when the sync fails', async () => { + lists = [async () => { throw new Error('boom'); }]; + const discovery = new EvonodeDiscovery(createClient, fallback); + + await expect(discovery.warm()).resolves.toBeUndefined(); + expect(fallback).not.toHaveBeenCalled(); + }); + + it('disconnect() closes the client', async () => { + const discovery = new EvonodeDiscovery(createClient, fallback); + await discovery.disconnect(); + expect(clients[0].disconnect).toHaveBeenCalledOnce(); + }); +}); diff --git a/src/api/evonode-discovery.ts b/src/api/evonode-discovery.ts new file mode 100644 index 0000000..f971c88 --- /dev/null +++ b/src/api/evonode-discovery.ts @@ -0,0 +1,177 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +import { fetchTestnetEvonodeAddresses, toDapiEndpoint } from './evonodes.js'; + +/** A DAPI endpoint in the shape dapi-client's `dapiAddresses` option takes. */ +export interface DapiAddress { + protocol: string; + host: string; + port: number; +} + +/** Longest a masternode list sync may take before the quorum service is asked instead. */ +export const DISCOVERY_TIMEOUT_MS = 10000; + +/** SimplifiedMNListEntry.nType of evonodes (HP masternodes), the only ones serving DAPI. */ +const EVONODE_TYPE = 1; + +/** Parse a `https://host:port` URL into a dapi-client address. */ +export function parseDapiAddress(url: string): DapiAddress { + const parsed = new URL(url); + return { + protocol: parsed.protocol.replace(':', ''), + host: parsed.hostname, + port: parsed.port ? parseInt(parsed.port, 10) : 443, + }; +} + +/** Distinct DAPI endpoints of the valid evonodes in a simplified masternode list. */ +export function evonodeEndpointsFromSml(sml: any): string[] { + const entries: any[] = sml?.getValidMasternodesList?.() ?? []; + const endpoints = new Set(); + for (const entry of entries) { + if (entry?.nType !== EVONODE_TYPE) continue; + const endpoint = toDapiEndpoint(entry.getIp?.(), Number(entry.platformHTTPPort)); + if (endpoint) endpoints.add(endpoint); + } + return [...endpoints]; +} + +class DeadlineError extends Error {} + +function rejectAfter(ms: number, message: string): { promise: Promise; clear: () => void } { + let timer: ReturnType | undefined; + const promise = new Promise((_, reject) => { + timer = setTimeout(() => reject(new DeadlineError(message)), ms); + }); + return { promise, clear: () => clearTimeout(timer) }; +} + +function rejectOnAbort(signal: AbortSignal | undefined, message: string): { promise: Promise; clear: () => void } { + let onAbort: (() => void) | undefined; + const promise = new Promise((_, reject) => { + if (!signal) return; + onAbort = () => reject(new Error(message)); + if (signal.aborted) onAbort(); + else signal.addEventListener('abort', onAbort, { once: true }); + }); + return { promise, clear: () => { if (onAbort) signal?.removeEventListener('abort', onAbort); } }; +} + +/** + * Finds the testnet evonodes to open IS lock streams on, using dapi-client's + * own masternode-list discovery: the client syncs the simplified masternode + * list from the network seeds and keeps it current over a stream, so every + * call sees the list as of the latest block. + * + * The valid evonodes are read from the list directly instead of through the + * client's address provider, which on testnet also filters them by a snapshot + * of addresses bundled with the library. Bad nodes in the list cost little: + * the subscription races several and replaces failed ones. + * + * When the masternode list can't be synced in time, the quorum service list + * is used instead. When both fail, getNodes() rejects, so the caller stops + * before broadcasting. + */ +export class EvonodeDiscovery { + private client: any; + // The current client's in-flight list read, shared by concurrent callers so + // they don't open a second masternode list stream. + private pendingRead?: Promise; + // Set when a read missed its deadline: the next read starts over on a new + // client instead of waiting on a sync that may never finish. + private replaceClient = false; + + constructor( + private readonly createClient: () => any, + private readonly fetchFallback: () => Promise = fetchTestnetEvonodeAddresses, + private readonly timeoutMs: number = DISCOVERY_TIMEOUT_MS + ) { + this.client = createClient(); + } + + /** The discovery client, for calls that may go to any node (e.g. getTransaction). */ + get sharedClient(): any { + return this.client; + } + + /** + * The current evonode endpoints. Rejects when neither the masternode list + * nor the quorum service yields one, or when `signal` aborts. + */ + async getNodes(signal?: AbortSignal): Promise { + const abortMessage = 'Evonode discovery aborted'; + const abort = rejectOnAbort(signal, abortMessage); + try { + let endpoints = await Promise.race([this.readListWithDeadline(), abort.promise]); + if (endpoints.length === 0) { + endpoints = await Promise.race([this.fetchFallback(), abort.promise]); + if (endpoints.length > 0) { + console.warn(`[evonode-discovery] using ${endpoints.length} evonodes from the quorum service`); + } + } + if (endpoints.length === 0) { + throw new Error('No testnet evonodes found: the masternode list and the quorum service both failed'); + } + return endpoints.map(parseDapiAddress); + } finally { + abort.clear(); + } + } + + /** + * Start syncing the masternode list ahead of the first getNodes(). Settles + * (never rejects) once the list is read, the sync fails, or the deadline + * passes; the sync keeps going after the deadline. + */ + async warm(): Promise { + await this.readListWithDeadline(); + } + + async disconnect(): Promise { + this.pendingRead = undefined; + try { await this.client.disconnect(); } catch { /* ignore */ } + } + + /** Endpoints from the masternode list, or [] when it fails or misses the deadline. */ + private async readListWithDeadline(): Promise { + const deadline = rejectAfter(this.timeoutMs, `masternode list sync timed out after ${this.timeoutMs}ms`); + try { + const endpoints = await Promise.race([this.readList(), deadline.promise]); + if (endpoints.length === 0) console.warn('[evonode-discovery] masternode list has no valid evonodes'); + return endpoints; + } catch (error) { + if (error instanceof DeadlineError) this.replaceClient = true; + console.warn('[evonode-discovery] masternode list unavailable:', error); + return []; + } finally { + deadline.clear(); + } + } + + private readList(): Promise { + // dapi-client sets the list stream to null once it errors or ends, and + // from then on returns the last list without ever resyncing. Replace the + // client then (or after a missed deadline), so a dropped or stuck stream + // (a network change, an iOS download prompt) can't leave a stale list. + if (this.replaceClient || this.client.dapiAddressProvider?.smlProvider?.stream === null) { + this.replaceClient = false; + const stale = this.client; + this.client = this.createClient(); + this.pendingRead = undefined; + Promise.resolve().then(() => stale.disconnect()).catch(() => {}); + } + const provider = this.client.dapiAddressProvider?.smlProvider; + if (!provider) return Promise.reject(new Error('dapi-client has no masternode list provider')); + + if (!this.pendingRead) { + const read = Promise.resolve(provider.getSimplifiedMNList()).then(evonodeEndpointsFromSml); + const clear = (): void => { + if (this.pendingRead === read) this.pendingRead = undefined; + }; + read.then(clear, clear); + this.pendingRead = read; + } + return this.pendingRead; + } +} diff --git a/src/api/evonodes.test.ts b/src/api/evonodes.test.ts index de4f0f2..c547b3c 100644 --- a/src/api/evonodes.test.ts +++ b/src/api/evonodes.test.ts @@ -1,11 +1,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { - MIN_DYNAMIC_EVONODES, TESTNET_MASTERNODES_URL, + fetchTestnetEvonodeAddresses, parseEvonodeAddresses, - resolveDapiAddresses, - resolveTestnetDapiAddresses, + toDapiEndpoint, } from './evonodes.js'; const node = (host: string, overrides: Record = {}) => ({ @@ -20,8 +19,6 @@ const node = (host: string, overrides: Record = {}) => ({ const payload = (...nodes: unknown[]) => ({ success: true, data: nodes }); -const FALLBACK = ['https://1.1.1.1:1443', 'https://1.1.1.2:1443', 'https://1.1.1.3:1443']; - describe('parseEvonodeAddresses', () => { it('keeps ENABLED nodes with a successful version check and maps them to :1443', () => { const result = parseEvonodeAddresses( @@ -72,7 +69,27 @@ describe('parseEvonodeAddresses', () => { ); }); -describe('resolveTestnetDapiAddresses', () => { +describe('toDapiEndpoint', () => { + it('builds an https endpoint, defaulting to port 1443', () => { + expect(toDapiEndpoint('68.67.122.1')).toBe('https://68.67.122.1:1443'); + expect(toDapiEndpoint('68.67.122.1', 443)).toBe('https://68.67.122.1:443'); + }); + + it.each([ + ['999.1.1.1', 1443], + ['[::1]', 1443], + ['user@host', 1443], + [undefined, 1443], + ['68.67.122.1', 0], + ['68.67.122.1', 70000], + ['68.67.122.1', Number.NaN], + ['68.67.122.1', '1443'], + ])('rejects host %j port %j', (host, port) => { + expect(toDapiEndpoint(host, port)).toBeUndefined(); + }); +}); + +describe('fetchTestnetEvonodeAddresses', () => { const fetchMock = vi.fn(); beforeEach(() => { @@ -95,55 +112,31 @@ describe('resolveTestnetDapiAddresses', () => { json: async () => body, }); - it('uses only the live list when it has enough nodes', async () => { - respond(payload(node('10.0.0.1'), node('10.0.0.2'), node('10.0.0.3'), node('10.0.0.4'))); - - const result = await resolveTestnetDapiAddresses(FALLBACK); + it('returns the healthy nodes from the quorum service', async () => { + respond(payload(node('10.0.0.1'), node('10.0.0.2', { status: 'POSE_BANNED' }), node('10.0.0.3'))); - expect(result).toEqual([ + await expect(fetchTestnetEvonodeAddresses()).resolves.toEqual([ 'https://10.0.0.1:1443', - 'https://10.0.0.2:1443', 'https://10.0.0.3:1443', - 'https://10.0.0.4:1443', ]); expect(fetchMock).toHaveBeenCalledOnce(); expect(fetchMock.mock.calls[0][0]).toBe(TESTNET_MASTERNODES_URL); }); - it(`tops up with the static list below ${MIN_DYNAMIC_EVONODES} live nodes, without duplicates`, async () => { - respond(payload(node('10.0.0.1'), node('1.1.1.2'))); - - const result = await resolveTestnetDapiAddresses(FALLBACK); - - expect(result).toEqual([ - 'https://10.0.0.1:1443', - 'https://1.1.1.2:1443', - 'https://1.1.1.1:1443', - 'https://1.1.1.3:1443', - ]); - }); - - it('falls back to the static list when no node is healthy', async () => { - respond(payload(node('10.0.0.1', { status: 'POSE_BANNED' }))); - - await expect(resolveTestnetDapiAddresses(FALLBACK)).resolves.toEqual(FALLBACK); - }); - - it('falls back to the static list on a network error, without retrying', async () => { + it('returns nothing on a network error, without retrying', async () => { fetchMock.mockRejectedValue(new TypeError('Failed to fetch')); - await expect(resolveTestnetDapiAddresses(FALLBACK)).resolves.toEqual(FALLBACK); + await expect(fetchTestnetEvonodeAddresses()).resolves.toEqual([]); expect(fetchMock).toHaveBeenCalledOnce(); }); - it('falls back to the static list on an HTTP error', async () => { + it('returns nothing on an HTTP error', async () => { respond({}, false); - await expect(resolveTestnetDapiAddresses(FALLBACK)).resolves.toEqual(FALLBACK); - expect(fetchMock).toHaveBeenCalledOnce(); + await expect(fetchTestnetEvonodeAddresses()).resolves.toEqual([]); }); - it('falls back to the static list on invalid JSON', async () => { + it('returns nothing on invalid JSON', async () => { fetchMock.mockResolvedValue({ ok: true, status: 200, @@ -153,30 +146,10 @@ describe('resolveTestnetDapiAddresses', () => { }, }); - await expect(resolveTestnetDapiAddresses(FALLBACK)).resolves.toEqual(FALLBACK); - }); - - describe('resolveDapiAddresses', () => { - it('resolves the live list for testnet', async () => { - respond(payload(node('10.0.0.1'), node('10.0.0.2'), node('10.0.0.3'))); - - const result = await resolveDapiAddresses({ type: 'testnet', dapiAddresses: FALLBACK }); - - expect(result).toHaveLength(3); - expect(result?.[0]).toBe('https://10.0.0.1:1443'); - }); - - it('makes no request for devnets, mainnet or networks without addresses', async () => { - const devnet = ['https://68.67.122.254:1443']; - - await expect(resolveDapiAddresses({ type: 'devnet', dapiAddresses: devnet })).resolves.toBe(devnet); - await expect(resolveDapiAddresses({ type: 'mainnet' })).resolves.toBeUndefined(); - await expect(resolveDapiAddresses({ type: 'testnet' })).resolves.toBeUndefined(); - expect(fetchMock).not.toHaveBeenCalled(); - }); + await expect(fetchTestnetEvonodeAddresses()).resolves.toEqual([]); }); - it('gives up after the timeout and uses the static list', async () => { + it('gives up after the 5 s timeout', async () => { vi.useFakeTimers(); fetchMock.mockImplementation( (_url: string, init?: RequestInit) => @@ -187,10 +160,10 @@ describe('resolveTestnetDapiAddresses', () => { }) ); - const result = resolveTestnetDapiAddresses(FALLBACK); + const result = fetchTestnetEvonodeAddresses(); await vi.advanceTimersByTimeAsync(5000); - await expect(result).resolves.toEqual(FALLBACK); + await expect(result).resolves.toEqual([]); expect(fetchMock).toHaveBeenCalledOnce(); }); }); diff --git a/src/api/evonodes.ts b/src/api/evonodes.ts index aa10209..20f7ebe 100644 --- a/src/api/evonodes.ts +++ b/src/api/evonodes.ts @@ -1,25 +1,41 @@ -import type { NetworkConfig } from '../config.js'; import { fetchJson } from '../utils/fetch-json.js'; /** * Quorum service lists every testnet masternode with its PoSe status and the * result of its DAPI version check. It answers CORS requests from any origin * (`access-control-allow-origin: *`, including preflights), so browsers can - * read it directly. + * read it directly. Used only as the fallback when dapi-client's masternode + * list discovery fails (see evonode-discovery.ts). */ export const TESTNET_MASTERNODES_URL = 'https://quorums.testnet.networks.dash.org/masternodes'; const EVONODE_FETCH_TIMEOUT_MS = 5000; -/** Below this many live nodes the fetched list is not trusted on its own. */ -export const MIN_DYNAMIC_EVONODES = 3; - -const DAPI_HTTPS_PORT = 1443; +/** Port of the evonode DAPI endpoint (gRPC-web over HTTPS). */ +export const DAPI_HTTPS_PORT = 1443; // Plain IPv4 / DNS hostnames only: the result is turned into a URL and handed // to dapi-client, so anything exotic (IPv6 literals, userinfo, paths) is dropped. const HOST_PATTERN = /^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$/; +/** + * `https://:` when `host` and `port` make a usable DAPI endpoint, + * otherwise undefined. + */ +export function toDapiEndpoint(host: unknown, port: unknown = DAPI_HTTPS_PORT): string | undefined { + if (typeof host !== 'string' || !HOST_PATTERN.test(host)) return undefined; + if (typeof port !== 'number' || !Number.isInteger(port) || port <= 0 || port > 65535) return undefined; + const endpoint = `https://${host}:${port}`; + // The regex alone admits non-URLs such as 999.1.1.1, and one of those + // would make the DAPI client constructor throw for the whole list. + try { + new URL(endpoint); + } catch { + return undefined; + } + return endpoint; +} + /** * Extract DAPI endpoints (`https://:1443`) of healthy evonodes from the * quorum service payload. Healthy means PoSe status ENABLED and a successful @@ -35,57 +51,23 @@ export function parseEvonodeAddresses(payload: unknown): string[] { const { address, status, versionCheck } = node as Record; if (status !== 'ENABLED' || versionCheck !== 'success' || typeof address !== 'string') continue; // `address` is the Core P2P endpoint (host:19999); only the host is reused. - const host = address.replace(/:\d+$/, ''); - if (!HOST_PATTERN.test(host)) continue; - const endpoint = `https://${host}:${DAPI_HTTPS_PORT}`; - // The regex alone admits non-URLs such as 999.1.1.1, and one of those - // would make the DAPI client constructor throw for the whole list. - try { - new URL(endpoint); - } catch { - continue; - } - endpoints.add(endpoint); + const endpoint = toDapiEndpoint(address.replace(/:\d+$/, '')); + if (endpoint) endpoints.add(endpoint); } return [...endpoints]; } /** - * Testnet DAPI endpoints for this session. Fetches the live evonode list - * (one attempt, 5 s) because certificates rotate every few days and nodes come - * and go, which makes a static list go stale. Falls back to `staticAddresses` - * when the fetch fails and tops the live list up with them when it yields - * fewer than {@link MIN_DYNAMIC_EVONODES} nodes. + * Healthy testnet evonode endpoints from the quorum service (one attempt, + * 5 s). Resolves an empty list, after logging why, when the service fails. */ -export async function resolveTestnetDapiAddresses( - staticAddresses: readonly string[] -): Promise { - let live: string[] = []; +export async function fetchTestnetEvonodeAddresses(): Promise { try { - live = parseEvonodeAddresses( + return parseEvonodeAddresses( await fetchJson(TESTNET_MASTERNODES_URL, {}, EVONODE_FETCH_TIMEOUT_MS) ); } catch (error) { - console.warn('Could not fetch the testnet evonode list; using the built-in list:', error); - } - - if (live.length >= MIN_DYNAMIC_EVONODES) return live; - if (live.length > 0) { - console.warn(`Only ${live.length} live testnet evonodes found; adding the built-in list`); - } - return [...new Set([...live, ...staticAddresses])]; -} - -/** - * DAPI endpoints to dial for a network: testnet resolves the live evonode - * list (see {@link resolveTestnetDapiAddresses}); mainnet and devnets use - * their configured addresses unchanged and make no request. - */ -export async function resolveDapiAddresses( - config: Pick -): Promise { - if (config.type === 'testnet' && config.dapiAddresses) { - return resolveTestnetDapiAddresses(config.dapiAddresses); + console.warn('Could not fetch the testnet evonode list from the quorum service:', error); + return []; } - return config.dapiAddresses; } diff --git a/src/api/islock.test.ts b/src/api/islock.test.ts index 9405120..6a1a908 100644 --- a/src/api/islock.test.ts +++ b/src/api/islock.test.ts @@ -7,6 +7,7 @@ const mocks = vi.hoisted(() => ({ getTransactionLockStatus: vi.fn(), getCoreChainLockedHeight: vi.fn(), getPlatformStatus: vi.fn(), + warmEvonodeDiscovery: vi.fn(), disconnect: vi.fn(), })); @@ -27,6 +28,7 @@ vi.mock('./dapi-subscription.js', () => ({ getTransactionLockStatus: mocks.getTransactionLockStatus, getCoreChainLockedHeight: mocks.getCoreChainLockedHeight, getPlatformStatus: mocks.getPlatformStatus, + warmEvonodeDiscovery: mocks.warmEvonodeDiscovery, disconnect: mocks.disconnect, })), })); @@ -74,7 +76,8 @@ describe('IslockService', () => { }); expect(TESTNET.rpcUrl).toBeUndefined(); - expect(TESTNET.dapiAddresses?.length).toBeGreaterThan(0); + // No built-in list: the subscription discovers testnet evonodes itself. + expect(TESTNET.dapiAddresses).toBeUndefined(); expect(service.supportsJsonRpc).toBe(false); const handle = await service.subscribeForInstantSendLock('txid', new Uint8Array([4]), { txid: 'prevout', vout: 0 }); @@ -112,4 +115,14 @@ describe('IslockService', () => { expect(mocks.subscribeForInstantSendLock).toHaveBeenCalledOnce(); await expect(handle.wait()).resolves.toBe(bytes); }); + + it('warms evonode discovery on testnet but not on JSON-RPC networks', async () => { + mocks.warmEvonodeDiscovery.mockResolvedValue(undefined); + + await new IslockService({ network: 'testnet' }).warmEvonodeDiscovery(); + expect(mocks.warmEvonodeDiscovery).toHaveBeenCalledOnce(); + + await new IslockService({ network: 'mainnet' }).warmEvonodeDiscovery(); + expect(mocks.warmEvonodeDiscovery).toHaveBeenCalledOnce(); + }); }); diff --git a/src/api/islock.ts b/src/api/islock.ts index a040ca4..6bedb83 100644 --- a/src/api/islock.ts +++ b/src/api/islock.ts @@ -68,8 +68,9 @@ export class IslockService { /** * Open the IS lock source before broadcasting and return a handle whose * `.wait()` resolves with the IS lock bytes once available. Networks without - * RPC (testnet, devnets) use the DAPI bloom subscription over their explicit - * dapiAddresses, which must be established before broadcast because + * RPC (testnet, devnets) use the DAPI bloom subscription (testnet over the + * evonodes it discovers, devnets over their configured dapiAddresses), which + * must be established before broadcast because * subscriptions don't replay historical IS locks. Mainnet uses JSON-RPC * polling, which can recover by txid and avoids browser-hostile dapi-client * stream discovery. @@ -211,6 +212,15 @@ export class IslockService { return this.subscriptionClient.getTransactionLockStatus(txid); } + /** + * Start finding testnet evonodes for the IS lock subscription. Never + * rejects; a no-op on networks with configured addresses or JSON-RPC. + */ + warmEvonodeDiscovery(): Promise { + if (this.hasJsonRpc) return Promise.resolve(); + return this.subscriptionClient.warmEvonodeDiscovery(); + } + async disconnect(): Promise { await this.subscriptionClient.disconnect(); } diff --git a/src/config.ts b/src/config.ts index 3ac3655..6f482c7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -46,38 +46,10 @@ export const TESTNET: NetworkConfig = { faucetBaseUrl: 'https://faucet.thepasta.org', // No rpcUrl: trpc.digitalcash.dev is stuck on Core v23 and has no IS locks // past the V24 fork (block 1,578,000). IS locks come from the DAPI bloom - // stream instead. Explicit addresses skip dapi-client's seed/SML discovery, - // which can't parse V24 CbTx v4 coinbases. - // - // This list is only the FALLBACK: at client init the app fetches the live - // evonode list from the quorum service (see api/evonodes.ts) because nodes - // and their certificates rotate within days. Snapshot (2026-10-23) of - // ENABLED evonodes whose DAPI version check passed, each with an IP-valid - // TLS cert and a CORS preflight that reflects the request origin. To - // regenerate (candidates still need the cert/CORS check above): - // curl -s https://quorums.testnet.networks.dash.org/masternodes | jq -r '.data | map(select(.status=="ENABLED" and .versionCheck=="success") | .address | sub(":[0-9]+$"; "")) | .[]' - dapiAddresses: [ - 'https://68.67.122.1:1443', - 'https://68.67.122.2:1443', - 'https://68.67.122.3:1443', - 'https://68.67.122.4:1443', - 'https://68.67.122.5:1443', - 'https://68.67.122.6:1443', - 'https://68.67.122.7:1443', - 'https://68.67.122.8:1443', - 'https://68.67.122.9:1443', - 'https://68.67.122.10:1443', - 'https://68.67.122.11:1443', - 'https://68.67.122.12:1443', - 'https://85.209.243.1:1443', - 'https://85.209.243.2:1443', - 'https://85.209.243.3:1443', - 'https://85.209.243.4:1443', - 'https://85.209.243.5:1443', - 'https://85.209.243.6:1443', - 'https://85.209.243.7:1443', - 'https://206.245.167.63:1443', - ], + // stream instead, on evonodes found through dapi-client's masternode list + // discovery (falling back to the quorum service's list); see + // api/evonode-discovery.ts. No dapiAddresses: evonodes and their + // certificates rotate within days, so any built-in list goes stale. }; export const MAINNET: NetworkConfig = { diff --git a/src/main.ts b/src/main.ts index 4ace06c..862543c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,7 +14,6 @@ import { InsightClient, isAmbiguousBroadcastError } from './api/insight.js'; import type { IslockService } from './api/islock.js'; import { fetchNetworkStatus } from './api/network-status.js'; import { DAPIClient } from './api/dapi.js'; -import { resolveDapiAddresses } from './api/evonodes.js'; import { buildInstantAssetLockProof, buildChainAssetLockProof } from './proof/index.js'; import { bech32m } from '@scure/base'; import { privateKeyToWif, bytesToHex, abortableSleep } from './utils/index.js'; @@ -291,20 +290,17 @@ let warmupStarted = false; async function initClients(network: string): Promise { const config = getNetwork(network); insightClient = new InsightClient(config); - // Testnet evonodes (and their certificates) rotate within days, so use the - // live list; the static one in config.ts is the fallback. - const [{ IslockService }, dapiAddresses] = await Promise.all([ - loadIslockModule(), - resolveDapiAddresses(config), - ]); - // The evonode fetch can take seconds. If the user switched networks meanwhile, + const { IslockService } = await loadIslockModule(); + // The chunk load can take seconds. If the user switched networks meanwhile, // the newer init owns the clients; installing these would pair this network's // IS-lock service with the other network's Insight client. if (state.network !== network) return; + // Testnet has no dapiAddresses: the service discovers its evonodes when a + // funding flow starts (see warmFundingConnect). islockService = new IslockService({ network, rpcUrl: config.rpcUrl, - dapiAddresses, + dapiAddresses: config.dapiAddresses, }); startNetworkStatusPolling(); } @@ -367,12 +363,18 @@ function downloadFundingKeyBackup(backupState: BridgeState): void { /** * Testnet: connect the SDK (shared with any connect in flight) and poll the - * health indicator as soon as it settles. Other networks: nothing. + * health indicator as soon as it settles, and sync the masternode list the IS + * lock subscription picks its evonodes from. Other networks: nothing. + * Never rejects. */ function warmFundingConnect(network: string): Promise { - return warmPlatformSdkForFunding(network).then((attempted) => { + const sdk = warmPlatformSdkForFunding(network).then((attempted) => { if (attempted) pollNetworkStatusNow?.(); }); + const evonodes = state.network === network + ? islockService?.warmEvonodeDiscovery() + : undefined; + return Promise.all([sdk, evonodes]).then(() => {}); } /** How long a funding flow waits for the testnet SDK connect before showing its deposit address anyway. */ From a6f1bd895f07122df93aebe9456942e37747c768 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 12:17:13 -0600 Subject: [PATCH 2/4] fix(testnet): read the masternode list once per discovery instead of streaming it Review found that dapi-client's long-lived masternode list stream is unsafe in browsers: grpc-web reports a stream error through 'end', so the provider nulls its stream without settling the pending read, while ReconnectableStream keeps reconnecting (maxRetriesOnError -1) with no listener. A dead seed (seed-1.pshenmic.dev doesn't resolve) or an iOS download prompt would hang every read for the full deadline and leak a stream for the life of the page. - fetchEvonodesFromSeeds opens subscribeToMasternodeList (retries 0) on one seed from dapi-client's testnet config, adds the next seed on failure or after 3 s of silence, parses the first (full) list with dashcore-lib, and cancels every stream and client. 10 s deadline, abortable. - EvonodeDiscovery keeps the list for 10 minutes, then reads it again; concurrent readers share one read; disconnect() aborts it. - Endpoints come from SimplifiedMNListEntry.getPlatformHTTPSEndpoint(), so evonodes registered with extended addresses are dialled on their platform_https host. - The shared client (lock-status diagnostic) now covers the discovered nodes instead of dapi-client's whitelist-filtered seed provider. - dapi-client 5 returns null for missing status sections where 3.x threw; a devnet status without a chain section still counts as a failed read, not a healthy one. - Simplifier cleanups: docs, unused export, redundant guard, stale test options; the e2e idle check also covers seed-1.pshenmic.dev. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 5 +- e2e/deterministic.spec.ts | 2 +- src/api/dapi-subscription-multi.test.ts | 140 +++++--- src/api/dapi-subscription.ts | 39 ++- src/api/evonode-discovery-sml.test.ts | 33 ++ src/api/evonode-discovery.test.ts | 404 ++++++++++++++++-------- src/api/evonode-discovery.ts | 359 ++++++++++++++------- src/api/evonodes.ts | 2 +- src/api/islock.test.ts | 1 - src/api/islock.ts | 4 +- src/api/platform-status-routing.test.ts | 8 +- src/main.ts | 5 +- src/types/external.d.ts | 8 + vite.config.ts | 2 + 14 files changed, 691 insertions(+), 321 deletions(-) create mode 100644 src/api/evonode-discovery-sml.test.ts diff --git a/CLAUDE.md b/CLAUDE.md index bffdac3..6abc002 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -44,7 +44,8 @@ src/ │ ├── dapi.ts # JSON-RPC client (mainnet InstantSend locks, best chain lock) │ ├── dapi-subscription.ts # DAPI bloom-filter stream (testnet/devnet InstantSend locks) │ ├── islock.ts # IslockService: picks the InstantSend lock source per network -│ └── evonodes.ts # Live testnet evonode list (static list in config.ts is the fallback) +│ ├── evonode-discovery.ts # Testnet evonodes from the masternode list the seeds serve +│ └── evonodes.ts # Quorum-service evonode list (discovery fallback) ├── platform/ # Dash Platform operations │ ├── identity.ts # Identity registration and top-up │ └── login.ts # Sign in with Dash: key checks and signing @@ -87,7 +88,7 @@ Network is determined by URL parameter: `?network=mainnet` (defaults to testnet) InstantSend lock source and Platform status per network: - **mainnet**: JSON-RPC at `rpc.digitalcash.dev` (`rpcUrl` in `config.ts`). -- **testnet**: no JSON-RPC service (trpc.digitalcash.dev never upgraded to Core V24). The lock comes from the DAPI bloom-filter stream on evonodes, discovered from the quorum service at init with the static `dapiAddresses` as fallback. Platform status is read through the EvoSDK (`platform/status.ts`) because dapi-client's `platform.getStatus()` throws on testnet nodes. +- **testnet**: no JSON-RPC service (trpc.digitalcash.dev never upgraded to Core V24). The lock comes from the DAPI bloom-filter stream on 3 evonodes at once. They are discovered when a funding flow starts, from the masternode list served by `@dashevo/dapi-client`'s testnet seeds, with the quorum service list as fallback; the subscription rejects before broadcast if both fail. There is no built-in node list. Platform status is read through the EvoSDK (`platform/status.ts`) because dapi-client's `platform.getStatus()` throws on testnet nodes. - **devnets**: their configured `rpcUrl` if any, otherwise the DAPI stream over their `dapiAddresses`; Platform status through dapi-client. ## Dependencies diff --git a/e2e/deterministic.spec.ts b/e2e/deterministic.spec.ts index 026f179..2745642 100644 --- a/e2e/deterministic.spec.ts +++ b/e2e/deterministic.spec.ts @@ -39,7 +39,7 @@ test.describe('Deterministic UI E2E (mock mode)', () => { if (/evo-sdk|wasm[-_]sdk/i.test(url.pathname)) sdkRequests.push(request.url()); // Evonode discovery (masternode list from the seeds, quorum-service // fallback) starts with a funding flow, not on page load. - if (/^seed-\d+\.testnet\.networks\.dash\.org$/.test(url.hostname) || url.pathname === '/masternodes') { + if (/^seed-\d+\./.test(url.hostname) || url.pathname === '/masternodes') { discoveryRequests.push(request.url()); } }); diff --git a/src/api/dapi-subscription-multi.test.ts b/src/api/dapi-subscription-multi.test.ts index b956b42..3e96a76 100644 --- a/src/api/dapi-subscription-multi.test.ts +++ b/src/api/dapi-subscription-multi.test.ts @@ -25,31 +25,14 @@ vi.mock('@dashevo/dapi-client', () => { class FakeClient { options: any; streams: any[] = []; + listStreams: any[] = []; disconnect = vi.fn(async () => {}); core: any; platform = {}; - dapiAddressProvider: any; - constructor(options: any) { this.options = options; state.clients.push(this); - if (!options.dapiAddresses) { - const smlProvider: any = { - stream: undefined, - getSimplifiedMNList: vi.fn(async () => { - if (state.smlHangs) return new Promise(() => {}); - if (state.smlError) { - smlProvider.stream = null; - throw state.smlError; - } - smlProvider.stream = {}; - const entries = state.smlHosts.map((ip) => ({ nType: 1, platformHTTPPort: 1443, getIp: () => ip })); - return { getValidMasternodesList: () => entries }; - }), - }; - this.dapiAddressProvider = { smlProvider }; - } this.core = { getBestBlockHeight: vi.fn(async () => { const only = options.dapiAddresses?.length === 1 ? options.dapiAddresses[0].host : undefined; @@ -58,6 +41,23 @@ vi.mock('@dashevo/dapi-client', () => { if (only && state.failedHeightHosts.has(only)) throw new Error('GrpcError: height refused'); return state.height; }), + // Seed clients serve the masternode list; see state.sml*. + subscribeToMasternodeList: vi.fn(async () => { + const stream: any = new EventEmitter(); + stream.cancel = vi.fn(); + this.listStreams.push(stream); + setTimeout(() => { + if (state.smlHangs) return; + if (state.smlError) { + stream.emit('error', state.smlError); + stream.emit('end'); + return; + } + const bytes = new TextEncoder().encode(JSON.stringify({ hosts: state.smlHosts })); + stream.emit('data', { getMasternodeListDiff_asU8: () => bytes }); + }, 1); + return stream; + }), subscribeToTransactionsWithProofs: vi.fn(async (_filter: unknown, _opts: unknown) => { if (options.dapiAddresses?.length === 1 && state.deadHosts.has(options.dapiAddresses[0].host)) { throw new Error('GrpcError: Response closed without headers'); @@ -90,6 +90,16 @@ vi.mock('@dashevo/dashcore-lib', () => ({ }, { BLOOM_UPDATE_ALL: 1 } ), + // Test masternode lists: the stream message is { hosts }, see subscribeToMasternodeList. + SimplifiedMNListDiff: class { + constructor(public obj: { hosts: string[] }) {} + }, + SimplifiedMNList: class { + constructor(private readonly diff: { obj: { hosts: string[] } }) {} + getValidMasternodesList() { + return this.diff.obj.hosts.map((host) => ({ getPlatformHTTPSEndpoint: () => ({ host, port: 1443 }) })); + } + }, // Test "IS locks" are just the txid as UTF-8 bytes. InstantLock: { fromBuffer: (buf: Buffer) => ({ txid: buf.toString('utf8') }), @@ -98,6 +108,8 @@ vi.mock('@dashevo/dashcore-lib', () => ({ })); vi.mock('../utils/islock-debug.js', () => ({ describeIslock: () => ({}) })); +// Test masternode list messages carry JSON instead of CBOR. +vi.mock('cbor', () => ({ default: { decodeFirstSync: (bytes: Buffer) => JSON.parse(bytes.toString('utf8')) } })); const fallbackMock = vi.hoisted(() => vi.fn<[], Promise>()); vi.mock('./evonodes.js', async (importOriginal) => ({ @@ -122,13 +134,23 @@ const TIMEOUT = 60000; const addresses = (n: number): string[] => Array.from({ length: n }, (_, i) => `https://node${i}.test:1443`); +const isSeed = (c: any): boolean => c.options.dapiAddresses?.[0]?.host.startsWith('seed-'); + const lockMessage = (txid: string) => ({ getInstantSendLockMessages: () => ({ getMessagesList_asU8: () => [new TextEncoder().encode(txid)] }), }); const blockMessage = { getRawMerkleBlock: () => new Uint8Array([1]) }; /** The one-address clients, in creation order. */ -const nodeClients = (): any[] => state.clients.filter((c) => c.options.dapiAddresses?.length === 1); +const nodeClients = (): any[] => + state.clients.filter( + (c) => + c.options.dapiAddresses?.length === 1 && + !isSeed(c) && + // Not the discovery's shared client, which gets neither call here. + (c.core.getBestBlockHeight.mock.calls.length > 0 || + c.core.subscribeToTransactionsWithProofs.mock.calls.length > 0) + ); const liveStreams = (): any[] => nodeClients().flatMap((c) => c.streams); const flush = async (): Promise => { await vi.advanceTimersByTimeAsync(0); }; @@ -616,21 +638,32 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { describe('testnet evonode discovery', () => { const hosts = (n: number): string[] => Array.from({ length: n }, (_, i) => `10.0.0.${i + 1}`); - const discoveryClients = (): any[] => state.clients.filter((c) => !c.options.dapiAddresses); + const seedClients = (): any[] => state.clients.filter(isSeed); + + /** Start a subscription, let discovery read the list and the first stream deliver a message. */ + async function startDiscovered(client: DAPISubscriptionClient, signal?: AbortSignal) { + const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT, undefined, signal); + // Long enough for every seed to fail in turn (each answers after 1 ms). + await vi.advanceTimersByTimeAsync(20); + liveStreams()[0]?.emit('data', blockMessage); + return pending; + } - it('opens the streams on distinct evonodes from the masternode list, not on the shared client', async () => { + it('opens the streams on distinct evonodes from the masternode list, read once from one seed', async () => { state.smlHosts = hosts(6); const client = new DAPISubscriptionClient({ network: 'testnet' }); - expect(discoveryClients()).toHaveLength(1); - expect(discoveryClients()[0].options.network).toBe('testnet'); + // Nothing is fetched until a subscription (or warm-up) needs it. + expect(state.clients).toHaveLength(0); - const sub = await startReady(client); + const sub = await startDiscovered(client); + expect(seedClients()).toHaveLength(1); + expect(seedClients()[0].listStreams[0].cancel).toHaveBeenCalled(); + expect(seedClients()[0].disconnect).toHaveBeenCalled(); const streamHosts = nodeClients().map((c) => c.options.dapiAddresses[0].host); expect(streamHosts).toHaveLength(ISLOCK_CONCURRENT_NODES); expect(new Set(streamHosts).size).toBe(ISLOCK_CONCURRENT_NODES); for (const host of streamHosts) expect(state.smlHosts).toContain(host); - expect(discoveryClients()[0].core.subscribeToTransactionsWithProofs).not.toHaveBeenCalled(); expect(fallbackMock).not.toHaveBeenCalled(); liveStreams()[1].emit('data', lockMessage(TXID)); @@ -641,45 +674,47 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { state.smlHosts = hosts(1); const client = new DAPISubscriptionClient({ network: 'testnet' }); - const sub = await startReady(client); + const sub = await startDiscovered(client); - expect(nodeClients()).toHaveLength(1); + expect(liveStreams()).toHaveLength(1); liveStreams()[0].emit('data', lockMessage(TXID)); await expect(sub.wait()).resolves.toBeInstanceOf(Uint8Array); }); - it('uses the list as it is at each subscription', async () => { - state.smlHosts = ['10.0.0.1']; + it('replaces a failed stream with another discovered evonode', async () => { + state.smlHosts = hosts(5); const client = new DAPISubscriptionClient({ network: 'testnet' }); - const first = await startReady(client); - liveStreams()[0].emit('data', lockMessage(TXID)); - await first.wait(); + const sub = await startDiscovered(client); - state.clients.length = 0; - state.smlHosts = ['10.0.0.9']; - await startReady(client); + liveStreams()[0].emit('error', new Error('GrpcError: stream reset')); + await flush(); - expect(nodeClients().map((c) => c.options.dapiAddresses[0].host)).toEqual(['10.0.0.9']); + expect(nodeClients()).toHaveLength(ISLOCK_CONCURRENT_NODES + 1); + const replacement = nodeClients()[ISLOCK_CONCURRENT_NODES]; + replacement.streams[0].emit('data', lockMessage(TXID)); + await expect(sub.wait()).resolves.toBeInstanceOf(Uint8Array); }); - it('falls back to the quorum service list when the masternode list fails', async () => { - state.smlError = new Error('Failed to parse payload'); + it('falls back to the quorum service list when no seed serves the masternode list', async () => { + state.smlError = new Error('Response closed without headers'); fallbackMock.mockResolvedValue(['https://10.1.0.1:1443', 'https://10.1.0.2:1443', 'https://10.1.0.3:1443']); const client = new DAPISubscriptionClient({ network: 'testnet' }); - await startReady(client); + await startDiscovered(client); expect(fallbackMock).toHaveBeenCalledOnce(); expect(nodeClients().map((c) => c.options.dapiAddresses[0].host).sort()).toEqual(['10.1.0.1', '10.1.0.2', '10.1.0.3']); + for (const seed of seedClients()) expect(seed.disconnect).toHaveBeenCalled(); }); it('rejects before opening any stream when no evonode can be found', async () => { state.smlError = new Error('No available addresses'); const client = new DAPISubscriptionClient({ network: 'testnet' }); - await expect(client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT)).rejects.toThrow( - /No testnet evonodes found/ - ); + const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT); + const assertion = expect(pending).rejects.toThrow(/No testnet evonodes found/); + await vi.advanceTimersByTimeAsync(50); + await assertion; expect(nodeClients()).toHaveLength(0); }); @@ -689,12 +724,12 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT); const assertion = expect(pending).rejects.toThrow(`InstantSend lock subscription aborted for ${TXID}`); - await flush(); + await vi.advanceTimersByTimeAsync(5); await client.disconnect(); await assertion; expect(nodeClients()).toHaveLength(0); - expect(discoveryClients()[0].disconnect).toHaveBeenCalled(); + expect(seedClients()[0].listStreams[0].cancel).toHaveBeenCalled(); }); it('a caller abort during discovery rejects with the abort error', async () => { @@ -704,20 +739,25 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT, undefined, controller.signal); const assertion = expect(pending).rejects.toThrow(`InstantSend lock subscription aborted for ${TXID}`); - await flush(); + await vi.advanceTimersByTimeAsync(5); controller.abort(); await assertion; expect(nodeClients()).toHaveLength(0); }); - it('warmEvonodeDiscovery() syncs the list without opening streams', async () => { + it('warmEvonodeDiscovery() reads the list ahead, and the subscription reuses it', async () => { state.smlHosts = hosts(3); const client = new DAPISubscriptionClient({ network: 'testnet' }); - await client.warmEvonodeDiscovery(); - - expect(discoveryClients()[0].dapiAddressProvider.smlProvider.getSimplifiedMNList).toHaveBeenCalledOnce(); + const warm = client.warmEvonodeDiscovery(); + await vi.advanceTimersByTimeAsync(5); + await warm; + expect(seedClients()).toHaveLength(1); expect(nodeClients()).toHaveLength(0); + + await startDiscovered(client); + expect(seedClients()).toHaveLength(1); + expect(nodeClients()).toHaveLength(3); }); it('configured addresses (devnets) never start discovery', async () => { @@ -726,7 +766,7 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { await client.warmEvonodeDiscovery(); await startReady(client); - expect(discoveryClients()).toHaveLength(0); + expect(seedClients()).toHaveLength(0); expect(fallbackMock).not.toHaveBeenCalled(); }); }); diff --git a/src/api/dapi-subscription.ts b/src/api/dapi-subscription.ts index 0afd6a1..d90f528 100644 --- a/src/api/dapi-subscription.ts +++ b/src/api/dapi-subscription.ts @@ -1,11 +1,18 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import DAPIClientModule from '@dashevo/dapi-client'; +import dapiNetworkConfigs from '@dashevo/dapi-client/lib/networkConfigs.js'; import dashcoreLib from '@dashevo/dashcore-lib'; import { getNetwork } from '../config.js'; import { hash160 } from '../crypto/hash.js'; import { describeIslock } from '../utils/islock-debug.js'; -import { EvonodeDiscovery, parseDapiAddress, type DapiAddress } from './evonode-discovery.js'; +import { + EvonodeDiscovery, + parseDapiAddress, + seedAddresses, + shuffled, + type DapiAddress, +} from './evonode-discovery.js'; import { fetchPlatformStatus, fetchPlatformStatusIfSdkReady, @@ -53,15 +60,6 @@ interface NodeSlot { failed: boolean; } -function shuffled(items: readonly T[]): T[] { - const copy = [...items]; - for (let i = copy.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [copy[i], copy[j]] = [copy[j], copy[i]]; - } - return copy; -} - export class DAPISubscriptionClient { readonly network: string; // Exactly one of these is set: testnet discovers its evonodes, every other @@ -86,9 +84,12 @@ export class DAPISubscriptionClient { this.nodeAddresses = [...new Map(parsed.map((a) => [`${a.host}:${a.port}`, a])).values()]; this.fixedClient = new DAPIClientClass({ ...CLIENT_OPTIONS, dapiAddresses: parsed }); } else if (getNetwork(config.network).type === 'testnet') { - this.discovery = new EvonodeDiscovery( - () => new DAPIClientClass({ ...CLIENT_OPTIONS, network: 'testnet' }) - ); + // Seeds come from the published dapi-client, so they follow its releases. + this.discovery = new EvonodeDiscovery({ + network: 'testnet', + seeds: seedAddresses(dapiNetworkConfigs.testnet.seeds), + createClient: (dapiAddresses) => new DAPIClientClass({ ...CLIENT_OPTIONS, dapiAddresses }), + }); } else { this.fixedClient = new DAPIClientClass({ ...CLIENT_OPTIONS, @@ -97,8 +98,12 @@ export class DAPISubscriptionClient { } } - /** Client for calls that may go to any node. */ - private get dapiClient(): any { + /** + * Client for calls that may go to any node. On testnet it covers the + * evonodes found by the last discovery, so it is undefined before the + * first subscription. + */ + private get dapiClient(): any | undefined { return this.discovery ? this.discovery.sharedClient : this.fixedClient; } @@ -648,6 +653,10 @@ export class DAPISubscriptionClient { const status = await this.dapiClient.platform.getStatus(); const chain = status.getChainStatus?.() ?? status.chain; + // dapi-client 5 returns null for sections a node leaves out, where 3.x + // threw. A status without its chain section says nothing about Platform, + // so keep treating it as a failed read rather than a healthy one. + if (!chain) throw new Error('Platform status has no chain section'); const clhRaw = chain?.getCoreChainLockedHeight?.() ?? chain?.coreChainLockedHeight; const lbhRaw = chain?.getLatestBlockHeight?.() ?? chain?.latestBlockHeight; diff --git a/src/api/evonode-discovery-sml.test.ts b/src/api/evonode-discovery-sml.test.ts new file mode 100644 index 0000000..561f9d3 --- /dev/null +++ b/src/api/evonode-discovery-sml.test.ts @@ -0,0 +1,33 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import dashcoreLib from '@dashevo/dashcore-lib'; +import { describe, expect, it } from 'vitest'; + +import { evonodeEndpointsFromSml } from './evonode-discovery.js'; + +// Real dashcore-lib entries (only the fields getPlatformHTTPSEndpoint reads), +// so a change in how the library derives the Platform endpoint shows up here. +const { SimplifiedMNListEntry } = dashcoreLib as any; +const entry = (fields: Record) => + Object.assign(Object.create(SimplifiedMNListEntry.prototype), fields); + +describe('evonodeEndpointsFromSml with dashcore-lib entries', () => { + it('uses service ip + platformHTTPPort for older evonodes and platform_https for extended-address ones', () => { + const sml = { + getValidMasternodesList: () => [ + entry({ nType: 1, nVersion: 2, service: '68.67.122.1:19999', platformHTTPPort: 1443 }), + // Regular masternode: serves no DAPI. + entry({ nType: 0, nVersion: 2, service: '68.67.122.2:19999' }), + // Extended addresses: the DAPI host differs from the Core P2P host. + entry({ + nType: 1, + nVersion: 3, + service: '68.67.122.3:19999', + addresses: { core_p2p: ['68.67.122.3:19999'], platform_https: ['evo3.example.org:443'] }, + }), + entry({ nType: 1, nVersion: 2, service: '0.0.0.0:0', platformHTTPPort: 1443 }), + ], + }; + + expect(evonodeEndpointsFromSml(sml)).toEqual(['https://68.67.122.1:1443', 'https://evo3.example.org:443']); + }); +}); diff --git a/src/api/evonode-discovery.test.ts b/src/api/evonode-discovery.test.ts index 29e2a74..23ce2a2 100644 --- a/src/api/evonode-discovery.test.ts +++ b/src/api/evonode-discovery.test.ts @@ -1,186 +1,313 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { EventEmitter } from 'node:events'; +import cbor from 'cbor'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +// A "masternode list" here is CBOR of { hosts: [...] }; the fake parser turns +// each host into a valid evonode on port 1443 ('bad' hosts make it throw). +vi.mock('@dashevo/dashcore-lib', () => { + class SimplifiedMNListDiff { + hosts: string[]; + constructor(obj: { hosts: string[] }, public network: string) { + if (obj.hosts.includes('bad')) throw new Error('Failed to parse payload'); + this.hosts = obj.hosts; + } + } + class SimplifiedMNList { + constructor(private readonly diff: SimplifiedMNListDiff) {} + getValidMasternodesList() { + return this.diff.hosts.map((host) => ({ getPlatformHTTPSEndpoint: () => ({ host, port: 1443 }) })); + } + } + return { default: { SimplifiedMNList, SimplifiedMNListDiff } }; +}); + import { - DISCOVERY_TIMEOUT_MS, + EVONODE_LIST_MAX_AGE_MS, EvonodeDiscovery, + MASTERNODE_LIST_TIMEOUT_MS, + SEED_HEDGE_DELAY_MS, evonodeEndpointsFromSml, + fetchEvonodesFromSeeds, parseDapiAddress, + seedAddresses, + type DapiAddress, } from './evonode-discovery.js'; -const entry = (ip: string, overrides: Record = {}) => ({ - nType: 1, - platformHTTPPort: 1443, - getIp: () => ip, - ...overrides, -}); -const sml = (...entries: unknown[]) => ({ getValidMasternodesList: () => entries }); - -/** A stand-in for a dapi-client instance: just its masternode list provider and disconnect(). */ -function fakeClient(getList: () => Promise) { - const smlProvider: any = { - stream: undefined, - getSimplifiedMNList: vi.fn(async () => { - const list = await getList(); - smlProvider.stream = {}; - return list; - }), +const listMessage = (...hosts: string[]) => ({ getMasternodeListDiff_asU8: () => new Uint8Array(cbor.encode({ hosts })) }); + +type Behaviour = + | { kind: 'list'; hosts: string[] } + | { kind: 'reject' } + | { kind: 'error-end' } + | { kind: 'silent' }; + +/** Fake dapi-client clients whose masternode list stream follows `behaviour[host]`. */ +function fakeNetwork(behaviour: Record) { + const clients: any[] = []; + const streams: any[] = []; + const createClient = (addresses: DapiAddress[]) => { + const host = addresses[0].host; + const client: any = { + addresses, + disconnect: vi.fn(async () => {}), + core: { + subscribeToMasternodeList: vi.fn(async () => { + const b = behaviour[host] ?? { kind: 'silent' }; + if (b.kind === 'reject') throw new Error(`UNAVAILABLE: Name resolution failed for ${host}`); + const stream: any = new EventEmitter(); + stream.cancel = vi.fn(); + streams.push(stream); + setTimeout(() => { + if (b.kind === 'list') stream.emit('data', listMessage(...b.hosts)); + // Browser grpc-web: a failed stream reports through both channels. + if (b.kind === 'error-end') { + stream.emit('error', new Error('Response closed without headers')); + stream.emit('end'); + } + }, 100); + return stream; + }), + }, + }; + clients.push(client); + return client; }; - return { dapiAddressProvider: { smlProvider }, disconnect: vi.fn(async () => {}) }; + return { clients, streams, createClient }; } -describe('evonodeEndpointsFromSml', () => { - it('keeps valid evonodes, using their Platform HTTP port, without duplicates', () => { - const result = evonodeEndpointsFromSml( - sml( - entry('68.67.122.1'), - entry('68.67.122.2', { nType: 0 }), // regular masternode: no DAPI - entry('68.67.122.3', { platformHTTPPort: '2443' }), - entry('68.67.122.1'), - entry('0.0.0.0.0'), - entry('68.67.122.4', { platformHTTPPort: undefined }), - entry('68.67.122.5', { getIp: undefined }), - null - ) - ); - - expect(result).toEqual(['https://68.67.122.1:1443', 'https://68.67.122.3:2443']); - }); - - it.each([undefined, null, {}, { getValidMasternodesList: () => undefined }])( - 'returns nothing for list %j', - (bad) => { - expect(evonodeEndpointsFromSml(bad)).toEqual([]); - } - ); +const seeds = seedAddresses(['seed-1.test:1443', 'seed-2.test:1443', 'seed-3.test:1443']); + +beforeEach(() => { + vi.useFakeTimers(); + vi.spyOn(console, 'warn').mockImplementation(() => {}); + vi.spyOn(console, 'log').mockImplementation(() => {}); }); -describe('parseDapiAddress', () => { - it('splits a URL into the address object dapi-client takes', () => { - expect(parseDapiAddress('https://68.67.122.1:1443')).toEqual({ protocol: 'https', host: '68.67.122.1', port: 1443 }); +afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); +}); + +describe('address helpers', () => { + it('parses seeds and URLs into the address objects dapi-client takes', () => { + expect(seedAddresses(['seed-1.testnet.networks.dash.org:1443'])).toEqual([ + { protocol: 'https', host: 'seed-1.testnet.networks.dash.org', port: 1443 }, + ]); expect(parseDapiAddress('https://evo.example.org')).toEqual({ protocol: 'https', host: 'evo.example.org', port: 443 }); }); + + it('keeps valid evonode endpoints from a list, without duplicates or unusable hosts', () => { + const entry = (endpoint: unknown) => ({ getPlatformHTTPSEndpoint: () => endpoint }); + const sml = { + getValidMasternodesList: () => [ + entry({ host: '68.67.122.1', port: 1443 }), + entry(null), // regular masternode + entry({ host: '68.67.122.1', port: 1443 }), + entry({ host: '2001:db8::1', port: 1443 }), + entry({ host: 'evo.example.org', port: 2443 }), + {}, + null, + ], + }; + + expect(evonodeEndpointsFromSml(sml)).toEqual(['https://68.67.122.1:1443', 'https://evo.example.org:2443']); + expect(evonodeEndpointsFromSml(undefined)).toEqual([]); + }); +}); + +describe('fetchEvonodesFromSeeds', () => { + it('reads the list from the first seed and closes its stream and client', async () => { + const net = fakeNetwork({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1', '10.0.0.2'] } }); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + await vi.advanceTimersByTimeAsync(100); + + await expect(result).resolves.toEqual(['https://10.0.0.1:1443', 'https://10.0.0.2:1443']); + expect(net.clients).toHaveLength(1); + expect(net.streams[0].cancel).toHaveBeenCalled(); + expect(net.clients[0].disconnect).toHaveBeenCalled(); + expect(net.clients[0].core.subscribeToMasternodeList).toHaveBeenCalledWith({ retries: 0 }); + }); + + it.each(['reject', 'error-end'] as const)('moves to the next seed at once when a seed fails (%s)', async (kind) => { + const net = fakeNetwork({ + 'seed-1.test': { kind }, + 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, + }); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + await vi.advanceTimersByTimeAsync(250); + + await expect(result).resolves.toEqual(['https://10.0.0.5:1443']); + expect(net.clients.map((c) => c.addresses[0].host)).toEqual(['seed-1.test', 'seed-2.test']); + }); + + it('treats a list it cannot parse as a failed seed', async () => { + const net = fakeNetwork({ + 'seed-1.test': { kind: 'list', hosts: ['bad'] }, + 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, + }); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + await vi.advanceTimersByTimeAsync(250); + + await expect(result).resolves.toEqual(['https://10.0.0.5:1443']); + }); + + it(`asks the next seed too when one is silent for ${SEED_HEDGE_DELAY_MS} ms, and cancels the slow one`, async () => { + const net = fakeNetwork({ + 'seed-1.test': { kind: 'silent' }, + 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, + }); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + await vi.advanceTimersByTimeAsync(SEED_HEDGE_DELAY_MS - 1); + expect(net.clients).toHaveLength(1); + await vi.advanceTimersByTimeAsync(101); + + await expect(result).resolves.toEqual(['https://10.0.0.5:1443']); + expect(net.streams[0].cancel).toHaveBeenCalled(); + expect(net.clients[0].disconnect).toHaveBeenCalled(); + }); + + it('rejects once every seed failed', async () => { + const failing = fakeNetwork({ + 'seed-1.test': { kind: 'reject' }, + 'seed-2.test': { kind: 'error-end' }, + 'seed-3.test': { kind: 'reject' }, + }); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => failing.createClient([seed]), 'testnet'); + const assertion = expect(result).rejects.toThrow(/3 seeds tried/); + await vi.advanceTimersByTimeAsync(250); + await assertion; + expect(failing.clients).toHaveLength(3); + }); + + it(`rejects after ${MASTERNODE_LIST_TIMEOUT_MS} ms and closes every stream`, async () => { + const net = fakeNetwork({}); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + const assertion = expect(result).rejects.toThrow(/timed out/); + await vi.advanceTimersByTimeAsync(MASTERNODE_LIST_TIMEOUT_MS); + await assertion; + + expect(net.streams).toHaveLength(3); + for (const stream of net.streams) expect(stream.cancel).toHaveBeenCalled(); + for (const client of net.clients) expect(client.disconnect).toHaveBeenCalled(); + }); + + it('rejects at once on abort and closes the open stream', async () => { + const net = fakeNetwork({}); + const controller = new AbortController(); + + const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet', controller.signal); + await vi.advanceTimersByTimeAsync(10); + controller.abort(); + + await expect(result).rejects.toThrow(/aborted/); + expect(net.streams[0].cancel).toHaveBeenCalled(); + }); }); describe('EvonodeDiscovery', () => { const fallback = vi.fn<[], Promise>(); - let clients: ReturnType[]; - let lists: Array<() => Promise>; - - const createClient = () => { - const client = fakeClient(lists[Math.min(clients.length, lists.length - 1)]); - clients.push(client); - return client; + let now = 0; + const discover = (behaviour: Record) => { + const net = fakeNetwork(behaviour); + const discovery = new EvonodeDiscovery({ + network: 'testnet', + seeds: seeds.slice(0, 1), + createClient: net.createClient, + fetchFallback: fallback, + now: () => now, + }); + const seedClients = () => net.clients.filter((c) => c.addresses[0].host.startsWith('seed-')); + return { net, discovery, seedClients }; }; beforeEach(() => { - vi.useFakeTimers(); - vi.spyOn(console, 'warn').mockImplementation(() => {}); + now = 0; fallback.mockReset(); fallback.mockResolvedValue([]); - clients = []; - lists = [async () => sml(entry('10.0.0.1'), entry('10.0.0.2'))]; }); - afterEach(() => { - vi.useRealTimers(); - vi.restoreAllMocks(); - }); + async function settle(promise: Promise): Promise { + await vi.advanceTimersByTimeAsync(100); + return promise; + } - it('returns the evonodes from the masternode list and does not ask the quorum service', async () => { - const discovery = new EvonodeDiscovery(createClient, fallback); + it('returns the evonodes from the masternode list, with a shared client over them', async () => { + const { discovery, net } = discover({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1', '10.0.0.2'] } }); - await expect(discovery.getNodes()).resolves.toEqual([ + expect(discovery.sharedClient).toBeUndefined(); + await expect(settle(discovery.getNodes())).resolves.toEqual([ { protocol: 'https', host: '10.0.0.1', port: 1443 }, { protocol: 'https', host: '10.0.0.2', port: 1443 }, ]); expect(fallback).not.toHaveBeenCalled(); - expect(discovery.sharedClient).toBe(clients[0]); + expect(discovery.sharedClient.addresses.map((a: DapiAddress) => a.host)).toEqual(['10.0.0.1', '10.0.0.2']); + expect(net.streams[0].cancel).toHaveBeenCalled(); }); - it('reads the current list on every call, so updates from the list stream are seen', async () => { - let current = sml(entry('10.0.0.1')); - lists = [async () => current]; - const discovery = new EvonodeDiscovery(createClient, fallback); + it(`reuses the list for ${EVONODE_LIST_MAX_AGE_MS / 60000} minutes, then reads it again`, async () => { + const behaviour: Record = { 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1'] } }; + const { discovery, seedClients } = discover(behaviour); + await settle(discovery.getNodes()); + const firstShared = discovery.sharedClient; + now = EVONODE_LIST_MAX_AGE_MS - 1; + behaviour['seed-1.test'] = { kind: 'list', hosts: ['10.0.0.9'] }; expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.1']); - current = sml(entry('10.0.0.7'), entry('10.0.0.8')); - expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.7', '10.0.0.8']); - expect(clients).toHaveLength(1); + expect(seedClients()).toHaveLength(1); + + now = EVONODE_LIST_MAX_AGE_MS; + expect((await settle(discovery.getNodes())).map((a) => a.host)).toEqual(['10.0.0.9']); + expect(seedClients()).toHaveLength(2); + // The shared client follows the new list; the old one is closed. + expect(discovery.sharedClient).not.toBe(firstShared); + await vi.advanceTimersByTimeAsync(0); + expect(firstShared.disconnect).toHaveBeenCalled(); }); - it('shares one in-flight sync between concurrent callers', async () => { - let release!: () => void; - lists = [() => new Promise((resolve) => { release = () => resolve(sml(entry('10.0.0.1'))); })]; - const discovery = new EvonodeDiscovery(createClient, fallback); + it('shares one read between concurrent callers', async () => { + const { discovery, seedClients } = discover({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1'] } }); const a = discovery.getNodes(); const b = discovery.getNodes(); const warm = discovery.warm(); - release(); + await vi.advanceTimersByTimeAsync(100); await expect(a).resolves.toHaveLength(1); await expect(b).resolves.toHaveLength(1); await expect(warm).resolves.toBeUndefined(); - expect(clients[0].dapiAddressProvider.smlProvider.getSimplifiedMNList).toHaveBeenCalledTimes(1); + expect(seedClients()).toHaveLength(1); }); - it('falls back to the quorum service when the masternode list sync fails', async () => { - lists = [async () => { throw new Error('Failed to parse payload'); }]; + it('falls back to the quorum service when the masternode list fails, and tries the list again next time', async () => { + const behaviour: Record = { 'seed-1.test': { kind: 'error-end' } }; + const { discovery, seedClients } = discover(behaviour); fallback.mockResolvedValue(['https://10.0.0.9:1443']); - const discovery = new EvonodeDiscovery(createClient, fallback); - await expect(discovery.getNodes()).resolves.toEqual([{ protocol: 'https', host: '10.0.0.9', port: 1443 }]); + expect((await settle(discovery.getNodes())).map((a) => a.host)).toEqual(['10.0.0.9']); expect(fallback).toHaveBeenCalledOnce(); - }); - - it('falls back to the quorum service when the list has no valid evonodes', async () => { - lists = [async () => sml(entry('10.0.0.1', { nType: 0 }))]; - fallback.mockResolvedValue(['https://10.0.0.9:1443']); - const discovery = new EvonodeDiscovery(createClient, fallback); - await expect(discovery.getNodes()).resolves.toHaveLength(1); - }); - - it(`falls back after ${DISCOVERY_TIMEOUT_MS} ms and starts over on a new client next time`, async () => { - lists = [() => new Promise(() => {}), async () => sml(entry('10.0.0.5'))]; - fallback.mockResolvedValue(['https://10.0.0.9:1443']); - const discovery = new EvonodeDiscovery(createClient, fallback); - - const first = discovery.getNodes(); - await vi.advanceTimersByTimeAsync(DISCOVERY_TIMEOUT_MS); - expect((await first).map((a) => a.host)).toEqual(['10.0.0.9']); - - expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.5']); - expect(clients).toHaveLength(2); - expect(clients[0].disconnect).toHaveBeenCalled(); - expect(discovery.sharedClient).toBe(clients[1]); - }); - - it('replaces the client once dapi-client dropped its list stream, instead of serving the stale list', async () => { - lists = [async () => sml(entry('10.0.0.1')), async () => sml(entry('10.0.0.2'))]; - const discovery = new EvonodeDiscovery(createClient, fallback); - await discovery.getNodes(); - - // What dapi-client does when the masternode list stream errors or ends. - clients[0].dapiAddressProvider.smlProvider.stream = null; - - expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.2']); - expect(clients).toHaveLength(2); - await vi.advanceTimersByTimeAsync(0); - expect(clients[0].disconnect).toHaveBeenCalled(); + behaviour['seed-1.test'] = { kind: 'list', hosts: ['10.0.0.1'] }; + expect((await settle(discovery.getNodes())).map((a) => a.host)).toEqual(['10.0.0.1']); + expect(seedClients()).toHaveLength(2); }); it('rejects when neither source yields an evonode, so nothing is broadcast', async () => { - lists = [async () => { throw new Error('No available addresses'); }]; - const discovery = new EvonodeDiscovery(createClient, fallback); + const { discovery } = discover({ 'seed-1.test': { kind: 'reject' } }); - await expect(discovery.getNodes()).rejects.toThrow(/No testnet evonodes found/); + const assertion = expect(discovery.getNodes()).rejects.toThrow(/No testnet evonodes found/); + await vi.advanceTimersByTimeAsync(100); + await assertion; }); - it('rejects at once when the signal aborts during the sync', async () => { - lists = [() => new Promise(() => {})]; - const discovery = new EvonodeDiscovery(createClient, fallback); + it('rejects at once when the signal aborts', async () => { + const { discovery } = discover({}); const controller = new AbortController(); const pending = discovery.getNodes(controller.signal); @@ -190,17 +317,28 @@ describe('EvonodeDiscovery', () => { expect(fallback).not.toHaveBeenCalled(); }); - it('warm() settles without rejecting when the sync fails', async () => { - lists = [async () => { throw new Error('boom'); }]; - const discovery = new EvonodeDiscovery(createClient, fallback); + it('disconnect() stops an in-flight read and closes the shared client', async () => { + const behaviour: Record = { 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1'] } }; + const { discovery, net } = discover(behaviour); + await settle(discovery.getNodes()); + const shared = discovery.sharedClient; - await expect(discovery.warm()).resolves.toBeUndefined(); - expect(fallback).not.toHaveBeenCalled(); + now = EVONODE_LIST_MAX_AGE_MS; + behaviour['seed-1.test'] = { kind: 'silent' }; + const warm = discovery.warm(); + await vi.advanceTimersByTimeAsync(10); + await discovery.disconnect(); + await warm; + + expect(shared.disconnect).toHaveBeenCalled(); + expect(net.streams[1].cancel).toHaveBeenCalled(); + expect(discovery.sharedClient).toBeUndefined(); }); - it('disconnect() closes the client', async () => { - const discovery = new EvonodeDiscovery(createClient, fallback); - await discovery.disconnect(); - expect(clients[0].disconnect).toHaveBeenCalledOnce(); + it('warm() settles without rejecting when the read fails', async () => { + const { discovery } = discover({ 'seed-1.test': { kind: 'reject' } }); + + await expect(settle(discovery.warm())).resolves.toBeUndefined(); + expect(fallback).not.toHaveBeenCalled(); }); }); diff --git a/src/api/evonode-discovery.ts b/src/api/evonode-discovery.ts index f971c88..afb1019 100644 --- a/src/api/evonode-discovery.ts +++ b/src/api/evonode-discovery.ts @@ -1,5 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import dashcoreLib from '@dashevo/dashcore-lib'; +import cbor from 'cbor'; import { fetchTestnetEvonodeAddresses, toDapiEndpoint } from './evonodes.js'; /** A DAPI endpoint in the shape dapi-client's `dapiAddresses` option takes. */ @@ -9,11 +11,19 @@ export interface DapiAddress { port: number; } -/** Longest a masternode list sync may take before the quorum service is asked instead. */ -export const DISCOVERY_TIMEOUT_MS = 10000; +/** Longest one masternode list fetch may take, across all seeds tried. */ +export const MASTERNODE_LIST_TIMEOUT_MS = 10000; +/** A seed that hasn't answered by then gets company: the next seed is asked too. */ +export const SEED_HEDGE_DELAY_MS = 3000; +/** A list older than this is fetched again before use. */ +export const EVONODE_LIST_MAX_AGE_MS = 10 * 60_000; -/** SimplifiedMNListEntry.nType of evonodes (HP masternodes), the only ones serving DAPI. */ -const EVONODE_TYPE = 1; +const { SimplifiedMNList, SimplifiedMNListDiff } = dashcoreLib as any; + +/** dapi-client seed entries (`host:port`) as addresses; DAPI is served over HTTPS. */ +export function seedAddresses(seeds: readonly string[]): DapiAddress[] { + return seeds.map((seed) => parseDapiAddress(`https://${seed}`)); +} /** Parse a `https://host:port` URL into a dapi-client address. */ export function parseDapiAddress(url: string): DapiAddress { @@ -30,69 +40,188 @@ export function evonodeEndpointsFromSml(sml: any): string[] { const entries: any[] = sml?.getValidMasternodesList?.() ?? []; const endpoints = new Set(); for (const entry of entries) { - if (entry?.nType !== EVONODE_TYPE) continue; - const endpoint = toDapiEndpoint(entry.getIp?.(), Number(entry.platformHTTPPort)); + // null for regular masternodes; handles evonodes registered with + // extended addresses, whose DAPI host can differ from the Core P2P host. + const platform = entry?.getPlatformHTTPSEndpoint?.(); + const endpoint = platform && toDapiEndpoint(platform.host, platform.port); if (endpoint) endpoints.add(endpoint); } return [...endpoints]; } -class DeadlineError extends Error {} +/** Evonode endpoints in a masternode list stream message (a full diff from the genesis block). */ +export function evonodeEndpointsFromListMessage(response: any, network: string): string[] { + const bytes = Buffer.from(response.getMasternodeListDiff_asU8()); + const diff = new SimplifiedMNListDiff(cbor.decodeFirstSync(bytes), network); + return evonodeEndpointsFromSml(new SimplifiedMNList(diff)); +} + +/** + * Read the simplified masternode list once from the network seeds and return + * its evonode endpoints. Opens `subscribeToMasternodeList` on one seed (its + * first message is the full list), adds the next seed when a seed fails or + * hasn't answered within {@link SEED_HEDGE_DELAY_MS}, takes the first list + * and cancels every stream. Rejects when all seeds fail, at + * {@link MASTERNODE_LIST_TIMEOUT_MS}, or when `signal` aborts. + * + * One-shot on purpose: dapi-client's own long-lived list stream never + * resyncs after an error, and in browsers it then keeps reconnecting with + * nobody listening. + */ +export function fetchEvonodesFromSeeds( + seeds: readonly DapiAddress[], + createClient: (seed: DapiAddress) => any, + network: string, + signal?: AbortSignal +): Promise { + return new Promise((resolve, reject) => { + const open = new Set<{ client: any; stream?: any }>(); + let next = 0; + let failures = 0; + let settled = false; + let hedgeTimer: ReturnType | undefined; + + const finish = (fn: () => void): void => { + if (settled) return; + settled = true; + clearTimeout(deadlineTimer); + clearTimeout(hedgeTimer); + signal?.removeEventListener('abort', onAbort); + for (const attempt of open) { + try { attempt.stream?.cancel(); } catch { /* ignore */ } + Promise.resolve().then(() => attempt.client.disconnect()).catch(() => {}); + } + open.clear(); + fn(); + }; + const onAbort = (): void => finish(() => reject(new Error('Masternode list fetch aborted'))); + const deadlineTimer = setTimeout( + () => finish(() => reject(new Error(`Masternode list fetch timed out after ${MASTERNODE_LIST_TIMEOUT_MS}ms`))), + MASTERNODE_LIST_TIMEOUT_MS + ); + + const tryNextSeed = (): void => { + clearTimeout(hedgeTimer); + const seed = seeds[next++]; + if (!seed) return; + hedgeTimer = setTimeout(tryNextSeed, SEED_HEDGE_DELAY_MS); + const attempt: { client: any; stream?: any } = { client: undefined }; + let failed = false; + const fail = (error: unknown): void => { + if (settled || failed) return; + failed = true; + open.delete(attempt); + try { attempt.stream?.cancel(); } catch { /* ignore */ } + Promise.resolve().then(() => attempt.client?.disconnect()).catch(() => {}); + console.warn(`[evonode-discovery] seed ${seed.host}:${seed.port} failed:`, error); + failures += 1; + if (failures >= seeds.length) { + const reason = error instanceof Error ? error.message : String(error); + finish(() => reject(new Error(`Masternode list unavailable: ${failures} seeds tried, last error: ${reason}`))); + } else { + tryNextSeed(); + } + }; + + try { + attempt.client = createClient(seed); + open.add(attempt); + Promise.resolve(attempt.client.core.subscribeToMasternodeList({ retries: 0 })).then((stream: any) => { + if (settled || failed) { + try { stream.cancel(); } catch { /* ignore */ } + return; + } + attempt.stream = stream; + stream.on('data', (response: unknown) => { + if (settled || failed) return; + let endpoints: string[]; + try { + endpoints = evonodeEndpointsFromListMessage(response, network); + } catch (error) { + fail(error); + return; + } + console.log(`[evonode-discovery] ${endpoints.length} evonodes from seed ${seed.host}:${seed.port}`); + finish(() => resolve(endpoints)); + }); + // In browsers a failed stream reports through 'end' (with a status) + // as well as 'error'; `failed` makes the second report a no-op. + stream.on('error', (error: unknown) => fail(error)); + stream.on('end', () => fail(new Error('Masternode list stream ended before sending the list'))); + }, fail); + } catch (error) { + fail(error); + } + }; -function rejectAfter(ms: number, message: string): { promise: Promise; clear: () => void } { - let timer: ReturnType | undefined; - const promise = new Promise((_, reject) => { - timer = setTimeout(() => reject(new DeadlineError(message)), ms); + if (signal?.aborted) { + onAbort(); + return; + } + signal?.addEventListener('abort', onAbort, { once: true }); + if (seeds.length === 0) { + finish(() => reject(new Error('No masternode list seeds configured'))); + return; + } + tryNextSeed(); }); - return { promise, clear: () => clearTimeout(timer) }; } -function rejectOnAbort(signal: AbortSignal | undefined, message: string): { promise: Promise; clear: () => void } { - let onAbort: (() => void) | undefined; - const promise = new Promise((_, reject) => { - if (!signal) return; - onAbort = () => reject(new Error(message)); - if (signal.aborted) onAbort(); - else signal.addEventListener('abort', onAbort, { once: true }); - }); - return { promise, clear: () => { if (onAbort) signal?.removeEventListener('abort', onAbort); } }; +export interface EvonodeDiscoveryOptions { + network: string; + seeds: readonly DapiAddress[]; + /** A dapi-client pinned to the given addresses. */ + createClient: (addresses: DapiAddress[]) => any; + /** Quorum-service list, used when the masternode list can't be read. */ + fetchFallback?: () => Promise; + now?: () => number; } /** - * Finds the testnet evonodes to open IS lock streams on, using dapi-client's - * own masternode-list discovery: the client syncs the simplified masternode - * list from the network seeds and keeps it current over a stream, so every - * call sees the list as of the latest block. + * Finds the testnet evonodes to open IS lock streams on, from the simplified + * masternode list served by the network seeds (dapi-client's discovery + * source), read with {@link fetchEvonodesFromSeeds}. The list is kept for + * {@link EVONODE_LIST_MAX_AGE_MS} and fetched again after that, so a long + * session never works from a stale list. * - * The valid evonodes are read from the list directly instead of through the - * client's address provider, which on testnet also filters them by a snapshot - * of addresses bundled with the library. Bad nodes in the list cost little: - * the subscription races several and replaces failed ones. + * Every valid evonode in the list is used, not dapi-client's testnet address + * provider result, which also filters by a snapshot of addresses bundled with + * the library. Bad nodes cost little: the subscription races several and + * replaces failed ones. * - * When the masternode list can't be synced in time, the quorum service list - * is used instead. When both fail, getNodes() rejects, so the caller stops - * before broadcasting. + * When the masternode list can't be read, the quorum service list is used + * instead. When both fail, getNodes() rejects, so the caller stops before + * broadcasting. */ export class EvonodeDiscovery { - private client: any; - // The current client's in-flight list read, shared by concurrent callers so - // they don't open a second masternode list stream. - private pendingRead?: Promise; - // Set when a read missed its deadline: the next read starts over on a new - // client instead of waiting on a sync that may never finish. - private replaceClient = false; - - constructor( - private readonly createClient: () => any, - private readonly fetchFallback: () => Promise = fetchTestnetEvonodeAddresses, - private readonly timeoutMs: number = DISCOVERY_TIMEOUT_MS - ) { - this.client = createClient(); + private readonly network: string; + private readonly seeds: readonly DapiAddress[]; + private readonly createClient: (addresses: DapiAddress[]) => any; + private readonly fetchFallback: () => Promise; + private readonly now: () => number; + private list?: { endpoints: string[]; fetchedAt: number }; + // In-flight masternode list read, shared by concurrent callers (warm() and + // getNodes()). Each read has its own deadline and closes its own streams. + private pendingFetch?: Promise; + // Aborts the in-flight read on disconnect(). + private fetchController?: AbortController; + private shared?: { key: string; client: any }; + + constructor(options: EvonodeDiscoveryOptions) { + this.network = options.network; + this.seeds = options.seeds; + this.createClient = options.createClient; + this.fetchFallback = options.fetchFallback ?? fetchTestnetEvonodeAddresses; + this.now = options.now ?? Date.now; } - /** The discovery client, for calls that may go to any node (e.g. getTransaction). */ - get sharedClient(): any { - return this.client; + /** + * A client over the most recently discovered evonodes, for calls that may + * go to any of them (e.g. getTransaction); undefined before the first + * discovery. + */ + get sharedClient(): any | undefined { + return this.shared?.client; } /** @@ -100,78 +229,96 @@ export class EvonodeDiscovery { * nor the quorum service yields one, or when `signal` aborts. */ async getNodes(signal?: AbortSignal): Promise { - const abortMessage = 'Evonode discovery aborted'; - const abort = rejectOnAbort(signal, abortMessage); - try { - let endpoints = await Promise.race([this.readListWithDeadline(), abort.promise]); - if (endpoints.length === 0) { - endpoints = await Promise.race([this.fetchFallback(), abort.promise]); - if (endpoints.length > 0) { - console.warn(`[evonode-discovery] using ${endpoints.length} evonodes from the quorum service`); - } + let endpoints = await this.whenNotAborted(this.readList(), signal); + if (endpoints.length === 0) { + endpoints = await this.whenNotAborted(this.fetchFallback(), signal); + if (endpoints.length > 0) { + console.warn(`[evonode-discovery] using ${endpoints.length} evonodes from the quorum service`); } - if (endpoints.length === 0) { - throw new Error('No testnet evonodes found: the masternode list and the quorum service both failed'); - } - return endpoints.map(parseDapiAddress); - } finally { - abort.clear(); } + if (endpoints.length === 0) { + throw new Error('No testnet evonodes found: the masternode list and the quorum service both failed'); + } + const nodes = endpoints.map(parseDapiAddress); + this.updateSharedClient(endpoints, nodes); + return nodes; } /** - * Start syncing the masternode list ahead of the first getNodes(). Settles - * (never rejects) once the list is read, the sync fails, or the deadline - * passes; the sync keeps going after the deadline. + * Read the masternode list ahead of the first getNodes(). Settles (never + * rejects) once the list is read or the read fails. */ async warm(): Promise { - await this.readListWithDeadline(); + await this.readList(); } async disconnect(): Promise { - this.pendingRead = undefined; - try { await this.client.disconnect(); } catch { /* ignore */ } - } - - /** Endpoints from the masternode list, or [] when it fails or misses the deadline. */ - private async readListWithDeadline(): Promise { - const deadline = rejectAfter(this.timeoutMs, `masternode list sync timed out after ${this.timeoutMs}ms`); - try { - const endpoints = await Promise.race([this.readList(), deadline.promise]); - if (endpoints.length === 0) console.warn('[evonode-discovery] masternode list has no valid evonodes'); - return endpoints; - } catch (error) { - if (error instanceof DeadlineError) this.replaceClient = true; - console.warn('[evonode-discovery] masternode list unavailable:', error); - return []; - } finally { - deadline.clear(); + this.fetchController?.abort(); + const shared = this.shared; + this.shared = undefined; + if (shared) { + try { await shared.client.disconnect(); } catch { /* ignore */ } } } + /** Endpoints from a fresh enough masternode list, or [] when it can't be read. */ private readList(): Promise { - // dapi-client sets the list stream to null once it errors or ends, and - // from then on returns the last list without ever resyncing. Replace the - // client then (or after a missed deadline), so a dropped or stuck stream - // (a network change, an iOS download prompt) can't leave a stale list. - if (this.replaceClient || this.client.dapiAddressProvider?.smlProvider?.stream === null) { - this.replaceClient = false; - const stale = this.client; - this.client = this.createClient(); - this.pendingRead = undefined; - Promise.resolve().then(() => stale.disconnect()).catch(() => {}); + if (this.list && this.now() - this.list.fetchedAt < EVONODE_LIST_MAX_AGE_MS) { + return Promise.resolve(this.list.endpoints); } - const provider = this.client.dapiAddressProvider?.smlProvider; - if (!provider) return Promise.reject(new Error('dapi-client has no masternode list provider')); - - if (!this.pendingRead) { - const read = Promise.resolve(provider.getSimplifiedMNList()).then(evonodeEndpointsFromSml); - const clear = (): void => { - if (this.pendingRead === read) this.pendingRead = undefined; - }; - read.then(clear, clear); - this.pendingRead = read; + if (!this.pendingFetch) { + const controller = new AbortController(); + this.fetchController = controller; + const fetch = fetchEvonodesFromSeeds( + shuffled(this.seeds), + (seed) => this.createClient([seed]), + this.network, + controller.signal + ).then( + (endpoints) => { + if (endpoints.length > 0) this.list = { endpoints, fetchedAt: this.now() }; + else console.warn('[evonode-discovery] masternode list has no valid evonodes'); + return endpoints; + }, + (error) => { + console.warn('[evonode-discovery] masternode list unavailable:', error); + return [] as string[]; + } + ).finally(() => { + if (this.pendingFetch === fetch) this.pendingFetch = undefined; + }); + this.pendingFetch = fetch; } - return this.pendingRead; + return this.pendingFetch; + } + + private updateSharedClient(endpoints: string[], nodes: DapiAddress[]): void { + const key = endpoints.join(','); + if (this.shared?.key === key) return; + const previous = this.shared; + this.shared = { key, client: this.createClient(nodes) }; + if (previous) Promise.resolve().then(() => previous.client.disconnect()).catch(() => {}); + } + + private whenNotAborted(promise: Promise, signal?: AbortSignal): Promise { + if (!signal) return promise; + return new Promise((resolve, reject) => { + const onAbort = (): void => reject(new Error('Evonode discovery aborted')); + if (signal.aborted) { + onAbort(); + return; + } + signal.addEventListener('abort', onAbort, { once: true }); + promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort)); + }); + } +} + +export function shuffled(items: readonly T[]): T[] { + const copy = [...items]; + for (let i = copy.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [copy[i], copy[j]] = [copy[j], copy[i]]; } + return copy; } diff --git a/src/api/evonodes.ts b/src/api/evonodes.ts index 20f7ebe..93eed29 100644 --- a/src/api/evonodes.ts +++ b/src/api/evonodes.ts @@ -12,7 +12,7 @@ export const TESTNET_MASTERNODES_URL = 'https://quorums.testnet.networks.dash.or const EVONODE_FETCH_TIMEOUT_MS = 5000; /** Port of the evonode DAPI endpoint (gRPC-web over HTTPS). */ -export const DAPI_HTTPS_PORT = 1443; +const DAPI_HTTPS_PORT = 1443; // Plain IPv4 / DNS hostnames only: the result is turned into a URL and handed // to dapi-client, so anything exotic (IPv6 literals, userinfo, paths) is dropped. diff --git a/src/api/islock.test.ts b/src/api/islock.test.ts index 6a1a908..28eb2a7 100644 --- a/src/api/islock.test.ts +++ b/src/api/islock.test.ts @@ -72,7 +72,6 @@ describe('IslockService', () => { const service = new IslockService({ network: 'testnet', rpcUrl: TESTNET.rpcUrl, - dapiAddresses: TESTNET.dapiAddresses, }); expect(TESTNET.rpcUrl).toBeUndefined(); diff --git a/src/api/islock.ts b/src/api/islock.ts index 6bedb83..4c12975 100644 --- a/src/api/islock.ts +++ b/src/api/islock.ts @@ -70,8 +70,8 @@ export class IslockService { * `.wait()` resolves with the IS lock bytes once available. Networks without * RPC (testnet, devnets) use the DAPI bloom subscription (testnet over the * evonodes it discovers, devnets over their configured dapiAddresses), which - * must be established before broadcast because - * subscriptions don't replay historical IS locks. Mainnet uses JSON-RPC + * must be established before broadcast because subscriptions don't replay + * historical IS locks. Mainnet uses JSON-RPC * polling, which can recover by txid and avoids browser-hostile dapi-client * stream discovery. */ diff --git a/src/api/platform-status-routing.test.ts b/src/api/platform-status-routing.test.ts index 5eb26ee..593178d 100644 --- a/src/api/platform-status-routing.test.ts +++ b/src/api/platform-status-routing.test.ts @@ -26,7 +26,6 @@ vi.mock('@dashevo/dashcore-lib', () => ({ default: {} })); import { DAPISubscriptionClient } from './dapi-subscription.js'; import { IslockService } from './islock.js'; import { fetchNetworkStatus } from './network-status.js'; -import { TESTNET } from '../config.js'; import type { InsightClient } from './insight.js'; const insightAt = (height: number): InsightClient => @@ -60,10 +59,7 @@ describe('Platform status routing', () => { new TypeError("Cannot read properties of undefined (reading 'getTotalSyncedTime')") ); mocks.fetchPlatformStatus.mockResolvedValue(freshStatus()); - const client = new DAPISubscriptionClient({ - network: 'testnet', - dapiAddresses: TESTNET.dapiAddresses, - }); + const client = new DAPISubscriptionClient({ network: 'testnet' }); await expect(client.getPlatformStatus()).resolves.toMatchObject({ coreChainLockedHeight: 1_578_100, @@ -119,7 +115,7 @@ describe('Platform status routing', () => { describe('network status on testnet', () => { const testnetIslock = () => - new IslockService({ network: 'testnet', dapiAddresses: TESTNET.dapiAddresses }); + new IslockService({ network: 'testnet' }); it('uses the DAPI/SDK path, not JSON-RPC', () => { expect(testnetIslock().supportsJsonRpc).toBe(false); diff --git a/src/main.ts b/src/main.ts index 862543c..2982313 100644 --- a/src/main.ts +++ b/src/main.ts @@ -371,10 +371,7 @@ function warmFundingConnect(network: string): Promise { const sdk = warmPlatformSdkForFunding(network).then((attempted) => { if (attempted) pollNetworkStatusNow?.(); }); - const evonodes = state.network === network - ? islockService?.warmEvonodeDiscovery() - : undefined; - return Promise.all([sdk, evonodes]).then(() => {}); + return Promise.all([sdk, islockService?.warmEvonodeDiscovery()]).then(() => {}); } /** How long a funding flow waits for the testnet SDK connect before showing its deposit address anyway. */ diff --git a/src/types/external.d.ts b/src/types/external.d.ts index 386fdb0..e2a21be 100644 --- a/src/types/external.d.ts +++ b/src/types/external.d.ts @@ -47,6 +47,14 @@ declare module '@dashevo/dapi-client' { export = DAPIClient; } +declare module '@dashevo/dapi-client/lib/networkConfigs.js' { + const networkConfigs: { + testnet: { seeds: string[] }; + mainnet: { seeds: string[] }; + }; + export default networkConfigs; +} + declare module '@dashevo/dashcore-lib' { interface BloomFilterInstance { vData: number[]; diff --git a/vite.config.ts b/vite.config.ts index 1ac9495..058151a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -43,7 +43,9 @@ export default defineConfig({ }, include: [ '@dashevo/dapi-client', + '@dashevo/dapi-client/lib/networkConfigs.js', '@dashevo/dashcore-lib', + 'cbor', ], }, }); From 5fff91d1217495df9f707caf1e55596f90dc962e Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 12:57:14 -0600 Subject: [PATCH 3/4] fix(testnet): prefer health-checked evonodes and keep the last list on failure Second review round on the discovery rewrite. - The masternode list says a node is registered and not PoSe-banned, not that its DAPI answers browsers. The quorum service's health-checked list is now read alongside it: when at least 3 nodes are in both, only those are used (the old primary source was health-filtered too). The quorum list is still the fallback when no seed serves the list. - Every result is kept for 10 minutes, including the fallback, so a dead seed set doesn't cost each subscription the full deadline. A refresh that finds nothing keeps the previous list instead of failing. - A throw in the stream-opened callback now fails the seed instead of escaping as an unhandled rejection; a read aborted by disconnect() can no longer clear a newer pending read. - Simplifier cleanups: one release() path for streams and clients, one pending-read field, createClient shared with the class, no unused exports or types. - Tests: late stream after settle, no hedge after success, pre-aborted signal, empty seeds, end without data, health narrowing, stale-list reuse; "opens nothing" checks now count every non-seed client. Co-Authored-By: Claude Opus 5.5 --- src/api/dapi-subscription-multi.test.ts | 13 +- src/api/dapi-subscription.ts | 2 +- src/api/evonode-discovery.test.ts | 136 ++++++++---- src/api/evonode-discovery.ts | 261 +++++++++++++----------- src/types/external.d.ts | 1 - 5 files changed, 248 insertions(+), 165 deletions(-) diff --git a/src/api/dapi-subscription-multi.test.ts b/src/api/dapi-subscription-multi.test.ts index 3e96a76..d0ef87a 100644 --- a/src/api/dapi-subscription-multi.test.ts +++ b/src/api/dapi-subscription-multi.test.ts @@ -639,6 +639,8 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { describe('testnet evonode discovery', () => { const hosts = (n: number): string[] => Array.from({ length: n }, (_, i) => `10.0.0.${i + 1}`); const seedClients = (): any[] => state.clients.filter(isSeed); + /** Every client that isn't a seed client: node clients and the shared client. */ + const nonSeedClients = (): any[] => state.clients.filter((c) => !isSeed(c)); /** Start a subscription, let discovery read the list and the first stream deliver a message. */ async function startDiscovered(client: DAPISubscriptionClient, signal?: AbortSignal) { @@ -664,7 +666,8 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { expect(streamHosts).toHaveLength(ISLOCK_CONCURRENT_NODES); expect(new Set(streamHosts).size).toBe(ISLOCK_CONCURRENT_NODES); for (const host of streamHosts) expect(state.smlHosts).toContain(host); - expect(fallbackMock).not.toHaveBeenCalled(); + // The quorum service is asked alongside, as a health hint (it vouches for nothing here). + expect(fallbackMock).toHaveBeenCalledOnce(); liveStreams()[1].emit('data', lockMessage(TXID)); await expect(sub.wait()).resolves.toBeInstanceOf(Uint8Array); @@ -715,7 +718,7 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { const assertion = expect(pending).rejects.toThrow(/No testnet evonodes found/); await vi.advanceTimersByTimeAsync(50); await assertion; - expect(nodeClients()).toHaveLength(0); + expect(nonSeedClients()).toHaveLength(0); }); it('disconnect() during discovery rejects with the abort error and opens nothing', async () => { @@ -728,7 +731,7 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { await client.disconnect(); await assertion; - expect(nodeClients()).toHaveLength(0); + expect(nonSeedClients()).toHaveLength(0); expect(seedClients()[0].listStreams[0].cancel).toHaveBeenCalled(); }); @@ -742,7 +745,7 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { await vi.advanceTimersByTimeAsync(5); controller.abort(); await assertion; - expect(nodeClients()).toHaveLength(0); + expect(nonSeedClients()).toHaveLength(0); }); it('warmEvonodeDiscovery() reads the list ahead, and the subscription reuses it', async () => { @@ -753,7 +756,7 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { await vi.advanceTimersByTimeAsync(5); await warm; expect(seedClients()).toHaveLength(1); - expect(nodeClients()).toHaveLength(0); + expect(nonSeedClients()).toHaveLength(0); await startDiscovered(client); expect(seedClients()).toHaveLength(1); diff --git a/src/api/dapi-subscription.ts b/src/api/dapi-subscription.ts index d90f528..3087724 100644 --- a/src/api/dapi-subscription.ts +++ b/src/api/dapi-subscription.ts @@ -103,7 +103,7 @@ export class DAPISubscriptionClient { * evonodes found by the last discovery, so it is undefined before the * first subscription. */ - private get dapiClient(): any | undefined { + private get dapiClient(): any { return this.discovery ? this.discovery.sharedClient : this.fixedClient; } diff --git a/src/api/evonode-discovery.test.ts b/src/api/evonode-discovery.test.ts index 23ce2a2..c9323e2 100644 --- a/src/api/evonode-discovery.test.ts +++ b/src/api/evonode-discovery.test.ts @@ -26,6 +26,7 @@ import { EVONODE_LIST_MAX_AGE_MS, EvonodeDiscovery, MASTERNODE_LIST_TIMEOUT_MS, + MIN_HEALTHY_EVONODES, SEED_HEDGE_DELAY_MS, evonodeEndpointsFromSml, fetchEvonodesFromSeeds, @@ -35,12 +36,15 @@ import { } from './evonode-discovery.js'; const listMessage = (...hosts: string[]) => ({ getMasternodeListDiff_asU8: () => new Uint8Array(cbor.encode({ hosts })) }); +const endpoint = (host: string) => `https://${host}:1443`; type Behaviour = | { kind: 'list'; hosts: string[] } | { kind: 'reject' } | { kind: 'error-end' } - | { kind: 'silent' }; + | { kind: 'end' } + | { kind: 'silent' } + | { kind: 'slow-open'; afterMs: number }; /** Fake dapi-client clients whose masternode list stream follows `behaviour[host]`. */ function fakeNetwork(behaviour: Record) { @@ -55,6 +59,7 @@ function fakeNetwork(behaviour: Record) { subscribeToMasternodeList: vi.fn(async () => { const b = behaviour[host] ?? { kind: 'silent' }; if (b.kind === 'reject') throw new Error(`UNAVAILABLE: Name resolution failed for ${host}`); + if (b.kind === 'slow-open') await new Promise((r) => setTimeout(r, b.afterMs)); const stream: any = new EventEmitter(); stream.cancel = vi.fn(); streams.push(stream); @@ -65,6 +70,7 @@ function fakeNetwork(behaviour: Record) { stream.emit('error', new Error('Response closed without headers')); stream.emit('end'); } + if (b.kind === 'end') stream.emit('end'); }, 100); return stream; }), @@ -98,7 +104,7 @@ describe('address helpers', () => { }); it('keeps valid evonode endpoints from a list, without duplicates or unusable hosts', () => { - const entry = (endpoint: unknown) => ({ getPlatformHTTPSEndpoint: () => endpoint }); + const entry = (platform: unknown) => ({ getPlatformHTTPSEndpoint: () => platform }); const sml = { getValidMasternodesList: () => [ entry({ host: '68.67.122.1', port: 1443 }), @@ -106,41 +112,41 @@ describe('address helpers', () => { entry({ host: '68.67.122.1', port: 1443 }), entry({ host: '2001:db8::1', port: 1443 }), entry({ host: 'evo.example.org', port: 2443 }), - {}, - null, ], }; expect(evonodeEndpointsFromSml(sml)).toEqual(['https://68.67.122.1:1443', 'https://evo.example.org:2443']); - expect(evonodeEndpointsFromSml(undefined)).toEqual([]); }); }); describe('fetchEvonodesFromSeeds', () => { - it('reads the list from the first seed and closes its stream and client', async () => { + it('reads the list from the first seed, closes its stream and client, and asks no other seed', async () => { const net = fakeNetwork({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1', '10.0.0.2'] } }); - const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); await vi.advanceTimersByTimeAsync(100); - await expect(result).resolves.toEqual(['https://10.0.0.1:1443', 'https://10.0.0.2:1443']); - expect(net.clients).toHaveLength(1); + await expect(result).resolves.toEqual([endpoint('10.0.0.1'), endpoint('10.0.0.2')]); expect(net.streams[0].cancel).toHaveBeenCalled(); expect(net.clients[0].disconnect).toHaveBeenCalled(); expect(net.clients[0].core.subscribeToMasternodeList).toHaveBeenCalledWith({ retries: 0 }); + // The hedge timer is cleared with the result. + await vi.advanceTimersByTimeAsync(SEED_HEDGE_DELAY_MS * 2); + expect(net.clients).toHaveLength(1); }); - it.each(['reject', 'error-end'] as const)('moves to the next seed at once when a seed fails (%s)', async (kind) => { + it.each(['reject', 'error-end', 'end'] as const)('moves to the next seed at once when a seed fails (%s)', async (kind) => { const net = fakeNetwork({ 'seed-1.test': { kind }, 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, }); - const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); await vi.advanceTimersByTimeAsync(250); - await expect(result).resolves.toEqual(['https://10.0.0.5:1443']); + await expect(result).resolves.toEqual([endpoint('10.0.0.5')]); expect(net.clients.map((c) => c.addresses[0].host)).toEqual(['seed-1.test', 'seed-2.test']); + expect(net.clients[0].disconnect).toHaveBeenCalled(); }); it('treats a list it cannot parse as a failed seed', async () => { @@ -149,10 +155,10 @@ describe('fetchEvonodesFromSeeds', () => { 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, }); - const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); await vi.advanceTimersByTimeAsync(250); - await expect(result).resolves.toEqual(['https://10.0.0.5:1443']); + await expect(result).resolves.toEqual([endpoint('10.0.0.5')]); }); it(`asks the next seed too when one is silent for ${SEED_HEDGE_DELAY_MS} ms, and cancels the slow one`, async () => { @@ -161,34 +167,50 @@ describe('fetchEvonodesFromSeeds', () => { 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, }); - const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); await vi.advanceTimersByTimeAsync(SEED_HEDGE_DELAY_MS - 1); expect(net.clients).toHaveLength(1); await vi.advanceTimersByTimeAsync(101); - await expect(result).resolves.toEqual(['https://10.0.0.5:1443']); + await expect(result).resolves.toEqual([endpoint('10.0.0.5')]); expect(net.streams[0].cancel).toHaveBeenCalled(); expect(net.clients[0].disconnect).toHaveBeenCalled(); }); + it('cancels a stream that opens only after another seed delivered the list', async () => { + const net = fakeNetwork({ + 'seed-1.test': { kind: 'slow-open', afterMs: SEED_HEDGE_DELAY_MS + 1000 }, + 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, + }); + + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); + await vi.advanceTimersByTimeAsync(SEED_HEDGE_DELAY_MS + 100); + await expect(result).resolves.toEqual([endpoint('10.0.0.5')]); + expect(net.streams).toHaveLength(1); + + await vi.advanceTimersByTimeAsync(1000); + expect(net.streams).toHaveLength(2); + expect(net.streams[1].cancel).toHaveBeenCalled(); + }); + it('rejects once every seed failed', async () => { - const failing = fakeNetwork({ + const net = fakeNetwork({ 'seed-1.test': { kind: 'reject' }, 'seed-2.test': { kind: 'error-end' }, 'seed-3.test': { kind: 'reject' }, }); - const result = fetchEvonodesFromSeeds(seeds, (seed) => failing.createClient([seed]), 'testnet'); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); const assertion = expect(result).rejects.toThrow(/3 seeds tried/); await vi.advanceTimersByTimeAsync(250); await assertion; - expect(failing.clients).toHaveLength(3); + expect(net.clients).toHaveLength(3); }); it(`rejects after ${MASTERNODE_LIST_TIMEOUT_MS} ms and closes every stream`, async () => { const net = fakeNetwork({}); - const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet'); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); const assertion = expect(result).rejects.toThrow(/timed out/); await vi.advanceTimersByTimeAsync(MASTERNODE_LIST_TIMEOUT_MS); await assertion; @@ -202,17 +224,27 @@ describe('fetchEvonodesFromSeeds', () => { const net = fakeNetwork({}); const controller = new AbortController(); - const result = fetchEvonodesFromSeeds(seeds, (seed) => net.createClient([seed]), 'testnet', controller.signal); + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet', controller.signal); await vi.advanceTimersByTimeAsync(10); controller.abort(); await expect(result).rejects.toThrow(/aborted/); expect(net.streams[0].cancel).toHaveBeenCalled(); }); + + it('rejects without contacting a seed when the signal is already aborted or there are no seeds', async () => { + const net = fakeNetwork({}); + const controller = new AbortController(); + controller.abort(); + + await expect(fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet', controller.signal)).rejects.toThrow(/aborted/); + await expect(fetchEvonodesFromSeeds([], net.createClient, 'testnet')).rejects.toThrow(/No masternode list seeds/); + expect(net.clients).toHaveLength(0); + }); }); describe('EvonodeDiscovery', () => { - const fallback = vi.fn<[], Promise>(); + const healthy = vi.fn<[], Promise>(); let now = 0; const discover = (behaviour: Record) => { const net = fakeNetwork(behaviour); @@ -220,17 +252,18 @@ describe('EvonodeDiscovery', () => { network: 'testnet', seeds: seeds.slice(0, 1), createClient: net.createClient, - fetchFallback: fallback, + fetchHealthy: healthy, now: () => now, }); const seedClients = () => net.clients.filter((c) => c.addresses[0].host.startsWith('seed-')); return { net, discovery, seedClients }; }; + const hostsOf = (nodes: DapiAddress[]) => nodes.map((a) => a.host); beforeEach(() => { now = 0; - fallback.mockReset(); - fallback.mockResolvedValue([]); + healthy.mockReset(); + healthy.mockResolvedValue([]); }); async function settle(promise: Promise): Promise { @@ -246,11 +279,26 @@ describe('EvonodeDiscovery', () => { { protocol: 'https', host: '10.0.0.1', port: 1443 }, { protocol: 'https', host: '10.0.0.2', port: 1443 }, ]); - expect(fallback).not.toHaveBeenCalled(); - expect(discovery.sharedClient.addresses.map((a: DapiAddress) => a.host)).toEqual(['10.0.0.1', '10.0.0.2']); + expect(healthy).toHaveBeenCalledOnce(); + expect(hostsOf(discovery.sharedClient.addresses)).toEqual(['10.0.0.1', '10.0.0.2']); expect(net.streams[0].cancel).toHaveBeenCalled(); }); + it(`narrows to the nodes the quorum service also vouches for when at least ${MIN_HEALTHY_EVONODES} are`, async () => { + const { discovery } = discover({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1', '10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5'] } }); + // 10.0.0.9 is not in the masternode list, so it is never used. + healthy.mockResolvedValue(['10.0.0.2', '10.0.0.3', '10.0.0.5', '10.0.0.9'].map(endpoint)); + + expect(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.2', '10.0.0.3', '10.0.0.5']); + }); + + it(`keeps the whole list when fewer than ${MIN_HEALTHY_EVONODES} nodes are vouched for`, async () => { + const { discovery } = discover({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1', '10.0.0.2', '10.0.0.3'] } }); + healthy.mockResolvedValue(['10.0.0.2', '10.0.0.3'].map(endpoint)); + + expect(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.1', '10.0.0.2', '10.0.0.3']); + }); + it(`reuses the list for ${EVONODE_LIST_MAX_AGE_MS / 60000} minutes, then reads it again`, async () => { const behaviour: Record = { 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1'] } }; const { discovery, seedClients } = discover(behaviour); @@ -259,11 +307,11 @@ describe('EvonodeDiscovery', () => { const firstShared = discovery.sharedClient; now = EVONODE_LIST_MAX_AGE_MS - 1; behaviour['seed-1.test'] = { kind: 'list', hosts: ['10.0.0.9'] }; - expect((await discovery.getNodes()).map((a) => a.host)).toEqual(['10.0.0.1']); + expect(hostsOf(await discovery.getNodes())).toEqual(['10.0.0.1']); expect(seedClients()).toHaveLength(1); now = EVONODE_LIST_MAX_AGE_MS; - expect((await settle(discovery.getNodes())).map((a) => a.host)).toEqual(['10.0.0.9']); + expect(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.9']); expect(seedClients()).toHaveLength(2); // The shared client follows the new list; the old one is closed. expect(discovery.sharedClient).not.toBe(firstShared); @@ -271,6 +319,16 @@ describe('EvonodeDiscovery', () => { expect(firstShared.disconnect).toHaveBeenCalled(); }); + it('keeps the previous list when a later read finds nothing', async () => { + const behaviour: Record = { 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1'] } }; + const { discovery } = discover(behaviour); + await settle(discovery.getNodes()); + + now = EVONODE_LIST_MAX_AGE_MS; + behaviour['seed-1.test'] = { kind: 'reject' }; + expect(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.1']); + }); + it('shares one read between concurrent callers', async () => { const { discovery, seedClients } = discover({ 'seed-1.test': { kind: 'list', hosts: ['10.0.0.1'] } }); @@ -283,22 +341,25 @@ describe('EvonodeDiscovery', () => { await expect(b).resolves.toHaveLength(1); await expect(warm).resolves.toBeUndefined(); expect(seedClients()).toHaveLength(1); + expect(healthy).toHaveBeenCalledOnce(); }); - it('falls back to the quorum service when the masternode list fails, and tries the list again next time', async () => { + it('uses the quorum service list when the masternode list fails, and keeps it for the max age', async () => { const behaviour: Record = { 'seed-1.test': { kind: 'error-end' } }; const { discovery, seedClients } = discover(behaviour); - fallback.mockResolvedValue(['https://10.0.0.9:1443']); + healthy.mockResolvedValue([endpoint('10.0.0.9')]); - expect((await settle(discovery.getNodes())).map((a) => a.host)).toEqual(['10.0.0.9']); - expect(fallback).toHaveBeenCalledOnce(); + expect(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.9']); + // Kept: a dead seed set doesn't cost every subscription the full deadline. + expect(hostsOf(await discovery.getNodes())).toEqual(['10.0.0.9']); + expect(seedClients()).toHaveLength(1); + now = EVONODE_LIST_MAX_AGE_MS; behaviour['seed-1.test'] = { kind: 'list', hosts: ['10.0.0.1'] }; - expect((await settle(discovery.getNodes())).map((a) => a.host)).toEqual(['10.0.0.1']); - expect(seedClients()).toHaveLength(2); + expect(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.1']); }); - it('rejects when neither source yields an evonode, so nothing is broadcast', async () => { + it('rejects when neither source ever yields an evonode, so nothing is broadcast', async () => { const { discovery } = discover({ 'seed-1.test': { kind: 'reject' } }); const assertion = expect(discovery.getNodes()).rejects.toThrow(/No testnet evonodes found/); @@ -314,7 +375,6 @@ describe('EvonodeDiscovery', () => { controller.abort(); await expect(pending).rejects.toThrow(/aborted/); - expect(fallback).not.toHaveBeenCalled(); }); it('disconnect() stops an in-flight read and closes the shared client', async () => { @@ -329,6 +389,7 @@ describe('EvonodeDiscovery', () => { await vi.advanceTimersByTimeAsync(10); await discovery.disconnect(); await warm; + await vi.advanceTimersByTimeAsync(0); expect(shared.disconnect).toHaveBeenCalled(); expect(net.streams[1].cancel).toHaveBeenCalled(); @@ -339,6 +400,5 @@ describe('EvonodeDiscovery', () => { const { discovery } = discover({ 'seed-1.test': { kind: 'reject' } }); await expect(settle(discovery.warm())).resolves.toBeUndefined(); - expect(fallback).not.toHaveBeenCalled(); }); }); diff --git a/src/api/evonode-discovery.ts b/src/api/evonode-discovery.ts index afb1019..789e05b 100644 --- a/src/api/evonode-discovery.ts +++ b/src/api/evonode-discovery.ts @@ -11,12 +11,21 @@ export interface DapiAddress { port: number; } +/** A dapi-client pinned to the given addresses. */ +export type CreateDapiClient = (addresses: DapiAddress[]) => any; + /** Longest one masternode list fetch may take, across all seeds tried. */ export const MASTERNODE_LIST_TIMEOUT_MS = 10000; /** A seed that hasn't answered by then gets company: the next seed is asked too. */ export const SEED_HEDGE_DELAY_MS = 3000; /** A list older than this is fetched again before use. */ export const EVONODE_LIST_MAX_AGE_MS = 10 * 60_000; +/** + * Fewest evonodes that both the masternode list and the quorum service's + * health check vouch for before discovery narrows to them (one per IS lock + * stream). + */ +export const MIN_HEALTHY_EVONODES = 3; const { SimplifiedMNList, SimplifiedMNListDiff } = dashcoreLib as any; @@ -37,23 +46,23 @@ export function parseDapiAddress(url: string): DapiAddress { /** Distinct DAPI endpoints of the valid evonodes in a simplified masternode list. */ export function evonodeEndpointsFromSml(sml: any): string[] { - const entries: any[] = sml?.getValidMasternodesList?.() ?? []; const endpoints = new Set(); - for (const entry of entries) { + for (const entry of sml.getValidMasternodesList()) { // null for regular masternodes; handles evonodes registered with // extended addresses, whose DAPI host can differ from the Core P2P host. - const platform = entry?.getPlatformHTTPSEndpoint?.(); + const platform = entry.getPlatformHTTPSEndpoint(); const endpoint = platform && toDapiEndpoint(platform.host, platform.port); if (endpoint) endpoints.add(endpoint); } return [...endpoints]; } -/** Evonode endpoints in a masternode list stream message (a full diff from the genesis block). */ -export function evonodeEndpointsFromListMessage(response: any, network: string): string[] { - const bytes = Buffer.from(response.getMasternodeListDiff_asU8()); - const diff = new SimplifiedMNListDiff(cbor.decodeFirstSync(bytes), network); - return evonodeEndpointsFromSml(new SimplifiedMNList(diff)); +function disconnectQuietly(client: any): void { + Promise.resolve().then(() => client?.disconnect()).catch(() => {}); +} + +function cancelQuietly(stream: any): void { + try { stream?.cancel(); } catch { /* ignore */ } } /** @@ -70,27 +79,29 @@ export function evonodeEndpointsFromListMessage(response: any, network: string): */ export function fetchEvonodesFromSeeds( seeds: readonly DapiAddress[], - createClient: (seed: DapiAddress) => any, + createClient: CreateDapiClient, network: string, signal?: AbortSignal ): Promise { return new Promise((resolve, reject) => { - const open = new Set<{ client: any; stream?: any }>(); + // Attempts still in play; leaving this set is what retires an attempt. + const open = new Set<{ client?: any; stream?: any }>(); let next = 0; let failures = 0; let settled = false; let hedgeTimer: ReturnType | undefined; + const release = (attempt: { client?: any; stream?: any }): void => { + cancelQuietly(attempt.stream); + disconnectQuietly(attempt.client); + }; const finish = (fn: () => void): void => { if (settled) return; settled = true; clearTimeout(deadlineTimer); clearTimeout(hedgeTimer); signal?.removeEventListener('abort', onAbort); - for (const attempt of open) { - try { attempt.stream?.cancel(); } catch { /* ignore */ } - Promise.resolve().then(() => attempt.client.disconnect()).catch(() => {}); - } + for (const attempt of open) release(attempt); open.clear(); fn(); }; @@ -105,14 +116,12 @@ export function fetchEvonodesFromSeeds( const seed = seeds[next++]; if (!seed) return; hedgeTimer = setTimeout(tryNextSeed, SEED_HEDGE_DELAY_MS); - const attempt: { client: any; stream?: any } = { client: undefined }; - let failed = false; + const attempt: { client?: any; stream?: any } = {}; + open.add(attempt); + const fail = (error: unknown): void => { - if (settled || failed) return; - failed = true; - open.delete(attempt); - try { attempt.stream?.cancel(); } catch { /* ignore */ } - Promise.resolve().then(() => attempt.client?.disconnect()).catch(() => {}); + if (!open.delete(attempt)) return; + release(attempt); console.warn(`[evonode-discovery] seed ${seed.host}:${seed.port} failed:`, error); failures += 1; if (failures >= seeds.length) { @@ -124,31 +133,35 @@ export function fetchEvonodesFromSeeds( }; try { - attempt.client = createClient(seed); - open.add(attempt); - Promise.resolve(attempt.client.core.subscribeToMasternodeList({ retries: 0 })).then((stream: any) => { - if (settled || failed) { - try { stream.cancel(); } catch { /* ignore */ } - return; - } - attempt.stream = stream; - stream.on('data', (response: unknown) => { - if (settled || failed) return; - let endpoints: string[]; - try { - endpoints = evonodeEndpointsFromListMessage(response, network); - } catch (error) { - fail(error); + attempt.client = createClient([seed]); + Promise.resolve(attempt.client.core.subscribeToMasternodeList({ retries: 0 })) + .then((stream: any) => { + if (!open.has(attempt)) { + // Settled or failed while the stream was being opened. + cancelQuietly(stream); return; } - console.log(`[evonode-discovery] ${endpoints.length} evonodes from seed ${seed.host}:${seed.port}`); - finish(() => resolve(endpoints)); - }); - // In browsers a failed stream reports through 'end' (with a status) - // as well as 'error'; `failed` makes the second report a no-op. - stream.on('error', (error: unknown) => fail(error)); - stream.on('end', () => fail(new Error('Masternode list stream ended before sending the list'))); - }, fail); + attempt.stream = stream; + stream.on('data', (response: any) => { + if (!open.has(attempt)) return; + let endpoints: string[]; + try { + const bytes = Buffer.from(response.getMasternodeListDiff_asU8()); + const diff = new SimplifiedMNListDiff(cbor.decodeFirstSync(bytes), network); + endpoints = evonodeEndpointsFromSml(new SimplifiedMNList(diff)); + } catch (error) { + fail(error); + return; + } + console.log(`[evonode-discovery] ${endpoints.length} evonodes from seed ${seed.host}:${seed.port}`); + finish(() => resolve(endpoints)); + }); + // In browsers a failed stream reports through 'end' (with a status) + // as well as 'error'; only the first report counts. + stream.on('error', (error: unknown) => fail(error)); + stream.on('end', () => fail(new Error('Masternode list stream ended before sending the list'))); + }) + .catch(fail); } catch (error) { fail(error); } @@ -170,48 +183,46 @@ export function fetchEvonodesFromSeeds( export interface EvonodeDiscoveryOptions { network: string; seeds: readonly DapiAddress[]; - /** A dapi-client pinned to the given addresses. */ - createClient: (addresses: DapiAddress[]) => any; - /** Quorum-service list, used when the masternode list can't be read. */ - fetchFallback?: () => Promise; + createClient: CreateDapiClient; + /** Quorum-service list of evonodes that pass its health check. */ + fetchHealthy?: () => Promise; now?: () => number; } /** - * Finds the testnet evonodes to open IS lock streams on, from the simplified - * masternode list served by the network seeds (dapi-client's discovery - * source), read with {@link fetchEvonodesFromSeeds}. The list is kept for - * {@link EVONODE_LIST_MAX_AGE_MS} and fetched again after that, so a long - * session never works from a stale list. + * Finds the testnet evonodes to open IS lock streams on. The source is the + * simplified masternode list served by the network seeds (dapi-client's + * discovery source), read with {@link fetchEvonodesFromSeeds}. The quorum + * service's list of nodes passing its DAPI health check is read alongside: + * when at least {@link MIN_HEALTHY_EVONODES} nodes are in both, only those + * are used; when the masternode list can't be read, the quorum service list + * is used on its own. * - * Every valid evonode in the list is used, not dapi-client's testnet address - * provider result, which also filters by a snapshot of addresses bundled with - * the library. Bad nodes cost little: the subscription races several and - * replaces failed ones. + * Every valid evonode in the list is considered, not dapi-client's testnet + * address provider result, which also filters by a snapshot of addresses + * bundled with the library. * - * When the masternode list can't be read, the quorum service list is used - * instead. When both fail, getNodes() rejects, so the caller stops before - * broadcasting. + * The result is kept for {@link EVONODE_LIST_MAX_AGE_MS}, then read again. + * If that read finds nothing, the previous list is used rather than none. + * getNodes() rejects only when no list was ever found, so the caller stops + * before broadcasting. */ export class EvonodeDiscovery { private readonly network: string; private readonly seeds: readonly DapiAddress[]; - private readonly createClient: (addresses: DapiAddress[]) => any; - private readonly fetchFallback: () => Promise; + private readonly createClient: CreateDapiClient; + private readonly fetchHealthy: () => Promise; private readonly now: () => number; private list?: { endpoints: string[]; fetchedAt: number }; - // In-flight masternode list read, shared by concurrent callers (warm() and - // getNodes()). Each read has its own deadline and closes its own streams. - private pendingFetch?: Promise; - // Aborts the in-flight read on disconnect(). - private fetchController?: AbortController; + // In-flight read, shared by concurrent callers (warm() and getNodes()). + private pending?: { promise: Promise; controller: AbortController }; private shared?: { key: string; client: any }; constructor(options: EvonodeDiscoveryOptions) { this.network = options.network; this.seeds = options.seeds; this.createClient = options.createClient; - this.fetchFallback = options.fetchFallback ?? fetchTestnetEvonodeAddresses; + this.fetchHealthy = options.fetchHealthy ?? fetchTestnetEvonodeAddresses; this.now = options.now ?? Date.now; } @@ -220,22 +231,16 @@ export class EvonodeDiscovery { * go to any of them (e.g. getTransaction); undefined before the first * discovery. */ - get sharedClient(): any | undefined { + get sharedClient(): any { return this.shared?.client; } /** - * The current evonode endpoints. Rejects when neither the masternode list - * nor the quorum service yields one, or when `signal` aborts. + * The current evonode endpoints. Rejects when none can be found, or when + * `signal` aborts. */ async getNodes(signal?: AbortSignal): Promise { - let endpoints = await this.whenNotAborted(this.readList(), signal); - if (endpoints.length === 0) { - endpoints = await this.whenNotAborted(this.fetchFallback(), signal); - if (endpoints.length > 0) { - console.warn(`[evonode-discovery] using ${endpoints.length} evonodes from the quorum service`); - } - } + const endpoints = await whenNotAborted(this.readList(), signal); if (endpoints.length === 0) { throw new Error('No testnet evonodes found: the masternode list and the quorum service both failed'); } @@ -245,51 +250,67 @@ export class EvonodeDiscovery { } /** - * Read the masternode list ahead of the first getNodes(). Settles (never - * rejects) once the list is read or the read fails. + * Find the evonodes ahead of the first getNodes(). Settles (never rejects) + * once the read finishes either way. */ async warm(): Promise { await this.readList(); } async disconnect(): Promise { - this.fetchController?.abort(); + this.pending?.controller.abort(); const shared = this.shared; this.shared = undefined; - if (shared) { - try { await shared.client.disconnect(); } catch { /* ignore */ } - } + if (shared) disconnectQuietly(shared.client); } - /** Endpoints from a fresh enough masternode list, or [] when it can't be read. */ + /** Endpoints of a fresh enough list, read again when stale; [] when none was ever found. */ private readList(): Promise { if (this.list && this.now() - this.list.fetchedAt < EVONODE_LIST_MAX_AGE_MS) { return Promise.resolve(this.list.endpoints); } - if (!this.pendingFetch) { + if (!this.pending) { const controller = new AbortController(); - this.fetchController = controller; - const fetch = fetchEvonodesFromSeeds( - shuffled(this.seeds), - (seed) => this.createClient([seed]), - this.network, - controller.signal - ).then( - (endpoints) => { - if (endpoints.length > 0) this.list = { endpoints, fetchedAt: this.now() }; - else console.warn('[evonode-discovery] masternode list has no valid evonodes'); - return endpoints; - }, - (error) => { - console.warn('[evonode-discovery] masternode list unavailable:', error); - return [] as string[]; - } - ).finally(() => { - if (this.pendingFetch === fetch) this.pendingFetch = undefined; - }); - this.pendingFetch = fetch; + const pending = { + controller, + promise: this.discover(controller.signal).finally(() => { + // A read aborted by disconnect() may finish after a newer one started. + if (this.pending === pending) this.pending = undefined; + }), + }; + this.pending = pending; } - return this.pendingFetch; + return this.pending.promise; + } + + private async discover(signal: AbortSignal): Promise { + const [fromList, healthy] = await Promise.all([ + fetchEvonodesFromSeeds(shuffled(this.seeds), this.createClient, this.network, signal).catch((error) => { + console.warn('[evonode-discovery] masternode list unavailable:', error); + return [] as string[]; + }), + this.fetchHealthy().catch(() => [] as string[]), + ]); + + let endpoints = fromList; + if (fromList.length > 0) { + const healthySet = new Set(healthy); + const vouched = fromList.filter((endpoint) => healthySet.has(endpoint)); + if (vouched.length >= MIN_HEALTHY_EVONODES) endpoints = vouched; + } else if (healthy.length > 0) { + console.warn(`[evonode-discovery] using ${healthy.length} evonodes from the quorum service`); + endpoints = healthy; + } + + if (endpoints.length > 0) { + this.list = { endpoints, fetchedAt: this.now() }; + return endpoints; + } + if (this.list) { + console.warn('[evonode-discovery] no evonodes found; keeping the previous list'); + return this.list.endpoints; + } + return []; } private updateSharedClient(endpoints: string[], nodes: DapiAddress[]): void { @@ -297,21 +318,21 @@ export class EvonodeDiscovery { if (this.shared?.key === key) return; const previous = this.shared; this.shared = { key, client: this.createClient(nodes) }; - if (previous) Promise.resolve().then(() => previous.client.disconnect()).catch(() => {}); + if (previous) disconnectQuietly(previous.client); } +} - private whenNotAborted(promise: Promise, signal?: AbortSignal): Promise { - if (!signal) return promise; - return new Promise((resolve, reject) => { - const onAbort = (): void => reject(new Error('Evonode discovery aborted')); - if (signal.aborted) { - onAbort(); - return; - } - signal.addEventListener('abort', onAbort, { once: true }); - promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort)); - }); - } +function whenNotAborted(promise: Promise, signal?: AbortSignal): Promise { + if (!signal) return promise; + return new Promise((resolve, reject) => { + const onAbort = (): void => reject(new Error('Evonode discovery aborted')); + if (signal.aborted) { + onAbort(); + return; + } + signal.addEventListener('abort', onAbort, { once: true }); + promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort)); + }); } export function shuffled(items: readonly T[]): T[] { diff --git a/src/types/external.d.ts b/src/types/external.d.ts index e2a21be..f6e1aa6 100644 --- a/src/types/external.d.ts +++ b/src/types/external.d.ts @@ -50,7 +50,6 @@ declare module '@dashevo/dapi-client' { declare module '@dashevo/dapi-client/lib/networkConfigs.js' { const networkConfigs: { testnet: { seeds: string[] }; - mainnet: { seeds: string[] }; }; export default networkConfigs; } From aca9fa3f2500ebafb30099b9c61fa6a0a9c3d80e Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 2 Nov 2026 13:12:53 -0600 Subject: [PATCH 4/4] fix(testnet): probe more evonodes for the height and skip empty seed lists Review follow-ups on #55. - When no node in the first batch of 3 answers the block-height probe, the next batch is asked, up to the stream budget of 9 nodes, before failing (still before broadcast). The streams then start from the batch that answered. With 31 of 41 testnet evonodes blackholed in a browser, 3 of 3 subscriptions now succeed (2 of 3 failed at 8 s). - A seed whose masternode list has no valid evonodes counts as a failed seed, so the next seed is asked. - The quorum service's health list is built from each node's platform_https endpoint (falling back to the Core host), so it matches the masternode list for evonodes registered with extended addresses. Co-Authored-By: Claude Opus 5.5 --- src/api/dapi-subscription-multi.test.ts | 35 +++++++++++++++++++++++++ src/api/dapi-subscription.ts | 32 +++++++++++++++++----- src/api/evonode-discovery.test.ts | 12 +++++++++ src/api/evonode-discovery.ts | 4 +++ src/api/evonodes.test.ts | 25 ++++++++++++++---- src/api/evonodes.ts | 22 +++++++++++++--- 6 files changed, 115 insertions(+), 15 deletions(-) diff --git a/src/api/dapi-subscription-multi.test.ts b/src/api/dapi-subscription-multi.test.ts index d0ef87a..1eb6d3e 100644 --- a/src/api/dapi-subscription-multi.test.ts +++ b/src/api/dapi-subscription-multi.test.ts @@ -367,6 +367,41 @@ describe('DAPISubscriptionClient multi-node IS lock subscription', () => { } }); + it('asks the next nodes when a whole batch fails its height request, and streams from those', async () => { + // Keep the shuffle in order so node0..2 form the first batch. + vi.spyOn(Math, 'random').mockReturnValue(0.999999); + for (const host of ['node0.test', 'node1.test', 'node2.test']) state.hungHeightHosts.add(host); + state.height = 700; + const client = new DAPISubscriptionClient({ network: 'testnet', dapiAddresses: addresses(6) }); + + const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT); + await vi.advanceTimersByTimeAsync(ISLOCK_HEIGHT_TIMEOUT_MS); + liveStreams()[0]?.emit('data', blockMessage); + const sub = await pending; + + const streaming = nodeClients().filter((c) => c.streams.length > 0).map((c) => c.options.dapiAddresses[0].host); + expect(streaming).toEqual(['node3.test', 'node4.test', 'node5.test']); + expect(liveStreams()[0]).toBeDefined(); + for (const node of nodeClients().filter((c) => c.streams.length > 0)) { + expect(node.core.subscribeToTransactionsWithProofs.mock.calls[0][1].fromBlockHeight).toBe(690); + } + liveStreams()[0].emit('data', lockMessage(TXID)); + await sub.wait(); + }); + + it(`stops probing heights after ${ISLOCK_MAX_NODE_ATTEMPTS} nodes`, async () => { + const many = addresses(12); + for (const url of many) state.failedHeightHosts.add(new URL(url).hostname); + const client = new DAPISubscriptionClient({ network: 'testnet', dapiAddresses: many }); + + const pending = client.subscribeForInstantSendLock(TXID, PUBKEY, UTXO, TIMEOUT); + const outcome = expect(pending).rejects.toThrow(new RegExp(`${ISLOCK_MAX_NODE_ATTEMPTS} nodes tried.*height refused`)); + await flush(); + await outcome; + expect(nodeClients()).toHaveLength(ISLOCK_MAX_NODE_ATTEMPTS); + expect(liveStreams()).toHaveLength(0); + }); + it('rejects at once when every node fails its height request', async () => { for (const host of ['node0.test', 'node1.test', 'node2.test']) state.failedHeightHosts.add(host); const client = new DAPISubscriptionClient({ network: 'testnet', dapiAddresses: addresses(3) }); diff --git a/src/api/dapi-subscription.ts b/src/api/dapi-subscription.ts index 3087724..730637d 100644 --- a/src/api/dapi-subscription.ts +++ b/src/api/dapi-subscription.ts @@ -147,7 +147,9 @@ export class DAPISubscriptionClient { private raceBestBlockHeight( txid: string, clients: Array, - signal?: AbortSignal + signal?: AbortSignal, + // Nodes that already failed in earlier batches, for the error message. + triedBefore = 0 ): Promise { return new Promise((resolve, reject) => { let done = false; @@ -170,7 +172,7 @@ export class DAPISubscriptionClient { if (++failures < clients.length) return; const reason = error instanceof Error ? error.message : String(error); settle(() => reject(new Error( - `InstantSend lock subscription failed for ${txid}: ${clients.length} nodes tried, last error: ${reason}` + `InstantSend lock subscription failed for ${txid}: ${triedBefore + clients.length} nodes tried, last error: ${reason}` ))); }; @@ -298,10 +300,28 @@ export class DAPISubscriptionClient { } onProgress?.('Getting current block height...'); nodes = shuffled(candidates); - clients = nodes - .slice(0, ISLOCK_CONCURRENT_NODES) - .map((address) => this.tryCreateNodeClient(address)); - currentHeight = await this.raceBestBlockHeight(txid, clients, raceAbort.signal); + // A batch where no node answers moves on to the next one, up to the + // same node budget the streams have; the streams then start from the + // batch that answered, skipping nodes already known to be down. + const probeLimit = Math.min(nodes.length, ISLOCK_MAX_NODE_ATTEMPTS); + let offset = 0; + for (;;) { + clients = nodes + .slice(offset, Math.min(offset + ISLOCK_CONCURRENT_NODES, probeLimit)) + .map((address) => this.tryCreateNodeClient(address)); + try { + currentHeight = await this.raceBestBlockHeight(txid, clients, raceAbort.signal, offset); + break; + } catch (error) { + if (raceAbort.signal.aborted) throw error; + for (const client of clients) this.disconnectQuietly(client); + clients = []; + offset += ISLOCK_CONCURRENT_NODES; + if (offset >= probeLimit) throw error; + console.warn(`[islock-sub] no block height from ${offset} nodes; asking the next ones`); + } + } + nodes = nodes.slice(offset); } catch (error) { for (const client of clients) this.disconnectQuietly(client); if (raceAbort.signal.aborted) { diff --git a/src/api/evonode-discovery.test.ts b/src/api/evonode-discovery.test.ts index c9323e2..d7cdde3 100644 --- a/src/api/evonode-discovery.test.ts +++ b/src/api/evonode-discovery.test.ts @@ -149,6 +149,18 @@ describe('fetchEvonodesFromSeeds', () => { expect(net.clients[0].disconnect).toHaveBeenCalled(); }); + it('treats a list with no valid evonodes as a failed seed', async () => { + const net = fakeNetwork({ + 'seed-1.test': { kind: 'list', hosts: [] }, + 'seed-2.test': { kind: 'list', hosts: ['10.0.0.5'] }, + }); + + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); + await vi.advanceTimersByTimeAsync(250); + + await expect(result).resolves.toEqual([endpoint('10.0.0.5')]); + }); + it('treats a list it cannot parse as a failed seed', async () => { const net = fakeNetwork({ 'seed-1.test': { kind: 'list', hosts: ['bad'] }, diff --git a/src/api/evonode-discovery.ts b/src/api/evonode-discovery.ts index 789e05b..2dbbf91 100644 --- a/src/api/evonode-discovery.ts +++ b/src/api/evonode-discovery.ts @@ -153,6 +153,10 @@ export function fetchEvonodesFromSeeds( fail(error); return; } + if (endpoints.length === 0) { + fail(new Error('Masternode list has no valid evonodes')); + return; + } console.log(`[evonode-discovery] ${endpoints.length} evonodes from seed ${seed.host}:${seed.port}`); finish(() => resolve(endpoints)); }); diff --git a/src/api/evonodes.test.ts b/src/api/evonodes.test.ts index c547b3c..ef3bb24 100644 --- a/src/api/evonodes.test.ts +++ b/src/api/evonodes.test.ts @@ -46,13 +46,17 @@ describe('parseEvonodeAddresses', () => { const result = parseEvonodeAddresses( payload( node('evo1.example.org'), - node('x', { address: 'user@evil.example:19999' }), - node('x', { address: 'host/path:19999' }), - node('x', { address: '[::1]:19999' }), + node('user@evil.example'), + node('host/path'), + node('[::1]'), node('999.1.1.1'), node('1.2.3.4.5'), - node('x', { address: '' }), - node('x', { address: 42 }), + // Records without platform_https fall back to the Core host. + node('x', { address: 'user@evil.example:19999', addresses: undefined }), + node('x', { address: '', addresses: undefined }), + node('x', { address: 42, addresses: undefined }), + node('x', { address: undefined, addresses: { platform_https: ['no-port'] } }), + node('x', { address: undefined, addresses: { platform_https: ['10.0.0.1:0'] } }), null, 'junk' ) @@ -61,6 +65,17 @@ describe('parseEvonodeAddresses', () => { expect(result).toEqual(['https://evo1.example.org:1443']); }); + it('uses the platform_https endpoint, which can differ from the Core P2P host', () => { + const result = parseEvonodeAddresses( + payload( + node('68.67.122.3', { addresses: { core_p2p: ['68.67.122.3:19999'], platform_https: ['evo3.example.org:443'] } }), + node('68.67.122.4', { addresses: undefined }) + ) + ); + + expect(result).toEqual(['https://evo3.example.org:443', 'https://68.67.122.4:1443']); + }); + it.each([undefined, null, 'x', {}, { data: {} }, { success: false }])( 'returns nothing for malformed payload %j', (bad) => { diff --git a/src/api/evonodes.ts b/src/api/evonodes.ts index 93eed29..f8ce714 100644 --- a/src/api/evonodes.ts +++ b/src/api/evonodes.ts @@ -48,15 +48,29 @@ export function parseEvonodeAddresses(payload: unknown): string[] { const endpoints = new Set(); for (const node of data) { if (!node || typeof node !== 'object') continue; - const { address, status, versionCheck } = node as Record; - if (status !== 'ENABLED' || versionCheck !== 'success' || typeof address !== 'string') continue; - // `address` is the Core P2P endpoint (host:19999); only the host is reused. - const endpoint = toDapiEndpoint(address.replace(/:\d+$/, '')); + const { address, addresses, status, versionCheck } = node as Record; + if (status !== 'ENABLED' || versionCheck !== 'success') continue; + const endpoint = platformEndpoint(addresses) ?? coreHostEndpoint(address); if (endpoint) endpoints.add(endpoint); } return [...endpoints]; } +/** The node's DAPI endpoint from `addresses.platform_https` ("host:port"), as the masternode list has it. */ +function platformEndpoint(addresses: unknown): string | undefined { + const https = (addresses as { platform_https?: unknown } | null | undefined)?.platform_https; + const first = Array.isArray(https) ? https[0] : undefined; + if (typeof first !== 'string') return undefined; + const separator = first.lastIndexOf(':'); + if (separator <= 0) return undefined; + return toDapiEndpoint(first.slice(0, separator), Number(first.slice(separator + 1))); +} + +/** Older records: `address` is the Core P2P endpoint (host:19999); only its host is reused, on port 1443. */ +function coreHostEndpoint(address: unknown): string | undefined { + return typeof address === 'string' ? toDapiEndpoint(address.replace(/:\d+$/, '')) : undefined; +} + /** * Healthy testnet evonode endpoints from the quorum service (one attempt, * 5 s). Resolves an empty list, after logging why, when the service fails.