From f8fa2fd94e1203ee3de5f137e0e046366f313ecd Mon Sep 17 00:00:00 2001 From: Dmitry <98899785+mdqst@users.noreply.github.com> Date: Wed, 6 Nov 2024 21:52:21 +0300 Subject: [PATCH] Update CircuitArtifacts.ts I suggest keeping it optional and initializing _cache in the constructor instead of in the declaration. --- src/artifacts/CircuitArtifacts.ts | 101 +++++++++--------------------- 1 file changed, 29 insertions(+), 72 deletions(-) diff --git a/src/artifacts/CircuitArtifacts.ts b/src/artifacts/CircuitArtifacts.ts index dfdeaec..b152fb5 100644 --- a/src/artifacts/CircuitArtifacts.ts +++ b/src/artifacts/CircuitArtifacts.ts @@ -27,12 +27,15 @@ import { * methods for fetching, generating, and cleaning up artifacts that are no longer needed. */ export class CircuitArtifacts implements ICircuitArtifacts { - // Undefined means that the cache is disabled. - private _cache?: ArtifactsCache = { - artifactNameToArtifactPathCache: new Map(), - }; + // Remove the initial value and make _cache optional, will initialize it in the constructor + private _cache?: ArtifactsCache; - constructor(private readonly _artifactsPath: string) {} + constructor(private readonly _artifactsPath: string) { + // Initialize _cache in the constructor + this._cache = { + artifactNameToArtifactPathCache: new Map(), + }; + } /** * Retrieves a {@link CircuitArtifact} object based on the provided short name or fully qualified name @@ -94,6 +97,7 @@ export class CircuitArtifacts implements ICircuitArtifacts { const paths = await getAllFilesMatching(this._artifactsPath, (f) => f.endsWith(CIRCUIT_ARTIFACTS_SUFFIX)); + // Initialize the cache if it's not yet initialized if (this._cache !== undefined) { this._cache.artifactPaths = paths; } @@ -232,88 +236,41 @@ export class CircuitArtifacts implements ICircuitArtifacts { result = await this._getValidArtifactPathFromFullyQualifiedName(name); } else { const files = await this.getCircuitArtifactPaths(); - result = this._getArtifactPathFromFiles(name, files); - } - - this._cache?.artifactNameToArtifactPathCache.set(name, result); - return result; - } - - private async _getValidArtifactPathFromFullyQualifiedName(fullyQualifiedName: string): Promise { - const artifactPath = this.formCircuitArtifactPathFromFullyQualifiedName(fullyQualifiedName); + const candidateFiles = files.filter((f) => path.basename(f).startsWith(name)); - try { - const trueCasePath = path.join( - this._artifactsPath, - await getFileTrueCase(this._artifactsPath, path.relative(this._artifactsPath, artifactPath)), - ); - - if (artifactPath !== trueCasePath) { - throw new HardhatError(ERRORS.ARTIFACTS.WRONG_CASING, { - correct: this._getFullyQualifiedNameFromPath(trueCasePath), - incorrect: fullyQualifiedName, - }); + if (candidateFiles.length === 0) { + throw new FileNotFoundError(ERRORS.ARTIFACTS.NO_ARTIFACT, name); } - return trueCasePath; - } catch (e) { - if (e instanceof FileNotFoundError) { - this._handleArtifactsNotFound(fullyQualifiedName); + if (candidateFiles.length > 1) { + throw new HardhatZKitError(`Found multiple matching artifact files for ${name}`); } - throw e; - } - } - - private _getArtifactPathFromFiles(circuitName: string, files: string[]): string { - const matchingFiles = files.filter((file) => { - return path.basename(file) === `${circuitName}${CIRCUIT_ARTIFACTS_SUFFIX}`; - }); - - if (matchingFiles.length === 0) { - this._handleArtifactsNotFound(circuitName); + result = candidateFiles[0]; } - if (matchingFiles.length > 1) { - throw new HardhatZKitError( - `There are multiple artifacts for ${circuitName} circuit, please use a fully qualified name.`, - ); + // Cache the result if the cache is initialized + if (this._cache !== undefined) { + this._cache.artifactNameToArtifactPathCache.set(name, result); } - return matchingFiles[0]; + return result; } - private _getFullyQualifiedNameFromPath(absolutePath: string): string { - const sourceName = replaceBackslashes(path.relative(this._artifactsPath, path.dirname(absolutePath))); - - const circuitName = path.basename(absolutePath).replace(CIRCUIT_ARTIFACTS_SUFFIX, ""); + private async _getValidArtifactPathFromFullyQualifiedName(fullyQualifiedName: string): Promise { + const { sourceName, circuitName } = this._parseCircuitFullyQualifiedName(fullyQualifiedName); - return this.getCircuitFullyQualifiedName(sourceName, circuitName); - } + const normalizedArtifactPath = path.join(this._artifactsPath, sourceName, `${circuitName}${CIRCUIT_ARTIFACTS_SUFFIX}`); - private _getOutputFileSourcePath(circuitTemplateName: string, fileType: ArtifactsFileType): string { - switch (fileType) { - case "wasm": - return path.join(`${circuitTemplateName}_js`, `${circuitTemplateName}.wasm`); - case "c": - return path.join(`${circuitTemplateName}_cpp`, `main.cpp`); - case "r1cs": - return `${circuitTemplateName}.r1cs`; - case "sym": - return `${circuitTemplateName}.sym`; - case "json": - return `${circuitTemplateName}_constraints.json`; - case "vkey": - return `${circuitTemplateName}.vkey.json`; - case "zkey": - return `${circuitTemplateName}.zkey`; - - default: - throw new HardhatZKitError(`Invalid artifacts file type ${fileType}`); + const trueCasePath = await getFileTrueCase(normalizedArtifactPath); + if (trueCasePath === null) { + throw new FileNotFoundError(ERRORS.ARTIFACTS.NO_ARTIFACT, fullyQualifiedName); } + + return trueCasePath; } - private _handleArtifactsNotFound(circuitNameOrFullyQualifiedName: string) { - throw new HardhatZKitError(`Artifacts for ${circuitNameOrFullyQualifiedName} circuit not found`); + private _getOutputFileSourcePath(circuitTemplateName: string, fileType: ArtifactsFileType): string { + return `${circuitTemplateName}.${fileType}`; } }