-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Update the default outputSelection setting of solc to decrease the artifacts size
#8121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54fca53
Update default solc outputSelection setting
alcuadrado e345a69
Update tests
alcuadrado 0f1038c
Add changeset
alcuadrado 621a5a8
Update ignition tests
alcuadrado 7a48ac0
Update changeset
alcuadrado 12a3a2e
Test that it still works with older versions
alcuadrado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "hardhat": patch | ||
| --- | ||
|
|
||
| Update the default outputSelection setting of solc to decrease the artifacts size. | ||
|
|
||
| NOTE: This change can lead to build info ids changing, despite compilation output's bytecodes being identical. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/hardhat/src/internal/builtin-plugins/solidity/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { CompilerInput } from "../../../types/solidity.js"; | ||
|
|
||
| export const DEFAULT_OUTPUT_SELECTION: CompilerInput["settings"]["outputSelection"] = | ||
| { | ||
| "*": { | ||
| "": ["ast"], | ||
| "*": [ | ||
| "abi", | ||
| "evm.bytecode.linkReferences", | ||
| "evm.bytecode.object", | ||
| "evm.bytecode.opcodes", | ||
| "evm.bytecode.sourceMap", | ||
| "evm.deployedBytecode.immutableReferences", | ||
| "evm.deployedBytecode.linkReferences", | ||
| "evm.deployedBytecode.object", | ||
| "evm.deployedBytecode.opcodes", | ||
| "evm.deployedBytecode.sourceMap", | ||
| "evm.methodIdentifiers", | ||
| ], | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
packages/hardhat/test/internal/builtin-plugins/solidity/build-system/output-selection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import type { CompilerOutput } from "../../../../../src/types/solidity.js"; | ||
|
|
||
| import assert from "node:assert/strict"; | ||
| import path from "node:path"; | ||
| import { describe, it } from "node:test"; | ||
|
|
||
| import { gte } from "semver"; | ||
|
|
||
| import { createHardhatRuntimeEnvironment } from "../../../../../src/internal/hre-initialization.js"; | ||
|
|
||
| import { useTestProjectTemplate } from "./resolver/helpers.js"; | ||
|
|
||
| const FIRST_VERSION_WITH_IMMUTABLE_REFERENCES = "0.6.5"; | ||
|
|
||
| const CONTRACTS: Record<string, string> = { | ||
| // Oldest version supported by hardhat | ||
| "0.4.11": `pragma solidity ^0.4.11;\ncontract Foo {}`, | ||
| // Last version without immutable references | ||
| "0.6.4": `pragma solidity ^0.6.4;\ncontract Foo {}`, | ||
| // First version with immutable references | ||
| "0.6.5": [ | ||
| "pragma solidity ^0.6.5;", | ||
| "contract Foo {", | ||
| " uint256 public immutable x;", | ||
| " constructor() public { x = 1; }", | ||
| "}", | ||
| ].join("\n"), | ||
| // Modern version | ||
| "0.8.28": [ | ||
| "// SPDX-License-Identifier: UNLICENSED", | ||
| "pragma solidity ^0.8.0;", | ||
| "contract Foo {", | ||
| " uint256 public immutable x;", | ||
| " constructor() { x = 1; }", | ||
| "}", | ||
| ].join("\n"), | ||
| }; | ||
|
|
||
| /** | ||
| * Returns the first contract output from a CompilerOutput. | ||
| */ | ||
| function getContractOutput(output: CompilerOutput) { | ||
| assert(output.contracts !== undefined, "Expected contracts in output"); | ||
|
|
||
| const sourceEntries = Object.values(output.contracts); | ||
| assert(sourceEntries.length > 0, "Expected at least one source in output"); | ||
|
|
||
| const contractEntries = Object.values(sourceEntries[0]); | ||
| assert( | ||
| contractEntries.length > 0, | ||
| "Expected at least one contract in output", | ||
| ); | ||
|
|
||
| return contractEntries[0]; | ||
| } | ||
|
|
||
| describe( | ||
| "Default output selection compatibility across solc versions", | ||
| { skip: process.env.HARDHAT_DISABLE_SLOW_TESTS === "true" }, | ||
| () => { | ||
| for (const [version, contractSource] of Object.entries(CONTRACTS)) { | ||
| it(`should produce expected output fields with solc ${version}`, async () => { | ||
| await using project = await useTestProjectTemplate({ | ||
| name: "output-selection-test", | ||
| version: "1.0.0", | ||
| files: { | ||
| "contracts/Foo.sol": contractSource, | ||
| }, | ||
| }); | ||
|
|
||
| const hre = await createHardhatRuntimeEnvironment( | ||
| { solidity: { version } }, | ||
| {}, | ||
| project.path, | ||
| ); | ||
|
|
||
| const rootFilePath = path.join(project.path, "contracts/Foo.sol"); | ||
|
|
||
| const jobsResult = await hre.solidity.getCompilationJobs( | ||
| [rootFilePath], | ||
| { force: true, quiet: true }, | ||
| ); | ||
|
|
||
| assert(jobsResult.success, `getCompilationJobs failed for ${version}`); | ||
|
|
||
| const compilationJob = jobsResult.compilationJobsPerFile | ||
| .values() | ||
| .next().value; | ||
| assert(compilationJob !== undefined, "Expected a compilation job"); | ||
|
|
||
| const { output } = await hre.solidity.runCompilationJob( | ||
| compilationJob, | ||
| { quiet: true }, | ||
| ); | ||
|
|
||
| const errors = (output.errors ?? []).filter( | ||
| (e) => e.severity === "error", | ||
| ); | ||
| assert.equal( | ||
| errors.length, | ||
| 0, | ||
| `Compilation errors with solc ${version}: ${errors.map((e) => e.message).join(", ")}`, | ||
| ); | ||
|
|
||
| const contract = getContractOutput(output); | ||
|
|
||
| assert(contract.abi !== undefined, "Expected abi in output"); | ||
|
|
||
| assert(contract.evm !== undefined, "Expected evm in output"); | ||
|
|
||
| assert( | ||
| contract.evm.bytecode !== undefined, | ||
| "Expected evm.bytecode in output", | ||
| ); | ||
| assert( | ||
| typeof contract.evm.bytecode.object === "string", | ||
| "Expected evm.bytecode.object", | ||
| ); | ||
| assert( | ||
| typeof contract.evm.bytecode.opcodes === "string", | ||
| "Expected evm.bytecode.opcodes", | ||
| ); | ||
| assert( | ||
| typeof contract.evm.bytecode.sourceMap === "string", | ||
| "Expected evm.bytecode.sourceMap", | ||
| ); | ||
| assert( | ||
| contract.evm.bytecode.linkReferences !== undefined, | ||
| "Expected evm.bytecode.linkReferences", | ||
| ); | ||
|
|
||
| assert( | ||
| contract.evm.deployedBytecode !== undefined, | ||
| "Expected evm.deployedBytecode in output", | ||
| ); | ||
| assert( | ||
| typeof contract.evm.deployedBytecode.object === "string", | ||
| "Expected evm.deployedBytecode.object", | ||
| ); | ||
| assert( | ||
| typeof contract.evm.deployedBytecode.opcodes === "string", | ||
| "Expected evm.deployedBytecode.opcodes", | ||
| ); | ||
| assert( | ||
| typeof contract.evm.deployedBytecode.sourceMap === "string", | ||
| "Expected evm.deployedBytecode.sourceMap", | ||
| ); | ||
| assert( | ||
| contract.evm.deployedBytecode.linkReferences !== undefined, | ||
| "Expected evm.deployedBytecode.linkReferences", | ||
| ); | ||
|
|
||
| assert( | ||
| contract.evm.methodIdentifiers !== undefined, | ||
| "Expected evm.methodIdentifiers in output", | ||
| ); | ||
|
|
||
| const supportsImmutables = gte( | ||
| version, | ||
| FIRST_VERSION_WITH_IMMUTABLE_REFERENCES, | ||
| ); | ||
|
|
||
| if (supportsImmutables) { | ||
| assert( | ||
| contract.evm.deployedBytecode.immutableReferences !== undefined, | ||
| `Expected evm.deployedBytecode.immutableReferences in solc ${version} output`, | ||
| ); | ||
| } else { | ||
| assert.equal( | ||
| contract.evm.deployedBytecode.immutableReferences, | ||
| undefined, | ||
| `Did not expect evm.deployedBytecode.immutableReferences in solc ${version} output`, | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default outputSelection now uses granular selectors (e.g. evm.deployedBytecode.immutableReferences, evm.bytecode.sourceMap, etc.). Since Hardhat supports compiling a wide range of solc versions (including older 0.4.x/0.5.x via solc-js), some of these selector paths may not be accepted by older compilers. Please add a regression test that compiles with the oldest supported solc version using the default settings (or gate/fallback the selector list by solc version) so compilation doesn’t fail on older versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, i added tests for this