Skip to content

Commit

Permalink
feat(avm): support preserving BB working dir for better debugging (#6990
Browse files Browse the repository at this point in the history
)

`AVM_PROVING_PRESERVE_WORKING_DIR=1 yarn test
src/e2e_prover/full.test.ts`

will now preserve the working dir, so that later you can just run the bb
command

```
  bb-prover INFO Preserving working directory /tmp/bb-XXgzVD/tmp-WrMwBo
  bb-prover AvmCircuit (prove) BB out - Executing BB with: avm_prove --avm-bytecode /tmp/bb-XXgzVD/tmp-WrMwBo/avm_bytecode.bin --avm-calldata /tmp/bb-XXgzVD/tmp-WrMwBo/avm_calldata.bin --avm-public-inputs /tmp/bb-XXgzVD/tmp-WrMwBo/avm_public_inputs.bin --avm-hints /tmp/bb-XXgzVD/tmp-WrMwBo/avm_hints.bin -o /tmp/bb-XXgzVD/tmp-WrMwBo
  bb-prover AvmCircuit (prove) BB out - terminate called after throwing an instance of 'std::out_of_range'
  what():  unordered_map::at
  bb-prover ERROR Failed to generate proof for avm-circuit: Failed to generate proof. Exit code null. Signal SIGABRT.
```
  • Loading branch information
fcarreiro authored Jun 10, 2024
1 parent e11f880 commit a9688f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 6 additions & 2 deletions yarn-project/bb-prover/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
private async generateAvmProofWithBB(input: AvmCircuitInputs, workingDirectory: string): Promise<BBSuccess> {
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}`);
Expand All @@ -475,7 +475,11 @@ export class BBNativeRollupProver implements ServerCircuitProver {
}

private async createAvmProof(input: AvmCircuitInputs): Promise<ProofAndVerificationKey> {
const cleanupDir: boolean = !process.env.AVM_PROVING_PRESERVE_WORKING_DIR;
const operation = async (bbWorkingDirectory: string): Promise<ProofAndVerificationKey> => {
if (!cleanupDir) {
logger.info(`Preserving working directory ${bbWorkingDirectory}`);
}
const provingResult = await this.generateAvmProofWithBB(input, bbWorkingDirectory);

const rawProof = await fs.readFile(provingResult.proofPath!);
Expand Down Expand Up @@ -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);
}

/**
Expand Down
15 changes: 10 additions & 5 deletions yarn-project/foundation/src/fs/run_in_dir.ts
Original file line number Diff line number Diff line change
@@ -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<T>(workingDirBase: string, fn: (dir: string) => Promise<T>): Promise<T> {
export async function runInDirectory<T>(
workingDirBase: string,
fn: (dir: string) => Promise<T>,
cleanup: boolean = true,
): Promise<T> {
// 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 });
}
}
}

0 comments on commit a9688f0

Please sign in to comment.