From afe0a525552d5649b910126154d44d082d383936 Mon Sep 17 00:00:00 2001 From: RunTimeAdmin <248247024+RunTimeAdmin@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:42:22 -0600 Subject: [PATCH 1/2] Fix two SDK findings from the 15 November performance review matchEvidenceRoot hashes every tree it tries through one memo of pairs, skips a cut-off that counts the same payments as the one before, and stops at a budget of pairs actually hashed. sigvara_verify_evidence tries 64 cut-offs again within 20,000 hashes, so a finalized root ten payments behind a list of 2,000 is reproduced, after 2,064 hashes, where the old bound gave up after 11,984. The verifier's provider uses staticNetwork and asks eth_chainId once, before its first read, refusing a node on another chain: a first sigvara_check_agent makes 14 calls, one of them eth_chainId, where it made 18 with five. 376 SDK tests. Co-Authored-By: Claude Opus 5.5 --- packages/sdk/CHANGELOG.md | 27 +++++++++++ packages/sdk/README.md | 7 +-- packages/sdk/src/evidence.ts | 57 +++++++++++++++++----- packages/sdk/src/mcp.ts | 49 ++++++++----------- packages/sdk/src/verifier.ts | 69 ++++++++++++++++++++++++++- packages/sdk/test/evidence.test.ts | 20 ++++++++ packages/sdk/test/mcp.test.ts | 69 +++++++++++++++++++++++++-- packages/sdk/test/rpc-timeout.test.ts | 45 +++++++++++++++++ 8 files changed, 294 insertions(+), 49 deletions(-) diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index 82016b7..5886d9f 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -1,5 +1,32 @@ # Changelog +## 1.0.0-alpha.21 + +### Fixed: `sigvara_verify_evidence` missed a finalized root a few payments behind a long list + +To bound what a hostile oracle could cost, the tool tried fewer settlement cut-offs as a list grew, five at 2,000 +payments, so a busy agent whose finalized root was ten payments behind its list read as not reproduced. Each +cut-off rebuilt its tree from nothing, though neighbouring cut-offs share all but a few pairs. + +`matchEvidenceRoot` now hashes every tree it tries through one memo of pairs and skips a cut-off that counts the +same payments as the one before. Its new `hashBudget` limits the pairs it hashes: past it no further cut-off is +tried, though the first always is. Its new `memo` lets several matches over one list share their work. The tool +tries up to 64 cut-offs again, within 20,000 hashes, and its finalized and pending matches share one memo. On 2,000 +payments with the root ten behind, the match finds it after 2,064 hashes; the old bound gave up after 11,984, and +the old code unbounded took 23,933. A list ordered so that its trees share little still stops at the budget, and +the result says the search reached its limit. `evidenceRoot` and `evidenceLayers` take an optional pair function +and work as before without one. + +### Fixed: the verifier asked the RPC its chain id with every wave of reads + +ethers sent `eth_chainId` beside every wave of reads, to notice a node that changes chains: 21 to 29% of the calls +the SDK made. `SigvaraVerifier` now builds its provider with `staticNetwork` and asks once, before its first read, +which the new `getRpcChainId` also returns. A verifier given a `chainId` refuses every read from an RPC that serves +another chain, with a `NETWORK_ERROR` naming both; it used to read whichever chain the URL served. A failed ask is +asked again on the next read. `sigvara-mcp` takes its own chain check from the verifier, so a first +`sigvara_check_agent` makes 14 calls, one of them `eth_chainId`, where it made 18 with five of them, and later +ones do not ask again. `rpcChainId` moved to the verifier and is still exported from `dist/mcp`. + ## 1.0.0-alpha.20 ### Added: the band an agent's score is in diff --git a/packages/sdk/README.md b/packages/sdk/README.md index 3b1fad0..ecbeddd 100644 --- a/packages/sdk/README.md +++ b/packages/sdk/README.md @@ -500,8 +500,8 @@ contract. The oracle's answer is untrusted: - It is read to at most 4 MB, with no redirects followed. - It is refused unless it names the agent asked about, or if it lists more than 2,000 payments. - Only the fields a leaf commits to, and the oracle's `outcomeSource` label from two fixed values, are returned. -- Matching a long list tries fewer settlement cut-offs, newest first, so a hostile list costs about a second, - not a minute. +- Matching a root hashes each pair of nodes once across all the settlement cut-offs it tries, and tries no more + once it has hashed 20,000, so a hostile list costs about a second, not a minute. The server is also a module, for a host that runs its own transport: @@ -513,7 +513,8 @@ const reply = await server.handle({ jsonrpc: '2.0', id: 1, method: 'tools/list' ``` Built from `rpcUrl` like this, it checks the chain as the command does. Given a `verifier` of your own, it -checks the chain only if you also pass `readChainId`. +checks the chain itself only if you also pass `readChainId`, though a `SigvaraVerifier` built with a `chainId` +refuses to read from another chain in any case. ## Links diff --git a/packages/sdk/src/evidence.ts b/packages/sdk/src/evidence.ts index aeba29e..52c3b38 100644 --- a/packages/sdk/src/evidence.ts +++ b/packages/sdk/src/evidence.ts @@ -82,24 +82,30 @@ export function hashPair(a: string, b: string): string { : ethers.keccak256(ethers.concat([b, a])); } -/** Every layer of the tree, leaves first. Empty for no leaves. */ -export function evidenceLayers(leaves: string[]): string[][] { +/** + * Every layer of the tree, leaves first. Empty for no leaves. `pair` hashes two nodes into their parent, and + * must give what hashPair gives: matchEvidenceRoot passes a memo of it. + */ +export function evidenceLayers(leaves: string[], pair: (a: string, b: string) => string = hashPair): string[][] { if (!leaves.length) return []; const layers = [leaves.slice()]; let level = leaves.slice(); while (level.length > 1) { const next: string[] = []; - for (let i = 0; i < level.length; i += 2) next.push(i + 1 < level.length ? hashPair(level[i], level[i + 1]) : level[i]); + for (let i = 0; i < level.length; i += 2) next.push(i + 1 < level.length ? pair(level[i], level[i + 1]) : level[i]); layers.push(next); level = next; } return layers; } -/** The root over these leaves, in order. The zero hash for none, which the contract treats as no root. */ -export function evidenceRoot(leaves: string[]): string { +/** + * The root over these leaves, in order. The zero hash for none, which the contract treats as no root. `pair` is + * as for evidenceLayers. + */ +export function evidenceRoot(leaves: string[], pair: (a: string, b: string) => string = hashPair): string { if (!leaves.length) return ZERO_ROOT; - const layers = evidenceLayers(leaves); + const layers = evidenceLayers(leaves, pair); return layers[layers.length - 1][0]; } @@ -130,11 +136,20 @@ export interface RootMatch { * reproduces it, as the explorer's matchRoot does (site/assets/explorer.js). * * A proposal commits to the payments settled by the time it was made, and a payer can sign an outcome after - * that. So the root is tried against every payment, against those counted by each cut-off, and against both - * with every payer-signed outcome read as not reported. With `at` (a proposal's time, in seconds) only that - * cut-off is tried. Each try rebuilds a tree, so the work is about (cut-offs tried) times (payments) times two; - * a caller facing a long list from a source it does not trust lowers `maxCuts`. `leaves`, when given, are the - * records' leaves as served, already built. Throws if a record cannot be a leaf at all. + * that. So the root is tried against every payment, against those counted by each of up to `maxCuts` cut-offs, + * and against both with every payer-signed outcome read as not reported. With `at` (a proposal's time, in + * seconds) only that cut-off is tried. `leaves`, when given, are the records' leaves as served, already built. + * Throws if a record cannot be a leaf at all. + * + * Neighbouring cut-offs count nearly the same payments in the same order, so their trees share every pair to the + * left of where they first differ. Every tree here is hashed through one memo of pairs, so a cut-off costs only + * the pairs no tree before it held: on a list in settlement order, whose lower cut-offs leave out its last + * payments, at most one a level of each tree, eleven at 2,000 payments. A cut-off that counts the same payments as + * the one before it is skipped. `hashBudget` bounds the pairs hashed: once past it no further cut-off is tried, + * though the first always is, in full. A caller facing a long list from a source it does not trust sets one, since + * a list ordered so that its trees share little costs a whole tree per cut-off. `memo`, when given, is that memo, + * so matches over the same list can share it; each pair hashed adds one entry, and only what a call adds counts + * against its budget. * * `delays` (chain id to seconds, as /evidence serves them) says how much later a payment chain's payment enters a * score than its settlement. `evidenceCut` (also from /evidence, present when a chain is on) says a proposal's @@ -145,12 +160,14 @@ export interface RootMatch { export function matchEvidenceRoot( records: EvidenceRecord[], committed: string, - { at, maxCuts = 64, leaves, delays = {}, evidenceCut = null }: { + { at, maxCuts = 64, leaves, delays = {}, evidenceCut = null, hashBudget = Infinity, memo = new Map() }: { at?: number; maxCuts?: number; leaves?: string[]; delays?: Record; evidenceCut?: { quantumSeconds: number; windowSeconds: number } | null; + hashBudget?: number; + memo?: Map; } = {}, ): RootMatch | null { if (!HASH.test(committed)) return null; @@ -170,13 +187,27 @@ export function matchEvidenceRoot( const cuts = at !== undefined ? [Number(at), ...quantized.filter((t) => t < Number(at))] : [Infinity, ...[...new Set(counts)].sort((a, b) => b - a).slice(0, maxCuts)]; + const start = memo.size; + const pair = (a: string, b: string) => { + const key = a + b; + let parent = memo.get(key); + if (parent === undefined) memo.set(key, (parent = hashPair(a, b))); + return parent; + }; + let tried: number[] | null = null; for (const cut of cuts) { + // The memo grows by one for each pair hashed. The first cut-off is tried whatever the budget. + if (tried && memo.size - start > hashBudget) break; const indexes = records.map((_, i) => (counts[i] <= cut ? i : -1)).filter((i) => i >= 0); + // The cut-offs only go down, so one that counts as many payments as the last one tried counts the same ones, + // whose trees did not match. + if (tried && indexes.length === tried.length) continue; + tried = indexes; const tries: [string[], boolean][] = [[asServed, false]]; if (indexes.some((i) => signed[i])) tries.push([unsetLeaves, true]); for (const [leaves, unset] of tries) { const picked = indexes.map((i) => leaves[i]); - if (evidenceRoot(picked).toLowerCase() === want) return { indexes, leaves: picked, cut, unset }; + if (evidenceRoot(picked, pair).toLowerCase() === want) return { indexes, leaves: picked, cut, unset }; } } return null; diff --git a/packages/sdk/src/mcp.ts b/packages/sdk/src/mcp.ts index 1350637..1909168 100644 --- a/packages/sdk/src/mcp.ts +++ b/packages/sdk/src/mcp.ts @@ -20,7 +20,7 @@ import readline from 'readline'; import type { Readable, Writable } from 'stream'; -import { ethers, FetchRequest } from 'ethers'; +import { ethers } from 'ethers'; import { SigvaraVerifier } from './verifier'; import { REASON_DETAIL, scoreBand, standingDecision } from './standing'; import type { StandingReason } from './standing'; @@ -70,8 +70,9 @@ export interface TrustServerOptions { /** * Asks the RPC which chain it serves. Checked once, before the first read, so a server pointed at * the wrong endpoint refuses rather than reading another chain's contracts at these addresses. - * Defaults to an eth_chainId call on rpcUrl when the verifier is built here. With an injected - * verifier and no readChainId, the chain is not checked. + * Defaults to the verifier's getRpcChainId when the verifier is built here: the one eth_chainId it + * asks before its first read anyway. With an injected verifier and no readChainId, the server does + * not check the chain. */ readChainId?: () => Promise; /** @@ -101,9 +102,12 @@ export interface TrustServerOptions { const MAX_EVIDENCE_BYTES = 4 * 1024 * 1024; const MAX_EVIDENCE_RECORDS = 2_000; /** - * Leaves hashed into trees while matching a root, at most. Each cut-off tried rebuilds a tree over the list, so an - * oracle serving a long list that matches nothing could otherwise hold the server's only thread for close to a - * minute. A long list gets fewer cut-offs, the newest first, where a finalized root usually sits. + * Pair hashes computed while matching one root, at most. The trees for every cut-off tried hash through one memo, + * so this counts only the pairs no earlier tree held: on a list in settlement order, the first trees and then at + * most eleven a tree for each cut-off after them, so all 64 cut-offs fit at the 2,000 payments this tool reads. A + * list ordered so that its trees share little costs whole trees per cut-off, and an oracle serving a long one that + * matches nothing could otherwise hold the server's only thread for close to a minute. Past the budget no further + * cut-off is tried, though the first always is, so one match hashes at most this plus one cut-off's trees. */ const MATCH_BUDGET = 20_000; /** Payments confirmed with verifyEvidence, the last listed, in one read. The root match covers the rest. */ @@ -113,11 +117,6 @@ const MAX_LISTED = 50; /** The latest settlement time accepted, the end of the year 9999, so a date can always be written. */ const MAX_SETTLED_AT = 253_402_300_799; -/** How many cut-offs to try for a list of `n`, within MATCH_BUDGET. */ -function cutsFor(n: number): number { - return Math.max(1, Math.min(64, Math.floor(MATCH_BUDGET / (2 * Math.max(1, n))))); -} - /** GET a JSON body, giving up after `timeoutMs` and refusing one larger than `maxBytes`. */ export async function fetchJsonBounded(url: string, timeoutMs: number, maxBytes: number): Promise { const controller = new AbortController(); @@ -151,17 +150,8 @@ export async function fetchJsonBounded(url: string, timeoutMs: number, maxBytes: } } -/** Asks an RPC endpoint which chain it serves, with one eth_chainId call. */ -export async function rpcChainId(rpcUrl: string, timeoutMs = 30_000): Promise { - const req = new FetchRequest(rpcUrl); - req.timeout = timeoutMs; - req.body = { jsonrpc: '2.0', id: 1, method: 'eth_chainId', params: [] }; - const res = await req.send(); - res.assertOk(); - const body = res.bodyJson as { result?: unknown }; - if (typeof body?.result !== 'string') throw new Error('eth_chainId returned no result'); - return Number(BigInt(body.result)); -} +// The verifier asks with it now. Exported here as well, where it was first, so an import from dist/mcp still works. +export { rpcChainId } from './verifier'; /** * What a failed read tells the model: ethers' short message, never the long one. The long message @@ -413,8 +403,8 @@ export function createTrustServer(options: TrustServerOptions): TrustServer { rpcUrl: options.rpcUrl as string, addresses, chainId, ...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }), }); - const readChainId = options.readChainId - ?? (options.verifier ? undefined : () => rpcChainId(options.rpcUrl as string, options.timeoutMs)); + // The verifier's own answer, so the server's check and the verifier's are one eth_chainId, not two. + const readChainId = options.readChainId ?? (options.verifier ? undefined : () => verifier.getRpcChainId()); const audience = options.audience ?? null; if (audience !== null) { // Whatever the challenge format refuses, refuse now rather than on every sigvara_challenge. @@ -701,19 +691,22 @@ export function createTrustServer(options: TrustServerOptions): TrustServer { const confirmed = new Map(); // Payments whose outcome the root covers as not reported, though the oracle now lists one the payer signed. const readAsUnreported = new Set(); - const maxCuts = cutsFor(records.length); + // One memo for both matches below: a pending root is usually over nearly the payments the finalized one is. + const memo = new Map(); if (problems.length) { finalized = { root: finalRoot === ZERO_ROOT ? null : finalRoot, reproduced: null, detail: 'Not checked: some payments listed cannot be part of any root.' }; } else if (finalRoot === ZERO_ROOT) { finalized = { root: null, reproduced: null, detail: 'No score with an evidence root has been finalized for this agent.' }; } else { - const m = matchEvidenceRoot(records, finalRoot, { leaves, maxCuts, delays }); + const m = matchEvidenceRoot(records, finalRoot, { leaves, delays, hashBudget: MATCH_BUDGET, memo }); if (!m) { finalized = { root: finalRoot, reproduced: false, detail: 'No reading of the payments listed reproduces the finalized root. The oracle no longer lists the set ' + 'this score was made on, or lists it differently.' - + (maxCuts < 64 ? ` With ${records.length} payments listed, only the ${maxCuts} latest settlement cut-offs were tried.` : '') }; + // The memo was empty before this match, so its size is the pairs the match hashed. + + (memo.size > MATCH_BUDGET ? ` With ${records.length} payments listed, the search reached its limit of ` + + `${MATCH_BUDGET} hashes, so older settlement cut-offs may not have been tried.` : '') }; } else { m.indexes.forEach((i) => covered.add(i)); if (m.unset) m.indexes.forEach((i) => { if (records[i].outcomeSource === 'payer_signed') readAsUnreported.add(i); }); @@ -759,7 +752,7 @@ export function createTrustServer(options: TrustServerOptions): TrustServer { detail: 'Proposed with no evidence root, so it commits to no payments.' }; } else if (pending.exists) { const m = problems.length ? null - : matchEvidenceRoot(records, pending.evidenceRoot, { at: Number(pending.proposedAt), leaves, maxCuts, delays, evidenceCut }); + : matchEvidenceRoot(records, pending.evidenceRoot, { at: Number(pending.proposedAt), leaves, delays, evidenceCut, hashBudget: MATCH_BUDGET, memo }); pendingResult = { root: pending.evidenceRoot, proposedAt: iso(pending.proposedAt), reproduced: problems.length ? null : !!m }; } const notYetFinalized = problems.length ? null diff --git a/packages/sdk/src/verifier.ts b/packages/sdk/src/verifier.ts index 16ae618..7cb31b3 100644 --- a/packages/sdk/src/verifier.ts +++ b/packages/sdk/src/verifier.ts @@ -1,4 +1,5 @@ import { ethers, FetchRequest } from 'ethers'; +import type { JsonRpcApiProviderOptions, PerformActionRequest } from 'ethers'; import { IDENTITY_ABI, REPUTATION_ABI, STAKING_ABI, ERC20_ABI } from './abis'; /** Multicall3, at the address it has on most EVM chains, Arc testnet among them. */ @@ -29,8 +30,64 @@ import type { */ export const DEFAULT_RPC_TIMEOUT_MS = 30_000; +/** Asks an RPC endpoint which chain it serves, with one eth_chainId call. */ +export async function rpcChainId(rpcUrl: string, timeoutMs = 30_000): Promise { + const req = new FetchRequest(rpcUrl); + req.timeout = timeoutMs; + req.body = { jsonrpc: '2.0', id: 1, method: 'eth_chainId', params: [] }; + const res = await req.send(); + res.assertOk(); + const body = res.bodyJson as { result?: unknown }; + if (typeof body?.result !== 'string') throw new Error('eth_chainId returned no result'); + return Number(BigInt(body.result)); +} + +/** + * The provider a verifier reads through: ethers' JsonRpcProvider with staticNetwork, and the node asked its chain + * id once, before the first read reaches it. + * + * Without staticNetwork ethers sends eth_chainId beside every wave of reads, to notice a node that changes chains, + * and the 15 November performance review measured that at 21 to 29% of the requests the SDK makes. With it and + * nothing more, ethers takes the configured chain on trust, and an RPC URL for another chain, where the same + * contracts can sit at the same addresses, would be read as if it were this one. So when a chain is configured, + * every read waits for the one answer and is refused if it names another chain. + */ +class ChainCheckedProvider extends ethers.JsonRpcProvider { + private served?: Promise; + + constructor(connection: FetchRequest, private readonly expectedChainId: number | undefined, options: JsonRpcApiProviderOptions) { + super(connection, expectedChainId, { ...options, staticNetwork: true }); + } + + /** The chain id the node says it serves, asked once. A failed ask is not kept, so the next read asks again. */ + servedChainId(): Promise { + if (!this.served) { + const { url, timeout } = this._getConnection(); + this.served = rpcChainId(url, timeout).catch((err) => { + this.served = undefined; + throw err; + }); + } + return this.served; + } + + // Every read reaches the node through here: contract calls, and getCode for Multicall3. + override async _perform(req: PerformActionRequest): Promise { + if (this.expectedChainId !== undefined) { + const served = await this.servedChainId(); + if (served !== this.expectedChainId) { + throw ethers.makeError( + `SigvaraVerifier: the RPC serves chain ${served}, but chain ${this.expectedChainId} is configured; nothing was read`, + 'NETWORK_ERROR', { event: 'chainId' }, + ); + } + } + return super._perform(req); + } +} + export class SigvaraVerifier { - private readonly provider: ethers.JsonRpcProvider; + private readonly provider: ChainCheckedProvider; private readonly addresses: ContractAddresses; private _chainId: number | undefined; @@ -49,11 +106,19 @@ export class SigvaraVerifier { connection.timeout = timeoutMs; // ethers folds what is asked in one tick into one batch, of up to 100 calls by default. Arc's public // RPC answers 20 and refuses the rest with a rate limit, so a busy gate would refuse agents over it. - this.provider = new ethers.JsonRpcProvider(connection, undefined, { batchMaxCount: 20 }); + this.provider = new ChainCheckedProvider(connection, config.chainId, { batchMaxCount: 20 }); this.addresses = config.addresses; this._chainId = config.chainId; } + /** + * The chain id the RPC serves, asked with one eth_chainId the first time and kept; a failed ask is asked again. + * With a chainId in the config, every read waits for it and is refused when it is another chain. + */ + getRpcChainId(): Promise { + return this.provider.servedChainId(); + } + private identity() { return (this._identity ??= new ethers.Contract(this.addresses.identity, IDENTITY_ABI, this.provider)); } diff --git a/packages/sdk/test/evidence.test.ts b/packages/sdk/test/evidence.test.ts index 11a4426..eae9a37 100644 --- a/packages/sdk/test/evidence.test.ts +++ b/packages/sdk/test/evidence.test.ts @@ -99,6 +99,26 @@ describe('matchEvidenceRoot', () => { expect(matchEvidenceRoot(FIXTURE, `0x${'99'.repeat(32)}`)).toBeNull(); expect(matchEvidenceRoot(FIXTURE, 'not a hash')).toBeNull(); }); + + it('hashes each pair once across its trees, and past its hash budget tries no further cut-off', () => { + // Sixty-four payments in settlement order, and a root over all but the last ten. + const list = Array.from({ length: 64 }, (_, i) => ({ ...FIXTURE[0], txHash: ethers.toBeHex(i + 1, 32), settledAt: 1_700_000_000 + i })); + const committed = root(list.slice(0, 54)); + const memo = new Map(); + expect(matchEvidenceRoot(list, committed, { memo })!.indexes).toHaveLength(54); + // The first tree is 63 pairs, and each lower cut-off adds at most one a level, six levels. The eleven trees + // rebuilt whole come to 638. + expect(memo.size).toBeLessThanOrEqual(63 + 10 * 6); + // A match given the same memo hashes nothing it already holds. + const held = memo.size; + matchEvidenceRoot(list, committed, { memo }); + expect(memo.size).toBe(held); + // The first cut-off is tried in full, both readings, whatever the budget, and the next one is not once past it. + expect(matchEvidenceRoot(list, committed, { hashBudget: 0 })).toBeNull(); + const later = FIXTURE.map((r, i) => (i === 2 ? { ...r, outcomeSource: 'payer_signed' } : r)); + const unsetRoot = root(later.map((r, i) => (i === 2 ? { ...r, success: null } : r))); + expect(matchEvidenceRoot(later, unsetRoot, { hashBudget: 0 })).toMatchObject({ indexes: [0, 1, 2], unset: true }); + }); }); describe('a payment that settled on a payment chain', () => { diff --git a/packages/sdk/test/mcp.test.ts b/packages/sdk/test/mcp.test.ts index 3b59ee0..1a5e7d3 100644 --- a/packages/sdk/test/mcp.test.ts +++ b/packages/sdk/test/mcp.test.ts @@ -13,6 +13,7 @@ import { signChallenge } from '../src/challenge'; import { seedToKeyPair, pubKeyToBytes32 } from '../src/keys'; import { computeDidHash } from '../src/did'; import { AgentStatus, SlashState } from '../src/types'; +import { ERC20_ABI, IDENTITY_ABI, REPUTATION_ABI, STAKING_ABI } from '../src/abis'; const CHAIN = 5042002; const AGENT = '0x00000000000000000000000000000000000000aa'; @@ -464,6 +465,57 @@ describe('the chain check when the server builds its own verifier', () => { rpc.close(); } }); + + it('asks the RPC for its chain id once, and not with every wave of reads', async () => { + // A node holding one Active, bonded agent, which counts what it is asked. Without staticNetwork ethers sent + // eth_chainId beside every wave of reads, four in a first sigvara_check_agent, on top of the server's own check. + const token = `0x${'44'.repeat(20)}`; + const contracts: Record = { + [ARC_TESTNET.addresses.identity.toLowerCase()]: new ethers.Interface(IDENTITY_ABI), + [ARC_TESTNET.addresses.reputation.toLowerCase()]: new ethers.Interface(REPUTATION_ABI), + [ARC_TESTNET.addresses.staking.toLowerCase()]: new ethers.Interface(STAKING_ABI), + [token]: new ethers.Interface(ERC20_ABI), + }; + const holds: Record = { + getIdentity: [['0x00000000000000000000000000000000000000cc', AGENT, `0x${'11'.repeat(32)}`, AgentStatus.Active, 1_700_000_000n]], + isActive: [true], hasMinimumStake: [true], getTotalScore: [50], getEarnedScore: [50], + getReputation: [[10, 5, 20, 0, 5, 10, BigInt(NOW - 60)]], + getStake: [1000n * SVR], minimumStake: [1000n * SVR], getPendingWithdrawal: [0n, 0n], + getSlashProposal: [[ethers.ZeroHash, ethers.ZeroAddress, ethers.ZeroAddress, 0n, SlashState.None, '0x', 0n, 0n]], + getPendingScore: [[[0, 0, 0, 0, 0, 0, 0n], 0n, false, ethers.ZeroHash, ethers.ZeroAddress, 0n]], + svrToken: [token], decimals: [18], + }; + const asked: string[] = []; + const answer = (m: { id: unknown; method: string; params: any[] }) => { + asked.push(m.method); + if (m.method === 'eth_chainId') return { jsonrpc: '2.0', id: m.id, result: ethers.toQuantity(CHAIN) }; + const iface = contracts[m.params[0].to.toLowerCase()]; + const fn = iface.parseTransaction({ data: m.params[0].data })!.name; + return { jsonrpc: '2.0', id: m.id, result: iface.encodeFunctionResult(fn, holds[fn]) }; + }; + const rpc = createServer((req, res) => { + let body = ''; + req.on('data', (d) => { body += d; }); + req.on('end', () => { + const m = JSON.parse(body); + res.setHeader('content-type', 'application/json'); + res.end(JSON.stringify(Array.isArray(m) ? m.map(answer) : answer(m))); + }); + }); + await new Promise((r) => rpc.listen(0, '127.0.0.1', () => r())); + const port = (rpc.address() as { port: number }).port; + try { + const s = createTrustServer({ chainId: CHAIN, addresses: ARC_TESTNET.addresses, rpcUrl: `http://127.0.0.1:${port}`, now: () => NOW }); + for (let i = 0; i < 2; i++) { + const r = await call(s, 'sigvara_check_agent', { agent: DID, threshold: 40 }); + expect(r.value.decision).toMatchObject({ ok: true }); + } + expect(asked.filter((m) => m === 'eth_chainId')).toHaveLength(1); + expect(asked.filter((m) => m === 'eth_call').length).toBeGreaterThan(20); + } finally { + rpc.close(); + } + }); }); describe('JSON-RPC edges', () => { @@ -792,19 +844,30 @@ describe('sigvara_verify_evidence, after review', () => { }); it('bounds the work a long list that matches nothing can cause', async () => { - const list = Array.from({ length: 2000 }, (_, i) => pay(i, { outcomeSource: 'payer_signed' })); + // Newest first, so a lower cut-off leaves out the first payments and moves every pair after them: each cut-off + // costs close to two whole trees, the memo saves little, and only the hash budget stops the search. Unbounded, + // a list like this took about 25 seconds on the review machine. + const list = Array.from({ length: 2000 }, (_, i) => pay(1999 - i, { outcomeSource: 'payer_signed' })); const t0 = Date.now(); const r = await run(chain(`0x${'ab'.repeat(32)}`), answer(list)); const took = Date.now() - t0; expect(r.value.finalized.reproduced).toBe(false); - expect(r.value.finalized.detail).toMatch(/only the 5 latest settlement cut-offs were tried/); - // Unbounded, this list took about 25 seconds on the review machine; bounded it is a small multiple of one rebuild. + expect(r.value.finalized.detail).toMatch(/reached its limit of 20000 hashes/); expect(took).toBeLessThan(5_000); const over = await run(chain(ZERO_ROOT), answer(Array.from({ length: 2001 }, (_, i) => pay(i)))); expect(over.isError).toBe(true); expect(over.text).toMatch(/more than 2000 payments/); }, 60_000); + it('reproduces a finalized root ten payments behind a list of 2000', async () => { + // A busy agent's finalized root trails the payments its oracle lists. Bounding the number of cut-offs by the + // length of the list tried only the five latest at 2000, so this root read as not reproduced. + const list = Array.from({ length: 2000 }, (_, i) => pay(i)); + const r = await run(chain(leafRoot(list.slice(0, 1990))), answer(list)); + expect(r.value.finalized).toMatchObject({ reproduced: true, covers: 1990, checkedWithContract: 100, confirmedByContract: 100 }); + expect(r.value.notYetFinalized).toBe(10); + }, 60_000); + it('reports a time no calendar can write, and a non-hex payer, as payments that cannot be rebuilt', async () => { const r = await run(chain(ZERO_ROOT), answer([pay(0, { settledAt: 9_000_000_000_000 }), pay(1, { payer: 'XE16167T4VS28BKVEF0VOZAYPEAHW6M05JE' })])); expect(r.isError).toBe(false); diff --git a/packages/sdk/test/rpc-timeout.test.ts b/packages/sdk/test/rpc-timeout.test.ts index 2426576..4eb74f7 100644 --- a/packages/sdk/test/rpc-timeout.test.ts +++ b/packages/sdk/test/rpc-timeout.test.ts @@ -94,3 +94,48 @@ describe('SigvaraVerifier: batches a node accepts', () => { } }); }); + +describe('SigvaraVerifier: the chain the node serves', () => { + it('is asked once before the first read, again after a failed ask, and a node on another chain is not read', async () => { + const asked: string[] = []; + // The node's answer to eth_chainId; null fails the ask. + let serves: string | null = null; + const server = http.createServer((req, res) => { + let body = ''; + req.on('data', (c) => { body += c; }); + req.on('end', () => { + const j = JSON.parse(body); + const answers = (Array.isArray(j) ? j : [j]).map((c: { id: number; method: string }) => { + asked.push(c.method); + if (c.method !== 'eth_chainId') return { jsonrpc: '2.0', id: c.id, result: `0x${'0'.repeat(63)}1` }; + return serves === null + ? { jsonrpc: '2.0', id: c.id, error: { code: -32000, message: 'busy' } } + : { jsonrpc: '2.0', id: c.id, result: serves }; + }); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(Array.isArray(j) ? answers : answers[0])); + }); + }); + await new Promise((r) => server.listen(0, '127.0.0.1', r)); + try { + const rpcUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; + const v = new SigvaraVerifier({ rpcUrl, addresses: ADDRESSES, chainId: 5042002 }); + await expect(v.isActive(DID)).rejects.toThrow(); + serves = '0x4cef52'; + // Three waves of reads, as three trust checks make. Without staticNetwork ethers asked with each of them. + for (let i = 0; i < 3; i++) await Promise.all([v.isActive(DID), v.hasMinimumStake(DID), v.getTotalScore(DID)]); + expect(asked.filter((m) => m === 'eth_chainId')).toHaveLength(2); + expect(asked.filter((m) => m === 'eth_call')).toHaveLength(9); + + asked.length = 0; + serves = '0x1'; + const elsewhere = new SigvaraVerifier({ rpcUrl, addresses: ADDRESSES, chainId: 5042002 }); + await expect(elsewhere.isActive(DID)).rejects.toThrow(/serves chain 1, but chain 5042002 is configured/); + await expect(elsewhere.getMinimumStake()).rejects.toThrow(/serves chain 1/); + expect(asked).toEqual(['eth_chainId']); + } finally { + server.closeAllConnections(); + server.close(); + } + }); +}); From 56be6863a7953b66c4c592401b5aa78c9fbd5b14 Mon Sep 17 00:00:00 2001 From: RunTimeAdmin <248247024+RunTimeAdmin@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:47:43 -0600 Subject: [PATCH 2/2] SDK test count 385, as this branch leaves it Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- docs/whitepaper.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 07738a9..4167d39 100644 --- a/README.md +++ b/README.md @@ -602,9 +602,9 @@ Everything below runs offline, with no deployed contracts, except three fork sui |---|---|---| | Contracts: unit, fuzz, E2E, upgrade, deploy-script, handover and 22 invariants | 796 | `forge test` | | Oracle | 1514 | `cd oracle && node --test` | -| SDK | 381 | `cd packages/sdk && npx vitest run` | +| SDK | 385 | `cd packages/sdk && npx vitest run` | -Counts are individual tests, so each of the 22 invariants counts once. The contracts were counted on 10 November 2026 and have not changed since; the SDK on 13 November and the oracle on 14 November. Without a fork URL, 784 of the contract tests run and 12 skip. Run after `npm run build`, 372 of the SDK's 381 pass and 9 skip: the 8 integration tests, which need a live deployment, and one check that runs only in CI. Without a build, the 9 built-package tests skip too. Vitest strips types without checking them, so `npm run typecheck` in `packages/sdk` checks the tests against the SDK's types as well as the sources. [`scripts/mutation`](scripts/mutation/README.md) holds the harnesses that measure whether these tests would catch a deliberate break in the contracts or deploy scripts. +Counts are individual tests, so each of the 22 invariants counts once. The contracts were counted on 10 November 2026 and have not changed since; the SDK on 15 November and the oracle on 14 November. Without a fork URL, 784 of the contract tests run and 12 skip. Run after `npm run build`, 376 of the SDK's 385 pass and 9 skip: the 8 integration tests, which need a live deployment, and one check that runs only in CI. Without a build, the 9 built-package tests skip too. Vitest strips types without checking them, so `npm run typecheck` in `packages/sdk` checks the tests against the SDK's types as well as the sources. [`scripts/mutation`](scripts/mutation/README.md) holds the harnesses that measure whether these tests would catch a deliberate break in the contracts or deploy scripts. Merges to `main` carry `[skip ci]`, to save CI minutes, so the push to `main` does not run the suites again. A change to code runs them on its pull request and merges only once every job there has passed; a docs-only change is checked locally with `node scripts/check-docs.mjs`. Each is checked against the `main` its branch started from, so two changes merged close together can still break `main`. That happened once, on 11 November 2026, and the next merge fixed it. The rule that every job must pass dates from 14 November 2026: before it, #281, #282 and #288 merged with their oracle job failing, and #283 and #297 fixed the tests that failed. On 13 November the oracle and SDK suites were run on `main` at `eaccb91` and passed. diff --git a/docs/whitepaper.md b/docs/whitepaper.md index 3c93f7e..fed12ba 100644 --- a/docs/whitepaper.md +++ b/docs/whitepaper.md @@ -635,7 +635,7 @@ apply to a fresh deployment, not to the testnet. **Test coverage**, as of 14 November 2026: 796 contract tests across 36 Foundry test files including fuzz and invariant tests (the two slash drill suites fork Arc testnet themselves, and -the twelve in `SplitAuthorityFork` skip without `--fork-url`), 1514 oracle tests and 381 in the SDK (9 skip +the twelve in `SplitAuthorityFork` skip without `--fork-url`), 1514 oracle tests and 385 in the SDK (9 skip outside CI and without a live deployment; every test that runs passes). CI runs Foundry, the oracle and SDK suites, and Slither static analysis on every push, plus a documentation check that fails when the site states something the contracts do not.