diff --git a/CHANGELOG.md b/CHANGELOG.md index 30794caa7..60924ddac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Members-only content in public repositories, Environments and the secret scan. P - **A partial clone's lazy blob fetch reads only the packs that hold the blobs** it needs, instead of downloading the whole repository again for each checkout, `git log -p` or blame. - **Issue and PR lists say when the repository's label definitions were not read completely**, instead of presenting the first page as all of them. - **A removed member's sealed author events on their own issues and PRs follow the late-content rule**, so they can no longer keep writing readable values under an old key. +- **Trending loads in a fresh tab on testnet.** A new connection learns the network's protocol version before the first page query. Before, the SDK started at protocol 13 and refused Trending's time-range read with "requires protocol version 14+" until you clicked Try again. ## [0.1.0] - 2026-10-04 diff --git a/forge-web/e2e/v2-explore.spec.ts b/forge-web/e2e/v2-explore.spec.ts index a226a3e6e..78ad2d499 100644 --- a/forge-web/e2e/v2-explore.spec.ts +++ b/forge-web/e2e/v2-explore.spec.ts @@ -34,7 +34,12 @@ test('x1. explore lists recent repos, trending, most starred and most forked, an await expect(recent.locator('a[href*="/repo"]').first()).toBeVisible({ timeout: 60_000 }) // Trending and Most starred are proved ranked reads (C-1); no "needs an indexer" note. await expect(page.getByTestId('trending-note')).toHaveCount(0) - await expect(page.getByTestId('explore-trending').getByRole('heading', { name: /Trending (this week|today)/ })).toBeVisible() + const trending = page.getByTestId('explore-trending') + await expect(trending.getByRole('heading', { name: /Trending (this week|today)/ })).toBeVisible() + // Its time-range read lands in a fresh tab, though it can be the page's first query: the SDK + // learns the protocol version (14 or more for `timeRange`) before the page queries. + await expect(trending.getByTestId('ranked-row').first().or(trending.getByText(/^No repo has \d+ new stars/))).toBeVisible({ timeout: 60_000 }) + await expect(trending.getByText('That read did not land')).toHaveCount(0) await expect(page.getByTestId('explore-most-starred').getByRole('heading', { name: 'Most starred' })).toBeVisible() // Most forked (ranked repo.forkOf, fresh core): the repos that are not forks form the index's // null group, which is never shown as a row. diff --git a/forge-web/lib/sdk/service-addresses.test.ts b/forge-web/lib/sdk/service-addresses.test.ts index 1a9d590a1..6b026fb7b 100644 --- a/forge-web/lib/sdk/service-addresses.test.ts +++ b/forge-web/lib/sdk/service-addresses.test.ts @@ -12,6 +12,8 @@ const recorded = vi.hoisted(() => ({ snapshots: vi.fn(async () => ({})), emptyTestnet: false, gate: vi.fn(), + version: 14, + latest: vi.fn(async (_q: { contractIds: string[] }) => new Map()), })) vi.mock('./budget', () => ({ installDapiFetchGate: recorded.gate, dapiBudget: { retryAfterMs: () => 0 } })) vi.mock('./wasm-fetch', () => ({ compileWasm: () => Promise.resolve({}), onWasmProgress: () => () => undefined })) @@ -30,18 +32,19 @@ vi.mock('../constants', async (importOriginal) => { }) vi.mock('@dashevo/evo-sdk', () => ({ ensureInitialized: async () => undefined, + DataContract: { fromBase64: () => ({}) }, EvoSDK: class { constructor(options: EvoSDKOptions) { recorded.options.push(options) } connect = async () => undefined - contracts = { fetch: recorded.fetch, getLatestVersions: async () => new Map() } + contracts = { fetch: recorded.fetch, getLatestVersions: recorded.latest, addKnown: async () => true } wasm = { free: recorded.free } - version = () => 14 + version = () => recorded.version }, })) import testnetDeployment from '../../../forge-contracts/deployments/testnet.json' import { NETWORKS, type Network } from '../constants' -import { EvoSdkService } from './service' +import { EvoSdkService, LEARN_VERSION_MS } from './service' const services: EvoSdkService[] = [] beforeEach(() => { @@ -49,9 +52,11 @@ beforeEach(() => { recorded.options.length = 0 recorded.emptyTestnet = false recorded.fetch.mockReset().mockResolvedValue({}) - recorded.snapshots.mockClear() + recorded.snapshots.mockReset().mockResolvedValue({}) recorded.free.mockClear() recorded.gate.mockClear() + recorded.version = 14 + recorded.latest.mockReset().mockResolvedValue(new Map()) }) afterEach(() => { services.splice(0).forEach((service) => service.cleanup()) @@ -112,3 +117,74 @@ describe('trusted SDK endpoint selection', () => { expect(recorded.gate).toHaveBeenCalledWith(NETWORKS.devnet.dapiAddresses) }) }) + +describe('the protocol version before the first query', () => { + const CORE = 'D2ZLdgghZTKGpES35a3KVDp5necSfZqqRHLSknjRPAV9' + beforeEach(() => { + recorded.snapshots.mockResolvedValue({ [CORE]: { version: 1, platformVersion: 14, bytes: '' } }) + }) + + /** + * Connect, noting each `getLatestVersions`: its ids, and whether a connection was live then. + * (No localStorage here, so the seeded-snapshot check after going live is always due.) + */ + async function connectNoting(network: Network): Promise<{ ids: string[]; live: boolean }[]> { + const service = new EvoSdkService() + services.push(service) + const calls: { ids: string[]; live: boolean }[] = [] + recorded.latest.mockImplementation(async ({ contractIds }) => { + calls.push({ ids: contractIds, live: service.generation > 0 }) + recorded.version = 14 + return new Map([[CORE, { version: 1 }]]) + }) + await service.initialize({ network, contractIds: [] }) + await vi.advanceTimersByTimeAsync(0) + return calls + } + + it('a new SDK below the snapshots\' version learns it from one proved read before going live', async () => { + // rc.8 starts a testnet SDK at 13; with every contract seeded, nothing else would teach it 14. + // The read asks the seeded-snapshot check's question, so that check sends nothing of its own. + recorded.version = 13 + expect(await connectNoting('testnet')).toEqual([{ ids: [CORE], live: false }]) + }) + + it('an SDK already at that version, or with no snapshots, sends nothing before going live', async () => { + // Only the seeded-snapshot check, after going live. + expect(await connectNoting('testnet')).toEqual([{ ids: [CORE], live: true }]) + + recorded.version = 13 + recorded.snapshots.mockResolvedValue({}) + expect(await connectNoting('testnet')).toEqual([]) + }) + + it('a read that fails or does not answer does not hold up the connect', async () => { + recorded.version = 13 + recorded.latest.mockRejectedValue(new Error('no available addresses')) + const failed = new EvoSdkService() + services.push(failed) + await failed.initialize({ network: 'testnet', contractIds: [] }) + expect(failed.getStatus().phase).toBe('ready') + + let answer!: () => void + const late = new Promise>((resolve) => (answer = () => resolve(new Map()))) + // Only the warm-up's read hangs (the seeded-snapshot check after going live is tracked anyway). + recorded.latest.mockReset().mockReturnValueOnce(late).mockResolvedValue(new Map()) + const hung = new EvoSdkService() + const connecting = hung.initialize({ network: 'testnet', contractIds: [] }) + await vi.waitFor(() => expect(recorded.latest).toHaveBeenCalledWith({ contractIds: [CORE] })) + expect(hung.getStatus().phase).not.toBe('ready') + await vi.advanceTimersByTimeAsync(LEARN_VERSION_MS) + await connecting + expect(hung.getStatus().phase).toBe('ready') + + // The read it stopped waiting for still borrows the SDK: retiring the connection frees it + // only once that read settles (a wasm `free()` under a running call traps). + recorded.free.mockClear() + hung.cleanup() + await vi.advanceTimersByTimeAsync(0) + expect(recorded.free).not.toHaveBeenCalled() + answer() + await vi.waitFor(() => expect(recorded.free).toHaveBeenCalledTimes(1)) + }) +}) diff --git a/forge-web/lib/sdk/service.ts b/forge-web/lib/sdk/service.ts index 077317e19..d9c5b94dd 100644 --- a/forge-web/lib/sdk/service.ts +++ b/forge-web/lib/sdk/service.ts @@ -27,9 +27,11 @@ * failure never falls back to unverified reads. * * What a new connection takes over from the one it replaces: - * - the protocol version: wasm-sdk already seeds each new SDK at the version the last one - * learned (`protocol_version_store`, mainnet/testnet; a devnet's floor is current), and the - * handle's `version()` never reports lower than a version a connection proved; + * - the protocol version: wasm-sdk seeds each new SDK at the version the last one learned + * (`protocol_version_store`, in localStorage, mainnet/testnet; a devnet's floor is current), + * the handle's `version()` never reports lower than a version a connection proved, and a + * connection below the deployment's version learns it before it goes live (without a + * store every connection starts at the built-in floor; {@link learnVersion}); * - which bundled contract snapshots turned out outdated: those are fetched, not seeded * again. It preloads only the contracts pages used recently, not every id ever named. * A refreshed connection is installed only while no write is running and the last one ended @@ -88,8 +90,15 @@ export interface EvoSdkConfig { export interface Connection { readonly sdk: EvoSDK readonly seeded: Map + /** The warm-up's version read, when it was still running as the connection went live ({@link learnVersion}). */ + readonly warming?: Promise + /** The seeded contracts' latest versions, when the warm-up read them ({@link revalidateSeeded} uses them). */ + readonly latest?: LatestVersions } +/** What `getDataContractsLatestVersions` answers: each contract's current version. */ +type LatestVersions = Awaited> + /** What a new connection takes over from the one it replaces. */ export interface Carry { /** @@ -228,6 +237,11 @@ function versionOf(sdk: EvoSDK): number | undefined { * frees a retired connection once its last call returned, {@link EvoSdkService.retire}). */ function dispose(connection: Connection): void { + // The warm-up's read still borrows the SDK: free it once that read settles. + if (connection.warming !== undefined) { + void connection.warming.then(() => dispose({ sdk: connection.sdk, seeded: connection.seeded })) + return + } try { ;(connection.sdk.wasm as unknown as { free?: () => void }).free?.() } catch { @@ -1050,13 +1064,16 @@ async function preload(sdk: EvoSDK, contractIds: readonly string[]): Promise): Promise> { +async function seed(sdk: EvoSDK, deploymentKey: string, outdated: ReadonlySet): Promise<{ seeded: Map; floor: number }> { const seeded = new Map() const snapshots = await loadContractSnapshots(deploymentKey).catch(() => ({})) + const floor = Math.max(0, ...Object.values(snapshots).map((s) => s.platformVersion)) const entries = Object.entries(snapshots).filter(([id]) => !outdated.has(id)) - if (entries.length === 0) return seeded + if (entries.length === 0) return { seeded, floor } const { DataContract } = await import('@dashevo/evo-sdk') for (const [id, snapshot] of entries) { try { @@ -1066,7 +1083,53 @@ async function seed(sdk: EvoSDK, deploymentKey: string, outdated: ReadonlySet }> { + if ((versionOf(sdk) ?? 0) >= floor) return {} + let settled = false + let latest: LatestVersions | undefined + const read = sdk.contracts + .getLatestVersions({ contractIds: [...contractIds] }) + .then((answer) => { + latest = answer + }) + .catch(() => undefined) + .finally(() => { + settled = true + }) + let timer: ReturnType | undefined + await Promise.race([ + read, + new Promise((resolve) => { + timer = setTimeout(resolve, LEARN_VERSION_MS) + }), + ]) + clearTimeout(timer) + // (Wrapped: an async function returning the promise itself would wait for it.) + if (!settled) return { running: read } + return latest !== undefined ? { latest } : {} } /** @@ -1074,7 +1137,8 @@ async function seed(sdk: EvoSDK, deploymentKey: string, outdated: ReadonlySet !seeded.has(id))) step(signal, sdk) - return { sdk, seeded } + const ids = seeded.size > 0 ? [...seeded.keys()] : [NETWORKS[config.network].dpnsContractId] + const { latest, running } = await learnVersion(sdk, floor, ids) + const connection: Connection = { sdk, seeded, ...(running !== undefined ? { warming: running } : {}), ...(latest !== undefined ? { latest } : {}) } + if (signal.aborted) { + dispose(connection) + throw new Error('the connect was abandoned') + } + return connection } /** The process-wide evo-sdk service singleton. */