Skip to content

Commit 127d9f1

Browse files
committed
Implement PR feedback
1 parent 09d73d5 commit 127d9f1

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -139,29 +139,31 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
139139
const { compilationJobsPerFile, indexedIndividualJobs } =
140140
compilationJobsResult;
141141

142-
const compilationJobs = [...new Set(compilationJobsPerFile.values())];
142+
const runnableCompilationJobs = [
143+
...new Set(compilationJobsPerFile.values()),
144+
];
143145

144146
// NOTE: We precompute the build ids in parallel here, which are cached
145147
// internally in each compilation job
146148
await Promise.all(
147-
compilationJobs.map(async (compilationJob) =>
148-
compilationJob.getBuildId(),
149+
runnableCompilationJobs.map(async (runnableCompilationJob) =>
150+
runnableCompilationJob.getBuildId(),
149151
),
150152
);
151153

152154
const runCompilationJobOptions: RunCompilationJobOptions = {
153155
quiet: options?.quiet,
154156
};
155157
const results: CompilationResult[] = await pMap(
156-
compilationJobs,
157-
async (compilationJob) => {
158+
runnableCompilationJobs,
159+
async (runnableCompilationJob) => {
158160
const compilerOutput = await this.runCompilationJob(
159-
compilationJob,
161+
runnableCompilationJob,
160162
runCompilationJobOptions,
161163
);
162164

163165
return {
164-
compilationJob,
166+
compilationJob: runnableCompilationJob,
165167
compilerOutput,
166168
cached: false,
167169
};
@@ -276,7 +278,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
276278

277279
if (options?.quiet !== true) {
278280
if (isSuccessfulBuild) {
279-
await this.#printCompilationResult(compilationJobs);
281+
await this.#printCompilationResult(runnableCompilationJobs);
280282
}
281283
}
282284

@@ -348,7 +350,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
348350

349351
// build job for each root file. At this point subgraphsWithConfig are 1 root file each
350352
const indexedIndividualJobs: Map<string, CompilationJob> = new Map();
351-
const contentHashes = new Map<string, string>();
353+
const sharedContentHashes = new Map<string, string>();
352354
await Promise.all(
353355
subgraphsWithConfig.map(async ([config, subgraph]) => {
354356
const solcLongVersion = solcVersionToLongVersion.get(config.version);
@@ -358,15 +360,15 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
358360
"solcLongVersion should not be undefined",
359361
);
360362

361-
const compilationJob = new CompilationJobImplementation(
363+
const individualJob = new CompilationJobImplementation(
362364
subgraph,
363365
config,
364366
solcLongVersion,
365367
this.#hooks,
366-
contentHashes,
368+
sharedContentHashes,
367369
);
368370

369-
await compilationJob.getBuildId(); // precompute
371+
await individualJob.getBuildId(); // precompute
370372

371373
assertHardhatInvariant(
372374
subgraph.getRoots().size === 1,
@@ -375,7 +377,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
375377

376378
const rootFilePath = Array.from(subgraph.getRoots().keys())[0];
377379

378-
indexedIndividualJobs.set(rootFilePath, compilationJob);
380+
indexedIndividualJobs.set(rootFilePath, individualJob);
379381
}),
380382
);
381383

@@ -483,18 +485,18 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
483485
"solcLongVersion should not be undefined",
484486
);
485487

486-
const compilationJob = new CompilationJobImplementation(
488+
const runnableCompilationJob = new CompilationJobImplementation(
487489
subgraph,
488490
solcConfig,
489491
solcLongVersion,
490492
this.#hooks,
491-
contentHashes,
493+
sharedContentHashes,
492494
);
493495

494496
for (const [userSourceName, root] of subgraph.getRoots().entries()) {
495497
compilationJobsPerFile.set(
496498
formatRootPath(userSourceName, root),
497-
compilationJob,
499+
runnableCompilationJob,
498500
);
499501
}
500502
}
@@ -503,34 +505,37 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
503505
}
504506

505507
public async runCompilationJob(
506-
compilationJob: CompilationJob,
508+
runnableCompilationJob: CompilationJob,
507509
options?: RunCompilationJobOptions,
508510
): Promise<CompilerOutput> {
509511
await this.#downloadConfiguredCompilers(options?.quiet);
510512

511513
let numberOfFiles = 0;
512-
for (const _ of compilationJob.dependencyGraph.getAllFiles()) {
514+
for (const _ of runnableCompilationJob.dependencyGraph.getAllFiles()) {
513515
numberOfFiles++;
514516
}
515517

516-
const numberOfRootFiles = compilationJob.dependencyGraph.getRoots().size;
518+
const numberOfRootFiles =
519+
runnableCompilationJob.dependencyGraph.getRoots().size;
517520

518-
const compiler = await getCompiler(compilationJob.solcConfig.version);
521+
const compiler = await getCompiler(
522+
runnableCompilationJob.solcConfig.version,
523+
);
519524

520525
log(
521-
`Compiling ${numberOfRootFiles} root files and ${numberOfFiles - numberOfRootFiles} dependency files with solc ${compilationJob.solcConfig.version} using ${compiler.compilerPath}`,
526+
`Compiling ${numberOfRootFiles} root files and ${numberOfFiles - numberOfRootFiles} dependency files with solc ${runnableCompilationJob.solcConfig.version} using ${compiler.compilerPath}`,
522527
);
523528

524529
assertHardhatInvariant(
525-
compilationJob.solcLongVersion === compiler.longVersion,
530+
runnableCompilationJob.solcLongVersion === compiler.longVersion,
526531
"The long version of the compiler should match the long version of the compilation job",
527532
);
528533

529-
return compiler.compile(await compilationJob.getSolcInput());
534+
return compiler.compile(await runnableCompilationJob.getSolcInput());
530535
}
531536

532537
public async remapCompilerError(
533-
compilationJob: CompilationJob,
538+
runnableCompilationJob: CompilationJob,
534539
error: CompilerOutputError,
535540
shouldShortenPaths: boolean = false,
536541
): Promise<CompilerOutputError> {
@@ -544,7 +549,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
544549
/(-->\s+)([^\s:\n]+)/g,
545550
(_match, prefix, inputSourceName) => {
546551
const file =
547-
compilationJob.dependencyGraph.getFileByInputSourceName(
552+
runnableCompilationJob.dependencyGraph.getFileByInputSourceName(
548553
inputSourceName,
549554
);
550555

@@ -563,17 +568,17 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
563568
}
564569

565570
public async emitArtifacts(
566-
compilationJob: CompilationJob,
571+
runnableCompilationJob: CompilationJob,
567572
compilerOutput: CompilerOutput,
568573
): Promise<EmitArtifactsResult> {
569574
const artifactsPerFile = new Map<string, string[]>();
570575
const typeFilePaths = new Map<string, string>();
571-
const buildId = await compilationJob.getBuildId();
576+
const buildId = await runnableCompilationJob.getBuildId();
572577

573578
// We emit the artifacts for each root file, first emitting one artifact
574579
// for each contract, and then one declaration file for the entire file,
575580
// which defines their types and augments the ArtifactMap type.
576-
for (const [userSourceName, root] of compilationJob.dependencyGraph
581+
for (const [userSourceName, root] of runnableCompilationJob.dependencyGraph
577582
.getRoots()
578583
.entries()) {
579584
const fileFolder = path.join(this.#options.artifactsPath, userSourceName);
@@ -648,15 +653,15 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
648653
// concurrently, and keep their lifetimes separated and small.
649654
await Promise.all([
650655
(async () => {
651-
const buildInfo = await getBuildInfo(compilationJob);
656+
const buildInfo = await getBuildInfo(runnableCompilationJob);
652657

653658
// TODO: Maybe formatting the build info is slow, but it's mostly
654659
// strings, so it probably shouldn't be a problem.
655660
await writeJsonFile(buildInfoPath, buildInfo);
656661
})(),
657662
(async () => {
658663
const buildInfoOutput = await getBuildInfoOutput(
659-
compilationJob,
664+
runnableCompilationJob,
660665
compilerOutput,
661666
);
662667

@@ -923,13 +928,13 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
923928
}
924929
}
925930

926-
async #printCompilationResult(compilationJobs: CompilationJob[]) {
931+
async #printCompilationResult(runnableCompilationJobs: CompilationJob[]) {
927932
const jobsPerVersionAndEvmVersion = new Map<
928933
string,
929934
Map<string, CompilationJob[]>
930935
>();
931936

932-
for (const job of compilationJobs) {
937+
for (const job of runnableCompilationJobs) {
933938
const solcVersion = job.solcConfig.version;
934939
const solcInput = await job.getSolcInput();
935940
const evmVersion =

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import {
66
writeJsonFileAsStream,
77
} from "@nomicfoundation/hardhat-utils/fs";
88

9+
/**
10+
* This object is used to store what source files produced which output files
11+
* and what was the last compiled jobHash (buildId) for the individual root file
12+
* The keys are the root file paths, as passed to the compile task. For project
13+
* files this would be the user source names.
14+
*/
915
export type CompileCache = Record<string, CompileCacheEntry>;
1016

1117
export interface CompileCacheEntry {

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export class CompilationJobImplementation implements CompilationJob {
2525
public readonly solcLongVersion: string;
2626

2727
readonly #hooks: HookManager;
28-
readonly #contentHashes: Map<string, string>;
28+
// This map is shared across compilation jobs and is meant to store content hashes of source files
29+
// It is used to speed up the hashing of compilation jobs
30+
readonly #sharedContentHashes: Map<string, string>;
2931

3032
#buildId: string | undefined;
3133
#solcInput: CompilerInput | undefined;
@@ -35,13 +37,13 @@ export class CompilationJobImplementation implements CompilationJob {
3537
solcConfig: SolcConfig,
3638
solcLongVersion: string,
3739
hooks: HookManager,
38-
contentHashes: Map<string, string> = new Map(),
40+
sharedContentHashes: Map<string, string> = new Map(),
3941
) {
4042
this.dependencyGraph = dependencyGraph;
4143
this.solcConfig = solcConfig;
4244
this.solcLongVersion = solcLongVersion;
4345
this.#hooks = hooks;
44-
this.#contentHashes = contentHashes;
46+
this.#sharedContentHashes = sharedContentHashes;
4547
}
4648

4749
public async getSolcInput(): Promise<CompilerInput> {
@@ -197,15 +199,10 @@ export class CompilationJobImplementation implements CompilationJob {
197199
// the format of the BuildInfo type.
198200
const format: BuildInfo["_format"] = "hh3-sol-build-info-1";
199201

200-
// NOTE: Historically, we used the source content hashes instead of the full
201-
// source contents inside the solc input used to compute the build id here.
202-
// This was an optimization that sped up the build ID computation in a case
203-
// where multiple compilation jobs share some source files. We decided to
204-
// remove it once the code coverage was added because it simplified the
205-
// implementation and because we expect the caching logic to change.
206202
const solcInput = await this.getSolcInput();
207203
const smallerSolcInput = { ...solcInput };
208204

205+
// We replace the source files content with their hashes for speeding up the build id computation
209206
smallerSolcInput.sources = Object.fromEntries(
210207
Object.entries(solcInput.sources).map(([sourceName, _source]) => [
211208
sourceName,
@@ -227,13 +224,13 @@ export class CompilationJobImplementation implements CompilationJob {
227224
}
228225

229226
#getSourceContentHash(sourceName: string, text: string): any {
230-
let hash = this.#contentHashes.get(sourceName);
227+
let hash = this.#sharedContentHashes.get(sourceName);
231228

232229
if (hash !== undefined) {
233230
return hash;
234231
}
235232
hash = createHash("sha1").update(text).digest("hex");
236-
this.#contentHashes.set(sourceName, hash);
233+
this.#sharedContentHashes.set(sourceName, hash);
237234
return hash;
238235
}
239236
}

v-next/hardhat/src/types/solidity/build-system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export interface GetCompilationJobsResult {
173173
export interface EmitArtifactsResult {
174174
artifactsPerFile: ReadonlyMap<string, string[]>;
175175
buildInfoPath: string;
176-
typeFilePaths: Map<string, string>;
176+
typeFilePaths: ReadonlyMap<string, string>;
177177
buildInfoOutputPath: string;
178178
}
179179

0 commit comments

Comments
 (0)