-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCompileRegistry.ts
More file actions
78 lines (66 loc) · 2.02 KB
/
CompileRegistry.ts
File metadata and controls
78 lines (66 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { inject, injectable, singleton } from "tsyringe";
import {
AreProofsEnabled,
CompileArtifact,
} from "../zkProgrammable/ZkProgrammable";
import {
ArtifactRecord,
AtomicCompileHelper,
CompileTarget,
} from "./AtomicCompileHelper";
/**
* The CompileRegistry compiles "compilable modules"
* (i.e. zkprograms, contracts or contractmodules)
* while making sure they don't get compiled twice in the same process in parallel.
*/
@injectable()
@singleton()
export class CompileRegistry {
public constructor(
@inject("AreProofsEnabled")
private readonly areProofsEnabled: AreProofsEnabled
) {
this.compiler = new AtomicCompileHelper(this.areProofsEnabled);
}
private compiler: AtomicCompileHelper;
private artifacts: ArtifactRecord = {};
private inForceProverBlock = false;
/**
* This function forces compilation even if the artifact itself is in the registry.
* Basically the statement is: The artifact along is not enough, we need to
* actually have the prover compiled.
* This is true for non-sideloaded circuit dependencies.
*/
public async forceProverExists(
f: (registry: CompileRegistry) => Promise<void>
) {
this.inForceProverBlock = true;
await f(this);
this.inForceProverBlock = false;
}
public async compile(target: CompileTarget) {
if (this.artifacts[target.name] === undefined || this.inForceProverBlock) {
const artifact = await this.compiler.compileContract(target);
this.artifacts[target.name] = artifact;
return artifact;
}
return this.artifacts[target.name];
}
public getArtifact(name: string): CompileArtifact | undefined {
if (this.artifacts[name] === undefined) {
throw new Error(
`Artifact for ${name} not available, did you compile it via the CompileRegistry?`
);
}
return this.artifacts[name];
}
public addArtifactsRaw(artifacts: ArtifactRecord) {
this.artifacts = {
...this.artifacts,
...artifacts,
};
}
public getAllArtifacts() {
return this.artifacts;
}
}