Skip to content

Commit 5a86d5c

Browse files
authored
Merge pull request #7154 from NomicFoundation/solidity-config-resolution
Solidity Config Resolution Enhancements
2 parents a82d412 + 813237a commit 5a86d5c

File tree

6 files changed

+166
-129
lines changed

6 files changed

+166
-129
lines changed

.changeset/pink-crabs-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Update how production profiles are derived from the default config ([#7154](https://github.com/NomicFoundation/hardhat/pull/7154))

v-next/hardhat-ignition-core/test/helpers/execution-result-fixtures.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export const staticCallResultFixturesArtifacts: {
405405
deployedLinkReferences: {},
406406
immutableReferences: {},
407407
inputSourceName: "project/contracts/C.sol",
408-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
408+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
409409
},
410410
};
411411

@@ -442,7 +442,7 @@ export const deploymentFixturesArtifacts: { [contractName: string]: Artifact } =
442442
deployedLinkReferences: {},
443443
immutableReferences: {},
444444
inputSourceName: "project/contracts/C.sol",
445-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
445+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
446446
},
447447
WithLibrary: {
448448
_format: "hh3-artifact-1",
@@ -471,7 +471,7 @@ export const deploymentFixturesArtifacts: { [contractName: string]: Artifact } =
471471
deployedLinkReferences: {},
472472
immutableReferences: {},
473473
inputSourceName: "project/contracts/C.sol",
474-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
474+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
475475
},
476476
WithAmbiguousLibraryName: {
477477
_format: "hh3-artifact-1",
@@ -508,7 +508,7 @@ export const deploymentFixturesArtifacts: { [contractName: string]: Artifact } =
508508
deployedLinkReferences: {},
509509
immutableReferences: {},
510510
inputSourceName: "project/contracts/C.sol",
511-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
511+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
512512
},
513513
};
514514

@@ -590,7 +590,7 @@ export const callEncodingFixtures: { [contractName: string]: Artifact } = {
590590
deployedLinkReferences: {},
591591
immutableReferences: {},
592592
inputSourceName: "project/contracts/C.sol",
593-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
593+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
594594
},
595595
ToTestEthersEncodingConversion: {
596596
_format: "hh3-artifact-1",
@@ -949,7 +949,7 @@ export const callEncodingFixtures: { [contractName: string]: Artifact } = {
949949
deployedLinkReferences: {},
950950
immutableReferences: {},
951951
inputSourceName: "project/contracts/C.sol",
952-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
952+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
953953
},
954954
FunctionNameValidation: {
955955
_format: "hh3-artifact-1",
@@ -1069,6 +1069,6 @@ export const callEncodingFixtures: { [contractName: string]: Artifact } = {
10691069
deployedLinkReferences: {},
10701070
immutableReferences: {},
10711071
inputSourceName: "project/contracts/C.sol",
1072-
buildInfoId: "solc-0_8_19-a45ba3c37b2deb0d1fadc772202f4cbff6d5a67a",
1072+
buildInfoId: "solc-0_8_19-2fe1cba8ef218c727cd1ce3da3aaa170f8016338",
10731073
},
10741074
};
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
export const DEFAULT_BUILD_PROFILE = "default";
22

3-
export const DEFAULT_BUILD_PROFILES = [
4-
"default",
5-
"production",
6-
"solidity-tests",
7-
"javascript-tests",
8-
] as const;
3+
export const DEFAULT_BUILD_PROFILES = ["default", "production"] as const;

v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compilation-job.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,26 @@ export class CompilationJobImplementation implements CompilationJob {
158158
};
159159
}
160160

161+
const resolvedSettings: CompilerInput["settings"] = {
162+
...settings,
163+
evmVersion:
164+
settings.evmVersion ??
165+
getEvmVersionFromSolcVersion(this.solcConfig.version),
166+
outputSelection: this.#dedupeAndSortOutputSelection(outputSelection),
167+
remappings: this.dependencyGraph.getAllRemappings(),
168+
};
169+
170+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
171+
We just sort the same object to make the builds more consistent */
172+
const sortedSettings = Object.fromEntries(
173+
Object.entries(resolvedSettings).sort(([keyA], [keyB]) =>
174+
keyA.localeCompare(keyB),
175+
),
176+
) as CompilerInput["settings"];
177+
161178
return {
162179
language: "Solidity",
163-
settings: {
164-
...settings,
165-
evmVersion:
166-
settings.evmVersion ??
167-
getEvmVersionFromSolcVersion(this.solcConfig.version),
168-
outputSelection: this.#dedupeAndSortOutputSelection(outputSelection),
169-
remappings: this.dependencyGraph.getAllRemappings(),
170-
},
180+
settings: sortedSettings,
171181
sources,
172182
};
173183
}

v-next/hardhat/src/internal/builtin-plugins/solidity/config.ts

Lines changed: 130 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import type { HardhatUserConfig } from "../../../config.js";
22
import type {
33
HardhatConfig,
4+
MultiVersionSolidityUserConfig,
5+
SingleVersionSolidityUserConfig,
6+
SolcConfig,
7+
SolcUserConfig,
48
SolidityBuildProfileConfig,
59
SolidityConfig,
610
SolidityUserConfig,
711
} from "../../../types/config.js";
812
import type { HardhatUserConfigValidationError } from "../../../types/hooks.js";
913

10-
import { isObject } from "@nomicfoundation/hardhat-utils/lang";
14+
import os from "node:os";
15+
16+
import { deepMerge, isObject } from "@nomicfoundation/hardhat-utils/lang";
1117
import { resolveFromRoot } from "@nomicfoundation/hardhat-utils/path";
1218
import {
1319
conditionalUnionType,
@@ -213,126 +219,57 @@ function resolveSolidityConfig(
213219
solidityConfig = [solidityConfig];
214220
}
215221

222+
// user provided an array of versions or a single version
216223
if (Array.isArray(solidityConfig)) {
217-
return {
218-
profiles: {
219-
default: {
220-
compilers: solidityConfig.map((version) => ({
221-
version,
222-
settings: {},
223-
})),
224-
overrides: {},
225-
isolated: false,
226-
preferWasm: false,
227-
},
228-
},
229-
npmFilesToBuild: [],
224+
const defaultSolidityConfig = {
225+
compilers: solidityConfig.map((version) => ({ version })),
230226
};
231-
}
232-
233-
if ("version" in solidityConfig) {
234227
return {
235228
profiles: {
236-
default: {
237-
compilers: [
238-
{
239-
version: solidityConfig.version,
240-
settings: solidityConfig.settings ?? {},
241-
},
242-
],
243-
overrides: {},
244-
isolated: solidityConfig.isolated ?? false,
245-
preferWasm: solidityConfig.preferWasm ?? false,
246-
},
229+
default: resolveBuildProfileConfig(defaultSolidityConfig),
230+
production: resolveBuildProfileConfig(
231+
copyFromDefault(defaultSolidityConfig),
232+
true,
233+
),
247234
},
248-
npmFilesToBuild: solidityConfig.npmFilesToBuild ?? [],
235+
npmFilesToBuild: [],
249236
};
250237
}
251238

252-
if ("compilers" in solidityConfig) {
239+
// user provided a single version config or a multi version config
240+
if ("version" in solidityConfig || "compilers" in solidityConfig) {
253241
return {
254242
profiles: {
255-
default: {
256-
preferWasm: solidityConfig.preferWasm ?? false,
257-
compilers: solidityConfig.compilers.map((compiler) => ({
258-
version: compiler.version,
259-
settings: compiler.settings ?? {},
260-
})),
261-
overrides: Object.fromEntries(
262-
Object.entries(solidityConfig.overrides ?? {}).map(
263-
([userSourceName, override]) => {
264-
return [
265-
userSourceName,
266-
{
267-
version: override.version,
268-
settings: override.settings ?? {},
269-
},
270-
];
271-
},
272-
),
273-
),
274-
isolated: solidityConfig.isolated ?? false,
275-
},
243+
default: resolveBuildProfileConfig(solidityConfig),
244+
production: resolveBuildProfileConfig(
245+
copyFromDefault(solidityConfig),
246+
true,
247+
),
276248
},
277249
npmFilesToBuild: solidityConfig.npmFilesToBuild ?? [],
278250
};
279251
}
280252

253+
// user provided a build profiles config
281254
const profiles: Record<string, SolidityBuildProfileConfig> = {};
282255

283-
// TODO: Merge the profiles
284256
for (const [profileName, profile] of Object.entries(
285257
solidityConfig.profiles,
286258
)) {
287-
const isolated = profile.isolated ?? profileName === "production";
288-
const preferWasm = profile.preferWasm ?? profileName === "production";
289-
290-
if ("version" in profile) {
291-
profiles[profileName] = {
292-
compilers: [
293-
{
294-
version: profile.version,
295-
settings: profile.settings ?? {},
296-
},
297-
],
298-
overrides: {},
299-
isolated,
300-
preferWasm,
301-
};
302-
continue;
303-
}
304-
305-
profiles[profileName] = {
306-
compilers: profile.compilers.map((compiler) => ({
307-
version: compiler.version,
308-
settings: compiler.settings ?? {},
309-
})),
310-
overrides: Object.fromEntries(
311-
Object.entries(profile.overrides ?? {}).map(
312-
([userSourceName, override]) => {
313-
return [
314-
userSourceName,
315-
{
316-
version: override.version,
317-
settings: override.settings ?? {},
318-
},
319-
];
320-
},
321-
),
322-
),
323-
isolated,
324-
preferWasm,
325-
};
259+
profiles[profileName] = resolveBuildProfileConfig(
260+
profile,
261+
profileName === "production",
262+
);
326263
}
327264

328-
// This will generate default build profiles (e.g. production) when they are not specified in the config, cloning from 'default', which is always present
265+
// This will generate default build profiles (e.g. production) when they are
266+
// not specified in the config, cloning from 'default', which is always present
329267
for (const profile of DEFAULT_BUILD_PROFILES) {
330268
if (!(profile in profiles)) {
331-
profiles[profile] = {
332-
...profiles.default,
333-
isolated: profile === "production",
334-
preferWasm: profile === "production",
335-
};
269+
profiles[profile] = resolveBuildProfileConfig(
270+
copyFromDefault(solidityConfig.profiles.default),
271+
profile === "production",
272+
);
336273
}
337274
}
338275

@@ -341,3 +278,99 @@ function resolveSolidityConfig(
341278
npmFilesToBuild: solidityConfig.npmFilesToBuild ?? [],
342279
};
343280
}
281+
282+
function resolveBuildProfileConfig(
283+
solidityConfig:
284+
| SingleVersionSolidityUserConfig
285+
| MultiVersionSolidityUserConfig,
286+
production: boolean = false,
287+
): SolidityBuildProfileConfig {
288+
if ("version" in solidityConfig) {
289+
return {
290+
compilers: [resolveSolcConfig(solidityConfig, production)],
291+
overrides: {},
292+
isolated: solidityConfig.isolated ?? production,
293+
preferWasm: solidityConfig.preferWasm ?? (production && shouldUseWasm()),
294+
};
295+
}
296+
297+
return {
298+
compilers: solidityConfig.compilers.map((compiler) =>
299+
resolveSolcConfig(compiler, production),
300+
),
301+
overrides: Object.fromEntries(
302+
Object.entries(solidityConfig.overrides ?? {}).map(
303+
([userSourceName, override]) => [
304+
userSourceName,
305+
resolveSolcConfig(override, production),
306+
],
307+
),
308+
),
309+
isolated: solidityConfig.isolated ?? production,
310+
preferWasm: solidityConfig.preferWasm ?? (production && shouldUseWasm()),
311+
};
312+
}
313+
314+
function resolveSolcConfig(
315+
solcConfig: SolcUserConfig,
316+
production: boolean = false,
317+
): SolcConfig {
318+
const defaultSolcConfigSettings: SolcConfig["settings"] = {
319+
outputSelection: {
320+
"*": {
321+
"": ["ast"],
322+
"*": [
323+
"abi",
324+
"evm.bytecode",
325+
"evm.deployedBytecode",
326+
"evm.methodIdentifiers",
327+
"metadata",
328+
],
329+
},
330+
},
331+
};
332+
333+
if (production) {
334+
defaultSolcConfigSettings.optimizer = {
335+
enabled: true,
336+
runs: 200,
337+
};
338+
}
339+
340+
return {
341+
version: solcConfig.version,
342+
settings: deepMerge(defaultSolcConfigSettings, solcConfig.settings ?? {}),
343+
};
344+
}
345+
346+
function copyFromDefault(
347+
defaultSolidityConfig:
348+
| SingleVersionSolidityUserConfig
349+
| MultiVersionSolidityUserConfig,
350+
): SingleVersionSolidityUserConfig | MultiVersionSolidityUserConfig {
351+
if ("version" in defaultSolidityConfig) {
352+
return {
353+
version: defaultSolidityConfig.version,
354+
};
355+
}
356+
357+
return {
358+
compilers: defaultSolidityConfig.compilers.map((c) => ({
359+
version: c.version,
360+
})),
361+
overrides: Object.fromEntries(
362+
Object.entries(defaultSolidityConfig.overrides ?? {}).map(
363+
([userSourceName, override]) => [
364+
userSourceName,
365+
{ version: override.version },
366+
],
367+
),
368+
),
369+
};
370+
}
371+
372+
// We use wasm builds in production to avoid using unofficial builds for deployments
373+
// This should change once https://github.com/ethereum/solidity/issues/11351 gets resolved
374+
export function shouldUseWasm(): boolean {
375+
return os.platform() === "linux" && os.arch() === "arm64";
376+
}

0 commit comments

Comments
 (0)