Skip to content

Commit f8fa2fd

Browse files
authored
Update CircuitArtifacts.ts
I suggest keeping it optional and initializing _cache in the constructor instead of in the declaration.
1 parent 120bd7a commit f8fa2fd

File tree

1 file changed

+29
-72
lines changed

1 file changed

+29
-72
lines changed

src/artifacts/CircuitArtifacts.ts

Lines changed: 29 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ import {
2727
* methods for fetching, generating, and cleaning up artifacts that are no longer needed.
2828
*/
2929
export class CircuitArtifacts implements ICircuitArtifacts {
30-
// Undefined means that the cache is disabled.
31-
private _cache?: ArtifactsCache = {
32-
artifactNameToArtifactPathCache: new Map(),
33-
};
30+
// Remove the initial value and make _cache optional, will initialize it in the constructor
31+
private _cache?: ArtifactsCache;
3432

35-
constructor(private readonly _artifactsPath: string) {}
33+
constructor(private readonly _artifactsPath: string) {
34+
// Initialize _cache in the constructor
35+
this._cache = {
36+
artifactNameToArtifactPathCache: new Map(),
37+
};
38+
}
3639

3740
/**
3841
* Retrieves a {@link CircuitArtifact} object based on the provided short name or fully qualified name
@@ -94,6 +97,7 @@ export class CircuitArtifacts implements ICircuitArtifacts {
9497

9598
const paths = await getAllFilesMatching(this._artifactsPath, (f) => f.endsWith(CIRCUIT_ARTIFACTS_SUFFIX));
9699

100+
// Initialize the cache if it's not yet initialized
97101
if (this._cache !== undefined) {
98102
this._cache.artifactPaths = paths;
99103
}
@@ -232,88 +236,41 @@ export class CircuitArtifacts implements ICircuitArtifacts {
232236
result = await this._getValidArtifactPathFromFullyQualifiedName(name);
233237
} else {
234238
const files = await this.getCircuitArtifactPaths();
235-
result = this._getArtifactPathFromFiles(name, files);
236-
}
237-
238-
this._cache?.artifactNameToArtifactPathCache.set(name, result);
239-
return result;
240-
}
241-
242-
private async _getValidArtifactPathFromFullyQualifiedName(fullyQualifiedName: string): Promise<string> {
243-
const artifactPath = this.formCircuitArtifactPathFromFullyQualifiedName(fullyQualifiedName);
239+
const candidateFiles = files.filter((f) => path.basename(f).startsWith(name));
244240

245-
try {
246-
const trueCasePath = path.join(
247-
this._artifactsPath,
248-
await getFileTrueCase(this._artifactsPath, path.relative(this._artifactsPath, artifactPath)),
249-
);
250-
251-
if (artifactPath !== trueCasePath) {
252-
throw new HardhatError(ERRORS.ARTIFACTS.WRONG_CASING, {
253-
correct: this._getFullyQualifiedNameFromPath(trueCasePath),
254-
incorrect: fullyQualifiedName,
255-
});
241+
if (candidateFiles.length === 0) {
242+
throw new FileNotFoundError(ERRORS.ARTIFACTS.NO_ARTIFACT, name);
256243
}
257244

258-
return trueCasePath;
259-
} catch (e) {
260-
if (e instanceof FileNotFoundError) {
261-
this._handleArtifactsNotFound(fullyQualifiedName);
245+
if (candidateFiles.length > 1) {
246+
throw new HardhatZKitError(`Found multiple matching artifact files for ${name}`);
262247
}
263248

264-
throw e;
265-
}
266-
}
267-
268-
private _getArtifactPathFromFiles(circuitName: string, files: string[]): string {
269-
const matchingFiles = files.filter((file) => {
270-
return path.basename(file) === `${circuitName}${CIRCUIT_ARTIFACTS_SUFFIX}`;
271-
});
272-
273-
if (matchingFiles.length === 0) {
274-
this._handleArtifactsNotFound(circuitName);
249+
result = candidateFiles[0];
275250
}
276251

277-
if (matchingFiles.length > 1) {
278-
throw new HardhatZKitError(
279-
`There are multiple artifacts for ${circuitName} circuit, please use a fully qualified name.`,
280-
);
252+
// Cache the result if the cache is initialized
253+
if (this._cache !== undefined) {
254+
this._cache.artifactNameToArtifactPathCache.set(name, result);
281255
}
282256

283-
return matchingFiles[0];
257+
return result;
284258
}
285259

286-
private _getFullyQualifiedNameFromPath(absolutePath: string): string {
287-
const sourceName = replaceBackslashes(path.relative(this._artifactsPath, path.dirname(absolutePath)));
288-
289-
const circuitName = path.basename(absolutePath).replace(CIRCUIT_ARTIFACTS_SUFFIX, "");
260+
private async _getValidArtifactPathFromFullyQualifiedName(fullyQualifiedName: string): Promise<string> {
261+
const { sourceName, circuitName } = this._parseCircuitFullyQualifiedName(fullyQualifiedName);
290262

291-
return this.getCircuitFullyQualifiedName(sourceName, circuitName);
292-
}
263+
const normalizedArtifactPath = path.join(this._artifactsPath, sourceName, `${circuitName}${CIRCUIT_ARTIFACTS_SUFFIX}`);
293264

294-
private _getOutputFileSourcePath(circuitTemplateName: string, fileType: ArtifactsFileType): string {
295-
switch (fileType) {
296-
case "wasm":
297-
return path.join(`${circuitTemplateName}_js`, `${circuitTemplateName}.wasm`);
298-
case "c":
299-
return path.join(`${circuitTemplateName}_cpp`, `main.cpp`);
300-
case "r1cs":
301-
return `${circuitTemplateName}.r1cs`;
302-
case "sym":
303-
return `${circuitTemplateName}.sym`;
304-
case "json":
305-
return `${circuitTemplateName}_constraints.json`;
306-
case "vkey":
307-
return `${circuitTemplateName}.vkey.json`;
308-
case "zkey":
309-
return `${circuitTemplateName}.zkey`;
310-
311-
default:
312-
throw new HardhatZKitError(`Invalid artifacts file type ${fileType}`);
265+
const trueCasePath = await getFileTrueCase(normalizedArtifactPath);
266+
if (trueCasePath === null) {
267+
throw new FileNotFoundError(ERRORS.ARTIFACTS.NO_ARTIFACT, fullyQualifiedName);
313268
}
269+
270+
return trueCasePath;
314271
}
315272

316-
private _handleArtifactsNotFound(circuitNameOrFullyQualifiedName: string) {
317-
throw new HardhatZKitError(`Artifacts for ${circuitNameOrFullyQualifiedName} circuit not found`);
273+
private _getOutputFileSourcePath(circuitTemplateName: string, fileType: ArtifactsFileType): string {
274+
return `${circuitTemplateName}.${fileType}`;
318275
}
319276
}

0 commit comments

Comments
 (0)