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 0c3a5e1..2745642 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+\./.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..1eb6d3e 100644 --- a/src/api/dapi-subscription-multi.test.ts +++ b/src/api/dapi-subscription-multi.test.ts @@ -15,12 +15,17 @@ 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', () => { class FakeClient { options: any; streams: any[] = []; + listStreams: any[] = []; disconnect = vi.fn(async () => {}); core: any; platform = {}; @@ -36,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'); @@ -68,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') }), @@ -76,6 +108,14 @@ 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) => ({ + ...(await importOriginal()), + fetchTestnetEvonodeAddresses: fallbackMock, +})); import { DAPISubscriptionClient, @@ -94,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); }; @@ -127,6 +177,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(() => {}); @@ -312,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) }); @@ -513,10 +603,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 +670,142 @@ 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 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) { + 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, read once from one seed', async () => { + state.smlHosts = hosts(6); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + // Nothing is fetched until a subscription (or warm-up) needs it. + expect(state.clients).toHaveLength(0); + + 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); + // 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); + }); + + it('subscribes with a single discovered evonode', async () => { + state.smlHosts = hosts(1); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + const sub = await startDiscovered(client); + + expect(liveStreams()).toHaveLength(1); + liveStreams()[0].emit('data', lockMessage(TXID)); + await expect(sub.wait()).resolves.toBeInstanceOf(Uint8Array); + }); + + it('replaces a failed stream with another discovered evonode', async () => { + state.smlHosts = hosts(5); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + const sub = await startDiscovered(client); + + liveStreams()[0].emit('error', new Error('GrpcError: stream reset')); + await flush(); + + 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 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 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' }); + + 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(nonSeedClients()).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 vi.advanceTimersByTimeAsync(5); + await client.disconnect(); + await assertion; + + expect(nonSeedClients()).toHaveLength(0); + expect(seedClients()[0].listStreams[0].cancel).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 vi.advanceTimersByTimeAsync(5); + controller.abort(); + await assertion; + expect(nonSeedClients()).toHaveLength(0); + }); + + it('warmEvonodeDiscovery() reads the list ahead, and the subscription reuses it', async () => { + state.smlHosts = hosts(3); + const client = new DAPISubscriptionClient({ network: 'testnet' }); + + const warm = client.warmEvonodeDiscovery(); + await vi.advanceTimersByTimeAsync(5); + await warm; + expect(seedClients()).toHaveLength(1); + expect(nonSeedClients()).toHaveLength(0); + + await startDiscovered(client); + expect(seedClients()).toHaveLength(1); + expect(nodeClients()).toHaveLength(3); + }); + + 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(seedClients()).toHaveLength(0); + expect(fallbackMock).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/api/dapi-subscription.ts b/src/api/dapi-subscription.ts index 5173a14..730637d 100644 --- a/src/api/dapi-subscription.ts +++ b/src/api/dapi-subscription.ts @@ -1,10 +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, + seedAddresses, + shuffled, + type DapiAddress, +} from './evonode-discovery.js'; import { fetchPlatformStatus, fetchPlatformStatusIfSdkReady, @@ -46,30 +54,19 @@ 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; 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; - 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 +74,46 @@ 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') { + // 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 { - 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. On testnet it covers the + * evonodes found by the last discovery, so it is undefined before the + * first subscription. + */ + 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. */ @@ -131,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; @@ -154,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}` ))); }; @@ -259,27 +277,56 @@ 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 { - currentHeight = await this.raceBestBlockHeight(txid, clients, raceAbort.signal); + 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); + // 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) { + throw new Error(`InstantSend lock subscription aborted for ${txid}`); + } throw error; } finally { this.activeSubscriptions.delete(abortRace); @@ -291,6 +338,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 +608,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 */ } } /** @@ -621,6 +673,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/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-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 new file mode 100644 index 0000000..d7cdde3 --- /dev/null +++ b/src/api/evonode-discovery.test.ts @@ -0,0 +1,416 @@ +/* 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 { + EVONODE_LIST_MAX_AGE_MS, + EvonodeDiscovery, + MASTERNODE_LIST_TIMEOUT_MS, + MIN_HEALTHY_EVONODES, + SEED_HEDGE_DELAY_MS, + evonodeEndpointsFromSml, + fetchEvonodesFromSeeds, + parseDapiAddress, + seedAddresses, + type DapiAddress, +} 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: 'end' } + | { kind: 'silent' } + | { kind: 'slow-open'; afterMs: number }; + +/** 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}`); + 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); + 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'); + } + if (b.kind === 'end') stream.emit('end'); + }, 100); + return stream; + }), + }, + }; + clients.push(client); + return client; + }; + return { clients, streams, createClient }; +} + +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(() => {}); +}); + +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 = (platform: unknown) => ({ getPlatformHTTPSEndpoint: () => platform }); + 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 }), + ], + }; + + expect(evonodeEndpointsFromSml(sml)).toEqual(['https://68.67.122.1:1443', 'https://evo.example.org:2443']); + }); +}); + +describe('fetchEvonodesFromSeeds', () => { + 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, net.createClient, 'testnet'); + await vi.advanceTimersByTimeAsync(100); + + 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', '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, net.createClient, 'testnet'); + await vi.advanceTimersByTimeAsync(250); + + 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 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'] }, + '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(`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, 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([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 net = fakeNetwork({ + 'seed-1.test': { kind: 'reject' }, + 'seed-2.test': { kind: 'error-end' }, + 'seed-3.test': { kind: 'reject' }, + }); + + const result = fetchEvonodesFromSeeds(seeds, net.createClient, 'testnet'); + const assertion = expect(result).rejects.toThrow(/3 seeds tried/); + await vi.advanceTimersByTimeAsync(250); + await assertion; + 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, net.createClient, '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, 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 healthy = vi.fn<[], Promise>(); + 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, + 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; + healthy.mockReset(); + healthy.mockResolvedValue([]); + }); + + async function settle(promise: Promise): Promise { + await vi.advanceTimersByTimeAsync(100); + return promise; + } + + 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'] } }); + + 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(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); + + 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(hostsOf(await discovery.getNodes())).toEqual(['10.0.0.1']); + expect(seedClients()).toHaveLength(1); + + now = EVONODE_LIST_MAX_AGE_MS; + 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); + await vi.advanceTimersByTimeAsync(0); + 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'] } }); + + const a = discovery.getNodes(); + const b = discovery.getNodes(); + const warm = discovery.warm(); + await vi.advanceTimersByTimeAsync(100); + + await expect(a).resolves.toHaveLength(1); + await expect(b).resolves.toHaveLength(1); + await expect(warm).resolves.toBeUndefined(); + expect(seedClients()).toHaveLength(1); + expect(healthy).toHaveBeenCalledOnce(); + }); + + 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); + healthy.mockResolvedValue([endpoint('10.0.0.9')]); + + 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(hostsOf(await settle(discovery.getNodes()))).toEqual(['10.0.0.1']); + }); + + 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/); + await vi.advanceTimersByTimeAsync(100); + await assertion; + }); + + it('rejects at once when the signal aborts', async () => { + const { discovery } = discover({}); + const controller = new AbortController(); + + const pending = discovery.getNodes(controller.signal); + controller.abort(); + + await expect(pending).rejects.toThrow(/aborted/); + }); + + 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; + + 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; + await vi.advanceTimersByTimeAsync(0); + + expect(shared.disconnect).toHaveBeenCalled(); + expect(net.streams[1].cancel).toHaveBeenCalled(); + expect(discovery.sharedClient).toBeUndefined(); + }); + + 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(); + }); +}); diff --git a/src/api/evonode-discovery.ts b/src/api/evonode-discovery.ts new file mode 100644 index 0000000..2dbbf91 --- /dev/null +++ b/src/api/evonode-discovery.ts @@ -0,0 +1,349 @@ +/* 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. */ +export interface DapiAddress { + protocol: string; + host: string; + 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; + +/** 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 { + 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 endpoints = new Set(); + 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 endpoint = platform && toDapiEndpoint(platform.host, platform.port); + if (endpoint) endpoints.add(endpoint); + } + return [...endpoints]; +} + +function disconnectQuietly(client: any): void { + Promise.resolve().then(() => client?.disconnect()).catch(() => {}); +} + +function cancelQuietly(stream: any): void { + try { stream?.cancel(); } catch { /* ignore */ } +} + +/** + * 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: CreateDapiClient, + network: string, + signal?: AbortSignal +): Promise { + return new Promise((resolve, reject) => { + // 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) release(attempt); + 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 } = {}; + open.add(attempt); + + const fail = (error: unknown): void => { + if (!open.delete(attempt)) return; + release(attempt); + 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]); + 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; + } + 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; + } + 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)); + }); + // 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); + } + }; + + 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(); + }); +} + +export interface EvonodeDiscoveryOptions { + network: string; + seeds: readonly DapiAddress[]; + 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. 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 considered, not dapi-client's testnet + * address provider result, which also filters by a snapshot of addresses + * bundled with the library. + * + * 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: CreateDapiClient; + private readonly fetchHealthy: () => Promise; + private readonly now: () => number; + private list?: { endpoints: string[]; fetchedAt: number }; + // 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.fetchHealthy = options.fetchHealthy ?? fetchTestnetEvonodeAddresses; + this.now = options.now ?? Date.now; + } + + /** + * 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 { + return this.shared?.client; + } + + /** + * The current evonode endpoints. Rejects when none can be found, or when + * `signal` aborts. + */ + async getNodes(signal?: AbortSignal): Promise { + 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'); + } + const nodes = endpoints.map(parseDapiAddress); + this.updateSharedClient(endpoints, nodes); + return nodes; + } + + /** + * 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.pending?.controller.abort(); + const shared = this.shared; + this.shared = undefined; + if (shared) disconnectQuietly(shared.client); + } + + /** 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.pending) { + const controller = new AbortController(); + 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.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 { + const key = endpoints.join(','); + if (this.shared?.key === key) return; + const previous = this.shared; + this.shared = { key, client: this.createClient(nodes) }; + if (previous) disconnectQuietly(previous.client); + } +} + +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[] { + 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.test.ts b/src/api/evonodes.test.ts index de4f0f2..ef3bb24 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( @@ -49,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' ) @@ -64,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) => { @@ -72,7 +84,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 +127,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 +161,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 +175,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..f8ce714 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; - +/** Port of the evonode DAPI endpoint (gRPC-web over HTTPS). */ 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 @@ -32,60 +48,40 @@ 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 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 { 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; +} + /** - * 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..28eb2a7 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, })), })); @@ -70,11 +72,11 @@ describe('IslockService', () => { const service = new IslockService({ network: 'testnet', rpcUrl: TESTNET.rpcUrl, - dapiAddresses: TESTNET.dapiAddresses, }); 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 +114,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..4c12975 100644 --- a/src/api/islock.ts +++ b/src/api/islock.ts @@ -68,9 +68,10 @@ 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 - * subscriptions don't replay historical IS locks. Mainnet uses JSON-RPC + * 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/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/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..2982313 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,15 @@ 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?.(); }); + 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..f6e1aa6 100644 --- a/src/types/external.d.ts +++ b/src/types/external.d.ts @@ -47,6 +47,13 @@ declare module '@dashevo/dapi-client' { export = DAPIClient; } +declare module '@dashevo/dapi-client/lib/networkConfigs.js' { + const networkConfigs: { + testnet: { 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', ], }, });