Skip to content

Commit

Permalink
chore: Add env var to disable bb cleanup
Browse files Browse the repository at this point in the history
Adds a new `BB_SKIP_CLEANUP` boolean env var to stop bb native prover
from cleaning up tmp dirs. Useful for retrieving files after a failed
run.
  • Loading branch information
spalladino committed Aug 13, 2024
1 parent 33d47d2 commit 99a761d
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions yarn-project/bb-prover/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export interface BBConfig {
bbBinaryPath: string;
bbWorkingDirectory: string;
/** Whether to skip tmp dir cleanup for debugging purposes */
bbSkipCleanup?: boolean;
}

export interface ACVMConfig {
Expand Down
13 changes: 9 additions & 4 deletions yarn-project/bb-prover/src/prover/bb_private_kernel_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
constructor(
private bbBinaryPath: string,
private bbWorkingDirectory: string,
private skipCleanup: boolean,
private log = createDebugLogger('aztec:bb-native-prover'),
) {}

Expand Down Expand Up @@ -119,7 +120,7 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
const operation = async (directory: string) => {
return await this._createClientIvcProof(directory, acirs, witnessStack);
};
return await runInDirectory(this.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

public getSiloedCommitments(publicInputs: PrivateCircuitPublicInputs) {
Expand Down Expand Up @@ -189,7 +190,7 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
return await this.computeVerificationKey(directory, bytecode, 'App', appCircuitName);
};

return await runInDirectory(this.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

/**
Expand Down Expand Up @@ -229,7 +230,7 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
await fs.writeFile(verificationKeyPath, verificationKey);
return await verifyProof(this.bbBinaryPath, proofFileName, verificationKeyPath!, logFunction);
};
return await runInDirectory(this.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

/**
Expand Down Expand Up @@ -270,7 +271,7 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
} satisfies CircuitWitnessGenerationStats);

// TODO(#7410) we dont need to generate vk's for these circuits, they are in the vk tree
const { verificationKey } = await runInDirectory(this.bbWorkingDirectory, dir =>
const { verificationKey } = await this.runInDirectory(dir =>
this.computeVerificationKey(dir, Buffer.from(compiledCircuit.bytecode, 'base64'), circuitType),
);
const kernelOutput: PrivateKernelSimulateOutput<O> = {
Expand Down Expand Up @@ -363,4 +364,8 @@ export class BBNativePrivateKernelProver implements PrivateKernelProver {
);
return proof;
}

private runInDirectory<T>(fn: (dir: string) => Promise<T>) {
return runInDirectory(this.bbWorkingDirectory, fn, this.skipCleanup);
}
}
16 changes: 10 additions & 6 deletions yarn-project/bb-prover/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {

return { circuitOutput: output, proof };
};
return await runInDirectory(this.config.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

private async generateAvmProofWithBB(input: AvmCircuitInputs, workingDirectory: string): Promise<BBSuccess> {
Expand Down Expand Up @@ -608,7 +608,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {

return { proof, verificationKey };
};
return await runInDirectory(this.config.bbWorkingDirectory, operation, cleanupDir);
return await this.runInDirectory(operation);
}

public async getTubeProof(
Expand Down Expand Up @@ -640,7 +640,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {

return { tubeVK, tubeProof };
};
return await runInDirectory(this.config.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

/**
Expand Down Expand Up @@ -699,7 +699,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
proof,
};
};
return await runInDirectory(this.config.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

/**
Expand Down Expand Up @@ -752,7 +752,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
logger.info(`Successfully verified proof from key in ${result.durationMs} ms`);
};

await runInDirectory(this.config.bbWorkingDirectory, operation);
await this.runInDirectory(operation);
}

/**
Expand Down Expand Up @@ -824,7 +824,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
true,
);
};
return await runInDirectory(this.config.bbWorkingDirectory, operation);
return await this.runInDirectory(operation);
}

/**
Expand Down Expand Up @@ -958,4 +958,8 @@ export class BBNativeRollupProver implements ServerCircuitProver {

return proof;
}

private runInDirectory<T>(fn: (dir: string) => Promise<T>) {
return runInDirectory(this.config.bbWorkingDirectory, fn, this.config.bbSkipCleanup);
}
}
4 changes: 2 additions & 2 deletions yarn-project/bb-prover/src/verifier/bb_verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class BBCircuitVerifier implements ClientProtocolCircuitVerifier {
proofType: 'ultra-honk',
} satisfies CircuitVerificationStats);
};
await runInDirectory(this.config.bbWorkingDirectory, operation);
await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup);
}

public async generateSolidityContract(circuit: ProtocolArtifact, contractName: string) {
Expand Down Expand Up @@ -168,7 +168,7 @@ export class BBCircuitVerifier implements ClientProtocolCircuitVerifier {
proofType: 'client-ivc',
} satisfies CircuitVerificationStats);
};
await runInDirectory(this.config.bbWorkingDirectory, operation);
await runInDirectory(this.config.bbWorkingDirectory, operation, this.config.bbSkipCleanup);
return true;
} catch (err) {
this.logger.warn(`Failed to verify ClientIVC proof for tx ${Tx.getHash(tx)}: ${String(err)}`);
Expand Down
1 change: 1 addition & 0 deletions yarn-project/foundation/src/config/env_var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ export type EnvVar =
| 'PXE_DATA_DIRECTORY'
| 'BB_BINARY_PATH'
| 'BB_WORKING_DIRECTORY'
| 'BB_SKIP_CLEANUP'
| 'PXE_PROVER_ENABLED';
4 changes: 2 additions & 2 deletions yarn-project/foundation/src/fs/run_in_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from 'path';
export async function runInDirectory<T>(
workingDirBase: string,
fn: (dir: string) => Promise<T>,
cleanup: boolean = true,
skipCleanup: boolean | undefined,
): Promise<T> {
// Create random directory to be used for temp files
const workingDirectory = await fs.mkdtemp(path.join(workingDirBase, 'tmp-'));
Expand All @@ -16,7 +16,7 @@ export async function runInDirectory<T>(
try {
return await fn(workingDirectory);
} finally {
if (cleanup) {
if (!skipCleanup) {
await fs.rm(workingDirectory, { recursive: true, force: true });
}
}
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/pxe/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { fileURLToPath } from 'url';
export interface BBProverConfig {
bbWorkingDirectory?: string;
bbBinaryPath?: string;
bbSkipCleanup?: boolean;
}

/**
Expand Down Expand Up @@ -72,6 +73,11 @@ export const pxeConfigMappings: ConfigMappingsType<PXEServiceConfig> = {
env: 'BB_WORKING_DIRECTORY',
description: 'Working directory for the BB binary',
},
bbSkipCleanup: {
env: 'BB_SKIP_CLEANUP',
description: 'True to skip cleanup of temporary files for debugging purposes',
...booleanConfigHelper(),
},
proverEnabled: {
env: 'PXE_PROVER_ENABLED',
description: 'Enable real proofs',
Expand Down
1 change: 1 addition & 0 deletions yarn-project/pxe/src/pxe_service/create_pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function createPXEService(
: new BBNativePrivateKernelProver(
config.bbBinaryPath!,
config.bbWorkingDirectory!,
!!config.bbSkipCleanup,
createDebugLogger('aztec:pxe:bb-native-prover' + (logSuffix ? `:${logSuffix}` : '')),
);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/providers/acvm_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@ export class NativeACVMSimulator implements SimulationProvider {
return result.witness;
};

return await runInDirectory(this.workingDirectory, operation);
return await runInDirectory(this.workingDirectory, operation, false);
}
}

0 comments on commit 99a761d

Please sign in to comment.