@@ -27,12 +27,15 @@ import {
27
27
* methods for fetching, generating, and cleaning up artifacts that are no longer needed.
28
28
*/
29
29
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 ;
34
32
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
+ }
36
39
37
40
/**
38
41
* Retrieves a {@link CircuitArtifact} object based on the provided short name or fully qualified name
@@ -94,6 +97,7 @@ export class CircuitArtifacts implements ICircuitArtifacts {
94
97
95
98
const paths = await getAllFilesMatching ( this . _artifactsPath , ( f ) => f . endsWith ( CIRCUIT_ARTIFACTS_SUFFIX ) ) ;
96
99
100
+ // Initialize the cache if it's not yet initialized
97
101
if ( this . _cache !== undefined ) {
98
102
this . _cache . artifactPaths = paths ;
99
103
}
@@ -232,88 +236,41 @@ export class CircuitArtifacts implements ICircuitArtifacts {
232
236
result = await this . _getValidArtifactPathFromFullyQualifiedName ( name ) ;
233
237
} else {
234
238
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 ) ) ;
244
240
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 ) ;
256
243
}
257
244
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 } ` ) ;
262
247
}
263
248
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 ] ;
275
250
}
276
251
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 ) ;
281
255
}
282
256
283
- return matchingFiles [ 0 ] ;
257
+ return result ;
284
258
}
285
259
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 ) ;
290
262
291
- return this . getCircuitFullyQualifiedName ( sourceName , circuitName ) ;
292
- }
263
+ const normalizedArtifactPath = path . join ( this . _artifactsPath , sourceName , `${ circuitName } ${ CIRCUIT_ARTIFACTS_SUFFIX } ` ) ;
293
264
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 ) ;
313
268
}
269
+
270
+ return trueCasePath ;
314
271
}
315
272
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 } ` ;
318
275
}
319
276
}
0 commit comments