conformance: a conformance suite for the AIP-23 proof kinds (one verdict form over the published verifiers, 35 rules derived from code, 122 vectors, a runner for any implementation, a second verifier in Python); four defects it found in the published verifiers are fixed here: the market judge accepted a timeline with its submission event withheld, and three verifiers threw on malformed input instead of refusing by name

This commit is contained in:
Aere Network committed 2026-10-03 13:26:03 +03:00
1 parent 2c000478dc
commit c9f2932e04
145 files changed
+17395 -104

No files matched your search

+39 -23
View File
@@ -21,6 +21,8 @@ import { ruleaza, rezultatHash, rezultatOcteti, specHash as specHashOf, keccak,
const AICI = path.dirname(fileURLToPath(import.meta.url));
export const ARTEFACT = path.join(AICI, 'AereComputeMarketV3.json');
export const STATUS = ['None', 'Open', 'Claimed', 'Submitted', 'Disputed', 'Paid', 'Refunded', 'Slashed'];
/** The rules the judge names. A refusal can only name one of these (the helper throws otherwise), so the list is the whole list. */
export const REGULI = ['forma', 'intreg', 'postare', 'termen', 'fereastra', 'arbitru', 'plata', 'rezultat', 'replay', 'dovada'];
export const MODE = ['REPLAY', 'ZK_VERIFIED', 'OPTIMISTIC'];
const H32 = /^0x[0-9a-f]{64}$/i;
const sha256 = (b) => '0x' + crypto.createHash('sha256').update(b).digest('hex');
@@ -83,51 +85,65 @@ export async function cronologie({ ethers, provider, market, artifact, jobId, fr
const ok = (m, cond, text) => { if (!cond) m.push(text); return !!cond; };
const low = (x) => String(x || '').toLowerCase();
export function judeca(d) {
const motive = [], randuri = []; const j = d.job, ev = d.events || []; const un = (n) => ev.find((e) => e.name === n); const toate = (n) => ev.filter((e) => e.name === n);
// a timeline the judge cannot read is refused by name, never thrown at the caller (2026-10-03: until today a missing field threw)
try { return judecaForma(d); } catch (e) { return { verdict: 'INVALID', motive: ['malformed timeline: ' + String(e.message).slice(0, 120)], reguli: ['forma'], randuri: [] }; }
}
function judecaForma(d) {
const motive = [], randuri = [], reguli = new Set(); const cere = (cond, text, regula) => { if (!REGULI.includes(regula)) throw new Error('piata: undeclared rule ' + regula); if (!cond) { motive.push(text); reguli.add(regula); } return !!cond; };
const j = d.job, ev = d.events || []; const un = (n) => ev.find((e) => e.name === n); const toate = (n) => ev.filter((e) => e.name === n);
const t = (e) => (e ? e.timestamp : null);
if (!j || !ev.length) return { verdict: 'INCOMPLETE', motive: ['no job or no events'], randuri };
if (!j || !ev.length) return { verdict: 'INCOMPLETE', motive: ['no job or no events'], reguli: [], randuri };
const intregi = (...x) => x.every(Number.isInteger);
const okForma = typeof j === 'object' && Array.isArray(ev) && ev.every((e) => e && typeof e === 'object' && typeof e.name === 'string' && e.args && typeof e.args === 'object' && typeof e.from === 'string' && intregi(e.timestamp, e.blockNumber))
&& !!d.constants && typeof d.constants.arbiter === 'string' && intregi(d.constants.arbitrationTimeout, j.deadline, j.submittedAt, j.challengeWindow, j.status) && (d.result == null || typeof d.result === 'string') /*PAZA:forma*/;
if (!cere(okForma, 'malformed timeline: events need name, args, from, integer timestamp and blockNumber; the job and the constants their integer fields', 'forma')) return { verdict: 'INVALID', motive, reguli: [...reguli].sort(), randuri };
for (const e of ev) randuri.push(`${new Date(e.timestamp * 1000).toISOString()} #${e.blockNumber} ${e.name} by ${e.from.slice(0, 10)}.. ${Object.entries(e.args).filter(([k]) => k !== 'result').map(([k, v]) => k + '=' + (H32.test(String(v)) ? String(v).slice(0, 10) + '..' : String(v).length > 20 ? String(v).slice(0, 10) + '..' : v)).join(' ')}`);
const posted = un('JobPosted'), claimed = un('JobClaimed'), submitted = un('ResultSubmitted') || un('ResultReferenced'), disputed = un('JobDisputed'), resolved = un('DisputeResolved'), timedOut = un('DisputeTimedOut'), paid = un('JobPaid'), refunded = un('JobRefunded'), slashed = un('JobSlashed');
// the timeline is WHOLE against the job state: an event the state implies cannot be withheld (2026-10-03: until today a timeline
// without its ResultSubmitted event skipped the challenge-window rule and came out VALID; found by the conformance suite)
const okIntreg = !!posted && (!j.provider || /^0x0{40}$/.test(String(j.provider)) || !!claimed) && (!(j.submittedAt > 0) || !!submitted) && (!(j.disputedAt > 0) || !!disputed) /*PAZA:intreg*/;
cere(okIntreg, 'the timeline lacks an event the job state implies (JobPosted; JobClaimed when a provider is set; the submission when submittedAt is set; JobDisputed when disputedAt is set)', 'intreg');
if (!posted) return { verdict: 'INVALID', motive, reguli: [...reguli].sort(), randuri };
// R0 the posting matches the job (the reward is compared through the payout events: the contract zeroes j.reward on settlement)
ok(motive, posted && low(posted.args.requester) === low(j.requester) && low(posted.args.specHash) === low(j.specHash) && Number(posted.args.deadline) === j.deadline && (j.reward === '0' || posted.args.reward === j.reward), 'the JobPosted event does not match the job (requester, reward, specHash, deadline)');
cere(posted && low(posted.args.requester) === low(j.requester) && low(posted.args.specHash) === low(j.specHash) && Number(posted.args.deadline) === j.deadline && (j.reward === '0' || posted.args.reward === j.reward) /*PAZA:postare*/, 'the JobPosted event does not match the job (requester, reward, specHash, deadline)', 'postare');
// R1 deadlines: a claim and a submission only up to the deadline
if (claimed) ok(motive, t(claimed) <= j.deadline /*PAZA:termen*/, 'the job was claimed after its deadline');
if (submitted) ok(motive, t(submitted) <= j.deadline && t(submitted) === j.submittedAt /*PAZA:termen*/, 'the result was submitted after the deadline, or submittedAt does not match the event');
if (refunded && claimed && !submitted && !disputed) ok(motive, t(refunded) > j.deadline /*PAZA:termen*/, 'the escrow was reclaimed as expired before the deadline');
if (claimed) cere(t(claimed) <= j.deadline /*PAZA:termen*/, 'the job was claimed after its deadline', 'termen');
if (submitted) cere(t(submitted) <= j.deadline && t(submitted) === j.submittedAt /*PAZA:termen*/, 'the result was submitted after the deadline, or submittedAt does not match the event', 'termen');
if (refunded && claimed && !submitted && !disputed) cere(t(refunded) > j.deadline /*PAZA:termen*/, 'the escrow was reclaimed as expired before the deadline', 'termen');
// R2 the challenge window: a dispute strictly inside it, a finalize (by anyone but the requester) only after it
if (disputed) ok(motive, submitted && t(disputed) < j.submittedAt + j.challengeWindow /*PAZA:fereastra*/, 'the dispute was raised after the challenge window closed');
if (paid && !resolved && submitted) ok(motive, low(paid.from) === low(j.requester) || t(paid) >= j.submittedAt + j.challengeWindow /*PAZA:fereastra*/, 'the provider was paid inside the challenge window by someone other than the requester (acceptResult is the requester\'s)');
if (disputed) cere(submitted && t(disputed) < j.submittedAt + j.challengeWindow /*PAZA:fereastra*/, 'the dispute was raised after the challenge window closed', 'fereastra');
if (paid && !resolved && submitted) cere(low(paid.from) === low(j.requester) || t(paid) >= j.submittedAt + j.challengeWindow /*PAZA:fereastra*/, 'the provider was paid inside the challenge window by someone other than the requester (acceptResult is the requester\'s)', 'fereastra');
// R3 who resolved: the arbiter, or the permissionless timeout after ARBITRATION_TIMEOUT
if (resolved) ok(motive, low(resolved.from) === low(d.constants.arbiter) /*PAZA:arbitru*/, 'the dispute was resolved by an address that is not the ARBITER');
if (timedOut) ok(motive, disputed && t(timedOut) >= j.disputedAt + d.constants.arbitrationTimeout /*PAZA:arbitru*/, 'the dispute was timed out before ARBITRATION_TIMEOUT');
if (resolved || timedOut) ok(motive, !!disputed, 'a resolution without a dispute');
if (resolved) cere(low(resolved.from) === low(d.constants.arbiter) /*PAZA:arbitru*/, 'the dispute was resolved by an address that is not the ARBITER', 'arbitru');
if (timedOut) cere(disputed && t(timedOut) >= j.disputedAt + d.constants.arbitrationTimeout /*PAZA:arbitru*/, 'the dispute was timed out before ARBITRATION_TIMEOUT', 'arbitru');
if (resolved || timedOut) cere(!!disputed /*PAZA:arbitru*/, 'a resolution without a dispute', 'arbitru');
// R4 the terminal payout matches the state
const st = STATUS[j.status]; let terminal = null;
if (st === 'Paid') { const okP = paid && low(paid.args.provider) === low(j.provider) && paid.args.reward === posted.args.reward && (!claimed || paid.args.bondReturned === claimed.args.providerBond) && (!resolved || resolved.args.providerWon === true) /*PAZA:plata*/; ok(motive, okP, 'status Paid without a matching JobPaid (provider, reward, bond returned, providerWon)'); terminal = paid && { status: st, payee: paid.args.provider, amount: paid.args.reward, txHash: paid.txHash, blockHash: paid.blockHash }; }
else if (st === 'Slashed') { const okS = slashed && disputed && resolved && resolved.args.providerWon === false && low(slashed.args.challenger) === low(disputed.args.challenger) && (!claimed || slashed.args.slashedProviderBond === claimed.args.providerBond) /*PAZA:plata*/; ok(motive, okS, 'status Slashed without a matching JobSlashed (challenger, provider bond) and DisputeResolved(false)'); terminal = slashed && { status: st, payee: slashed.args.challenger, amount: slashed.args.slashedProviderBond, refundedTo: j.requester, txHash: slashed.txHash, blockHash: slashed.blockHash }; }
else if (st === 'Refunded') { const okR = refunded && low(refunded.args.requester) === low(j.requester) && refunded.args.reward === posted.args.reward /*PAZA:plata*/; ok(motive, okR, 'status Refunded without a matching JobRefunded (requester, reward)'); terminal = refunded && { status: st, payee: refunded.args.requester, amount: refunded.args.reward, txHash: refunded.txHash, blockHash: refunded.blockHash }; }
ok(motive, j.status !== 5 || j.reward === '0', 'a paid job still holds a reward');
if (st === 'Paid') { const okP = paid && low(paid.args.provider) === low(j.provider) && paid.args.reward === posted.args.reward && (!claimed || paid.args.bondReturned === claimed.args.providerBond) && (!resolved || resolved.args.providerWon === true) /*PAZA:plata*/; cere(okP, 'status Paid without a matching JobPaid (provider, reward, bond returned, providerWon)', 'plata'); terminal = paid && { status: st, payee: paid.args.provider, amount: paid.args.reward, txHash: paid.txHash, blockHash: paid.blockHash }; }
else if (st === 'Slashed') { const okS = slashed && disputed && resolved && resolved.args.providerWon === false && low(slashed.args.challenger) === low(disputed.args.challenger) && (!claimed || slashed.args.slashedProviderBond === claimed.args.providerBond) /*PAZA:plata*/; cere(okS, 'status Slashed without a matching JobSlashed (challenger, provider bond) and DisputeResolved(false)', 'plata'); terminal = slashed && { status: st, payee: slashed.args.challenger, amount: slashed.args.slashedProviderBond, refundedTo: j.requester, txHash: slashed.txHash, blockHash: slashed.blockHash }; }
else if (st === 'Refunded') { const okR = refunded && low(refunded.args.requester) === low(j.requester) && refunded.args.reward === posted.args.reward /*PAZA:plata*/; cere(okR, 'status Refunded without a matching JobRefunded (requester, reward)', 'plata'); terminal = refunded && { status: st, payee: refunded.args.requester, amount: refunded.args.reward, txHash: refunded.txHash, blockHash: refunded.blockHash }; }
cere((j.status !== 5 || j.reward === '0') /*PAZA:plata*/, 'a paid job still holds a reward', 'plata');
// R5 the result bytes hash to the on-chain commitment
if (d.result && submitted) ok(motive, keccak(Buffer.from(d.result.slice(2), 'hex')) === low(j.resultHash) && low(submitted.args.resultHash) === low(j.resultHash) /*PAZA:rezultat*/, 'the submitted bytes do not hash to the job\'s resultHash');
if (d.result && submitted) cere(keccak(Buffer.from(d.result.slice(2), 'hex')) === low(j.resultHash) && low(submitted.args.resultHash) === low(j.resultHash) /*PAZA:rezultat*/, 'the submitted bytes do not hash to the job\'s resultHash', 'rezultat');
// R6 REPLAY: the public spec recomputed; the arbiter's decision must agree with the recomputation
let replay = null;
if (d.spec && d.result && submitted) {
ok(motive, specHashOf(d.spec) === low(j.specHash), 'the public spec does not hash to the job\'s specHash');
cere(specHashOf(d.spec) === low(j.specHash) /*PAZA:replay*/, 'the public spec does not hash to the job\'s specHash', 'replay');
try { const r = ruleaza(d.spec); const corect = rezultatHash(r) === low(j.resultHash); replay = { recomputedHash: rezultatHash(r), providerRight: corect };
if (disputed) { replay.challengerRecomputedRight = low(disputed.args.recomputedResultHash) === rezultatHash(r); }
if (resolved) ok(motive, resolved.args.providerWon === corect /*PAZA:replay*/, `the arbiter sided with the ${resolved.args.providerWon ? 'provider' : 'challenger'} but the replay says the provider is ${corect ? 'right' : 'wrong'}`);
if (st === 'Paid' && !resolved) ok(motive, corect /*PAZA:replay*/, 'the provider was paid for a result the replay does not reproduce (no one disputed in time)');
} catch (e) { motive.push('the replay could not run: ' + e.message); }
if (resolved) cere(resolved.args.providerWon === corect /*PAZA:replay*/, `the arbiter sided with the ${resolved.args.providerWon ? 'provider' : 'challenger'} but the replay says the provider is ${corect ? 'right' : 'wrong'}`, 'replay');
if (st === 'Paid' && !resolved) cere(corect /*PAZA:replay*/, 'the provider was paid for a result the replay does not reproduce (no one disputed in time)', 'replay');
} catch (e) { cere(false, 'the replay could not run: ' + e.message, 'replay'); }
}
// R7 the provider's envelope names this result and existed before the window closed
if (d.envelope) {
const e = d.envelope, stmt = e.statement || {};
const okE = e.kind === 'aere-proof-of-execution-attestation' && low(stmt.outputHash) === low(j.resultHash) && low(stmt.inputHash) === low(j.specHash) && sha256(Buffer.from(JSON.stringify(stmt), 'utf8')) === low(e.statementHash) /*PAZA:dovada*/;
ok(motive, okE, 'the execution envelope does not name this job\'s specHash/resultHash, or its statementHash is not its statement\'s');
if (e.notarization && submitted) ok(motive, Number(e.notarization.firstSeen) <= j.submittedAt + j.challengeWindow /*PAZA:dovada*/, 'the envelope was notarized only after the challenge window closed');
cere(okE, 'the execution envelope does not name this job\'s specHash/resultHash, or its statementHash is not its statement\'s', 'dovada');
if (e.notarization && submitted) cere(Number(e.notarization.firstSeen) <= j.submittedAt + j.challengeWindow /*PAZA:dovada*/, 'the envelope was notarized only after the challenge window closed', 'dovada');
}
const terminalStatus = ['Paid', 'Refunded', 'Slashed'].includes(st);
return { verdict: motive.length ? 'INVALID' : terminalStatus ? 'VALID' : 'INCOMPLETE', motive, randuri, status: st, terminal, replay };
return { verdict: motive.length ? 'INVALID' : terminalStatus ? 'VALID' : 'INCOMPLETE', motive, reguli: [...reguli].sort(), randuri, status: st, terminal, replay };
}
/** The AIP-23 settlement receipt of a terminal job: the payout transaction, the parties, the amount, the instruction (market, job, status). */
export function chitanta(d, { createdAt = new Date().toISOString() } = {}) {