Skip to content

Commit 8674b4f

Browse files
authored
Add context of the resolved circuit to the cache (#55)
* Added context of the resolved circuit to the cache * Fixed typo in test * Reverted changes. Reverted context caching. Cached parsing result
1 parent 120bd7a commit 8674b4f

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/hardhat-zkit",
3-
"version": "0.4.13",
3+
"version": "0.4.14",
44
"description": "The ultimate TypeScript environment for Circom development",
55
"main": "dist/src/index.js",
66
"types": "dist/src/index.d.ts",

src/cache/schemas/compile-schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const TemplateSchema = z.object({
2828
parameters: z.string().array(),
2929
isCustom: z.boolean(),
3030
parallel: z.boolean(),
31+
parsedInputs: z.record(z.string(), InputDataSchema).optional(),
3132
});
3233

3334
export const TemplatesSchema = z.record(z.string(), TemplateSchema);

src/core/dependencies/CircomFilesResolver.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
validateSourceNameFormat,
1414
} from "hardhat/utils/source-names";
1515

16-
import { HardhatError, assertHardhatInvariant } from "hardhat/internal/core/errors";
16+
import { assertHardhatInvariant, HardhatError } from "hardhat/internal/core/errors";
1717
import { ERRORS } from "hardhat/internal/core/errors-list";
1818
import { getRealPath } from "hardhat/internal/util/fs-utils";
1919
import { createNonCryptographicHashBasedIdentifier } from "hardhat/internal/util/hash";
@@ -24,8 +24,8 @@ import { HardhatZKitError } from "../../errors";
2424

2525
import {
2626
CircomResolvedFile as ICircomResolvedFile,
27-
ResolvedMainComponentData,
2827
ResolvedFileData,
28+
ResolvedMainComponentData,
2929
SignalType,
3030
VisibilityType,
3131
} from "../../types/core";
@@ -249,11 +249,12 @@ export class CircomFilesResolver {
249249
},
250250
);
251251

252-
const parsedInputs = this._parser.parseTemplateInputs(
253-
fileWithTemplate,
254-
templateName,
255-
mainComponentData.parameters,
256-
);
252+
if (!fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs) {
253+
fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs =
254+
this._parser.parseTemplateInputs(fileWithTemplate, templateName, mainComponentData.parameters);
255+
}
256+
257+
const parsedInputs = fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs!;
257258

258259
for (const key of Object.keys(parsedInputs)) {
259260
const signalType: SignalType = this._getSignalType(parsedInputs[key].type);

src/types/core/dependencies/parser/circom-files-visitor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export type Template = {
5959
isCustom: boolean;
6060
parallel: boolean;
6161
context: TemplateDefinitionContext;
62+
parsedInputs?: Record<string, InputData>;
6263
};
6364

6465
export type Templates = {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pragma circom 2.0.0;
2+
3+
include "base/mul2Base.circom";
4+
5+
component main = Multiplier2();

test/integration/tasks.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ describe("ZKit tasks", async function () {
379379
"contracts",
380380
"generated-types",
381381
"hardhat.config.ts",
382+
"mock-circuits",
382383
"package.json",
383384
]);
384385

test/unit/cache/circuits-compile-cache.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getNormalizedFullPath } from "@src/utils/path-utils";
99

1010
import { defaultCompileFlags } from "../../constants";
1111
import { CompileCacheEntry } from "@src/types/cache";
12-
import { TASK_CIRCUITS_COMPILE, ZKIT_SCOPE_NAME } from "@src/task-names";
12+
import { TASK_CIRCUITS_COMPILE, TASK_CIRCUITS_MAKE, ZKIT_SCOPE_NAME } from "@src/task-names";
1313
import { CIRCUITS_COMPILE_CACHE_FILENAME, CIRCUIT_COMPILE_CACHE_VERSION } from "@src/constants";
1414

1515
import { CircuitsCompileCache, createCircuitsCompileCache, resetCircuitsCompileCache } from "@src/cache";
@@ -107,4 +107,21 @@ describe("CircuitsCompileCache", () => {
107107
fsExtra.rmSync(typesDir, { recursive: true, force: true });
108108
});
109109
});
110+
111+
describe("context-caching", () => {
112+
useEnvironment("with-circuits", true);
113+
114+
it("should correctly cache the context", async function () {
115+
await this.hre.run({ scope: ZKIT_SCOPE_NAME, task: TASK_CIRCUITS_MAKE });
116+
117+
const mockCircuitSourcePath = getNormalizedFullPath(this.hre.config.paths.root, "mock-circuits/newMul2.circom");
118+
const mockCircuitDestinationPath = getNormalizedFullPath(this.hre.config.paths.root, "circuits/newMul2.circom");
119+
120+
fsExtra.copyFileSync(mockCircuitSourcePath, mockCircuitDestinationPath);
121+
122+
await expect(this.hre.run({ scope: ZKIT_SCOPE_NAME, task: TASK_CIRCUITS_MAKE })).to.be.fulfilled;
123+
124+
fsExtra.rmSync(mockCircuitDestinationPath);
125+
});
126+
});
110127
});

0 commit comments

Comments
 (0)