Skip to content

Commit

Permalink
Merge 405c1fb into 57e97ae
Browse files Browse the repository at this point in the history
  • Loading branch information
kanej authored Jun 2, 2022
2 parents 57e97ae + 405c1fb commit 2c686e4
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 66 deletions.
60 changes: 58 additions & 2 deletions server/src/services/validation/worker/build/buildInputsToSolc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SolcBuild } from "hardhat/types";
import {
WorkerState,
BuildJob,
Expand All @@ -7,7 +8,7 @@ export interface SolcInput {
built: true;
solcVersion: string;
input: unknown;
compilationJob: unknown;
solcBuild: SolcBuild;
sourcePaths: string[];
}

Expand Down Expand Up @@ -59,11 +60,17 @@ export async function buildInputsToSolc(

const solcVersion = buildJob.context.compilationJob.getSolcConfig().version;

await getSolcBuild(workerState, buildJob, solcVersion);

if (isJobCancelled(buildJob)) {
return cancel(buildJob);
}

return {
built: true,
solcVersion,
input: buildJob.context.input,
compilationJob: buildJob.context.compilationJob,
solcBuild: buildJob.context.solcBuild,
sourcePaths: buildJob.context.sourcePaths ?? [],
};
}
Expand Down Expand Up @@ -186,6 +193,55 @@ async function getSolcInput(
return null;
}

/**
* The solc build subtask, this downloads the appropriate compiler
* for the given solc version, then checks the hash of the solc binary.
* As these checks are expensive, we cache in the workerState whether
* the download and check has already been done
* @param workerState the state shared between build jobs
* @param buildJob the container for the context of the build job
* @param solcVersion the solc compiler to download
* @returns a promise that the context has been populated
* with the compiler path details
*/
async function getSolcBuild(
{
hre,
tasks: { TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD },
compilerMetadataCache,
}: WorkerState,
{ context }: BuildJob,
solcVersion: string
) {
try {
const cachedBuildVersionPromise = compilerMetadataCache[solcVersion];

if (cachedBuildVersionPromise !== undefined) {
const cachedSolcBuild = await cachedBuildVersionPromise;

context.solcBuild = cachedSolcBuild;

return;
}

const solcBuildPromise = hre.run(TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, {
quiet: true,
solcVersion,
});

compilerMetadataCache[solcVersion] = solcBuildPromise;

const solcBuild: SolcBuild = await solcBuildPromise;

context.solcBuild = solcBuild;
} catch (err) {
// remove the cached promise on build task failure
delete compilerMetadataCache[solcVersion];

throw err;
}
}

function cancel({ jobId, projectBasePath }: BuildJob): {
built: false;
result: ValidationCompleteMessage;
Expand Down
33 changes: 22 additions & 11 deletions server/src/services/validation/worker/build/solcCompile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SolcBuild } from "hardhat/types";
import { HardhatCompilerError, WorkerState } from "../../../../types";
import { SolcInput } from "./buildInputsToSolc";

Expand All @@ -8,18 +9,28 @@ export interface SolcResult {
[key: string]: unknown;
};
};
solcBuild: SolcBuild;
}

export function solcCompile(
{ hre, tasks: { TASK_COMPILE_SOLIDITY_COMPILE } }: WorkerState,
{ solcVersion, input, compilationJob }: SolcInput
export async function solcCompile(
{
hre,
tasks: { TASK_COMPILE_SOLIDITY_RUN_SOLCJS, TASK_COMPILE_SOLIDITY_RUN_SOLC },
}: WorkerState,
{ input, solcBuild }: SolcInput
): Promise<SolcResult> {
return hre.run(TASK_COMPILE_SOLIDITY_COMPILE, {
solcVersion,
input,
quiet: true,
compilationJob,
compilationJobs: [compilationJob],
compilationJobIndex: 0,
});
let output;
if (solcBuild.isSolcJs) {
output = await hre.run(TASK_COMPILE_SOLIDITY_RUN_SOLCJS, {
input,
solcJsPath: solcBuild.compilerPath,
});
} else {
output = await hre.run(TASK_COMPILE_SOLIDITY_RUN_SOLC, {
input,
solcPath: solcBuild.compilerPath,
});
}

return { output, solcBuild };
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export async function initialiseWorkerState(
TASK_COMPILE_SOLIDITY_GET_DEPENDENCY_GRAPH,
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT,
TASK_COMPILE_SOLIDITY_COMPILE,
TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD,
TASK_COMPILE_SOLIDITY_RUN_SOLCJS,
TASK_COMPILE_SOLIDITY_RUN_SOLC,
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require(`${hardhatBase}/builtin-tasks/task-names`);

Expand All @@ -59,8 +61,11 @@ export async function initialiseWorkerState(
TASK_COMPILE_SOLIDITY_GET_DEPENDENCY_GRAPH,
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT,
TASK_COMPILE_SOLIDITY_COMPILE,
TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD,
TASK_COMPILE_SOLIDITY_RUN_SOLCJS,
TASK_COMPILE_SOLIDITY_RUN_SOLC,
},
compilerMetadataCache: {},
send,
logger,
};
Expand Down
52 changes: 30 additions & 22 deletions server/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Connection } from "vscode-languageserver";
import * as childProcess from "child_process";
import { TextDocuments } from "vscode-languageserver/node";
import { TextDocument } from "vscode-languageserver-textdocument";
import { Logger } from "@utils/Logger";
import { WorkspaceFolder } from "vscode-languageserver-protocol";
import { SolFileIndexMap, SolProjectMap, Diagnostic } from "@common/types";
import { HardhatProject } from "@analyzer/HardhatProject";
import { Telemetry } from "./telemetry/types";
import type { Connection } from "vscode-languageserver";
import type { Serializable } from "child_process";
import type { TextDocuments } from "vscode-languageserver/node";
import type { TextDocument } from "vscode-languageserver-textdocument";
import type { Logger } from "@utils/Logger";
import type { WorkspaceFolder } from "vscode-languageserver-protocol";
import type { SolFileIndexMap, SolProjectMap, Diagnostic } from "@common/types";
import type { HardhatProject } from "@analyzer/HardhatProject";
import type { SolcBuild } from "hardhat/types";
import type { Telemetry } from "./telemetry/types";

export type CancelResolver = (diagnostics: {
[key: string]: Diagnostic[];
Expand All @@ -19,7 +20,7 @@ export interface CompilerProcess {
solidityCompilePromise: Promise<unknown>;
};

send: (message: childProcess.Serializable) => void;
send: (message: Serializable) => void;
kill: () => void;
}

Expand Down Expand Up @@ -87,6 +88,8 @@ export interface BuildContext {
compilationJob?: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input?: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
solcBuild?: any;
}

export interface BuildJob {
Expand Down Expand Up @@ -134,20 +137,25 @@ export interface WorkerState {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
SolidityFilesCache: any;
tasks: {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS: any;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
TASK_COMPILE_SOLIDITY_GET_SOURCE_NAMES: any;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
TASK_COMPILE_SOLIDITY_GET_DEPENDENCY_GRAPH: any;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE: any;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT: any;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
TASK_COMPILE_SOLIDITY_COMPILE: any;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_GET_SOURCE_NAMES: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_GET_DEPENDENCY_GRAPH: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_RUN_SOLCJS: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
TASK_COMPILE_SOLIDITY_RUN_SOLC: string;
};
send: (message: ValidationCompleteMessage) => Promise<void>;
compilerMetadataCache: { [key: string]: Promise<SolcBuild> };
logger: WorkerLogger;
}

Expand Down
Loading

0 comments on commit 2c686e4

Please sign in to comment.