Skip to content

Commit

Permalink
fix: serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Nov 27, 2024
1 parent 38cd368 commit bb5ff3c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type ProvingJobProducer, ProvingRequestType, makePublicInputsAndRecursiveProof } from '@aztec/circuit-types';
import { RECURSIVE_PROOF_LENGTH, VerificationKeyData, makeRecursiveProof } from '@aztec/circuits.js';
import { makeBaseParityInputs, makeParityPublicInputs } from '@aztec/circuits.js/testing';
import { AbortError } from '@aztec/foundation/error';
import { promiseWithResolvers } from '@aztec/foundation/promise';

import { jest } from '@jest/globals';
Expand Down Expand Up @@ -54,7 +53,6 @@ describe('CachingBrokerFacade', () => {
await jest.advanceTimersToNextTimerAsync();

const job = broker.enqueueProvingJob.mock.calls[0][0];
await expect(cache.getProvingJobStatus(job.id)).resolves.toEqual({ status: 'in-queue' });

reject(new Error('Failed to enqueue job'));

Expand All @@ -63,9 +61,6 @@ describe('CachingBrokerFacade', () => {
});

it('awaits existing job if in progress', async () => {
const { promise, reject } = promiseWithResolvers<any>();
broker.enqueueProvingJob.mockResolvedValue(promise);

const inputs = makeBaseParityInputs();
void facade.getBaseParityProof(inputs).catch(() => {});
await jest.advanceTimersToNextTimerAsync();
Expand All @@ -74,8 +69,6 @@ describe('CachingBrokerFacade', () => {
void facade.getBaseParityProof(inputs).catch(() => {});
await jest.advanceTimersToNextTimerAsync();
expect(broker.enqueueProvingJob).toHaveBeenCalledTimes(1);

reject(new AbortError('Job was cancelled'));
});

it('reuses already cached results', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class CachingBrokerFacade implements ServerCircuitProver {
let jobEnqueued = false;
try {
const cachedResult = await this.cache.getProvingJobStatus(id);
if (cachedResult.status !== 'not-found') {
this.log.debug(`Found cached result for job=${id}: status=${cachedResult.status}`);
}

if (cachedResult.status === 'fulfilled') {
const output = await this.proofStore.getProofOutput(cachedResult.value);
if (output.type === type) {
Expand Down Expand Up @@ -94,6 +98,7 @@ export class CachingBrokerFacade implements ServerCircuitProver {
});
await this.cache.setProvingJobStatus(id, { status: 'in-queue' });
} catch (err) {
this.log.error(`Failed to enqueue proving job id=${id}: ${err}`);
await this.cache.setProvingJobStatus(id, { status: 'not-found' });
throw err;
}
Expand Down
12 changes: 7 additions & 5 deletions yarn-project/prover-client/src/proving_broker/proof_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
type ProvingJobResultsMap,
type ProvingRequestType,
} from '@aztec/circuit-types';
import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';
import { type ZodFor } from '@aztec/foundation/schemas';

/**
* A database for storing proof inputs and outputs.
Expand Down Expand Up @@ -81,24 +83,24 @@ export class InlineProofStore implements ProofStore {
}

getProofInput(uri: ProofUri): Promise<ProvingJobInputs> {
return Promise.resolve(ProvingJobInputs.parse(this.decode(uri)));
return Promise.resolve(this.decode(uri, ProvingJobInputs));
}

getProofOutput(uri: ProofUri): Promise<ProvingJobResult> {
return Promise.resolve(ProvingJobResult.parse(this.decode(uri)));
return Promise.resolve(this.decode(uri, ProvingJobResult));
}

private encode(obj: object): ProofUri {
const encoded = encodeURIComponent(JSON.stringify(obj));
const encoded = encodeURIComponent(jsonStringify(obj));
return (PREFIX + SEPARATOR + encoded) as ProofUri;
}

private decode(uri: ProofUri): object {
private decode<T>(uri: ProofUri, schema: ZodFor<T>): T {
const [prefix, data] = uri.split(SEPARATOR);
if (prefix !== PREFIX) {
throw new Error('Invalid proof input URI: ' + prefix);
}

return JSON.parse(decodeURIComponent(data));
return jsonParseWithSchema(decodeURIComponent(data), schema);
}
}

0 comments on commit bb5ff3c

Please sign in to comment.