@aztec-labs/bb-prover 6.0.0-rc.3 → 6.0.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.
- package/dest/avm_proving_tests/avm_proving_tester.d.ts +1 -1
- package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
- package/dest/avm_proving_tests/avm_proving_tester.js +2 -1
- package/dest/bb/bb_js_debug.d.ts +1 -1
- package/dest/bb/bb_js_debug.d.ts.map +1 -1
- package/dest/bb/bb_js_debug.js +1 -0
- package/dest/prover/client/bb_private_kernel_prover.d.ts +2 -1
- package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
- package/dest/prover/client/bb_private_kernel_prover.js +3 -1
- package/dest/prover/proof_utils.d.ts +1 -1
- package/dest/prover/proof_utils.d.ts.map +1 -1
- package/dest/prover/proof_utils.js +1 -0
- package/dest/prover/server/bb_prover.d.ts +1 -1
- package/dest/prover/server/bb_prover.d.ts.map +1 -1
- package/dest/prover/server/bb_prover.js +1 -0
- package/dest/verifier/batch_chonk_verifier.d.ts +25 -2
- package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -1
- package/dest/verifier/batch_chonk_verifier.js +100 -40
- package/dest/verifier/index.d.ts +2 -1
- package/dest/verifier/index.d.ts.map +1 -1
- package/dest/verifier/index.js +1 -0
- package/dest/verifier/self_healing_chonk_verifier.d.ts +76 -0
- package/dest/verifier/self_healing_chonk_verifier.d.ts.map +1 -0
- package/dest/verifier/self_healing_chonk_verifier.js +231 -0
- package/package.json +17 -16
- package/src/avm_proving_tests/avm_proving_tester.ts +2 -1
- package/src/bb/bb_js_debug.ts +1 -0
- package/src/prover/client/bb_private_kernel_prover.ts +3 -1
- package/src/prover/proof_utils.ts +1 -0
- package/src/prover/server/bb_prover.ts +1 -0
- package/src/verifier/batch_chonk_verifier.ts +83 -37
- package/src/verifier/index.ts +1 -0
- package/src/verifier/self_healing_chonk_verifier.ts +242 -0
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { BackendType, Barretenberg } from '@aztec-foundation/bb.js';
|
|
2
2
|
|
|
3
|
+
import { CHONK_PROOF_LENGTH } from '@aztec-labs/constants';
|
|
3
4
|
import { FifoFrameReader } from '@aztec-labs/foundation/fifo';
|
|
4
5
|
import { createLogger } from '@aztec-labs/foundation/log';
|
|
5
|
-
import { SerialQueue } from '@aztec-labs/foundation/queue';
|
|
6
|
+
import { Semaphore, SerialQueue } from '@aztec-labs/foundation/queue';
|
|
6
7
|
import { Timer } from '@aztec-labs/foundation/timer';
|
|
7
8
|
import { ProtocolCircuitVks } from '@aztec-labs/noir-protocol-circuits-types/server/vks';
|
|
8
9
|
import type { ClientProtocolCircuitVerifier, IVCProofVerificationResult } from '@aztec-labs/stdlib/interfaces/server';
|
|
@@ -20,8 +21,15 @@ import type { BBConfig } from '../config.js';
|
|
|
20
21
|
import { IVCVerifierMetrics } from './queued_chonk_verifier.js';
|
|
21
22
|
|
|
22
23
|
const execFileAsync = promisify(execFile);
|
|
23
|
-
const RESULT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
24
24
|
const STOP_DRAIN_TIMEOUT_MS = 5_000;
|
|
25
|
+
/**
|
|
26
|
+
* Most proofs in flight at once. bb itself (its C++ batch verifier) rejects any proof enqueued once 1024 are already
|
|
27
|
+
* waiting (its `MAX_QUEUE_SIZE`), and reports it as FAILED: the same result as a proof it checked and found invalid.
|
|
28
|
+
* This cap prevents that. Proofs in flight here include every proof bb has queued, so capping them at bb's limit means
|
|
29
|
+
* bb's queue never overflows. A proof enqueued past the cap waits here, and goes to bb once one in flight gets its
|
|
30
|
+
* result.
|
|
31
|
+
*/
|
|
32
|
+
export const MAX_PROOFS_IN_FLIGHT = 1024;
|
|
25
33
|
|
|
26
34
|
/** Result from the FIFO, matching the C++ VerifyResult struct. */
|
|
27
35
|
interface FifoVerifyResult {
|
|
@@ -38,7 +46,8 @@ interface PendingRequest {
|
|
|
38
46
|
resolve: (result: IVCProofVerificationResult) => void;
|
|
39
47
|
reject: (error: Error) => void;
|
|
40
48
|
totalTimer: Timer;
|
|
41
|
-
|
|
49
|
+
/** The startup request that makes bb open the result stream; not a proof anyone asked about. */
|
|
50
|
+
probe?: boolean;
|
|
42
51
|
}
|
|
43
52
|
|
|
44
53
|
/**
|
|
@@ -68,6 +77,11 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
68
77
|
private stopped = false;
|
|
69
78
|
private fatalError: Error | undefined;
|
|
70
79
|
private pendingDrainedResolvers = new Set<() => void>();
|
|
80
|
+
/**
|
|
81
|
+
* Holds a token for each entry in `pendingRequests`, so a proof past {@link MAX_PROOFS_IN_FLIGHT} waits here for
|
|
82
|
+
* room. stop() and a failure cancel it, which lets every waiting proof through to be turned away.
|
|
83
|
+
*/
|
|
84
|
+
private readonly inFlightSlots = new Semaphore(MAX_PROOFS_IN_FLIGHT);
|
|
71
85
|
|
|
72
86
|
private constructor(
|
|
73
87
|
private config: Pick<BBConfig, 'bbChonkVerifyConcurrency'> & Partial<Pick<BBConfig, 'bbBinaryPath'>>,
|
|
@@ -136,6 +150,7 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
136
150
|
batchSize: this.batchSize,
|
|
137
151
|
fifoPath: this.fifoPath,
|
|
138
152
|
});
|
|
153
|
+
await this.openResultStream();
|
|
139
154
|
} catch (err) {
|
|
140
155
|
this.fifoReader.stop();
|
|
141
156
|
this.deregisterExitCleanup();
|
|
@@ -146,9 +161,34 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
146
161
|
this.logger.info('BatchChonkVerifier started', { fifoPath: this.fifoPath });
|
|
147
162
|
}
|
|
148
163
|
|
|
149
|
-
|
|
164
|
+
/**
|
|
165
|
+
* Makes bb open the result stream now. bb opens it only to write its first result, and the stream ending is how a bb
|
|
166
|
+
* death reaches proofs already queued, so until then such a death would leave them waiting forever. A proof with no
|
|
167
|
+
* fields fails to deserialize, and bb writes that result before acknowledging the request.
|
|
168
|
+
*/
|
|
169
|
+
private async openResultStream(): Promise<void> {
|
|
170
|
+
await this.inFlightSlots.acquire();
|
|
171
|
+
const requestId = this.nextRequestId++;
|
|
172
|
+
const result = new Promise<IVCProofVerificationResult>((resolve, reject) => {
|
|
173
|
+
this.pendingRequests.set(requestId, { resolve, reject, totalTimer: new Timer(), probe: true });
|
|
174
|
+
});
|
|
175
|
+
// Awaited together so a failure of the stream before bb acknowledges the request is still handled.
|
|
176
|
+
await Promise.all([this.bb.chonkBatchVerifierQueue({ requestId, vkIndex: 0, proofFields: [] }), result]);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Verifies a tx's Chonk proof. Returns `valid: false` only for a malformed proof or one bb checked and rejected; rejects
|
|
181
|
+
* whenever the proof could not be checked, such as when bb died with it in flight (see {@link isFailed}).
|
|
182
|
+
*/
|
|
183
|
+
public async verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
|
|
150
184
|
const totalTimer = new Timer();
|
|
151
|
-
|
|
185
|
+
const txHash = tx.getTxHash().toString();
|
|
186
|
+
// The empty placeholder can't be handed to bb at all; bb rejects any other malformed proof itself.
|
|
187
|
+
if (tx.chonkProof.fields.length !== CHONK_PROOF_LENGTH) {
|
|
188
|
+
this.logger.warn(`Rejecting malformed Chonk proof`, { txHash, length: tx.chonkProof.fields.length });
|
|
189
|
+
return { valid: false, durationMs: 0, totalDurationMs: totalTimer.ms() };
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
152
192
|
const circuit = tx.data.forPublic ? 'HidingKernelToPublic' : 'HidingKernelToRollup';
|
|
153
193
|
const vkIndex = this.vkIndexMap.get(circuit);
|
|
154
194
|
if (vkIndex === undefined) {
|
|
@@ -157,39 +197,31 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
157
197
|
const proofWithPubInputs = tx.chonkProof.attachPublicInputs(tx.data.publicInputs().toFields());
|
|
158
198
|
const proofFields = proofWithPubInputs.fieldsWithPublicInputs.map(f => f.toBuffer());
|
|
159
199
|
return await this.enqueueProof(vkIndex, proofFields);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
200
|
+
} catch (err) {
|
|
201
|
+
// A verdict only ever arrives through handleResult, so bb gave no answer on this proof.
|
|
202
|
+
this.logger.warn(`Could not verify Chonk proof`, { txHash, err });
|
|
203
|
+
throw err;
|
|
204
|
+
}
|
|
164
205
|
}
|
|
165
206
|
|
|
166
|
-
/** Enqueue raw proof fields for verification. Used directly by tests with custom VKs. */
|
|
167
|
-
public enqueueProof(vkIndex: number, proofFields: Uint8Array[]): Promise<IVCProofVerificationResult> {
|
|
207
|
+
/** Enqueue raw proof fields for verification, once there is room. Used directly by tests with custom VKs. */
|
|
208
|
+
public async enqueueProof(vkIndex: number, proofFields: Uint8Array[]): Promise<IVCProofVerificationResult> {
|
|
209
|
+
// A free slot is taken without yielding, so a proof enqueued before stop() is sent before stop() ends the sends.
|
|
210
|
+
if (!this.inFlightSlots.tryAcquire()) {
|
|
211
|
+
await this.inFlightSlots.acquire();
|
|
212
|
+
}
|
|
168
213
|
if (this.stopped) {
|
|
169
|
-
|
|
214
|
+
throw new Error('BatchChonkVerifier stopped');
|
|
170
215
|
}
|
|
171
216
|
if (this.fatalError) {
|
|
172
|
-
|
|
217
|
+
throw this.fatalError;
|
|
173
218
|
}
|
|
174
219
|
|
|
175
220
|
const totalTimer = new Timer();
|
|
176
221
|
const requestId = this.nextRequestId++;
|
|
177
222
|
|
|
178
223
|
const resultPromise = new Promise<IVCProofVerificationResult>((resolve, reject) => {
|
|
179
|
-
|
|
180
|
-
const pending = this.pendingRequests.get(requestId);
|
|
181
|
-
if (!pending) {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
this.pendingRequests.delete(requestId);
|
|
185
|
-
pending.reject(new Error(`BatchChonkVerifier result timed out for request_id=${requestId}`));
|
|
186
|
-
this.notifyPendingDrained();
|
|
187
|
-
}, RESULT_TIMEOUT_MS);
|
|
188
|
-
// A pending result timer must never keep the host process alive on its own (e.g. an
|
|
189
|
-
// orphaned request at process exit); the FIFO reader keeps the loop alive while results
|
|
190
|
-
// are genuinely awaited.
|
|
191
|
-
timeout.unref();
|
|
192
|
-
this.pendingRequests.set(requestId, { resolve, reject, totalTimer, timeout });
|
|
224
|
+
this.pendingRequests.set(requestId, { resolve, reject, totalTimer });
|
|
193
225
|
});
|
|
194
226
|
|
|
195
227
|
void this.sendQueue
|
|
@@ -204,21 +236,26 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
204
236
|
});
|
|
205
237
|
})
|
|
206
238
|
.catch(err => {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
clearTimeout(pending.timeout);
|
|
211
|
-
pending.reject(err instanceof Error ? err : new Error(String(err)));
|
|
212
|
-
this.notifyPendingDrained();
|
|
213
|
-
}
|
|
239
|
+
// bb not taking the proof (bb.js reports the process gone before the result stream ends, or bb errors) leaves
|
|
240
|
+
// its queue in an unknown state, so the verifier fails and is replaced.
|
|
241
|
+
this.failVerifier(err instanceof Error ? err : new Error(String(err)));
|
|
214
242
|
});
|
|
215
243
|
|
|
216
|
-
return resultPromise;
|
|
244
|
+
return await resultPromise;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Whether the verifier has failed for good: its bb process or result stream broke, so every proof in flight was
|
|
249
|
+
* rejected unchecked, as is every proof enqueued from now on. A failed verifier never recovers; replace it.
|
|
250
|
+
*/
|
|
251
|
+
public isFailed(): boolean {
|
|
252
|
+
return this.fatalError !== undefined;
|
|
217
253
|
}
|
|
218
254
|
|
|
219
255
|
public async stop(): Promise<void> {
|
|
220
256
|
this.logger.info('Stopping BatchChonkVerifier');
|
|
221
257
|
this.stopped = true;
|
|
258
|
+
this.inFlightSlots.cancel();
|
|
222
259
|
|
|
223
260
|
try {
|
|
224
261
|
// Stop accepting new proofs and flush the send queue. Bound it so an unresponsive
|
|
@@ -308,12 +345,20 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
308
345
|
return;
|
|
309
346
|
}
|
|
310
347
|
this.pendingRequests.delete(result.request_id);
|
|
311
|
-
|
|
348
|
+
this.inFlightSlots.release();
|
|
349
|
+
if (pending.probe) {
|
|
350
|
+
pending.resolve({ valid: result.status === 0, durationMs: 0, totalDurationMs: 0 });
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
312
353
|
|
|
313
354
|
const valid = result.status === 0; // VerifyStatus::OK
|
|
314
355
|
const durationMs = result.time_in_verify_ms;
|
|
315
356
|
const totalDurationMs = pending.totalTimer.ms();
|
|
316
357
|
|
|
358
|
+
// Every FAILED result is a verdict on the proof, whatever its message. bb reports a malformed proof (an off-curve
|
|
359
|
+
// or non-canonical commitment) as an exception it caught, worded like a failure of its own, so treating those as
|
|
360
|
+
// anything else would let anyone swap a garbage proof into a real tx without penalty. A bb death surfaces through
|
|
361
|
+
// the result stream failing instead.
|
|
317
362
|
const ivcResult: IVCProofVerificationResult = { valid, durationMs, totalDurationMs };
|
|
318
363
|
this.metrics.recordIVCVerification(ivcResult);
|
|
319
364
|
|
|
@@ -348,8 +393,8 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
348
393
|
private rejectPendingRequests(error: Error): void {
|
|
349
394
|
for (const [id, pending] of Array.from(this.pendingRequests)) {
|
|
350
395
|
pending.reject(error);
|
|
351
|
-
clearTimeout(pending.timeout);
|
|
352
396
|
this.pendingRequests.delete(id);
|
|
397
|
+
this.inFlightSlots.release();
|
|
353
398
|
}
|
|
354
399
|
this.notifyPendingDrained();
|
|
355
400
|
}
|
|
@@ -358,6 +403,7 @@ export class BatchChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
|
358
403
|
if (!this.fatalError) {
|
|
359
404
|
this.fatalError = error;
|
|
360
405
|
}
|
|
406
|
+
this.inFlightSlots.cancel();
|
|
361
407
|
this.rejectPendingRequests(error);
|
|
362
408
|
}
|
|
363
409
|
|
package/src/verifier/index.ts
CHANGED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { CHONK_PROOF_LENGTH } from '@aztec-labs/constants';
|
|
2
|
+
import { LruSet } from '@aztec-labs/foundation/collection';
|
|
3
|
+
import { createLogger } from '@aztec-labs/foundation/log';
|
|
4
|
+
import { promiseWithResolvers } from '@aztec-labs/foundation/promise';
|
|
5
|
+
import { ReadWriteLock } from '@aztec-labs/foundation/queue';
|
|
6
|
+
import { InterruptibleSleep } from '@aztec-labs/foundation/sleep';
|
|
7
|
+
import { Timer } from '@aztec-labs/foundation/timer';
|
|
8
|
+
import type { ClientProtocolCircuitVerifier, IVCProofVerificationResult } from '@aztec-labs/stdlib/interfaces/server';
|
|
9
|
+
import type { Tx } from '@aztec-labs/stdlib/tx';
|
|
10
|
+
import { createHash } from 'node:crypto';
|
|
11
|
+
|
|
12
|
+
import type { BBConfig } from '../config.js';
|
|
13
|
+
import { BatchChonkVerifier } from './batch_chonk_verifier.js';
|
|
14
|
+
|
|
15
|
+
/** A verifier that, once failed, stays failed: {@link BatchChonkVerifier}, or a double of it in tests. */
|
|
16
|
+
export interface FailingChonkVerifier extends ClientProtocolCircuitVerifier {
|
|
17
|
+
/** Whether the verifier has failed permanently and must be replaced. */
|
|
18
|
+
isFailed(): boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Keeps a {@link BatchChonkVerifier} running by replacing it whenever its bb process dies, so callers only ever get a
|
|
23
|
+
* verdict on a proof.
|
|
24
|
+
*
|
|
25
|
+
* The verifier holds a session on one bb process (verification keys, worker pool, result FIFO), and a bb death leaves
|
|
26
|
+
* it failed for good. Everything that session holds comes from configuration, so a fresh verifier restores it exactly.
|
|
27
|
+
*
|
|
28
|
+
* A bb death fails every proof in flight at once, without saying which of them caused it. So each of those is checked
|
|
29
|
+
* again on its own, with no other proof in flight (see {@link verifyProof}), and a death then is down to that proof: it
|
|
30
|
+
* is invalid, and remembered, so it doesn't crash bb again when it is sent again. A bb that dies on every proof looks
|
|
31
|
+
* the same from here, so a proof is only blamed once bb has given a verdict on some other proof since it last blamed
|
|
32
|
+
* one, and waits for one until then. A bb that never works therefore blames nothing (and verifies nothing, which the
|
|
33
|
+
* verification metrics show), rather than giving anyone who can crash it a way to shut the node down.
|
|
34
|
+
*
|
|
35
|
+
* A bb that cannot be started at all is nothing the node can work around, so it exits for its orchestrator to restart
|
|
36
|
+
* it. Replacements are made at most once a `replaceIntervalMs`.
|
|
37
|
+
*/
|
|
38
|
+
export class SelfHealingChonkVerifier implements ClientProtocolCircuitVerifier {
|
|
39
|
+
/** How many proofs that crashed bb are remembered. */
|
|
40
|
+
static readonly CRASHED_PROOFS_REMEMBERED = 1024;
|
|
41
|
+
|
|
42
|
+
private readonly logger = createLogger('bb-prover:self_healing_chonk_verifier');
|
|
43
|
+
/** The verifier verifications go to, or the creation of its replacement. */
|
|
44
|
+
private current: Promise<FailingChonkVerifier>;
|
|
45
|
+
/** The verifier in use, which stop() stops; unset while its replacement is being created. */
|
|
46
|
+
private started: FailingChonkVerifier | undefined;
|
|
47
|
+
/** When the verifier was last replaced, as a `Date.now()` timestamp in ms; 0 if it never has been. */
|
|
48
|
+
private lastReplacedAtMs = 0;
|
|
49
|
+
private stopped = false;
|
|
50
|
+
/** Wakes verifications waiting for the next replacement when the verifier stops. */
|
|
51
|
+
private readonly sleeper = new InterruptibleSleep();
|
|
52
|
+
/**
|
|
53
|
+
* Every check holds this lock: shared normally, so bb batches proofs together, and exclusive for a proof checked on
|
|
54
|
+
* its own after a bb death, so that no other proof is in flight with it.
|
|
55
|
+
*/
|
|
56
|
+
private readonly checkLock = new ReadWriteLock();
|
|
57
|
+
/** Proofs found to crash bb, by the hash of their tx, so they are answered without sending them to bb again. */
|
|
58
|
+
private readonly crashedProofs = new LruSet<string>(SelfHealingChonkVerifier.CRASHED_PROOFS_REMEMBERED);
|
|
59
|
+
/** Whether bb has given a verdict on any proof since a proof was last blamed for crashing it. */
|
|
60
|
+
private verdictSinceLastBlame = false;
|
|
61
|
+
/** Resolved when bb next gives a verdict, or the verifier stops. */
|
|
62
|
+
private nextVerdict = promiseWithResolvers<void>();
|
|
63
|
+
|
|
64
|
+
constructor(
|
|
65
|
+
private readonly create: () => Promise<FailingChonkVerifier>,
|
|
66
|
+
first: FailingChonkVerifier,
|
|
67
|
+
private readonly label: string,
|
|
68
|
+
private readonly replaceIntervalMs = 1000,
|
|
69
|
+
private readonly exitNode: (err: unknown) => never = exitBecauseBbWontStart,
|
|
70
|
+
) {
|
|
71
|
+
this.started = first;
|
|
72
|
+
this.current = Promise.resolve(first);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Start a batch verifier for the protocol circuits, replaced whenever it fails. */
|
|
76
|
+
static async new(config: BBConfig, batchSize: number, label: string): Promise<SelfHealingChonkVerifier> {
|
|
77
|
+
const create = () => BatchChonkVerifier.new(config, batchSize, label);
|
|
78
|
+
return new SelfHealingChonkVerifier(create, await create(), label);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Verifies a tx's Chonk proof. Answers valid or invalid, and rejects only once stopped or on an error other than bb
|
|
83
|
+
* dying:
|
|
84
|
+
* 1. The proof is checked alongside any others, so bb batches it with them.
|
|
85
|
+
* 2. If bb dies with it in flight, it is checked again on its own: proofs already in flight finish first, new ones
|
|
86
|
+
* wait once its turn starts, and proofs checked on their own go one at a time, in order.
|
|
87
|
+
* 3. If bb dies on it again, it is blamed and answered invalid, provided bb has given a verdict on some other proof
|
|
88
|
+
* since it last blamed one. Otherwise it waits for such a verdict and goes back to step 2.
|
|
89
|
+
*/
|
|
90
|
+
public async verifyProof(tx: Tx): Promise<IVCProofVerificationResult> {
|
|
91
|
+
const timer = new Timer();
|
|
92
|
+
// A tx with no proof is invalid without asking bb, and must not count as bb giving a verdict.
|
|
93
|
+
if (tx.chonkProof.fields.length !== CHONK_PROOF_LENGTH) {
|
|
94
|
+
return { valid: false, durationMs: 0, totalDurationMs: timer.ms() };
|
|
95
|
+
}
|
|
96
|
+
if (this.crashedBefore(tx)) {
|
|
97
|
+
return { valid: false, durationMs: 0, totalDurationMs: timer.ms() };
|
|
98
|
+
}
|
|
99
|
+
const result = await this.checkLock.runShared(() => this.verifyOnLiveVerifier(tx));
|
|
100
|
+
if (result) {
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
for (;;) {
|
|
104
|
+
const result = await this.checkLock.runExclusive(() => this.verifyOnItsOwn(tx, timer));
|
|
105
|
+
if (result) {
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
this.logger.warn(
|
|
109
|
+
'Chonk verifier went down on a proof checked on its own, but has given no verdict since it last did',
|
|
110
|
+
{ label: this.label, txHash: tx.getTxHash().toString() },
|
|
111
|
+
);
|
|
112
|
+
await this.nextVerdict.promise;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public async stop(): Promise<void> {
|
|
117
|
+
this.stopped = true;
|
|
118
|
+
this.sleeper.interrupt();
|
|
119
|
+
this.nextVerdict.resolve();
|
|
120
|
+
await this.started?.stop();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Checks a proof with no other proof in flight, and blames it if bb dies on it. Both happen within its exclusive
|
|
125
|
+
* turn, so a copy of the same tx checked next is answered from `crashedProofs` rather than crashing bb again. Returns
|
|
126
|
+
* undefined if bb died but has given no verdict since it last blamed a proof, so nothing can be blamed yet.
|
|
127
|
+
*/
|
|
128
|
+
private async verifyOnItsOwn(tx: Tx, timer: Timer): Promise<IVCProofVerificationResult | undefined> {
|
|
129
|
+
if (this.crashedBefore(tx)) {
|
|
130
|
+
return { valid: false, durationMs: 0, totalDurationMs: timer.ms() };
|
|
131
|
+
}
|
|
132
|
+
const result = await this.verifyOnLiveVerifier(tx);
|
|
133
|
+
if (result || !this.verdictSinceLastBlame) {
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
this.logger.warn('Proof took the Chonk verifier down when checked on its own', {
|
|
137
|
+
label: this.label,
|
|
138
|
+
txHash: tx.getTxHash().toString(),
|
|
139
|
+
});
|
|
140
|
+
this.verdictSinceLastBlame = false;
|
|
141
|
+
this.crashedProofs.add(crashKey(tx));
|
|
142
|
+
return { valid: false, durationMs: 0, totalDurationMs: timer.ms() };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Whether a copy of this tx has crashed bb. Skips the hash, which serializes the whole tx, until some proof has. */
|
|
146
|
+
private crashedBefore(tx: Tx): boolean {
|
|
147
|
+
return this.crashedProofs.size > 0 && this.crashedProofs.has(crashKey(tx));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Verifies on the live verifier. Returns undefined if that verifier died with the proof in flight, so the proof was
|
|
152
|
+
* never checked.
|
|
153
|
+
*/
|
|
154
|
+
private async verifyOnLiveVerifier(tx: Tx): Promise<IVCProofVerificationResult | undefined> {
|
|
155
|
+
const verifier = await this.liveVerifier();
|
|
156
|
+
try {
|
|
157
|
+
const result = await verifier.verifyProof(tx);
|
|
158
|
+
this.verdictSinceLastBlame = true;
|
|
159
|
+
this.nextVerdict.resolve();
|
|
160
|
+
this.nextVerdict = promiseWithResolvers<void>();
|
|
161
|
+
return result;
|
|
162
|
+
} catch (err) {
|
|
163
|
+
if (verifier.isFailed() && !this.stopped) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
throw err;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private async liveVerifier(): Promise<FailingChonkVerifier> {
|
|
171
|
+
for (;;) {
|
|
172
|
+
const current = this.current;
|
|
173
|
+
const verifier = await current;
|
|
174
|
+
if (this.stopped) {
|
|
175
|
+
throw new Error('Chonk verifier stopped');
|
|
176
|
+
}
|
|
177
|
+
if (!verifier.isFailed()) {
|
|
178
|
+
return verifier;
|
|
179
|
+
}
|
|
180
|
+
// Only the first caller to find this verifier failed replaces it; the rest share the replacement. Either way the
|
|
181
|
+
// loop checks the replacement too, since it may have failed before the proof gets to it.
|
|
182
|
+
if (this.current !== current) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const waitMs = this.lastReplacedAtMs + this.replaceIntervalMs - Date.now();
|
|
186
|
+
if (waitMs > 0) {
|
|
187
|
+
await this.sleeper.sleep(waitMs);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
this.lastReplacedAtMs = Date.now();
|
|
191
|
+
this.logger.warn('Replacing the failed Chonk verifier', { label: this.label });
|
|
192
|
+
this.started = undefined;
|
|
193
|
+
void this.retire(verifier);
|
|
194
|
+
this.current = this.startReplacement();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private async startReplacement(): Promise<FailingChonkVerifier> {
|
|
199
|
+
let verifier: FailingChonkVerifier;
|
|
200
|
+
try {
|
|
201
|
+
verifier = await this.create();
|
|
202
|
+
} catch (err) {
|
|
203
|
+
if (this.stopped) {
|
|
204
|
+
throw new Error('Chonk verifier stopped');
|
|
205
|
+
}
|
|
206
|
+
this.exitNode(err);
|
|
207
|
+
}
|
|
208
|
+
if (this.stopped) {
|
|
209
|
+
// stop() did not wait for this one, so it stops itself.
|
|
210
|
+
await this.retire(verifier);
|
|
211
|
+
throw new Error('Chonk verifier stopped');
|
|
212
|
+
}
|
|
213
|
+
this.started = verifier;
|
|
214
|
+
this.logger.info('Replaced the failed Chonk verifier', { label: this.label });
|
|
215
|
+
return verifier;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private async retire(verifier: FailingChonkVerifier): Promise<void> {
|
|
219
|
+
try {
|
|
220
|
+
await verifier.stop();
|
|
221
|
+
} catch (err) {
|
|
222
|
+
this.logger.warn('Error stopping a replaced Chonk verifier', { label: this.label, err });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Identifies a tx by its full contents: its hash does not cover the proof, so it could be reused with an honest one. */
|
|
228
|
+
function crashKey(tx: Tx): string {
|
|
229
|
+
return createHash('sha256').update(tx.toBuffer()).digest('hex');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Exits the process because no bb process could be started to replace a dead one. No proof is involved in starting
|
|
234
|
+
* bb, so the environment is broken: crash, and let the orchestrator restart the node.
|
|
235
|
+
*/
|
|
236
|
+
function exitBecauseBbWontStart(err: unknown): never {
|
|
237
|
+
createLogger('bb-prover:self_healing_chonk_verifier').fatal(
|
|
238
|
+
'Could not start a bb process to replace the failed Chonk verifier; exiting',
|
|
239
|
+
err,
|
|
240
|
+
);
|
|
241
|
+
process.exit(1);
|
|
242
|
+
}
|