|
| 1 | +import { inject, injectable, singleton } from "tsyringe"; |
| 2 | + |
| 3 | +import { AreProofsEnabled } from "../zkProgrammable/ZkProgrammable"; |
| 4 | + |
| 5 | +import { |
| 6 | + ArtifactRecord, |
| 7 | + AtomicCompileHelper, |
| 8 | + CompileTarget, |
| 9 | +} from "./AtomicCompileHelper"; |
| 10 | +import { CompilableModule } from "./CompilableModule"; |
| 11 | + |
| 12 | +interface GenericCompilableModule<Artifact> { |
| 13 | + compile(registry: CompileRegistry): Promise<Artifact>; |
| 14 | +} |
| 15 | + |
| 16 | +export type InferDependencyArtifacts< |
| 17 | + Dependencies extends Record<string, CompilableModule>, |
| 18 | +> = { |
| 19 | + [Key in keyof Dependencies]: Dependencies[Key] extends GenericCompilableModule< |
| 20 | + infer Artifact |
| 21 | + > |
| 22 | + ? Artifact |
| 23 | + : void; |
| 24 | +}; |
| 25 | +/** |
| 26 | + * The CompileRegistry compiles "compilable modules" |
| 27 | + * (i.e. zkprograms, contracts or contractmodules) |
| 28 | + * while making sure they don't get compiled twice in the same process in parallel. |
| 29 | + */ |
| 30 | +@injectable() |
| 31 | +@singleton() |
| 32 | +export class CompileRegistry { |
| 33 | + public constructor( |
| 34 | + @inject("AreProofsEnabled") |
| 35 | + private readonly areProofsEnabled: AreProofsEnabled |
| 36 | + ) { |
| 37 | + this.compiler = new AtomicCompileHelper(this.areProofsEnabled); |
| 38 | + } |
| 39 | + |
| 40 | + private compiler: AtomicCompileHelper; |
| 41 | + |
| 42 | + private artifacts: ArtifactRecord = {}; |
| 43 | + |
| 44 | + // private cachedModuleOutputs: Record<string, ArtifactRecord | void> = {}; |
| 45 | + |
| 46 | + // TODO Add possibility to force recompilation for non-sideloaded dependencies |
| 47 | + |
| 48 | + public async compile(target: CompileTarget) { |
| 49 | + if (this.artifacts[target.name] === undefined) { |
| 50 | + const artifact = await this.compiler.compileContract(target); |
| 51 | + this.artifacts[target.name] = artifact; |
| 52 | + return artifact; |
| 53 | + } |
| 54 | + return this.artifacts[target.name]; |
| 55 | + } |
| 56 | + |
| 57 | + // public async compileModule< |
| 58 | + // ReturnType extends ArtifactRecord, |
| 59 | + // Dependencies extends Record<string, CompilableModule>, |
| 60 | + // >( |
| 61 | + // compile: ( |
| 62 | + // compiler: AtomicCompileHelper, |
| 63 | + // args: InferDependencyArtifacts<Dependencies> |
| 64 | + // ) => Promise<ReturnType>, |
| 65 | + // dependencies?: Dependencies |
| 66 | + // ): Promise<ReturnType | undefined> { |
| 67 | + // const collectedArtifacts = await mapSequential( |
| 68 | + // Object.entries(dependencies ?? {}), |
| 69 | + // async ([depName, dep]) => { |
| 70 | + // if (this.cachedModuleOutputs[depName] !== undefined) { |
| 71 | + // return [depName, this.cachedModuleOutputs[depName]]; |
| 72 | + // } |
| 73 | + // const artifact = await dep.compile(this); |
| 74 | + // if (artifact !== undefined) { |
| 75 | + // this.artifacts = { |
| 76 | + // ...this.artifacts, |
| 77 | + // ...artifact, |
| 78 | + // }; |
| 79 | + // } |
| 80 | + // this.cachedModuleOutputs[depName] = artifact; |
| 81 | + // return [depName, artifact]; |
| 82 | + // } |
| 83 | + // ); |
| 84 | + // |
| 85 | + // const artifacts = await compile( |
| 86 | + // this.compile, |
| 87 | + // Object.fromEntries(collectedArtifacts) |
| 88 | + // ); |
| 89 | + // |
| 90 | + // this.artifacts = { |
| 91 | + // ...this.artifacts, |
| 92 | + // ...artifacts, |
| 93 | + // }; |
| 94 | + // |
| 95 | + // return artifacts; |
| 96 | + // } |
| 97 | + |
| 98 | + public getArtifact(name: string) { |
| 99 | + if (this.artifacts[name] === undefined) { |
| 100 | + throw new Error( |
| 101 | + `Artifact for ${name} not available, did you compile it via the CompileRegistry?` |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return this.artifacts[name]; |
| 106 | + } |
| 107 | + |
| 108 | + public addArtifactsRaw(artifacts: ArtifactRecord) { |
| 109 | + this.artifacts = { |
| 110 | + ...this.artifacts, |
| 111 | + ...artifacts, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + public getAllArtifacts() { |
| 116 | + return this.artifacts; |
| 117 | + } |
| 118 | +} |
0 commit comments