Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Nov 25, 2024
1 parent f958d82 commit 8e2fa60
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion yarn-project/prover-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export { EpochProverManager } from '@aztec/circuit-types';
export * from './tx-prover/tx-prover.js';
export * from './config.js';
export * from './tx-prover/factory.js';
export * from './orchestrator/orchestrator_cache.js';
export * from './proving_broker/prover_cache/memory.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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';
import { type MockProxy, mock } from 'jest-mock-extended';

import { CachingBrokerFacade } from './caching_broker_facade.js';
import { InlineProofStore } from './proof_store.js';
import { InMemoryProverCache } from './prover_cache/memory.js';

describe('CachingBrokerFacade', () => {
let facade: CachingBrokerFacade;
let cache: InMemoryProverCache;
let proofStore: InlineProofStore;
let broker: MockProxy<ProvingJobProducer>;

beforeAll(() => {
jest.useFakeTimers();
});

beforeEach(() => {
broker = mock<ProvingJobProducer>({
enqueueProvingJob: jest.fn<any>(),
getProvingJobStatus: jest.fn<any>(),
removeAndCancelProvingJob: jest.fn<any>(),
waitForJobToSettle: jest.fn<any>(),
});
cache = new InMemoryProverCache();
proofStore = new InlineProofStore();
facade = new CachingBrokerFacade(broker, cache, proofStore);
});

it('marks job as in progress', async () => {
const controller = new AbortController();
void facade.getBaseParityProof(makeBaseParityInputs(), controller.signal);

await jest.advanceTimersToNextTimerAsync();

expect(broker.enqueueProvingJob).toHaveBeenCalled();
const job = broker.enqueueProvingJob.mock.calls[0][0];

await expect(cache.getProvingJobStatus(job.id)).resolves.toEqual({ status: 'in-queue' });
controller.abort();
});

it('removes the cached value if a job fails to enqueue', async () => {
const { promise, reject } = promiseWithResolvers<any>();
broker.enqueueProvingJob.mockResolvedValue(promise);

void facade.getBaseParityProof(makeBaseParityInputs()).catch(() => {});
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'));

await jest.advanceTimersToNextTimerAsync();
await expect(cache.getProvingJobStatus(job.id)).resolves.toEqual({ status: 'not-found' });
});

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();
expect(broker.enqueueProvingJob).toHaveBeenCalledTimes(1);

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 () => {
const { promise, resolve } = promiseWithResolvers<any>();
broker.enqueueProvingJob.mockResolvedValue(Promise.resolve());
broker.waitForJobToSettle.mockResolvedValue(promise);

const inputs = makeBaseParityInputs();
void facade.getBaseParityProof(inputs);
await jest.advanceTimersToNextTimerAsync();

expect(broker.enqueueProvingJob).toHaveBeenCalledTimes(1);
const job = broker.enqueueProvingJob.mock.calls[0][0];

const result = makePublicInputsAndRecursiveProof(
makeParityPublicInputs(),
makeRecursiveProof(RECURSIVE_PROOF_LENGTH),
VerificationKeyData.makeFakeHonk(),
);

const outputUri = await proofStore.saveProofOutput(job.id, ProvingRequestType.BASE_PARITY, result);
resolve({
status: 'fulfilled',
value: outputUri,
});

await jest.advanceTimersToNextTimerAsync();
await expect(cache.getProvingJobStatus(job.id)).resolves.toEqual({ status: 'fulfilled', value: outputUri });

await expect(facade.getBaseParityProof(inputs)).resolves.toEqual(result);
expect(broker.enqueueProvingJob).toHaveBeenCalledTimes(1); // job was only ever enqueued once
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { sha256 } from '@aztec/foundation/crypto';
import { createDebugLogger } from '@aztec/foundation/log';
import { retryUntil } from '@aztec/foundation/retry';

import { InMemoryProverCache } from '../orchestrator/orchestrator_cache.js';
import { InlineProofStore, type ProofStore } from './proof_store.js';
import { InMemoryProverCache } from './prover_cache/memory.js';

/**
* A facade around a job broker that generates stable job ids and caches results
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/prover-client/src/tx-prover/tx-prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { join } from 'path';

import { type ProverClientConfig } from '../config.js';
import { ProvingOrchestrator } from '../orchestrator/orchestrator.js';
import { InMemoryProverCache } from '../orchestrator/orchestrator_cache.js';
import { CachingBrokerFacade } from '../proving_broker/caching_broker_facade.js';
import { InlineProofStore } from '../proving_broker/proof_store.js';
import { InMemoryProverCache } from '../proving_broker/prover_cache/memory.js';
import { ProvingAgent } from '../proving_broker/proving_agent.js';

/**
Expand Down

0 comments on commit 8e2fa60

Please sign in to comment.