|
| 1 | +import { |
| 2 | + AreProofsEnabled, |
| 3 | + CompileArtifact, |
| 4 | + MOCK_VERIFICATION_KEY, |
| 5 | +} from "../zkProgrammable/ZkProgrammable"; |
| 6 | +import { isSubtypeOfName } from "../utils"; |
| 7 | +import { TypedClass } from "../types"; |
| 8 | +import { log } from "../log"; |
| 9 | + |
| 10 | +export type ArtifactRecord = Record<string, CompileArtifact>; |
| 11 | + |
| 12 | +export type CompileTarget = { |
| 13 | + name: string; |
| 14 | + compile: () => Promise<CompileArtifact>; |
| 15 | +}; |
| 16 | + |
| 17 | +export class AtomicCompileHelper { |
| 18 | + public constructor(private readonly areProofsEnabled: AreProofsEnabled) {} |
| 19 | + |
| 20 | + private compilationPromises: { |
| 21 | + [key: string]: Promise<CompileArtifact>; |
| 22 | + } = {}; |
| 23 | + |
| 24 | + public async compileContract( |
| 25 | + contract: CompileTarget, |
| 26 | + overrideProofsEnabled?: boolean |
| 27 | + ): Promise<CompileArtifact> { |
| 28 | + let newPromise = false; |
| 29 | + const { name } = contract; |
| 30 | + if (this.compilationPromises[name] === undefined) { |
| 31 | + const proofsEnabled = |
| 32 | + overrideProofsEnabled ?? this.areProofsEnabled.areProofsEnabled; |
| 33 | + |
| 34 | + // We only care about proofs enabled here if it's a contract, because |
| 35 | + // in all other cases, ZkProgrammable already handles this switch, and we |
| 36 | + // want to preserve the artifact layout (which might be more than one |
| 37 | + // entry for ZkProgrammables) |
| 38 | + if ( |
| 39 | + proofsEnabled || |
| 40 | + !isSubtypeOfName( |
| 41 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 42 | + contract as unknown as TypedClass<any>, |
| 43 | + "SmartContract" |
| 44 | + ) |
| 45 | + ) { |
| 46 | + log.time(`Compiling ${name}`); |
| 47 | + this.compilationPromises[name] = contract.compile(); |
| 48 | + newPromise = true; |
| 49 | + } else { |
| 50 | + log.trace(`Compiling ${name} - mock`); |
| 51 | + this.compilationPromises[name] = Promise.resolve({ |
| 52 | + verificationKey: MOCK_VERIFICATION_KEY, |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | + const result = await this.compilationPromises[name]; |
| 57 | + if (newPromise) { |
| 58 | + log.timeEnd.info(`Compiling ${name}`); |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | +} |
0 commit comments