diff --git a/yarn-project/bb-prover/src/prover/bb_prover.ts b/yarn-project/bb-prover/src/prover/bb_prover.ts index 5870bfb13e2..ad39a749448 100644 --- a/yarn-project/bb-prover/src/prover/bb_prover.ts +++ b/yarn-project/bb-prover/src/prover/bb_prover.ts @@ -464,7 +464,7 @@ export class BBNativeRollupProver implements ServerCircuitProver { private async generateAvmProofWithBB(input: AvmCircuitInputs, workingDirectory: string): Promise { logger.debug(`Proving avm-circuit...`); - const provingResult = await generateAvmProof(this.config.bbBinaryPath, workingDirectory, input, logger.debug); + const provingResult = await generateAvmProof(this.config.bbBinaryPath, workingDirectory, input, logger.verbose); if (provingResult.status === BB_RESULT.FAILURE) { logger.error(`Failed to generate proof for avm-circuit: ${provingResult.reason}`); @@ -475,7 +475,11 @@ export class BBNativeRollupProver implements ServerCircuitProver { } private async createAvmProof(input: AvmCircuitInputs): Promise { + const cleanupDir: boolean = !process.env.AVM_PROVING_PRESERVE_WORKING_DIR; const operation = async (bbWorkingDirectory: string): Promise => { + if (!cleanupDir) { + logger.info(`Preserving working directory ${bbWorkingDirectory}`); + } const provingResult = await this.generateAvmProofWithBB(input, bbWorkingDirectory); const rawProof = await fs.readFile(provingResult.proofPath!); @@ -503,7 +507,7 @@ export class BBNativeRollupProver implements ServerCircuitProver { return { proof, verificationKey }; }; - return await runInDirectory(this.config.bbWorkingDirectory, operation); + return await runInDirectory(this.config.bbWorkingDirectory, operation, cleanupDir); } /** diff --git a/yarn-project/foundation/src/fs/run_in_dir.ts b/yarn-project/foundation/src/fs/run_in_dir.ts index 174be3fc583..7b39b27aca8 100644 --- a/yarn-project/foundation/src/fs/run_in_dir.ts +++ b/yarn-project/foundation/src/fs/run_in_dir.ts @@ -1,18 +1,23 @@ -import { randomBytes } from 'crypto'; import * as fs from 'fs/promises'; +import * as path from 'path'; // Create a random directory underneath a 'base' directory // Calls a provided method, ensures the random directory is cleaned up afterwards -export async function runInDirectory(workingDirBase: string, fn: (dir: string) => Promise): Promise { +export async function runInDirectory( + workingDirBase: string, + fn: (dir: string) => Promise, + cleanup: boolean = true, +): Promise { // Create random directory to be used for temp files - const workingDirectory = `${workingDirBase}/${randomBytes(8).toString('hex')}`; - await fs.mkdir(workingDirectory, { recursive: true }); + const workingDirectory = await fs.mkdtemp(path.join(workingDirBase, 'tmp-')); await fs.access(workingDirectory); try { return await fn(workingDirectory); } finally { - await fs.rm(workingDirectory, { recursive: true, force: true }); + if (cleanup) { + await fs.rm(workingDirectory, { recursive: true, force: true }); + } } }