Skip to content

Commit

Permalink
Update CircuitArtifacts.ts
Browse files Browse the repository at this point in the history
I suggest keeping it optional and initializing _cache in the constructor instead of in the declaration.
  • Loading branch information
mdqst authored Nov 6, 2024
1 parent 120bd7a commit f8fa2fd
Showing 1 changed file with 29 additions and 72 deletions.
101 changes: 29 additions & 72 deletions src/artifacts/CircuitArtifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<string> {
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<string> {
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}`;
}
}

0 comments on commit f8fa2fd

Please sign in to comment.