@aztec/prover-client 0.0.0-test.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/README.md +1 -0
  2. package/dest/bin/get-proof-inputs.d.ts +2 -0
  3. package/dest/bin/get-proof-inputs.d.ts.map +1 -0
  4. package/dest/bin/get-proof-inputs.js +51 -0
  5. package/dest/block_builder/index.d.ts +6 -0
  6. package/dest/block_builder/index.d.ts.map +1 -0
  7. package/dest/block_builder/index.js +1 -0
  8. package/dest/block_builder/light.d.ts +33 -0
  9. package/dest/block_builder/light.d.ts.map +1 -0
  10. package/dest/block_builder/light.js +82 -0
  11. package/dest/config.d.ts +17 -0
  12. package/dest/config.d.ts.map +1 -0
  13. package/dest/config.js +39 -0
  14. package/dest/index.d.ts +4 -0
  15. package/dest/index.d.ts.map +1 -0
  16. package/dest/index.js +2 -0
  17. package/dest/mocks/fixtures.d.ts +20 -0
  18. package/dest/mocks/fixtures.d.ts.map +1 -0
  19. package/dest/mocks/fixtures.js +77 -0
  20. package/dest/mocks/test_context.d.ts +55 -0
  21. package/dest/mocks/test_context.d.ts.map +1 -0
  22. package/dest/mocks/test_context.js +193 -0
  23. package/dest/orchestrator/block-building-helpers.d.ts +55 -0
  24. package/dest/orchestrator/block-building-helpers.d.ts.map +1 -0
  25. package/dest/orchestrator/block-building-helpers.js +285 -0
  26. package/dest/orchestrator/block-proving-state.d.ts +76 -0
  27. package/dest/orchestrator/block-proving-state.d.ts.map +1 -0
  28. package/dest/orchestrator/block-proving-state.js +269 -0
  29. package/dest/orchestrator/epoch-proving-state.d.ts +60 -0
  30. package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -0
  31. package/dest/orchestrator/epoch-proving-state.js +163 -0
  32. package/dest/orchestrator/index.d.ts +2 -0
  33. package/dest/orchestrator/index.d.ts.map +1 -0
  34. package/dest/orchestrator/index.js +1 -0
  35. package/dest/orchestrator/orchestrator.d.ts +110 -0
  36. package/dest/orchestrator/orchestrator.d.ts.map +1 -0
  37. package/dest/orchestrator/orchestrator.js +690 -0
  38. package/dest/orchestrator/orchestrator_metrics.d.ts +8 -0
  39. package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -0
  40. package/dest/orchestrator/orchestrator_metrics.js +17 -0
  41. package/dest/orchestrator/tx-proving-state.d.ts +34 -0
  42. package/dest/orchestrator/tx-proving-state.d.ts.map +1 -0
  43. package/dest/orchestrator/tx-proving-state.js +94 -0
  44. package/dest/prover-client/factory.d.ts +6 -0
  45. package/dest/prover-client/factory.d.ts.map +1 -0
  46. package/dest/prover-client/factory.js +5 -0
  47. package/dest/prover-client/index.d.ts +3 -0
  48. package/dest/prover-client/index.d.ts.map +1 -0
  49. package/dest/prover-client/index.js +2 -0
  50. package/dest/prover-client/prover-client.d.ts +42 -0
  51. package/dest/prover-client/prover-client.d.ts.map +1 -0
  52. package/dest/prover-client/prover-client.js +110 -0
  53. package/dest/prover-client/server-epoch-prover.d.ts +28 -0
  54. package/dest/prover-client/server-epoch-prover.d.ts.map +1 -0
  55. package/dest/prover-client/server-epoch-prover.js +40 -0
  56. package/dest/proving_broker/broker_prover_facade.d.ts +46 -0
  57. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -0
  58. package/dest/proving_broker/broker_prover_facade.js +344 -0
  59. package/dest/proving_broker/config.d.ts +83 -0
  60. package/dest/proving_broker/config.d.ts.map +1 -0
  61. package/dest/proving_broker/config.js +104 -0
  62. package/dest/proving_broker/factory.d.ts +5 -0
  63. package/dest/proving_broker/factory.d.ts.map +1 -0
  64. package/dest/proving_broker/factory.js +9 -0
  65. package/dest/proving_broker/fixtures.d.ts +5 -0
  66. package/dest/proving_broker/fixtures.d.ts.map +1 -0
  67. package/dest/proving_broker/fixtures.js +12 -0
  68. package/dest/proving_broker/index.d.ts +10 -0
  69. package/dest/proving_broker/index.d.ts.map +1 -0
  70. package/dest/proving_broker/index.js +9 -0
  71. package/dest/proving_broker/proof_store/factory.d.ts +6 -0
  72. package/dest/proving_broker/proof_store/factory.d.ts.map +1 -0
  73. package/dest/proving_broker/proof_store/factory.js +36 -0
  74. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts +14 -0
  75. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts.map +1 -0
  76. package/dest/proving_broker/proof_store/gcs_proof_store.js +51 -0
  77. package/dest/proving_broker/proof_store/index.d.ts +4 -0
  78. package/dest/proving_broker/proof_store/index.d.ts.map +1 -0
  79. package/dest/proving_broker/proof_store/index.js +3 -0
  80. package/dest/proving_broker/proof_store/inline_proof_store.d.ts +15 -0
  81. package/dest/proving_broker/proof_store/inline_proof_store.d.ts.map +1 -0
  82. package/dest/proving_broker/proof_store/inline_proof_store.js +41 -0
  83. package/dest/proving_broker/proof_store/proof_store.d.ts +36 -0
  84. package/dest/proving_broker/proof_store/proof_store.d.ts.map +1 -0
  85. package/dest/proving_broker/proof_store/proof_store.js +3 -0
  86. package/dest/proving_broker/proving_agent.d.ts +46 -0
  87. package/dest/proving_broker/proving_agent.d.ts.map +1 -0
  88. package/dest/proving_broker/proving_agent.js +134 -0
  89. package/dest/proving_broker/proving_agent_instrumentation.d.ts +8 -0
  90. package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +1 -0
  91. package/dest/proving_broker/proving_agent_instrumentation.js +16 -0
  92. package/dest/proving_broker/proving_broker.d.ts +64 -0
  93. package/dest/proving_broker/proving_broker.d.ts.map +1 -0
  94. package/dest/proving_broker/proving_broker.js +570 -0
  95. package/dest/proving_broker/proving_broker_database/memory.d.ts +16 -0
  96. package/dest/proving_broker/proving_broker_database/memory.d.ts.map +1 -0
  97. package/dest/proving_broker/proving_broker_database/memory.js +54 -0
  98. package/dest/proving_broker/proving_broker_database/persisted.d.ts +25 -0
  99. package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -0
  100. package/dest/proving_broker/proving_broker_database/persisted.js +182 -0
  101. package/dest/proving_broker/proving_broker_database.d.ts +39 -0
  102. package/dest/proving_broker/proving_broker_database.d.ts.map +1 -0
  103. package/dest/proving_broker/proving_broker_database.js +3 -0
  104. package/dest/proving_broker/proving_broker_instrumentation.d.ts +29 -0
  105. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -0
  106. package/dest/proving_broker/proving_broker_instrumentation.js +110 -0
  107. package/dest/proving_broker/proving_job_controller.d.ts +33 -0
  108. package/dest/proving_broker/proving_job_controller.d.ts.map +1 -0
  109. package/dest/proving_broker/proving_job_controller.js +166 -0
  110. package/dest/proving_broker/rpc.d.ts +27 -0
  111. package/dest/proving_broker/rpc.d.ts.map +1 -0
  112. package/dest/proving_broker/rpc.js +66 -0
  113. package/dest/test/mock_prover.d.ts +35 -0
  114. package/dest/test/mock_prover.d.ts.map +1 -0
  115. package/dest/test/mock_prover.js +82 -0
  116. package/package.json +112 -0
  117. package/src/bin/get-proof-inputs.ts +59 -0
  118. package/src/block_builder/index.ts +6 -0
  119. package/src/block_builder/light.ts +101 -0
  120. package/src/config.ts +55 -0
  121. package/src/index.ts +4 -0
  122. package/src/mocks/fixtures.ts +117 -0
  123. package/src/mocks/test_context.ts +257 -0
  124. package/src/orchestrator/block-building-helpers.ts +553 -0
  125. package/src/orchestrator/block-proving-state.ts +379 -0
  126. package/src/orchestrator/epoch-proving-state.ts +252 -0
  127. package/src/orchestrator/index.ts +1 -0
  128. package/src/orchestrator/orchestrator.ts +971 -0
  129. package/src/orchestrator/orchestrator_metrics.ts +22 -0
  130. package/src/orchestrator/tx-proving-state.ts +139 -0
  131. package/src/prover-client/factory.ts +14 -0
  132. package/src/prover-client/index.ts +2 -0
  133. package/src/prover-client/prover-client.ts +162 -0
  134. package/src/prover-client/server-epoch-prover.ts +51 -0
  135. package/src/proving_broker/broker_prover_facade.ts +585 -0
  136. package/src/proving_broker/config.ts +138 -0
  137. package/src/proving_broker/factory.ts +18 -0
  138. package/src/proving_broker/fixtures.ts +15 -0
  139. package/src/proving_broker/index.ts +9 -0
  140. package/src/proving_broker/proof_store/factory.ts +42 -0
  141. package/src/proving_broker/proof_store/gcs_proof_store.ts +72 -0
  142. package/src/proving_broker/proof_store/index.ts +3 -0
  143. package/src/proving_broker/proof_store/inline_proof_store.ts +63 -0
  144. package/src/proving_broker/proof_store/proof_store.ts +54 -0
  145. package/src/proving_broker/proving_agent.ts +181 -0
  146. package/src/proving_broker/proving_agent_instrumentation.ts +21 -0
  147. package/src/proving_broker/proving_broker.ts +687 -0
  148. package/src/proving_broker/proving_broker_database/memory.ts +63 -0
  149. package/src/proving_broker/proving_broker_database/persisted.ts +218 -0
  150. package/src/proving_broker/proving_broker_database.ts +44 -0
  151. package/src/proving_broker/proving_broker_instrumentation.ts +145 -0
  152. package/src/proving_broker/proving_job_controller.ts +194 -0
  153. package/src/proving_broker/rpc.ts +95 -0
  154. package/src/test/mock_prover.ts +253 -0
@@ -0,0 +1,138 @@
1
+ import { type L1ReaderConfig, l1ReaderConfigMappings } from '@aztec/ethereum';
2
+ import {
3
+ type ConfigMappingsType,
4
+ booleanConfigHelper,
5
+ getDefaultConfig,
6
+ numberConfigHelper,
7
+ } from '@aztec/foundation/config';
8
+ import { type DataStoreConfig, dataConfigMappings } from '@aztec/kv-store/config';
9
+ import { ProvingRequestType } from '@aztec/stdlib/proofs';
10
+
11
+ import { z } from 'zod';
12
+
13
+ export const ProverBrokerConfig = z.object({
14
+ /** If starting a prover broker locally, the max number of retries per proving job */
15
+ proverBrokerJobMaxRetries: z.number().int().nonnegative(),
16
+ /** If starting a prover broker locally, the time after which a job times out and gets assigned to a different agent */
17
+ proverBrokerJobTimeoutMs: z.number().int().nonnegative(),
18
+ /** If starting a prover broker locally, the interval the broker checks for timed out jobs */
19
+ proverBrokerPollIntervalMs: z.number().int().nonnegative(),
20
+ /** If starting a prover broker locally, the directory to store broker data */
21
+ dataDirectory: z.string().optional(),
22
+ /** The size of the data store map */
23
+ dataStoreMapSizeKB: z.number().int().nonnegative(),
24
+ /** The prover broker may batch jobs together before writing to the database */
25
+ proverBrokerBatchSize: z.number().int().nonnegative(),
26
+ /** How often the job batches get flushed */
27
+ proverBrokerBatchIntervalMs: z.number().int().nonnegative(),
28
+ /** The maximum number of epochs to keep results for */
29
+ proverBrokerMaxEpochsToKeepResultsFor: z.number().int().nonnegative(),
30
+ });
31
+
32
+ export type ProverBrokerConfig = z.infer<typeof ProverBrokerConfig> &
33
+ Pick<DataStoreConfig, 'dataStoreMapSizeKB' | 'dataDirectory'> &
34
+ L1ReaderConfig;
35
+
36
+ export const proverBrokerConfigMappings: ConfigMappingsType<ProverBrokerConfig> = {
37
+ proverBrokerJobTimeoutMs: {
38
+ env: 'PROVER_BROKER_JOB_TIMEOUT_MS',
39
+ description: 'Jobs are retried if not kept alive for this long',
40
+ ...numberConfigHelper(30_000),
41
+ },
42
+ proverBrokerPollIntervalMs: {
43
+ env: 'PROVER_BROKER_POLL_INTERVAL_MS',
44
+ description: 'The interval to check job health status',
45
+ ...numberConfigHelper(1_000),
46
+ },
47
+ proverBrokerJobMaxRetries: {
48
+ env: 'PROVER_BROKER_JOB_MAX_RETRIES',
49
+ description: 'If starting a prover broker locally, the max number of retries per proving job',
50
+ ...numberConfigHelper(3),
51
+ },
52
+ proverBrokerBatchSize: {
53
+ env: 'PROVER_BROKER_BATCH_SIZE',
54
+ description: 'The prover broker writes jobs to disk in batches',
55
+ ...numberConfigHelper(100),
56
+ },
57
+ proverBrokerBatchIntervalMs: {
58
+ env: 'PROVER_BROKER_BATCH_INTERVAL_MS',
59
+ description: 'How often to flush batches to disk',
60
+ ...numberConfigHelper(50),
61
+ },
62
+ proverBrokerMaxEpochsToKeepResultsFor: {
63
+ env: 'PROVER_BROKER_MAX_EPOCHS_TO_KEEP_RESULTS_FOR',
64
+ description: 'The maximum number of epochs to keep results for',
65
+ ...numberConfigHelper(1),
66
+ },
67
+ ...l1ReaderConfigMappings,
68
+ ...dataConfigMappings,
69
+ };
70
+
71
+ export const defaultProverBrokerConfig: ProverBrokerConfig = getDefaultConfig(proverBrokerConfigMappings);
72
+
73
+ export const ProverAgentConfig = z.object({
74
+ /** The number of prover agents to start */
75
+ proverAgentCount: z.number(),
76
+ /** The types of proofs the prover agent can generate */
77
+ proverAgentProofTypes: z.array(z.nativeEnum(ProvingRequestType)),
78
+ /** How often the prover agents poll for jobs */
79
+ proverAgentPollIntervalMs: z.number(),
80
+ /** The URL where this agent takes jobs from */
81
+ proverBrokerUrl: z.string().optional(),
82
+ /** Whether to construct real proofs */
83
+ realProofs: z.boolean(),
84
+ /** The type of artificial delay to introduce */
85
+ proverTestDelayType: z.enum(['fixed', 'realistic']),
86
+ /** If using fixed delay, the time each operation takes. */
87
+ proverTestDelayMs: z.number(),
88
+ /** If using realistic delays, what percentage of realistic times to apply. */
89
+ proverTestDelayFactor: z.number(),
90
+ });
91
+
92
+ export type ProverAgentConfig = z.infer<typeof ProverAgentConfig>;
93
+
94
+ export const proverAgentConfigMappings: ConfigMappingsType<ProverAgentConfig> = {
95
+ proverAgentCount: {
96
+ env: 'PROVER_AGENT_COUNT',
97
+ description: 'Whether this prover has a local prover agent',
98
+ ...numberConfigHelper(1),
99
+ },
100
+ proverAgentPollIntervalMs: {
101
+ env: 'PROVER_AGENT_POLL_INTERVAL_MS',
102
+ description: 'The interval agents poll for jobs at',
103
+ ...numberConfigHelper(100),
104
+ },
105
+ proverAgentProofTypes: {
106
+ env: 'PROVER_AGENT_PROOF_TYPES',
107
+ description: 'The types of proofs the prover agent can generate',
108
+ parseEnv: (val: string) =>
109
+ val
110
+ .split(',')
111
+ .map(v => ProvingRequestType[v as any])
112
+ .filter(v => typeof v === 'number'),
113
+ },
114
+ proverBrokerUrl: {
115
+ env: 'PROVER_BROKER_HOST',
116
+ description: 'The URL where this agent takes jobs from',
117
+ },
118
+ realProofs: {
119
+ env: 'PROVER_REAL_PROOFS',
120
+ description: 'Whether to construct real proofs',
121
+ ...booleanConfigHelper(false),
122
+ },
123
+ proverTestDelayType: {
124
+ env: 'PROVER_TEST_DELAY_TYPE',
125
+ description: 'The type of artificial delay to introduce',
126
+ defaultValue: 'fixed',
127
+ },
128
+ proverTestDelayMs: {
129
+ env: 'PROVER_TEST_DELAY_MS',
130
+ description: 'Artificial delay to introduce to all operations to the test prover.',
131
+ ...numberConfigHelper(0),
132
+ },
133
+ proverTestDelayFactor: {
134
+ env: 'PROVER_TEST_DELAY_FACTOR',
135
+ description: 'If using realistic delays, what percentage of realistic times to apply.',
136
+ ...numberConfigHelper(1),
137
+ },
138
+ };
@@ -0,0 +1,18 @@
1
+ import type { TelemetryClient } from '@aztec/telemetry-client';
2
+
3
+ import type { ProverBrokerConfig } from './config.js';
4
+ import { ProvingBroker } from './proving_broker.js';
5
+ import { InMemoryBrokerDatabase } from './proving_broker_database/memory.js';
6
+ import { KVBrokerDatabase } from './proving_broker_database/persisted.js';
7
+
8
+ export async function createAndStartProvingBroker(
9
+ config: ProverBrokerConfig,
10
+ client: TelemetryClient,
11
+ ): Promise<ProvingBroker> {
12
+ const database = config.dataDirectory ? await KVBrokerDatabase.new(config, client) : new InMemoryBrokerDatabase();
13
+
14
+ const broker = new ProvingBroker(database, config, client);
15
+
16
+ await broker.start();
17
+ return broker;
18
+ }
@@ -0,0 +1,15 @@
1
+ import { randomBytes } from '@aztec/foundation/crypto';
2
+ import { type ProofUri, type ProvingJobId, makeProvingJobId } from '@aztec/stdlib/interfaces/server';
3
+ import { ProvingRequestType } from '@aztec/stdlib/proofs';
4
+
5
+ export function makeRandomProvingJobId(epochNumber?: number): ProvingJobId {
6
+ return makeProvingJobId(epochNumber ?? 1, ProvingRequestType.BASE_PARITY, randomBytes(8).toString('hex'));
7
+ }
8
+
9
+ export function makeInputsUri(): ProofUri {
10
+ return randomBytes(8).toString('hex') as ProofUri;
11
+ }
12
+
13
+ export function makeOutputsUri(): ProofUri {
14
+ return randomBytes(8).toString('hex') as ProofUri;
15
+ }
@@ -0,0 +1,9 @@
1
+ export * from './proving_agent.js';
2
+ export * from './proving_broker.js';
3
+ export * from './rpc.js';
4
+ export * from './proving_broker_database.js';
5
+ export * from './proving_broker_database/memory.js';
6
+ export * from './proving_broker_database/persisted.js';
7
+ export * from './proof_store/index.js';
8
+ export * from './factory.js';
9
+ export * from './config.js';
@@ -0,0 +1,42 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+
3
+ import { GoogleCloudStorageProofStore } from './gcs_proof_store.js';
4
+ import { InlineProofStore } from './inline_proof_store.js';
5
+ import type { ProofStore } from './proof_store.js';
6
+
7
+ export function createProofStore(config: string | undefined, logger = createLogger('prover-client:proof-store')) {
8
+ if (config === undefined) {
9
+ logger.info('Creating inline proof store');
10
+ return new InlineProofStore();
11
+ } else if (config.startsWith('gs://')) {
12
+ try {
13
+ const url = new URL(config);
14
+ const bucket = url.host;
15
+ const path = url.pathname.replace(/^\/+/, '');
16
+ logger.info(`Creating google cloud proof store at ${bucket}`, { bucket, path });
17
+ return new GoogleCloudStorageProofStore(bucket, path);
18
+ } catch (err) {
19
+ throw new Error(
20
+ `Invalid google cloud proof store definition: '${config}'. Supported values are 'gs://bucket-name/path/to/store'.`,
21
+ );
22
+ }
23
+ } else {
24
+ throw new Error(`Unknown proof store config: '${config}'. Supported values are 'gs://bucket-name/path/to/store'.`);
25
+ }
26
+ }
27
+
28
+ export function createProofStoreForUri(
29
+ uri: string,
30
+ logger = createLogger('prover-client:proof-store'),
31
+ ): Pick<ProofStore, 'getProofInput' | 'getProofOutput'> {
32
+ if (uri.startsWith('data://')) {
33
+ return createProofStore(undefined, logger);
34
+ } else if (uri.startsWith('gs://')) {
35
+ const url = new URL(uri);
36
+ const basePath = url.pathname.replace(/^\/+/, '').split('/').slice(0, -3);
37
+ url.pathname = basePath.join('/');
38
+ return createProofStore(uri, logger);
39
+ } else {
40
+ throw new Error(`Unknown proof store config: '${uri}'. Supported protocols are 'data://' and 'gs://'.`);
41
+ }
42
+ }
@@ -0,0 +1,72 @@
1
+ import {
2
+ type ProofUri,
3
+ type ProvingJobId,
4
+ type ProvingJobInputs,
5
+ type ProvingJobInputsMap,
6
+ type ProvingJobResult,
7
+ type ProvingJobResultsMap,
8
+ getProvingJobInputClassFor,
9
+ } from '@aztec/stdlib/interfaces/server';
10
+ import { ProvingRequestType } from '@aztec/stdlib/proofs';
11
+
12
+ import { Storage } from '@google-cloud/storage';
13
+ import { join } from 'path';
14
+
15
+ import type { ProofStore } from './proof_store.js';
16
+
17
+ const INPUTS_PATH = 'inputs';
18
+
19
+ export class GoogleCloudStorageProofStore implements ProofStore {
20
+ private readonly storage: Storage;
21
+
22
+ constructor(private readonly bucketName: string, private readonly path: string) {
23
+ this.storage = new Storage();
24
+ }
25
+
26
+ public async saveProofInput<T extends ProvingRequestType>(
27
+ id: ProvingJobId,
28
+ type: T,
29
+ inputs: ProvingJobInputsMap[T],
30
+ ): Promise<ProofUri> {
31
+ const path = join(this.path, INPUTS_PATH, ProvingRequestType[type], id);
32
+ const file = this.storage.bucket(this.bucketName).file(path);
33
+ await file.save(inputs.toBuffer());
34
+ return file.cloudStorageURI.toString() as ProofUri;
35
+ }
36
+
37
+ saveProofOutput<T extends ProvingRequestType>(
38
+ _id: ProvingJobId,
39
+ _type: T,
40
+ _result: ProvingJobResultsMap[T],
41
+ ): Promise<ProofUri> {
42
+ throw new Error('Not implemented');
43
+ }
44
+
45
+ public async getProofInput(uri: ProofUri): Promise<ProvingJobInputs> {
46
+ try {
47
+ const url = new URL(uri);
48
+ const bucket = this.storage.bucket(url.host);
49
+ const path = url.pathname.replace(/^\/+/, '');
50
+ const file = bucket.file(path);
51
+ if (!(await file.exists())) {
52
+ throw new Error(`File at ${uri} does not exist`);
53
+ }
54
+
55
+ const typeString = path.split('/').at(-2);
56
+ const type = typeString ? ProvingRequestType[typeString as keyof typeof ProvingRequestType] : undefined;
57
+ if (type === undefined) {
58
+ throw new Error(`Unrecognized proof type ${type} in path ${path}`);
59
+ }
60
+
61
+ const contents = await file.download();
62
+ const inputs = getProvingJobInputClassFor(type).fromBuffer(contents[0]);
63
+ return { inputs, type } as ProvingJobInputs;
64
+ } catch (err) {
65
+ throw new Error(`Error getting proof input at ${uri}: ${err}`);
66
+ }
67
+ }
68
+
69
+ getProofOutput(_uri: ProofUri): Promise<ProvingJobResult> {
70
+ throw new Error('Not implemented');
71
+ }
72
+ }
@@ -0,0 +1,3 @@
1
+ export * from './proof_store.js';
2
+ export * from './inline_proof_store.js';
3
+ export * from './factory.js';
@@ -0,0 +1,63 @@
1
+ import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';
2
+ import {
3
+ type ProofUri,
4
+ type ProvingJobId,
5
+ ProvingJobInputs,
6
+ type ProvingJobInputsMap,
7
+ ProvingJobResult,
8
+ type ProvingJobResultsMap,
9
+ } from '@aztec/stdlib/interfaces/server';
10
+ import type { ProvingRequestType } from '@aztec/stdlib/proofs';
11
+ import type { ZodFor } from '@aztec/stdlib/schemas';
12
+
13
+ import type { ProofStore } from './proof_store.js';
14
+
15
+ // use an ASCII encoded data uri https://datatracker.ietf.org/doc/html/rfc2397#section-2
16
+ // we do this to avoid double encoding to base64 (since the inputs already serialize to a base64 string)
17
+ const PREFIX = 'data:application/json;charset=utf-8';
18
+ const SEPARATOR = ',';
19
+
20
+ /**
21
+ * An implementation of a proof input/output database that stores data inline in the URI.
22
+ */
23
+ export class InlineProofStore implements ProofStore {
24
+ saveProofInput<T extends ProvingRequestType>(
25
+ _id: ProvingJobId,
26
+ type: T,
27
+ inputs: ProvingJobInputsMap[T],
28
+ ): Promise<ProofUri> {
29
+ const jobInputs = { type, inputs } as ProvingJobInputs;
30
+ return Promise.resolve(this.encode(jobInputs));
31
+ }
32
+
33
+ saveProofOutput<T extends ProvingRequestType>(
34
+ _id: ProvingJobId,
35
+ type: T,
36
+ result: ProvingJobResultsMap[T],
37
+ ): Promise<ProofUri> {
38
+ const jobResult = { type, result } as ProvingJobResult;
39
+ return Promise.resolve(this.encode(jobResult));
40
+ }
41
+
42
+ getProofInput(uri: ProofUri): Promise<ProvingJobInputs> {
43
+ return Promise.resolve(this.decode(uri, ProvingJobInputs));
44
+ }
45
+
46
+ getProofOutput(uri: ProofUri): Promise<ProvingJobResult> {
47
+ return Promise.resolve(this.decode(uri, ProvingJobResult));
48
+ }
49
+
50
+ private encode(obj: object): ProofUri {
51
+ const encoded = encodeURIComponent(jsonStringify(obj));
52
+ return (PREFIX + SEPARATOR + encoded) as ProofUri;
53
+ }
54
+
55
+ private decode<T>(uri: ProofUri, schema: ZodFor<T>): Promise<T> {
56
+ const [prefix, data] = uri.split(SEPARATOR);
57
+ if (prefix !== PREFIX) {
58
+ throw new Error('Invalid proof input URI: ' + prefix);
59
+ }
60
+
61
+ return jsonParseWithSchema(decodeURIComponent(data), schema);
62
+ }
63
+ }
@@ -0,0 +1,54 @@
1
+ import type {
2
+ ProofUri,
3
+ ProvingJobId,
4
+ ProvingJobInputs,
5
+ ProvingJobInputsMap,
6
+ ProvingJobResult,
7
+ ProvingJobResultsMap,
8
+ } from '@aztec/stdlib/interfaces/server';
9
+ import type { ProvingRequestType } from '@aztec/stdlib/proofs';
10
+
11
+ /**
12
+ * A database for storing proof inputs and outputs.
13
+ */
14
+ export interface ProofStore {
15
+ /**
16
+ * Save a proof input to the database.
17
+ * @param jobId - The ID of the job the proof input is associated with.
18
+ * @param type - The type of the proving request.
19
+ * @param inputs - The proof input to save.
20
+ * @returns The URI of the saved proof input.
21
+ */
22
+ saveProofInput<T extends ProvingRequestType>(
23
+ jobId: ProvingJobId,
24
+ type: T,
25
+ inputs: ProvingJobInputsMap[T],
26
+ ): Promise<ProofUri>;
27
+
28
+ /**
29
+ * Save a proof output to the database.
30
+ * @param jobId - The ID of the job the proof input is associated with.
31
+ * @param type - The type of the proving request.
32
+ * @param result - The proof output to save.
33
+ * @returns The URI of the saved proof output.
34
+ */
35
+ saveProofOutput<T extends ProvingRequestType>(
36
+ id: ProvingJobId,
37
+ type: T,
38
+ result: ProvingJobResultsMap[T],
39
+ ): Promise<ProofUri>;
40
+
41
+ /**
42
+ * Retrieve a proof input from the database.
43
+ * @param uri - The URI of the proof input to retrieve.
44
+ * @returns The proof input.
45
+ */
46
+ getProofInput(uri: ProofUri): Promise<ProvingJobInputs>;
47
+
48
+ /**
49
+ * Retrieve a proof output from the database.
50
+ * @param uri - The URI of the proof output to retrieve.
51
+ * @returns The proof output.
52
+ */
53
+ getProofOutput(uri: ProofUri): Promise<ProvingJobResult>;
54
+ }
@@ -0,0 +1,181 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import { RunningPromise } from '@aztec/foundation/running-promise';
3
+ import { truncate } from '@aztec/foundation/string';
4
+ import { Timer } from '@aztec/foundation/timer';
5
+ import { ProvingError } from '@aztec/stdlib/errors';
6
+ import type {
7
+ ProvingJob,
8
+ ProvingJobConsumer,
9
+ ProvingJobId,
10
+ ProvingJobInputs,
11
+ ProvingJobResultsMap,
12
+ ServerCircuitProver,
13
+ } from '@aztec/stdlib/interfaces/server';
14
+ import { ProvingRequestType } from '@aztec/stdlib/proofs';
15
+ import {
16
+ type TelemetryClient,
17
+ type Traceable,
18
+ type Tracer,
19
+ getTelemetryClient,
20
+ trackSpan,
21
+ } from '@aztec/telemetry-client';
22
+
23
+ import type { ProofStore } from './proof_store/index.js';
24
+ import { ProvingAgentInstrumentation } from './proving_agent_instrumentation.js';
25
+ import { ProvingJobController, ProvingJobControllerStatus } from './proving_job_controller.js';
26
+
27
+ /**
28
+ * A helper class that encapsulates a circuit prover and connects it to a job source.
29
+ */
30
+ export class ProvingAgent implements Traceable {
31
+ private currentJobController?: ProvingJobController;
32
+ private runningPromise: RunningPromise;
33
+ private instrumentation: ProvingAgentInstrumentation;
34
+ private idleTimer: Timer | undefined;
35
+
36
+ public readonly tracer: Tracer;
37
+
38
+ constructor(
39
+ /** The source of proving jobs */
40
+ private broker: ProvingJobConsumer,
41
+ /** Database holding proof inputs and outputs */
42
+ private proofStore: ProofStore,
43
+ /** The prover implementation to defer jobs to */
44
+ private circuitProver: ServerCircuitProver,
45
+ /** Optional list of allowed proof types to build */
46
+ private proofAllowList: Array<ProvingRequestType> = [],
47
+ /** How long to wait between jobs */
48
+ private pollIntervalMs = 1000,
49
+ /** A telemetry client through which to emit metrics */
50
+ client: TelemetryClient = getTelemetryClient(),
51
+ private log = createLogger('prover-client:proving-agent'),
52
+ ) {
53
+ this.tracer = client.getTracer('ProvingAgent');
54
+ this.instrumentation = new ProvingAgentInstrumentation(client);
55
+ this.runningPromise = new RunningPromise(this.work.bind(this), this.log, this.pollIntervalMs);
56
+ }
57
+
58
+ public setCircuitProver(circuitProver: ServerCircuitProver): void {
59
+ this.circuitProver = circuitProver;
60
+ }
61
+
62
+ public isRunning(): boolean {
63
+ return this.runningPromise?.isRunning() ?? false;
64
+ }
65
+
66
+ public start(): void {
67
+ this.idleTimer = new Timer();
68
+ this.runningPromise.start();
69
+ }
70
+
71
+ public async stop(): Promise<void> {
72
+ this.currentJobController?.abort();
73
+ await this.runningPromise.stop();
74
+ }
75
+
76
+ @trackSpan('ProvingAgent.safeWork')
77
+ private async work() {
78
+ // every tick we need to
79
+ // (1) either do a heartbeat, telling the broker that we're working
80
+ // (2) get a new job
81
+ // If during (1) the broker returns a new job that means we can cancel the current job and start the new one
82
+ let maybeJob: { job: ProvingJob; time: number } | undefined;
83
+ if (this.currentJobController?.getStatus() === ProvingJobControllerStatus.PROVING) {
84
+ maybeJob = await this.broker.reportProvingJobProgress(
85
+ this.currentJobController.getJobId(),
86
+ this.currentJobController.getStartedAt(),
87
+ { allowList: this.proofAllowList },
88
+ );
89
+ } else {
90
+ maybeJob = await this.broker.getProvingJob({ allowList: this.proofAllowList });
91
+ }
92
+
93
+ if (!maybeJob) {
94
+ return;
95
+ }
96
+
97
+ if (this.idleTimer) {
98
+ this.instrumentation.recordIdleTime(this.idleTimer);
99
+ }
100
+ this.idleTimer = undefined;
101
+
102
+ const { job, time } = maybeJob;
103
+ await this.startJob(job, time);
104
+ }
105
+
106
+ private async startJob(job: ProvingJob, startedAt: number): Promise<void> {
107
+ let abortedProofJobId: string | undefined;
108
+ let abortedProofName: string | undefined;
109
+
110
+ if (this.currentJobController?.getStatus() === ProvingJobControllerStatus.PROVING) {
111
+ abortedProofJobId = this.currentJobController.getJobId();
112
+ abortedProofName = this.currentJobController.getProofTypeName();
113
+ this.currentJobController?.abort();
114
+ }
115
+
116
+ let inputs: ProvingJobInputs;
117
+ try {
118
+ inputs = await this.proofStore.getProofInput(job.inputsUri);
119
+ } catch (err) {
120
+ const maybeJob = await this.broker.reportProvingJobError(job.id, 'Failed to load proof inputs', true, {
121
+ allowList: this.proofAllowList,
122
+ });
123
+
124
+ if (maybeJob) {
125
+ return this.startJob(maybeJob.job, maybeJob.time);
126
+ }
127
+
128
+ return;
129
+ }
130
+
131
+ this.currentJobController = new ProvingJobController(
132
+ job.id,
133
+ inputs,
134
+ job.epochNumber,
135
+ startedAt,
136
+ this.circuitProver,
137
+ this.handleJobResult,
138
+ );
139
+
140
+ if (abortedProofJobId) {
141
+ this.log.info(
142
+ `Aborting job id=${abortedProofJobId} type=${abortedProofName} to start new job id=${this.currentJobController.getJobId()} type=${this.currentJobController.getProofTypeName()} inputsUri=${truncate(
143
+ job.inputsUri,
144
+ )}`,
145
+ );
146
+ } else {
147
+ this.log.info(
148
+ `Starting job id=${this.currentJobController.getJobId()} type=${this.currentJobController.getProofTypeName()} inputsUri=${truncate(
149
+ job.inputsUri,
150
+ )}`,
151
+ );
152
+ }
153
+
154
+ this.currentJobController.start();
155
+ }
156
+
157
+ handleJobResult = async <T extends ProvingRequestType>(
158
+ jobId: ProvingJobId,
159
+ type: T,
160
+ err: Error | undefined,
161
+ result: ProvingJobResultsMap[T] | undefined,
162
+ ) => {
163
+ let maybeJob: { job: ProvingJob; time: number } | undefined;
164
+ if (err) {
165
+ const retry = err.name === ProvingError.NAME ? (err as ProvingError).retry : false;
166
+ this.log.error(`Job id=${jobId} type=${ProvingRequestType[type]} failed err=${err.message} retry=${retry}`, err);
167
+ maybeJob = await this.broker.reportProvingJobError(jobId, err.message, retry, { allowList: this.proofAllowList });
168
+ } else if (result) {
169
+ const outputUri = await this.proofStore.saveProofOutput(jobId, type, result);
170
+ this.log.info(`Job id=${jobId} type=${ProvingRequestType[type]} completed outputUri=${truncate(outputUri)}`);
171
+ maybeJob = await this.broker.reportProvingJobSuccess(jobId, outputUri, { allowList: this.proofAllowList });
172
+ }
173
+
174
+ if (maybeJob) {
175
+ const { job, time } = maybeJob;
176
+ await this.startJob(job, time);
177
+ } else {
178
+ this.idleTimer = new Timer();
179
+ }
180
+ };
181
+ }
@@ -0,0 +1,21 @@
1
+ import type { Timer } from '@aztec/foundation/timer';
2
+ import { type Histogram, Metrics, type TelemetryClient, ValueType } from '@aztec/telemetry-client';
3
+
4
+ export class ProvingAgentInstrumentation {
5
+ private idleTime: Histogram;
6
+
7
+ constructor(client: TelemetryClient, name = 'ProvingAgent') {
8
+ const meter = client.getMeter(name);
9
+
10
+ this.idleTime = meter.createHistogram(Metrics.PROVING_AGENT_IDLE, {
11
+ description: 'Records how long an agent was idle',
12
+ unit: 's',
13
+ valueType: ValueType.DOUBLE,
14
+ });
15
+ }
16
+
17
+ recordIdleTime(msOrTimer: Timer | number) {
18
+ const duration = typeof msOrTimer === 'number' ? msOrTimer : msOrTimer.ms();
19
+ this.idleTime.record(duration / 1000);
20
+ }
21
+ }