@aztec/prover-client 0.0.1-commit.b655e406 → 0.0.1-commit.b8a057fa
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.
- package/dest/config.d.ts +2 -2
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +17 -3
- package/dest/index.d.ts +1 -1
- package/dest/light/index.d.ts +2 -0
- package/dest/light/index.d.ts.map +1 -0
- package/dest/light/index.js +1 -0
- package/dest/light/lightweight_checkpoint_builder.d.ts +52 -0
- package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -0
- package/dest/light/lightweight_checkpoint_builder.js +233 -0
- package/dest/mocks/fixtures.d.ts +1 -4
- package/dest/mocks/fixtures.d.ts.map +1 -1
- package/dest/mocks/fixtures.js +9 -18
- package/dest/mocks/test_context.d.ts +38 -55
- package/dest/mocks/test_context.d.ts.map +1 -1
- package/dest/mocks/test_context.js +143 -142
- package/dest/orchestrator/block-building-helpers.d.ts +18 -20
- package/dest/orchestrator/block-building-helpers.d.ts.map +1 -1
- package/dest/orchestrator/block-building-helpers.js +90 -115
- package/dest/orchestrator/block-proving-state.d.ts +20 -11
- package/dest/orchestrator/block-proving-state.d.ts.map +1 -1
- package/dest/orchestrator/block-proving-state.js +87 -19
- package/dest/orchestrator/checkpoint-proving-state.d.ts +24 -27
- package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
- package/dest/orchestrator/checkpoint-proving-state.js +28 -94
- package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +161 -0
- package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
- package/dest/orchestrator/checkpoint-sub-tree-orchestrator.js +1066 -0
- package/dest/orchestrator/chonk-cache.d.ts +39 -0
- package/dest/orchestrator/chonk-cache.d.ts.map +1 -0
- package/dest/orchestrator/chonk-cache.js +80 -0
- package/dest/orchestrator/index.d.ts +4 -2
- package/dest/orchestrator/index.d.ts.map +1 -1
- package/dest/orchestrator/index.js +3 -1
- package/dest/orchestrator/orchestrator_metrics.d.ts +1 -3
- package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -1
- package/dest/orchestrator/orchestrator_metrics.js +2 -15
- package/dest/orchestrator/proving-scheduler.d.ts +81 -0
- package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
- package/dest/orchestrator/proving-scheduler.js +120 -0
- package/dest/orchestrator/top-tree-orchestrator.d.ts +88 -0
- package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
- package/dest/orchestrator/top-tree-orchestrator.js +232 -0
- package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
- package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
- package/dest/orchestrator/top-tree-proving-state.js +185 -0
- package/dest/orchestrator/tx-proving-state.d.ts +7 -6
- package/dest/orchestrator/tx-proving-state.d.ts.map +1 -1
- package/dest/orchestrator/tx-proving-state.js +7 -16
- package/dest/prover-client/factory.d.ts +3 -3
- package/dest/prover-client/factory.d.ts.map +1 -1
- package/dest/prover-client/index.d.ts +1 -1
- package/dest/prover-client/prover-client.d.ts +65 -8
- package/dest/prover-client/prover-client.d.ts.map +1 -1
- package/dest/prover-client/prover-client.js +66 -14
- package/dest/proving_broker/broker_prover_facade.d.ts +26 -24
- package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
- package/dest/proving_broker/broker_prover_facade.js +21 -33
- package/dest/proving_broker/config.d.ts +14 -62
- package/dest/proving_broker/config.d.ts.map +1 -1
- package/dest/proving_broker/config.js +29 -6
- package/dest/proving_broker/factory.d.ts +1 -1
- package/dest/proving_broker/fixtures.d.ts +3 -2
- package/dest/proving_broker/fixtures.d.ts.map +1 -1
- package/dest/proving_broker/fixtures.js +3 -2
- package/dest/proving_broker/index.d.ts +2 -1
- package/dest/proving_broker/index.d.ts.map +1 -1
- package/dest/proving_broker/index.js +1 -0
- package/dest/proving_broker/proof_store/factory.d.ts +2 -5
- package/dest/proving_broker/proof_store/factory.d.ts.map +1 -1
- package/dest/proving_broker/proof_store/factory.js +7 -30
- package/dest/proving_broker/proof_store/file_store_proof_store.d.ts +18 -0
- package/dest/proving_broker/proof_store/file_store_proof_store.d.ts.map +1 -0
- package/dest/proving_broker/proof_store/file_store_proof_store.js +60 -0
- package/dest/proving_broker/proof_store/index.d.ts +2 -2
- package/dest/proving_broker/proof_store/index.d.ts.map +1 -1
- package/dest/proving_broker/proof_store/index.js +1 -1
- package/dest/proving_broker/proof_store/inline_proof_store.d.ts +1 -1
- package/dest/proving_broker/proof_store/inline_proof_store.d.ts.map +1 -1
- package/dest/proving_broker/proof_store/proof_store.d.ts +1 -1
- package/dest/proving_broker/proving_agent.d.ts +5 -9
- package/dest/proving_broker/proving_agent.d.ts.map +1 -1
- package/dest/proving_broker/proving_agent.js +4 -19
- package/dest/proving_broker/proving_broker.d.ts +8 -5
- package/dest/proving_broker/proving_broker.d.ts.map +1 -1
- package/dest/proving_broker/proving_broker.js +74 -22
- package/dest/proving_broker/proving_broker_database/memory.d.ts +3 -2
- package/dest/proving_broker/proving_broker_database/memory.d.ts.map +1 -1
- package/dest/proving_broker/proving_broker_database/persisted.d.ts +5 -3
- package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -1
- package/dest/proving_broker/proving_broker_database/persisted.js +394 -5
- package/dest/proving_broker/proving_broker_database.d.ts +3 -2
- package/dest/proving_broker/proving_broker_database.d.ts.map +1 -1
- package/dest/proving_broker/proving_broker_instrumentation.d.ts +3 -1
- package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
- package/dest/proving_broker/proving_broker_instrumentation.js +22 -35
- package/dest/proving_broker/proving_job_controller.d.ts +5 -3
- package/dest/proving_broker/proving_job_controller.d.ts.map +1 -1
- package/dest/proving_broker/proving_job_controller.js +8 -6
- package/dest/proving_broker/rpc.d.ts +9 -5
- package/dest/proving_broker/rpc.d.ts.map +1 -1
- package/dest/proving_broker/rpc.js +87 -23
- package/dest/test/epoch_settlement.d.ts +36 -0
- package/dest/test/epoch_settlement.d.ts.map +1 -0
- package/dest/test/epoch_settlement.js +52 -0
- package/dest/test/index.d.ts +2 -0
- package/dest/test/index.d.ts.map +1 -0
- package/dest/test/index.js +1 -0
- package/dest/test/mock_proof_store.d.ts +1 -1
- package/dest/test/mock_proof_store.d.ts.map +1 -1
- package/dest/test/mock_prover.d.ts +5 -6
- package/dest/test/mock_prover.d.ts.map +1 -1
- package/dest/test/mock_prover.js +4 -4
- package/package.json +25 -23
- package/src/config.ts +19 -3
- package/src/light/index.ts +1 -0
- package/src/light/lightweight_checkpoint_builder.ts +322 -0
- package/src/mocks/fixtures.ts +9 -31
- package/src/mocks/test_context.ts +182 -217
- package/src/orchestrator/block-building-helpers.ts +129 -209
- package/src/orchestrator/block-proving-state.ts +109 -22
- package/src/orchestrator/checkpoint-proving-state.ts +32 -123
- package/src/orchestrator/checkpoint-sub-tree-orchestrator.ts +1044 -0
- package/src/orchestrator/chonk-cache.ts +99 -0
- package/src/orchestrator/index.ts +8 -1
- package/src/orchestrator/orchestrator_metrics.ts +2 -25
- package/src/orchestrator/proving-scheduler.ts +160 -0
- package/src/orchestrator/top-tree-orchestrator.ts +384 -0
- package/src/orchestrator/top-tree-proving-state.ts +219 -0
- package/src/orchestrator/tx-proving-state.ts +10 -27
- package/src/prover-client/factory.ts +6 -2
- package/src/prover-client/prover-client.ts +148 -29
- package/src/proving_broker/broker_prover_facade.ts +47 -56
- package/src/proving_broker/config.ts +33 -3
- package/src/proving_broker/fixtures.ts +8 -3
- package/src/proving_broker/index.ts +1 -0
- package/src/proving_broker/proof_store/factory.ts +10 -32
- package/src/proving_broker/proof_store/file_store_proof_store.ts +78 -0
- package/src/proving_broker/proof_store/index.ts +1 -1
- package/src/proving_broker/proving_agent.ts +6 -19
- package/src/proving_broker/proving_broker.ts +70 -17
- package/src/proving_broker/proving_broker_database/memory.ts +2 -1
- package/src/proving_broker/proving_broker_database/persisted.ts +22 -7
- package/src/proving_broker/proving_broker_database.ts +2 -1
- package/src/proving_broker/proving_broker_instrumentation.ts +23 -35
- package/src/proving_broker/proving_job_controller.ts +13 -7
- package/src/proving_broker/rpc.ts +46 -20
- package/src/test/epoch_settlement.ts +82 -0
- package/src/test/index.ts +1 -0
- package/src/test/mock_prover.ts +3 -15
- package/dest/block-factory/index.d.ts +0 -2
- package/dest/block-factory/index.d.ts.map +0 -1
- package/dest/block-factory/index.js +0 -1
- package/dest/block-factory/light.d.ts +0 -38
- package/dest/block-factory/light.d.ts.map +0 -1
- package/dest/block-factory/light.js +0 -94
- package/dest/orchestrator/epoch-proving-state.d.ts +0 -73
- package/dest/orchestrator/epoch-proving-state.d.ts.map +0 -1
- package/dest/orchestrator/epoch-proving-state.js +0 -233
- package/dest/orchestrator/orchestrator.d.ts +0 -113
- package/dest/orchestrator/orchestrator.d.ts.map +0 -1
- package/dest/orchestrator/orchestrator.js +0 -848
- package/dest/prover-client/server-epoch-prover.d.ts +0 -31
- package/dest/prover-client/server-epoch-prover.d.ts.map +0 -1
- package/dest/prover-client/server-epoch-prover.js +0 -40
- package/dest/proving_broker/proof_store/gcs_proof_store.d.ts +0 -14
- package/dest/proving_broker/proof_store/gcs_proof_store.d.ts.map +0 -1
- package/dest/proving_broker/proof_store/gcs_proof_store.js +0 -52
- package/dest/proving_broker/proving_agent_instrumentation.d.ts +0 -8
- package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +0 -1
- package/dest/proving_broker/proving_agent_instrumentation.js +0 -16
- package/src/block-factory/index.ts +0 -1
- package/src/block-factory/light.ts +0 -140
- package/src/orchestrator/epoch-proving-state.ts +0 -328
- package/src/orchestrator/orchestrator.ts +0 -1272
- package/src/prover-client/server-epoch-prover.ts +0 -70
- package/src/proving_broker/proof_store/gcs_proof_store.ts +0 -76
- package/src/proving_broker/proving_agent_instrumentation.ts +0 -21
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
|
|
2
|
+
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import type { L2Block } from '@aztec/stdlib/block';
|
|
4
|
+
import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
|
|
5
|
+
import type { PublicChonkVerifierPublicInputs } from '@aztec/stdlib/rollup';
|
|
6
|
+
|
|
7
|
+
/** Result of a chonk-verifier proof, cached by tx hash. */
|
|
8
|
+
export type ChonkVerifierProofResult = PublicInputsAndRecursiveProof<
|
|
9
|
+
PublicChonkVerifierPublicInputs,
|
|
10
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Shared cache of `getPublicChonkVerifierProof` results, keyed by tx hash.
|
|
15
|
+
*
|
|
16
|
+
* Owned by the prover-node and shared across every epoch / session: a tx remined into
|
|
17
|
+
* a different block (e.g. after a brief L1 reorg) reuses the already-computed chonk
|
|
18
|
+
* proof rather than redoing it. Entries are released via `releaseForBlocks` once the
|
|
19
|
+
* containing block is no longer interesting to the caller (e.g. its epoch's proof
|
|
20
|
+
* submission window has expired).
|
|
21
|
+
*/
|
|
22
|
+
export class ChonkCache {
|
|
23
|
+
private readonly cache = new Map<string, Promise<ChonkVerifierProofResult>>();
|
|
24
|
+
private readonly pending = new Map<string, AbortController>();
|
|
25
|
+
private readonly log: Logger;
|
|
26
|
+
private stopped = false;
|
|
27
|
+
|
|
28
|
+
constructor(bindings?: LoggerBindings) {
|
|
29
|
+
this.log = createLogger('prover-client:chonk-cache', bindings);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Returns the cached promise for `txHash`, or `undefined` if none is registered. */
|
|
33
|
+
public get(txHash: string): Promise<ChonkVerifierProofResult> | undefined {
|
|
34
|
+
return this.cache.get(txHash);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Atomic get-or-compute: returns the cached promise for `txHash`, or runs `factory`
|
|
39
|
+
* (passing an AbortSignal the cache controls) and caches its result. Concurrent
|
|
40
|
+
* callers for the same `txHash` share the same promise.
|
|
41
|
+
*
|
|
42
|
+
* Rejected promises are evicted so a future caller can retry. The factory's
|
|
43
|
+
* AbortSignal fires when the cache is stopped.
|
|
44
|
+
*/
|
|
45
|
+
public getOrCompute(
|
|
46
|
+
txHash: string,
|
|
47
|
+
factory: (signal: AbortSignal) => Promise<ChonkVerifierProofResult>,
|
|
48
|
+
): Promise<ChonkVerifierProofResult> {
|
|
49
|
+
if (this.stopped) {
|
|
50
|
+
return Promise.reject(new Error('ChonkCache is stopped'));
|
|
51
|
+
}
|
|
52
|
+
const existing = this.cache.get(txHash);
|
|
53
|
+
if (existing) {
|
|
54
|
+
return existing;
|
|
55
|
+
}
|
|
56
|
+
const controller = new AbortController();
|
|
57
|
+
this.pending.set(txHash, controller);
|
|
58
|
+
this.log.debug(`Enqueueing chonk-verifier circuit`, { txHash });
|
|
59
|
+
const promise = factory(controller.signal).finally(() => this.pending.delete(txHash));
|
|
60
|
+
// Silently observe the rejection branch and evict so a retry is possible.
|
|
61
|
+
promise.catch(err => {
|
|
62
|
+
this.cache.delete(txHash);
|
|
63
|
+
this.log.debug(`Chonk-verifier proof failed; evicted from cache`, { txHash, error: `${err}` });
|
|
64
|
+
});
|
|
65
|
+
this.cache.set(txHash, promise);
|
|
66
|
+
return promise;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Drops cache entries for every tx in the supplied blocks. */
|
|
70
|
+
public releaseForBlocks(blocks: L2Block[]): void {
|
|
71
|
+
let released = 0;
|
|
72
|
+
for (const block of blocks) {
|
|
73
|
+
for (const txEffect of block.body.txEffects) {
|
|
74
|
+
if (this.cache.delete(txEffect.txHash.toString())) {
|
|
75
|
+
released++;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (released > 0) {
|
|
80
|
+
this.log.debug(`Released ${released} chonk-verifier cache entries`, {
|
|
81
|
+
blockCount: blocks.length,
|
|
82
|
+
releasedCount: released,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Aborts every in-flight chonk-verifier job and clears the cache. */
|
|
88
|
+
public stop(): void {
|
|
89
|
+
if (this.stopped) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
this.stopped = true;
|
|
93
|
+
for (const controller of this.pending.values()) {
|
|
94
|
+
controller.abort();
|
|
95
|
+
}
|
|
96
|
+
this.pending.clear();
|
|
97
|
+
this.cache.clear();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -1 +1,8 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { CheckpointSubTreeOrchestrator, type SubTreeResult } from './checkpoint-sub-tree-orchestrator.js';
|
|
2
|
+
export { ChonkCache, type ChonkVerifierProofResult } from './chonk-cache.js';
|
|
3
|
+
export {
|
|
4
|
+
TopTreeOrchestrator,
|
|
5
|
+
TopTreeCancelledError,
|
|
6
|
+
type CheckpointTopTreeData,
|
|
7
|
+
type TopTreeResult,
|
|
8
|
+
} from './top-tree-orchestrator.js';
|
|
@@ -1,41 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Histogram,
|
|
3
|
-
Metrics,
|
|
4
|
-
type TelemetryClient,
|
|
5
|
-
type Tracer,
|
|
6
|
-
type UpDownCounter,
|
|
7
|
-
ValueType,
|
|
8
|
-
} from '@aztec/telemetry-client';
|
|
1
|
+
import { type Histogram, Metrics, type TelemetryClient, type Tracer } from '@aztec/telemetry-client';
|
|
9
2
|
|
|
10
3
|
export class ProvingOrchestratorMetrics {
|
|
11
4
|
public readonly tracer: Tracer;
|
|
12
5
|
|
|
13
6
|
private baseRollupInputsDuration: Histogram;
|
|
14
|
-
private avmFallbackCount: UpDownCounter;
|
|
15
7
|
|
|
16
8
|
constructor(client: TelemetryClient, name = 'ProvingOrchestrator') {
|
|
17
9
|
this.tracer = client.getTracer(name);
|
|
18
10
|
const meter = client.getMeter(name);
|
|
19
11
|
|
|
20
|
-
this.baseRollupInputsDuration = meter.createHistogram(Metrics.PROVING_ORCHESTRATOR_BASE_ROLLUP_INPUTS_DURATION
|
|
21
|
-
unit: 'ms',
|
|
22
|
-
description: 'Duration to build base rollup inputs',
|
|
23
|
-
valueType: ValueType.INT,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
this.avmFallbackCount = meter.createUpDownCounter(Metrics.PROVING_ORCHESTRATOR_AVM_FALLBACK_COUNT, {
|
|
27
|
-
description: 'How many times the AVM fallback was used',
|
|
28
|
-
valueType: ValueType.INT,
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
this.avmFallbackCount.add(0);
|
|
12
|
+
this.baseRollupInputsDuration = meter.createHistogram(Metrics.PROVING_ORCHESTRATOR_BASE_ROLLUP_INPUTS_DURATION);
|
|
32
13
|
}
|
|
33
14
|
|
|
34
15
|
recordBaseRollupInputs(durationMs: number) {
|
|
35
16
|
this.baseRollupInputsDuration.record(Math.ceil(durationMs));
|
|
36
17
|
}
|
|
37
|
-
|
|
38
|
-
incAvmFallback() {
|
|
39
|
-
this.avmFallbackCount.add(1);
|
|
40
|
-
}
|
|
41
18
|
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { AbortError } from '@aztec/foundation/error';
|
|
2
|
+
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
4
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Minimal surface a deferred-proving state must expose. Both `CheckpointProvingState` /
|
|
8
|
+
* `BlockProvingState` (used by `CheckpointSubTreeOrchestrator`) and `TopTreeProvingState`
|
|
9
|
+
* (used by `TopTreeOrchestrator`) satisfy it.
|
|
10
|
+
*/
|
|
11
|
+
export interface ProvingStateLike {
|
|
12
|
+
/** Returns false once the state has been cancelled or otherwise invalidated. */
|
|
13
|
+
verifyState(): boolean;
|
|
14
|
+
/** Surfaces a proving error to the state's owner. */
|
|
15
|
+
reject(reason: string): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Common scheduling infrastructure shared by every orchestrator that drives broker
|
|
20
|
+
* proving jobs:
|
|
21
|
+
*
|
|
22
|
+
* - A shared `SerialQueue` (`deferredJobQueue`) that serialises the *act of handing a
|
|
23
|
+
* job to the broker*, one initiation per event-loop tick. Each queue task kicks off
|
|
24
|
+
* `safeJob` (which submits to the broker) without awaiting it, then yields with
|
|
25
|
+
* `sleep(0)`; the next task therefore runs on the following macrotask. This does NOT
|
|
26
|
+
* cap how many broker jobs are concurrently in flight — the broker's own queue absorbs
|
|
27
|
+
* that. What it bounds is the burst rate: a sub-tree that synchronously discovers
|
|
28
|
+
* thousands of ready jobs can't flood the broker (and monopolise the event loop) in a
|
|
29
|
+
* single tick. The queue is owned by the `ProverClient` and shared across every
|
|
30
|
+
* orchestrator (every sub-tree and top-tree across every concurrent epoch session), so
|
|
31
|
+
* this pacing is applied once globally rather than once-per-orchestrator.
|
|
32
|
+
* - A list of `AbortController`s (`pendingProvingJobs`) so a `cancel()` can abort
|
|
33
|
+
* in-flight broker jobs when needed.
|
|
34
|
+
* - A `deferredProving<T>(state, request, callback, isCancelled?)` method that wraps
|
|
35
|
+
* a broker request in the standard "submit, drop result if state invalidated, push
|
|
36
|
+
* errors to state.reject" envelope.
|
|
37
|
+
*
|
|
38
|
+
* Subclasses own their own concrete proving state and define `cancelInternal()` for
|
|
39
|
+
* the rest of the cleanup work (closing world-state forks, marking sub-trees
|
|
40
|
+
* cancelled, etc.). Because the queue is shared, neither `cancel()` nor `stop()` touch
|
|
41
|
+
* it — they only abort this orchestrator's in-flight broker jobs. Queued-but-unrun jobs
|
|
42
|
+
* for a cancelled orchestrator no-op via the guards in `deferredProving`.
|
|
43
|
+
*/
|
|
44
|
+
export abstract class ProvingScheduler {
|
|
45
|
+
protected pendingProvingJobs: AbortController[] = [];
|
|
46
|
+
protected logger: Logger;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly deferredJobQueue: SerialQueue,
|
|
50
|
+
loggerName = 'prover-client:proving-scheduler',
|
|
51
|
+
bindings?: LoggerBindings,
|
|
52
|
+
) {
|
|
53
|
+
this.logger = createLogger(loggerName, bindings);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Number of broker jobs currently in flight. */
|
|
57
|
+
public getNumPendingProvingJobs(): number {
|
|
58
|
+
return this.pendingProvingJobs.length;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Optionally aborts every in-flight broker job. Aborting is the right choice on
|
|
63
|
+
* reorg-driven cancel (where the in-flight inputs are no longer valid) and the
|
|
64
|
+
* wrong choice on shutdown (where leaving jobs in the broker queue lets a restart
|
|
65
|
+
* pick them up). The shared queue is not touched — queued-but-unrun jobs belonging
|
|
66
|
+
* to this orchestrator no-op once their controller is aborted (or once their state
|
|
67
|
+
* is marked invalid by the subclass).
|
|
68
|
+
*/
|
|
69
|
+
protected resetSchedulerState(abortJobs: boolean): void {
|
|
70
|
+
if (abortJobs) {
|
|
71
|
+
for (const controller of this.pendingProvingJobs) {
|
|
72
|
+
controller.abort();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Subclass-defined cancellation. Implementations call `resetSchedulerState(...)`
|
|
79
|
+
* and then do their own cleanup (close world-state forks, propagate cancel into
|
|
80
|
+
* the proving state, etc.).
|
|
81
|
+
*/
|
|
82
|
+
protected abstract cancelInternal(): void;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Standard stop: cancel this orchestrator's work. The shared queue is owned by the
|
|
86
|
+
* `ProverClient` and outlives every orchestrator, so it is not drained here.
|
|
87
|
+
*/
|
|
88
|
+
public stop(): Promise<void> {
|
|
89
|
+
this.cancelInternal();
|
|
90
|
+
return Promise.resolve();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Submits a broker request. The returned-via-callback result is dropped if the
|
|
95
|
+
* state has become invalid (re-org, cancellation) by the time it lands. Errors
|
|
96
|
+
* are routed to `state.reject` unless they are abort-driven or the state is
|
|
97
|
+
* already invalid (in which case they're a stale echo of the cancel).
|
|
98
|
+
*
|
|
99
|
+
* @param state - Object exposing `verifyState()` and `reject()`.
|
|
100
|
+
* @param request - The broker call. Receives the controller's signal.
|
|
101
|
+
* @param callback - Runs on success, after `verifyState()` is checked.
|
|
102
|
+
* @param isCancelled - Optional extra cancellation predicate (e.g. a `cancelled`
|
|
103
|
+
* flag the subclass maintains independently of the state). Defaults to never.
|
|
104
|
+
*/
|
|
105
|
+
protected deferredProving<S extends ProvingStateLike, T>(
|
|
106
|
+
state: S,
|
|
107
|
+
request: (signal: AbortSignal) => Promise<T>,
|
|
108
|
+
callback: (result: T) => void | Promise<void>,
|
|
109
|
+
isCancelled: () => boolean = () => false,
|
|
110
|
+
): void {
|
|
111
|
+
if (!state.verifyState()) {
|
|
112
|
+
this.logger.debug(`Not enqueuing job, state no longer valid`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const controller = new AbortController();
|
|
117
|
+
this.pendingProvingJobs.push(controller);
|
|
118
|
+
|
|
119
|
+
// We use a 'safeJob'. We don't want promise rejections in the proving pool — we
|
|
120
|
+
// want to capture the error here and reject the proving state while keeping the
|
|
121
|
+
// event loop free of rejections.
|
|
122
|
+
const safeJob = async () => {
|
|
123
|
+
try {
|
|
124
|
+
if (controller.signal.aborted) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const result = await request(controller.signal);
|
|
128
|
+
if (controller.signal.aborted || !state.verifyState() || isCancelled()) {
|
|
129
|
+
this.logger.debug(`State no longer valid, discarding result`);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
await callback(result);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
if (err instanceof AbortError || isCancelled()) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (!state.verifyState()) {
|
|
138
|
+
this.logger.debug(`State no longer valid, discarding error from proving job`, err);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
this.logger.error(`Error thrown when proving job`, err);
|
|
142
|
+
state.reject(`${err}`);
|
|
143
|
+
} finally {
|
|
144
|
+
const idx = this.pendingProvingJobs.indexOf(controller);
|
|
145
|
+
if (idx > -1) {
|
|
146
|
+
this.pendingProvingJobs.splice(idx, 1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
void this.deferredJobQueue.put(async () => {
|
|
152
|
+
// Kick off the broker submission without awaiting it — awaiting here would serialise all
|
|
153
|
+
// proving (one job at a time) and kill parallelism. The `sleep(0)` yields the event loop
|
|
154
|
+
// so the next queued job initiates on the following macrotask, pacing bursts rather than
|
|
155
|
+
// bounding in-flight broker concurrency (the broker's own queue handles that).
|
|
156
|
+
void safeJob();
|
|
157
|
+
await sleep(0);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|