Skip to content

Commit

Permalink
Add context of the resolved circuit to the cache (#55)
Browse files Browse the repository at this point in the history
* Added context of the resolved circuit to the cache

* Fixed typo in test

* Reverted changes. Reverted context caching. Cached parsing result
  • Loading branch information
KyrylR authored Nov 9, 2024
1 parent 120bd7a commit 8674b4f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-zkit",
"version": "0.4.13",
"version": "0.4.14",
"description": "The ultimate TypeScript environment for Circom development",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/cache/schemas/compile-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const TemplateSchema = z.object({
parameters: z.string().array(),
isCustom: z.boolean(),
parallel: z.boolean(),
parsedInputs: z.record(z.string(), InputDataSchema).optional(),
});

export const TemplatesSchema = z.record(z.string(), TemplateSchema);
Expand Down
15 changes: 8 additions & 7 deletions src/core/dependencies/CircomFilesResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
validateSourceNameFormat,
} from "hardhat/utils/source-names";

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

import {
CircomResolvedFile as ICircomResolvedFile,
ResolvedMainComponentData,
ResolvedFileData,
ResolvedMainComponentData,
SignalType,
VisibilityType,
} from "../../types/core";
Expand Down Expand Up @@ -249,11 +249,12 @@ export class CircomFilesResolver {
},
);

const parsedInputs = this._parser.parseTemplateInputs(
fileWithTemplate,
templateName,
mainComponentData.parameters,
);
if (!fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs) {
fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs =
this._parser.parseTemplateInputs(fileWithTemplate, templateName, mainComponentData.parameters);
}

const parsedInputs = fileWithTemplate.fileData.parsedFileData.templates[templateName].parsedInputs!;

for (const key of Object.keys(parsedInputs)) {
const signalType: SignalType = this._getSignalType(parsedInputs[key].type);
Expand Down
1 change: 1 addition & 0 deletions src/types/core/dependencies/parser/circom-files-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type Template = {
isCustom: boolean;
parallel: boolean;
context: TemplateDefinitionContext;
parsedInputs?: Record<string, InputData>;
};

export type Templates = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.0.0;

include "base/mul2Base.circom";

component main = Multiplier2();
1 change: 1 addition & 0 deletions test/integration/tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ describe("ZKit tasks", async function () {
"contracts",
"generated-types",
"hardhat.config.ts",
"mock-circuits",
"package.json",
]);

Expand Down
19 changes: 18 additions & 1 deletion test/unit/cache/circuits-compile-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getNormalizedFullPath } from "@src/utils/path-utils";

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

import { CircuitsCompileCache, createCircuitsCompileCache, resetCircuitsCompileCache } from "@src/cache";
Expand Down Expand Up @@ -107,4 +107,21 @@ describe("CircuitsCompileCache", () => {
fsExtra.rmSync(typesDir, { recursive: true, force: true });
});
});

describe("context-caching", () => {
useEnvironment("with-circuits", true);

it("should correctly cache the context", async function () {
await this.hre.run({ scope: ZKIT_SCOPE_NAME, task: TASK_CIRCUITS_MAKE });

const mockCircuitSourcePath = getNormalizedFullPath(this.hre.config.paths.root, "mock-circuits/newMul2.circom");
const mockCircuitDestinationPath = getNormalizedFullPath(this.hre.config.paths.root, "circuits/newMul2.circom");

fsExtra.copyFileSync(mockCircuitSourcePath, mockCircuitDestinationPath);

await expect(this.hre.run({ scope: ZKIT_SCOPE_NAME, task: TASK_CIRCUITS_MAKE })).to.be.fulfilled;

fsExtra.rmSync(mockCircuitDestinationPath);
});
});
});

0 comments on commit 8674b4f

Please sign in to comment.