Skip to content

Commit

Permalink
Merge pull request #772 from NomicFoundation/verify-circular-imports
Browse files Browse the repository at this point in the history
Handle circular imports in verify loop
  • Loading branch information
zoeyTM authored Jun 3, 2024
2 parents dbea0f5 + ccfdcca commit 5a6187c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
15 changes: 12 additions & 3 deletions packages/core/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ function resolveChainConfig(
return chainConfig;
}

function getImportSourceNames(
export function getImportSourceNames(
sourceName: string,
buildInfo: BuildInfo
buildInfo: BuildInfo,
visited: Record<string, boolean> = {}
): string[] {
if (visited[sourceName]) {
return [];
}

visited[sourceName] = true;

const contractSource = buildInfo.input.sources[sourceName].content;
const { imports } = analyze(contractSource);

Expand All @@ -109,7 +116,9 @@ function getImportSourceNames(

return [
...importSources,
...importSources.flatMap((i) => getImportSourceNames(i, buildInfo)),
...importSources.flatMap((i) =>
getImportSourceNames(i, buildInfo, visited)
),
];
}

Expand Down
73 changes: 72 additions & 1 deletion packages/core/test/verify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { assert } from "chai";
import path from "path";

import { VerifyResult, getVerificationInformation } from "../src";
import { BuildInfo, VerifyResult, getVerificationInformation } from "../src";
import { getImportSourceNames } from "../src/verify";

describe("verify", () => {
it("should not verify an unitialized deployment", async () => {
Expand Down Expand Up @@ -201,4 +202,74 @@ describe("verify", () => {
assert.deepEqual(actualSources, expectedSources);
}
});

describe("getImportSourceNames", () => {
const exampleBuildInfo: BuildInfo = {
_format: "hh-sol-artifact-1",
id: "example",
solcVersion: "0.8.19",
solcLongVersion: "0.8.19+commit.7dd6d404",
input: {
language: "Solidity",
settings: {
optimizer: {},
outputSelection: {},
},
sources: {},
},
output: {
contracts: {},
sources: {},
},
};

it("should handle circular imports", () => {
const buildInfo: BuildInfo = {
...exampleBuildInfo,
input: {
...exampleBuildInfo.input,
sources: {
"contracts/A.sol": {
content: 'import "./B.sol";',
},
"contracts/B.sol": {
content: 'import "./A.sol";',
},
},
},
};

const result = getImportSourceNames("contracts/A.sol", buildInfo);

assert.deepEqual(result, ["contracts/B.sol", "contracts/A.sol"]);
});

it("should handle indirect circular imports", () => {
const buildInfo: BuildInfo = {
...exampleBuildInfo,
input: {
...exampleBuildInfo.input,
sources: {
"contracts/A.sol": {
content: 'import "./B.sol";',
},
"contracts/B.sol": {
content: 'import "./C.sol";',
},
"contracts/C.sol": {
content: 'import "./A.sol";',
},
},
},
};

const result = getImportSourceNames("contracts/A.sol", buildInfo);

assert.deepEqual(result, [
"contracts/B.sol",
"contracts/C.sol",
"contracts/A.sol",
]);
});
});
});

0 comments on commit 5a6187c

Please sign in to comment.