|
| 1 | +import { inject, injectable, singleton } from "tsyringe"; |
| 2 | +import { |
| 3 | + AreProofsEnabled, |
| 4 | + mapSequential, |
| 5 | + MOCK_VERIFICATION_KEY, |
| 6 | +} from "@proto-kit/common"; |
| 7 | + |
| 8 | +import { |
| 9 | + Artifact, |
| 10 | + AtomicCompileHelper, |
| 11 | + GenericCompileTarget, |
| 12 | +} from "./AtomicCompileHelper"; |
| 13 | +import { CompilableModule } from "./CompilableModule"; |
| 14 | + |
| 15 | +/** |
| 16 | + * The CompileRegistry compiles "compilable modules" |
| 17 | + * (i.e. zkprograms, contracts or contractmodules) |
| 18 | + * while making sure they don't get compiled twice in the same process in parallel. |
| 19 | + */ |
| 20 | +@injectable() |
| 21 | +@singleton() |
| 22 | +export class CompileRegistry { |
| 23 | + public constructor( |
| 24 | + @inject("AreProofsEnabled") |
| 25 | + private readonly areProofsEnabled: AreProofsEnabled |
| 26 | + ) { |
| 27 | + this.compile = new AtomicCompileHelper(this.areProofsEnabled); |
| 28 | + } |
| 29 | + |
| 30 | + public compile: AtomicCompileHelper; |
| 31 | + |
| 32 | + private artifacts: Record<string, Artifact | "compiled-but-no-artifact"> = {}; |
| 33 | + |
| 34 | + public async compileModule<ReturnArtifact extends Artifact>( |
| 35 | + // TODO Make name inferred by the module token |
| 36 | + name: string, |
| 37 | + compile: ( |
| 38 | + registry: CompileRegistry, |
| 39 | + ...args: unknown[] |
| 40 | + ) => GenericCompileTarget<ReturnArtifact>, |
| 41 | + dependencies: Record<string, CompilableModule> = {} |
| 42 | + ): Promise<ReturnArtifact | undefined> { |
| 43 | + const collectedArtifacts = await mapSequential( |
| 44 | + Object.entries(dependencies), |
| 45 | + async ([depName, dep]) => { |
| 46 | + if (this.artifacts[depName] !== undefined) { |
| 47 | + return this.artifacts[depName]; |
| 48 | + } |
| 49 | + const artifact = |
| 50 | + (await dep.compile(this)) ?? "compiled-but-no-artifact"; |
| 51 | + this.artifacts[depName] = artifact; |
| 52 | + return artifact; |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + const target = compile(this, ...collectedArtifacts); |
| 57 | + const artifact = await this.compile.program<ReturnArtifact>(name, target); |
| 58 | + |
| 59 | + this.artifacts[name] = artifact ?? "compiled-but-no-artifact"; |
| 60 | + |
| 61 | + return artifact; |
| 62 | + } |
| 63 | + |
| 64 | + public getArtifact<ArtifactType extends Artifact>(name: string) { |
| 65 | + if (this.artifacts[name] === undefined) { |
| 66 | + throw new Error( |
| 67 | + `Artifact for ${name} not available, did you compile it via the CompileRegistry?` |
| 68 | + ); |
| 69 | + } |
| 70 | + if (!this.areProofsEnabled.areProofsEnabled) { |
| 71 | + return MOCK_VERIFICATION_KEY; |
| 72 | + } |
| 73 | + |
| 74 | + const artifact = this.artifacts[name]; |
| 75 | + if (artifact === "compiled-but-no-artifact") { |
| 76 | + throw new Error( |
| 77 | + `Module ${name} didn't return the requested artifact even though proofs are enabled` |
| 78 | + ); |
| 79 | + } |
| 80 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 81 | + return artifact as ArtifactType; |
| 82 | + } |
| 83 | + |
| 84 | + public addArtifactsRaw(artifacts: Record<string, Artifact>) { |
| 85 | + Object.entries(artifacts).forEach(([key, value]) => { |
| 86 | + this.artifacts[key] = value; |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
0 commit comments