Skip to content

Commit 5124992

Browse files
authored
Merge pull request #7107 from NomicFoundation/compiler-selection-changes
Compiler selection changes
2 parents 713ef8c + a530dc0 commit 5124992

File tree

16 files changed

+380
-61
lines changed

16 files changed

+380
-61
lines changed

.changeset/nine-lies-press.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@nomicfoundation/hardhat-utils": patch
3+
"hardhat": patch
4+
---
5+
6+
Add preferWasm flag to config. Default to wasm on production profile. Support linux-aarch64 native compiler ([#5993](https://github.com/NomicFoundation/hardhat/issues/5993))

v-next/hardhat-utils/src/crypto.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { keccak256 as keccak256Impl } from "ethereum-cryptography/keccak";
2+
import { sha256 as sha256Impl } from "ethereum-cryptography/sha256";
23

34
/**
45
* Computes the Keccak-256 hash of the input bytes.
@@ -10,6 +11,16 @@ export async function keccak256(bytes: Uint8Array): Promise<Uint8Array> {
1011
return keccak256Impl(bytes);
1112
}
1213

14+
/**
15+
* Computes the SHA-256 hash of the input bytes.
16+
*
17+
* @param bytes The input bytes to hash.
18+
* @returns The SHA-256 hash of the input bytes.
19+
*/
20+
export async function sha256(bytes: Uint8Array): Promise<Uint8Array> {
21+
return sha256Impl(bytes);
22+
}
23+
1324
/**
1425
* Creates a non-cryptographic hash-based identifier for the given input.
1526
*

v-next/hardhat-verify/test/solc-versions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe("solc-versions", () => {
2828
},
2929
],
3030
overrides: {},
31+
preferWasm: false,
3132
};
3233
const expected = ["0.8.18", "0.7.2", "0.4.11"];
3334

@@ -50,6 +51,7 @@ describe("solc-versions", () => {
5051
settings: {},
5152
},
5253
},
54+
preferWasm: false,
5355
};
5456
const expected = ["0.5.5", "0.6.4"];
5557

@@ -81,6 +83,7 @@ describe("solc-versions", () => {
8183
settings: {},
8284
},
8385
},
86+
preferWasm: false,
8487
};
8588
const expected = ["0.8.18", "0.7.2", "0.5.5", "0.6.4"];
8689

@@ -104,6 +107,7 @@ describe("solc-versions", () => {
104107
settings: {},
105108
},
106109
},
110+
preferWasm: false,
107111
};
108112
const expected = ["0.8.18", "0.8.18"];
109113

@@ -131,6 +135,7 @@ describe("solc-versions", () => {
131135
settings: {},
132136
},
133137
},
138+
preferWasm: false,
134139
};
135140

136141
await assertRejectsWithHardhatError(
@@ -160,6 +165,7 @@ describe("solc-versions", () => {
160165
settings: {},
161166
},
162167
},
168+
preferWasm: false,
163169
};
164170

165171
await assertRejectsWithHardhatError(

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CompileCache } from "./cache.js";
2+
import type { Compiler } from "./compiler/compiler.js";
23
import type { DependencyGraphImplementation } from "./dependency-graph.js";
34
import type { Artifact } from "../../../../types/artifacts.js";
45
import type { SolcConfig, SolidityConfig } from "../../../../types/config.js";
@@ -13,6 +14,7 @@ import type {
1314
RunCompilationJobOptions,
1415
GetCompilationJobsResult,
1516
EmitArtifactsResult,
17+
RunCompilationJobResult,
1618
} from "../../../../types/solidity/build-system.js";
1719
import type { CompilationJob } from "../../../../types/solidity/compilation-job.js";
1820
import type {
@@ -76,6 +78,7 @@ interface CompilationResult {
7678
compilationJob: CompilationJob;
7779
compilerOutput: CompilerOutput;
7880
cached: boolean;
81+
compiler: Compiler;
7982
}
8083

8184
export interface SolidityBuildSystemOptions {
@@ -152,21 +155,19 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
152155
),
153156
);
154157

155-
const runCompilationJobOptions: RunCompilationJobOptions = {
156-
quiet: options?.quiet,
157-
};
158158
const results: CompilationResult[] = await pMap(
159159
runnableCompilationJobs,
160160
async (runnableCompilationJob) => {
161-
const compilerOutput = await this.runCompilationJob(
161+
const { output, compiler } = await this.runCompilationJob(
162162
runnableCompilationJob,
163-
runCompilationJobOptions,
163+
options,
164164
);
165165

166166
return {
167167
compilationJob: runnableCompilationJob,
168-
compilerOutput,
168+
compilerOutput: output,
169169
cached: false,
170+
compiler,
170171
};
171172
},
172173
{
@@ -301,22 +302,15 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
301302
readSourceFileFactory(this.#hooks),
302303
);
303304

304-
const buildProfileName = options?.buildProfile ?? DEFAULT_BUILD_PROFILE;
305-
306-
if (this.#options.solidityConfig.profiles[buildProfileName] === undefined) {
307-
throw new HardhatError(
308-
HardhatError.ERRORS.CORE.SOLIDITY.BUILD_PROFILE_NOT_FOUND,
309-
{
310-
buildProfileName,
311-
},
312-
);
313-
}
305+
const { buildProfileName, buildProfile } = this.#getBuildProfile(
306+
options?.buildProfile,
307+
);
314308

315309
log(`Using build profile ${buildProfileName}`);
316310

317311
const solcConfigSelector = new SolcConfigSelector(
318312
buildProfileName,
319-
this.#options.solidityConfig.profiles[buildProfileName],
313+
buildProfile,
320314
dependencyGraph,
321315
);
322316

@@ -340,15 +334,19 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
340334
subgraphsWithConfig.push([configOrError, subgraph]);
341335
}
342336

343-
// build version => longVersion map
337+
// get longVersion and isWasm from the compiler for each version
344338
const solcVersionToLongVersion = new Map<string, string>();
339+
const versionIsWasm = new Map<string, boolean>();
345340
for (const [solcConfig] of subgraphsWithConfig) {
346341
let solcLongVersion = solcVersionToLongVersion.get(solcConfig.version);
347342

348343
if (solcLongVersion === undefined) {
349-
const compiler = await getCompiler(solcConfig.version);
344+
const compiler = await getCompiler(solcConfig.version, {
345+
preferWasm: buildProfile.preferWasm,
346+
});
350347
solcLongVersion = compiler.longVersion;
351348
solcVersionToLongVersion.set(solcConfig.version, solcLongVersion);
349+
versionIsWasm.set(solcConfig.version, compiler.isSolcJs);
352350
}
353351
}
354352

@@ -394,13 +392,20 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
394392
for (const [rootFile, compilationJob] of indexedIndividualJobs.entries()) {
395393
const jobHash = await compilationJob.getBuildId();
396394
const cacheResult = this.#compileCache[rootFile];
395+
const isWasm = versionIsWasm.get(compilationJob.solcConfig.version);
396+
397+
assertHardhatInvariant(
398+
isWasm !== undefined,
399+
`Version ${compilationJob.solcConfig.version} not present in isWasm map`,
400+
);
397401

398402
// If there's no cache for the root file, or the compilation job changed, or using force flag, or isolated mode changed, compile it
399403
if (
400404
options?.force === true ||
401405
cacheResult === undefined ||
402406
cacheResult.jobHash !== jobHash ||
403-
cacheResult.isolated !== isolated
407+
cacheResult.isolated !== isolated ||
408+
cacheResult.wasm !== isWasm
404409
) {
405410
rootFilesToCompile.add(rootFile);
406411
continue;
@@ -506,10 +511,26 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
506511
return { compilationJobsPerFile, indexedIndividualJobs };
507512
}
508513

514+
#getBuildProfile(buildProfileName: string = DEFAULT_BUILD_PROFILE) {
515+
const buildProfile =
516+
this.#options.solidityConfig.profiles[buildProfileName];
517+
518+
if (buildProfile === undefined) {
519+
throw new HardhatError(
520+
HardhatError.ERRORS.CORE.SOLIDITY.BUILD_PROFILE_NOT_FOUND,
521+
{
522+
buildProfileName,
523+
},
524+
);
525+
}
526+
527+
return { buildProfileName, buildProfile };
528+
}
529+
509530
public async runCompilationJob(
510531
runnableCompilationJob: CompilationJob,
511532
options?: RunCompilationJobOptions,
512-
): Promise<CompilerOutput> {
533+
): Promise<RunCompilationJobResult> {
513534
await this.#downloadConfiguredCompilers(options?.quiet);
514535

515536
let numberOfFiles = 0;
@@ -520,8 +541,11 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
520541
const numberOfRootFiles =
521542
runnableCompilationJob.dependencyGraph.getRoots().size;
522543

544+
const { buildProfile } = this.#getBuildProfile(options?.buildProfile);
545+
523546
const compiler = await getCompiler(
524547
runnableCompilationJob.solcConfig.version,
548+
{ preferWasm: buildProfile.preferWasm },
525549
);
526550

527551
log(
@@ -533,7 +557,10 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
533557
"The long version of the compiler should match the long version of the compilation job",
534558
);
535559

536-
return compiler.compile(await runnableCompilationJob.getSolcInput());
560+
const output = await compiler.compile(
561+
await runnableCompilationJob.getSolcInput(),
562+
);
563+
return { output, compiler };
537564
}
538565

539566
public async remapCompilerError(
@@ -906,6 +933,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
906933
buildInfoPath: emitArtifactsResult.buildInfoPath,
907934
buildInfoOutputPath: emitArtifactsResult.buildInfoOutputPath,
908935
typeFilePath,
936+
wasm: result.compiler.isSolcJs,
909937
};
910938
}
911939
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface CompileCacheEntry {
2121
buildInfoOutputPath: string;
2222
artifactPaths: string[];
2323
typeFilePath: string;
24+
wasm: boolean;
2425
}
2526

2627
const CACHE_FILE_NAME = `compile-cache.json`;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ export class SolcJsCompiler implements Compiler {
137137
const compilerFileUrl = pathToFileURL(this.compilerPath);
138138
// NOTE: The script path passed to a tsx/esm loader is an exception to the
139139
// above rule since the tsx/esm loader doesn't support URLs with a scheme.
140-
if (scriptPath.endsWith(".ts")) {
141-
args.push(scriptPath);
142-
} else {
143-
args.push(scriptFileUrl);
144-
}
140+
args.push(scriptPath);
145141
args.push(compilerFileUrl.href);
146142
} else {
147143
args.push(scriptPath, this.compilerPath);

0 commit comments

Comments
 (0)